thread-manager 1.0.0

A streamlined Rust library for efficient thread pooling and parallel job execution, designed for simplicity, flexibility, and performance.
Documentation
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
518
519
520
521
522
523
524
525
use std::sync::Arc;

use crate::assert::assert_wpc;
use crate::channel::JobChannel;
use crate::channel::ResultChannel;
use crate::dispatch::DispatchCycle;
use crate::iterator::ResultIter;
use crate::iterator::YieldResultIter;
use crate::status::ManagerStatus;
use crate::worker::ThreadWorker;

type FnType<T> = Box<dyn Fn() -> T + Send + 'static>;

/// A thread manager for executing jobs in parallel.
/// This struct manages a pool of worker threads and distributes jobs among them.
///
/// # Type Parameters
/// - `F`: The type of the function or closure that the threads will execute.
/// - `T`: The type of the value returned by the function or closure.
///
/// # Fields
/// - `wpc`: The number of Workers-Per-Channel.
/// - `dispatch`: An instance of `DispatchCycle` to manage job distribution.
/// - `workers`: A vector of `ThreadWorker` instances representing the worker threads.
/// - `channels`: A vector of job channels for dispatching jobs to workers.
/// - `result_channel`: A channel for collecting the results of the jobs.
/// - `manager_status`: An instance of `ManagerStatus` to track the status of the manager.
pub struct ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    wpc: usize,
    dispatch: DispatchCycle,
    workers: Vec<ThreadWorker<F, T>>,
    channels: Vec<Arc<JobChannel<F>>>,
    result_channel: Arc<ResultChannel<T>>,
    manager_status: Arc<ManagerStatus>,
}

impl<F, T> ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    /// Creates a new instance of `ThreadManagerCore` with a specified number of worker threads.
    ///
    /// # Arguments
    /// - `size`: The number of worker threads to create.
    ///
    /// # Returns
    /// A new instance of `ThreadManagerCore`.
    pub fn new(size: usize) -> Self {
        let dispatch: DispatchCycle = DispatchCycle::new(size);
        let workers: Vec<ThreadWorker<F, T>> = Vec::with_capacity(size);
        let channels: Vec<Arc<JobChannel<F>>> = Vec::with_capacity(size);
        let result_channel: Arc<ResultChannel<T>> = Arc::new(ResultChannel::new());
        let manager_status: Arc<ManagerStatus> = Arc::new(ManagerStatus::new());

        let mut manager: ThreadManagerCore<F, T> = Self {
            wpc: 1,
            dispatch,
            workers,
            channels,
            result_channel,
            manager_status,
        };
        manager.create_workers(size);
        manager
    }

    /// Creates a new instance of `ThreadManagerCore` with a specified number of worker threads
    /// and a specific workers-per-channel ratio.
    ///
    /// # Arguments
    /// - `size`: The number of worker threads to create.
    /// - `wpc`: The number of workers per channel.
    ///
    /// # Returns
    /// A new instance of `ThreadManagerCore` with the specified configuration.
    pub fn new_asymmetric(size: usize, wpc: usize) -> Self {
        assert_wpc(size, wpc);
        let dispatch: DispatchCycle = DispatchCycle::new(size);
        let workers: Vec<ThreadWorker<F, T>> = Vec::with_capacity(size);
        let channels: Vec<Arc<JobChannel<F>>> = Vec::with_capacity(size);
        let result_channel: Arc<ResultChannel<T>> = Arc::new(ResultChannel::new());
        let manager_status: Arc<ManagerStatus> = Arc::new(ManagerStatus::new());

        let mut manager: ThreadManagerCore<F, T> = Self {
            wpc,
            dispatch,
            workers,
            channels,
            result_channel,
            manager_status,
        };
        manager.create_workers(size);
        manager
    }

    /// Executes a given function by sending it to an available worker thread.
    ///
    /// # Arguments
    /// - `function`: The function to be executed by the worker thread.
    pub fn execute(&self, function: F) {
        let id: usize = self.dispatch.fetch_and_update();
        let worker: &ThreadWorker<F, T> = &self.workers[id];
        worker.send(function);
    }

    /// Resizes the pool of worker threads.
    ///
    /// # Arguments
    /// - `size`: The new size of the worker pool.
    pub fn resize(&mut self, size: usize) {
        assert_wpc(size, self.wpc);
        let dispatch_size: usize = self.dispatch.fetch_size();

        if size > self.workers.len() {
            let additional_size: usize = size - self.workers.len();
            self.start_workers(dispatch_size, self.workers.len());
            self.create_workers(additional_size);
            self.dispatch.set_size(size);
        } else if size < dispatch_size {
            self.send_release_workers(size, dispatch_size);
            self.dispatch.set_size(size);
        } else if size > dispatch_size {
            self.start_workers(dispatch_size, size);
            self.dispatch.set_size(size);
        }
    }
}

