diff --git a/V2/sleep/Cargo.lock b/V2/sleep/Cargo.lock index 8acea86..d352971 100644 --- a/V2/sleep/Cargo.lock +++ b/V2/sleep/Cargo.lock @@ -3,7 +3,7 @@ name = "sleep" version = "0.1.0" dependencies = [ "argparse 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "shuteye 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -31,11 +31,6 @@ name = "redox_syscall" version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "shuteye" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "time" version = "0.1.36" @@ -62,7 +57,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135" "checksum redox_syscall 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "29dbdfd4b9df8ab31dec47c6087b7b13cbf4a776f335e4de8efba8288dda075b" -"checksum shuteye 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2fae018891baadd06ff0281ff137ccd1641044d9bb9498b1138ffecf28f30172" "checksum time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "211b63c112206356ef1ff9b19355f43740fc3f85960c598a93d3a3d3ba7beade" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/V2/sleep/Cargo.toml b/V2/sleep/Cargo.toml index 04986f4..48785e9 100644 --- a/V2/sleep/Cargo.toml +++ b/V2/sleep/Cargo.toml @@ -5,5 +5,5 @@ authors = ["Simon Wörner "] [dependencies] argparse = ">=0.2.1" -shuteye = ">=0.2.0" +libc = ">=0.2" time = ">=0.1.36" diff --git a/V2/sleep/src/main.rs b/V2/sleep/src/main.rs index 040f04b..de3fc28 100644 --- a/V2/sleep/src/main.rs +++ b/V2/sleep/src/main.rs @@ -1,6 +1,6 @@ #![feature(step_by)] extern crate argparse; -extern crate shuteye; +extern crate libc; extern crate time; use std::io::prelude::*; @@ -9,9 +9,36 @@ use std::path::Path; use std::time::Duration; use argparse::{ArgumentParser, Print, Store}; -use shuteye::sleep; +use libc::{c_long, clock_nanosleep, time_t, timespec}; use time::precise_time_ns; +const CLOCK_MONOTONIC: i32 = 1; + +pub fn sleep(duration: Duration) -> Option { + 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) }