Skip to content

Commit 3318d4c

Browse files
committed
Add downlevel implementations for Mutex and Condvar
1 parent 44ee6fe commit 3318d4c

File tree

14 files changed

+645
-4
lines changed

14 files changed

+645
-4
lines changed

library/std/src/sys/pal/windows/c.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub const INVALID_HANDLE_VALUE: HANDLE = ::core::ptr::without_provenance_mut(-1i
2121
pub const EXIT_SUCCESS: u32 = 0;
2222
pub const EXIT_FAILURE: u32 = 1;
2323

24-
#[cfg(target_vendor = "win7")]
24+
#[cfg(any(target_vendor = "win7", target_vendor = "rust9x"))]
2525
pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { Ptr: ptr::null_mut() };
26-
#[cfg(target_vendor = "win7")]
26+
#[cfg(any(target_vendor = "win7", target_vendor = "rust9x"))]
2727
pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { Ptr: ptr::null_mut() };
2828
#[cfg(not(target_thread_local))]
2929
pub const INIT_ONCE_STATIC_INIT: INIT_ONCE = INIT_ONCE { Ptr: ptr::null_mut() };
@@ -291,3 +291,44 @@ compat_fn_with_fallback! {
291291
Status as u32
292292
}
293293
}
294+
295+
#[cfg(target_vendor = "rust9x")]
296+
compat_fn_with_fallback! {
297+
pub static KERNEL32: &CStr = c"kernel32" => { load: false, unicows: false };
298+
299+
// >= NT 4
300+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-signalobjectandwait
301+
pub fn SignalObjectAndWait(
302+
hobjecttosignal: HANDLE,
303+
hobjecttowaiton: HANDLE,
304+
dwmilliseconds: u32,
305+
balertable: BOOL
306+
) -> WAIT_EVENT { rtabort!("unimplemented") }
307+
// >= NT 4
308+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-tryentercriticalsection
309+
pub fn TryEnterCriticalSection(lpcriticalsection: *mut CRITICAL_SECTION) -> BOOL {
310+
rtabort!("unimplemented")
311+
}
312+
// >= Win7 / Server 2008 R2
313+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-tryacquiresrwlockexclusive
314+
pub fn TryAcquireSRWLockExclusive(srwlock: *mut SRWLOCK) -> BOOLEAN { rtabort!("unimplemented") }
315+
pub fn TryAcquireSRWLockShared(srwlock: *mut SRWLOCK) -> BOOLEAN { rtabort!("unimplemented") }
316+
// >= Vista / Server 2008
317+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-acquiresrwlockexclusive
318+
pub fn AcquireSRWLockExclusive(srwlock: *mut SRWLOCK) -> () { rtabort!("unimplemented") }
319+
pub fn AcquireSRWLockShared(srwlock: *mut SRWLOCK) -> () { rtabort!("unimplemented") }
320+
pub fn ReleaseSRWLockExclusive(srwlock: *mut SRWLOCK) -> () { rtabort!("unimplemented") }
321+
pub fn ReleaseSRWLockShared(srwlock: *mut SRWLOCK) -> () { rtabort!("unimplemented") }
322+
pub fn SleepConditionVariableSRW(
323+
conditionvariable: *mut CONDITION_VARIABLE,
324+
srwlock: *mut SRWLOCK,
325+
dwmilliseconds: u32,
326+
flags: u32,
327+
) -> BOOL { rtabort!("unimplemented") }
328+
pub fn WakeAllConditionVariable(conditionvariable: *mut CONDITION_VARIABLE) -> () {
329+
rtabort!("unimplemented")
330+
}
331+
pub fn WakeConditionVariable(conditionvariable: *mut CONDITION_VARIABLE) -> () {
332+
rtabort!("unimplemented")
333+
}
334+
}

