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
use instant::*;
use std::io;
use std::sync::Arc;
use std::thread;
use std::time;
use std::sync::atomic::{AtomicBool, Ordering};

/// A service to periodically call `Instant::update()`
pub struct Updater {
    period: time::Duration,
    running: Arc<AtomicBool>,
    th: Option<thread::JoinHandle<()>>,
}

impl Updater {
    /// Spawns a background task to call `Instant::update()` periodically
    pub fn start(mut self) -> Result<Self, io::Error> {
        let period = self.period;
        let running = self.running.clone();
        running.store(true, Ordering::Relaxed);
        let th: thread::JoinHandle<()> =
            try!(thread::Builder::new().name("coarsetime_updater".to_string()).spawn(move || {
                while running.load(Ordering::Relaxed) != false {
                    thread::sleep(period);
                    Instant::update();
                }
            }));
        self.th = Some(th);
        Instant::update();
        Ok(self)
    }

    /// Stops the periodic updates
    pub fn stop(mut self) -> Result<(), io::Error> {
        self.running.store(false, Ordering::Relaxed);
        self.th.take().expect("updater is not running").join().map_err(|_| {
            io::Error::new(io::ErrorKind::Other, "failed to properly stop the updater")
        })
    }

    /// Creates a new `Updater` with the specified update period, in milliseconds.
    pub fn new(period_millis: u64) -> Updater {
        Updater {
            period: time::Duration::from_millis(period_millis),
            running: Arc::new(AtomicBool::new(false)),
            th: None,
        }
    }
}