use crate::common::time::Instant;
use portable_atomic::AtomicU64;
use std::sync::atomic::Ordering;
#[derive(Debug)]
pub(crate) struct AtomicInstant {
instant: AtomicU64,
}
impl Default for AtomicInstant {
fn default() -> Self {
Self {
instant: AtomicU64::new(u64::MAX),
}
}
}
impl AtomicInstant {
pub(crate) fn new(instant: Instant) -> Self {
debug_assert!(instant.as_nanos() != u64::MAX);
Self {
instant: AtomicU64::new(instant.as_nanos()),
}
}
pub(crate) fn is_set(&self) -> bool {
self.instant.load(Ordering::Acquire) != u64::MAX
}
pub(crate) fn instant(&self) -> Option<Instant> {
let ts = self.instant.load(Ordering::Acquire);
if ts == u64::MAX {
None
} else {
Some(Instant::from_nanos(ts))
}
}
pub(crate) fn set_instant(&self, instant: Instant) {
debug_assert!(instant.as_nanos() != u64::MAX);
self.instant.store(instant.as_nanos(), Ordering::Release);
}
}