library/std/src/sys/pal/windows/c/bindings.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,14 +2526,18 @@ Windows.Win32.System.Threading.CREATE_SUSPENDED
25262526
Windows.Win32.System.Threading.CREATE_UNICODE_ENVIRONMENT
25272527
Windows.Win32.System.Threading.CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
25282528
Windows.Win32.System.Threading.CREATE_WAITABLE_TIMER_MANUAL_RESET
2529+
Windows.Win32.System.Threading.CreateEventA
25292530
Windows.Win32.System.Threading.CreateEventW
2531+
Windows.Win32.System.Threading.CreateMutexA
25302532
Windows.Win32.System.Threading.CreateProcessW
25312533
Windows.Win32.System.Threading.CreateThread
25322534
Windows.Win32.System.Threading.CreateWaitableTimerExW
25332535
Windows.Win32.System.Threading.DEBUG_ONLY_THIS_PROCESS
25342536
Windows.Win32.System.Threading.DEBUG_PROCESS
2537+
Windows.Win32.System.Threading.DeleteCriticalSection
25352538
Windows.Win32.System.Threading.DeleteProcThreadAttributeList
25362539
Windows.Win32.System.Threading.DETACHED_PROCESS
2540+
Windows.Win32.System.Threading.EnterCriticalSection
25372541
Windows.Win32.System.Threading.ExitProcess
25382542
Windows.Win32.System.Threading.EXTENDED_STARTUPINFO_PRESENT
25392543
Windows.Win32.System.Threading.GetActiveProcessorCount
@@ -2548,9 +2552,11 @@ Windows.Win32.System.Threading.INFINITE
25482552
Windows.Win32.System.Threading.INHERIT_CALLER_PRIORITY
25492553
Windows.Win32.System.Threading.INHERIT_PARENT_AFFINITY
25502554
Windows.Win32.System.Threading.INIT_ONCE_INIT_FAILED
2555+
Windows.Win32.System.Threading.InitializeCriticalSection
25512556
Windows.Win32.System.Threading.InitializeProcThreadAttributeList
25522557
Windows.Win32.System.Threading.InitOnceBeginInitialize
25532558
Windows.Win32.System.Threading.InitOnceComplete
2559+
Windows.Win32.System.Threading.LeaveCriticalSection
25542560
Windows.Win32.System.Threading.LPPROC_THREAD_ATTRIBUTE_LIST
25552561
Windows.Win32.System.Threading.LPTHREAD_START_ROUTINE
25562562
Windows.Win32.System.Threading.NORMAL_PRIORITY_CLASS
@@ -2563,10 +2569,14 @@ Windows.Win32.System.Threading.PROFILE_KERNEL
25632569
Windows.Win32.System.Threading.PROFILE_SERVER
25642570
Windows.Win32.System.Threading.PROFILE_USER
25652571
Windows.Win32.System.Threading.REALTIME_PRIORITY_CLASS
2572+
Windows.Win32.System.Threading.ReleaseMutex
25662573
Windows.Win32.System.Threading.ReleaseSRWLockExclusive
25672574
Windows.Win32.System.Threading.ReleaseSRWLockShared
2575+
Windows.Win32.System.Threading.ResetEvent
2576+
Windows.Win32.System.Threading.SetEvent
25682577
Windows.Win32.System.Threading.SetThreadStackGuarantee
25692578
Windows.Win32.System.Threading.SetWaitableTimer
2579+
Windows.Win32.System.Threading.SignalObjectAndWait
25702580
Windows.Win32.System.Threading.Sleep
25712581
Windows.Win32.System.Threading.SleepConditionVariableSRW
25722582
Windows.Win32.System.Threading.SleepEx
@@ -2602,6 +2612,7 @@ Windows.Win32.System.Threading.TlsGetValue
26022612
Windows.Win32.System.Threading.TlsSetValue
26032613
Windows.Win32.System.Threading.TryAcquireSRWLockExclusive
26042614
Windows.Win32.System.Threading.TryAcquireSRWLockShared
2615+
Windows.Win32.System.Threading.TryEnterCriticalSection
26052616
Windows.Win32.System.Threading.UpdateProcThreadAttribute
26062617
Windows.Win32.System.Threading.WaitForMultipleObjects
26072618
Windows.Win32.System.Threading.WaitForSingleObject

