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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use super::*;

use channel::select;
use crossbeam_channel as channel;

#[derive(Debug)]
enum TimerMsg {
    Schedule(TimerEntry),
    Cancel(Uuid),
    Stop,
}

#[derive(Clone)]
pub struct TimerRef {
    work_queue: channel::Sender<TimerMsg>,
}

impl Timer for TimerRef {
    fn schedule_once<F>(&mut self, id: Uuid, timeout: Duration, action: F) -> ()
    where
        F: FnOnce(Uuid) + Send + 'static,
    {
        let e = TimerEntry::OneShot {
            id,
            timeout,
            action: Box::new(action),
        };
        self.work_queue
            .send(TimerMsg::Schedule(e))
            .unwrap_or_else(|e| eprintln!("Could not send Schedule msg: {:?}", e));
    }

    fn schedule_periodic<F>(&mut self, id: Uuid, delay: Duration, period: Duration, action: F) -> ()
    where
        F: Fn(Uuid) + Send + 'static,
    {
        let e = TimerEntry::Periodic {
            id,
            delay,
            period,
            action: Box::new(action),
        };
        self.work_queue
            .send(TimerMsg::Schedule(e))
            .unwrap_or_else(|e| eprintln!("Could not send Schedule msg: {:?}", e));
    }
    fn cancel(&mut self, id: Uuid) {
        self.work_queue
            .send(TimerMsg::Cancel(id))
            .unwrap_or_else(|e| eprintln!("Could not send Cancel msg: {:?}", e));
    }
}

pub struct TimerWithThread {
    timer_thread: thread::JoinHandle<()>,
    work_queue: channel::Sender<TimerMsg>,
}

impl TimerWithThread {
    pub fn new() -> io::Result<TimerWithThread> {
        let (s, r) = channel::unbounded();
        let handle = thread::Builder::new()
            .name("timer-thread".to_string())
            .spawn(move || {
                let timer = TimerThread::new(r);
                timer.run();
            })?;
        let twt = TimerWithThread {
            timer_thread: handle,
            work_queue: s,
        };
        Ok(twt)
    }

    pub fn timer_ref(&self) -> TimerRef {
        TimerRef {
            work_queue: self.work_queue.clone(),
        }
    }

    pub fn shutdown(self) -> Result<(), ThreadTimerError> {
        self.work_queue
            .send(TimerMsg::Stop)
            .unwrap_or_else(|e| eprintln!("Could not send Stop msg: {:?}", e));
        match self.timer_thread.join() {
            Ok(_) => Ok(()),
            Err(_) => {
                eprintln!("Timer thread panicked!");
                Err(ThreadTimerError::CouldNotJoinThread)
            }
        }
    }

    pub fn shutdown_async(&self) -> Result<(), ThreadTimerError> {
        self.work_queue
            .send(TimerMsg::Stop)
            .unwrap_or_else(|e| eprintln!("Could not send Stop msg: {:?}", e));
        Ok(())
    }
}

impl fmt::Debug for TimerWithThread {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<TimerWithThread>")
    }
}

#[derive(Debug)]
pub enum ThreadTimerError {
    CouldNotSendStopAsync,
    CouldNotSendStop(TimerWithThread),
    CouldNotJoinThread,
}

pub struct TimerThread {
    timer: QuadWheelWithOverflow,
    work_queue: channel::Receiver<TimerMsg>,
    running: bool,
    start: Instant,
    last_check: u128,
}

impl TimerThread {
    fn new(work_queue: channel::Receiver<TimerMsg>) -> TimerThread {
        TimerThread {
            timer: QuadWheelWithOverflow::new(),
            work_queue,
            running: true,
            start: Instant::now(),
            last_check: 0u128,
        }
    }

