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
use std::sync::{
	mpsc::{self, Sender},
	Arc, Mutex,
};

use crate::{thread_pool::worker::THREAD_LOCAL_PANIC_HOOK, utils::optimal_number_of_threads};

use self::{
	state::{state_cell::StateCell, State},
	worker::{Task, Worker},
};

///
#[derive(Debug)]
pub struct ThreadPool {
	_workers: Vec<Worker>,
	sender: Sender<Task>,
	capacity: usize,
	state: Arc<State>,
}

/// Max number or thread supported atm
pub const MAX_THREADS: usize = std::mem::size_of::<StateCell>() * 8;

impl ThreadPool {
	/// Will return [`ThreadPool`] with `capacity = logical-cores - 1`
	/// all the threads will spawn immediately
	#[inline]
	pub fn new() -> Self {
		Self::with_capacity(optimal_number_of_threads(u16::MAX as usize))
	}

	/// Will return [`ThreadPool`] with user defined capacity
	#[inline]
	pub fn with_capacity(capacity: usize) -> Self {
		assert!(
			capacity <= MAX_THREADS,
			"ThreadPool: Does not support capacity over {}",
			MAX_THREADS
		);

		let mut _workers = Vec::with_capacity(capacity);

		let (sender, receiver) = mpsc::channel();
		let receiver = Arc::new(Mutex::new(receiver));

		let state = Arc::new(State::default());

		let prev_hook = std::panic::take_hook();

		std::panic::set_hook(Box::new(move |info| {
			unsafe {
				match THREAD_LOCAL_PANIC_HOOK {
					Some(f) => (*f)(),
					None => {}
				}
			}

			prev_hook(info);
		}));

		for id in 0..capacity {
			_workers.push(Worker::new(receiver.clone(), id, state.clone()));
		}

		Self {
			_workers,
			sender,
			capacity,
			state,
		}
	}

	/// Returns [`ThreadPool`] capacity
	#[inline(always)]
	pub fn capacity(&self) -> usize {
		self.capacity
	}

	/// Executes passed function in
	#[inline]
	pub fn execute<F: FnOnce() + Send + 'static>(&self, f: F) {
		self
			.sender
			.send(Task::New(Box::new(f)))
			.expect("Error while sending job to thread worker")
	}

	/// Returns an iterator for capacity
	#[inline]
	pub fn iter(&self) -> std::ops::Range<usize> {
		0..self.capacity
	}

	/// Does not actually call `join` on a thread
	/// Instead breaks from internal loop
	#[inline]
	pub fn join(self) {
		drop(self)
	}

	/// Returns an amount of threads panicking
	#[inline(always)]
	pub fn check_panics(&self) -> usize {
		self.state.panicking.count()
	}

	/// Returns an amount of busy threads
	#[inline(always)]
	pub fn check_busy(&self) -> usize {
		self.state.busy.count()
	}
}

impl Drop for ThreadPool {
	fn drop(&mut self) {
		for _ in self.iter() {
			self.sender.send(Task::Break).unwrap()
		}
	}
}

impl Default for ThreadPool {
	#[inline(always)]
	fn default() -> Self {
		Self::new()
	}
}

mod state;
mod worker;