-
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?
Conversation
r? @cuviper |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This can be done without touching all pal's, ill be doing that tomorrow, now its bed time 😴 edit: I was mistaken, tidy does not allow #[cfg(...)] in |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Except for unix they are all copies of the existing non platform specific implementation. This is the only way since tidy does not allow `#[cfg(target_os = ` in src/thread/mod.rs. Once this is merged more specializations will follow.
Co-authored-by: Josh Stone <cuviper@gmail.com>
Co-authored-by: Jonas Böttiger <jonasboettiger@icloud.com> - use the internal platform specific Instant::CLOCK_ID - skip allow(unused) on on platform that uses it such that it can not become dead code - sleep_until: remove leftover links
- In contradiction to nanosleep clock_nanosleep returns the error directly and does not require a call to `os::errno()`. - The last argument to clock_nanosleep can be NULL removing the need for mutating the timespec. - Missed an `allow(unused)` that could be made conditional.
if res == 0 { | ||
break; | ||
} else { | ||
assert_eq!(res, libc::EINTR); |
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.
Would it make sense to add a comment or message to the assert explaining why we should only get res == 0
or res == EINTR
? I thought about adding one however sleep does not do so, would it be to verbose?
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.
Sure, a succinct comment would be good.
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. |
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
// Try to use 64-bit time in preparation for Y2038. | |
#[cfg(all( | |
target_os = "linux", | |
target_env = "gnu", | |
target_pointer_width = "32", | |
not(target_arch = "riscv32") | |
))] | |
{ | |
use crate::sys::weak::weak; | |
// __clock_gettime64 was added to 32-bit arches in glibc 2.34, | |
// and it handles both vDSO calls and ENOSYS fallbacks itself. | |
weak!( | |
fn __clock_gettime64( | |
clockid: libc::clockid_t, | |
tp: *mut __timespec64, | |
) -> libc::c_int; | |
); | |
if let Some(clock_gettime64) = __clock_gettime64.get() { | |
let mut t = MaybeUninit::uninit(); | |
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap(); | |
let t = unsafe { t.assume_init() }; | |
return Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap(); | |
} | |
} |
related tracking issue: #113752
Supersedes #118480 for the reasons see: #113752 (comment)
Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).