rat_salsa/
timer.rs

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//!
//! Support for timers.
//!

use std::cell::{Cell, RefCell};
use std::time::{Duration, Instant};

/// Holds all the timers.
#[derive(Debug, Default)]
pub(crate) struct Timers {
    tags: Cell<usize>,
    timers: RefCell<Vec<TimerImpl>>,
}

/// Handle for a submitted timer.
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
pub struct TimerHandle(usize);

#[derive(Debug)]
struct TimerImpl {
    tag: usize,
    count: usize,
    repeat: Option<usize>,
    next: Instant,
    timer: Duration,
}

impl Timers {
    /// Returns the next sleep time.
    pub(crate) fn sleep_time(&self) -> Option<Duration> {
        let timers = self.timers.borrow();
        if let Some(timer) = timers.last() {
            let now = Instant::now();
            if now > timer.next {
                Some(Duration::from_nanos(0))
            } else {
                Some(timer.next.duration_since(now))
            }
        } else {
            None
        }
    }

    /// Polls for the next timer event.
    pub(crate) fn poll(&self) -> bool {
        let timers = self.timers.borrow();
        if let Some(timer) = timers.last() {
            Instant::now() >= timer.next
        } else {
            false
        }
    }

    /// Polls for the next timer event.
    /// Removes/recalculates the event and reorders the queue.
    pub(crate) fn read(&self) -> Option<TimerEvent> {
        let mut timers = self.timers.borrow_mut();

        let timer = timers.pop();
        if let Some(mut timer) = timer {
            if Instant::now() >= timer.next {
                let evt = TimerEvent(TimeOut {
                    handle: TimerHandle(timer.tag),
                    counter: timer.count,
                });

                // reschedule
                if let Some(repeat) = timer.repeat {
                    timer.count += 1;
                    if timer.count < repeat {
                        timer.next += timer.timer;
                        Self::add_impl(timers.as_mut(), timer);
                    }
                }

                Some(evt)
            } else {
                self.timers.borrow_mut().push(timer);
                None
            }
        } else {
            None
        }
    }

    fn add_impl(timers: &mut Vec<TimerImpl>, t: TimerImpl) {
        'f: {
            for i in 0..timers.len() {
                if timers[i].next <= t.next {
                    timers.insert(i, t);
                    break 'f;
                }
            }
            timers.push(t);
        }
    }

    /// Add a timer.
    #[must_use]
    pub(crate) fn add(&self, t: TimerDef) -> TimerHandle {
        let tag = self.tags.get() + 1;
        self.tags.set(tag);

        let t = TimerImpl {
            tag,
            count: 0,
            repeat: t.repeat,
            next: if let Some(next) = t.next {
                next
            } else {
                Instant::now() + t.timer
            },
            timer: t.timer,
        };

        let mut timers = self.timers.borrow_mut();
        Self::add_impl(timers.as_mut(), t);

        TimerHandle(tag)
    }

    /// Remove a timer.
    pub(crate) fn remove(&self, tag: TimerHandle) {
        let mut timer = self.timers.borrow_mut();
        for i in 0..timer.len() {
            if timer[i].tag == tag.0 {
                timer.remove(i);
                break;
            }
        }
    }
}

/// Timing event data. Used by [TimerEvent].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeOut {
    pub handle: TimerHandle,
    pub counter: usize,
}

/// Timer event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimerEvent(pub TimeOut);

/// Holds the information to start a timer.
#[derive(Debug, Default)]
pub struct TimerDef {
    /// Optional repeat.
    repeat: Option<usize>,
    /// Duration
    timer: Duration,
    /// Specific time.
    next: Option<Instant>,
}

impl TimerDef {
    pub fn new() -> Self {
        Default::default()
    }

    /// Repeat forever.
    pub fn repeat_forever(mut self) -> Self {
        self.repeat = Some(usize::MAX);
        self
    }

    /// Repeat count.
    pub fn repeat(mut self, repeat: usize) -> Self {
        self.repeat = Some(repeat);
        self
    }

    /// Timer interval.
    pub fn timer(mut self, timer: Duration) -> Self {
        self.timer = timer;
        self
    }

    /// Next time the timer is due. Can set a start delay for a repeating timer,
    /// or as an oneshot event for a given instant.
    pub fn next(mut self, next: Instant) -> Self {
        self.next = Some(next);
        self
    }
}