Upload 2.0.2
[physicsfs] / lzma / CPP / Windows / Synchronization.h
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   HRes Close() { return Event_Close(&_object); }
30   #ifdef _WIN32
31   HRes 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   HRes 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   HRes Set() { return Event_Set(&_object); }
50   // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); }
51   HRes Reset() { return Event_Reset(&_object); }
52   HRes Lock() { return Event_Wait(&_object); }
53 };
54
55 class CManualResetEvent: public CBaseEvent
56 {
57 public:
58   HRes Create(bool initiallyOwn = false)
59   {
60     return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0);
61   }
62   HRes CreateIfNotCreated()
63   {
64     if (IsCreated())
65       return 0;
66     return ManualResetEvent_CreateNotSignaled(&_object);
67   }
68   #ifdef _WIN32
69   HRes 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   HRes Create()
80   {
81     return AutoResetEvent_CreateNotSignaled(&_object);
82   }
83   HRes 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   HRes Lock(DWORD timeoutInterval = INFINITE)
96     { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); }
97 };
98 class CMutex: public CObject
99 {
100 public:
101   HRes 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   HRes 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   HRes 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   HRes Close() {  return Semaphore_Close(&_object); }
137   operator HANDLE() { return _object.handle; }
138   HRes Create(UInt32 initiallyCount, UInt32 maxCount)
139   {
140     return Semaphore_Create(&_object, initiallyCount, maxCount);
141   }
142   HRes Release() { return Semaphore_Release1(&_object); }
143   HRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); }
144   HRes 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