library/std/src/sys/pal/windows/c/windows_sys.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE)
1111
windows_targets::link!("kernel32.dll" "system" fn CompareStringOrdinal(lpstring1 : PCWSTR, cchcount1 : i32, lpstring2 : PCWSTR, cchcount2 : i32, bignorecase : BOOL) -> COMPARESTRING_RESULT);
1212
windows_targets::link!("kernel32.dll" "system" fn CopyFileExW(lpexistingfilename : PCWSTR, lpnewfilename : PCWSTR, lpprogressroutine : LPPROGRESS_ROUTINE, lpdata : *const core::ffi::c_void, pbcancel : *mut BOOL, dwcopyflags : u32) -> BOOL);
1313
windows_targets::link!("kernel32.dll" "system" fn CreateDirectoryW(lppathname : PCWSTR, lpsecurityattributes : *const SECURITY_ATTRIBUTES) -> BOOL);
14+
windows_targets::link!("kernel32.dll" "system" fn CreateEventA(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCSTR) -> HANDLE);
1415
windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE);
1516
windows_targets::link!("kernel32.dll" "system" fn CreateFileW(lpfilename : PCWSTR, dwdesiredaccess : u32, dwsharemode : FILE_SHARE_MODE, lpsecurityattributes : *const SECURITY_ATTRIBUTES, dwcreationdisposition : FILE_CREATION_DISPOSITION, dwflagsandattributes : FILE_FLAGS_AND_ATTRIBUTES, htemplatefile : HANDLE) -> HANDLE);
1617
windows_targets::link!("kernel32.dll" "system" fn CreateHardLinkW(lpfilename : PCWSTR, lpexistingfilename : PCWSTR, lpsecurityattributes : *const SECURITY_ATTRIBUTES) -> BOOL);
18+
windows_targets::link!("kernel32.dll" "system" fn CreateMutexA(lpmutexattributes : *const SECURITY_ATTRIBUTES, binitialowner : BOOL, lpname : PCSTR) -> HANDLE);
1719
windows_targets::link!("kernel32.dll" "system" fn CreateNamedPipeW(lpname : PCWSTR, dwopenmode : FILE_FLAGS_AND_ATTRIBUTES, dwpipemode : NAMED_PIPE_MODE, nmaxinstances : u32, noutbuffersize : u32, ninbuffersize : u32, ndefaulttimeout : u32, lpsecurityattributes : *const SECURITY_ATTRIBUTES) -> HANDLE);
1820
windows_targets::link!("kernel32.dll" "system" fn CreatePipe(hreadpipe : *mut HANDLE, hwritepipe : *mut HANDLE, lppipeattributes : *const SECURITY_ATTRIBUTES, nsize : u32) -> BOOL);
1921
windows_targets::link!("kernel32.dll" "system" fn CreateProcessW(lpapplicationname : PCWSTR, lpcommandline : PWSTR, lpprocessattributes : *const SECURITY_ATTRIBUTES, lpthreadattributes : *const SECURITY_ATTRIBUTES, binherithandles : BOOL, dwcreationflags : PROCESS_CREATION_FLAGS, lpenvironment : *const core::ffi::c_void, lpcurrentdirectory : PCWSTR, lpstartupinfo : *const STARTUPINFOW, lpprocessinformation : *mut PROCESS_INFORMATION) -> BOOL);
2022
windows_targets::link!("kernel32.dll" "system" fn CreateSymbolicLinkW(lpsymlinkfilename : PCWSTR, lptargetfilename : PCWSTR, dwflags : SYMBOLIC_LINK_FLAGS) -> BOOLEAN);
2123
windows_targets::link!("kernel32.dll" "system" fn CreateThread(lpthreadattributes : *const SECURITY_ATTRIBUTES, dwstacksize : usize, lpstartaddress : LPTHREAD_START_ROUTINE, lpparameter : *const core::ffi::c_void, dwcreationflags : THREAD_CREATION_FLAGS, lpthreadid : *mut u32) -> HANDLE);
2224
windows_targets::link!("kernel32.dll" "system" fn CreateWaitableTimerExW(lptimerattributes : *const SECURITY_ATTRIBUTES, lptimername : PCWSTR, dwflags : u32, dwdesiredaccess : u32) -> HANDLE);
25+
windows_targets::link!("kernel32.dll" "system" fn DeleteCriticalSection(lpcriticalsection : *mut CRITICAL_SECTION));
2326
windows_targets::link!("kernel32.dll" "system" fn DeleteFileW(lpfilename : PCWSTR) -> BOOL);
2427
windows_targets::link!("kernel32.dll" "system" fn DeleteProcThreadAttributeList(lpattributelist : LPPROC_THREAD_ATTRIBUTE_LIST));
2528
windows_targets::link!("kernel32.dll" "system" fn DeviceIoControl(hdevice : HANDLE, dwiocontrolcode : u32, lpinbuffer : *const core::ffi::c_void, ninbuffersize : u32, lpoutbuffer : *mut core::ffi::c_void, noutbuffersize : u32, lpbytesreturned : *mut u32, lpoverlapped : *mut OVERLAPPED) -> BOOL);
2629
windows_targets::link!("kernel32.dll" "system" fn DuplicateHandle(hsourceprocesshandle : HANDLE, hsourcehandle : HANDLE, htargetprocesshandle : HANDLE, lptargethandle : *mut HANDLE, dwdesiredaccess : u32, binherithandle : BOOL, dwoptions : DUPLICATE_HANDLE_OPTIONS) -> BOOL);
30+
windows_targets::link!("kernel32.dll" "system" fn EnterCriticalSection(lpcriticalsection : *mut CRITICAL_SECTION));
2731
windows_targets::link!("kernel32.dll" "system" fn ExitProcess(uexitcode : u32) -> !);
2832
windows_targets::link!("kernel32.dll" "system" fn FindClose(hfindfile : HANDLE) -> BOOL);
2933
windows_targets::link!("kernel32.dll" "system" fn FindFirstFileExW(lpfilename : PCWSTR, finfolevelid : FINDEX_INFO_LEVELS, lpfindfiledata : *mut core::ffi::c_void, fsearchop : FINDEX_SEARCH_OPS, lpsearchfilter : *const core::ffi::c_void, dwadditionalflags : FIND_FIRST_EX_FLAGS) -> HANDLE);
@@ -65,7 +69,9 @@ windows_targets::link!("kernel32.dll" "system" fn GetVersion() -> u32);
6569
windows_targets::link!("kernel32.dll" "system" fn GetWindowsDirectoryW(lpbuffer : PWSTR, usize : u32) -> u32);
6670
windows_targets::link!("kernel32.dll" "system" fn InitOnceBeginInitialize(lpinitonce : *mut INIT_ONCE, dwflags : u32, fpending : *mut BOOL, lpcontext : *mut *mut core::ffi::c_void) -> BOOL);
6771
windows_targets::link!("kernel32.dll" "system" fn InitOnceComplete(lpinitonce : *mut INIT_ONCE, dwflags : u32, lpcontext : *const core::ffi::c_void) -> BOOL);
72+
windows_targets::link!("kernel32.dll" "system" fn InitializeCriticalSection(lpcriticalsection : *mut CRITICAL_SECTION));
6873
windows_targets::link!("kernel32.dll" "system" fn InitializeProcThreadAttributeList(lpattributelist : LPPROC_THREAD_ATTRIBUTE_LIST, dwattributecount : u32, dwflags : u32, lpsize : *mut usize) -> BOOL);
74+
windows_targets::link!("kernel32.dll" "system" fn LeaveCriticalSection(lpcriticalsection : *mut CRITICAL_SECTION));
6975
windows_targets::link!("kernel32.dll" "system" fn LoadLibraryA(lplibfilename : PCSTR) -> HMODULE);
7076
windows_targets::link!("kernel32.dll" "system" fn LocalFree(hmem : HLOCAL) -> HLOCAL);
7177
windows_targets::link!("kernel32.dll" "system" fn LockFileEx(hfile : HANDLE, dwflags : LOCK_FILE_FLAGS, dwreserved : u32, nnumberofbytestolocklow : u32, nnumberofbytestolockhigh : u32, lpoverlapped : *mut OVERLAPPED) -> BOOL);
@@ -76,11 +82,14 @@ windows_targets::link!("kernel32.dll" "system" fn QueryPerformanceFrequency(lpfr
7682
windows_targets::link!("kernel32.dll" "system" fn ReadConsoleW(hconsoleinput : HANDLE, lpbuffer : *mut core::ffi::c_void, nnumberofcharstoread : u32, lpnumberofcharsread : *mut u32, pinputcontrol : *const CONSOLE_READCONSOLE_CONTROL) -> BOOL);
7783
windows_targets::link!("kernel32.dll" "system" fn ReadFile(hfile : HANDLE, lpbuffer : *mut u8, nnumberofbytestoread : u32, lpnumberofbytesread : *mut u32, lpoverlapped : *mut OVERLAPPED) -> BOOL);
7884
windows_targets::link!("kernel32.dll" "system" fn ReadFileEx(hfile : HANDLE, lpbuffer : *mut u8, nnumberofbytestoread : u32, lpoverlapped : *mut OVERLAPPED, lpcompletionroutine : LPOVERLAPPED_COMPLETION_ROUTINE) -> BOOL);
85+
windows_targets::link!("kernel32.dll" "system" fn ReleaseMutex(hmutex : HANDLE) -> BOOL);
7986
windows_targets::link!("kernel32.dll" "system" fn ReleaseSRWLockExclusive(srwlock : *mut SRWLOCK));
8087
windows_targets::link!("kernel32.dll" "system" fn ReleaseSRWLockShared(srwlock : *mut SRWLOCK));
8188
windows_targets::link!("kernel32.dll" "system" fn RemoveDirectoryW(lppathname : PCWSTR) -> BOOL);
89+
windows_targets::link!("kernel32.dll" "system" fn ResetEvent(hevent : HANDLE) -> BOOL);
8290
windows_targets::link!("kernel32.dll" "system" fn SetCurrentDirectoryW(lppathname : PCWSTR) -> BOOL);
8391
windows_targets::link!("kernel32.dll" "system" fn SetEnvironmentVariableW(lpname : PCWSTR, lpvalue : PCWSTR) -> BOOL);
92+
windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
8493
windows_targets::link!("kernel32.dll" "system" fn SetFileAttributesW(lpfilename : PCWSTR, dwfileattributes : FILE_FLAGS_AND_ATTRIBUTES) -> BOOL);
8594
windows_targets::link!("kernel32.dll" "system" fn SetFileInformationByHandle(hfile : HANDLE, fileinformationclass : FILE_INFO_BY_HANDLE_CLASS, lpfileinformation : *const core::ffi::c_void, dwbuffersize : u32) -> BOOL);
8695
windows_targets::link!("kernel32.dll" "system" fn SetFilePointerEx(hfile : HANDLE, lidistancetomove : i64, lpnewfilepointer : *mut i64, dwmovemethod : SET_FILE_POINTER_MOVE_METHOD) -> BOOL);
@@ -89,6 +98,7 @@ windows_targets::link!("kernel32.dll" "system" fn SetHandleInformation(hobject :
8998
windows_targets::link!("kernel32.dll" "system" fn SetLastError(dwerrcode : WIN32_ERROR));
9099
windows_targets::link!("kernel32.dll" "system" fn SetThreadStackGuarantee(stacksizeinbytes : *mut u32) -> BOOL);
91100
windows_targets::link!("kernel32.dll" "system" fn SetWaitableTimer(htimer : HANDLE, lpduetime : *const i64, lperiod : i32, pfncompletionroutine : PTIMERAPCROUTINE, lpargtocompletionroutine : *const core::ffi::c_void, fresume : BOOL) -> BOOL);
101+
windows_targets::link!("kernel32.dll" "system" fn SignalObjectAndWait(hobjecttosignal : HANDLE, hobjecttowaiton : HANDLE, dwmilliseconds : u32, balertable : BOOL) -> WAIT_EVENT);
92102
windows_targets::link!("kernel32.dll" "system" fn Sleep(dwmilliseconds : u32));
93103
windows_targets::link!("kernel32.dll" "system" fn SleepConditionVariableSRW(conditionvariable : *mut CONDITION_VARIABLE, srwlock : *mut SRWLOCK, dwmilliseconds : u32, flags : u32) -> BOOL);
94104
windows_targets::link!("kernel32.dll" "system" fn SleepEx(dwmilliseconds : u32, balertable : BOOL) -> u32);
@@ -100,6 +110,7 @@ windows_targets::link!("kernel32.dll" "system" fn TlsGetValue(dwtlsindex : u32)
100110
windows_targets::link!("kernel32.dll" "system" fn TlsSetValue(dwtlsindex : u32, lptlsvalue : *const core::ffi::c_void) -> BOOL);
101111
windows_targets::link!("kernel32.dll" "system" fn TryAcquireSRWLockExclusive(srwlock : *mut SRWLOCK) -> BOOLEAN);
102112
windows_targets::link!("kernel32.dll" "system" fn TryAcquireSRWLockShared(srwlock : *mut SRWLOCK) -> BOOLEAN);
113+
windows_targets::link!("kernel32.dll" "system" fn TryEnterCriticalSection(lpcriticalsection : *mut CRITICAL_SECTION) -> BOOL);
103114
windows_targets::link!("kernel32.dll" "system" fn UnlockFile(hfile : HANDLE, dwfileoffsetlow : u32, dwfileoffsethigh : u32, nnumberofbytestounlocklow : u32, nnumberofbytestounlockhigh : u32) -> BOOL);
104115
windows_targets::link!("kernel32.dll" "system" fn UpdateProcThreadAttribute(lpattributelist : LPPROC_THREAD_ATTRIBUTE_LIST, dwflags : u32, attribute : usize, lpvalue : *const core::ffi::c_void, cbsize : usize, lppreviousvalue : *mut core::ffi::c_void, lpreturnsize : *const usize) -> BOOL);
105116
windows_targets::link!("kernel32.dll" "system" fn WaitForMultipleObjects(ncount : u32, lphandles : *const HANDLE, bwaitall : BOOL, dwmilliseconds : u32) -> WAIT_EVENT);
@@ -399,6 +410,29 @@ pub const CREATE_SUSPENDED: PROCESS_CREATION_FLAGS = 4u32;
399410
pub const CREATE_UNICODE_ENVIRONMENT: PROCESS_CREATION_FLAGS = 1024u32;
400411
pub const CREATE_WAITABLE_TIMER_HIGH_RESOLUTION: u32 = 2u32;
401412
pub const CREATE_WAITABLE_TIMER_MANUAL_RESET: u32 = 1u32;
413+
#[repr(C)]
414+
#[derive(Clone, Copy)]
415+
pub struct CRITICAL_SECTION {
416+
pub DebugInfo: *mut CRITICAL_SECTION_DEBUG,
417+
pub LockCount: i32,
418+
pub RecursionCount: i32,
419+
pub OwningThread: HANDLE,
420+
pub LockSemaphore: HANDLE,
421+
pub SpinCount: usize,
422+
}
423+
#[repr(C)]
424+
#[derive(Clone, Copy)]
425+
pub struct CRITICAL_SECTION_DEBUG {
426+
pub Type: u16,
427+
pub CreatorBackTraceIndex: u16,
428+
pub CriticalSection: *mut CRITICAL_SECTION,
429+
pub ProcessLocksList: LIST_ENTRY,
430+
pub EntryCount: u32,
431+
pub ContentionCount: u32,
432+
pub Flags: u32,
433+
pub CreatorBackTraceIndexHigh: u16,
434+
pub Identifier: u16,
435+
}
402436
pub const CSTR_EQUAL: COMPARESTRING_RESULT = 2i32;
403437
pub const CSTR_GREATER_THAN: COMPARESTRING_RESULT = 3i32;
404438
pub const CSTR_LESS_THAN: COMPARESTRING_RESULT = 1i32;
@@ -2751,6 +2785,12 @@ pub struct LINGER {
27512785
pub l_onoff: u16,
27522786
pub l_linger: u16,
27532787
}
2788+
#[repr(C)]
2789+
#[derive(Clone, Copy)]
2790+
pub struct LIST_ENTRY {
2791+
pub Flink: *mut LIST_ENTRY,
2792+
pub Blink: *mut LIST_ENTRY,
2793+
}
27542794
pub const LOCKFILE_EXCLUSIVE_LOCK: LOCK_FILE_FLAGS = 2u32;
27552795
pub const LOCKFILE_FAIL_IMMEDIATELY: LOCK_FILE_FLAGS = 1u32;
27562796
pub type LOCK_FILE_FLAGS = u32;

library/std/src/sys/pal/windows/compat/checks.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn init_rust9x_checks() {
2323
// do any dynamic allocation, don't call LoadLibrary, etc.
2424

2525
init_windows_version_check();
26+
init_mutex_kind_check();
2627
}
2728

2829
static mut IS_NT: bool = true;
@@ -31,3 +32,38 @@ fn init_windows_version_check() {
3132
// according to old MSDN info, the high-order bit is set only on 95/98/ME.
3233
unsafe { IS_NT = c::GetVersion() < 0x8000_0000 };
3334
}
35+
36+
#[derive(Clone, Copy, PartialEq)]
37+
pub(crate) enum MutexKind {
38+
/// Win 7+ (Vista doesn't support the `Try*` APIs)
39+
SrwLock,
40+
/// NT 4+ (9x/ME/NT3.x support critical sections, but don't support `TryEnterCriticalSection`)
41+
CriticalSection,
42+
/// `CreateMutex`, available everywhere
43+
Legacy,
44+
}
45+
46+
static mut MUTEX_KIND: MutexKind = MutexKind::Legacy;
47+
48+
#[inline(always)]
49+
pub(crate) fn mutex_kind() -> MutexKind {
50+
unsafe { MUTEX_KIND }
51+
}
52+
53+
fn init_mutex_kind_check() {
54+
let kind = if c::TryAcquireSRWLockExclusive::available().is_some() {
55+
MutexKind::SrwLock
56+
} else if {
57+
// Windows 9x exports `TryEnterCriticalSection`, but it returns ERROR_CALL_NOT_IMPLEMENTED.
58+
// MSDN specifies that the function is available on NT4 and later.
59+
is_windows_nt() && c::TryEnterCriticalSection::available().is_some()
60+
} {
61+
MutexKind::CriticalSection
62+
} else {
63+
MutexKind::Legacy
64+
};
65+
66+
unsafe {
67+
MUTEX_KIND = kind;
68+
}
69+
}

library/std/src/sys/pal/windows/futex.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use core::ffi::c_void;
24
use core::sync::atomic::{
35
AtomicBool, AtomicI8, AtomicI16, AtomicI32, AtomicI64, AtomicIsize, AtomicPtr, AtomicU8,

library/std/src/sys/sync/condvar/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cfg_if::cfg_if! {
22
if #[cfg(any(
3-
all(target_os = "windows", not(target_vendor="win7")),
3+
all(target_os = "windows", not(any(target_vendor="win7", target_vendor = "rust9x"))),
44
target_os = "linux",
55
target_os = "android",
66
target_os = "freebsd",
@@ -21,6 +21,10 @@ cfg_if::cfg_if! {
2121
} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
2222
mod windows7;
2323
pub use windows7::Condvar;
24+
} else if #[cfg(all(target_os = "windows", target_vendor = "rust9x"))] {
25+
mod windows7;
26+
mod rust9x;
27+
pub use rust9x::Condvar;
2428
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
2529
mod sgx;
2630
pub use sgx::Condvar;

0 commit comments

Comments
 (0)