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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#![forbid(unsafe_code)]

//! ThreadPools.
//! Threadpool provides a way to manage and execute tasks concurrently using a fixed number of worker threads. 
//! It allows you to submit tasks that will be executed by one of the available worker threads, 
//! providing an efficient way to parallelize work across multiple threads.
//!
//! Maintaining a pool of threads over creating a new thread for each task has the benefit that 
//! thread creation and destruction overhead is restricted to the initial creation of the pool, 
//! which may result in better performance.
//! 
//! # Examples
//! 
//! ```
//! use base_threadpool::{ThreadPool, ThreadPoolBuilder};
//! use std::sync::{Arc, Mutex};
//!
//! let thread_pool = ThreadPoolBuilder::default().build();
//! let value = Arc::new(Mutex::new(0));
//!
//! (0..4).for_each(move |_| {
//!     let value = Arc::clone(&value);
//!     thread_pool.execute(move || {
//!         let mut ir = 0;
//!         (0..100_000_000).for_each(|_| {
//!             ir += 1;
//!         });
//!
//!         let mut lock = value.lock().unwrap();
//!         *lock += ir;
//!     });
//! });
//! ```
use std::{
    num::NonZero,
    sync::{mpsc, Arc, Mutex},
    thread::{self, JoinHandle},
};

/// `ThreadPool` provides a way to manage and execute tasks concurrently using a fixed number of worker threads.
///
/// It allows you to submit tasks that will be executed by one of the available worker threads,
/// providing an efficient way to parallelize work across multiple threads.
#[derive(Debug)]
pub struct ThreadPool {
    workers: Vec<ThreadWorker>,
    producer: Option<mpsc::Sender<ThreadJob>>,
}

impl ThreadPool {
    /// Discovery method for [`ThreadPoolBuilder`].
    ///
    /// Returns a default [`ThreadPoolBuilder`] for constructing a new [`ThreadPool`].
    ///
    /// This method provides a convenient way to start building a `ThreadPool` with default settings,
    /// which can then be customized as needed.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPool;
    ///
    /// let pool = ThreadPool::builder().build();
    /// ```
    pub fn builder() -> ThreadPoolBuilder {
        ThreadPoolBuilder::default()
    }

    /// Schedules a task to be executed by the thread pool.
    ///
    /// This method takes a closure and schedules it to be run on one of the worker threads.
    ///
    ///
    /// # Panics
    ///
    /// Panics if the thread pool has been shut down or if there's an error sending the job.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPool;
    /// use std::sync::{Arc, Mutex};
    ///
    /// // Create a thread pool with 4 worker threads
    /// let pool = ThreadPool::builder().num_threads(4).build();
    ///
    /// // Create a list of items to process
    /// let items = vec!["apple", "banana", "cherry", "date", "elderberry"];
    /// let processed_items = Arc::new(Mutex::new(Vec::new()));
    ///
    /// // Process each item concurrently
    /// for item in items {
    ///     let processed_items = Arc::clone(&processed_items);
    ///     pool.execute(move || {
    ///         // Simulate some processing time
    ///         std::thread::sleep(std::time::Duration::from_millis(100));
    ///
    ///         // Process the item (in this case, convert to uppercase)
    ///         let processed = item.to_uppercase();
    ///
    ///         // Store the processed item
    ///         processed_items.lock().unwrap().push(processed);
    ///     });
    /// }
    /// ```
    #[inline]
    pub fn execute<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = Box::new(f);

        self.producer
            .as_ref()
            .unwrap()
            .send(ThreadJob::Run(job))
            .unwrap();
    }

    /// Waits for all worker threads in the pool to finish their current tasks and then shuts down the pool.
    ///
    /// This function will block until all workers have completed.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPool;
    /// use std::sync::{
    ///     atomic::{AtomicU16, Ordering},
    ///     Arc,
    /// };
    ///
    /// let mut pool = ThreadPool::builder().build();
    /// let counter = Arc::new(AtomicU16::new(0));
    ///
    /// (0..100).for_each(|_| {
    ///     let counter = Arc::clone(&counter);
    ///     pool.execute(move || {
    ///         let _ = counter.fetch_add(1, Ordering::SeqCst);
    ///     });
    /// });
    ///
    /// pool.join();
    /// assert_eq!(counter.load(Ordering::SeqCst), 100);
    /// ```
    pub fn join(&mut self) {
        (0..self.workers.len()).for_each(|_| {
            self.producer
                .as_ref()
                .unwrap()
                .send(ThreadJob::Stop)
                .unwrap();
        });

        // make sure that the channel gets closed once the thread pool is disposed
        drop(self.producer.take());

        self.workers.iter_mut().for_each(|worker| {
            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        });
    }

    /// This method provides information about the level of concurrency available in the thread pool.
    ///
    /// Returns the number of worker threads in the pool.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPool;
    ///
    /// let pool = ThreadPool::builder().num_threads(4).build();
    /// assert_eq!(pool.num_threads(), 4);
    /// ```
    #[doc(alias = "available_parallelism")]
    #[doc(alias = "available_concurrency")]
    #[doc(alias = "available_workers")]
    #[doc(alias = "available_threads")]
    pub fn num_threads(&self) -> usize {
        self.workers.len()
    }
}

