clock_bound_shm/
common.rs1use crate::{syserror, ShmError};
2use nix::sys::time::TimeSpec;
3use nix::time::{clock_gettime, ClockId};
4
5pub const CLOCK_REALTIME: ClockId = ClockId::CLOCK_REALTIME;
6
7#[cfg(target_os = "macos")]
9pub const CLOCK_MONOTONIC: ClockId = ClockId::CLOCK_MONOTONIC;
10#[cfg(not(target_os = "macos"))]
11pub const CLOCK_MONOTONIC: ClockId = ClockId::CLOCK_MONOTONIC_COARSE;
12
13pub fn clock_gettime_safe(clock_id: ClockId) -> Result<TimeSpec, ShmError> {
20 match clock_gettime(clock_id) {
21 Ok(ts) => Ok(ts),
22 _ => syserror!("clock_gettime"),
23 }
24}
25
26#[cfg(test)]
27mod t_common {
28 use super::*;
29 use std::{thread, time};
30
31 #[test]
33 fn clock_gettime_safe_realtime() {
34 let one = clock_gettime_safe(CLOCK_REALTIME).expect("Failed on clock_gettime");
35 thread::sleep(time::Duration::from_millis(10));
37 let two = clock_gettime_safe(CLOCK_REALTIME).expect("Failed on clock_gettime");
38
39 assert!(two > one);
40 }
41
42 #[test]
44 fn clock_gettime_safe_monotonic() {
45 let one = clock_gettime_safe(CLOCK_MONOTONIC).expect("Failed on clock_gettime");
46 thread::sleep(time::Duration::from_millis(11));
50 let two = clock_gettime_safe(CLOCK_MONOTONIC).expect("Failed on clock_gettime");
51
52 assert!(two > one);
53 }
54}