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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

mod interval;
mod sleep;
mod timeout;
mod waiter;

use core::time::Duration;
use core::u64::MAX;
pub use interval::*;
pub use sleep::*;
pub use timeout::*;
use waiter::Waiter;

const WAITS_NUM: usize = 64;
static WAITS: Waiter<WAITS_NUM> = Waiter::new();

/// Nanosecond since `app start time` or `os start time`
pub fn now() -> u64 {
    tick_clock::now()
}

pub struct Tick {
    tick: tick_clock::Tick,
}
impl Tick {
    /// returns nanosecond
    pub fn tick(&mut self, interval: Duration) -> u64 {
        let now = self.tick.tick(interval);
        if let Some(next) = WAITS.take_next() {
            if now < next {
                WAITS.set_next(next);
                return now;
            }
        } else {
            return now;
        }
        let mut min = MAX;
        WAITS.retain(|entity| {
            if entity.val <= now {
                entity.waker.wake_by_ref();
                false
            } else {
                min = min.min(entity.val);
                true
            }
        });
        WAITS.set_next(min);
        now
    }
}
pub fn take_tick() -> Option<Tick> {
    if let Some(tick) = tick_clock::take_tick() {
        Some(Tick { tick })
    } else {
        None
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn auto_tick(interval: Duration) {
    use std::thread;
    use std::time::Instant;

    let mut ticker = take_tick().unwrap();
    let mut last = Instant::now();
    thread::Builder::new()
        .name("async-tick".into())
        .spawn(move || loop {
            let now = Instant::now();
            ticker.tick(now.saturating_duration_since(last));
            last = now;
            thread::sleep(interval);
        })
        .unwrap();
}