michael@0: // Windows/Synchronization.h michael@0: michael@0: #ifndef __WINDOWS_SYNCHRONIZATION_H michael@0: #define __WINDOWS_SYNCHRONIZATION_H michael@0: michael@0: #include "Defs.h" michael@0: #include "Handle.h" michael@0: michael@0: namespace NWindows { michael@0: namespace NSynchronization { michael@0: michael@0: class CObject: public CHandle michael@0: { michael@0: public: michael@0: bool Lock(DWORD timeoutInterval = INFINITE) michael@0: { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0); } michael@0: }; michael@0: michael@0: class CBaseEvent: public CObject michael@0: { michael@0: public: michael@0: bool Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, michael@0: LPSECURITY_ATTRIBUTES securityAttributes = NULL) michael@0: { michael@0: _handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset), michael@0: BoolToBOOL(initiallyOwn), name); michael@0: return (_handle != 0); michael@0: } michael@0: michael@0: bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) michael@0: { michael@0: _handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); michael@0: return (_handle != 0); michael@0: } michael@0: michael@0: bool Set() { return BOOLToBool(::SetEvent(_handle)); } michael@0: bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } michael@0: bool Reset() { return BOOLToBool(::ResetEvent(_handle)); } michael@0: }; michael@0: michael@0: class CEvent: public CBaseEvent michael@0: { michael@0: public: michael@0: CEvent() {}; michael@0: CEvent(bool manualReset, bool initiallyOwn, michael@0: LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES securityAttributes = NULL); michael@0: }; michael@0: michael@0: class CManualResetEvent: public CEvent michael@0: { michael@0: public: michael@0: CManualResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, michael@0: LPSECURITY_ATTRIBUTES securityAttributes = NULL): michael@0: CEvent(true, initiallyOwn, name, securityAttributes) {}; michael@0: }; michael@0: michael@0: class CAutoResetEvent: public CEvent michael@0: { michael@0: public: michael@0: CAutoResetEvent(bool initiallyOwn = false, LPCTSTR name = NULL, michael@0: LPSECURITY_ATTRIBUTES securityAttributes = NULL): michael@0: CEvent(false, initiallyOwn, name, securityAttributes) {}; michael@0: }; michael@0: michael@0: class CMutex: public CObject michael@0: { michael@0: public: michael@0: bool Create(bool initiallyOwn, LPCTSTR name = NULL, michael@0: LPSECURITY_ATTRIBUTES securityAttributes = NULL) michael@0: { michael@0: _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name); michael@0: return (_handle != 0); michael@0: } michael@0: bool Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) michael@0: { michael@0: _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); michael@0: return (_handle != 0); michael@0: } michael@0: bool Release() { return BOOLToBool(::ReleaseMutex(_handle)); } michael@0: }; michael@0: michael@0: class CMutexLock michael@0: { michael@0: CMutex &_object; michael@0: public: michael@0: CMutexLock(CMutex &object): _object(object) { _object.Lock(); } michael@0: ~CMutexLock() { _object.Release(); } michael@0: }; michael@0: michael@0: class CCriticalSection michael@0: { michael@0: CRITICAL_SECTION _object; michael@0: // void Initialize() { ::InitializeCriticalSection(&_object); } michael@0: // void Delete() { ::DeleteCriticalSection(&_object); } michael@0: public: michael@0: CCriticalSection() { ::InitializeCriticalSection(&_object); } michael@0: ~CCriticalSection() { ::DeleteCriticalSection(&_object); } michael@0: void Enter() { ::EnterCriticalSection(&_object); } michael@0: void Leave() { ::LeaveCriticalSection(&_object); } michael@0: }; michael@0: michael@0: class CCriticalSectionLock michael@0: { michael@0: CCriticalSection &_object; michael@0: void Unlock() { _object.Leave(); } michael@0: public: michael@0: CCriticalSectionLock(CCriticalSection &object): _object(object) michael@0: {_object.Enter(); } michael@0: ~CCriticalSectionLock() { Unlock(); } michael@0: }; michael@0: michael@0: }} michael@0: michael@0: #endif