Skip to content

Commit

Permalink
Minimise spin ops (maybe)
Browse files Browse the repository at this point in the history
sleep_ns: Use Duration::from_nanos
  • Loading branch information
alexheretic committed Dec 12, 2024
1 parent 5ae4cfe commit 304096f
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ impl SpinSleeper {
/// Puts the [current thread to sleep](fn.native_sleep.html) for the duration less the
/// configured native accuracy. Then spins until the specified duration has elapsed.
pub fn sleep(self, duration: Duration) {
let start = Instant::now();
let deadline = Instant::now() + duration;
let accuracy = Duration::new(0, self.native_accuracy_ns);
if duration > accuracy {
native_sleep(duration - accuracy);
}
// spin the rest of the duration
while start.elapsed() < duration {
while Instant::now() < deadline {
match self.spin_strategy {
SpinStrategy::YieldThread => thread::yield_now(),
SpinStrategy::SpinLoopHint => std::hint::spin_loop(),
Expand All @@ -169,9 +169,7 @@ impl SpinSleeper {
/// Puts the [current thread to sleep](fn.native_sleep.html) for the give nanoseconds-duration
/// less the configured native accuracy. Then spins until the specified duration has elapsed.
pub fn sleep_ns(self, nanoseconds: Nanoseconds) {
let subsec_ns = (nanoseconds % 1_000_000_000) as u32;
let seconds = nanoseconds / 1_000_000_000;
self.sleep(Duration::new(seconds, subsec_ns))
self.sleep(Duration::from_nanos(nanoseconds))
}
}

Expand Down

0 comments on commit 304096f

Please sign in to comment.