Skip to content

Commit 235fb8b

Browse files
authored
Rollup merge of rust-lang#77619 - fusion-engineering-forks:wasm-parker, r=dtolnay
Use futex-based thread-parker for Wasm32. This uses the existing `sys_common/thread_parker/futex.rs` futex-based thread parker (that was already used for Linux) for wasm32 as well (if the wasm32 atomics target feature is enabled, which is not the case by default). Wasm32 provides the basic futex operations as instructions: https://webassembly.github.io/threads/syntax/instructions.html These are now exposed from `sys::futex::{futex_wait, futex_wake}`, just like on Linux. So, `thread_parker/futex.rs` stays completely unmodified.
2 parents 284f1df + f84f01c commit 235fb8b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::arch::wasm32;
2+
use crate::convert::TryInto;
3+
use crate::sync::atomic::AtomicI32;
4+
use crate::time::Duration;
5+
6+
pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
7+
let timeout = timeout.and_then(|t| t.as_nanos().try_into().ok()).unwrap_or(-1);
8+
unsafe {
9+
wasm32::memory_atomic_wait32(futex as *const AtomicI32 as *mut i32, expected, timeout);
10+
}
11+
}
12+
13+
pub fn futex_wake(futex: &AtomicI32) {
14+
unsafe {
15+
wasm32::memory_atomic_notify(futex as *const AtomicI32 as *mut i32, 1);
16+
}
17+
}

library/std/src/sys/wasm/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ cfg_if::cfg_if! {
5555
pub mod mutex;
5656
#[path = "rwlock_atomics.rs"]
5757
pub mod rwlock;
58+
#[path = "futex_atomics.rs"]
59+
pub mod futex;
5860
} else {
5961
#[path = "../unsupported/condvar.rs"]
6062
pub mod condvar;

library/std/src/sys_common/thread_parker/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
cfg_if::cfg_if! {
2-
if #[cfg(any(target_os = "linux", target_os = "android"))] {
2+
if #[cfg(any(
3+
target_os = "linux",
4+
target_os = "android",
5+
all(target_arch = "wasm32", target_feature = "atomics"),
6+
))] {
37
mod futex;
48
pub use futex::Parker;
59
} else {

0 commit comments

Comments
 (0)