Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,28 @@ pub fn timestamp_cycles() -> u64 {

#[cfg(not(target_arch = "x86_64"))]
{
use std::sync::atomic::{AtomicU64, Ordering};
static TIMESTAMP: AtomicU64 = AtomicU64::new(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be static?

const MONOTONIC_CLOCK_MULTPIPLIER: u64 = 1_000_000_000;

let mut ts = libc::timespec {
tv_sec: 0,
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)
}
}

Expand Down