Skip to content

Use clock BOOTTIME for thread::sleep on linux #113513

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ impl Thread {
};
secs -= ts.tv_sec as u64;
let ts_ptr = &mut ts as *mut _;
if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
// Since linux kernel d6ed449 (v >~ 4.19) the MONOTONIC clock
// used by nanosleep behaves like the BOOTTIME clock.
// Ensures sleep behaves the same on linux regardless of kernel.
#[cfg(target_os = "linux")]
let ret = libc::clock_nanosleep(libc::CLOCK_BOOTTIME, 0, ts_ptr, ts_ptr);
#[cfg(not(target_os = "linux"))]
let ret = libc::nanosleep(ts_ptr, ts_ptr);
if ret == -1 {
assert_eq!(os::errno(), libc::EINTR);
secs += ts.tv_sec as u64;
nsecs = ts.tv_nsec;
Expand Down