moved sleep function to lib.rs
This commit is contained in:
42
V2/sleep/src/lib.rs
Normal file
42
V2/sleep/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
use libc::{c_int, c_long, time_t, timespec};
|
||||||
|
|
||||||
|
const CLOCK_MONOTONIC: i32 = 1;
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
fn clock_nanosleep(clk_id: libc::clockid_t, flags: libc::c_int, rqtp: *const libc::timespec, rmtp: *mut libc::timespec) -> libc::c_int {
|
||||||
|
unsafe { libc::clock_nanosleep(clk_id, flags, rqtp as *const _, rmtp as *mut _) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
unsafe { libc::nanosleep(rqtp as *const _, rmtp as *mut _) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sleep(duration: Duration) -> Result<Duration, c_int> {
|
||||||
|
let ts = duration_to_timespec(duration);
|
||||||
|
let mut remain = timespec {
|
||||||
|
tv_sec: 0,
|
||||||
|
tv_nsec: 0,
|
||||||
|
};
|
||||||
|
let ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &mut remain);
|
||||||
|
|
||||||
|
if ret == 0 {
|
||||||
|
Ok(timespec_to_duration(remain))
|
||||||
|
} else {
|
||||||
|
Err(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn duration_to_timespec(duration: Duration) -> timespec {
|
||||||
|
timespec {
|
||||||
|
tv_sec: duration.as_secs() as time_t,
|
||||||
|
tv_nsec: duration.subsec_nanos() as c_long,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timespec_to_duration(timespec: timespec) -> Duration {
|
||||||
|
Duration::new(timespec.tv_sec as u64, timespec.tv_nsec as u32)
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#![feature(step_by)]
|
#![feature(step_by)]
|
||||||
extern crate argparse;
|
extern crate argparse;
|
||||||
extern crate libc;
|
extern crate sleep;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
@@ -9,36 +9,9 @@ use std::path::Path;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use argparse::{ArgumentParser, Print, Store};
|
use argparse::{ArgumentParser, Print, Store};
|
||||||
use libc::{c_long, clock_nanosleep, time_t, timespec};
|
use sleep::sleep;
|
||||||
use time::precise_time_ns;
|
use time::precise_time_ns;
|
||||||
|
|
||||||
const CLOCK_MONOTONIC: i32 = 1;
|
|
||||||
|
|
||||||
pub fn sleep(duration: Duration) -> Option<Duration> {
|
|
||||||
let ts = duration_to_timespec(duration);
|
|
||||||
let mut remain = timespec {
|
|
||||||
tv_sec: 0,
|
|
||||||
tv_nsec: 0,
|
|
||||||
};
|
|
||||||
let ret = unsafe { clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &mut remain) };
|
|
||||||
println!("ret = {}", ret);
|
|
||||||
if remain.tv_nsec == 0 && remain.tv_sec == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some(timespec_to_duration(remain))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn duration_to_timespec(duration: Duration) -> timespec {
|
|
||||||
timespec {
|
|
||||||
tv_sec: duration.as_secs() as time_t,
|
|
||||||
tv_nsec: duration.subsec_nanos() as c_long,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn timespec_to_duration(timespec: timespec) -> Duration {
|
|
||||||
Duration::new(timespec.tv_sec as u64, timespec.tv_nsec as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn duration_from_ns(duration_ns: u64) -> Duration {
|
fn duration_from_ns(duration_ns: u64) -> Duration {
|
||||||
Duration::new(duration_ns / 1_000_000_000, (duration_ns % 1_000_000_000) as u32)
|
Duration::new(duration_ns / 1_000_000_000, (duration_ns % 1_000_000_000) as u32)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user