Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use wasm-sync to allow usage on the main browser thread #1110

Merged
merged 12 commits into from
Jan 17, 2024
27 changes: 23 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,37 @@ jobs:
# unsupported threading, but we don't have an environment to execute in.
# wasm32-wasi can test the fallback by running in wasmtime.
RReverser marked this conversation as resolved.
Show resolved Hide resolved
wasm:
name: WebAssembly
name: WebAssembly (standalone)
runs-on: ubuntu-latest
strategy:
matrix:
include:
- toolchain: stable
- toolchain: nightly
rustflags: -C target-feature=+atomics,+bulk-memory,+mutable-globals
env:
RUSTFLAGS: ${{ matrix.rustflags }}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
targets: wasm32-unknown-unknown
- run: cargo build --verbose --target wasm32-unknown-unknown

# wasm32-wasi can test the fallback by running in wasmtime.
wasi:
RReverser marked this conversation as resolved.
Show resolved Hide resolved
name: WebAssembly (WASI)
runs-on: ubuntu-latest
env:
CARGO_TARGET_WASM32_WASI_RUNNER: /home/runner/.wasmtime/bin/wasmtime
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown,wasm32-wasi
- run: cargo check --verbose --target wasm32-unknown-unknown
- run: cargo check --verbose --target wasm32-wasi
targets: wasm32-wasi
- run: curl https://wasmtime.dev/install.sh -sSf | bash
- run: cargo build --verbose --target wasm32-wasi
- run: cargo test --verbose --target wasm32-wasi --package rayon
- run: cargo test --verbose --target wasm32-wasi --package rayon-core

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ exclude = ["ci"]
[dependencies]
rayon-core = { version = "1.12.0", path = "rayon-core" }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", target_feature = "atomics"))'.dependencies]
wasm_sync = "0.1.0"

# This is a public dependency!
[dependencies.either]
version = "1.0"
Expand Down
11 changes: 11 additions & 0 deletions ci/compat-Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rayon-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ categories = ["concurrency"]
crossbeam-deque = "0.8.1"
crossbeam-utils = "0.8.0"

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", target_feature = "atomics"))'.dependencies]
wasm_sync = "0.1.0"

[dev-dependencies]
rand = "0.8"
rand_xorshift = "0.3"
Expand Down
3 changes: 2 additions & 1 deletion rayon-core/src/latch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::sync::Arc;
use std::usize;

use crate::registry::{Registry, WorkerThread};
use crate::sync::{Condvar, Mutex};

/// We define various kinds of latches, which are all a primitive signaling
/// mechanism. A latch starts as false. Eventually someone calls `set()` and
Expand Down
14 changes: 14 additions & 0 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ pub use self::thread_pool::current_thread_index;
pub use self::thread_pool::ThreadPool;
pub use self::thread_pool::{yield_local, yield_now, Yield};

#[cfg(not(all(
target_arch = "wasm32",
target_os = "unknown",
target_feature = "atomics"
)))]
use std::sync;

#[cfg(all(
target_arch = "wasm32",
target_os = "unknown",
target_feature = "atomics"
))]
use wasm_sync as sync;

use self::registry::{CustomSpawn, DefaultSpawn, ThreadSpawn};

/// Returns the maximum number of threads that Rayon supports in a single thread-pool.
Expand Down
3 changes: 2 additions & 1 deletion rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::job::{JobFifo, JobRef, StackJob};
use crate::latch::{AsCoreLatch, CoreLatch, Latch, LatchRef, LockLatch, OnceLatch, SpinLatch};
use crate::sleep::Sleep;
use crate::sync::Mutex;
use crate::unwind;
use crate::{
ErrorKind, ExitHandler, PanicHandler, StartHandler, ThreadPoolBuildError, ThreadPoolBuilder,
Expand All @@ -15,7 +16,7 @@ use std::io;
use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Once};
use std::sync::{Arc, Once};
use std::thread;
use std::usize;

Expand Down
2 changes: 1 addition & 1 deletion rayon-core/src/sleep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! for an overview.

use crate::latch::CoreLatch;
use crate::sync::{Condvar, Mutex};
use crossbeam_utils::CachePadded;
use std::sync::atomic::Ordering;
use std::sync::{Condvar, Mutex};
use std::thread;
use std::usize;

Expand Down
15 changes: 14 additions & 1 deletion src/iter/par_bridge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[cfg(not(all(
target_arch = "wasm32",
target_os = "unknown",
target_feature = "atomics"
)))]
use std::sync::Mutex;

#[cfg(all(
target_arch = "wasm32",
target_os = "unknown",
target_feature = "atomics"
))]
use wasm_sync::Mutex;

use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use crate::iter::plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer};
use crate::iter::ParallelIterator;
use crate::{current_num_threads, current_thread_index};
Expand Down