impl<F, T> ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    fn get_channel(&self, id: usize) -> Arc<JobChannel<F>> {
        let channel_id: usize = id / self.wpc;
        self.channels[channel_id].clone()
    }

    fn create_channels(&mut self, size: usize) {
        for _ in 0..(size / self.wpc) {
            let channel: JobChannel<F> = JobChannel::new();
            let channel: Arc<JobChannel<F>> = Arc::new(channel);
            self.channels.push(channel);
        }
    }

    fn create_workers(&mut self, size: usize) {
        self.create_channels(size);
        let worker_size: usize = self.workers.len();

        for idx in 0..size {
            let id: usize = idx + worker_size;
            let job_channel: Arc<JobChannel<F>> = self.get_channel(id);
            let result_channel: Arc<ResultChannel<T>> = self.result_channel.clone();
            let manager_status: Arc<ManagerStatus> = self.manager_status.clone();
            let worker: ThreadWorker<F, T> =
                ThreadWorker::new(id, job_channel, result_channel, manager_status);

            worker.start();
            self.workers.push(worker);
        }
    }
}

impl<F, T> ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    /// Joins all worker threads, effectively blocking the current thread until all worker threads have completed their execution.
    ///
    /// # Note
    /// This method will block the current thread until all worker threads have finished processing their jobs.
    pub fn join(&self) {
        self.send_release_workers(0, self.workers.len());
        self.join_workers(0, self.workers.len());
        self.clear_channels(0, self.channels.len());
    }

    /// Terminates all worker threads gracefully.
    ///
    /// # Note
    /// This method will block until the currently executing job among threads is completed.
    pub fn terminate_all(&self) {
        self.set_termination_workers(0, self.workers.len());
        self.send_release_workers(0, self.workers.len());
        self.join_workers(0, self.workers.len());
        self.clear_channels(0, self.channels.len());
    }

    /// Provides the job distribution across the worker threads.
    ///
    /// # Returns
    /// A vector containing the count of jobs executed by each worker thread.
    pub fn job_distribution(&self) -> Vec<usize> {
        let mut distribution: Vec<usize> = Vec::with_capacity(self.workers.len());
        for worker in self.workers.iter() {
            distribution.push(worker.status().received());
        }
        distribution
    }

    /// Checks if all jobs have been finished.
    ///
    /// # Returns
    /// `true` if all jobs are finished, `false` otherwise.
    pub fn has_finished(&self) -> bool {
        for job_channel in self.channels.iter() {
            if !job_channel.is_finished() {
                return false;
            }
        }
        true
    }

    /// Retrieves an iterator over the results of completed jobs.
    ///
    /// # Returns
    /// An iterator (`ResultIter`) over the results of the jobs that have been completed.
    pub fn results<'a>(&'a self) -> ResultIter<'a, T> {
        ResultIter::new(&self.result_channel)
    }

    /// Retrieves an iterator that yields results as they become available.
    ///
    /// # Returns
    /// An iterator (`YieldResultIter`) that yields results from worker threads.
    /// This method blocks for each result until the job queue is complete.
    pub fn yield_results<'a>(&'a self) -> YieldResultIter<'a, F, T> {
        YieldResultIter::new(&self.workers, &self.result_channel)
    }

    /// Returns the number of active worker threads (both busy and waiting).
    ///
    /// # Returns
    /// The total number of active worker threads.
    pub fn active_threads(&self) -> usize {
        self.manager_status.active_threads()
    }

    /// Returns the number of worker threads that are currently busy executing a job.
    ///
    /// # Returns
    /// The number of busy worker threads.
    pub fn busy_threads(&self) -> usize {
        self.manager_status.busy_threads()
    }

    /// Returns the number of worker threads that are currently waiting for a job.
    ///
    /// # Returns
    /// The number of waiting worker threads.
    pub fn waiting_threads(&self) -> usize {
        self.manager_status.waiting_threads()
    }

    /// Returns the number of jobs currently in the queue waiting to be executed.
    ///
    /// # Returns
    /// The size of the job queue.
    pub fn job_queue(&self) -> usize {
        let mut queue: usize = 0;
        for job_channel in self.channels.iter() {
            queue += job_channel.status().pending();
        }
        queue
    }

    /// Returns the total number of jobs that have been sent to worker threads.
    ///
    /// # Returns
    /// The number of sent jobs.
    pub fn sent_jobs(&self) -> usize {
        let mut sent: usize = 0;
        for job_channel in self.channels.iter() {
            sent += job_channel.status().sent();
        }
        sent
    }

    /// Returns the total number of jobs that have been received by worker threads.
    ///
    /// # Returns
    /// The number of received jobs.
    pub fn received_jobs(&self) -> usize {
        let mut received: usize = 0;
        for job_channel in self.channels.iter() {
            received += job_channel.status().received();
        }
        received
    }

    /// Returns the total number of jobs that have been concluded by worker threads.
    ///
    /// # Returns
    /// The number of concluded jobs.
    pub fn concluded_jobs(&self) -> usize {
        let mut concluded: usize = 0;
        for job_channel in self.channels.iter() {
            concluded += job_channel.status().concluded();
        }
        concluded
    }
}

