Skip to content

Commit cee6db1

Browse files
committed
Auto merge of #116461 - ChrisDenton:sleep, r=thomcc
Windows: Support sub-millisecond sleep Use `CreateWaitableTimerExW` with `CREATE_WAITABLE_TIMER_HIGH_RESOLUTION`. Does not work before Windows 10, version 1803 so in that case we fallback to using `Sleep`. I've created a `WaitableTimer` type so it can one day be adapted to also support waiting to an absolute time (which has been talked about). Note though that it currently returns `Err(())` because we can't do anything with the errors other than fallback to the old `Sleep`. Feel free to tell me to do errors properly. It just didn't seem worth constructing an `io::Error` if we're never going to surface it to the user. And it *should* all be infallible anyway unless the OS is too old to support it. Closes #43376
2 parents 271dcc1 + fbf2567 commit cee6db1

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

library/std/src/sys/windows/c/windows_sys.lst

+6
Original file line numberDiff line numberDiff line change
@@ -2505,9 +2505,12 @@ Windows.Win32.System.Threading.CREATE_SEPARATE_WOW_VDM
25052505
Windows.Win32.System.Threading.CREATE_SHARED_WOW_VDM
25062506
Windows.Win32.System.Threading.CREATE_SUSPENDED
25072507
Windows.Win32.System.Threading.CREATE_UNICODE_ENVIRONMENT
2508+
Windows.Win32.System.Threading.CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
2509+
Windows.Win32.System.Threading.CREATE_WAITABLE_TIMER_MANUAL_RESET
25082510
Windows.Win32.System.Threading.CreateEventW
25092511
Windows.Win32.System.Threading.CreateProcessW
25102512
Windows.Win32.System.Threading.CreateThread
2513+
Windows.Win32.System.Threading.CreateWaitableTimerExW
25112514
Windows.Win32.System.Threading.DEBUG_ONLY_THIS_PROCESS
25122515
Windows.Win32.System.Threading.DEBUG_PROCESS
25132516
Windows.Win32.System.Threading.DeleteProcThreadAttributeList
@@ -2544,6 +2547,7 @@ Windows.Win32.System.Threading.REALTIME_PRIORITY_CLASS
25442547
Windows.Win32.System.Threading.ReleaseSRWLockExclusive
25452548
Windows.Win32.System.Threading.ReleaseSRWLockShared
25462549
Windows.Win32.System.Threading.SetThreadStackGuarantee
2550+
Windows.Win32.System.Threading.SetWaitableTimer
25472551
Windows.Win32.System.Threading.Sleep
25482552
Windows.Win32.System.Threading.SleepConditionVariableSRW
25492553
Windows.Win32.System.Threading.SleepEx
@@ -2570,6 +2574,8 @@ Windows.Win32.System.Threading.TerminateProcess
25702574
Windows.Win32.System.Threading.THREAD_CREATE_RUN_IMMEDIATELY
25712575
Windows.Win32.System.Threading.THREAD_CREATE_SUSPENDED
25722576
Windows.Win32.System.Threading.THREAD_CREATION_FLAGS
2577+
Windows.Win32.System.Threading.TIMER_ALL_ACCESS
2578+
Windows.Win32.System.Threading.TIMER_MODIFY_STATE
25732579
Windows.Win32.System.Threading.TLS_OUT_OF_INDEXES
25742580
Windows.Win32.System.Threading.TlsAlloc
25752581
Windows.Win32.System.Threading.TlsFree

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

