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
#![warn(missing_docs)]

use std::sync::{Arc, Condvar, Mutex};
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
use std::{process, thread};
use std::collections::{HashMap, VecDeque};

type BoxedJob = Box<Runnable + Send + 'static>;

/// A trait for giving a type an ability to run some code.
pub trait Runnable {
    /// Runs some code.
    fn run(self: Box<Self>);
}

impl<F: FnOnce()> Runnable for F {
    #[inline]
    fn run(self: Box<F>) {
        (*self)()
    }
}

struct Worker {
    handle: Option<thread::JoinHandle<()>>,
}

impl Worker {
    fn new(
        id: usize,
        id_counter: Arc<AtomicUsize>,
        job_queue: Arc<Mutex<VecDeque<BoxedJob>>>,
        condvar: Arc<Condvar>,
        workers: Arc<Mutex<HashMap<usize, Worker>>>,
        removed_handles: Arc<Mutex<Vec<Option<thread::JoinHandle<()>>>>>,
        busy_workers_count: Arc<AtomicUsize>,
        min_size: Arc<usize>,
        max_size: Arc<AtomicUsize>,
        shutdown: Arc<AtomicBool>,
    ) -> Self {
        let builder = thread::Builder::new().name(format!("worker-{}", id));
        let handle = builder.spawn(move || loop {
            let (job, remaining_job_count) = {
                let mut guard = job_queue.lock().unwrap();
                while guard.is_empty() && !shutdown.load(Ordering::SeqCst) {
                    // println!("[worker-{}] waiting...", id);
                    guard = condvar.wait(guard).unwrap();
                    // println!("[worker-{}] notified", id);
                }

                // println!("[worker-{}] got new new job", id);

                // queue is not empty at this point, so unwrap() is safe
                (guard.pop_front(), guard.len())
            };

            if job.is_none() {
                break;
            }

            let job = job.unwrap();

            busy_workers_count.fetch_add(1, Ordering::SeqCst);

            let max_size_prime = max_size.load(Ordering::SeqCst);
            if max_size_prime != 0 {
                let mut guard = workers.lock().unwrap();
                // println!("remaining job count: {}", remaining_job_count);
                if remaining_job_count > guard.len() {
                    let busy_workers = busy_workers_count.load(Ordering::SeqCst);
                    if guard.len() < max_size_prime && busy_workers >= *min_size && !shutdown.load(Ordering::SeqCst) {
                        let new_id = id_counter.fetch_add(1, Ordering::SeqCst);
                        // println!("inserting new one: {}", new_id);
                        guard.insert(
                            new_id,
                            Worker::new(
                                new_id,
                                id_counter.clone(),
                                job_queue.clone(),
                                condvar.clone(),
                                workers.clone(),
                                removed_handles.clone(),
                                busy_workers_count.clone(),
                                min_size.clone(),
                                max_size.clone(),
                                shutdown.clone(),
                            ),
                        );
                        // println!("new workers size: {}", guard.len());
                    }
                }
                // drop(guard); // commented out just for the reminder...
            }

            // println!("[worker-{}] running job", id);
            job.run();

            busy_workers_count.fetch_sub(1, Ordering::SeqCst);

            let mut guard = workers.lock().unwrap();
            if guard.len() > *min_size && busy_workers_count.load(Ordering::SeqCst) < *min_size {
                let worker = guard.remove(&id);
                drop(guard);

                if let Some(worker) = worker {
                    if let Some(handle) = worker.handle {
                        let mut guard = removed_handles.lock().unwrap();
                        guard.push(Some(handle));
                    }
                }

                // println!("[worker-{}] done working and REMOVED", id);
                break;
            }
        });

        match handle {
            Ok(h) => {
                return Self {
                    handle: Some(h),
                };
            }
            Err(e) => {
                eprintln!("Error: {}", e);
                process::exit(1);
            }
        }
    }
}

