diff --git a/V2/sleep/src/lib.rs b/V2/sleep/src/lib.rs index 8d0c260..143e7a5 100644 --- a/V2/sleep/src/lib.rs +++ b/V2/sleep/src/lib.rs @@ -2,8 +2,6 @@ extern crate libc; use std::time::Duration; -const CLOCK_MONOTONIC: i32 = 1; - #[cfg(target_os = "linux")] fn clock_nanosleep(clk_id: libc::clockid_t, flags: libc::c_int, @@ -14,11 +12,7 @@ fn clock_nanosleep(clk_id: libc::clockid_t, } #[cfg(target_os = "macos")] -fn clock_nanosleep(_clk_id: libc::c_int, - _flags: libc::c_int, - rqtp: *const libc::timespec, - rmtp: *mut libc::timespec) - -> libc::c_int { +fn nanosleep(rqtp: *const libc::timespec, rmtp: *mut libc::timespec) -> libc::c_int { unsafe { libc::nanosleep(rqtp as *const _, rmtp as *mut _) } } @@ -31,7 +25,12 @@ pub fn sleep(duration: Duration) -> Result { tv_sec: 0, tv_nsec: 0, }; - let ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &mut remain); + + #[cfg(target_os = "linux")] + let ret = clock_nanosleep(libc::CLOCK_MONOTONIC, 0, &ts, &mut remain); + + #[cfg(target_os = "macos")] + let ret = nanosleep(&ts, &mut remain); if ret == 0 { Ok(timespec_to_duration(remain)) diff --git a/V2/sleep/src/main.rs b/V2/sleep/src/main.rs index 1e6b4d6..391348a 100644 --- a/V2/sleep/src/main.rs +++ b/V2/sleep/src/main.rs @@ -8,10 +8,16 @@ use std::fs::File; use std::path::Path; use std::time::Duration; -use argparse::{ArgumentParser, Print, Store, StoreTrue}; -use sleep::{set_priority, sleep}; +use argparse::{ArgumentParser, Print, Store}; +use sleep::sleep; use time::precise_time_ns; +#[cfg(target_os = "linux")] +use argparse::StoreTrue; + +#[cfg(target_os = "linux")] +use sleep::set_priority; + fn duration_from_ns(duration_ns: u64) -> Duration { Duration::new(duration_ns / 1_000_000_000, (duration_ns % 1_000_000_000) as u32) @@ -22,11 +28,11 @@ fn duration_to_ns(duration: Duration) -> u64 { fn measure_duration(sleep_duration: Duration) -> Duration { let start = precise_time_ns(); - let remain = sleep(sleep_duration); + let _remain = sleep(sleep_duration); let end = precise_time_ns(); #[cfg(debug_assertions)] - println!("remain = {:?}", remain); + println!("remain = {:?}", _remain); let duration = end - start; let duration = duration_from_ns(duration); @@ -80,10 +86,15 @@ fn main() { let mut max: u64 = 100_000_000; let mut step: u64 = 1_000_000; let mut count: u64 = 100; - let mut realtime: bool = false; let mut output: String = "".to_string(); let mut file: Option = None; + #[cfg(target_os = "linux")] + let mut realtime: bool = false; + + #[cfg(target_os = "macos")] + let realtime: bool = false; + { let mut ap = ArgumentParser::new(); ap.set_description(env!("CARGO_PKG_DESCRIPTION")); @@ -91,6 +102,7 @@ fn main() { ap.refer(&mut max).add_option(&["--max"], Store, "Sleep period end"); ap.refer(&mut step).add_option(&["--step"], Store, "Sleep period step size"); ap.refer(&mut count).add_option(&["--loop"], Store, "Count of measurements per period"); + #[cfg(target_os = "linux")] ap.refer(&mut realtime).add_option(&["--rt"], StoreTrue, "Output file"); ap.refer(&mut output).add_option(&["-o", "--out"], Store, "Output file"); ap.add_option(&["-V", "--version"], @@ -100,8 +112,12 @@ fn main() { } if realtime { + #[cfg(target_os = "linux")] let ret = set_priority(99); + #[cfg(target_os = "macos")] + let ret = 0; + if ret != 0 { panic!("Set realtime priority failed."); }