Skip to content

Commit af269ab

Browse files
committed
std: round up the deadline in sleep_until
1 parent ba65b43 commit af269ab

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,11 @@ impl Instant {
313313
}
314314

315315
/// Returns `self` converted into units of `mach_absolute_time`, or `None`
316-
/// if `self` is before the system boot time.
316+
/// if `self` is before the system boot time. If the conversion cannot be
317+
/// performed precisely, this ceils the result up to the nearest
318+
/// representable value.
317319
#[cfg(target_vendor = "apple")]
318-
pub fn into_mach_absolute_time(self) -> Option<u128> {
320+
pub fn into_mach_absolute_time_ceil(self) -> Option<u128> {
319321
#[repr(C)]
320322
struct mach_timebase_info {
321323
numer: u32,
@@ -338,7 +340,7 @@ impl Instant {
338340
// This multiplication cannot overflow since multiplying a 94-bit
339341
// number by a 32-bit number yields a number that needs at most
340342
// 126 bits.
341-
Some(nanos * u128::from(timebase.denom) / u128::from(timebase.numer))
343+
Some((nanos * u128::from(timebase.denom)).div_ceil(u128::from(timebase.numer)))
342344
}
343345
}
344346

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ pub fn sleep_until(deadline: crate::time::Instant) {
649649
safe fn mach_wait_until(deadline: u64) -> libc::kern_return_t;
650650
}
651651

652-
let Some(deadline) = deadline.into_inner().into_mach_absolute_time() else {
652+
// Make sure to round up to ensure that we definitely sleep until after
653+
// the deadline has elapsed.
654+
let Some(deadline) = deadline.into_inner().into_mach_absolute_time_ceil() else {
653655
// Since the deadline is before the system boot time, it has already
654656
// passed, so we can return immediately.
655657
return;

0 commit comments

Comments
 (0)