/// JobPool manages a job queue to be run on a specified number of threads.
pub struct JobPool {
    size: Arc<usize>,
    max_size: Arc<AtomicUsize>,
    busy_workers_count: Arc<AtomicUsize>,
    workers: Arc<Mutex<HashMap<usize, Worker>>>,
    removed_handles: Arc<Mutex<Vec<Option<thread::JoinHandle<()>>>>>,
    job_queue: Arc<Mutex<VecDeque<BoxedJob>>>,
    condvar: Arc<Condvar>,
    shutdown: Arc<AtomicBool>,
}

impl JobPool {
    ///
    /// Creates a new job pool.
    ///
    /// Using the number of cpu cores as the argument for `size` is recommended.
    /// Higher values can result iSeqCstn larger memory footprint,
    /// and non-optimal performance.
    ///
    /// # Panics
    ///
    /// This function will panic if the argument for `size` is 0.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use jobpool::JobPool;
    ///
    /// let pool_size: usize = 8; // number of cpu cores is recommended
    /// let mut pool = JobPool::new(pool_size);
    /// pool.queue(|| {
    ///     // do some work
    /// });
    /// // ...
    /// pool.shutdown(); // blocks until all jobs are done
    /// ```
    pub fn new(size: usize) -> Self {
        if size == 0 {
            panic!("size cannot be 0")
        }

        let job_queue = Arc::new(Mutex::new(VecDeque::new()));
        let condvar = Arc::new(Condvar::new());
        let max_size = Arc::new(AtomicUsize::new(0));
        let worker_id_counter = Arc::new(AtomicUsize::new(size));
        let busy_workers_count = Arc::new(AtomicUsize::new(0));
        let shutdown = Arc::new(AtomicBool::new(false));
        let size = Arc::new(size);
        let removed_handles = Arc::new(Mutex::new(Vec::new()));

        let workers = {
            let workers = Arc::new(Mutex::new(HashMap::new()));
            let mut guard = workers.lock().unwrap();
            for id in 0..*size {
                guard.insert(
                    id,
                    Worker::new(
                        id,
                        worker_id_counter.clone(),
                        job_queue.clone(),
                        condvar.clone(),
                        workers.clone(),
                        removed_handles.clone(),
                        busy_workers_count.clone(),
                        size.clone(),
                        max_size.clone(),
                        shutdown.clone(),
                    ),
                );
            }
            workers.clone()
        };

        Self {
            size,
            max_size,
            busy_workers_count,
            workers,
            removed_handles,
            job_queue,
            condvar,
            shutdown,
        }
    }

    /// Queues a new "job".
    ///
    /// A "job" can be a closure with no arguments and returns, or
    /// a type with `Runnable` trait. A queued job gets run in a first-come, first-serve basis.
    ///
    /// # Panics
    ///
    /// This method will panic if the JobPool instance has already been shutdown.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use jobpool::JobPool;
    ///
    /// let pool_size: usize = 8; // number of cpu cores is recommended
    /// let mut pool = JobPool::new(pool_size);
    /// pool.queue(|| {
    ///     // do some work
    /// });
    /// // ...
    /// pool.shutdown(); // blocks until all jobs are done
    /// ```
    pub fn queue<J>(&mut self, job: J)
    where
        J: Runnable + Send + 'static,
    {
        if self.shutdown.load(Ordering::SeqCst) {
            panic!("Error: this threadpool has been shutdown!");
        } else {
            self.push(Box::new(job));
        }
    }

