-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
base: master
Are you sure you want to change the base?
Changes from all commits
8763ffa
b21da82
5065a35
cfe7c6b
9c52693
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,70 @@ impl Thread { | |
} | ||
} | ||
|
||
// Any unix that has clock_nanosleep | ||
#[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 intead. 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to add a comment or message to the assert explaining why we should only get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, a succinct comment would be good. |
||
} | ||
} | ||
} | ||
} | ||
|
||
// 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()) }; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't necessarily need to deal with this now, but 32-bit glibc also has
__clock_nanosleep_time64
for future-proofing, which could use like this__clock_gettime64
code:rust/library/std/src/sys/pal/unix/time.rs
Lines 103 to 128 in 6c8138d