ReactOS 0.4.17-dev-573-g8315b8c
sync_static.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS SDK
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Statically linked SRW lock, condition variable and init-once surface for llvm-mingw runtimes
5 * COPYRIGHT: Copyright 2026 Ahmed Arif <arif.ing@outlook.com>
6 */
7
8/*
9 * libc++ references the Win7 SRW lock APIs (directly and through dllimport slots) and the Vista condition
10 * variable and one-time initialization APIs (through dllimport slots). Bind them to the RTL implementation
11 * linked statically from rtl_vista (sdk/lib/rtl's srw.c, condvar.c and runonce.c) instead of exporting them
12 * from kernel32_vista.dll: modules get one self-contained, consistent synchronization implementation
13 * (ReactOS' lock layout is not Windows-compatible, mixing implementations on the same lock would be fatal)
14 * and no import that real Windows cannot satisfy.
15 */
16
17/* This TU defines the kernel32 Win7 surface itself: make the headers declare it, non-dllimport */
18#define _KERNEL32_
19#undef _WIN32_WINNT
20#define _WIN32_WINNT 0x0601
21
22/* Bind the Rtl references to the static rtl_vista implementation, not to ntdll(_vista).dll imports */
23#define _NTSYSTEM_
24
25#define WIN32_NO_STATUS
26#include <windef.h>
27#include <winbase.h>
28#define NTOS_MODE_USER
29#include <ndk/rtlfuncs.h>
30
31#include <imp_alias.h>
32
33/* The kernel32 names, for direct references */
34
35VOID
38{
40}
41
42VOID
45{
47}
48
49VOID
52{
54}
55
56VOID
59{
61}
62
63VOID
66{
68}
69
73{
75}
76
80{
82}
83
84VOID
86WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable)
87{
89}
90
91VOID
93WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable)
94{
96}
97
101{
102 if (Timeout == INFINITE) return NULL;
103 Time->QuadPart = (ULONGLONG)Timeout * -10000;
104 return Time;
105}
106
107BOOL
108WINAPI
109SleepConditionVariableSRW(PCONDITION_VARIABLE ConditionVariable, PSRWLOCK SRWLock, DWORD Timeout, ULONG Flags)
110{
113
114 Status = RtlSleepConditionVariableSRW(ConditionVariable, SRWLock, GetNtTimeout(&Time, Timeout), Flags);
116 {
118 return FALSE;
119 }
120 return TRUE;
121}
122
123BOOL
124WINAPI
126{
127 return NT_SUCCESS(RtlRunOnceExecuteOnce(InitOnce,
129 Parameter,
130 Context));
131}
132
133/* The dllimport slots, bound directly to the RTL functions where the signatures match */
134
unsigned char BOOLEAN
Definition: actypes.h:127
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_TIMEOUT
Definition: d3dkmdt.h:49
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define SetLastError(x)
Definition: compat.h:752
#define INFINITE
Definition: serial.h:102
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:24
#define IMP_ALIAS_STDCALL(name, size, target)
Definition: imp_alias.h:34
static PSRWLOCK
Definition: sync.c:47
static PLARGE_INTEGER Time
Definition: time.c:37
NTSYSAPI VOID NTAPI RtlWakeAllConditionVariable(_Inout_ PRTL_CONDITION_VARIABLE ConditionVariable)
VOID NTAPI RtlReleaseSRWLockExclusive(IN OUT PRTL_SRWLOCK SRWLock)
Definition: srw.c:710
NTSYSAPI VOID NTAPI RtlWakeConditionVariable(_Inout_ PRTL_CONDITION_VARIABLE ConditionVariable)
VOID NTAPI RtlInitializeSRWLock(OUT PRTL_SRWLOCK SRWLock)
Definition: srw.c:317
VOID NTAPI RtlAcquireSRWLockExclusive(IN OUT PRTL_SRWLOCK SRWLock)
Definition: srw.c:591
NTSYSAPI NTSTATUS NTAPI RtlSleepConditionVariableSRW(_Inout_ PRTL_CONDITION_VARIABLE ConditionVariable, _Inout_ PRTL_SRWLOCK SRWLock, _In_opt_ PLARGE_INTEGER TimeOut, _In_ ULONG Flags)
VOID NTAPI RtlReleaseSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
Definition: srw.c:526
VOID NTAPI RtlAcquireSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
Definition: srw.c:325
static ULONG Timeout
Definition: ping.c:61
_Maybe_raises_SEH_exception_ NTSTATUS NTAPI RtlRunOnceExecuteOnce(_Inout_ PRTL_RUN_ONCE RunOnce, _In_ __inner_callback PRTL_RUN_ONCE_INIT_FN InitFn, _Inout_opt_ PVOID Parameter, _Outptr_opt_result_maybenull_ PVOID *Context)
Definition: runonce.c:121
_In_ PVOID Context
Definition: storport.h:2269
BOOL WINAPI InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter, LPVOID *Context)
Definition: sync_static.c:125
FORCEINLINE PLARGE_INTEGER GetNtTimeout(PLARGE_INTEGER Time, DWORD Timeout)
Definition: sync_static.c:100
BOOLEAN WINAPI TryAcquireSRWLockExclusive(PSRWLOCK SRWLock)
Definition: sync_static.c:72
BOOL WINAPI SleepConditionVariableSRW(PCONDITION_VARIABLE ConditionVariable, PSRWLOCK SRWLock, DWORD Timeout, ULONG Flags)
Definition: sync_static.c:109
BOOLEAN WINAPI TryAcquireSRWLockShared(PSRWLOCK SRWLock)
Definition: sync_static.c:79
VOID WINAPI AcquireSRWLockExclusive(PSRWLOCK SRWLock)
Definition: sync_static.c:44
VOID WINAPI AcquireSRWLockShared(PSRWLOCK SRWLock)
Definition: sync_static.c:51
VOID WINAPI WakeAllConditionVariable(PCONDITION_VARIABLE ConditionVariable)
Definition: sync_static.c:93
VOID WINAPI InitializeSRWLock(PSRWLOCK SRWLock)
Definition: sync_static.c:37
VOID WINAPI ReleaseSRWLockExclusive(PSRWLOCK SRWLock)
Definition: sync_static.c:58
VOID WINAPI ReleaseSRWLockShared(PSRWLOCK SRWLock)
Definition: sync_static.c:65
VOID WINAPI WakeConditionVariable(PCONDITION_VARIABLE ConditionVariable)
Definition: sync_static.c:86
uint64_t ULONGLONG
Definition: typedefs.h:67
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
#define FORCEINLINE
Definition: wdftypes.h:67
BOOL(WINAPI * PINIT_ONCE_FN)(_Inout_ PINIT_ONCE InitOnce, _Inout_opt_ PVOID Parameter, _Outptr_opt_result_maybenull_ PVOID *Context)
Definition: winbase.h:3663
PRTL_RUN_ONCE PINIT_ONCE
Definition: winbase.h:3653
#define WINAPI
Definition: msvc.h:6
NTSYSAPI BOOLEAN WINAPI RtlTryAcquireSRWLockShared(RTL_SRWLOCK *)
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
NTSYSAPI BOOLEAN WINAPI RtlTryAcquireSRWLockExclusive(RTL_SRWLOCK *)
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
_In_ __inner_callback PRTL_RUN_ONCE_INIT_FN InitFn
Definition: rtlfuncs.h:2550
_Inout_opt_ PVOID Parameter
Definition: rtltypes.h:336
RTL_RUN_ONCE_INIT_FN * PRTL_RUN_ONCE_INIT_FN
Definition: rtltypes.h:338