+32
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ extern "system" {
151151
) -> HANDLE;
152152
}
153153
#[link(name = "kernel32")]
154+
extern "system" {
155+
pub fn CreateWaitableTimerExW(
156+
lptimerattributes: *const SECURITY_ATTRIBUTES,
157+
lptimername: PCWSTR,
158+
dwflags: u32,
159+
dwdesiredaccess: u32,
160+
) -> HANDLE;
161+
}
162+
#[link(name = "kernel32")]
154163
extern "system" {
155164
pub fn DeleteFileW(lpfilename: PCWSTR) -> BOOL;
156165
}
@@ -508,6 +517,17 @@ extern "system" {
508517
pub fn SetThreadStackGuarantee(stacksizeinbytes: *mut u32) -> BOOL;
509518
}
510519
#[link(name = "kernel32")]
520+
extern "system" {
521+
pub fn SetWaitableTimer(
522+
htimer: HANDLE,
523+
lpduetime: *const i64,
524+
lperiod: i32,
525+
pfncompletionroutine: PTIMERAPCROUTINE,
526+
lpargtocompletionroutine: *const ::core::ffi::c_void,
527+
fresume: BOOL,
528+
) -> BOOL;
529+
}
530+
#[link(name = "kernel32")]
511531
extern "system" {
512532
pub fn Sleep(dwmilliseconds: u32) -> ();
513533
}
@@ -1165,6 +1185,8 @@ pub const CREATE_SEPARATE_WOW_VDM: PROCESS_CREATION_FLAGS = 2048u32;
11651185
pub const CREATE_SHARED_WOW_VDM: PROCESS_CREATION_FLAGS = 4096u32;
11661186
pub const CREATE_SUSPENDED: PROCESS_CREATION_FLAGS = 4u32;
11671187
pub const CREATE_UNICODE_ENVIRONMENT: PROCESS_CREATION_FLAGS = 1024u32;
1188+
pub const CREATE_WAITABLE_TIMER_HIGH_RESOLUTION: u32 = 2u32;
1189+
pub const CREATE_WAITABLE_TIMER_MANUAL_RESET: u32 = 1u32;
11681190
pub const CSTR_EQUAL: COMPARESTRING_RESULT = 2i32;
11691191
pub const CSTR_GREATER_THAN: COMPARESTRING_RESULT = 3i32;
11701192
pub const CSTR_LESS_THAN: COMPARESTRING_RESULT = 1i32;
@@ -3775,6 +3797,13 @@ pub const PROFILE_SERVER: PROCESS_CREATION_FLAGS = 1073741824u32;
37753797
pub const PROFILE_USER: PROCESS_CREATION_FLAGS = 268435456u32;
37763798
pub const PROGRESS_CONTINUE: u32 = 0u32;
37773799
pub type PSTR = *mut u8;
3800+
pub type PTIMERAPCROUTINE = ::core::option::Option<
3801+
unsafe extern "system" fn(
3802+
lpargtocompletionroutine: *const ::core::ffi::c_void,
3803+
dwtimerlowvalue: u32,
3804+
dwtimerhighvalue: u32,
3805+
) -> (),
3806+
>;
37783807
pub type PWSTR = *mut u16;
37793808
pub const READ_CONTROL: FILE_ACCESS_RIGHTS = 131072u32;
37803809
pub const REALTIME_PRIORITY_CLASS: PROCESS_CREATION_FLAGS = 256u32;
@@ -3922,6 +3951,7 @@ pub type SYMBOLIC_LINK_FLAGS = u32;
39223951
pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: SYMBOLIC_LINK_FLAGS = 2u32;
39233952
pub const SYMBOLIC_LINK_FLAG_DIRECTORY: SYMBOLIC_LINK_FLAGS = 1u32;
39243953
pub const SYMLINK_FLAG_RELATIVE: u32 = 1u32;
3954+
pub type SYNCHRONIZATION_ACCESS_RIGHTS = u32;
39253955
pub const SYNCHRONIZE: FILE_ACCESS_RIGHTS = 1048576u32;
39263956
#[repr(C)]
39273957
pub struct SYSTEM_INFO {
@@ -3968,6 +3998,8 @@ pub const TCP_NODELAY: i32 = 1i32;
39683998
pub const THREAD_CREATE_RUN_IMMEDIATELY: THREAD_CREATION_FLAGS = 0u32;
39693999
pub const THREAD_CREATE_SUSPENDED: THREAD_CREATION_FLAGS = 4u32;
39704000
pub type THREAD_CREATION_FLAGS = u32;
4001+
pub const TIMER_ALL_ACCESS: SYNCHRONIZATION_ACCESS_RIGHTS = 2031619u32;
4002+
pub const TIMER_MODIFY_STATE: SYNCHRONIZATION_ACCESS_RIGHTS = 2u32;
39714003
#[repr(C)]
39724004
pub struct TIMEVAL {
39734005
pub tv_sec: i32,

library/std/src/sys/windows/thread.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::time::Duration;
1212

1313
use core::ffi::c_void;
1414

15+
use super::time::WaitableTimer;
1516
use super::to_u16s;
1617

1718
pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
@@ -87,7 +88,17 @@ impl Thread {
8788
}
8889

8990
pub fn sleep(dur: Duration) {
90-
unsafe { c::Sleep(super::dur2timeout(dur)) }
91+
fn high_precision_sleep(dur: Duration) -> Result<(), ()> {
92+
let timer = WaitableTimer::high_resolution()?;
93+
timer.set(dur)?;
94+
timer.wait()
95+
}
96+
// Attempt to use high-precision sleep (Windows 10, version 1803+).
97+
// On error fallback to the standard `Sleep` function.
98+
// Also preserves the zero duration behaviour of `Sleep`.
99+
if dur.is_zero() || high_precision_sleep(dur).is_err() {
100+
unsafe { c::Sleep(super::dur2timeout(dur)) }
101+
}
91102
}
92103

93104
pub fn handle(&self) -> &Handle {

library/std/src/sys/windows/time.rs

+38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::cmp::Ordering;
22
use crate::fmt;
33
use crate::mem;
4+
use crate::ptr::{null, null_mut};
45
use crate::sys::c;
56
use crate::sys_common::IntoInner;
67
use crate::time::Duration;
78

89
use core::hash::{Hash, Hasher};
10+
use core::ops::Neg;
911

1012
const NANOS_PER_SEC: u64 = 1_000_000_000;
1113
const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
@@ -222,3 +224,39 @@ mod perf_counter {
222224
qpc_value
223225
}
224226
}
227+
228+
/// A timer you can wait on.
229+
pub(super) struct WaitableTimer {
230+
handle: c::HANDLE,
231+
}
232+
impl WaitableTimer {
233+
/// Create a high-resolution timer. Will fail before Windows 10, version 1803.
234+
pub fn high_resolution() -> Result<Self, ()> {
235+
let handle = unsafe {
236+
c::CreateWaitableTimerExW(
237+
null(),
238+
null(),
239+
c::CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
240+
c::TIMER_ALL_ACCESS,
241+
)
242+
};
243+
if handle != null_mut() { Ok(Self { handle }) } else { Err(()) }
244+
}
245+
pub fn set(&self, duration: Duration) -> Result<(), ()> {
246+
// Convert the Duration to a format similar to FILETIME.
247+
// Negative values are relative times whereas positive values are absolute.
248+
// Therefore we negate the relative duration.
249+
let time = checked_dur2intervals(&duration).ok_or(())?.neg();
250+
let result = unsafe { c::SetWaitableTimer(self.handle, &time, 0, None, null(), c::FALSE) };
251+
if result != 0 { Ok(()) } else { Err(()) }
252+
}
253+
pub fn wait(&self) -> Result<(), ()> {
254+
let result = unsafe { c::WaitForSingleObject(self.handle, c::INFINITE) };
255+
if result != c::WAIT_FAILED { Ok(()) } else { Err(()) }
256+
}
257+
}
258+
impl Drop for WaitableTimer {
259+
fn drop(&mut self) {
260+
unsafe { c::CloseHandle(self.handle) };
261+
}
262+
}

src/tools/miri/src/shims/windows/foreign_items.rs

+12
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
272272

273273
this.Sleep(timeout)?;
274274
}
275+
"CreateWaitableTimerExW" => {
276+
let [attributes, name, flags, access] =
277+
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
278+
this.read_pointer(attributes)?;
279+
this.read_pointer(name)?;
280+
this.read_scalar(flags)?.to_u32()?;
281+
this.read_scalar(access)?.to_u32()?;
282+
// Unimplemented. Always return failure.
283+
let not_supported = this.eval_windows("c", "ERROR_NOT_SUPPORTED");
284+
this.set_last_error(not_supported)?;
285+
this.write_null(dest)?;
286+
}
275287

276288
// Synchronization primitives
277289
"AcquireSRWLockExclusive" => {

0 commit comments

Comments
 (0)