2020-11-11 22:25:48 +00:00
|
|
|
#![cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "android",
|
|
|
|
|
all(target_os = "emscripten", target_feature = "atomics")
|
|
|
|
|
))]
|
2020-09-19 18:03:10 +02:00
|
|
|
|
2022-04-14 09:51:25 +02:00
|
|
|
use crate::sync::atomic::AtomicU32;
|
2020-09-19 18:03:10 +02:00
|
|
|
use crate::time::Duration;
|
|
|
|
|
|
2022-04-11 14:26:52 +02:00
|
|
|
/// Wait for a futex_wake operation to wake us.
|
|
|
|
|
///
|
|
|
|
|
/// Returns directly if the futex doesn't hold the expected value.
|
|
|
|
|
///
|
|
|
|
|
/// Returns false on timeout, and true in all other cases.
|
2020-11-11 22:25:48 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2022-04-14 09:51:25 +02:00
|
|
|
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
|
2022-03-24 11:11:31 +01:00
|
|
|
use super::time::Timespec;
|
2022-03-24 09:51:48 +01:00
|
|
|
use crate::ptr::null;
|
|
|
|
|
use crate::sync::atomic::Ordering::Relaxed;
|
|
|
|
|
|
|
|
|
|
// Calculate the timeout as an absolute timespec.
|
2022-04-01 11:10:58 +02:00
|
|
|
//
|
|
|
|
|
// Overflows are rounded up to an infinite timeout (None).
|
2022-03-24 09:51:48 +01:00
|
|
|
let timespec =
|
2022-03-24 11:11:31 +01:00
|
|
|
timeout.and_then(|d| Some(Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d)?));
|
2022-03-24 09:51:48 +01:00
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
// No need to wait if the value already changed.
|
|
|
|
|
if futex.load(Relaxed) != expected {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use FUTEX_WAIT_BITSET rather than FUTEX_WAIT to be able to give an
|
|
|
|
|
// absolute time rather than a relative time.
|
|
|
|
|
let r = unsafe {
|
|
|
|
|
libc::syscall(
|
|
|
|
|
libc::SYS_futex,
|
2022-04-14 09:51:25 +02:00
|
|
|
futex as *const AtomicU32,
|
2022-03-24 09:51:48 +01:00
|
|
|
libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG,
|
|
|
|
|
expected,
|
2022-03-24 11:11:31 +01:00
|
|
|
timespec.as_ref().map_or(null(), |t| &t.t as *const libc::timespec),
|
2022-03-24 09:51:48 +01:00
|
|
|
null::<u32>(), // This argument is unused for FUTEX_WAIT_BITSET.
|
|
|
|
|
!0u32, // A full bitmask, to make it behave like a regular FUTEX_WAIT.
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match (r < 0).then(super::os::errno) {
|
|
|
|
|
Some(libc::ETIMEDOUT) => return false,
|
|
|
|
|
Some(libc::EINTR) => continue,
|
|
|
|
|
_ => return true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-19 18:03:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 22:25:48 +00:00
|
|
|
#[cfg(target_os = "emscripten")]
|
2022-04-14 09:51:25 +02:00
|
|
|
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) {
|
2020-11-11 22:25:48 +00:00
|
|
|
extern "C" {
|
|
|
|
|
fn emscripten_futex_wait(
|
2022-04-14 09:51:25 +02:00
|
|
|
addr: *const AtomicU32,
|
2020-11-11 22:25:48 +00:00
|
|
|
val: libc::c_uint,
|
|
|
|
|
max_wait_ms: libc::c_double,
|
|
|
|
|
) -> libc::c_int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
emscripten_futex_wait(
|
2022-04-14 09:51:25 +02:00
|
|
|
futex,
|
|
|
|
|
expected,
|
2020-11-12 03:40:15 +00:00
|
|
|
timeout.map_or(crate::f64::INFINITY, |d| d.as_secs_f64() * 1000.0),
|
2020-11-11 22:25:48 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 14:26:52 +02:00
|
|
|
/// Wake up one thread that's blocked on futex_wait on this futex.
|
|
|
|
|
///
|
|
|
|
|
/// Returns true if this actually woke up such a thread,
|
|
|
|
|
/// or false if no thread was waiting on this futex.
|
2020-11-11 22:25:48 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2022-04-14 09:51:25 +02:00
|
|
|
pub fn futex_wake(futex: &AtomicU32) -> bool {
|
2020-09-19 18:03:10 +02:00
|
|
|
unsafe {
|
|
|
|
|
libc::syscall(
|
|
|
|
|
libc::SYS_futex,
|
2022-04-14 09:51:25 +02:00
|
|
|
futex as *const AtomicU32,
|
2020-09-19 18:03:10 +02:00
|
|
|
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
|
|
|
|
|
1,
|
2022-04-06 16:30:49 +02:00
|
|
|
) > 0
|
2020-09-19 18:03:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-11 22:25:48 +00:00
|
|
|
|
2022-04-11 14:26:52 +02:00
|
|
|
/// Wake up all threads that are waiting on futex_wait on this futex.
|
2022-03-17 12:27:30 +01:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2022-04-14 09:51:25 +02:00
|
|
|
pub fn futex_wake_all(futex: &AtomicU32) {
|
2022-03-17 12:27:30 +01:00
|
|
|
unsafe {
|
|
|
|
|
libc::syscall(
|
|
|
|
|
libc::SYS_futex,
|
2022-04-14 09:51:25 +02:00
|
|
|
futex as *const AtomicU32,
|
2022-03-17 12:27:30 +01:00
|
|
|
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
|
|
|
|
|
i32::MAX,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 22:25:48 +00:00
|
|
|
#[cfg(target_os = "emscripten")]
|
2022-04-14 09:51:25 +02:00
|
|
|
pub fn futex_wake(futex: &AtomicU32) -> bool {
|
2020-11-11 22:25:48 +00:00
|
|
|
extern "C" {
|
2022-04-14 09:51:25 +02:00
|
|
|
fn emscripten_futex_wake(addr: *const AtomicU32, count: libc::c_int) -> libc::c_int;
|
2020-11-11 22:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 09:51:25 +02:00
|
|
|
unsafe { emscripten_futex_wake(futex, 1) > 0 }
|
2020-11-11 22:25:48 +00:00
|
|
|
}
|