-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwsync.h
More file actions
241 lines (209 loc) · 6.75 KB
/
Copy pathwsync.h
File metadata and controls
241 lines (209 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#if !defined(_INC_WSYNC_) && defined(__cplusplus)
#define _INC_WSYNC_
#include <windows.h>
#include <wbase.h>
// Encapsulated CRITICAL_SECTION
class WCriticalSection
{
protected:
CRITICAL_SECTION CriticalSection;
public:
VOID Enter()
{
EnterCriticalSection(&CriticalSection);
}
VOID Leave()
{
LeaveCriticalSection(&CriticalSection);
}
#if _WIN32_WINNT >= 0x0400
BOOL TryEnter()
{
return TryEnterCriticalSection(&CriticalSection);
}
#endif
WCriticalSection()
{
InitializeCriticalSection(&CriticalSection);
}
~WCriticalSection()
{
DeleteCriticalSection(&CriticalSection);
}
};
/**
Base class for sync-object classes.
Adds functionality for waiting to WKernelObject.
*/
class WSynchronizer : public WKernelObject
{
protected:
WSynchronizer(HANDLE handle)
: WKernelObject(handle)
{
}
public:
DWORD Wait(DWORD dwMilliseconds = INFINITE)
{
return WaitForSingleObject(hObject, dwMilliseconds);
}
#if _WIN32_WINNT >= 0x0400
BOOL SignalAndWaitForObject(HANDLE hObjectToWaitOn,
DWORD dwMilliseconds = INFINITE, // time-out interval in milliseconds
BOOL bAlertable = false) // alertable flag
{
return ::SignalObjectAndWait(hObject, hObjectToWaitOn, dwMilliseconds,
bAlertable);
}
BOOL SignalAndWaitForObject(const WSynchronizer &ObjectToWaitOn,
DWORD dwMilliseconds = INFINITE, // time-out interval in milliseconds
BOOL bAlertable = false) // alertable flag
{
return ::SignalObjectAndWait(hObject, ObjectToWaitOn.Handle(),
dwMilliseconds, bAlertable);
}
BOOL SignalObjectAndWait(HANDLE hObjectToSignal,
DWORD dwMilliseconds = INFINITE, // time-out interval in milliseconds
BOOL bAlertable = false) // alertable flag
{
return ::SignalObjectAndWait(hObjectToSignal, hObject, dwMilliseconds,
bAlertable);
}
BOOL SignalObjectAndWait(const WSynchronizer &ObjectToSignal,
DWORD dwMilliseconds = INFINITE, // time-out interval in milliseconds
BOOL bAlertable = false) // alertable flag
{
return ::SignalObjectAndWait(ObjectToSignal.Handle(), hObject,
dwMilliseconds, bAlertable);
}
#endif
};
class WEvent: public WSynchronizer
{
public:
explicit WEvent(
BOOL bManualReset = TRUE, // flag for manual-reset event
BOOL bInitialState = FALSE, // flag for initial state
LPSECURITY_ATTRIBUTES lpEventAttributes = NULL, // pointer to sec attr
LPCSTR lpName = NULL) // pointer to event-object name
: WSynchronizer(CreateEventA(lpEventAttributes, bManualReset, bInitialState,
lpName))
{ }
explicit WEvent(
BOOL bManualReset, // flag for manual-reset event
BOOL bInitialState, // flag for initial state
LPSECURITY_ATTRIBUTES lpEventAttributes, // pointer to sec attr
LPCWSTR lpName) // pointer to event-object name
: WSynchronizer(CreateEventW(lpEventAttributes, bManualReset, bInitialState,
lpName))
{ }
explicit WEvent(
LPCSTR lpName, // pointer to event-object name
DWORD dwDesiredAccess = EVENT_ALL_ACCESS, // access flag
BOOL bInheritHandle = FALSE) // inherit flag
: WSynchronizer(OpenEventA(dwDesiredAccess, bInheritHandle, lpName))
{ }
explicit WEvent(
LPCWSTR lpName, // pointer to event-object name
DWORD dwDesiredAccess = EVENT_ALL_ACCESS, // access flag
BOOL bInheritHandle = FALSE) // inherit flag
: WSynchronizer(OpenEventW(dwDesiredAccess, bInheritHandle, lpName))
{ }
BOOL Set()
{
return SetEvent(hObject);
}
BOOL Reset()
{
return ResetEvent(hObject);
}
BOOL Pulse()
{
return PulseEvent(hObject);
}
};
class WMutex: public WSynchronizer
{
public:
explicit WMutex(
BOOL bInitialOwner = true, // flag for initial ownership
LPCTSTR lpName = NULL, // pointer to mutex-object name
LPSECURITY_ATTRIBUTES lpMutexAttributes = NULL) // pointer to sec attr
: WSynchronizer(CreateMutex(lpMutexAttributes, bInitialOwner, lpName))
{ }
explicit WMutex(
LPCTSTR lpName, // pointer to mutex-object name
DWORD dwDesiredAccess = MUTEX_ALL_ACCESS, // access flag
BOOL bInheritHandle = false) // inherit flag
: WSynchronizer(OpenMutex(dwDesiredAccess, bInheritHandle, lpName))
{ }
BOOL Release()
{
return ReleaseMutex(hObject);
}
};
class WSemaphore: public WSynchronizer
{
public:
explicit WSemaphore(
LONG lInitialCount = 0, // initial count
LONG lMaximumCount = 1, // maximum count
LPSECURITY_ATTRIBUTES lpSemaphoreAttributes = NULL, // ptr to sec attr
LPCTSTR lpName = NULL)
: WSynchronizer(CreateSemaphore(lpSemaphoreAttributes, lInitialCount,
lMaximumCount, lpName))
{ }
explicit WSemaphore(
LPCTSTR lpName, // pointer to mutex-object name
DWORD dwDesiredAccess = MUTEX_ALL_ACCESS, // access flag
BOOL bInheritHandle = false) // inherit flag
: WSynchronizer(OpenSemaphore(dwDesiredAccess, bInheritHandle, lpName))
{ }
BOOL Release(
LONG lReleaseCount = 1, // amount to add to current count
LPLONG lpPreviousCount = NULL) // address of previous count
{
return ReleaseSemaphore(hObject, lReleaseCount, lpPreviousCount);
}
};
#if _WIN32_WINNT >= 0x0400
#ifndef TIMER_ALL_ACCESS
# define TIMER_QUERY_STATE 0x0001
# define TIMER_MODIFY_STATE 0x0002
# define TIMER_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|\
TIMER_QUERY_STATE|TIMER_MODIFY_STATE)
#endif
class WWaitableTimer : public WSynchronizer
{
public:
explicit WWaitableTimer(
BOOL bManualReset = false, // flag for manual reset state
LPSECURITY_ATTRIBUTES lpTimerAttributes = NULL, // pointer to sec attribs
LPCTSTR lpTimerName = NULL) // pointer to timer object name
: WSynchronizer(CreateWaitableTimer(lpTimerAttributes, bManualReset,
lpTimerName))
{ }
explicit WWaitableTimer(
LPCTSTR lpTimerName, // pointer to timer object name
DWORD dwDesiredAccess = TIMER_ALL_ACCESS, // access flag
BOOL bInheritHandle = false) // inherit flag
: WSynchronizer(OpenWaitableTimer(dwDesiredAccess, bInheritHandle,
lpTimerName))
{ }
BOOL Set(
const LARGE_INTEGER *pDueTime, // when timer will become signaled
LONG lPeriod, // periodic timer interval
PTIMERAPCROUTINE pfnCompletionRoutine, // pointer to completion routine
LPVOID lpArgToCompletionRoutine, // data passed to completion routine
BOOL fResume) // flag for resume state
{
return SetWaitableTimer(hObject, pDueTime, lPeriod, pfnCompletionRoutine,
lpArgToCompletionRoutine, fResume);
}
BOOL Cancel()
{
return CancelWaitableTimer(hObject);
}
};
#endif // _WIN32_WINNT >= 0x0400
#endif // _INC_WSYNC_