rlm@1: // Windows/Synchronization.h rlm@1: rlm@1: #ifndef __WINDOWS_SYNCHRONIZATION_H rlm@1: #define __WINDOWS_SYNCHRONIZATION_H rlm@1: rlm@1: #include "Defs.h" rlm@1: rlm@1: extern "C" rlm@1: { rlm@1: #include "../../C/Threads.h" rlm@1: } rlm@1: rlm@1: #ifdef _WIN32 rlm@1: #include "Handle.h" rlm@1: #endif rlm@1: rlm@1: namespace NWindows { rlm@1: namespace NSynchronization { rlm@1: rlm@1: class CBaseEvent rlm@1: { rlm@1: protected: rlm@1: ::CEvent _object; rlm@1: public: rlm@1: bool IsCreated() { return Event_IsCreated(&_object) != 0; } rlm@1: operator HANDLE() { return _object.handle; } rlm@1: CBaseEvent() { Event_Construct(&_object); } rlm@1: ~CBaseEvent() { Close(); } rlm@1: WRes Close() { return Event_Close(&_object); } rlm@1: #ifdef _WIN32 rlm@1: WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, rlm@1: LPSECURITY_ATTRIBUTES securityAttributes = NULL) rlm@1: { rlm@1: _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset), rlm@1: BoolToBOOL(initiallyOwn), name); rlm@1: if (_object.handle != 0) rlm@1: return 0; rlm@1: return ::GetLastError(); rlm@1: } rlm@1: WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) rlm@1: { rlm@1: _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); rlm@1: if (_object.handle != 0) rlm@1: return 0; rlm@1: return ::GetLastError(); rlm@1: } rlm@1: #endif rlm@1: rlm@1: WRes Set() { return Event_Set(&_object); } rlm@1: // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } rlm@1: WRes Reset() { return Event_Reset(&_object); } rlm@1: WRes Lock() { return Event_Wait(&_object); } rlm@1: }; rlm@1: rlm@1: class CManualResetEvent: public CBaseEvent rlm@1: { rlm@1: public: rlm@1: WRes Create(bool initiallyOwn = false) rlm@1: { rlm@1: return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0); rlm@1: } rlm@1: WRes CreateIfNotCreated() rlm@1: { rlm@1: if (IsCreated()) rlm@1: return 0; rlm@1: return ManualResetEvent_CreateNotSignaled(&_object); rlm@1: } rlm@1: #ifdef _WIN32 rlm@1: WRes CreateWithName(bool initiallyOwn, LPCTSTR name) rlm@1: { rlm@1: return CBaseEvent::Create(true, initiallyOwn, name); rlm@1: } rlm@1: #endif rlm@1: }; rlm@1: rlm@1: class CAutoResetEvent: public CBaseEvent rlm@1: { rlm@1: public: rlm@1: WRes Create() rlm@1: { rlm@1: return AutoResetEvent_CreateNotSignaled(&_object); rlm@1: } rlm@1: WRes CreateIfNotCreated() rlm@1: { rlm@1: if (IsCreated()) rlm@1: return 0; rlm@1: return AutoResetEvent_CreateNotSignaled(&_object); rlm@1: } rlm@1: }; rlm@1: rlm@1: #ifdef _WIN32 rlm@1: class CObject: public CHandle rlm@1: { rlm@1: public: rlm@1: WRes Lock(DWORD timeoutInterval = INFINITE) rlm@1: { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); } rlm@1: }; rlm@1: class CMutex: public CObject rlm@1: { rlm@1: public: rlm@1: WRes Create(bool initiallyOwn, LPCTSTR name = NULL, rlm@1: LPSECURITY_ATTRIBUTES securityAttributes = NULL) rlm@1: { rlm@1: _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name); rlm@1: if (_handle != 0) rlm@1: return 0; rlm@1: return ::GetLastError(); rlm@1: } rlm@1: WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) rlm@1: { rlm@1: _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); rlm@1: if (_handle != 0) rlm@1: return 0; rlm@1: return ::GetLastError(); rlm@1: } rlm@1: WRes Release() rlm@1: { rlm@1: return ::ReleaseMutex(_handle) ? 0 : ::GetLastError(); rlm@1: } rlm@1: }; rlm@1: class CMutexLock rlm@1: { rlm@1: CMutex *_object; rlm@1: public: rlm@1: CMutexLock(CMutex &object): _object(&object) { _object->Lock(); } rlm@1: ~CMutexLock() { _object->Release(); } rlm@1: }; rlm@1: #endif rlm@1: rlm@1: class CSemaphore rlm@1: { rlm@1: ::CSemaphore _object; rlm@1: public: rlm@1: CSemaphore() { Semaphore_Construct(&_object); } rlm@1: ~CSemaphore() { Close(); } rlm@1: WRes Close() { return Semaphore_Close(&_object); } rlm@1: operator HANDLE() { return _object.handle; } rlm@1: WRes Create(UInt32 initiallyCount, UInt32 maxCount) rlm@1: { rlm@1: return Semaphore_Create(&_object, initiallyCount, maxCount); rlm@1: } rlm@1: WRes Release() { return Semaphore_Release1(&_object); } rlm@1: WRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); } rlm@1: WRes Lock() { return Semaphore_Wait(&_object); } rlm@1: }; rlm@1: rlm@1: class CCriticalSection rlm@1: { rlm@1: ::CCriticalSection _object; rlm@1: public: rlm@1: CCriticalSection() { CriticalSection_Init(&_object); } rlm@1: ~CCriticalSection() { CriticalSection_Delete(&_object); } rlm@1: void Enter() { CriticalSection_Enter(&_object); } rlm@1: void Leave() { CriticalSection_Leave(&_object); } rlm@1: }; rlm@1: rlm@1: class CCriticalSectionLock rlm@1: { rlm@1: CCriticalSection *_object; rlm@1: void Unlock() { _object->Leave(); } rlm@1: public: rlm@1: CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); } rlm@1: ~CCriticalSectionLock() { Unlock(); } rlm@1: }; rlm@1: rlm@1: }} rlm@1: rlm@1: #endif