Skip to content

Specialize sleep_until implementation for unix (except mac) #141829

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/hermit/thread.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use super::hermit_abi;
use crate::ffi::CStr;
use crate::mem::ManuallyDrop;
use crate::num::NonZero;
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{io, ptr};

pub type Tid = hermit_abi::Tid;
@@ -86,6 +86,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
unsafe {
let _ = hermit_abi::join(self.tid);
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/itron/thread.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use crate::mem::ManuallyDrop;
use crate::num::NonZero;
use crate::ptr::NonNull;
use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{hint, io};

pub struct Thread {
@@ -205,6 +205,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
// Safety: `ThreadInner` is alive at this point
let inner = unsafe { self.p_inner.as_ref() };
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/sgx/thread.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZero;
use crate::time::Duration;
use crate::time::{Duration, Instant};

pub struct Thread(task_queue::JoinHandle);

@@ -132,6 +132,14 @@ impl Thread {
usercalls::wait_timeout(0, dur, || true);
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
self.0.wait();
}
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/teeos/thread.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use crate::ffi::CStr;
use crate::mem::{self, ManuallyDrop};
use crate::num::NonZero;
use crate::sys::os;
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{cmp, io, ptr};

pub const DEFAULT_MIN_STACK_SIZE: usize = 8 * 1024;
@@ -109,6 +109,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

/// must join, because no pthread_detach supported
pub fn join(self) {
let id = self.into_id();
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/uefi/thread.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use crate::ffi::CStr;
use crate::io;
use crate::num::NonZero;
use crate::ptr::NonNull;
use crate::time::Duration;
use crate::time::{Duration, Instant};

pub struct Thread(!);

@@ -39,6 +39,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
self.0
}
72 changes: 71 additions & 1 deletion library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use crate::sys::weak::dlsym;
#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
use crate::sys::weak::weak;
use crate::sys::{os, stack_overflow};
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{cmp, io, ptr};
#[cfg(not(any(
target_os = "l4re",
@@ -296,6 +296,76 @@ impl Thread {
}
}

// Any unix that has clock_nanosleep
// If this list changes update the MIRI chock_nanosleep shim
#[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",
target_os = "vxworks",
))]
pub fn sleep_until(deadline: Instant) {
let Some(ts) = deadline.into_inner().into_timespec().to_timespec() else {
// The deadline is further in the future then can be passed to
// clock_nanosleep. We have to use Self::sleep instead. This might
// happen on 32 bit platforms, especially closer to 2038.
let now = Instant::now();
if let Some(delay) = deadline.checked_duration_since(now) {
Self::sleep(delay);
}
return;
};

unsafe {
// When we get interrupted (res = EINTR) call clock_nanosleep again
loop {
let res = libc::clock_nanosleep(
super::time::Instant::CLOCK_ID,
libc::TIMER_ABSTIME,
&ts,
core::ptr::null_mut(), // not required with TIMER_ABSTIME
);

if res == 0 {
break;
} else {
assert_eq!(
res,
libc::EINTR,
"timespec is in range,
clockid is valid and kernel should support it"
);
}
}
}
}

// Any unix that does not have clock_nanosleep
#[cfg(not(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",
target_os = "vxworks",
)))]
pub fn sleep_until(deadline: Instant) {
let now = Instant::now();
if let Some(delay) = deadline.checked_duration_since(now) {
Self::sleep(delay);
}
}

pub fn join(self) {
let id = self.into_id();
let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
18 changes: 13 additions & 5 deletions library/std/src/sys/pal/unix/time.rs
Original file line number Diff line number Diff line change
@@ -261,6 +261,10 @@ pub struct Instant {
}

impl Instant {
#[cfg(target_vendor = "apple")]
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
#[cfg(not(target_vendor = "apple"))]
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_MONOTONIC;
pub fn now() -> Instant {
// https://www.manpagez.com/man/3/clock_gettime/
//
@@ -273,11 +277,7 @@ impl Instant {
//
// Instant on macos was historically implemented using mach_absolute_time;
// we preserve this value domain out of an abundance of caution.
#[cfg(target_vendor = "apple")]
const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
#[cfg(not(target_vendor = "apple"))]
const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
Instant { t: Timespec::now(clock_id) }
Instant { t: Timespec::now(Self::CLOCK_ID) }
}

pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -291,6 +291,14 @@ impl Instant {
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant { t: self.t.checked_sub_duration(other)? })
}

#[cfg_attr(
not(target_os = "linux"),
allow(unused, reason = "needed by the `sleep_until` on some unix platforms")
)]
pub(crate) fn into_timespec(self) -> Timespec {
self.t
}
}

impl fmt::Debug for Instant {
6 changes: 5 additions & 1 deletion library/std/src/sys/pal/unsupported/thread.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZero;
use crate::time::Duration;
use crate::time::{Duration, Instant};

pub struct Thread(!);

@@ -26,6 +26,10 @@ impl Thread {
panic!("can't sleep");
}

pub fn sleep_until(_deadline: Instant) {
panic!("can't sleep");
}

pub fn join(self) {
self.0
}
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/wasi/thread.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

use crate::ffi::CStr;
use crate::num::NonZero;
use crate::time::Duration;
use crate::time::{Duration, Instant};
use crate::{io, mem};

cfg_if::cfg_if! {
@@ -171,6 +171,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
cfg_if::cfg_if! {
if #[cfg(target_feature = "atomics")] {
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/wasm/atomics/thread.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use crate::ffi::CStr;
use crate::io;
use crate::num::NonZero;
use crate::sys::unsupported;
use crate::time::Duration;
use crate::time::{Duration, Instant};

pub struct Thread(!);

@@ -41,6 +41,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {}
}

10 changes: 9 additions & 1 deletion library/std/src/sys/pal/windows/thread.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,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;
@@ -106,6 +106,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn handle(&self) -> &Handle {
&self.handle
}
10 changes: 9 additions & 1 deletion library/std/src/sys/pal/xous/thread.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use crate::os::xous::ffi::{
map_memory, update_memory_flags,
};
use crate::os::xous::services::{TicktimerScalar, ticktimer_server};
use crate::time::Duration;
use crate::time::{Duration, Instant};

pub struct Thread {
tid: ThreadId,
@@ -128,6 +128,14 @@ impl Thread {
}
}

pub fn sleep_until(deadline: Instant) {
let now = Instant::now();

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

pub fn join(self) {
join_thread(self.tid).unwrap();
}
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.