about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2022-04-19 09:20:51 +0200
committerMara Bos <m-ou.se@m-ou.se>2022-04-19 09:21:54 +0200
commit65987ae8f57e6f529c0de33ac0787a20bad266fb (patch)
tree74755863e665ba25824052b5435fadfeb73d1c40
parent8305398d7ae6128811ec2b3223939bcd067530c2 (diff)
downloadrust-65987ae8f57e6f529c0de33ac0787a20bad266fb.tar.gz
rust-65987ae8f57e6f529c0de33ac0787a20bad266fb.zip
Make std::sys::wasm::futex consistent with unix::futex.
-rw-r--r--library/std/src/sys/wasm/atomics/futex.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/library/std/src/sys/wasm/atomics/futex.rs b/library/std/src/sys/wasm/atomics/futex.rs
index bbe9bd6951a..11413ba3bf5 100644
--- a/library/std/src/sys/wasm/atomics/futex.rs
+++ b/library/std/src/sys/wasm/atomics/futex.rs
@@ -3,19 +3,33 @@ use crate::convert::TryInto;
 use crate::sync::atomic::AtomicU32;
 use crate::time::Duration;
 
-pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) {
+/// 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.
+pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
     let timeout = timeout.and_then(|t| t.as_nanos().try_into().ok()).unwrap_or(-1);
     unsafe {
         wasm32::memory_atomic_wait32(
             futex as *const AtomicU32 as *mut i32,
             expected as i32,
             timeout,
-        );
+        ) < 2
     }
 }
 
-pub fn futex_wake(futex: &AtomicU32) {
+/// 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.
+pub fn futex_wake(futex: &AtomicU32) -> bool {
+    unsafe { wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, 1) > 0 }
+}
+
+/// Wake up all threads that are waiting on futex_wait on this futex.
+pub fn futex_wake_all(futex: &AtomicU32) {
     unsafe {
-        wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, 1);
+        wasm32::memory_atomic_notify(futex as *const AtomicU32 as *mut i32, i32::MAX as u32);
     }
 }