Skip to content

Commit

Permalink
Rollup merge of rust-lang#77619 - fusion-engineering-forks:wasm-parke…
Browse files Browse the repository at this point in the history
…r, 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.
  • Loading branch information
JohnTitor authored Oct 15, 2020
2 parents db8b2cd + f84f01c commit 1fc8249
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions library/std/src/sys/wasm/futex_atomics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::arch::wasm32;
use crate::convert::TryInto;
use crate::sync::atomic::AtomicI32;
use crate::time::Duration;

pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
let timeout = timeout.and_then(|t| t.as_nanos().try_into().ok()).unwrap_or(-1);
unsafe {
wasm32::memory_atomic_wait32(futex as *const AtomicI32 as *mut i32, expected, timeout);
}
}

pub fn futex_wake(futex: &AtomicI32) {
unsafe {
wasm32::memory_atomic_notify(futex as *const AtomicI32 as *mut i32, 1);
}
}
2 changes: 2 additions & 0 deletions library/std/src/sys/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ cfg_if::cfg_if! {
pub mod mutex;
#[path = "rwlock_atomics.rs"]
pub mod rwlock;
#[path = "futex_atomics.rs"]
pub mod futex;
} else {
#[path = "../unsupported/condvar.rs"]
pub mod condvar;
Expand Down
6 changes: 5 additions & 1 deletion library/std/src/sys_common/thread_parker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
cfg_if::cfg_if! {
if #[cfg(any(target_os = "linux", target_os = "android"))] {
if #[cfg(any(
target_os = "linux",
target_os = "android",
all(target_arch = "wasm32", target_feature = "atomics"),
))] {
mod futex;
pub use futex::Parker;
} else {
Expand Down

0 comments on commit 1fc8249

Please sign in to comment.