Skip to content

Commit

Permalink
Merge pull request #1110 from RReverser/wasm-sync
Browse files Browse the repository at this point in the history
Use wasm-sync to allow usage on the main browser thread
  • Loading branch information
cuviper authored Jan 17, 2024
2 parents b80b406 + 97e7d77 commit b03b68c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 10 deletions.
31 changes: 25 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,39 @@ jobs:

# wasm32-unknown-unknown builds, and even has the runtime fallback for
# unsupported threading, but we don't have an environment to execute in.
# wasm32-wasi can test the fallback by running in wasmtime.
wasm:
name: WebAssembly
name: WebAssembly (standalone)
runs-on: ubuntu-latest
strategy:
matrix:
include:
- toolchain: stable
- toolchain: nightly
cargoflags: --features web_spin_lock
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 ${{ matrix.cargoflags }}

# wasm32-wasi can test the fallback by running in wasmtime.
wasi:
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 All @@ -93,6 +112,6 @@ jobs:
done:
name: Complete
runs-on: ubuntu-latest
needs: [check, test, demo, i686, wasm, fmt]
needs: [check, test, demo, i686, wasm, wasi, fmt]
steps:
- run: exit 0
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ exclude = ["ci"]

[dependencies]
rayon-core = { version = "1.12.0", path = "rayon-core" }
wasm_sync = { version = "0.1.0", optional = true }

# This is a public dependency!
[dependencies.either]
version = "1.0"
default-features = false

[features]
# This feature switches to a spin-lock implementation on the browser's
# main thread to avoid the forbidden `atomics.wait`.
#
# Only useful on the `wasm32-unknown-unknown` target.
web_spin_lock = ["dep:wasm_sync", "rayon-core/web_spin_lock"]

[dev-dependencies]
rand = "0.8"
rand_xorshift = "0.3"
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.

9 changes: 9 additions & 0 deletions rayon-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ categories = ["concurrency"]
[dependencies]
crossbeam-deque = "0.8.1"
crossbeam-utils = "0.8.0"
wasm_sync = { version = "0.1.0", optional = true }

[features]

# This feature switches to a spin-lock implementation on the browser's
# main thread to avoid the forbidden `atomics.wait`.
#
# Only useful on the `wasm32-unknown-unknown` target.
web_spin_lock = ["dep:wasm_sync"]

[dev-dependencies]
rand = "0.8"
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
6 changes: 6 additions & 0 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ 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(feature = "web_spin_lock"))]
use std::sync;

#[cfg(feature = "web_spin_lock")]
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
7 changes: 6 additions & 1 deletion src/iter/par_bridge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[cfg(not(feature = "web_spin_lock"))]
use std::sync::Mutex;

#[cfg(feature = "web_spin_lock")]
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

0 comments on commit b03b68c

Please sign in to comment.