diff --git a/rayon-core/Cargo.toml b/rayon-core/Cargo.toml index 2cd5372bb..f8793352a 100644 --- a/rayon-core/Cargo.toml +++ b/rayon-core/Cargo.toml @@ -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" diff --git a/rayon-core/src/latch.rs b/rayon-core/src/latch.rs index b0cbbd833..6c2e4fe97 100644 --- a/rayon-core/src/latch.rs +++ b/rayon-core/src/latch.rs @@ -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 diff --git a/rayon-core/src/lib.rs b/rayon-core/src/lib.rs index 7001c8c1d..9a9eb3fa4 100644 --- a/rayon-core/src/lib.rs +++ b/rayon-core/src/lib.rs @@ -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" +)))] +pub use std::sync; + +#[cfg(all( + target_arch = "wasm32", + target_os = "unknown", + target_feature = "atomics" +))] +pub 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. diff --git a/rayon-core/src/registry.rs b/rayon-core/src/registry.rs index e4f2ac7cd..46cd22b31 100644 --- a/rayon-core/src/registry.rs +++ b/rayon-core/src/registry.rs @@ -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, @@ -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; diff --git a/rayon-core/src/sleep/mod.rs b/rayon-core/src/sleep/mod.rs index 03d1077f7..fa1f7beed 100644 --- a/rayon-core/src/sleep/mod.rs +++ b/rayon-core/src/sleep/mod.rs @@ -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; diff --git a/src/iter/par_bridge.rs b/src/iter/par_bridge.rs index eb058d3e6..3e7d65d79 100644 --- a/src/iter/par_bridge.rs +++ b/src/iter/par_bridge.rs @@ -1,5 +1,5 @@ +use rayon_core::sync::Mutex; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use std::sync::Mutex; use crate::iter::plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer}; use crate::iter::ParallelIterator; diff --git a/src/result.rs b/src/result.rs index 43685ca43..d6f84e25f 100644 --- a/src/result.rs +++ b/src/result.rs @@ -7,7 +7,7 @@ use crate::iter::plumbing::*; use crate::iter::*; -use std::sync::Mutex; +use rayon_core::sync::Mutex; use crate::option;