Use futex-based thread-parker for Wasm32.

This commit is contained in:
Mara Bos 2020-10-06 20:01:42 +02:00
parent 5ded394553
commit f84f01c014
3 changed files with 24 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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 {