use std::hint;
use std::thread;
use std::time::{Duration, Instant};
const SPIN_MARGIN: Duration = Duration::from_millis(1);
pub fn precise_sleep(until: Instant) {
let sleep = until
.checked_duration_since(Instant::now())
.and_then(|left| left.checked_sub(SPIN_MARGIN));
if let Some(sleep) = sleep {
thread::sleep(sleep);
}
while Instant::now() < until {
hint::spin_loop();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_never_returns_before_the_deadline() {
for micros in [0, 50, 300, 1000, 1500, 5000] {
let until = Instant::now() + Duration::from_micros(micros);
precise_sleep(until);
assert!(Instant::now() >= until, "{micros} µs");
}
}
#[test]
fn a_deadline_in_the_past_returns_at_once() {
let start = Instant::now();
for _ in 0..1000 {
precise_sleep(start);
}
assert!(
start.elapsed() < Duration::from_secs(1),
"{:?}",
start.elapsed()
);
}
#[test]
fn it_waits_out_a_deadline_that_is_many_sleeps_away() {
let start = Instant::now();
let mut until = start;
for _ in 0..10 {
until += Duration::from_millis(2);
precise_sleep(until);
}
assert!(start.elapsed() >= Duration::from_millis(20));
}
}