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