Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Windows/Synchronization.h |
michael@0 | 2 | |
michael@0 | 3 | #ifndef __WINDOWS_SYNCHRONIZATION_H |
michael@0 | 4 | #define __WINDOWS_SYNCHRONIZATION_H |
michael@0 | 5 | |
michael@0 | 6 | #include "Defs.h" |
michael@0 | 7 | #include "Handle.h" |
michael@0 | 8 | |
michael@0 | 9 | namespace NWindows { |
michael@0 | 10 | namespace NSynchronization { |
michael@0 | 11 | |
michael@0 | 12 | class CObject: public CHandle |
michael@0 | 13 | { |
michael@0 | 14 | public: |
michael@0 | 15 | bool Lock(DWORD timeoutInterval = INFINITE) |
michael@0 | 16 | { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0); } |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | class CBaseEvent: public CObject |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | bool Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, |
michael@0 | 23 | LPSECURITY_ATTRIBUTES securityAttributes = NULL) |
michael@0 | 24 | { |
michael@0 | 25 | _handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset), |
michael@0 | 26 | BoolToBOOL(initiallyOwn), name); |
michael@0 | 27 | return (_handle != 0); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) |
michael@0 | 31 | { |
michael@0 | 32 | _handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); |
michael@0 | 33 | return (_handle != 0); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | bool Set() { return BOOLToBool(::SetEvent(_handle)); } |
michael@0 | 37 | bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } |
michael@0 | 38 | bool Reset() { return BOOLToBool(::ResetEvent(_handle)); } |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | class CEvent: public CBaseEvent |
michael@0 | 42 | { |
michael@0 | 43 | public: |
michael@0 | 44 | CEvent() {}; |
michael@0 | 45 | CEvent(bool manualReset, bool initiallyOwn, |
michael@0 | 46 | LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES securityAttributes = NULL); |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | class CManualResetEvent: public CEvent |
michael@0 | 50 | { |
michael@0 | 51 | public: |
michael@0 | 52 | CManualResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, |
michael@0 | 53 | LPSECURITY_ATTRIBUTES securityAttributes = NULL): |
michael@0 | 54 | CEvent(true, initiallyOwn, name, securityAttributes) {}; |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | class CAutoResetEvent: public CEvent |
michael@0 | 58 | { |
michael@0 | 59 | public: |
michael@0 | 60 | CAutoResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, |
michael@0 | 61 | LPSECURITY_ATTRIBUTES securityAttributes = NULL): |
michael@0 | 62 | CEvent(false, initiallyOwn, name, securityAttributes) {}; |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | class CMutex: public CObject |
michael@0 | 66 | { |
michael@0 | 67 | public: |
michael@0 | 68 | bool Create(bool initiallyOwn, LPCTSTR name = NULL, |
michael@0 | 69 | LPSECURITY_ATTRIBUTES securityAttributes = NULL) |
michael@0 | 70 | { |
michael@0 | 71 | _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name); |
michael@0 | 72 | return (_handle != 0); |
michael@0 | 73 | } |
michael@0 | 74 | bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) |
michael@0 | 75 | { |
michael@0 | 76 | _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); |
michael@0 | 77 | return (_handle != 0); |
michael@0 | 78 | } |
michael@0 | 79 | bool Release() { return BOOLToBool(::ReleaseMutex(_handle)); } |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | class CMutexLock |
michael@0 | 83 | { |
michael@0 | 84 | CMutex &_object; |
michael@0 | 85 | public: |
michael@0 | 86 | CMutexLock(CMutex &object): _object(object) { _object.Lock(); } |
michael@0 | 87 | ~CMutexLock() { _object.Release(); } |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | class CCriticalSection |
michael@0 | 91 | { |
michael@0 | 92 | CRITICAL_SECTION _object; |
michael@0 | 93 | // void Initialize() { ::InitializeCriticalSection(&_object); } |
michael@0 | 94 | // void Delete() { ::DeleteCriticalSection(&_object); } |
michael@0 | 95 | public: |
michael@0 | 96 | CCriticalSection() { ::InitializeCriticalSection(&_object); } |
michael@0 | 97 | ~CCriticalSection() { ::DeleteCriticalSection(&_object); } |
michael@0 | 98 | void Enter() { ::EnterCriticalSection(&_object); } |
michael@0 | 99 | void Leave() { ::LeaveCriticalSection(&_object); } |
michael@0 | 100 | }; |
michael@0 | 101 | |
michael@0 | 102 | class CCriticalSectionLock |
michael@0 | 103 | { |
michael@0 | 104 | CCriticalSection &_object; |
michael@0 | 105 | void Unlock() { _object.Leave(); } |
michael@0 | 106 | public: |
michael@0 | 107 | CCriticalSectionLock(CCriticalSection &object): _object(object) |
michael@0 | 108 | {_object.Enter(); } |
michael@0 | 109 | ~CCriticalSectionLock() { Unlock(); } |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | }} |
michael@0 | 113 | |
michael@0 | 114 | #endif |