-
Notifications
You must be signed in to change notification settings - Fork 273
/
thread.rs
65 lines (59 loc) · 1.79 KB
/
thread.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
fn get_worker_count() -> usize {
#[cfg(all(feature = "worker-max", feature = "worker-single"))]
{
compile_error!(r#"Cannot use "worker-max" and "worker-single" features together"#);
}
#[cfg(not(any(feature = "worker-max", feature = "worker-single")))]
{
4 // Default
}
#[cfg(feature = "worker-single")]
{
1
}
#[cfg(feature = "worker-max")]
{
#[cfg(not(wasm))]
{
std::thread::available_parallelism().unwrap().get()
}
#[cfg(wasm)]
{
let mut key;
let global_object = js_sys::global();
let global = global_object.as_ref();
key = wasm_bindgen::JsValue::from_str("navigator");
let navigator = js_sys::Reflect::get(&global, &key).unwrap();
key = wasm_bindgen::JsValue::from_str("hardwareConcurrency");
let hardware_concurrency = js_sys::Reflect::get(&navigator, &key).unwrap();
hardware_concurrency.as_f64().unwrap() as usize
}
}
}
#[cfg(not(wasm))]
mod io {
use super::*;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use threadpool::ThreadPool;
lazy_static! {
pub static ref THREAD_POOL: Mutex<ThreadPool> = Mutex::new(ThreadPool::with_name(
"frb_workerpool".into(),
get_worker_count()
));
}
}
#[cfg(not(wasm))]
pub use io::THREAD_POOL;
#[cfg(wasm)]
mod web {
use super::*;
use crate::{script_path, wasm_bindgen_src::pool::WorkerPool};
thread_local! {
pub static WORKER_POOL: Option<WorkerPool> = WorkerPool::new(
get_worker_count(), script_path().unwrap())
.map_err(|err| crate::console_error!("Failed to spawn worker: {:?}", err)).ok()
}
}
#[cfg(wasm)]
pub use web::WORKER_POOL;