1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Windows/Synchronization.h 2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef __WINDOWS_SYNCHRONIZATION_H 4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define __WINDOWS_SYNCHRONIZATION_H 5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../C/Threads.h" 7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "Defs.h" 9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef _WIN32 11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "Handle.h" 12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif 13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 14baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NWindows { 15baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NSynchronization { 16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 17baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CBaseEvent 18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 19baa3858d3f5d128a5c8466b700098109edcad5f2repo syncprotected: 20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ::CEvent _object; 21baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync bool IsCreated() { return Event_IsCreated(&_object) != 0; } 23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync operator HANDLE() { return _object; } 24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CBaseEvent() { Event_Construct(&_object); } 25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ~CBaseEvent() { Close(); } 26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Close() { return Event_Close(&_object); } 27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #ifdef _WIN32 28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES sa = NULL) 29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync _object = ::CreateEvent(sa, BoolToBOOL(manualReset), BoolToBOOL(initiallyOwn), name); 31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (name == NULL && _object != 0) 32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ::GetLastError(); 34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) 36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync _object = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); 38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (_object != 0) 39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ::GetLastError(); 41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Set() { return Event_Set(&_object); } 45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } 46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Reset() { return Event_Reset(&_object); } 47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Lock() { return Event_Wait(&_object); } 48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 50baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CManualResetEvent: public CBaseEvent 51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 52baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Create(bool initiallyOwn = false) 54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0); 56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes CreateIfNotCreated() 58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (IsCreated()) 60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ManualResetEvent_CreateNotSignaled(&_object); 62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #ifdef _WIN32 64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes CreateWithName(bool initiallyOwn, LPCTSTR name) 65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return CBaseEvent::Create(true, initiallyOwn, name); 67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 71baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CAutoResetEvent: public CBaseEvent 72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 73baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Create() 75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return AutoResetEvent_CreateNotSignaled(&_object); 77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes CreateIfNotCreated() 79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (IsCreated()) 81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return AutoResetEvent_CreateNotSignaled(&_object); 83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifdef _WIN32 87baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CObject: public CHandle 88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 89baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Lock(DWORD timeoutInterval = INFINITE) 91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); } 92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 93baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CMutex: public CObject 94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 95baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Create(bool initiallyOwn, LPCTSTR name = NULL, LPSECURITY_ATTRIBUTES sa = NULL) 97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync _handle = ::CreateMutex(sa, BoolToBOOL(initiallyOwn), name); 99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (name == NULL && _handle != 0) 100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ::GetLastError(); 102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #ifndef UNDER_CE 104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) 105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); 107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync if (_handle != 0) 108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return 0; 109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ::GetLastError(); 110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync #endif 112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Release() 113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return ::ReleaseMutex(_handle) ? 0 : ::GetLastError(); 115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 117baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CMutexLock 118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CMutex *_object; 120baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CMutexLock(CMutex &object): _object(&object) { _object->Lock(); } 122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ~CMutexLock() { _object->Release(); } 123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif 125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 126baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CSemaphore 127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ::CSemaphore _object; 129baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CSemaphore() { Semaphore_Construct(&_object); } 131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ~CSemaphore() { Close(); } 132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Close() { return Semaphore_Close(&_object); } 133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync operator HANDLE() { return _object; } 134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Create(UInt32 initiallyCount, UInt32 maxCount) 135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync { 136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync return Semaphore_Create(&_object, initiallyCount, maxCount); 137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync } 138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Release() { return Semaphore_Release1(&_object); } 139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); } 140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync WRes Lock() { return Semaphore_Wait(&_object); } 141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 143baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CCriticalSection 144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ::CCriticalSection _object; 146baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CCriticalSection() { CriticalSection_Init(&_object); } 148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ~CCriticalSection() { CriticalSection_Delete(&_object); } 149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync void Enter() { CriticalSection_Enter(&_object); } 150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync void Leave() { CriticalSection_Leave(&_object); } 151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 153baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass CCriticalSectionLock 154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{ 155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CCriticalSection *_object; 156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync void Unlock() { _object->Leave(); } 157baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); } 159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync ~CCriticalSectionLock() { Unlock(); } 160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}; 161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}} 163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif 165