File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ Add ` clock_nanosleep() `
Original file line number Diff line number Diff line change @@ -199,3 +199,55 @@ pub fn clock_getcpuclockid(pid: Pid) -> Result<ClockId> {
199199 Err ( Errno :: from_i32 ( ret) )
200200 }
201201}
202+
203+ #[ cfg( any(
204+ linux_android,
205+ solarish,
206+ freebsdlike,
207+ target_os = "netbsd" ,
208+ target_os = "hurd" ,
209+ target_os = "aix"
210+ ) ) ]
211+ libc_bitflags ! {
212+ /// Flags that are used for arming the timer.
213+ pub struct ClockNanosleepFlags : libc:: c_int {
214+ TIMER_ABSTIME ;
215+ }
216+ }
217+
218+ /// Suspend execution of this thread for the amount of time specified by `request`
219+ /// and measured against the clock speficied by `clock_id`. If `flags` is
220+ /// `TIMER_ABSTIME`, this function will suspend execution until the time value of
221+ /// clock_id reaches the absolute time specified by `request`. If a signal is caught
222+ /// by a signal-catching function, or a signal causes the process to terminate,
223+ /// this sleep is interrrupted.
224+ ///
225+ /// see also [man 3 clock_nanosleep](https://pubs.opengroup.org/onlinepubs/009695399/functions/clock_nanosleep.html)
226+ #[ cfg( any(
227+ linux_android,
228+ solarish,
229+ freebsdlike,
230+ target_os = "netbsd" ,
231+ target_os = "hurd" ,
232+ target_os = "aix"
233+ ) ) ]
234+ pub fn clock_nanosleep (
235+ clock_id : ClockId ,
236+ flags : ClockNanosleepFlags ,
237+ request : & TimeSpec ,
238+ ) -> Result < TimeSpec > {
239+ let mut remain = TimeSpec :: new ( 0 , 0 ) ;
240+ let ret = unsafe {
241+ libc:: clock_nanosleep (
242+ clock_id. as_raw ( ) ,
243+ flags. bits ( ) ,
244+ request. as_ref ( ) as * const _ ,
245+ remain. as_mut ( ) as * mut _ ,
246+ )
247+ } ;
248+ if ret == 0 {
249+ Ok ( remain)
250+ } else {
251+ Err ( Errno :: from_i32 ( ret) )
252+ }
253+ }
Original file line number Diff line number Diff line change @@ -39,3 +39,28 @@ pub fn test_clock_id_pid_cpu_clock_id() {
3939 . unwrap ( )
4040 . unwrap ( ) ;
4141}
42+
43+ #[ cfg( any(
44+ linux_android,
45+ solarish,
46+ freebsdlike,
47+ target_os = "netbsd" ,
48+ target_os = "hurd" ,
49+ target_os = "aix"
50+ ) ) ]
51+ #[ test]
52+ pub fn test_clock_nanosleep ( ) {
53+ use nix:: {
54+ sys:: time:: { TimeSpec , TimeValLike } ,
55+ time:: { clock_nanosleep, ClockNanosleepFlags } ,
56+ } ;
57+
58+ let sleep_time = TimeSpec :: microseconds ( 1 ) ;
59+ let res = clock_nanosleep (
60+ ClockId :: CLOCK_MONOTONIC ,
61+ ClockNanosleepFlags :: empty ( ) ,
62+ & sleep_time,
63+ ) ;
64+ let expected = TimeSpec :: microseconds ( 0 ) ;
65+ assert_eq ! ( res, Ok ( expected) ) ;
66+ }
You can’t perform that action at this time.
0 commit comments