4 all(target_os = "emscripten", target_feature = "atomics")
7 #[cfg(any(target_os = "linux", target_os = "android"))]
8 use crate::convert::TryInto;
9 #[cfg(any(target_os = "linux", target_os = "android"))]
11 use crate::sync::atomic::AtomicI32;
12 use crate::time::Duration;
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| {
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 _,
27 futex as *const AtomicI32,
28 libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
30 timespec.as_ref().map_or(null(), |d| d as *const libc::timespec),
35 #[cfg(target_os = "emscripten")]
36 pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
38 fn emscripten_futex_wait(
39 addr: *const AtomicI32,
41 max_wait_ms: libc::c_double,
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),
56 #[cfg(any(target_os = "linux", target_os = "android"))]
57 pub fn futex_wake(futex: &AtomicI32) {
61 futex as *const AtomicI32,
62 libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
68 #[cfg(target_os = "emscripten")]
69 pub fn futex_wake(futex: &AtomicI32) {
71 fn emscripten_futex_wake(addr: *const AtomicI32, count: libc::c_int) -> libc::c_int;
75 emscripten_futex_wake(futex as *const AtomicI32, 1);