Skip to content

Commit 5065a35

Browse files
committed
sleep_until: handle 32bit time_t targets
1 parent b21da82 commit 5065a35

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

library/std/src/sys/pal/unix/thread.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,17 @@ impl Thread {
310310
target_os = "vxworks",
311311
))]
312312
pub fn sleep_until(deadline: Instant) {
313-
let mut ts = deadline
314-
.into_inner()
315-
.into_timespec()
316-
.to_timespec()
317-
.expect("Timespec is narrower then libc::timespec thus conversion can't fail");
313+
let Some(mut ts) = deadline.into_inner().into_timespec().to_timespec() else {
314+
// The deadline is further in the future then can be passed to
315+
// clock_nanosleep. We have to use Self::sleep intead. This might
316+
// happen on 32 bit platforms, especially closer to 2038.
317+
let now = Instant::now();
318+
if let Some(delay) = deadline.checked_duration_since(now) {
319+
Self::sleep(delay);
320+
}
321+
return;
322+
};
323+
318324
let ts_ptr = &mut ts as *mut _;
319325

320326
// If we're awoken with a signal and the return value is -1
@@ -347,7 +353,6 @@ impl Thread {
347353
)))]
348354
pub fn sleep_until(deadline: Instant) {
349355
let now = Instant::now();
350-
351356
if let Some(delay) = deadline.checked_duration_since(now) {
352357
Self::sleep(delay);
353358
}

0 commit comments

Comments
 (0)