Skip to content

Commit 568f04b

Browse files
committed
Add fallback impls for sleep and time functions
- `GetSystemTimePreciseAsFileTime`: fall back to `GetSystemTimeAsFileTime` - `GetSystemTimeAsFileTime`: fall back to `GetSystemTime` and `SystemTimeToFileTime` - (only necessary for NT 3.1) - `SwitchToThread`: fall back to `Sleep(0)` - `CreateWaitableTimerExW`: optional import, fallback already in place
1 parent 738e445 commit 568f04b

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ compat_fn_with_fallback! {
140140

141141
// >= Win8 / Server 2012
142142
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
143-
#[cfg(target_vendor = "win7")]
143+
#[cfg(any(target_vendor = "win7", target_vendor = "rust9x"))]
144144
pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () {
145145
unsafe { GetSystemTimeAsFileTime(lpsystemtimeasfiletime) }
146146
}
@@ -346,3 +346,35 @@ compat_fn_with_fallback! {
346346
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadstackguarantee
347347
pub fn SetThreadStackGuarantee(stacksizeinbytes: *mut u32) -> BOOL { TRUE }
348348
}
349+
350+
#[cfg(target_vendor = "rust9x")]
351+
compat_fn_with_fallback! {
352+
pub static KERNEL32: &CStr = c"kernel32" => { load: false, unicows: false };
353+
// >= 95 / NT 3.5
354+
// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime
355+
pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: *mut FILETIME) {
356+
unsafe {
357+
// implementation based on old MSDN docs
358+
let mut st: SYSTEMTIME = crate::mem::zeroed();
359+
GetSystemTime(&mut st);
360+
crate::sys::cvt(SystemTimeToFileTime(&st, lpSystemTimeAsFileTime)).unwrap();
361+
}
362+
}
363+
// >= NT 4
364+
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-switchtothread
365+
pub fn SwitchToThread() -> BOOL {
366+
unsafe { Sleep(0); }
367+
TRUE
368+
}
369+
370+
// >= Vista / Server 2008
371+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createwaitabletimerexw
372+
pub fn CreateWaitableTimerExW(
373+
lptimerattributes: *const SECURITY_ATTRIBUTES,
374+
lptimername: PCWSTR,
375+
dwflags: u32,
376+
dwdesiredaccess: u32
377+
) -> HANDLE {
378+
ptr::null_mut()
379+
}
380+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,7 @@ Windows.Win32.System.Pipes.PIPE_TYPE_MESSAGE
24912491
Windows.Win32.System.Pipes.PIPE_WAIT
24922492
Windows.Win32.System.SystemInformation.GetSystemDirectoryW
24932493
Windows.Win32.System.SystemInformation.GetSystemInfo
2494+
Windows.Win32.System.SystemInformation.GetSystemTime
24942495
Windows.Win32.System.SystemInformation.GetSystemTimeAsFileTime
24952496
Windows.Win32.System.SystemInformation.GetSystemTimePreciseAsFileTime
24962497
Windows.Win32.System.SystemInformation.GetVersion
@@ -2616,5 +2617,6 @@ Windows.Win32.System.Threading.WaitForMultipleObjects
26162617
Windows.Win32.System.Threading.WaitForSingleObject
26172618
Windows.Win32.System.Threading.WakeAllConditionVariable
26182619
Windows.Win32.System.Threading.WakeConditionVariable
2620+
Windows.Win32.System.Time.SystemTimeToFileTime
26192621
Windows.Win32.System.WindowsProgramming.PROGRESS_CONTINUE
26202622
Windows.Win32.UI.Shell.GetUserProfileDirectoryW

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ windows_targets::link!("kernel32.dll" "system" fn GetProcessId(process : HANDLE)
6161
windows_targets::link!("kernel32.dll" "system" fn GetStdHandle(nstdhandle : STD_HANDLE) -> HANDLE);
6262
windows_targets::link!("kernel32.dll" "system" fn GetSystemDirectoryW(lpbuffer : PWSTR, usize : u32) -> u32);
6363
windows_targets::link!("kernel32.dll" "system" fn GetSystemInfo(lpsysteminfo : *mut SYSTEM_INFO));
64+
windows_targets::link!("kernel32.dll" "system" fn GetSystemTime(lpsystemtime : *mut SYSTEMTIME));
6465
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimeAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6566
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6667
windows_targets::link!("kernel32.dll" "system" fn GetTempPathW(nbufferlength : u32, lpbuffer : PWSTR) -> u32);
@@ -102,6 +103,7 @@ windows_targets::link!("kernel32.dll" "system" fn Sleep(dwmilliseconds : u32));
102103
windows_targets::link!("kernel32.dll" "system" fn SleepConditionVariableSRW(conditionvariable : *mut CONDITION_VARIABLE, srwlock : *mut SRWLOCK, dwmilliseconds : u32, flags : u32) -> BOOL);
103104
windows_targets::link!("kernel32.dll" "system" fn SleepEx(dwmilliseconds : u32, balertable : BOOL) -> u32);
104105
windows_targets::link!("kernel32.dll" "system" fn SwitchToThread() -> BOOL);
106+
windows_targets::link!("kernel32.dll" "system" fn SystemTimeToFileTime(lpsystemtime : *const SYSTEMTIME, lpfiletime : *mut FILETIME) -> BOOL);
105107
windows_targets::link!("kernel32.dll" "system" fn TerminateProcess(hprocess : HANDLE, uexitcode : u32) -> BOOL);
106108
windows_targets::link!("kernel32.dll" "system" fn TlsAlloc() -> u32);
107109
windows_targets::link!("kernel32.dll" "system" fn TlsFree(dwtlsindex : u32) -> BOOL);
@@ -3056,6 +3058,18 @@ pub type SYNCHRONIZATION_ACCESS_RIGHTS = u32;
30563058
pub const SYNCHRONIZE: FILE_ACCESS_RIGHTS = 1048576u32;
30573059
#[repr(C)]
30583060
#[derive(Clone, Copy)]
3061+
pub struct SYSTEMTIME {
3062+
pub wYear: u16,
3063+
pub wMonth: u16,
3064+
pub wDayOfWeek: u16,
3065+
pub wDay: u16,
3066+
pub wHour: u16,
3067+
pub wMinute: u16,
3068+
pub wSecond: u16,
3069+
pub wMilliseconds: u16,
3070+
}
3071+
#[repr(C)]
3072+
#[derive(Clone, Copy)]
30593073
pub struct SYSTEM_INFO {
30603074
pub Anonymous: SYSTEM_INFO_0,
30613075
pub dwPageSize: u32,

0 commit comments

Comments
 (0)