Mercurial > vba-linux
view src/win32/7zip/7z/CPP/Windows/Synchronization.h @ 5:8fe0c57e53d2
concentrating on lua first
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:39:40 -0600 |
parents | f9f4f1b99eed |
children |
line wrap: on
line source
1 // Windows/Synchronization.h3 #ifndef __WINDOWS_SYNCHRONIZATION_H4 #define __WINDOWS_SYNCHRONIZATION_H6 #include "Defs.h"8 extern "C"9 {10 #include "../../C/Threads.h"11 }13 #ifdef _WIN3214 #include "Handle.h"15 #endif17 namespace NWindows {18 namespace NSynchronization {20 class CBaseEvent21 {22 protected:23 ::CEvent _object;24 public:25 bool IsCreated() { return Event_IsCreated(&_object) != 0; }26 operator HANDLE() { return _object.handle; }27 CBaseEvent() { Event_Construct(&_object); }28 ~CBaseEvent() { Close(); }29 WRes Close() { return Event_Close(&_object); }30 #ifdef _WIN3231 WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL,32 LPSECURITY_ATTRIBUTES securityAttributes = NULL)33 {34 _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset),35 BoolToBOOL(initiallyOwn), name);36 if (_object.handle != 0)37 return 0;38 return ::GetLastError();39 }40 WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)41 {42 _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name);43 if (_object.handle != 0)44 return 0;45 return ::GetLastError();46 }47 #endif49 WRes Set() { return Event_Set(&_object); }50 // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); }51 WRes Reset() { return Event_Reset(&_object); }52 WRes Lock() { return Event_Wait(&_object); }53 };55 class CManualResetEvent: public CBaseEvent56 {57 public:58 WRes Create(bool initiallyOwn = false)59 {60 return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0);61 }62 WRes CreateIfNotCreated()63 {64 if (IsCreated())65 return 0;66 return ManualResetEvent_CreateNotSignaled(&_object);67 }68 #ifdef _WIN3269 WRes CreateWithName(bool initiallyOwn, LPCTSTR name)70 {71 return CBaseEvent::Create(true, initiallyOwn, name);72 }73 #endif74 };76 class CAutoResetEvent: public CBaseEvent77 {78 public:79 WRes Create()80 {81 return AutoResetEvent_CreateNotSignaled(&_object);82 }83 WRes CreateIfNotCreated()84 {85 if (IsCreated())86 return 0;87 return AutoResetEvent_CreateNotSignaled(&_object);88 }89 };91 #ifdef _WIN3292 class CObject: public CHandle93 {94 public:95 WRes Lock(DWORD timeoutInterval = INFINITE)96 { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); }97 };98 class CMutex: public CObject99 {100 public:101 WRes Create(bool initiallyOwn, LPCTSTR name = NULL,102 LPSECURITY_ATTRIBUTES securityAttributes = NULL)103 {104 _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name);105 if (_handle != 0)106 return 0;107 return ::GetLastError();108 }109 WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)110 {111 _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name);112 if (_handle != 0)113 return 0;114 return ::GetLastError();115 }116 WRes Release()117 {118 return ::ReleaseMutex(_handle) ? 0 : ::GetLastError();119 }120 };121 class CMutexLock122 {123 CMutex *_object;124 public:125 CMutexLock(CMutex &object): _object(&object) { _object->Lock(); }126 ~CMutexLock() { _object->Release(); }127 };128 #endif130 class CSemaphore131 {132 ::CSemaphore _object;133 public:134 CSemaphore() { Semaphore_Construct(&_object); }135 ~CSemaphore() { Close(); }136 WRes Close() { return Semaphore_Close(&_object); }137 operator HANDLE() { return _object.handle; }138 WRes Create(UInt32 initiallyCount, UInt32 maxCount)139 {140 return Semaphore_Create(&_object, initiallyCount, maxCount);141 }142 WRes Release() { return Semaphore_Release1(&_object); }143 WRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); }144 WRes Lock() { return Semaphore_Wait(&_object); }145 };147 class CCriticalSection148 {149 ::CCriticalSection _object;150 public:151 CCriticalSection() { CriticalSection_Init(&_object); }152 ~CCriticalSection() { CriticalSection_Delete(&_object); }153 void Enter() { CriticalSection_Enter(&_object); }154 void Leave() { CriticalSection_Leave(&_object); }155 };157 class CCriticalSectionLock158 {159 CCriticalSection *_object;160 void Unlock() { _object->Leave(); }161 public:162 CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); }163 ~CCriticalSectionLock() { Unlock(); }164 };166 }}168 #endif