impl Drop for ThreadPool {
    fn drop(&mut self) {
        if self.producer.is_some() {
            // finish up the work that has already been picked up
            self.join();
        }
    }
}

/// A builder for configuring and creating a [`ThreadPool`].
///
/// This builder allows you to set various parameters for the thread pool,
/// such as the number of threads, stack size, and a name prefix for the threads.
///
/// # Examples
///
/// Creating a thread pool with default settings:
///
/// ```
/// use base_threadpool::ThreadPoolBuilder;
///
/// let pool = ThreadPoolBuilder::default().build();
/// ```
///
/// Creating a customized thread pool:
///
/// ```
/// use base_threadpool::ThreadPoolBuilder;
///
/// let pool = ThreadPoolBuilder::default()
///     .num_threads(4)
///     .stack_size(3 * 1024 * 1024)
///     .name_prefix("worker".to_string())
///     .build();
/// ```
#[derive(Debug)]
pub struct ThreadPoolBuilder {
    num_threads: NonZero<usize>,
    stack_size: Option<usize>,
    name_prefix: Option<String>,
}

/// Default parameters for [`ThreadPoolBuilder`]
/// The Default number of threads available for the [`ThreadPool`] is [`std::thread::available_parallelism`].
impl Default for ThreadPoolBuilder {
    fn default() -> ThreadPoolBuilder {
        ThreadPoolBuilder {
            num_threads: thread::available_parallelism().unwrap(),
            stack_size: Option::default(),
            name_prefix: Option::default(),
        }
    }
}

impl ThreadPoolBuilder {
    /// Constructs a new instance of [`ThreadPoolBuilder`] with specified parameters.
    ///
    /// # Arguments
    ///
    /// * `num_threads` - The number of threads in the pool. Must be greater than 0.
    /// * `stack_size` - The stack size for each thread in bytes.
    /// * `name_prefix` - A prefix for naming the threads in the pool.
    ///
    /// # Panics
    ///
    /// Panics if `num_threads` is 0.
    // # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPoolBuilder;
    ///
    /// let builder = ThreadPoolBuilder::new(4, 2 * 1024 * 1024, "custom-worker".to_string());
    /// let pool = builder.build();
    /// ```
    pub fn new(num_threads: usize, stack_size: usize, name_prefix: String) -> ThreadPoolBuilder {
        assert!(num_threads > 0);

        ThreadPoolBuilder {
            num_threads: NonZero::new(num_threads).unwrap(),
            stack_size: Some(stack_size),
            name_prefix: Some(name_prefix),
        }
    }

    /// Builds and returns a new [`ThreadPool`] instance based on the current configuration.
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPoolBuilder;
    ///
    /// let pool = ThreadPoolBuilder::default().num_threads(2).build();
    /// ```
    pub fn build(&self) -> ThreadPool {
        let (producer, consumer) = mpsc::channel();
        let consumer = Arc::new(Mutex::new(consumer));

        let mut workers = Vec::with_capacity(self.num_threads.into());
        (0..self.num_threads.into()).for_each(|id| {
            let consumer = Arc::clone(&consumer);
            let mut builder = thread::Builder::new();

            if let Some(stack_size) = self.stack_size {
                builder = builder.stack_size(stack_size);
            }

            if let Some(prefix) = &self.name_prefix {
                builder = builder.name(format!("{}-{}", prefix, id));
            }

            let worker = ThreadWorker::new(id, consumer, builder);
            workers.push(worker);
        });

        ThreadPool {
            workers,
            producer: Some(producer),
        }
    }

    /// Sets the number of threads for the thread pool.
    ///
    /// # Panics
    ///
    /// Panics if `num_threads` is 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPoolBuilder;
    ///
    /// let builder = ThreadPoolBuilder::default().num_threads(8);
    /// ```
    pub fn num_threads(mut self, num_threads: usize) -> ThreadPoolBuilder {
        assert!(num_threads > 0);

        self.num_threads = NonZero::new(num_threads).unwrap();
        self
    }

    /// Sets the stack size, in bytes, for each thread in the pool.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPoolBuilder;
    ///
    /// let builder = ThreadPoolBuilder::default().stack_size(4 * 1024 * 1024);
    /// ```
    pub fn stack_size(mut self, stack_size: usize) -> ThreadPoolBuilder {
        self.stack_size = Some(stack_size);
        self
    }