    pub fn run(mut self) {
        while self.running {
            let elap = self.elapsed();
            if elap > 0 {
                for _ in 0..elap {
                    self.tick();
                }
            } else {
                match self.work_queue.try_recv() {
                    Ok(msg) => self.handle_msg(msg),
                    Err(channel::TryRecvError::Empty) => {
                        // nothing to tick and nothing in the queue
                        match self.timer.can_skip() {
                            Skip::None => {
                                thread::yield_now();
                                // if last_check.elapsed().as_nanos() > 800000 {
                                //     // if more than 8/10ms left
                                //     thread::yield_now(); // just let someone else run until the next tick
                                // } // else hot wait to reduce inaccuracy
                            }
                            Skip::Empty => {
                                // wait until something is scheduled
                                // don't even need to bother skipping time in the wheel,
                                // since all times in there are relative
                                match self.work_queue.recv() {
                                    Ok(msg) => {
                                        self.reset(); // since we waited for an arbitrary time and taking a new timestamp incures no error
                                        self.handle_msg(msg)
                                    }
                                    Err(channel::RecvError) => {
                                        panic!("Timer work_queue unexpectedly shut down!")
                                    }
                                }
                            }
                            Skip::Millis(ms) if ms > 5 => {
                                let ms = ms - 5; // balance OS scheduler inaccuracy
                                                 // wait until something is scheduled but max skip
                                let timeout = Duration::from_millis(ms as u64);
                                let res = select! {
                                    recv(self.work_queue) -> msg => msg.ok(),
                                    default(timeout) => None,
                                };
                                let elap = self.elapsed();
                                let longms = ms as u128;
                                if elap > longms {
                                    // took longer to get rescheduled than we wanted
                                    self.timer.skip(ms);
                                    for _ in 0..(elap - longms) {
                                        self.tick();
                                    }
                                } else if elap < longms {
                                    // we got woken up early, no need to tick
                                    self.timer.skip(elap as u32)
                                } else {
                                    // elap == ms
                                    // next action should be a tick, but add the new items first
                                    self.timer.skip(ms);
                                }
                                match res {
                                    Some(msg) => self.handle_msg(msg),
                                    None => (), // restart loop
                                }
                            }
                            Skip::Millis(_) => {
                                thread::yield_now();
                                // if last_check.elapsed().as_nanos() > 800000 {
                                //     // if more than 8/10ms left
                                //     thread::yield_now(); // just let someone else run until the next tick
                                // } // else hot wait to reduce inaccuracy
                            }
                        }
                    }
                    Err(channel::TryRecvError::Disconnected) => {
                        panic!("Timer work_queue unexpectedly shut down!")
                    }
                }
            }
        }
    }

    #[inline(always)]
    fn elapsed(&mut self) -> u128 {
        let elap = self.start.elapsed().as_millis();
        let rel_elap = elap - self.last_check;
        self.last_check = elap;
        rel_elap
    }

    #[inline(always)]
    fn reset(&mut self) {
        self.start = Instant::now();
        self.last_check = 0;
    }

    #[inline(always)]
    fn handle_msg(&mut self, msg: TimerMsg) -> () {
        match msg {
            TimerMsg::Stop => self.running = false,
            TimerMsg::Schedule(e) => {
                match self.timer.insert(e) {
                    Ok(_) => (), // ok
                    Err(TimerError::Expired(e)) => {
                        self.execute(e);
                    }
                    Err(f) => panic!("Could not insert timer entry! {:?}", f),
                }
            }
            TimerMsg::Cancel(id) => match self.timer.cancel(id) {
                Ok(_) => (),                     // ok
                Err(TimerError::NotFound) => (), // also ok, might have been triggered already
                Err(f) => panic!("Unexpected error cancelling timer! {:?}", f),
            },
        }
    }

    #[inline(always)]
    fn execute(&mut self, e: TimerEntry) -> () {
        match e.execute() {
            Some(re_e) => match self.timer.insert(re_e) {
                Ok(_) => (), // great
                Err(TimerError::Expired(re_e)) => {
                    // This could happen if someone specifies 0ms period
                    eprintln!("TimerEntry could not be inserted properly: {:?}", re_e);
                }
                Err(f) => panic!("Could not insert timer entry! {:?}", f),
            },
            None => (), // great
        }
    }

    #[inline(always)]
    fn tick(&mut self) -> () {
        let mut res = self.timer.tick();
        for e in res.drain(..) {
            self.execute(e);
        }
    }
}