impl<F, T> ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    fn start_workers(&self, st: usize, en: usize) {
        for worker in self.workers[st..en].iter() {
            worker.start();
        }
    }

    fn join_workers(&self, st: usize, en: usize) {
        for worker in self.workers[st..en].iter() {
            worker.join();
        }
    }

    fn clear_channels(&self, st: usize, en: usize) {
        for job_channel in self.channels[st..en].iter() {
            job_channel.clear();
        }
    }

    fn set_termination_workers(&self, st: usize, en: usize) {
        for worker in self.workers[st..en].iter() {
            worker.set_termination(true);
        }
    }

    fn send_release_workers(&self, st: usize, en: usize) {
        for worker in self.workers[st..en].iter() {
            worker.send_release();
        }
    }
}

impl<F, T> Drop for ThreadManagerCore<F, T>
where
    F: Fn() -> T + Send + 'static,
    T: Send + 'static,
{
    fn drop(&mut self) {
        self.terminate_all();
    }
}

/// A dynamic dispatch version of `ThreadManagerCore` for managing threads that execute functions
/// returning a specific type `T`.
///
/// # Type Parameters
/// - `T`: The type of the value returned by the functions executed by the threads.
pub struct ThreadManager<T>
where
    T: Send + 'static,
{
    manager: ThreadManagerCore<FnType<T>, T>,
}

