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>
45 #define CREATE_MUTEX_INITIAL_OWNER 0x00000001
47 WINPR_ATTR_MALLOC(CloseHandle, 1)
48 WINPR_API HANDLE CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
51 WINPR_ATTR_MALLOC(CloseHandle, 1)
52 WINPR_API HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
55 WINPR_ATTR_MALLOC(CloseHandle, 1)
56 WINPR_API HANDLE CreateMutexExA(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCSTR lpName,
57 DWORD dwFlags, DWORD dwDesiredAccess);
59 WINPR_ATTR_MALLOC(CloseHandle, 1)
60 WINPR_API HANDLE CreateMutexExW(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCWSTR lpName,
61 DWORD dwFlags, DWORD dwDesiredAccess);
63 WINPR_ATTR_MALLOC(CloseHandle, 1)
64 WINPR_API HANDLE OpenMutexA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
66 WINPR_ATTR_MALLOC(CloseHandle, 1)
67 WINPR_API HANDLE OpenMutexW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
69 WINPR_API BOOL ReleaseMutex(HANDLE hMutex);
72 #define CreateMutex CreateMutexW
73 #define CreateMutexEx CreateMutexExW
74 #define OpenMutex OpenMutexW
76 #define CreateMutex CreateMutexA
77 #define CreateMutexEx CreateMutexExA
78 #define OpenMutex OpenMutexA
83 WINPR_ATTR_MALLOC(CloseHandle, 1)
84 WINPR_API HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
85 LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName);
87 WINPR_ATTR_MALLOC(CloseHandle, 1)
88 WINPR_API HANDLE CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
89 LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName);
91 WINPR_ATTR_MALLOC(CloseHandle, 1)
92 WINPR_API HANDLE OpenSemaphoreA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
94 WINPR_ATTR_MALLOC(CloseHandle, 1)
95 WINPR_API HANDLE OpenSemaphoreW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
98 #define CreateSemaphore CreateSemaphoreW
99 #define OpenSemaphore OpenSemaphoreW
101 #define CreateSemaphore CreateSemaphoreA
102 #define OpenSemaphore OpenSemaphoreA
105 WINPR_API BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount);
108 #define CREATE_EVENT_MANUAL_RESET 0x00000001
109 #define CREATE_EVENT_INITIAL_SET 0x00000002
111 WINPR_ATTR_MALLOC(CloseHandle, 1)
112 WINPR_API HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
113 BOOL bInitialState, LPCSTR lpName);
115 WINPR_ATTR_MALLOC(CloseHandle, 1)
116 WINPR_API HANDLE CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
117 BOOL bInitialState, LPCWSTR lpName);
119 WINPR_ATTR_MALLOC(CloseHandle, 1)
120 WINPR_API HANDLE CreateEventExA(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCSTR lpName,
121 DWORD dwFlags, DWORD dwDesiredAccess);
123 WINPR_ATTR_MALLOC(CloseHandle, 1)
124 WINPR_API HANDLE CreateEventExW(LPSECURITY_ATTRIBUTES lpEventAttributes, LPCWSTR lpName,
125 DWORD dwFlags, DWORD dwDesiredAccess);
127 WINPR_ATTR_MALLOC(CloseHandle, 1)
128 WINPR_API HANDLE OpenEventA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName);
130 WINPR_ATTR_MALLOC(CloseHandle, 1)
131 WINPR_API HANDLE OpenEventW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName);
133 WINPR_API BOOL SetEvent(HANDLE hEvent);
134 WINPR_API BOOL ResetEvent(HANDLE hEvent);
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);
141 #define CreateEvent CreateEventW
142 #define CreateEventEx CreateEventExW
143 #define OpenEvent OpenEventW
145 #define CreateEvent CreateEventA
146 #define CreateEventEx CreateEventExA
147 #define OpenEvent OpenEventA
152 typedef PVOID RTL_CONDITION_VARIABLE;
153 typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
163 HANDLE LockSemaphore;
173 DWORD dwSpinCount, DWORD Flags);
174 WINPR_API BOOL InitializeCriticalSectionAndSpinCount(
LPCRITICAL_SECTION lpCriticalSection,
189 WINPR_API VOID Sleep(DWORD dwMilliseconds);
190 WINPR_API DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable);
194 WINPR_API VOID WakeByAddressAll(PVOID Address);
195 WINPR_API VOID WakeByAddressSingle(PVOID Address);
197 WINPR_API BOOL WaitOnAddress(VOID
volatile* Address, PVOID CompareAddress,
size_t AddressSize,
198 DWORD dwMilliseconds);
202 #define INFINITE 0xFFFFFFFFUL
204 #define WAIT_OBJECT_0 0x00000000UL
205 #define WAIT_ABANDONED 0x00000080UL
206 #define WAIT_IO_COMPLETION 0x000000C0UL
209 #define WAIT_TIMEOUT 0x00000102UL
212 #define WAIT_FAILED 0xFFFFFFFFUL
214 #define MAXIMUM_WAIT_OBJECTS 64
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);
223 WINPR_API DWORD SignalObjectAndWait(HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
224 DWORD dwMilliseconds, BOOL bAlertable);
228 #define CREATE_WAITABLE_TIMER_MANUAL_RESET 0x00000001
239 HMODULE LocalizedReasonModule;
240 ULONG LocalizedReasonId;
241 ULONG ReasonStringCount;
242 LPWSTR* ReasonStrings;
245 LPWSTR SimpleReasonString;
249 typedef VOID (*PTIMERAPCROUTINE)(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue,
250 DWORD dwTimerHighValue);
252 WINPR_ATTR_MALLOC(CloseHandle, 1)
253 WINPR_API HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
254 BOOL bManualReset, LPCSTR lpTimerName);
256 WINPR_ATTR_MALLOC(CloseHandle, 1)
257 WINPR_API HANDLE CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
258 BOOL bManualReset, LPCWSTR lpTimerName);
260 WINPR_ATTR_MALLOC(CloseHandle, 1)
261 WINPR_API HANDLE CreateWaitableTimerExA(LPSECURITY_ATTRIBUTES lpTimerAttributes,
262 LPCSTR lpTimerName, DWORD dwFlags,
263 DWORD dwDesiredAccess);
265 WINPR_ATTR_MALLOC(CloseHandle, 1)
266 WINPR_API HANDLE CreateWaitableTimerExW(LPSECURITY_ATTRIBUTES lpTimerAttributes,
267 LPCWSTR lpTimerName, DWORD dwFlags,
268 DWORD dwDesiredAccess);
270 WINPR_API BOOL SetWaitableTimer(HANDLE hTimer, const
LARGE_INTEGER* lpDueTime, LONG lPeriod,
271 PTIMERAPCROUTINE pfnCompletionRoutine,
272 LPVOID lpArgToCompletionRoutine, BOOL fResume);
274 WINPR_API BOOL SetWaitableTimerEx(HANDLE hTimer, const
LARGE_INTEGER* lpDueTime, LONG lPeriod,
275 PTIMERAPCROUTINE pfnCompletionRoutine,
277 ULONG TolerableDelay);
279 WINPR_ATTR_MALLOC(CloseHandle, 1)
280 WINPR_API HANDLE OpenWaitableTimerA(DWORD dwDesiredAccess, BOOL bInheritHandle,
283 WINPR_ATTR_MALLOC(CloseHandle, 1)
284 WINPR_API HANDLE OpenWaitableTimerW(DWORD dwDesiredAccess, BOOL bInheritHandle,
285 LPCWSTR lpTimerName);
287 WINPR_API BOOL CancelWaitableTimer(HANDLE hTimer);
290 #define CreateWaitableTimer CreateWaitableTimerW
291 #define CreateWaitableTimerEx CreateWaitableTimerExW
292 #define OpenWaitableTimer OpenWaitableTimerW
294 #define CreateWaitableTimer CreateWaitableTimerA
295 #define CreateWaitableTimerEx CreateWaitableTimerExA
296 #define OpenWaitableTimer OpenWaitableTimerA
299 WINPR_API
int GetTimerFileDescriptor(HANDLE hEvent);
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
316 typedef VOID (*WAITORTIMERCALLBACK)(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
318 WINPR_ATTR_MALLOC(CloseHandle, 1)
319 WINPR_API HANDLE CreateTimerQueue(
void);
321 WINPR_API BOOL DeleteTimerQueue(HANDLE TimerQueue);
322 WINPR_API BOOL DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent);
324 WINPR_API BOOL CreateTimerQueueTimer(HANDLE* phNewTimer, HANDLE TimerQueue,
325 WAITORTIMERCALLBACK Callback,
void* Parameter,
326 DWORD DueTime, DWORD Period, ULONG Flags);
327 WINPR_API BOOL ChangeTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, ULONG DueTime,
329 WINPR_API BOOL DeleteTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, HANDLE CompletionEvent);
333 #if (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
334 #define InitializeCriticalSectionEx(lpCriticalSection, dwSpinCount, Flags) \
335 InitializeCriticalSectionAndSpinCount(lpCriticalSection, dwSpinCount)
338 WINPR_PRAGMA_DIAG_PUSH
339 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
341 #ifndef _RTL_RUN_ONCE_DEF
342 #define _RTL_RUN_ONCE_DEF
344 WINPR_PRAGMA_DIAG_POP
346 #define RTL_RUN_ONCE_INIT \
351 #define RTL_RUN_ONCE_CHECK_ONLY 0x00000001
352 #define RTL_RUN_ONCE_ASYNC 0x00000002
353 #define RTL_RUN_ONCE_INIT_FAILED 0x00000004
355 #define RTL_RUN_ONCE_CTX_RESERVED_BITS 2
362 typedef ULONG CALLBACK RTL_RUN_ONCE_INIT_FN(
PRTL_RUN_ONCE RunOnce, PVOID Parameter,
364 typedef RTL_RUN_ONCE_INIT_FN* PRTL_RUN_ONCE_INIT_FN;
368 #if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
372 #define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT
377 typedef BOOL(CALLBACK* PINIT_ONCE_FN)(
PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context);
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);
386 #define InitOnceBeginInitialize winpr_InitOnceBeginInitialize
387 #define InitOnceComplete winpr_InitOnceComplete
388 #define InitOnceExecuteOnce winpr_InitOnceExecuteOnce
389 #define InitOnceInitialize winpr_InitOnceInitialize
394 #if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0602) && !defined(_SYNCHAPI_H_))
395 #define WINPR_SYNCHRONIZATION_BARRIER 1
398 #ifdef WINPR_SYNCHRONIZATION_BARRIER
404 ULONG_PTR Reserved3[2];
413 #define SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY 0x01
414 #define SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY 0x02
415 #define SYNCHRONIZATION_BARRIER_FLAGS_NO_DELETE 0x04
417 WINPR_API BOOL WINAPI winpr_InitializeSynchronizationBarrier(
423 #define InitializeSynchronizationBarrier winpr_InitializeSynchronizationBarrier
424 #define EnterSynchronizationBarrier winpr_EnterSynchronizationBarrier
425 #define DeleteSynchronizationBarrier winpr_DeleteSynchronizationBarrier
431 WINPR_API VOID USleep(DWORD dwMicroseconds);
433 WINPR_ATTR_MALLOC(CloseHandle, 1)
434 WINPR_API HANDLE CreateFileDescriptorEventW(LPSECURITY_ATTRIBUTES lpEventAttributes,
435 BOOL bManualReset, BOOL bInitialState,
436 int FileDescriptor, ULONG mode);
438 WINPR_ATTR_MALLOC(CloseHandle, 1)
439 WINPR_API HANDLE CreateFileDescriptorEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,
440 BOOL bManualReset, BOOL bInitialState,
441 int FileDescriptor, ULONG mode);
443 WINPR_ATTR_MALLOC(CloseHandle, 1)
444 WINPR_API HANDLE CreateWaitObjectEvent(LPSECURITY_ATTRIBUTES lpEventAttributes,
445 BOOL bManualReset, BOOL bInitialState,
void* pObject);
448 #define CreateFileDescriptorEvent CreateFileDescriptorEventW
450 #define CreateFileDescriptorEvent CreateFileDescriptorEventA
453 WINPR_API
int GetEventFileDescriptor(HANDLE hEvent);
454 WINPR_API
int SetEventFileDescriptor(HANDLE hEvent,
int FileDescriptor, ULONG mode);
456 WINPR_API
void* GetEventWaitObject(HANDLE hEvent);