1use std::time::{Instant, SystemTime};
7
8#[must_use]
10#[inline]
11#[expect(
12 clippy::disallowed_methods,
13 reason = "the probe clock is the real clock unless a harness installs its virtual one"
14)]
15pub fn now() -> Instant {
16 #[cfg(feature = "control")]
17 if let Some(hooks) = hooks::get() {
18 return (hooks.now)();
19 }
20 Instant::now()
21}
22
23#[must_use]
26#[inline]
27#[expect(
28 clippy::disallowed_methods,
29 reason = "the probe clock is the real clock unless a harness installs its virtual one"
30)]
31pub fn system_now() -> SystemTime {
32 #[cfg(feature = "control")]
33 if let Some(hooks) = hooks::get() {
34 return (hooks.system_now)();
35 }
36 SystemTime::now()
37}
38
39#[inline]
43pub fn real_timer(component: &'static str) {
44 #[cfg(feature = "control")]
45 if let Some(controller) = crate::control::current() {
46 controller.real_timer(component);
47 }
48 #[cfg(not(feature = "control"))]
49 let _ = component;
50}
51
52#[cfg(feature = "control")]
53pub use hooks::install;
54
55#[cfg(feature = "control")]
56mod hooks {
57 use std::sync::{PoisonError, RwLock};
58 use std::time::{Instant, SystemTime};
59
60 #[derive(Clone, Copy)]
61 pub(super) struct Hooks {
62 pub(super) now: fn() -> Instant,
63 pub(super) system_now: fn() -> SystemTime,
64 }
65
66 static HOOKS: RwLock<Option<Hooks>> = RwLock::new(None);
67
68 #[doc(hidden)]
72 pub fn install(now: fn() -> Instant, system_now: fn() -> SystemTime) {
73 *HOOKS.write().unwrap_or_else(PoisonError::into_inner) = Some(Hooks { now, system_now });
74 }
75
76 pub(super) fn get() -> Option<Hooks> {
77 *HOOKS.read().unwrap_or_else(PoisonError::into_inner)
78 }
79}