Skip to content

Commit 3959cd8

Browse files
authored
Rollup merge of rust-lang#96206 - m-ou-se:wasm-futex-locks, r=alexcrichton
Use sys::unix::locks::futex* on wasm+atomics. This removes the wasm-specific lock implementations and instead re-uses the implementations from sys::unix. Tracking issue: rust-lang#93740 cc `@alexcrichton`
2 parents c785dec + 8f2913c commit 3959cd8

File tree

6 files changed

+24
-358
lines changed

6 files changed

+24
-358
lines changed

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

-102
This file was deleted.

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
}

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

-64
This file was deleted.

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

-145
This file was deleted.

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

-34
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,3 @@ pub mod guard {
5353
None
5454
}
5555
}
56-
57-
// We currently just use our own thread-local to store our
58-
// current thread's ID, and then we lazily initialize it to something allocated
59-
// from a global counter.
60-
pub fn my_id() -> u32 {
61-
use crate::sync::atomic::{AtomicU32, Ordering::SeqCst};
62-
63-
static NEXT_ID: AtomicU32 = AtomicU32::new(0);
64-
65-
#[thread_local]
66-
static mut MY_ID: u32 = 0;
67-
68-
unsafe {
69-
// If our thread ID isn't set yet then we need to allocate one. Do so
70-
// with with a simple "atomically add to a global counter" strategy.
71-
// This strategy doesn't handled what happens when the counter
72-
// overflows, however, so just abort everything once the counter
73-
// overflows and eventually we could have some sort of recycling scheme
74-
// (or maybe this is all totally irrelevant by that point!). In any case
75-
// though we're using a CAS loop instead of a `fetch_add` to ensure that
76-
// the global counter never overflows.
77-
if MY_ID == 0 {
78-
let mut cur = NEXT_ID.load(SeqCst);
79-
MY_ID = loop {
80-
let next = cur.checked_add(1).unwrap_or_else(|| crate::process::abort());
81-
match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) {
82-
Ok(_) => break next,
83-
Err(i) => cur = i,
84-
}
85-
};
86-
}
87-
MY_ID
88-
}
89-
}

0 commit comments

Comments
 (0)