tick_clock/
lib.rs

1#![no_std]
2
3use core::sync::atomic::{AtomicBool, Ordering::SeqCst};
4use core::time::Duration;
5
6static USED: AtomicBool = AtomicBool::new(false);
7static mut TIME: u64 = 0;
8
9/// Nanosecond since `app start time` or `os start time`
10pub fn now() -> u64 {
11    unsafe { TIME }
12}
13clock_source::register_clock_source!(now);
14
15pub struct Tick {}
16impl Tick {
17    /// returns nanosecond
18    pub fn tick(&mut self, interval: Duration) -> u64 {
19        unsafe {
20            TIME += interval.as_nanos() as u64;
21            TIME
22        }
23    }
24}
25pub fn take_tick() -> Option<Tick> {
26    if USED.compare_exchange(false, true, SeqCst, SeqCst).is_ok() {
27        Some(Tick {})
28    } else {
29        None
30    }
31}