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