linux_futex/
timeout.rs

1use libc::{c_long, time_t};
2use std::time::{Duration, Instant, SystemTime};
3
4/// A point in time on either the monotonic clock ([`Instant`]) or real time clock ([`SystemTime`]).
5pub unsafe trait Timeout {
6	#[doc(hidden)]
7	fn as_timespec(self) -> (i32, libc::timespec);
8}
9
10unsafe impl Timeout for Instant {
11	#[inline]
12	#[doc(hidden)]
13	fn as_timespec(self) -> (i32, libc::timespec) {
14		(
15			0,
16			as_timespec(self.duration_since(unsafe { std::mem::zeroed() })),
17		)
18	}
19}
20
21unsafe impl Timeout for SystemTime {
22	#[inline]
23	#[doc(hidden)]
24	fn as_timespec(self) -> (i32, libc::timespec) {
25		(
26			libc::FUTEX_CLOCK_REALTIME,
27			as_timespec(self.duration_since(SystemTime::UNIX_EPOCH).unwrap()),
28		)
29	}
30}
31
32#[inline]
33pub(crate) fn as_timespec(d: Duration) -> libc::timespec {
34	libc::timespec {
35		tv_sec: d.as_secs() as time_t,
36		tv_nsec: d.subsec_nanos() as c_long,
37	}
38}