Skip to content

Commit 947899e

Browse files
committed
(lib/sleep_until) adds test & update docs & fix mac support
1 parent 70e0d21 commit 947899e

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

library/std/src/sys/pal/unix/thread.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,6 @@ impl Thread {
357357

358358
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))]
359359
pub fn sleep_until(deadline: crate::time::Instant) {
360-
// does not count during sleep/suspend same as clock monotonic
361-
// does instant use mach_absolute_time?
362-
// https://developer.apple.com/library/archive/technotes/tn2169/_index.html
363-
364360
use super::time::Timespec;
365361
use core::mem::MaybeUninit;
366362

library/std/src/sys/pal/unix/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(in crate::sys) const TIMESPEC_MAX_CAPPED: libc::timespec = libc::timespec {
1919
#[repr(transparent)]
2020
#[rustc_layout_scalar_valid_range_start(0)]
2121
#[rustc_layout_scalar_valid_range_end(999_999_999)]
22-
pub(in crate::sys::unix) struct Nanoseconds(pub(in crate::sys::unix) u32);
22+
pub(crate) struct Nanoseconds(pub(crate) u32);
2323

2424
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2525
pub struct SystemTime {
@@ -28,8 +28,8 @@ pub struct SystemTime {
2828

2929
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3030
pub(crate) struct Timespec {
31-
tv_sec: i64,
32-
tv_nsec: Nanoseconds,
31+
pub(crate) tv_sec: i64,
32+
pub(crate) tv_nsec: Nanoseconds,
3333
}
3434

3535
impl SystemTime {
@@ -288,7 +288,7 @@ impl Instant {
288288
Some(Instant { t: self.t.checked_sub_duration(other)? })
289289
}
290290

291-
pub(in crate::sys::unix) fn into_timespec(self) -> Timespec {
291+
pub(crate) fn into_timespec(self) -> Timespec {
292292
self.t
293293
}
294294
}

library/std/src/thread/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,34 @@ pub fn sleep(dur: Duration) {
878878
///
879879
/// # Platform-specific behavior
880880
///
881-
/// This function uses [`sleep`] internally, see its platform-specific behavior.
881+
/// In most cases this function will call an OS specific function. Where that
882+
/// is not supported [`sleep`] is used. Those platforms are referred to as other
883+
/// in the table below.
882884
///
885+
/// # Underlying System calls
886+
///
887+
/// The following system calls are [currently] being used:
888+
///
889+
///
890+
/// | Platform | System call |
891+
/// |-----------|----------------------------------------------------------------------|
892+
/// | Linux | [clock_nanosleep] (Monotonic clock) |
893+
/// | BSD except OpenBSD | [clock_nanosleep] (Monotonic Clock)] |
894+
/// | Android | [clock_nanosleep] (Monotonic Clock)] |
895+
/// | Solaris | [clock_nanosleep] (Monotonic Clock)] |
896+
/// | Illumos | [clock_nanosleep] (Monotonic Clock)] |
897+
/// | Darwin | [mach_wait_until] |
898+
/// | WASI | [subscription_clock] |
899+
/// | Windows | [SetWaitableTimer] |
900+
/// | Other | `sleep_until` uses [`sleep`] and does not issue a syscall itself |
901+
///
902+
/// [currently]: crate::io#platform-specific-behavior
903+
/// [clock_nanosleep]: https://linux.die.net/man/3/clock_nanosleep
904+
/// [subscription_clock]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription_clock-record
905+
/// [mach_wait_until]: https://developer.apple.com/library/archive/technotes/tn2169/_index.html
906+
/// [SetWaitableTimer]: https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer
907+
///
908+
/// **Disclaimer:** These system calls might change over time.
883909
///
884910
/// # Examples
885911
///
@@ -904,9 +930,9 @@ pub fn sleep(dur: Duration) {
904930
/// }
905931
/// ```
906932
///
907-
/// A slow api we must not call too fast and which takes a few
933+
/// A slow API we must not call too fast and which takes a few
908934
/// tries before succeeding. By using `sleep_until` the time the
909-
/// api call takes does not influence when we retry or when we give up
935+
/// API call takes does not influence when we retry or when we give up
910936
///
911937
/// ```no_run
912938
/// #![feature(thread_sleep_until)]

library/std/tests/thread.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::cell::{Cell, RefCell};
22
use std::sync::{Arc, Mutex};
33
use std::thread;
44
use std::time::Duration;
5+
#[feature(thread_sleep_until)]
6+
use std::time::Instant;
57

68
#[test]
79
#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads
@@ -78,3 +80,14 @@ fn available_parallelism() {
7880
// check that std::thread::available_parallelism() returns a valid value
7981
assert!(thread::available_parallelism().is_ok());
8082
}
83+
84+
#[test]
85+
fn sleep_until() {
86+
let now = Instant::now();
87+
let period = Duration::from_millis(100);
88+
let deadline = now + period;
89+
thread::sleep_until(deadline);
90+
91+
let elapsed = now.elapsed();
92+
assert!(elapsed >= period);
93+
}

0 commit comments

Comments
 (0)