2016-12-06 21:41:44 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
|
|
|
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// In addition, as a special exception, the copyright holders give
|
|
|
|
// permission to link the code of portions of this program with the
|
|
|
|
// OpenSSL library under certain conditions as described in each
|
|
|
|
// individual source file, and distribute linked combinations including
|
|
|
|
// the two.
|
|
|
|
//
|
|
|
|
// You must obey the GNU General Public License in all respects for all
|
|
|
|
// of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
// exception, you may extend this exception to your version of the
|
|
|
|
// file(s), but you are not obligated to do so. If you do not wish to do
|
|
|
|
// so, delete this exception statement from your version. If you delete
|
|
|
|
// this exception statement from all source files in the program, then
|
|
|
|
// also delete it here.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Clock interface and implementations for testability.
|
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
use libc;
|
2016-12-06 21:41:44 -05:00
|
|
|
#[cfg(test)] use std::sync::Mutex;
|
2016-12-30 00:05:57 -05:00
|
|
|
use std::mem;
|
2016-12-06 21:41:44 -05:00
|
|
|
use std::thread;
|
2016-12-30 00:05:57 -05:00
|
|
|
use time::{Duration, Timespec};
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
/// Abstract interface to the system clocks. This is for testability.
|
|
|
|
pub trait Clocks : Sync {
|
|
|
|
/// Gets the current time from `CLOCK_REALTIME`.
|
|
|
|
fn realtime(&self) -> Timespec;
|
|
|
|
|
|
|
|
/// Gets the current time from `CLOCK_MONOTONIC`.
|
|
|
|
fn monotonic(&self) -> Timespec;
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
/// Causes the current thread to sleep for the specified time.
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration);
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
/// Singleton "real" clocks.
|
|
|
|
pub static REAL: RealClocks = RealClocks {};
|
|
|
|
|
|
|
|
/// Real clocks; see static `REAL` instance.
|
|
|
|
pub struct RealClocks {}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl RealClocks {
|
|
|
|
fn get(&self, clock: libc::clockid_t) -> Timespec {
|
|
|
|
unsafe {
|
|
|
|
let mut ts = mem::uninitialized();
|
|
|
|
assert_eq!(0, libc::clock_gettime(clock, &mut ts));
|
2016-12-30 09:35:10 -05:00
|
|
|
Timespec::new(ts.tv_sec as i64, ts.tv_nsec as i32)
|
2016-12-30 00:05:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl Clocks for RealClocks {
|
|
|
|
fn realtime(&self) -> Timespec { self.get(libc::CLOCK_REALTIME) }
|
|
|
|
fn monotonic(&self) -> Timespec { self.get(libc::CLOCK_MONOTONIC) }
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration) {
|
2016-12-06 21:41:44 -05:00
|
|
|
match how_long.to_std() {
|
|
|
|
Ok(d) => thread::sleep(d),
|
|
|
|
Err(e) => warn!("Invalid duration {:?}: {}", how_long, e),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulated clock for testing.
|
|
|
|
#[cfg(test)]
|
2016-12-30 00:05:57 -05:00
|
|
|
pub struct SimulatedClocks {
|
|
|
|
boot: Timespec,
|
|
|
|
uptime: Mutex<Duration>,
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2016-12-30 00:05:57 -05:00
|
|
|
impl SimulatedClocks {
|
|
|
|
pub fn new(boot: Timespec) -> SimulatedClocks {
|
|
|
|
SimulatedClocks {
|
|
|
|
boot: boot,
|
|
|
|
uptime: Mutex::new(Duration::seconds(0)),
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2016-12-30 00:05:57 -05:00
|
|
|
impl Clocks for SimulatedClocks {
|
|
|
|
fn realtime(&self) -> Timespec { self.boot + *self.uptime.lock().unwrap() }
|
|
|
|
fn monotonic(&self) -> Timespec { Timespec::new(0, 0) + *self.uptime.lock().unwrap() }
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
/// Advances the clock by the specified amount without actually sleeping.
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration) {
|
|
|
|
let mut l = self.uptime.lock().unwrap();
|
2016-12-06 21:41:44 -05:00
|
|
|
*l = *l + how_long;
|
|
|
|
}
|
|
|
|
}
|