uLib  User mode C/C++ extended API library for Win32 programmers.
SyncObj.h
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode library.
3 // Module: Class wraps for standard IPC sychronization objects.
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 #if 1//def __cplusplus // Nothing in here is accessible in C
9 
10 #ifndef __uLib_Sync_h_incl__
11 #define __uLib_Sync_h_incl__
12 
13 #include <uLib/UtilFunc.h>
14 
15 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 
27 typedef class TCritSec * PCritSec;
28 typedef class TSyncObj * PSyncObj;
29 typedef class TEvent * PEvent;
30 typedef class TMutex * PMutex;
31 typedef class TSemaphore * PSemaphore;
32 typedef class TWaitTimer * PWaitTimer;
33 
34 //==---------------------------------------------------------------------------
44 //==---------------------------------------------------------------------------
45 
46 class TCritSec {
47 private: // Unaccessible, so nobody can call them. (They are illegal for CRITICAL_SECTION).
48  TCritSec( const TCritSec& src ) { __debugbreak(); }
49  TCritSec& operator = ( const TCritSec& src ) { __debugbreak(); return *this; }
50 protected:
51  CRITICAL_SECTION _cSec;
52 public:
53  TCritSec();
54  TCritSec( DWORD SpinCount );
55  ~TCritSec();
56 
57  DWORD SetSpinCount( DWORD SpinCount );
58  void Enter();
59  bool TryEnter();
60  void Leave();
61 };
62 
63 //==---------------------------------------------------------------------------
72 //==---------------------------------------------------------------------------
73 
74 class TSyncObj {
75 private: // Unaccessible, so nobody can call them (they would clobber _hSync if implemented).
76  TSyncObj& operator = ( const TSyncObj& Src ) { __debugbreak(); return *this; }
77  HANDLE& operator = ( HANDLE& Src ) { __debugbreak(); return _hSync; }
78 protected:
79  HANDLE _hSync;
80 public:
81  TSyncObj();
82  TSyncObj( const HANDLE& Src );
83  ~TSyncObj();
84 
85  bool Close();
86 
87  bool Wait( DWORD msWait );
88  bool WaitEx( DWORD msWait, bool Alertable );
89  bool SignalAndWait( HANDLE hWaitFor, DWORD msWait, bool Alertable );
90 
93 
94  HANDLE RegisterWait( WAITORTIMERCALLBACK Callback, PVOID Context, DWORD msWait, DWORD Flags );
95  bool UnregisterWait( HANDLE hWait );
96 
97  operator HANDLE() { return _hSync; }
98  HANDLE GetHandle() { return _hSync; }
99  bool Initialized() { return (_hSync != NULL); }
100 
103 
104  HANDLE Duplicate(
105  HANDLE TargetProc, bool Inherit, ACCESS_MASK Access=0, DWORD Opt = DUPLICATE_SAME_ACCESS
106  );
107 };
108 
110 #ifdef __GNUC__
111 #pragma GCC diagnostics pop
112 #endif
113 
115 //==---------------------------------------------------------------------------
117 //==---------------------------------------------------------------------------
118 
119 class TEvent : public TSyncObj {
120 public:
122  TEvent( LPSECURITY_ATTRIBUTES SecAttr, bool Manual, bool Init, CSTR Name = NULL );
123  TEvent( DWORD Access, bool Inherit, CSTR Name );
124  ~TEvent();
125 
126  bool Set();
127  bool Reset();
128  bool Pulse();
129 };
131 
132 //==---------------------------------------------------------------------------
134 //==---------------------------------------------------------------------------
135 
136 class TMutex : public TSyncObj {
137 public:
139  TMutex( LPSECURITY_ATTRIBUTES SecAttr, bool Own, CSTR Name = NULL );
140  TMutex( DWORD Access, bool Inherit, CSTR Name );
141  ~TMutex();
142 
143  bool Release();
144 };
145 
146 //==---------------------------------------------------------------------------
148 //==---------------------------------------------------------------------------
149 
150 class TSemaphore : public TSyncObj {
151 public:
153  TSemaphore( LPSECURITY_ATTRIBUTES SecAttr, LONG Count, LONG MaxCount, CSTR Name = NULL );
154  TSemaphore( DWORD Access, bool Inherit, CSTR Name );
155  ~TSemaphore();
156 
157  bool Release( LONG Count, OPTOUT LPLONG OldCount = NULL );
158 
159  // Add some extra functionality to the standard semaphores.
160 
161  LONG ReleaseAll();
162  LONG Lock();
163 };
164 
165 //==---------------------------------------------------------------------------
176 //==---------------------------------------------------------------------------
177 
178 class TWaitTimer : public TSyncObj {
179 public:
181  TWaitTimer( LPSECURITY_ATTRIBUTES SecAttr, bool Manual, CSTR Name = NULL );
182 
184  TWaitTimer( DWORD Access, bool Inherit, CSTR Name );
185 
187  ~TWaitTimer();
188 
199 
200  bool Set( FILETIME Due, LONG Period, PTIMERAPCROUTINE Action, PVOID Context, bool Resume );
201 
206 
207  bool Set( INT64 Due, LONG Period, PTIMERAPCROUTINE Action, PVOID Context, bool Resume );
208 
209  bool Cancel();
210 };
211 
212 //==---------------------------------------------------------------------------
214 #endif//ndef __uLib_Sync_h_incl__
215 #endif//def __cplusplus
216 // EOF
CRITICAL_SECTION _cSec
Definition: SyncObj.h:51
class TMutex * PMutex
Definition: SyncObj.h:30
unsigned long DWORD
Definition: Common.h:414
TSemaphore(LPSECURITY_ATTRIBUTES SecAttr, LONG Count, LONG MaxCount, CSTR Name=NULL)
Definition: SyncObj.cpp:277
bool Initialized()
Definition: SyncObj.h:99
HANDLE GetHandle()
Definition: SyncObj.h:98
bool UnregisterWait(HANDLE hWait)
Definition: SyncObj.cpp:200
#define CSTR
Definition: Common.h:329
bool Release(LONG Count, OPTOUT LPLONG OldCount=NULL)
Definition: SyncObj.cpp:294
~TCritSec()
Definition: SyncObj.cpp:62
class TCritSec * PCritSec
Definition: SyncObj.h:27
~TMutex()
Definition: SyncObj.cpp:265
TSyncObj()
Definition: SyncObj.cpp:123
bool WaitEx(DWORD msWait, bool Alertable)
Definition: SyncObj.cpp:165
bool Cancel()
Definition: SyncObj.cpp:356
#define OPTOUT
Definition: Common.h:264
bool Reset()
Definition: SyncObj.cpp:241
bool Wait(DWORD msWait)
Definition: SyncObj.cpp:153
~TEvent()
Definition: SyncObj.cpp:231
bool Release()
Definition: SyncObj.cpp:270
class TEvent * PEvent
Definition: SyncObj.h:29
~TSyncObj()
Definition: SyncObj.cpp:131
class TSyncObj * PSyncObj
Definition: SyncObj.h:28
TWaitTimer(LPSECURITY_ATTRIBUTES SecAttr, bool Manual, CSTR Name=NULL)
Definition: SyncObj.cpp:325
void Leave()
Definition: SyncObj.cpp:116
HANDLE RegisterWait(WAITORTIMERCALLBACK Callback, PVOID Context, DWORD msWait, DWORD Flags)
Definition: SyncObj.cpp:189
bool Set(FILETIME Due, LONG Period, PTIMERAPCROUTINE Action, PVOID Context, bool Resume)
Definition: SyncObj.cpp:340
HANDLE _hSync
Definition: SyncObj.h:79
signed __int64 INT64
Definition: Common.h:401
LONG ReleaseAll()
Definition: SyncObj.cpp:300
LONG Lock()
Definition: SyncObj.cpp:312
bool TryEnter()
Definition: SyncObj.cpp:111
TCritSec()
Definition: SyncObj.cpp:29
TEvent(LPSECURITY_ATTRIBUTES SecAttr, bool Manual, bool Init, CSTR Name=NULL)
Definition: SyncObj.cpp:219
bool SignalAndWait(HANDLE hWaitFor, DWORD msWait, bool Alertable)
Definition: SyncObj.cpp:177
class TSemaphore * PSemaphore
Definition: SyncObj.h:31
bool Pulse()
Note: Documented as unreliable. Included just for completeness.
Definition: SyncObj.cpp:246
bool Close()
Definition: SyncObj.cpp:136
class TWaitTimer * PWaitTimer
Definition: SyncObj.h:32
bool Set()
Definition: SyncObj.cpp:236
HANDLE Duplicate(HANDLE TargetProc, bool Inherit, ACCESS_MASK Access=0, DWORD Opt=DUPLICATE_SAME_ACCESS)
Definition: SyncObj.cpp:205
DWORD SetSpinCount(DWORD SpinCount)
Definition: SyncObj.cpp:67
TMutex(LPSECURITY_ATTRIBUTES SecAttr, bool Own, CSTR Name=NULL)
Definition: SyncObj.cpp:253
void Enter()
Definition: SyncObj.cpp:93