diff --git a/src/rand.rs b/src/rand.rs index 6b759ff..a2e7641 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -24,6 +24,8 @@ pub fn timestamp_cycles() -> u64 { #[cfg(not(target_arch = "x86_64"))] { + use std::sync::atomic::{AtomicU64, Ordering}; + static TIMESTAMP: AtomicU64 = AtomicU64::new(0); const MONOTONIC_CLOCK_MULTPIPLIER: u64 = 1_000_000_000; let mut ts = libc::timespec { @@ -31,10 +33,19 @@ pub fn timestamp_cycles() -> u64 { tv_nsec: 0, }; - unsafe { - libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts); + // clock_gettime(libc::CLOCK_MONOTONIC) may return the same value, so retry... + loop { + unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) }; + let curr = (ts.tv_sec as u64) * MONOTONIC_CLOCK_MULTPIPLIER + (ts.tv_nsec as u64); + let prev = TIMESTAMP.load(Ordering::Acquire); + if prev < curr + && TIMESTAMP + .compare_exchange(prev, curr, Ordering::AcqRel, Ordering::Relaxed) + .is_ok() + { + return curr; + } } - (ts.tv_sec as u64) * MONOTONIC_CLOCK_MULTPIPLIER + (ts.tv_nsec as u64) } } diff --git a/src/unix/tempdir.rs b/src/unix/tempdir.rs index d265039..f3a8ba9 100644 --- a/src/unix/tempdir.rs +++ b/src/unix/tempdir.rs @@ -144,6 +144,7 @@ mod tests { let path = t.as_path(); assert!(path.exists()); assert!(path.is_dir()); + #[cfg(not(target_os = "macos"))] assert!(path.starts_with(temp_dir())); } @@ -185,12 +186,14 @@ mod tests { let path = t.as_path(); assert!(path.exists()); assert!(path.is_dir()); + #[cfg(not(target_os = "macos"))] assert!(path.starts_with("/tmp/")); let t = TempDir::new_in(Path::new("/tmp")).unwrap(); let path = t.as_path(); assert!(path.exists()); assert!(path.is_dir()); + #[cfg(not(target_os = "macos"))] assert!(path.starts_with("/tmp")); }