cubeb_utils_win.h (1542B)
1 /* 2 * Copyright © 2016 Mozilla Foundation 3 * 4 * This program is made available under an ISC-style license. See the 5 * accompanying file LICENSE for details. 6 */ 7 8 #if !defined(CUBEB_UTILS_WIN) 9 #define CUBEB_UTILS_WIN 10 11 #include "cubeb-internal.h" 12 #include <windows.h> 13 14 /* This wraps an SRWLock to track the owner in debug mode, adapted from 15 NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx 16 */ 17 class owned_critical_section { 18 public: 19 owned_critical_section() 20 : srwlock(SRWLOCK_INIT) 21 #ifndef NDEBUG 22 , 23 owner(0) 24 #endif 25 { 26 } 27 28 void lock() 29 { 30 AcquireSRWLockExclusive(&srwlock); 31 #ifndef NDEBUG 32 XASSERT(owner != GetCurrentThreadId() && "recursive locking"); 33 owner = GetCurrentThreadId(); 34 #endif 35 } 36 37 void unlock() 38 { 39 #ifndef NDEBUG 40 /* GetCurrentThreadId cannot return 0: it is not a the valid thread id */ 41 owner = 0; 42 #endif 43 ReleaseSRWLockExclusive(&srwlock); 44 } 45 46 /* This is guaranteed to have the good behaviour if it succeeds. The behaviour 47 is undefined otherwise. */ 48 void assert_current_thread_owns() 49 { 50 #ifndef NDEBUG 51 /* This implies owner != 0, because GetCurrentThreadId cannot return 0. */ 52 XASSERT(owner == GetCurrentThreadId()); 53 #endif 54 } 55 56 private: 57 SRWLOCK srwlock; 58 #ifndef NDEBUG 59 DWORD owner; 60 #endif 61 62 // Disallow copy and assignment because SRWLock cannot be copied. 63 owned_critical_section(const owned_critical_section &); 64 owned_critical_section & operator=(const owned_critical_section &); 65 }; 66 67 #endif /* CUBEB_UTILS_WIN */