use CLOCK_MONOTONIC from libc, added os x support

This commit is contained in:
Simon Wörner
2017-04-03 19:46:55 +02:00
parent bb066d5ba8
commit 20f4698db7
2 changed files with 28 additions and 13 deletions

View File

@@ -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<Duration, libc::c_int> {
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))