Skip to content

Commit

Permalink
Rollup merge of #103594 - maniwani:fix-issue-91417, r=thomcc
Browse files Browse the repository at this point in the history
Fix non-associativity of `Instant` math on `aarch64-apple-darwin` targets

This is a duplicate of #94100 (since the original author is unresponsive), which resolves #91417.

On `aarch64-apple-darwin` targets, the internal resolution of `Instant` is lower than that of `Duration`, so math between them becomes non-associative with small-enough durations.

This PR makes this target use the standard Unix implementation (where `Instant` has 1ns resolution), but with `CLOCK_UPTIME_RAW` so it still returns the same values as `mach_absolute_time`[^1].

(Edit: I need someone to confirm that this still works, I do not have access to an M1 device.)

[^1]: https://www.manpagez.com/man/3/clock_gettime/
  • Loading branch information
matthiaskrgr authored Nov 18, 2022
2 parents 3efbf30 + f4f5159 commit 6419151
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
25 changes: 16 additions & 9 deletions library/std/src/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ impl From<libc::timespec> for Timespec {
}
}

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
#[cfg(any(
all(target_os = "macos", any(not(target_arch = "aarch64"), miri)),
target_os = "ios",
target_os = "watchos"
))]
mod inner {
use crate::sync::atomic::{AtomicU64, Ordering};
use crate::sys::cvt;
Expand Down Expand Up @@ -265,7 +269,11 @@ mod inner {
}
}

#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "watchos")))]
#[cfg(not(any(
all(target_os = "macos", any(not(target_arch = "aarch64"), miri)),
target_os = "ios",
target_os = "watchos"
)))]
mod inner {
use crate::fmt;
use crate::mem::MaybeUninit;
Expand All @@ -281,7 +289,11 @@ mod inner {

impl Instant {
pub fn now() -> Instant {
Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
#[cfg(target_os = "macos")]
const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
#[cfg(not(target_os = "macos"))]
const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
Instant { t: Timespec::now(clock_id) }
}

pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
Expand Down Expand Up @@ -312,13 +324,8 @@ mod inner {
}
}

#[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon")))]
pub type clock_t = libc::c_int;
#[cfg(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon"))]
pub type clock_t = libc::c_ulong;

impl Timespec {
pub fn now(clock: clock_t) -> Timespec {
pub fn now(clock: libc::clockid_t) -> Timespec {
// Try to use 64-bit time in preparation for Y2038.
#[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
{
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/time/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ fn instant_math_is_associative() {
// Changing the order of instant math shouldn't change the results,
// especially when the expression reduces to X + identity.
assert_eq!((now + offset) - now, (now - now) + offset);

// On any platform, `Instant` should have the same resolution as `Duration` (e.g. 1 nanosecond)
// or better. Otherwise, math will be non-associative (see #91417).
let now = Instant::now();
let provided_offset = Duration::from_nanos(1);
let later = now + provided_offset;
let measured_offset = later - now;
assert_eq!(measured_offset, provided_offset);
}

#[test]
Expand Down

0 comments on commit 6419151

Please sign in to comment.