impl<T> ThreadManager<T>
where
    T: Send + 'static,
{
    /// Creates a new instance of `ThreadManager` with a specified number of worker threads.
    ///
    /// # Arguments
    /// - `size`: The number of worker threads to create.
    ///
    /// # Returns
    /// A new instance of `ThreadManager`.
    pub fn new(size: usize) -> Self {
        let manager: ThreadManagerCore<FnType<T>, T> = ThreadManagerCore::new(size);
        Self { manager }
    }

    /// Creates a new instance of `ThreadManager` with a specified number of worker threads
    /// and a specific workers-per-channel ratio.
    ///
    /// # Arguments
    /// - `size`: The number of worker threads to create.
    /// - `wpc`: The number of workers per channel.
    ///
    /// # Returns
    /// A new instance of `ThreadManager` with the specified configuration.
    pub fn new_asymmetric(size: usize, wpc: usize) -> Self {
        let manager: ThreadManagerCore<FnType<T>, T> = ThreadManagerCore::new_asymmetric(size, wpc);
        Self { manager }
    }

    /// Executes a given function by sending it to an available worker thread.
    ///
    /// # Type Parameters
    /// - `F`: The type of the function to execute.
    ///
    /// # Arguments
    /// - `function`: The function to be executed by the worker thread.
    pub fn execute<F>(&self, function: F)
    where
        F: Fn() -> T + Send + 'static,
    {
        self.manager.execute(Box::new(function))
    }

    /// Resizes the pool of worker threads.
    ///
    /// # Arguments
    /// - `size`: The new size of the worker pool.
    pub fn resize(&mut self, size: usize) {
        self.manager.resize(size)
    }

    /// Joins all worker threads, effectively blocking the current thread until all worker threads have completed their execution.
    ///
    /// # Note
    /// This method will block the current thread until all worker threads have finished processing their jobs.
    pub fn join(&self) {
        self.manager.join();
    }

    /// Terminates all worker threads gracefully.
    ///
    /// # Note
    /// This method will block until the currently executing job among threads is completed.
    pub fn terminate_all(&self) {
        self.manager.terminate_all()
    }

    /// Provides the job distribution across the worker threads.
    ///
    /// # Returns
    /// A vector containing the count of jobs executed by each worker thread.
    pub fn job_distribution(&self) -> Vec<usize> {
        self.manager.job_distribution()
    }

    /// Checks if all jobs have been finished.
    ///
    /// # Returns
    /// `true` if all jobs are finished, `false` otherwise.
    pub fn has_finished(&self) -> bool {
        self.manager.has_finished()
    }

    /// Retrieves an iterator over the results of completed jobs.
    ///
    /// # Returns
    /// An iterator (`ResultIter`) over the results of the jobs that have been completed.
    pub fn results<'a>(&'a self) -> ResultIter<'a, T> {
        self.manager.results()
    }

    /// Retrieves an iterator that yields results as they become available.
    ///
    /// # Returns
    /// An iterator (`YieldResultIter`) that yields results from worker threads.
    /// This method blocks for each result until the job queue is complete.
    pub fn yield_results<'a>(&'a self) -> YieldResultIter<'a, FnType<T>, T> {
        self.manager.yield_results()
    }

    /// Returns the number of active worker threads (both busy and waiting).
    ///
    /// # Returns
    /// The total number of active worker threads.
    pub fn active_threads(&self) -> usize {
        self.manager.active_threads()
    }

    /// Returns the number of worker threads that are currently busy executing a job.
    ///
    /// # Returns
    /// The number of busy worker threads.
    pub fn busy_threads(&self) -> usize {
        self.manager.busy_threads()
    }

    /// Returns the number of worker threads that are currently waiting for a job.
    ///
    /// # Returns
    /// The number of waiting worker threads.
    pub fn waiting_threads(&self) -> usize {
        self.manager.waiting_threads()
    }

    /// Returns the number of jobs currently in the queue waiting to be executed.
    ///
    /// # Returns
    /// The size of the job queue.
    pub fn job_queue(&self) -> usize {
        self.manager.job_queue()
    }

    /// Returns the total number of jobs that have been sent to worker threads.
    ///
    /// # Returns
    /// The number of sent jobs.
    pub fn sent_jobs(&self) -> usize {
        self.manager.sent_jobs()
    }

    /// Returns the total number of jobs that have been received by worker threads.
    ///
    /// # Returns
    /// The number of received jobs.
    pub fn received_jobs(&self) -> usize {
        self.manager.received_jobs()
    }

    /// Returns the total number of jobs that have been concluded by worker threads.
    ///
    /// # Returns
    /// The number of concluded jobs.
    pub fn concluded_jobs(&self) -> usize {
        self.manager.concluded_jobs()
    }
}