@@ -3,19 +3,33 @@ use crate::convert::TryInto;
3
3
use crate :: sync:: atomic:: AtomicU32 ;
4
4
use crate :: time:: Duration ;
5
5
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 {
7
12
let timeout = timeout. and_then ( |t| t. as_nanos ( ) . try_into ( ) . ok ( ) ) . unwrap_or ( -1 ) ;
8
13
unsafe {
9
14
wasm32:: memory_atomic_wait32 (
10
15
futex as * const AtomicU32 as * mut i32 ,
11
16
expected as i32 ,
12
17
timeout,
13
- ) ;
18
+ ) < 2
14
19
}
15
20
}
16
21
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 ) {
18
32
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 ) ;
20
34
}
21
35
}
0 commit comments