Skip to content

Commit 65987ae

Browse files
committed
Make std::sys::wasm::futex consistent with unix::futex.
1 parent 8305398 commit 65987ae

File tree

1 file changed

+18
-4
lines changed
  • library/std/src/sys/wasm/atomics

1 file changed

+18
-4
lines changed

library/std/src/sys/wasm/atomics/futex.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ use crate::convert::TryInto;
33
use crate::sync::atomic::AtomicU32;
44
use crate::time::Duration;
55

6-
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) {
6+
/// Wait for a futex_wake operation to wake us.
7+
///
8+
/// Returns directly if the futex doesn't hold the expected value.
9+
///
10+
/// Returns false on timeout, and true in all other cases.
11+
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
712
let timeout = timeout.and_then(|t| t.as_nanos().try_into().ok()).unwrap_or(-1);
813
unsafe {
914
wasm32::memory_atomic_wait32(
1015
futex as *const AtomicU32 as *mut i32,
1116
expected as i32,
1217
timeout,
13-
);
18+
) < 2
1419
}
1520
}
1621

17-
pub fn futex_wake(futex: &AtomicU32) {
22+
/// Wake up one thread that's blocked on futex_wait on this futex.
23+
///
24+
/// Returns true if this actually woke up such a thread,
25+
/// or false if no thread was waiting on this futex.
26+
pub fn futex_wake(futex: &AtomicU32) -> bool {
27+
unsafe { wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, 1) > 0 }
28+
}
29+
30+
/// Wake up all threads that are waiting on futex_wait on this futex.
31+
pub fn futex_wake_all(futex: &AtomicU32) {
1832
unsafe {
19-
wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, 1);
33+
wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, i32::MAX as u32);
2034
}
2135
}

0 commit comments

Comments
 (0)