00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #ifndef OW32_SyncObjects_h
00027 #define OW32_SyncObjects_h
00028
00029 #include <OW32/XHANDLE.h>
00030
00031 namespace OW32
00032 {
00033
00035 class OW32_LIB_EXPORT CSyncObject
00036 {
00037 private:
00038 CSyncObject(const CSyncObject& );
00039 CSyncObject& operator=(const CSyncObject& );
00040
00041 protected:
00043 CSyncObject() {}
00045 virtual ~CSyncObject() = 0 {}
00046
00047 public:
00052 virtual BOOL Lock(DWORD dwTimeout = INFINITE) = 0;
00054 virtual void Unlock() = 0;
00055 };
00056
00058 class OW32_LIB_EXPORT CSingleLock
00059 {
00060 private:
00061 CSyncObject& m_SyncObject;
00062 BOOL m_bLocked;
00063
00064 CSingleLock(const CSingleLock& );
00065 CSingleLock operator=(const CSingleLock& );
00066
00067 public:
00069 CSingleLock(CSyncObject& Object) :
00070 m_SyncObject(Object)
00071 {
00072 m_bLocked = m_SyncObject.Lock();
00073 }
00074
00076 ~CSingleLock()
00077 {
00078 if (m_bLocked)
00079 m_SyncObject.Unlock();
00080 }
00081
00083 BOOL IsLocked() { return m_bLocked; }
00084
00086 void Lock()
00087 {
00088 if (!m_bLocked) {
00089 m_bLocked = m_SyncObject.Lock();
00090 }
00091 }
00092
00094 void Unlock()
00095 {
00096 if (m_bLocked) {
00097 m_SyncObject.Unlock();
00098 m_bLocked = FALSE;
00099 }
00100 }
00101 };
00102
00104 class OW32_LIB_EXPORT CCriticalSection :
00105 public CSyncObject
00106 {
00107 private:
00108 CRITICAL_SECTION m_cs;
00109
00110 CCriticalSection(const CCriticalSection& );
00111 CCriticalSection& operator=(const CCriticalSection& );
00112
00113 public:
00115 CCriticalSection()
00116 {
00117 InitializeCriticalSection(&m_cs);
00118 }
00119
00121 ~CCriticalSection()
00122 {
00123 DeleteCriticalSection(&m_cs);
00124 }
00125
00130 BOOL Lock(DWORD dwTimeout=INFINITE)
00131 {
00132 dwTimeout;
00133 EnterCriticalSection(&m_cs);
00134 return TRUE;
00135 }
00136
00139 void Unlock()
00140 {
00141 LeaveCriticalSection(&m_cs);
00142 }
00143 };
00144
00146 class OW32_LIB_EXPORT CEvent :
00147 public CSyncObject
00148 {
00149 private:
00150 XHANDLE m_hEvent;
00151
00152 CEvent(const CEvent& );
00153 CEvent& operator= (const CEvent& );
00154
00155 public:
00162 CEvent(BOOL bManualReset = FALSE, BOOL bInitialState = FALSE,
00163 LPCTSTR lpszName = NULL, LPSECURITY_ATTRIBUTES lpSecAttr = NULL)
00164 {
00165 m_hEvent = CreateEvent(lpSecAttr, bManualReset, bInitialState, lpszName);
00166 }
00167
00171 CEvent(HANDLE hEvent) :
00172 m_hEvent(hEvent)
00173 {
00174 }
00175
00179 CEvent& operator=(HANDLE hEvent)
00180 {
00181 m_hEvent = hEvent;
00182 return *this;
00183 }
00184
00189 virtual BOOL Lock(DWORD dwTimeout=INFINITE)
00190 {
00191 return WaitForSingleObject(m_hEvent, dwTimeout)==WAIT_OBJECT_0;
00192 }
00194 void Unlock() {}
00195
00197 BOOL Set() { return ::SetEvent(m_hEvent); }
00199 BOOL Reset() { return ::ResetEvent(m_hEvent); }
00201 BOOL Pulse() { return ::PulseEvent(m_hEvent); }
00202
00204 operator HANDLE() const { return m_hEvent; }
00205 };
00206
00215 class OW32_LIB_EXPORT CSemaphore :
00216 public CSyncObject
00217 {
00218 XHANDLE m_hSemaphore;
00219
00220 CSemaphore(const CSemaphore& );
00221 CSemaphore& operator= (const CSemaphore& );
00222
00223 public:
00230 CSemaphore(LONG lMaximumCount, LONG lInitialCount = 0,
00231 LPCTSTR lpszName = NULL, LPSECURITY_ATTRIBUTES lpSecAttr = NULL)
00232 {
00233 m_hSemaphore = CreateSemaphore(lpSecAttr, lInitialCount, lMaximumCount, lpszName);
00234 }
00235
00239 CSemaphore(HANDLE hSemaphore) :
00240 m_hSemaphore(hSemaphore)
00241 {
00242 }
00243
00247 CSemaphore& operator=(HANDLE hSemaphore)
00248 {
00249 m_hSemaphore = hSemaphore;
00250 return *this;
00251 }
00252
00257 virtual BOOL Lock(DWORD dwTimeout = INFINITE)
00258 {
00259 return WaitForSingleObject(m_hSemaphore, dwTimeout)==WAIT_OBJECT_0;
00260 }
00261
00263 void Unlock() {}
00264
00270 BOOL Release(LONG lReleaseCount, LPLONG lPreviousCount=NULL)
00271 {
00272 return ReleaseSemaphore(m_hSemaphore, lReleaseCount, lPreviousCount);
00273 }
00274
00276 operator HANDLE() const { return m_hSemaphore; }
00277 };
00278
00280 class OW32_LIB_EXPORT CMutex :
00281 public CSyncObject
00282 {
00283 XHANDLE m_hMutex;
00284
00285 CMutex(const CMutex& );
00286 CMutex& operator= (const CMutex& );
00287
00288 public:
00294 CMutex(BOOL bInitialOwner = FALSE, LPCTSTR lpszName = NULL,
00295 LPSECURITY_ATTRIBUTES lpSecAttr = NULL)
00296 {
00297 m_hMutex = CreateMutex(lpSecAttr, bInitialOwner, lpszName);
00298 }
00299
00303 CMutex(HANDLE hMutex) :
00304 m_hMutex(hMutex)
00305 {
00306 }
00307
00311 CMutex& operator=(HANDLE hMutex)
00312 {
00313 m_hMutex = hMutex;
00314 return *this;
00315 }
00316
00321 virtual BOOL Lock(DWORD dwTimeout = INFINITE)
00322 {
00323 return WaitForSingleObject(m_hMutex, dwTimeout)==WAIT_OBJECT_0;
00324 }
00325
00327 void Unlock()
00328 {
00329 ReleaseMutex(m_hMutex);
00330 }
00331
00333 operator HANDLE() const { return m_hMutex; }
00334 };
00335
00336 #if _WIN32_WINNT >= 0x400
00337
00339 class OW32_LIB_EXPORT CWaitableTimer :
00340 public CSyncObject
00341 {
00342 XHANDLE m_hTimer;
00343
00344 CWaitableTimer(const CWaitableTimer& );
00345 CWaitableTimer& operator= (const CWaitableTimer& );
00346
00347 public:
00353 CWaitableTimer(BOOL bManualReset = FALSE, LPCTSTR lpszName = NULL,
00354 LPSECURITY_ATTRIBUTES lpSecAttr = NULL)
00355 {
00356 m_hTimer = CreateWaitableTimer(lpSecAttr, bManualReset, lpszName);
00357 }
00358
00366 BOOL Set(const LARGE_INTEGER* pDueTime, long Period = 0,
00367 PTIMERAPCROUTINE pfnCompletionRoutine = NULL, LPVOID lpArgToCompletionRoutine = NULL,
00368 BOOL fResume = FALSE)
00369 {
00370 return ::SetWaitableTimer(m_hTimer, pDueTime, Period, pfnCompletionRoutine,
00371 lpArgToCompletionRoutine, fResume);
00372 }
00373
00381 BOOL Set(__int64 DueTime, long Period = 0,
00382 PTIMERAPCROUTINE pfnCompletionRoutine = NULL, LPVOID lpArgToCompletionRoutine = NULL,
00383 BOOL fResume = FALSE)
00384 {
00385 LARGE_INTEGER li;
00386 li.QuadPart = DueTime;
00387 return ::SetWaitableTimer(m_hTimer, &li, Period, pfnCompletionRoutine,
00388 lpArgToCompletionRoutine, fResume);
00389 }
00390
00394 BOOL Cancel()
00395 {
00396 return ::CancelWaitableTimer(m_hTimer);
00397 }
00398
00403 virtual BOOL Lock(DWORD dwTimeout = INFINITE)
00404 {
00405 return WaitForSingleObject(m_hTimer, dwTimeout)==WAIT_OBJECT_0;
00406 }
00407
00409 void Unlock() {}
00410
00412 operator HANDLE() { return m_hTimer; }
00413 };
00414
00415 #endif
00416
00417 }
00418
00419 #endif // OW32_SyncObjects_h