1 // Windows/Synchronization.h
3 #ifndef __WINDOWS_SYNCHRONIZATION_H
4 #define __WINDOWS_SYNCHRONIZATION_H
10 #include "../../C/Threads.h"
18 namespace NSynchronization {
25 bool IsCreated() { return Event_IsCreated(&_object) != 0; }
26 operator HANDLE() { return _object.handle; }
27 CBaseEvent() { Event_Construct(&_object); }
28 ~CBaseEvent() { Close(); }
29 HRes Close() { return Event_Close(&_object); }
31 HRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL,
32 LPSECURITY_ATTRIBUTES securityAttributes = NULL)
34 _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset),
35 BoolToBOOL(initiallyOwn), name);
36 if (_object.handle != 0)
38 return ::GetLastError();
40 HRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
42 _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name);
43 if (_object.handle != 0)
45 return ::GetLastError();
49 HRes Set() { return Event_Set(&_object); }
50 // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); }
51 HRes Reset() { return Event_Reset(&_object); }
52 HRes Lock() { return Event_Wait(&_object); }
55 class CManualResetEvent: public CBaseEvent
58 HRes Create(bool initiallyOwn = false)
60 return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0);
62 HRes CreateIfNotCreated()
66 return ManualResetEvent_CreateNotSignaled(&_object);
69 HRes CreateWithName(bool initiallyOwn, LPCTSTR name)
71 return CBaseEvent::Create(true, initiallyOwn, name);
76 class CAutoResetEvent: public CBaseEvent
81 return AutoResetEvent_CreateNotSignaled(&_object);
83 HRes CreateIfNotCreated()
87 return AutoResetEvent_CreateNotSignaled(&_object);
92 class CObject: public CHandle
95 HRes Lock(DWORD timeoutInterval = INFINITE)
96 { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); }
98 class CMutex: public CObject
101 HRes Create(bool initiallyOwn, LPCTSTR name = NULL,
102 LPSECURITY_ATTRIBUTES securityAttributes = NULL)
104 _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name);
107 return ::GetLastError();
109 HRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
111 _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name);
114 return ::GetLastError();
118 return ::ReleaseMutex(_handle) ? 0 : ::GetLastError();
125 CMutexLock(CMutex &object): _object(&object) { _object->Lock(); }
126 ~CMutexLock() { _object->Release(); }
132 ::CSemaphore _object;
134 CSemaphore() { Semaphore_Construct(&_object); }
135 ~CSemaphore() { Close(); }
136 HRes Close() { return Semaphore_Close(&_object); }
137 operator HANDLE() { return _object.handle; }
138 HRes Create(UInt32 initiallyCount, UInt32 maxCount)
140 return Semaphore_Create(&_object, initiallyCount, maxCount);
142 HRes Release() { return Semaphore_Release1(&_object); }
143 HRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); }
144 HRes Lock() { return Semaphore_Wait(&_object); }
147 class CCriticalSection
149 ::CCriticalSection _object;
151 CCriticalSection() { CriticalSection_Init(&_object); }
152 ~CCriticalSection() { CriticalSection_Delete(&_object); }
153 void Enter() { CriticalSection_Enter(&_object); }
154 void Leave() { CriticalSection_Leave(&_object); }
157 class CCriticalSectionLock
159 CCriticalSection *_object;
160 void Unlock() { _object->Leave(); }
162 CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); }
163 ~CCriticalSectionLock() { Unlock(); }