Mercurial > vba-linux
diff src/win32/7zip/7z/CPP/Windows/Synchronization.h @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/CPP/Windows/Synchronization.h Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,168 @@ 1.4 +// Windows/Synchronization.h 1.5 + 1.6 +#ifndef __WINDOWS_SYNCHRONIZATION_H 1.7 +#define __WINDOWS_SYNCHRONIZATION_H 1.8 + 1.9 +#include "Defs.h" 1.10 + 1.11 +extern "C" 1.12 +{ 1.13 +#include "../../C/Threads.h" 1.14 +} 1.15 + 1.16 +#ifdef _WIN32 1.17 +#include "Handle.h" 1.18 +#endif 1.19 + 1.20 +namespace NWindows { 1.21 +namespace NSynchronization { 1.22 + 1.23 +class CBaseEvent 1.24 +{ 1.25 +protected: 1.26 + ::CEvent _object; 1.27 +public: 1.28 + bool IsCreated() { return Event_IsCreated(&_object) != 0; } 1.29 + operator HANDLE() { return _object.handle; } 1.30 + CBaseEvent() { Event_Construct(&_object); } 1.31 + ~CBaseEvent() { Close(); } 1.32 + WRes Close() { return Event_Close(&_object); } 1.33 + #ifdef _WIN32 1.34 + WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, 1.35 + LPSECURITY_ATTRIBUTES securityAttributes = NULL) 1.36 + { 1.37 + _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset), 1.38 + BoolToBOOL(initiallyOwn), name); 1.39 + if (_object.handle != 0) 1.40 + return 0; 1.41 + return ::GetLastError(); 1.42 + } 1.43 + WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) 1.44 + { 1.45 + _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); 1.46 + if (_object.handle != 0) 1.47 + return 0; 1.48 + return ::GetLastError(); 1.49 + } 1.50 + #endif 1.51 + 1.52 + WRes Set() { return Event_Set(&_object); } 1.53 + // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } 1.54 + WRes Reset() { return Event_Reset(&_object); } 1.55 + WRes Lock() { return Event_Wait(&_object); } 1.56 +}; 1.57 + 1.58 +class CManualResetEvent: public CBaseEvent 1.59 +{ 1.60 +public: 1.61 + WRes Create(bool initiallyOwn = false) 1.62 + { 1.63 + return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0); 1.64 + } 1.65 + WRes CreateIfNotCreated() 1.66 + { 1.67 + if (IsCreated()) 1.68 + return 0; 1.69 + return ManualResetEvent_CreateNotSignaled(&_object); 1.70 + } 1.71 + #ifdef _WIN32 1.72 + WRes CreateWithName(bool initiallyOwn, LPCTSTR name) 1.73 + { 1.74 + return CBaseEvent::Create(true, initiallyOwn, name); 1.75 + } 1.76 + #endif 1.77 +}; 1.78 + 1.79 +class CAutoResetEvent: public CBaseEvent 1.80 +{ 1.81 +public: 1.82 + WRes Create() 1.83 + { 1.84 + return AutoResetEvent_CreateNotSignaled(&_object); 1.85 + } 1.86 + WRes CreateIfNotCreated() 1.87 + { 1.88 + if (IsCreated()) 1.89 + return 0; 1.90 + return AutoResetEvent_CreateNotSignaled(&_object); 1.91 + } 1.92 +}; 1.93 + 1.94 +#ifdef _WIN32 1.95 +class CObject: public CHandle 1.96 +{ 1.97 +public: 1.98 + WRes Lock(DWORD timeoutInterval = INFINITE) 1.99 + { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); } 1.100 +}; 1.101 +class CMutex: public CObject 1.102 +{ 1.103 +public: 1.104 + WRes Create(bool initiallyOwn, LPCTSTR name = NULL, 1.105 + LPSECURITY_ATTRIBUTES securityAttributes = NULL) 1.106 + { 1.107 + _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name); 1.108 + if (_handle != 0) 1.109 + return 0; 1.110 + return ::GetLastError(); 1.111 + } 1.112 + WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) 1.113 + { 1.114 + _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); 1.115 + if (_handle != 0) 1.116 + return 0; 1.117 + return ::GetLastError(); 1.118 + } 1.119 + WRes Release() 1.120 + { 1.121 + return ::ReleaseMutex(_handle) ? 0 : ::GetLastError(); 1.122 + } 1.123 +}; 1.124 +class CMutexLock 1.125 +{ 1.126 + CMutex *_object; 1.127 +public: 1.128 + CMutexLock(CMutex &object): _object(&object) { _object->Lock(); } 1.129 + ~CMutexLock() { _object->Release(); } 1.130 +}; 1.131 +#endif 1.132 + 1.133 +class CSemaphore 1.134 +{ 1.135 + ::CSemaphore _object; 1.136 +public: 1.137 + CSemaphore() { Semaphore_Construct(&_object); } 1.138 + ~CSemaphore() { Close(); } 1.139 + WRes Close() { return Semaphore_Close(&_object); } 1.140 + operator HANDLE() { return _object.handle; } 1.141 + WRes Create(UInt32 initiallyCount, UInt32 maxCount) 1.142 + { 1.143 + return Semaphore_Create(&_object, initiallyCount, maxCount); 1.144 + } 1.145 + WRes Release() { return Semaphore_Release1(&_object); } 1.146 + WRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); } 1.147 + WRes Lock() { return Semaphore_Wait(&_object); } 1.148 +}; 1.149 + 1.150 +class CCriticalSection 1.151 +{ 1.152 + ::CCriticalSection _object; 1.153 +public: 1.154 + CCriticalSection() { CriticalSection_Init(&_object); } 1.155 + ~CCriticalSection() { CriticalSection_Delete(&_object); } 1.156 + void Enter() { CriticalSection_Enter(&_object); } 1.157 + void Leave() { CriticalSection_Leave(&_object); } 1.158 +}; 1.159 + 1.160 +class CCriticalSectionLock 1.161 +{ 1.162 + CCriticalSection *_object; 1.163 + void Unlock() { _object->Leave(); } 1.164 +public: 1.165 + CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); } 1.166 + ~CCriticalSectionLock() { Unlock(); } 1.167 +}; 1.168 + 1.169 +}} 1.170 + 1.171 +#endif