    /// Sets the name prefix for threads in the pool.
    ///
    /// # Examples
    ///
    /// ```
    /// use base_threadpool::ThreadPoolBuilder;
    ///
    /// let builder = ThreadPoolBuilder::default().name_prefix("my-worker".to_string());
    /// ```
    pub fn name_prefix(mut self, name_prefix: String) -> ThreadPoolBuilder {
        self.name_prefix = Some(name_prefix);
        self
    }
}

enum ThreadJob {
    Stop,
    Run(Box<dyn FnOnce() + Send + 'static>),
}

#[derive(Debug)]
struct ThreadWorker {
    id: usize,
    thread: Option<JoinHandle<()>>,
}
impl ThreadWorker {
    fn new(
        id: usize,
        consumer: Arc<Mutex<mpsc::Receiver<ThreadJob>>>,
        builder: thread::Builder,
    ) -> ThreadWorker {
        let thread = builder
            .spawn(move || loop {
                let job = consumer.lock().unwrap().recv().unwrap();

                match job {
                    ThreadJob::Run(job) => job(),
                    ThreadJob::Stop => break,
                };
            })
            .unwrap();

        ThreadWorker {
            id,
            thread: Some(thread),
        }
    }
}

impl std::fmt::Display for ThreadWorker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}]", self.id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        sync::{Arc, Mutex},
        thread, time,
    };

    mod helpers {
        use super::*;

        const TARGET: usize = 500_000_000;

        pub fn get_sequential_speed() -> time::Duration {
            let mut value = 0;
            let start = time::Instant::now();

            (0..TARGET).for_each(|_| {
                value += 1;
            });

            start.elapsed()
        }

        pub fn get_parallel_speed() -> time::Duration {
            let mut pool = ThreadPoolBuilder::default().build();
            let num_threads = pool.num_threads();
            let value = Arc::new(Mutex::new(0));
            let start = time::Instant::now();

            assert!(num_threads > 0);

            (0..num_threads).for_each(|_| {
                let value = Arc::clone(&value);
                let mut ir = 0;
                pool.execute(move || {
                    (0..TARGET / num_threads).for_each(|_| {
                        ir += 1;
                    });

                    let mut value = value.lock().unwrap();
                    *value += ir;
                });
            });

            pool.join();
            start.elapsed()
        }
    }

    #[test]
    fn construct_pool() {
        let mut pool = ThreadPoolBuilder::default().build();

        let p = Arc::new(Mutex::new(5));
        let v = Arc::clone(&p);
        pool.execute(move || {
            let mut lock = v.lock().unwrap();
            *lock += 1;

            thread::sleep(std::time::Duration::from_secs(5));
        });

        pool.execute(|| {
            thread::sleep(std::time::Duration::from_secs(10));
        });

        pool.join();
        assert_eq!(*p.lock().unwrap(), 6);
    }

    #[test]
    fn test_sequential_vs_parallel_speed() {
        let sequential = helpers::get_sequential_speed();
        let parallel = helpers::get_parallel_speed();

        println!("sequential speed: {sequential:#?}\nparallel speed: {parallel:#?}");
        assert!(sequential > parallel);
        assert!(sequential > parallel / 2);
    }

    #[test]
    fn test_join_disposal() {}

    #[test]
    fn test_setup_builder_default() {
        let pool = ThreadPoolBuilder::default();

        assert_eq!(pool.num_threads, thread::available_parallelism().unwrap());
        assert_eq!(pool.name_prefix, None);
        assert_eq!(pool.stack_size, None);
    }

    #[test]
    fn test_setup_builder_new() {
        let pool = ThreadPoolBuilder::new(1, 5 * 1024, "PrivatePool".to_string());

        assert_eq!(pool.num_threads, NonZero::new(1).unwrap());
        assert_eq!(pool.stack_size, Some(5 * 1024));
        assert_eq!(pool.name_prefix, Some("PrivatePool".to_string()));
    }

    #[test]
    fn test_setup_builder_num_threads() {
        let pool = ThreadPoolBuilder::default().num_threads(4).build();

        assert_eq!(pool.num_threads(), 4);
    }

    #[test]
    fn test_setup_builder_prefix_name() {
        let pool = ThreadPoolBuilder::default().name_prefix("DarkPrivatisedPool".to_string());

        assert_eq!(pool.name_prefix, Some("DarkPrivatisedPool".to_string()));
    }

    #[test]
    fn test_setup_builder_stack_size() {
        let pool = ThreadPoolBuilder::default().stack_size(5 * 1024 * 1024);

        assert_eq!(pool.stack_size, Some(5 * 1024 * 1024));
    }
}