]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unix/futex.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / library / std / src / sys / unix / futex.rs
1 #![cfg(any(
2 target_os = "linux",
3 target_os = "android",
4 all(target_os = "emscripten", target_feature = "atomics")
5 ))]
6
7 #[cfg(any(target_os = "linux", target_os = "android"))]
8 use crate::convert::TryInto;
9 #[cfg(any(target_os = "linux", target_os = "android"))]
10 use crate::ptr::null;
11 use crate::sync::atomic::AtomicI32;
12 use crate::time::Duration;
13
14 #[cfg(any(target_os = "linux", target_os = "android"))]
15 pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
16 let timespec = timeout.and_then(|d| {
17 Some(libc::timespec {
18 // Sleep forever if the timeout is longer than fits in a timespec.
19 tv_sec: d.as_secs().try_into().ok()?,
20 // This conversion never truncates, as subsec_nanos is always <1e9.
21 tv_nsec: d.subsec_nanos() as _,
22 })
23 });
24 unsafe {
25 libc::syscall(
26 libc::SYS_futex,
27 futex as *const AtomicI32,
28 libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
29 expected,
30 timespec.as_ref().map_or(null(), |d| d as *const libc::timespec),
31 );
32 }
33 }
34
35 #[cfg(target_os = "emscripten")]
36 pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
37 extern "C" {
38 fn emscripten_futex_wait(
39 addr: *const AtomicI32,
40 val: libc::c_uint,
41 max_wait_ms: libc::c_double,
42 ) -> libc::c_int;
43 }
44
45 unsafe {
46 emscripten_futex_wait(
47 futex as *const AtomicI32,
48 // `val` is declared unsigned to match the Emscripten headers, but since it's used as
49 // an opaque value, we can ignore the meaning of signed vs. unsigned and cast here.
50 expected as libc::c_uint,
51 timeout.map_or(crate::f64::INFINITY, |d| d.as_secs_f64() * 1000.0),
52 );
53 }
54 }
55
56 #[cfg(any(target_os = "linux", target_os = "android"))]
57 pub fn futex_wake(futex: &AtomicI32) {
58 unsafe {
59 libc::syscall(
60 libc::SYS_futex,
61 futex as *const AtomicI32,
62 libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
63 1,
64 );
65 }
66 }
67
68 #[cfg(target_os = "emscripten")]
69 pub fn futex_wake(futex: &AtomicI32) {
70 extern "C" {
71 fn emscripten_futex_wake(addr: *const AtomicI32, count: libc::c_int) -> libc::c_int;
72 }
73
74 unsafe {
75 emscripten_futex_wake(futex as *const AtomicI32, 1);
76 }
77 }