#[cfg(target_os = "macos")]
type ClockId = u32;
#[cfg(target_os = "linux")]
type ClockId = i32;
#[cfg(target_os = "macos")]
const MONOTONIC_CLOCK: ClockId = 8;
#[cfg(target_os = "linux")]
const MONOTONIC_CLOCK: ClockId = 1;
#[repr(C)]
struct Timespec {
tv_sec: i64,
tv_nsec: i64,
}
#[link(name = "c")]
unsafe extern "C" {
safe fn clock_gettime(clock_id: ClockId, tp: &mut Timespec) -> i32;
}
pub(crate) fn monotonic_nanos() -> u64 {
let mut time = Timespec {
tv_sec: 0,
tv_nsec: 0,
};
let result = clock_gettime(MONOTONIC_CLOCK, &mut time);
assert_eq!(result, 0, "clock_gettime failed");
time.tv_sec as u64 * 1_000_000_000 + time.tv_nsec as u64
}