Mercurial > vba-linux
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:8ced16adf2e1 | 1:f9f4f1b99eed |
---|---|
1 // Windows/Synchronization.h | |
2 | |
3 #ifndef __WINDOWS_SYNCHRONIZATION_H | |
4 #define __WINDOWS_SYNCHRONIZATION_H | |
5 | |
6 #include "Defs.h" | |
7 | |
8 extern "C" | |
9 { | |
10 #include "../../C/Threads.h" | |
11 } | |
12 | |
13 #ifdef _WIN32 | |
14 #include "Handle.h" | |
15 #endif | |
16 | |
17 namespace NWindows { | |
18 namespace NSynchronization { | |
19 | |
20 class CBaseEvent | |
21 { | |
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 _WIN32 | |
31 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 #endif | |
48 | |
49 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 }; | |
54 | |
55 class CManualResetEvent: public CBaseEvent | |
56 { | |
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 _WIN32 | |
69 WRes CreateWithName(bool initiallyOwn, LPCTSTR name) | |
70 { | |
71 return CBaseEvent::Create(true, initiallyOwn, name); | |
72 } | |
73 #endif | |
74 }; | |
75 | |
76 class CAutoResetEvent: public CBaseEvent | |
77 { | |
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 }; | |
90 | |
91 #ifdef _WIN32 | |
92 class CObject: public CHandle | |
93 { | |
94 public: | |
95 WRes Lock(DWORD timeoutInterval = INFINITE) | |
96 { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); } | |
97 }; | |
98 class CMutex: public CObject | |
99 { | |
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 CMutexLock | |
122 { | |
123 CMutex *_object; | |
124 public: | |
125 CMutexLock(CMutex &object): _object(&object) { _object->Lock(); } | |
126 ~CMutexLock() { _object->Release(); } | |
127 }; | |
128 #endif | |
129 | |
130 class CSemaphore | |
131 { | |
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 }; | |
146 | |
147 class CCriticalSection | |
148 { | |
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 }; | |
156 | |
157 class CCriticalSectionLock | |
158 { | |
159 CCriticalSection *_object; | |
160 void Unlock() { _object->Leave(); } | |
161 public: | |
162 CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); } | |
163 ~CCriticalSectionLock() { Unlock(); } | |
164 }; | |
165 | |
166 }} | |
167 | |
168 #endif |