/*1* Copyright © 2016 Mozilla Foundation2*3* This program is made available under an ISC-style license. See the4* accompanying file LICENSE for details.5*/67#if !defined(CUBEB_UTILS_WIN)8#define CUBEB_UTILS_WIN910#include "cubeb-internal.h"11#include <windows.h>1213/* This wraps an SRWLock to track the owner in debug mode, adapted from14NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx15*/16class owned_critical_section {17public:18owned_critical_section()19: srwlock(SRWLOCK_INIT)20#ifndef NDEBUG21,22owner(0)23#endif24{25}2627void lock()28{29AcquireSRWLockExclusive(&srwlock);30#ifndef NDEBUG31XASSERT(owner != GetCurrentThreadId() && "recursive locking");32owner = GetCurrentThreadId();33#endif34}3536void unlock()37{38#ifndef NDEBUG39/* GetCurrentThreadId cannot return 0: it is not a the valid thread id */40owner = 0;41#endif42ReleaseSRWLockExclusive(&srwlock);43}4445/* This is guaranteed to have the good behaviour if it succeeds. The behaviour46is undefined otherwise. */47void assert_current_thread_owns()48{49#ifndef NDEBUG50/* This implies owner != 0, because GetCurrentThreadId cannot return 0. */51XASSERT(owner == GetCurrentThreadId());52#endif53}5455private:56SRWLOCK srwlock;57#ifndef NDEBUG58DWORD owner;59#endif6061// Disallow copy and assignment because SRWLock cannot be copied.62owned_critical_section(const owned_critical_section &);63owned_critical_section & operator=(const owned_critical_section &);64};6566#endif /* CUBEB_UTILS_WIN */676869