use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TimerPrecision {
#[default]
Normal,
High,
Exact,
}
impl crate::prop::PropEnum for TimerPrecision {
const NAMES: &'static [&'static str] = &["normal", "high", "exact"];
fn from_index(index: usize) -> Option<Self> {
[Self::Normal, Self::High, Self::Exact].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
pub const SPIN: Duration = Duration::from_micros(1500);
struct Resolution {
hook: Option<fn(bool)>,
requests: usize,
}
static RESOLUTION: Mutex<Resolution> = Mutex::new(Resolution { hook: None, requests: 0 });
pub fn set_resolution_hook(hook: fn(bool)) {
if let Ok(mut r) = RESOLUTION.lock() {
r.hook = Some(hook);
if r.requests > 0 {
hook(true);
}
}
}
pub fn fine_clock(on: bool) {
let Ok(mut r) = RESOLUTION.lock() else { return };
let before = r.requests;
r.requests = if on { before + 1 } else { before.saturating_sub(1) };
if let Some(hook) = r.hook
&& (before == 0) != (r.requests == 0)
{
hook(r.requests > 0);
}
}
pub fn park_until(deadline: Instant, precision: TimerPrecision, stop: &AtomicBool) -> bool {
let margin = if precision == TimerPrecision::Exact { SPIN } else { Duration::ZERO };
loop {
if stop.load(Ordering::Relaxed) {
return false;
}
let now = Instant::now();
if now + margin >= deadline {
break;
}
std::thread::park_timeout(deadline - margin - now);
}
while Instant::now() < deadline {
std::hint::spin_loop();
}
!stop.load(Ordering::Relaxed)
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
static CALLS: AtomicUsize = AtomicUsize::new(0);
fn hook(on: bool) {
CALLS.fetch_add(if on { 1 } else { 100 }, Ordering::SeqCst);
}
#[test]
fn requests_are_counted_and_exact_is_exact() {
set_resolution_hook(hook);
fine_clock(true);
fine_clock(true);
fine_clock(false);
assert_eq!(CALLS.load(Ordering::SeqCst), 1, "só o primeiro pedido liga");
fine_clock(false);
assert_eq!(CALLS.load(Ordering::SeqCst), 101, "o último devolve");
let deadline = Instant::now() + Duration::from_millis(1);
assert!(park_until(deadline, TimerPrecision::Exact, &AtomicBool::new(false)));
let late = Instant::now() - deadline;
assert!(late < Duration::from_millis(1), "atrasou {late:?}");
}
}