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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! A thread pool library based on `wasm-mt` ([github](https://github.com/w3reality/wasm-mt) | [crate](https://crates.io/crates/wasm-mt)).
//!
//! #### Examples
//!
//! You can run all the following apps in browser!
//!
//! - **pool_exec** - How to use <code>wasm_mt_pool</code>. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/pool_exec/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/pool_exec) ]
//! - **http** - A multithreaded server based on <code>wasm_mt_pool</code>. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/http/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/http) ]
//! - **pool_arraybuffers** - Demo of using <code>ThreadPool::new_with_arraybuffers()</code>. [ [live](https://w3reality.github.io/wasm-mt/crates/pool/examples/pool_arraybuffers/index.html) | [source](https://github.com/w3reality/wasm-mt/tree/master/crates/pool/examples/pool_arraybuffers) ]
//!
//! # Getting started
//!
//! Requirements:
//!
//! - rustc (nightly)
//! - [`wasm-pack build`](https://github.com/rustwasm/wasm-pack#%EF%B8%8F-commands) with the [`--target no-modules`](https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#without-a-bundler) option
//!
//! Cargo.toml:
//!
//! ```toml
//! wasm-mt-pool = "0.1"
//! serde = { version = "1.0", features = ["derive"] }
//! serde_closure = "0.2"
//! ```
//!
//! # Usage
//!
//! ```rust
//! #![feature(async_closure)]
//!
//! use wasm_mt_pool::prelude::*;
//! use wasm_mt::utils::{console_ln, sleep};
//!
//! let size = 2;
//! let pkg_js = "./pkg/pool_exec.js"; // path to `wasm-bindgen`'s JS binding
//! let pool = ThreadPool::new(size, pkg_js).and_init().await.unwrap();
//!
//! let num = 4;
//!
//! console_ln!("a) 💦 pool_exec! {} closures:", num);
//! for _ in 0..num {
//!     pool_exec!(pool, move || {
//!         console_ln!("a) closure: done.");
//!         Ok(JsValue::NULL)
//!     });
//! }
//!
//! console_ln!("b) 💦 pool_exec! {} async closures:", num);
//! for _ in 0..num {
//!     pool_exec!(pool, async move || {
//!         sleep(1000).await;
//!         console_ln!("b) async closure: done.");
//!         Ok(JsValue::NULL)
//!     });
//! }
//!
//! let cb = move |result| {
//!     console_ln!("callback: result: {:?}", result);
//! };
//!
//! console_ln!("c) 💦 pool_exec! {} closures with callback:", num);
//! for _ in 0..num {
//!     pool_exec!(pool, move || {
//!         console_ln!("c) closure: done.");
//!         Ok(JsValue::from("C"))
//!     }, cb);
//! }
//!
//! console_ln!("d) 💦 pool_exec! {} async closures with callback:", num);
//! for _ in 0..num {
//!     pool_exec!(pool, async move || {
//!         sleep(1000).await;
//!         console_ln!("d) async closure: done.");
//!         Ok(JsValue::from("D"))
//!     }, cb);
//! }
//!
//! sleep(6_000).await; // Do sleep long enough to ensure all jobs are completed.
//! assert_eq!(pool.count_pending_jobs(), 0);
//! ```

#![feature(trait_alias)]
#![feature(async_closure)]

pub use wasm_mt;
use wasm_mt::{debug_ln, WasmMt, Thread, MtClosure, MtAsyncClosure};
use js_sys::ArrayBuffer;

pub mod prelude;
mod resolver;
use resolver::Resolver;

use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use std::rc::Rc;
use std::cell::RefCell;

type ResultJJ = Result<JsValue, JsValue>;

pub trait PoolCallback = FnOnce(ResultJJ,) -> () + 'static;

struct ThreadPoolInner {
    size: usize,
    mt: WasmMt,
    threads: RefCell<Vec<Thread>>,
    resolver: Resolver,
}

impl ThreadPoolInner {
    fn new(size: usize, pkg_js_uri: &str) -> Self {
        assert!(size > 0);
        Self {
            size,
            mt: WasmMt::new(pkg_js_uri),
            threads: RefCell::new(Vec::with_capacity(size)),
            resolver: Resolver::new(),
        }
    }

    fn new_with_arraybuffers(size: usize, ab_js: ArrayBuffer, ab_wasm: ArrayBuffer) -> Self {
        assert!(size > 0);
        Self {
            size,
            mt: WasmMt::new_with_arraybuffers(ab_js, ab_wasm),
            threads: RefCell::new(Vec::with_capacity(size)),
            resolver: Resolver::new(),
        }
    }

    async fn init(&self) -> Result<(), JsValue> {
        let mut threads = self.threads.borrow_mut();

        self.mt.init().await?;
        for id in 0..self.size {
            let pth = self.mt.thread();
            pth.set_id(&id.to_string());
            threads.push(pth);
        }

        for pth in threads.iter() {
            pth.init().await?;
        }

        Ok(())
    }

    async fn execute<F>(&self, clos: F) -> ResultJJ where F: MtClosure {
        let threads = self.threads.borrow();
        let pth = self.resolver.resolve_runnable(&threads).await?;

        let result = pth.exec(clos).await;
        debug_ln!("pth {} done with result: {:?}", pth.get_id().unwrap(), result);
        self.resolver.notify_job_complete(pth);
        result
    }

    async fn execute_async<F, T>(&self, aclos: F) -> ResultJJ where F: MtAsyncClosure<T> {
        let threads = self.threads.borrow();
        let pth = self.resolver.resolve_runnable(&threads).await?;

        let result = pth.exec_async(aclos).await;
        debug_ln!("pth {} done with result: {:?}", pth.get_id().unwrap(), result);
        self.resolver.notify_job_complete(pth);
        result
    }

    async fn execute_js(&self, js: &str, is_async: bool) -> ResultJJ {
        let threads = self.threads.borrow();
        let pth = self.resolver.resolve_runnable(&threads).await?;

        let result = if is_async {
            pth.exec_js_async(js).await
        } else {
            pth.exec_js(js).await
        };
        debug_ln!("pth {} done with result: {:?}", pth.get_id().unwrap(), result);
        self.resolver.notify_job_complete(pth);
        result
    }

    fn drop_inner(&self) {
        debug_ln!("[drop] drop_inner(): terminating {} workers ...", self.size);
        self.resolver.cancel_pending_jobs();
        self.threads.borrow().iter().for_each(|pth| pth.terminate());
    }
}

#[macro_export]
macro_rules! pool_exec {
    ($pool:expr, async $clos:expr) => (($pool).exec_async(FnOnce!(async $clos)));
    ($pool:expr, $clos:expr) => (($pool).exec(FnOnce!($clos)));
    ($pool:expr, async $clos:expr, $cb:expr) => (($pool).exec_async_with_cb(FnOnce!(async $clos), $cb));
    ($pool:expr, $clos:expr, $cb:expr) => (($pool).exec_with_cb(FnOnce!($clos), $cb));
}

#[macro_export]
macro_rules! pool_exec_js {
    ($pool:expr, $str:expr) => (($pool).exec_js($str));
    ($pool:expr, $str:expr, $cb:expr) => (($pool).exec_js_with_cb($str, $cb));
}

#[macro_export]
macro_rules! pool_exec_js_async {
    ($pool:expr, $str:expr) => (($pool).exec_js_async($str));
    ($pool:expr, $str:expr, $cb:expr) => (($pool).exec_js_async_with_cb($str, $cb));
}


pub struct ThreadPool(Rc<ThreadPoolInner>);

impl Drop for ThreadPool {
    fn drop(&mut self) {
        debug_ln!("[drop] ThreadPool::drop(): sc: {}", Rc::strong_count(&self.0));
        self.0.drop_inner();
    }
}

impl ThreadPool {
    pub fn new(size: usize, pkg_js_uri: &str) -> Self {
        Self(Rc::new(ThreadPoolInner::new(size, pkg_js_uri)))
    }

    pub fn new_with_arraybuffers(size: usize, ab_js: ArrayBuffer, ab_wasm: ArrayBuffer) -> Self {
        Self(Rc::new(ThreadPoolInner::new_with_arraybuffers(size, ab_js, ab_wasm)))
    }

    pub fn set_ab_init(&self, ab: ArrayBuffer) {
        self.0.mt.set_ab_init(ab);
    }

    pub async fn init(&self) -> Result<&Self, JsValue> {
        self.0.init().await?;
        Ok(self)
    }

    pub async fn and_init(self) -> Result<Self, JsValue> {
        self.init().await?;
        Ok(self)
    }

    pub fn count_pending_jobs(&self) -> usize {
        self.0.resolver.count_pending_jobs()
    }

    fn drop_cb_result(_: ResultJJ) {}

    pub fn exec<F>(&self, job: F) where F: MtClosure {
        self.exec_with_cb(job, Self::drop_cb_result);
    }
    pub fn exec_async<F, T>(&self, job: F) where F: MtAsyncClosure<T> {
        self.exec_async_with_cb(job, Self::drop_cb_result);
    }
    pub fn exec_with_cb<F, G>(&self, job: F, cb: G) where
    F: MtClosure, G: PoolCallback {
        let pool_inner = self.0.clone();
        spawn_local(async move {
            cb(pool_inner.execute(job).await);
        });
    }
    pub fn exec_async_with_cb<F, T, G>(&self, job: F, cb: G) where
    F: MtAsyncClosure<T>, G: PoolCallback {
        let pool_inner = self.0.clone();
        spawn_local(async move {
            cb(pool_inner.execute_async(job).await);
        });
    }

    pub fn exec_js(&self, js: &str) {
        self.exec_js_inner(js, false, Self::drop_cb_result);
    }
    pub fn exec_js_async(&self, js: &str) {
        self.exec_js_inner(js, true, Self::drop_cb_result);
    }
    pub fn exec_js_with_cb<G>(&self, js: &str, cb: G) where G: PoolCallback {
        self.exec_js_inner(js, false, cb);
    }
    pub fn exec_js_async_with_cb<G>(&self, js: &str, cb: G) where G: PoolCallback {
        self.exec_js_inner(js, true, cb);
    }
    fn exec_js_inner<G>(&self, js: &str, is_async: bool, cb: G) where G: PoolCallback {
        let pool_inner = self.0.clone();
        let js = js.to_string();
        spawn_local(async move {
            cb(pool_inner.execute_js(js.as_str(), is_async).await);
        });
    }
}