Don't use futexes on netbsd.

The latest NetBSD release doesn't include the futex syscall yet.
This commit is contained in:
Mara Bos 2022-05-03 09:08:38 +02:00
parent 1b9c7e6f1a
commit 7b7d1d6c48
4 changed files with 5 additions and 44 deletions

View File

@ -4,27 +4,18 @@
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
))]
use crate::sync::atomic::AtomicU32;
use crate::time::Duration;
#[cfg(target_os = "netbsd")]
pub const SYS___futex: i32 = 166;
/// 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.
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "netbsd"
))]
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
use super::time::Timespec;
use crate::ptr::null;
@ -65,19 +56,6 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
crate::ptr::invalid_mut(umtx_timeout_size),
umtx_timeout_ptr as *mut _,
)
} else if #[cfg(target_os = "netbsd")] {
// Netbsd's futex syscall takes addr2 and val2 as separate arguments.
// (Both are unused for FUTEX_WAIT[_BITSET].)
libc::syscall(
SYS___futex,
futex as *const AtomicU32,
libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG,
expected,
timespec.as_ref().map_or(null(), |t| &t.t as *const libc::timespec),
null::<u32>(), // addr2: This argument is unused for FUTEX_WAIT_BITSET.
0, // val2: This argument is unused for FUTEX_WAIT_BITSET.
!0u32, // val3 / bitmask: A full bitmask, to make it behave like a regular FUTEX_WAIT.
)
} else {
libc::syscall(
libc::SYS_futex,
@ -106,36 +84,22 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
/// or false if no thread was waiting on this futex.
///
/// On some platforms, this always returns false.
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn futex_wake(futex: &AtomicU32) -> bool {
let ptr = futex as *const AtomicU32;
let op = libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG;
unsafe {
cfg_if::cfg_if! {
if #[cfg(target_os = "netbsd")] {
libc::syscall(SYS___futex, ptr, op, 1) > 0
} else {
libc::syscall(libc::SYS_futex, ptr, op, 1) > 0
}
}
}
unsafe { libc::syscall(libc::SYS_futex, ptr, op, 1) > 0 }
}
/// Wake up all threads that are waiting on futex_wait on this futex.
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn futex_wake_all(futex: &AtomicU32) {
let ptr = futex as *const AtomicU32;
let op = libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG;
unsafe {
cfg_if::cfg_if! {
if #[cfg(target_os = "netbsd")] {
libc::syscall(SYS___futex, ptr, op, i32::MAX);
} else {
libc::syscall(libc::SYS_futex, ptr, op, i32::MAX);
}
}
}
}
// FreeBSD doesn't tell us how many threads are woken up, so this always returns false.
#[cfg(target_os = "freebsd")]

View File

@ -5,7 +5,6 @@ cfg_if::cfg_if! {
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
))] {
mod futex;

View File

@ -6,7 +6,6 @@
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
)))]

View File

@ -5,7 +5,6 @@ cfg_if::cfg_if! {
all(target_arch = "wasm32", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
))] {
mod futex;