Skip to content

Commit e565c33

Browse files
committed
Use unix implementation of Instant for macos aarch64
1 parent d55e299 commit e565c33

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Hash for Timespec {
114114
}
115115
}
116116

117-
#[cfg(any(target_os = "macos", target_os = "ios"))]
117+
#[cfg(any(all(target_os = "macos", not(target_arch = "aarch64")), target_os = "ios"))]
118118
mod inner {
119119
use crate::fmt;
120120
use crate::sync::atomic::{AtomicU64, Ordering};
@@ -146,14 +146,12 @@ mod inner {
146146
type mach_timebase_info_t = *mut mach_timebase_info;
147147
type kern_return_t = libc::c_int;
148148

149-
pub type clockid_t = libc::clockid_t;
150-
151149
impl Instant {
152150
pub fn now() -> Instant {
153151
extern "C" {
154-
fn clock_gettime_nsec_np(clock_id: clockid_t) -> u64;
152+
fn mach_absolute_time() -> u64;
155153
}
156-
Instant { t: unsafe { clock_gettime_nsec_np(8) } }
154+
Instant { t: unsafe { mach_absolute_time() } }
157155
}
158156

159157
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -265,7 +263,7 @@ mod inner {
265263
}
266264
}
267265

268-
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
266+
#[cfg(not(any(all(target_os = "macos", not(target_arch = "aarch64")), target_os = "ios")))]
269267
mod inner {
270268
use crate::fmt;
271269
use crate::sys::cvt;
@@ -287,7 +285,11 @@ mod inner {
287285

288286
impl Instant {
289287
pub fn now() -> Instant {
290-
Instant { t: now(libc::CLOCK_MONOTONIC) }
288+
#[cfg(target_os = "macos")]
289+
const clock_id: clock_t = 8;
290+
#[cfg(not(target_os = "macos"))]
291+
const clock_id: clock_t = libc::CLOCK_MONOTONIC;
292+
Instant { t: now(clock_id) }
291293
}
292294

293295
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -345,10 +347,12 @@ mod inner {
345347
}
346348
}
347349

348-
#[cfg(not(any(target_os = "dragonfly", target_os = "espidf")))]
350+
#[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "macos")))]
349351
pub type clock_t = libc::c_int;
350352
#[cfg(any(target_os = "dragonfly", target_os = "espidf"))]
351353
pub type clock_t = libc::c_ulong;
354+
#[cfg(target_os = "macos")]
355+
pub type clock_t = libc::clockid_t;
352356

353357
fn now(clock: clock_t) -> Timespec {
354358
let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } };

library/std/src/time/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fn macos_resolution_regression() {
1818
let t0 = Instant::now();
1919
let t1 = t0 + Duration::from_nanos(50);
2020
let d = t1 - t0;
21-
dbg!(t0, t1, d);
2221
assert_eq!(t0 + d, t1);
2322
}
2423

0 commit comments

Comments
 (0)