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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*!
# [poolite](https://github.com/biluohc/poolite)

A lite threadpool library written for Rust.

## Usage

On Cargo.toml:

```toml
 [dependencies]
 poolite = "0.7.0"
```

## Documentation  
* Visit [Docs.rs](https://docs.rs/poolite/)  

or 

* Run `cargo doc --open` after modified the toml file.

## Base usage
```
extern crate poolite;
use poolite::Pool;

fn main() {
    let pool = Pool::new().run().unwrap();
    for i in 0..38 {
        pool.push(move || test(i));
    }

    pool.join(); //wait for the pool
}

fn test(msg: i32) {
    println!("key: {}\tvalue: {}", msg, fib(msg));
}

fn fib(msg: i32) -> i32 {
    match msg {
        0...2 => 1,
        x => fib(x - 1) + fib(x - 2),
    }
}
```

## `Scoped` task
```
extern crate poolite;
use poolite::Pool;

fn main() {
    let pool = Pool::new().run().unwrap();
    let mut array = (0..100usize).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();

    // scoped method will waiting scoped's task running finish.
    pool.scoped(|scope| for i in array.iter_mut() {
        // have to move
        scope.push(move|| i.1 = i.0*i.0);
    });

    for (i, j) in array {
        println!("key: {}\tvalue: {}", i, j);
    }
}
```

## [More Examples](https://github.com/biluohc/poolite/blob/master/examples/)
*/
#[macro_use]
extern crate log;
extern crate mxo_env_logger;
extern crate crossbeam_channel;
extern crate num_cpus;

use crossbeam_channel::{unbounded, Sender, Receiver, RecvTimeoutError};
use mxo_env_logger::{init, LogErr};

use std::sync::atomic::{Ordering, AtomicUsize, AtomicBool};
use std::sync::{Arc, Once, ONCE_INIT};
use std::fmt::{self, Debug, Display};
use std::time::Duration;
use std::mem::transmute;
use std::marker::PhantomData;
use std::error::Error;
use std::thread;
use std::io;
unsafe impl Send for Pool {}
unsafe impl Sync for Pool {}

/// The `Pool` struct
#[derive(Debug)]
pub struct Pool {
    inner: Inner,
}

impl Pool {
    pub fn new() -> Self {
        Self::with_builder(Builder::default())
    }
    pub fn with_builder(b: Builder) -> Self {
        assert!(b.max >= b.min, "min > max");
        assert!(b.max != 0, "max == 0");

        Self { inner: Inner::with_builder(b) }
    }
    /// Get `Pool`'s settings
    pub fn as_builder(&self) -> &Builder {
        self.inner.as_builder()
    }
    pub fn run(mut self) -> Result<Self, PoolError> {
        let _ = init();
        match (&mut self).inner.run() {
            Ok(_) => Ok(self),
            Err(e) => Err(PoolError::new(self, e)),
        }
    }
    /// All threads are waiting and tasks_queue'length is 0.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
    /// Returns the length of the tasks_queue.
    pub fn tasks_len(&self) -> usize {
        self.inner.tasks_len()
    }
    // #[doc(hidden)]
    /// Contains the number of ready to create
    pub fn threads_future(&self) -> usize {
        self.inner.threads_future()
    }
    /// Returns the number of threads in the Pool.
    pub fn threads_alive(&self) -> usize {
        self.inner.threads_alive()
    }
    /// Returns the number of threads that is waiting for Task in the Pool
    pub fn threads_waiting(&self) -> usize {
        self.inner.threads_waiting()
    }
    /// The daemon thread's status
    pub fn daemon_alive(&self) -> bool {
        self.inner.daemon_alive()
    }
    #[doc(hidden)]
    pub fn dropped(&self) -> bool {
        self.inner.dropped()
    }
    /// Appends a task to the Pool,
    ///
    /// it receives `Fn() + Send + 'static,FnMut() + Send + 'static` and `FnOnce() + Send + 'static>`.
    pub fn push<T>(&self, task: T)
    where
        T: Runable + Send + 'static,
    {
        self.inner.push(
            Box::new(task) as Box<Runable + Send + 'static>,
        )
    }
    /// Manually add the number of threads to `Pool`
    pub fn add_threads(&self, add_num: usize) -> Result<(), (usize, io::Error)> {
        self.inner.add_threads(add_num)
    }
    ///wait for the pool
    pub fn join(&self) {
        self.join_ms(10);
    }
    #[doc(hidden)]
    pub fn join_ms(&self, ms: u64) {
        while !self.is_empty() {
            thread::sleep(Duration::from_millis(ms)); //wait for the pool time(ms).
        }
    }
}

impl Default for Pool {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for Pool {
    fn drop(&mut self) {
        self.inner.as_builder().dropped.store(
            true,
            Ordering::SeqCst,
        )
    }
}

include!("inner.rs");
include!("scope.rs");

/// The error type for the pool's `run()` if the pool spawning the daemon thread fails.
#[derive(Debug)]
pub struct PoolError {
    pool: Pool,
    error: std::io::Error,
}

impl PoolError {
    #[inline]
    fn new(pool: Pool, error: std::io::Error) -> Self {
        PoolError {
            pool: pool,
            error: error,
        }
    }
    ///  Into `Pool`
    #[inline]
    pub fn into_inner(self) -> Pool {
        self.pool
    }
    /// Into `std::io::Error`
    #[inline]
    pub fn into_error(self) -> std::io::Error {
        self.error
    }
}

impl Error for PoolError {
    fn description(&self) -> &str {
        self.error.description()
    }
}

impl Display for PoolError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;
        write!(
            f,
            "PoolError {{ pool : Pool, err : {} }}",
            self.error.description()
        )
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::{Arc, Mutex};
    use std::time::Duration;
    use std::thread;
    #[should_panic]
    #[test]
    fn min_bq_max() {
        let _pool = Builder::new().min(100).max(99).run();
    }
    #[should_panic]
    #[test]
    fn max_zero() {
        let _pool = Builder::new().max(0).run();
    }
    #[test]
    fn min_eq_max() {
        let _pool = Builder::new().min(100).max(100).run();
    }
    #[test]
    fn min_max() {
        let min0 = Builder::min_default();
        let max0 = Builder::max_default();

        let p0 = Builder::new().max(min0 - 1);
        assert_eq!(min0 - 1, p0.max);
        assert_eq!(min0 - 1, p0.min);

        let p1 = Builder::new().min(max0 + 1);
        assert_eq!(p1.min, max0 + 1);
        assert_eq!(p1.max, max0 + 1);

        let p2 = Builder::new().min(max0).max(min0);
        assert_eq!(p2.min, max0);
        assert_eq!(p2.max, min0);
    }
    #[test]
    fn fn_fnmut_fnonce_closure() {
        fn fnn() {
            println!("call Fn() push");
        }

        fn fnm(msg: &mut String) {
            println!("{}", msg);
            *msg = "call FnMut() return".to_owned()
        }

        fn fno(msg: String) {
            println!("{}", msg);
        }
        let mut str = std::env::args().nth(0).unwrap();
        let str1 = std::env::args().nth(0).unwrap();
        let str2 = std::env::args().nth(0).unwrap();

        let pool = Pool::new().run().unwrap();
        pool.push(fnn);
        pool.push(move || fnm(&mut str));
        pool.push(move || fno(str1));

        let closure = move || for _ in 0..str2.len() {
            if std::env::args().count() > 100_0000 {
                println!("Fake");
            }
        };
        pool.push(closure);
        pool.join()
    }

    #[test]
    fn pool() {
        let pool = Pool::new();
        assert!(Builder::num_cpus() >= 1);
        assert_eq!(pool.threads_alive(), 0);
        assert_eq!(pool.threads_waiting(), 0);
        assert_eq!(*pool.as_builder().min_get(), Builder::num_cpus() + 1);
        assert_eq!(
            *pool.as_builder().max_get(),
            (Builder::num_cpus() + 1) * Builder::num_cpus()
        );
        assert_eq!(
            pool.as_builder().timeout_get(),
            Some(&Duration::from_millis(TIME_OUT_MS))
        );
        assert!(pool.as_builder().name_get().is_none());
        assert!(pool.as_builder().stack_size_get().is_none());
        assert_eq!(
            *pool.as_builder().load_limit_get(),
            Builder::num_cpus() * Builder::num_cpus()
        );

        assert_eq!(
            pool.as_builder().daemon_get(),
            Some(&Duration::from_millis(TIME_OUT_MS))
        );
        assert!(!pool.daemon_alive()); // not run, so
        assert!(!pool.dropped());

        let pool = pool.run().unwrap();
        let array = (0..33usize).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();

        let map = Arc::new(Mutex::new(array));
        for i in 0..33 {
            let mutex = map.clone();
            pool.push(move || test(i, mutex));
        }

        while !pool.is_empty() {
            thread::sleep(Duration::from_millis(10)); //wait for the pool 10ms.
            eprint!(
                "len()/min()/max(): {}/{}/{}",
                pool.threads_alive(),
                pool.as_builder().min_get(),
                pool.as_builder().max_get()
            );
        }

        for &(k, v) in map.lock().unwrap().iter() {
            println!("key: {}\tvalue: {}", k, v);
        }

        assert!(pool.threads_alive() > 0);
        assert!(pool.threads_waiting() > 0);
        assert_eq!(*pool.as_builder().min_get(), Builder::num_cpus() + 1);
        assert_eq!(
            *pool.as_builder().max_get(),
            (Builder::num_cpus() + 1) * Builder::num_cpus()
        );
        assert_eq!(
            pool.as_builder().timeout_get(),
            Some(&Duration::from_millis(TIME_OUT_MS))
        );
        assert!(pool.as_builder().name_get().is_none());
        assert!(pool.as_builder().stack_size_get().is_none());
        assert_eq!(
            *pool.as_builder().load_limit_get(),
            Builder::num_cpus() * Builder::num_cpus()
        );

        assert_eq!(
            pool.as_builder().daemon_get(),
            Some(&Duration::from_millis(TIME_OUT_MS))
        );
        assert!(pool.daemon_alive());
        assert!(!pool.dropped());
        println!("{:?}", pool);
    }

    fn test(msg: usize, map: Arc<Mutex<Vec<(usize,usize)>>>) {
        let res = fib(msg);
        let mut maplock = map.lock().unwrap();
        maplock[msg as usize].1 = res;
    }
    fn fib(msg: usize) -> usize {
        match msg {
            0...2 => 1,
            x => fib(x - 1) + fib(x - 2),
        }
    }
    #[test]
    fn scope_fib() {
        let pool = Pool::new().run().unwrap();
        let mut array = (0..33usize).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();

        let mutex = Arc::new(Mutex::new(array.clone()));
        for i in 0..33usize {
            let mutex = mutex.clone();
            pool.push(move || test(i, mutex));
        }

        pool.scoped(|scope| for i in array.iter_mut() {
            scope.push(move|| i.1 = fib(i.0));
        });

        pool.join();
        let array_true = mutex.lock().unwrap();
        assert_eq!(*array_true,array);
        for (i, j) in array {
            println!("key: {}\tvalue: {}", i, j);
        }
    }
    #[test]
    fn scope_x2() {
        let pool = Pool::new().run().unwrap();
        let mut array = (0..100usize).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();

        let mutex = Arc::new(Mutex::new(array.clone()));
        for i in 0..100 {
            let mutex = mutex.clone();
            pool.push(move || x2(i, mutex));
        }

        pool.scoped(|scope| for i in array.iter_mut() {
            scope.push(move|| i.1 = i.0*i.0);
        });

        pool.join();
        let array_true = mutex.lock().unwrap();
        assert_eq!(*array_true,array);
        for (i, j) in array {
            println!("key: {}\tvalue: {}", i, j);
        }
    }
    fn x2(msg: usize, map: Arc<Mutex<Vec<(usize,usize)>>>) {
        let res = msg*msg;
        let mut maplock = map.lock().unwrap();
        maplock[msg].1 = res;
    }
}