pub use std::time::{Instant, SystemTime, UNIX_EPOCH};
#[inline]
pub fn system_time_now() -> SystemTime {
#[cfg(any(test, feature = "test-failpoints"))]
if let Some(now) = *TEST_WALL_CLOCK
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner())
{
return now;
}
SystemTime::now()
}
#[cfg(any(test, feature = "test-failpoints"))]
static TEST_WALL_CLOCK: std::sync::RwLock<Option<SystemTime>> = std::sync::RwLock::new(None);
#[cfg(any(test, feature = "test-failpoints"))]
static TEST_WALL_CLOCK_GATE: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[cfg(any(test, feature = "test-failpoints"))]
pub struct TestWallClockGuard {
_gate: std::sync::MutexGuard<'static, ()>,
}
#[cfg(any(test, feature = "test-failpoints"))]
impl TestWallClockGuard {
pub fn install(now: SystemTime) -> Self {
let gate = TEST_WALL_CLOCK_GATE
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
*TEST_WALL_CLOCK
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(now);
Self { _gate: gate }
}
pub fn set(&self, now: SystemTime) {
*TEST_WALL_CLOCK
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(now);
}
}
#[cfg(any(test, feature = "test-failpoints"))]
impl Drop for TestWallClockGuard {
fn drop(&mut self) {
*TEST_WALL_CLOCK
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner()) = None;
}
}