Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize sleep_until implementation #118480

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(lib/sleep_until) use target_vendor apple + fix windows removeal + add
more unix platforms

target_vendor's done by:
Co-authored-by: Jonas Böttiger <jonasboettiger@icloud.com>
  • Loading branch information
dvdsk committed Oct 26, 2024
commit d1ae96a433e228dbd25eac44d84f41c091d35450
45 changes: 12 additions & 33 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ impl Thread {
target_os = "android",
target_os = "solaris",
target_os = "illumos",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
target_os = "dragonfly",
target_os = "hurd",
target_os = "fuchsia",
target_vendor = "apple"
)))]
pub fn sleep_until(deadline: Instant) {
let now = Instant::now();
Expand All @@ -323,14 +323,17 @@ impl Thread {
}
}

// Note depends on clock_nanosleep (not supported on macos/ios/watchos/tvos)
// Note depends on clock_nanosleep (not supported on os's by apple)
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "linux",
target_os = "android",
target_os = "solaris",
target_os = "illumos",
target_os = "dragonfly",
target_os = "hurd",
target_os = "fuchsia",
))]
pub fn sleep_until(deadline: crate::time::Instant) {
let mut ts = deadline
Expand All @@ -355,13 +358,7 @@ impl Thread {
}
}

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
#[cfg(target_vendor = "apple")]
pub fn sleep_until(deadline: crate::time::Instant) {
use core::mem::MaybeUninit;

Expand Down Expand Up @@ -398,35 +395,17 @@ impl Thread {
}
}

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
#[cfg(target_vendor = "apple")]
const KERN_SUCCESS: libc::c_int = 0;

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
#[cfg(target_vendor = "apple")]
#[repr(C)]
struct mach_timebase_info_type {
numer: u32,
denom: u32,
}

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
#[cfg(target_vendor = "apple")]
extern "C" {
fn mach_wait_until(deadline: u64) -> libc::c_int;
fn mach_timebase_info(info: *mut mach_timebase_info_type) -> libc::c_int;
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/pal/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::os::windows::io::{AsRawHandle, HandleOrNull};
use crate::sys::handle::Handle;
use crate::sys::{c, stack_overflow};
use crate::sys_common::FromInner;
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{io, ptr};

pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Thread {
let now = Instant::now();

if let Some(delay) = deadline.checked_duration_since(now) {
sleep(delay);
Self::sleep(delay);
}
}

Expand Down
3 changes: 3 additions & 0 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ pub fn sleep(dur: Duration) {
/// | Android | [clock_nanosleep] (Monotonic Clock)] |
/// | Solaris | [clock_nanosleep] (Monotonic Clock)] |
/// | Illumos | [clock_nanosleep] (Monotonic Clock)] |
/// | Dragonfly | [clock_nanosleep] (Monotonic Clock)] |
/// | Hurd | [clock_nanosleep] (Monotonic Clock)] |
/// | Fuchsia | [clock_nanosleep] (Monotonic Clock)] |
/// | Darwin | [mach_wait_until] |
/// | WASI | [subscription_clock] |
/// | Other | `sleep_until` uses [`sleep`] and does not issue a syscall itself |
Expand Down