Skip to content

Commit 3b293c1

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 af7fda5 commit 3b293c1

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 45 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,47 @@ 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+
381+
// >= 98 / NT 4
382+
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer
383+
pub fn SetWaitableTimer(htimer: HANDLE,
384+
lpduetime: *const i64,
385+
lperiod: i32,
386+
pfncompletionroutine: PTIMERAPCROUTINE,
387+
lpargtocompletionroutine: *const core::ffi::c_void,
388+
fresume: BOOL
389+
) -> BOOL {
390+
rtabort!("unimplemented")
391+
}
392+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,7 @@ Windows.Win32.System.Pipes.PIPE_TYPE_MESSAGE
24932493
Windows.Win32.System.Pipes.PIPE_WAIT
24942494
Windows.Win32.System.SystemInformation.GetSystemDirectoryW
24952495
Windows.Win32.System.SystemInformation.GetSystemInfo
2496+
Windows.Win32.System.SystemInformation.GetSystemTime
24962497
Windows.Win32.System.SystemInformation.GetSystemTimeAsFileTime
24972498
Windows.Win32.System.SystemInformation.GetSystemTimePreciseAsFileTime
24982499
Windows.Win32.System.SystemInformation.GetVersion
@@ -2618,6 +2619,7 @@ Windows.Win32.System.Threading.WaitForMultipleObjects
26182619
Windows.Win32.System.Threading.WaitForSingleObject
26192620
Windows.Win32.System.Threading.WakeAllConditionVariable
26202621
Windows.Win32.System.Threading.WakeConditionVariable
2622+
Windows.Win32.System.Time.SystemTimeToFileTime
26212623
Windows.Win32.System.WindowsProgramming.FILE_RENAME_FLAG_POSIX_SEMANTICS
26222624
Windows.Win32.System.WindowsProgramming.FILE_RENAME_FLAG_REPLACE_IF_EXISTS
26232625
Windows.Win32.System.WindowsProgramming.PROGRESS_CONTINUE

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ windows_targets::link!("kernel32.dll" "system" fn GetProcessId(process : HANDLE)
6262
windows_targets::link!("kernel32.dll" "system" fn GetStdHandle(nstdhandle : STD_HANDLE) -> HANDLE);
6363
windows_targets::link!("kernel32.dll" "system" fn GetSystemDirectoryW(lpbuffer : PWSTR, usize : u32) -> u32);
6464
windows_targets::link!("kernel32.dll" "system" fn GetSystemInfo(lpsysteminfo : *mut SYSTEM_INFO));
65+
windows_targets::link!("kernel32.dll" "system" fn GetSystemTime(lpsystemtime : *mut SYSTEMTIME));
6566
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimeAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6667
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6768
windows_targets::link!("kernel32.dll" "system" fn GetTempPathW(nbufferlength : u32, lpbuffer : PWSTR) -> u32);
@@ -103,6 +104,7 @@ windows_targets::link!("kernel32.dll" "system" fn Sleep(dwmilliseconds : u32));
103104
windows_targets::link!("kernel32.dll" "system" fn SleepConditionVariableSRW(conditionvariable : *mut CONDITION_VARIABLE, srwlock : *mut SRWLOCK, dwmilliseconds : u32, flags : u32) -> BOOL);
104105
windows_targets::link!("kernel32.dll" "system" fn SleepEx(dwmilliseconds : u32, balertable : BOOL) -> u32);
105106
windows_targets::link!("kernel32.dll" "system" fn SwitchToThread() -> BOOL);
107+
windows_targets::link!("kernel32.dll" "system" fn SystemTimeToFileTime(lpsystemtime : *const SYSTEMTIME, lpfiletime : *mut FILETIME) -> BOOL);
106108
windows_targets::link!("kernel32.dll" "system" fn TerminateProcess(hprocess : HANDLE, uexitcode : u32) -> BOOL);
107109
windows_targets::link!("kernel32.dll" "system" fn TlsAlloc() -> u32);
108110
windows_targets::link!("kernel32.dll" "system" fn TlsFree(dwtlsindex : u32) -> BOOL);
@@ -3073,6 +3075,18 @@ pub type SYNCHRONIZATION_ACCESS_RIGHTS = u32;
30733075
pub const SYNCHRONIZE: FILE_ACCESS_RIGHTS = 1048576u32;
30743076
#[repr(C)]
30753077
#[derive(Clone, Copy)]
3078+
pub struct SYSTEMTIME {
3079+
pub wYear: u16,
3080+
pub wMonth: u16,
3081+
pub wDayOfWeek: u16,
3082+
pub wDay: u16,
3083+
pub wHour: u16,
3084+
pub wMinute: u16,
3085+
pub wSecond: u16,
3086+
pub wMilliseconds: u16,
3087+
}
3088+
#[repr(C)]
3089+
#[derive(Clone, Copy)]
30763090
pub struct SYSTEM_INFO {
30773091
pub Anonymous: SYSTEM_INFO_0,
30783092
pub dwPageSize: u32,

0 commit comments

Comments
 (0)