Skip to content

Commit

Permalink
delay: Abstract architecture-dependent functions
Browse files Browse the repository at this point in the history
This commit introduces `rdtsc()` and `pause()` to abstract
architecture-specific instruction calls.

Signed-off-by: Akira Moroo <[email protected]>
  • Loading branch information
retrage committed Oct 31, 2022
1 parent c500e5e commit bc6bb75
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ const NSECS_PER_SEC: u64 = 1000000000;
const CPU_KHZ_DEFAULT: u64 = 200;
const PAUSE_THRESHOLD_TICKS: u64 = 150;

#[cfg(target_arch = "x86_64")]
#[inline]
unsafe fn rdtsc() -> u64 {
_rdtsc()
}

#[cfg(target_arch = "x86_64")]
#[inline]
unsafe fn pause() {
asm!("pause");
}

pub fn ndelay(ns: u64) {
let delta = ns * CPU_KHZ_DEFAULT / NSECS_PER_SEC;
let mut pause_delta = 0;
unsafe {
let start = _rdtsc();
let start = rdtsc();
if delta > PAUSE_THRESHOLD_TICKS {
pause_delta = delta - PAUSE_THRESHOLD_TICKS;
}
while _rdtsc() - start < pause_delta {
asm!("pause");
while rdtsc() - start < pause_delta {
pause();
}
while _rdtsc() - start < delta {}
while rdtsc() - start < delta {}
}
}

Expand Down

0 comments on commit bc6bb75

Please sign in to comment.