    /// Automatically increase the number of worker threads as needed until `max_size` is reached.
    ///
    /// # Panics
    ///
    /// This method will panic if `max_size` is less than or equal to initial JobPool size.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use jobpool::JobPool;
    ///
    /// let pool_size: usize = 8; // number of cpu cores is recommended
    /// let mut pool = JobPool::new(pool_size);
    /// pool.auto_grow(100);
    /// for _ in 0..1000 {
    ///     pool.queue(|| {
    ///         // do some work
    ///     });
    /// }
    /// // ...
    /// pool.shutdown(); // blocks until all jobs are done
    /// ```
    pub fn auto_grow(&mut self, max_size: usize) {
        if max_size <= *self.size {
            panic!("max_size must be greater than initial JobPool size");
        }
        self.max_size.store(max_size, Ordering::SeqCst);
    }

    /// Get the number of current active worker threads.
    pub fn active_workers_count(&self) -> usize {
        return self.busy_workers_count.load(Ordering::SeqCst);
    }

    fn push(&mut self, job: BoxedJob) {
        let mut guard = self.job_queue.lock().unwrap();
        guard.push_back(job);
        self.condvar.notify_one();
    }

    /// Shuts down this instance of JobPool.
    ///
    /// This method will wait for all of the queued jobs to finish.
    /// It also gets called automatically as the instance goes out of scope,
    /// so calling this method can be optional.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use jobpool::JobPool;
    ///
    /// let pool_size: usize = 8; // number of cpu cores is recommended
    /// let mut pool = JobPool::new(pool_size);
    /// pool.queue(|| {
    ///     // do some work
    /// });
    /// // ...
    /// pool.shutdown(); // blocks until all jobs are done
    /// ```
    pub fn shutdown(&mut self) {
        if !self.can_shutdown() {
            return;
        }

        let mut handles = Vec::new();

        let mut guard = self.workers.lock().unwrap();
        handles.reserve(guard.len());

        for (_, worker) in &mut *guard {
            if let Some(handle) = worker.handle.take() {
                // println!("[{}] shutting down", handle.thread().name().unwrap());
                handles.push(handle);
            }
        }
        drop(guard);

        let mut guard = self.removed_handles.lock().unwrap();
        for handle in &mut *guard {
            handles.push(handle.take().unwrap());
        }
        drop(guard);

        for handle in handles {
            let _ = handle.join();
        }
    }

    /// Shuts down this instance of JobPool without waiting for threads to finish.
    ///
    /// This method will return all of the threads' `JoinHandle`s.
    /// It won't wait for the threads to finish, and it must be called explicitly.
    /// Calling `shutdown()` after this method won't have any effect.
    /// Unlike `shutdown()`, it doesn't get called automatically after going out of scope.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use jobpool::JobPool;
    ///
    /// let pool_size: usize = 8; // number of cpu cores is recommended
    /// let mut pool = JobPool::new(pool_size);
    /// pool.queue(|| {
    ///     // do some work
    /// });
    /// // ...
    /// let handles = pool.shutdown_no_wait();
    /// // ...
    /// if let Some(handles) = handles {
    ///     for handle in handles {
    ///         let _ = handle.join();
    ///     }
    /// }
    /// ```
    pub fn shutdown_no_wait(&mut self) -> Option<Vec<thread::JoinHandle<()>>> {
        if !self.can_shutdown() {
            return None;
        }

        let mut handles = Vec::new();

        let mut guard = self.workers.lock().unwrap();
        handles.reserve(guard.len());

        for (_, worker) in &mut *guard {
            if let Some(handle) = worker.handle.take() {
                // println!("[{}] shutting down", handle.thread().name().unwrap());
                handles.push(handle);
            }
        }
        drop(guard);

        let mut guard = self.removed_handles.lock().unwrap();
        for handle in &mut *guard {
            handles.push(handle.take().unwrap());
        }
        drop(guard);

        Some(handles)
    }

    fn can_shutdown(&mut self) -> bool {
        if self.shutdown.load(Ordering::SeqCst) {
            return false;
        }

        self.shutdown.store(true, Ordering::SeqCst);
        self.condvar.notify_all();

        return true;
    }
}

impl Drop for JobPool {
    fn drop(&mut self) {
        self.shutdown();
    }
}