Skip to content

Commit

Permalink
Merge #71
Browse files Browse the repository at this point in the history
71: Disable multithreading for all wasm32 targets. r=torkleyy a=BartAdv

With the coming of `wasm32-unknown-unknown` it might be good idea to use `target_arch = "wasm32"` to detect whether to build with multithreading support or not.
  • Loading branch information
bors[bot] committed Feb 10, 2018
2 parents 2edde02 + fd2536e commit 65b806e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ travis-ci = { repository = "torkleyy/shred" }
arrayvec = "0.3"
fxhash = "0.2"
mopa = "0.2"
pulse = "0.5"
rayon = "0.8"
pulse = { version = "0.5", optional = true }
rayon = { version = "0.8", optional = true }
shred-derive = { path = "shred-derive", version = "0.3" }
smallvec = "0.4"

[dev-dependencies]
cgmath = "0.15"

[features]
default = ["parallel"]
parallel = ["pulse", "rayon"]

[[example]]
name = "par_seq"
required-features = ["parallel"]
14 changes: 7 additions & 7 deletions src/dispatch/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub struct DispatcherBuilder<'a, 'b> {
map: FxHashMap<String, SystemId>,
stages_builder: StagesBuilder<'a>,
thread_local: ThreadLocal<'b>,
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
thread_pool:
Option<::std::sync::Arc<::rayon::ThreadPool>>,
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
/// Same as
/// [`add_pool()`](struct.DispatcherBuilder.html#method.add_pool),
/// but returns `self` to enable method chaining.
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub fn with_pool(mut self, pool: ::std::sync::Arc<::rayon::ThreadPool>) -> Self {
self.add_pool(pool);

Expand All @@ -248,7 +248,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {

/// Attach a rayon thread pool to the builder
/// and use that instead of creating one.
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub fn add_pool(&mut self, pool: ::std::sync::Arc<::rayon::ThreadPool>) {
self.thread_pool = Some(pool);
}
Expand All @@ -261,14 +261,14 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
pub fn build(self) -> Dispatcher<'a, 'b> {
use dispatch::dispatcher::new_dispatcher;

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
let d = new_dispatcher(
self.stages_builder.build(),
self.thread_local,
self.thread_pool.unwrap_or_else(Self::create_thread_pool),
);

#[cfg(target_os = "emscripten")]
#[cfg(not(feature = "parallel"))]
let d = new_dispatcher(self.stages_builder.build(), self.thread_local);

d
Expand All @@ -281,7 +281,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
SystemId(id)
}

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
fn create_thread_pool() -> ::std::sync::Arc<::rayon::ThreadPool> {
use std::sync::Arc;
use rayon::{Configuration, ThreadPool};
Expand All @@ -292,7 +292,7 @@ impl<'a, 'b> DispatcherBuilder<'a, 'b> {
}
}

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
impl<'b> DispatcherBuilder<'static, 'b> {
/// Builds an async dispatcher.
///
Expand Down
17 changes: 9 additions & 8 deletions src/dispatch/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use system::RunNow;
pub struct Dispatcher<'a, 'b> {
stages: Vec<Stage<'a>>,
thread_local: ThreadLocal<'b>,
#[cfg(not(target_os = "emscripten"))] thread_pool: ::std::sync::Arc<::rayon::ThreadPool>,
#[cfg(feature = "parallel")]
thread_pool: ::std::sync::Arc<::rayon::ThreadPool>,
}

impl<'a, 'b> Dispatcher<'a, 'b> {
Expand All @@ -29,10 +30,10 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
/// [`dispatch_par`]: struct.Dispatcher.html#method.dispatch_par
/// [`dispatch_seq`]: struct.Dispatcher.html#method.dispatch_seq
pub fn dispatch(&mut self, res: &Resources) {
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
self.dispatch_par(res);

#[cfg(target_os = "emscripten")]
#[cfg(not(feature = "parallel"))]
self.dispatch_seq(res);

self.dispatch_thread_local(res);
Expand All @@ -44,12 +45,11 @@ impl<'a, 'b> Dispatcher<'a, 'b> {
/// This operation blocks the
/// executing thread.
///
/// Only available on platforms with
/// multithreading support (so not on emscripten).
/// Only available with "parallel" feature enabled.
///
/// Please note that this method assumes that no resource
/// is currently borrowed. If that's the case, it panics.
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub fn dispatch_par(&mut self, res: &Resources) {
let stages = &mut self.stages;

Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct SystemId(pub usize);
pub type SystemExecSend<'b> = Box<for<'a> RunNow<'a> + Send + 'b>;
pub type ThreadLocal<'a> = SmallVec<[Box<for<'b> RunNow<'b> + 'a>; 4]>;

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub fn new_dispatcher<'a, 'b>(
stages: Vec<Stage<'a>>,
thread_local: ThreadLocal<'b>,
Expand All @@ -101,7 +101,7 @@ pub fn new_dispatcher<'a, 'b>(
}
}

#[cfg(target_os = "emscripten")]
#[cfg(not(feature = "parallel"))]
pub fn new_dispatcher<'a, 'b>(
stages: Vec<Stage<'a>>,
thread_local: ThreadLocal<'b>,
Expand Down Expand Up @@ -185,6 +185,7 @@ mod tests {
}

#[test]
#[cfg(feature = "parallel")]
fn stages_async() {
let mut d = new_builder().build_async(new_resources());

Expand Down
8 changes: 4 additions & 4 deletions src/dispatch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub use self::async::AsyncDispatcher;
pub use self::builder::DispatcherBuilder;
pub use self::dispatcher::Dispatcher;
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub use self::par_seq::{Par, ParSeq, Seq};

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
mod async;
mod builder;
mod dispatcher;
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
mod par_seq;
mod stage;
mod util;
2 changes: 1 addition & 1 deletion src/dispatch/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a> Stage<'a> {
Default::default()
}

#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub fn execute(&mut self, res: &Resources) {
use rayon::prelude::*;

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ extern crate arrayvec;
extern crate fxhash;
#[macro_use]
extern crate mopa;
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
extern crate pulse;
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
extern crate rayon;
extern crate smallvec;

Expand All @@ -73,9 +73,9 @@ mod res;
mod system;

pub use dispatch::{Dispatcher, DispatcherBuilder};
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub use dispatch::{Par, ParSeq, Seq};
#[cfg(not(target_os = "emscripten"))]
#[cfg(feature = "parallel")]
pub use dispatch::AsyncDispatcher;
pub use res::{Fetch, FetchId, FetchIdMut, FetchMut, Resource, ResourceId, Resources};
pub use system::{RunNow, RunningTime, System, SystemData};

0 comments on commit 65b806e

Please sign in to comment.