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
use crate::pool::spawn::QueueCore;
use crate::pool::worker::WorkerThread;
use crate::pool::{CloneRunnerBuilder, Local, Remote, Runner, RunnerBuilder, ThreadPool};
use crate::queue::{self, multilevel, LocalQueue, QueueType, TaskCell};
use crate::task::{callback, future};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
#[derive(Clone)]
pub struct SchedConfig {
pub max_thread_count: usize,
pub min_thread_count: usize,
pub max_inplace_spin: usize,
pub max_idle_time: Duration,
pub max_wait_time: Duration,
pub wake_backoff: Duration,
pub alloc_slot_backoff: Duration,
}
impl Default for SchedConfig {
fn default() -> SchedConfig {
SchedConfig {
max_thread_count: num_cpus::get(),
min_thread_count: 1,
max_inplace_spin: 4,
max_idle_time: Duration::from_millis(1),
max_wait_time: Duration::from_millis(1),
wake_backoff: Duration::from_millis(1),
alloc_slot_backoff: Duration::from_millis(2),
}
}
}
pub struct LazyBuilder<T> {
builder: Builder,
core: Arc<QueueCore<T>>,
local_queues: Vec<LocalQueue<T>>,
}
impl<T> LazyBuilder<T> {
pub fn name(mut self, name_prefix: impl Into<String>) -> LazyBuilder<T> {
self.builder.name_prefix = name_prefix.into();
self
}
}
impl<T> LazyBuilder<T>
where
T: TaskCell + Send + 'static,
{
pub fn build<F>(self, mut factory: F) -> ThreadPool<T>
where
F: RunnerBuilder,
F::Runner: Runner<TaskCell = T> + Send + 'static,
{
let mut threads = Vec::with_capacity(self.builder.sched_config.max_thread_count);
for (i, local_queue) in self.local_queues.into_iter().enumerate() {
let runner = factory.build();
let name = format!("{}-{}", self.builder.name_prefix, i);
let mut builder = thread::Builder::new().name(name);
if let Some(size) = self.builder.stack_size {
builder = builder.stack_size(size)
}
let local = Local::new(i + 1, local_queue, self.core.clone());
let thd = WorkerThread::new(local, runner);
threads.push(
builder
.spawn(move || {
thd.run();
})
.unwrap(),
);
}
ThreadPool {
remote: Remote::new(self.core),
threads: Mutex::new(threads),
}
}
}
#[derive(Clone)]
pub struct Builder {
name_prefix: String,
stack_size: Option<usize>,
sched_config: SchedConfig,
}
impl Builder {
pub fn new(name_prefix: impl Into<String>) -> Builder {
Builder {
name_prefix: name_prefix.into(),
stack_size: None,
sched_config: SchedConfig::default(),
}
}
pub fn max_thread_count(&mut self, count: usize) -> &mut Self {
if count > 0 {
self.sched_config.max_thread_count = count;
}
self
}
pub fn min_thread_count(&mut self, count: usize) -> &mut Self {
if count > 0 {
self.sched_config.min_thread_count = count;
}
self
}
pub fn max_inplace_spin(&mut self, count: usize) -> &mut Self {
self.sched_config.max_inplace_spin = count;
self
}
pub fn max_idle_time(&mut self, time: Duration) -> &mut Self {
self.sched_config.max_idle_time = time;
self
}
pub fn max_wait_time(&mut self, time: Duration) -> &mut Self {
self.sched_config.max_wait_time = time;
self
}
pub fn wake_backoff(&mut self, time: Duration) -> &mut Self {
self.sched_config.wake_backoff = time;
self
}
pub fn alloc_slot_backoff(&mut self, time: Duration) -> &mut Self {
self.sched_config.alloc_slot_backoff = time;
self
}
pub fn stack_size(&mut self, size: usize) -> &mut Self {
if size > 0 {
self.stack_size = Some(size);
}
self
}
pub fn freeze<T>(&self) -> (Remote<T>, LazyBuilder<T>)
where
T: TaskCell + Send,
{
self.freeze_with_queue(QueueType::SingleLevel)
}
pub fn freeze_with_queue<T>(&self, queue_type: QueueType) -> (Remote<T>, LazyBuilder<T>)
where
T: TaskCell + Send,
{
assert!(self.sched_config.min_thread_count <= self.sched_config.max_thread_count);
let (injector, local_queues) = queue::build(queue_type, self.sched_config.max_thread_count);
let core = Arc::new(QueueCore::new(injector, self.sched_config.clone()));
(
Remote::new(core.clone()),
LazyBuilder {
builder: self.clone(),
core,
local_queues,
},
)
}
pub fn build_callback_pool(&self) -> ThreadPool<callback::TaskCell> {
let rb = CloneRunnerBuilder(callback::Runner::default());
self.build_with_queue_and_runner(QueueType::SingleLevel, rb)
}
pub fn build_future_pool(&self) -> ThreadPool<future::TaskCell> {
let fb = CloneRunnerBuilder(future::Runner::default());
self.build_with_queue_and_runner(QueueType::SingleLevel, fb)
}
pub fn build_multilevel_future_pool(&self) -> ThreadPool<future::TaskCell> {
let fb = CloneRunnerBuilder(future::Runner::default());
let queue_builder = multilevel::Builder::new(multilevel::Config::default());
let runner_builder = queue_builder.runner_builder(fb);
self.build_with_queue_and_runner(QueueType::Multilevel(queue_builder), runner_builder)
}
pub fn build_with_queue_and_runner<T, B>(
&self,
queue_type: QueueType,
runner_builder: B,
) -> ThreadPool<T>
where
T: TaskCell + Send + 'static,
B: RunnerBuilder,
B::Runner: Runner<TaskCell = T> + Send + 'static,
{
self.freeze_with_queue(queue_type).1.build(runner_builder)
}
}