]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/futex.rs
Rollup merge of #77890 - gilescope:welformed-json-output-from-libtest, r=KodrAus
[rust.git] / library / std / src / sys / unix / futex.rs
1 #![cfg(any(target_os = "linux", target_os = "android"))]
2
3 use crate::convert::TryInto;
4 use crate::ptr::null;
5 use crate::sync::atomic::AtomicI32;
6 use crate::time::Duration;
7
8 pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
9     let timespec = timeout.and_then(|d| {
10         Some(libc::timespec {
11             // Sleep forever if the timeout is longer than fits in a timespec.
12             tv_sec: d.as_secs().try_into().ok()?,
13             // This conversion never truncates, as subsec_nanos is always <1e9.
14             tv_nsec: d.subsec_nanos() as _,
15         })
16     });
17     unsafe {
18         libc::syscall(
19             libc::SYS_futex,
20             futex as *const AtomicI32,
21             libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
22             expected,
23             timespec.as_ref().map_or(null(), |d| d as *const libc::timespec),
24         );
25     }
26 }
27
28 pub fn futex_wake(futex: &AtomicI32) {
29     unsafe {
30         libc::syscall(
31             libc::SYS_futex,
32             futex as *const AtomicI32,
33             libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
34             1,
35         );
36     }
37 }