FreeRDP
include/winpr/synch.h
1 
22 #ifndef WINPR_SYNCH_H
23 #define WINPR_SYNCH_H
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <winpr/platform.h>
30 #include <winpr/winpr.h>
31 #include <winpr/wtypes.h>
32 #include <winpr/error.h>
33 #include <winpr/handle.h>
34 
35 #include <winpr/nt.h>
36 
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41 
42 #ifndef _WIN32
43 
44 /* Mutex */
45 #define CREATE_MUTEX_INITIAL_OWNER 0x00000001
46 
47  WINPR_ATTR_MALLOC(CloseHandle, 1)
48  WINPR_API HANDLE CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
49  LPCSTR lpName);
50 
51  WINPR_ATTR_MALLOC(CloseHandle, 1)
52  WINPR_API HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
53  LPCWSTR lpName);
54 
55  WINPR_ATTR_MALLOC(CloseHandle, 1)
56  WINPR_API HANDLE CreateMutexExA(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCSTR lpName,
57  DWORD dwFlags, DWORD dwDesiredAccess);
58 
59  WINPR_ATTR_MALLOC(CloseHandle, 1)
60  WINPR_API HANDLE CreateMutexExW(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCWSTR lpName,
61  DWORD dwFlags, DWORD dwDesiredAccess);
62 
63  WINPR_ATTR_MALLOC(CloseHandle, 1)
64  WINPR_API HANDLE OpenMutexA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
65 
66  WINPR_ATTR_MALLOC(CloseHandle, 1)
67  WINPR_API HANDLE OpenMutexW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
68 
69  WINPR_API BOOL ReleaseMutex(HANDLE hMutex);
70 
71 #ifdef UNICODE
72 #define CreateMutex CreateMutexW
73 #define CreateMutexEx CreateMutexExW
74 #define OpenMutex OpenMutexW
75 #else
76 #define CreateMutex CreateMutexA
77 #define CreateMutexEx CreateMutexExA
78 #define OpenMutex OpenMutexA
79 #endif
80 
81  /* Semaphore */
82 
83  WINPR_ATTR_MALLOC(CloseHandle, 1)
84  WINPR_API HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
85  LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName);
86 
87  WINPR_ATTR_MALLOC(CloseHandle, 1)
88  WINPR_API HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
89  LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName);
90 
91  WINPR_ATTR_MALLOC(CloseHandle, 1)
92  WINPR_API HANDLE OpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
93 
94  WINPR_ATTR_MALLOC(CloseHandle, 1)
95  WINPR_API HANDLE OpenSemaphoreW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
96 
97 #ifdef UNICODE
98 #define CreateSemaphore CreateSemaphoreW
99 #define OpenSemaphore OpenSemaphoreW
100 #else
101 #define CreateSemaphore CreateSemaphoreA
102 #define OpenSemaphore OpenSemaphoreA
103 #endif
104 
105  WINPR_API BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount);
106 
107 /* Event */
108 #define CREATE_EVENT_MANUAL_RESET 0x00000001
109 #define CREATE_EVENT_INITIAL_SET 0x00000002
110 
111  WINPR_ATTR_MALLOC(CloseHandle, 1)
112  WINPR_API HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
113  BOOL bInitialState, LPCSTR lpName);
114 
115  WINPR_ATTR_MALLOC(CloseHandle, 1)
116  WINPR_API HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
117  BOOL bInitialState, LPCWSTR lpName);
118 
119  WINPR_ATTR_MALLOC(CloseHandle, 1)
120  WINPR_API HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName,
121  DWORD dwFlags, DWORD dwDesiredAccess);
122 
123  WINPR_ATTR_MALLOC(CloseHandle, 1)
124  WINPR_API HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName,
125  DWORD dwFlags, DWORD dwDesiredAccess);
126 
127  WINPR_ATTR_MALLOC(CloseHandle, 1)
128  WINPR_API HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
129 
130  WINPR_ATTR_MALLOC(CloseHandle, 1)
131  WINPR_API HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
132 
133  WINPR_API BOOL SetEvent(HANDLE hEvent);
134  WINPR_API BOOL ResetEvent(HANDLE hEvent);
135 
136 #if defined(WITH_DEBUG_EVENTS)
137 #define DumpEventHandles() DumpEventHandles_(__func__, __FILE__, __LINE__)
138  WINPR_API void DumpEventHandles_(const char* fkt, const char* file, size_t line);
139 #endif
140 #ifdef UNICODE
141 #define CreateEvent CreateEventW
142 #define CreateEventEx CreateEventExW
143 #define OpenEvent OpenEventW
144 #else
145 #define CreateEvent CreateEventA
146 #define CreateEventEx CreateEventExA
147 #define OpenEvent OpenEventA
148 #endif
149 
150  /* Condition Variable */
151 
152  typedef PVOID RTL_CONDITION_VARIABLE;
153  typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
154 
155  /* Critical Section */
156 
157  typedef struct
158  {
159  PVOID DebugInfo;
160  LONG LockCount;
161  LONG RecursionCount;
162  HANDLE OwningThread;
163  HANDLE LockSemaphore;
164  ULONG_PTR SpinCount;
166 
170 
171  WINPR_API VOID InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
172  WINPR_API BOOL InitializeCriticalSectionEx(LPCRITICAL_SECTION lpCriticalSection,
173  DWORD dwSpinCount, DWORD Flags);
174  WINPR_API BOOL InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection,
175  DWORD dwSpinCount);
176 
177  WINPR_API DWORD SetCriticalSectionSpinCount(LPCRITICAL_SECTION lpCriticalSection,
178  DWORD dwSpinCount);
179 
180  WINPR_API VOID EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
181  WINPR_API BOOL TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
182 
183  WINPR_API VOID LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
184 
185  WINPR_API VOID DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection);
186 
187  /* Sleep */
188 
189  WINPR_API VOID Sleep(DWORD dwMilliseconds);
190  WINPR_API DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable);
191 
192  /* Address */
193 
194  WINPR_API VOID WakeByAddressAll(PVOID Address);
195  WINPR_API VOID WakeByAddressSingle(PVOID Address);
196 
197  WINPR_API BOOL WaitOnAddress(VOID volatile* Address, PVOID CompareAddress, size_t AddressSize,
198  DWORD dwMilliseconds);
199 
200  /* Wait */
201 
202 #define INFINITE 0xFFFFFFFFUL
203 
204 #define WAIT_OBJECT_0 0x00000000UL
205 #define WAIT_ABANDONED 0x00000080UL
206 #define WAIT_IO_COMPLETION 0x000000C0UL
207 
208 #ifndef WAIT_TIMEOUT
209 #define WAIT_TIMEOUT 0x00000102UL
210 #endif
211 
212 #define WAIT_FAILED 0xFFFFFFFFUL
213 
214 #define MAXIMUM_WAIT_OBJECTS 64
215 
216  WINPR_API DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
217  WINPR_API DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable);
218  WINPR_API DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
219  DWORD dwMilliseconds);
220  WINPR_API DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll,
221  DWORD dwMilliseconds, BOOL bAlertable);
222 
223  WINPR_API DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
224  DWORD dwMilliseconds, BOOL bAlertable);
225 
226  /* Waitable Timer */
227 
228 #define CREATE_WAITABLE_TIMER_MANUAL_RESET 0x00000001
229 
230  typedef struct
231  {
232  ULONG Version;
233  DWORD Flags;
234 
235  union
236  {
237  struct
238  {
239  HMODULE LocalizedReasonModule;
240  ULONG LocalizedReasonId;
241  ULONG ReasonStringCount;
242  LPWSTR* ReasonStrings;
243  } Detailed;
244 
245  LPWSTR SimpleReasonString;
246  } Reason;
248 
249  typedef VOID (*PTIMERAPCROUTINE)(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue,
250  DWORD dwTimerHighValue);
251 
252  WINPR_ATTR_MALLOC(CloseHandle, 1)
253  WINPR_API HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
254  BOOL bManualReset, LPCSTR lpTimerName);
255 
256  WINPR_ATTR_MALLOC(CloseHandle, 1)
257  WINPR_API HANDLE CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
258  BOOL bManualReset, LPCWSTR lpTimerName);
259 
260  WINPR_ATTR_MALLOC(CloseHandle, 1)
261  WINPR_API HANDLE CreateWaitableTimerExA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
262  LPCSTR lpTimerName, DWORD dwFlags,
263  DWORD dwDesiredAccess);
264 
265  WINPR_ATTR_MALLOC(CloseHandle, 1)
266  WINPR_API HANDLE CreateWaitableTimerExW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
267  LPCWSTR lpTimerName, DWORD dwFlags,
268  DWORD dwDesiredAccess);
269 
270  WINPR_API BOOL SetWaitableTimer(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
271  PTIMERAPCROUTINE pfnCompletionRoutine,
272  LPVOID lpArgToCompletionRoutine, BOOL fResume);
273 
274  WINPR_API BOOL SetWaitableTimerEx(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
275  PTIMERAPCROUTINE pfnCompletionRoutine,
276  LPVOID lpArgToCompletionRoutine, PREASON_CONTEXT WakeContext,
277  ULONG TolerableDelay);
278 
279  WINPR_ATTR_MALLOC(CloseHandle, 1)
280  WINPR_API HANDLE OpenWaitableTimerA(DWORD dwDesiredAccess, BOOL bInheritHandle,
281  LPCSTR lpTimerName);
282 
283  WINPR_ATTR_MALLOC(CloseHandle, 1)
284  WINPR_API HANDLE OpenWaitableTimerW(DWORD dwDesiredAccess, BOOL bInheritHandle,
285  LPCWSTR lpTimerName);
286 
287  WINPR_API BOOL CancelWaitableTimer(HANDLE hTimer);
288 
289 #ifdef UNICODE
290 #define CreateWaitableTimer CreateWaitableTimerW
291 #define CreateWaitableTimerEx CreateWaitableTimerExW
292 #define OpenWaitableTimer OpenWaitableTimerW
293 #else
294 #define CreateWaitableTimer CreateWaitableTimerA
295 #define CreateWaitableTimerEx CreateWaitableTimerExA
296 #define OpenWaitableTimer OpenWaitableTimerA
297 #endif
298 
299  WINPR_API int GetTimerFileDescriptor(HANDLE hEvent);
300 
305 #define WT_EXECUTEDEFAULT 0x00000000
306 #define WT_EXECUTEINIOTHREAD 0x00000001
307 #define WT_EXECUTEINUITHREAD 0x00000002
308 #define WT_EXECUTEINWAITTHREAD 0x00000004
309 #define WT_EXECUTEONLYONCE 0x00000008
310 #define WT_EXECUTELONGFUNCTION 0x00000010
311 #define WT_EXECUTEINTIMERTHREAD 0x00000020
312 #define WT_EXECUTEINPERSISTENTIOTHREAD 0x00000040
313 #define WT_EXECUTEINPERSISTENTTHREAD 0x00000080
314 #define WT_TRANSFER_IMPERSONATION 0x00000100
315 
316  typedef VOID (*WAITORTIMERCALLBACK)(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
317 
318  WINPR_ATTR_MALLOC(CloseHandle, 1)
319  WINPR_API HANDLE CreateTimerQueue(void);
320 
321  WINPR_API BOOL DeleteTimerQueue(HANDLE TimerQueue);
322  WINPR_API BOOL DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent);
323 
324  WINPR_API BOOL CreateTimerQueueTimer(PHANDLE phNewTimer, HANDLE TimerQueue,
325  WAITORTIMERCALLBACK Callback, PVOID Parameter,
326  DWORD DueTime, DWORD Period, ULONG Flags);
327  WINPR_API BOOL ChangeTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, ULONG DueTime,
328  ULONG Period);
329  WINPR_API BOOL DeleteTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, HANDLE CompletionEvent);
330 
331 #endif
332 
333 #if (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
334 #define InitializeCriticalSectionEx(lpCriticalSection, dwSpinCount, Flags) \
335  InitializeCriticalSectionAndSpinCount(lpCriticalSection, dwSpinCount)
336 #endif
337 
338  WINPR_PRAGMA_DIAG_PUSH
339  WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
340 
341 #ifndef _RTL_RUN_ONCE_DEF
342 #define _RTL_RUN_ONCE_DEF
343 
344  WINPR_PRAGMA_DIAG_POP
345 
346 #define RTL_RUN_ONCE_INIT \
347  { \
348  0 \
349  }
350 
351 #define RTL_RUN_ONCE_CHECK_ONLY 0x00000001
352 #define RTL_RUN_ONCE_ASYNC 0x00000002
353 #define RTL_RUN_ONCE_INIT_FAILED 0x00000004
354 
355 #define RTL_RUN_ONCE_CTX_RESERVED_BITS 2
356 
357  typedef struct
358  {
359  PVOID Ptr;
361 
362  typedef ULONG CALLBACK RTL_RUN_ONCE_INIT_FN(PRTL_RUN_ONCE RunOnce, PVOID Parameter,
363  PVOID* Context);
364  typedef RTL_RUN_ONCE_INIT_FN* PRTL_RUN_ONCE_INIT_FN;
365 
366 #endif
367 
368 #if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
369 
370  /* One-Time Initialization */
371 
372 #define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT
373 
374  typedef RTL_RUN_ONCE INIT_ONCE;
375  typedef PRTL_RUN_ONCE PINIT_ONCE;
376  typedef PRTL_RUN_ONCE LPINIT_ONCE;
377  typedef BOOL(CALLBACK* PINIT_ONCE_FN)(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context);
378 
379  WINPR_API BOOL winpr_InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags,
380  PBOOL fPending, LPVOID* lpContext);
381  WINPR_API BOOL winpr_InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext);
382  WINPR_API BOOL winpr_InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn,
383  PVOID Parameter, LPVOID* Context);
384  WINPR_API VOID winpr_InitOnceInitialize(PINIT_ONCE InitOnce);
385 
386 #define InitOnceBeginInitialize winpr_InitOnceBeginInitialize
387 #define InitOnceComplete winpr_InitOnceComplete
388 #define InitOnceExecuteOnce winpr_InitOnceExecuteOnce
389 #define InitOnceInitialize winpr_InitOnceInitialize
390 #endif
391 
392  /* Synchronization Barrier */
393 
394 #if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0602) && !defined(_SYNCHAPI_H_))
395 #define WINPR_SYNCHRONIZATION_BARRIER 1
396 #endif
397 
398 #ifdef WINPR_SYNCHRONIZATION_BARRIER
399 
400  typedef struct
401  {
402  DWORD Reserved1;
403  DWORD Reserved2;
404  ULONG_PTR Reserved3[2];
405  DWORD Reserved4;
406  DWORD Reserved5;
408 
412 
413 #define SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY 0x01
414 #define SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY 0x02
415 #define SYNCHRONIZATION_BARRIER_FLAGS_NO_DELETE 0x04
416 
417  WINPR_API BOOL WINAPI winpr_InitializeSynchronizationBarrier(
418  LPSYNCHRONIZATION_BARRIER lpBarrier, LONG lTotalThreads, LONG lSpinCount);
419  WINPR_API BOOL WINAPI winpr_EnterSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier,
420  DWORD dwFlags);
421  WINPR_API BOOL WINAPI winpr_DeleteSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier);
422 
423 #define InitializeSynchronizationBarrier winpr_InitializeSynchronizationBarrier
424 #define EnterSynchronizationBarrier winpr_EnterSynchronizationBarrier
425 #define DeleteSynchronizationBarrier winpr_DeleteSynchronizationBarrier
426 
427 #endif
428 
429  /* Extended API */
430 
431  WINPR_API VOID USleep(DWORD dwMicroseconds);
432 
433  WINPR_ATTR_MALLOC(CloseHandle, 1)
434  WINPR_API HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes,
435  BOOL bManualReset, BOOL bInitialState,
436  int FileDescriptor, ULONG mode);
437 
438  WINPR_ATTR_MALLOC(CloseHandle, 1)
439  WINPR_API HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,
440  BOOL bManualReset, BOOL bInitialState,
441  int FileDescriptor, ULONG mode);
442 
443  WINPR_ATTR_MALLOC(CloseHandle, 1)
444  WINPR_API HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes,
445  BOOL bManualReset, BOOL bInitialState, void* pObject);
446 
447 #ifdef UNICODE
448 #define CreateFileDescriptorEvent CreateFileDescriptorEventW
449 #else
450 #define CreateFileDescriptorEvent CreateFileDescriptorEventA
451 #endif
452 
453  WINPR_API int GetEventFileDescriptor(HANDLE hEvent);
454  WINPR_API int SetEventFileDescriptor(HANDLE hEvent, int FileDescriptor, ULONG mode);
455 
456  WINPR_API void* GetEventWaitObject(HANDLE hEvent);
457 
458 #ifdef __cplusplus
459 }
460 #endif
461 
462 #endif /* WINPR_SYNCH_H */