Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude = [
]

[dependencies]
libc = { version = "0.2.78", features = [ "extra_traits" ] }
libc = { version = "0.2.80", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "0.1.10"

Expand Down
46 changes: 45 additions & 1 deletion src/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sys::time::TimeSpec;
use bitflags::bitflags;
use crate::sys::time::{TimeSpec, TimeValLike};
#[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
Expand Down Expand Up @@ -256,3 +257,46 @@ pub fn clock_getcpuclockid(pid: Pid) -> Result<ClockId> {
Err(Error::Sys(Errno::from_i32(ret)))
}
}

bitflags! {
/// Flags that are used for arming the timer.
pub struct ClockNanosleepFlags: libc::c_int {
const TIMER_ABSTIME = libc::TIMER_ABSTIME;
}
}
Comment on lines +261 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the libc_flags macro instead:

Suggested change
bitflags! {
/// Flags that are used for arming the timer.
pub struct ClockNanosleepFlags: libc::c_int {
const TIMER_ABSTIME = libc::TIMER_ABSTIME;
}
}
libc_bitflags! {
/// Flags that are used for arming the timer.
pub struct ClockNanosleepFlags: libc::c_int {
TIMER_ABSTIME;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, adding a doc comment for this flag would be great:)


/// Suspend execution of this thread for the amount of time specified by rqtp
/// and measured against the clock speficied by ClockId. If flags is
/// TIMER_ABSTIME, this function will suspend execution until the time value of
/// clock_id reaches the absolute time specified by rqtp. If a signal is caught
/// by a signal-catching function, or a signal causes the process to terminate,
/// this sleep is interrrupted.
/// see also [man 3 clock_nanosleep](https://pubs.opengroup.org/onlinepubs/009695399/functions/clock_nanosleep.html)
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "linux",
target_os = "android",
target_os = "solaris",
target_os = "illumos",
))]
Comment on lines +275 to +282
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use cfg aliases now:

Suggested change
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "linux",
target_os = "android",
target_os = "solaris",
target_os = "illumos",
))]
#[cfg(any(
linux_android,
solarish,
target_os = "freebsd",
target_os = "netbsd"
))]

pub fn clock_nanosleep(
clock_id: ClockId,
flags: ClockNanosleepFlags,
rqtp: &TimeSpec,
) -> Result<TimeSpec> {
let mut rmtp: TimeSpec = TimeSpec::nanoseconds(0);
let ret = unsafe {
libc::clock_nanosleep(
clock_id.as_raw(),
flags.bits(),
rqtp.as_ref() as *const _,
rmtp.as_mut() as *mut _,
)
};
if ret == 0 {
Ok(rmtp)
} else {
Err(Error::Sys(Errno::from_i32(ret)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Err(Error::Sys(Errno::from_i32(ret)))
Err(Errno::from_i32(ret))

}
}
15 changes: 14 additions & 1 deletion test/test_time.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use nix::sys::time::{TimeSpec, TimeValLike};
#[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
Expand All @@ -6,7 +7,7 @@
target_os = "emscripten",
))]
use nix::time::clock_getcpuclockid;
use nix::time::{clock_getres, clock_gettime, ClockId};
use nix::time::{clock_getres, clock_gettime, clock_nanosleep, ClockId, ClockNanosleepFlags};

#[test]
pub fn test_clock_getres() {
Expand Down Expand Up @@ -54,3 +55,15 @@ pub fn test_clock_id_pid_cpu_clock_id() {
.map(ClockId::now)
.is_ok());
}

#[test]
pub fn test_clock_nanosleep() {
let sleep_time = TimeSpec::microseconds(1);
let res = clock_nanosleep(
ClockId::CLOCK_MONOTONIC,
ClockNanosleepFlags::empty(),
&sleep_time,
);
let expected = TimeSpec::microseconds(0);
assert_eq!(res, Ok(expected));
}