moved sleep function to lib.rs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#![feature(step_by)]
|
||||
extern crate argparse;
|
||||
extern crate libc;
|
||||
extern crate sleep;
|
||||
extern crate time;
|
||||
|
||||
use std::io::prelude::*;
|
||||
@@ -9,36 +9,9 @@ use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use argparse::{ArgumentParser, Print, Store};
|
||||
use libc::{c_long, clock_nanosleep, time_t, timespec};
|
||||
use sleep::sleep;
|
||||
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 {
|
||||
Duration::new(duration_ns / 1_000_000_000, (duration_ns % 1_000_000_000) as u32)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user