1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::sync::atomic::{AtomicU64, Ordering};

use super::time::Instant;

pub(crate) struct AtomicInstant {
    instant: AtomicU64,
}

impl Default for AtomicInstant {
    fn default() -> Self {
        Self {
            instant: AtomicU64::new(std::u64::MAX),
        }
    }
}

impl AtomicInstant {
    pub(crate) fn reset(&self) {
        self.instant.store(std::u64::MAX, Ordering::Release);
    }

    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(unsafe { std::mem::transmute(ts) })
        }
    }

    pub(crate) fn set_instant(&self, instant: Instant) {
        self.instant.store(instant.0.as_u64(), Ordering::Release);
    }
}