recoverable_thread_pool/thread_pool/sync/
impl.rs

1use crate::*;
2use recoverable_spawn::sync::*;
3
4/// Sync implementation of thread pool operations.
5impl ThreadPool {
6    /// Creates a new thread pool with the specified number of workers.
7    ///
8    /// # Arguments
9    ///
10    /// - `usize` - The number of worker threads to create.
11    ///
12    /// # Returns
13    ///
14    /// - `ThreadPool` - The new thread pool instance.
15    pub fn new(size: usize) -> ThreadPool {
16        let (sender, receiver) = mpsc::channel();
17        let receiver: Arc<Mutex<Receiver<ThreadPoolJob>>> = Arc::new(Mutex::new(receiver));
18        let mut workers: Vec<Worker> = Vec::with_capacity(size);
19        let mut id: usize = 0;
20        loop {
21            if id >= size {
22                break;
23            }
24            let worker: Option<Worker> = Worker::new(id, Arc::clone(&receiver));
25            if worker.is_some() {
26                workers.push(worker.unwrap_or_default());
27                id += 1;
28            }
29        }
30        ThreadPool { workers, sender }
31    }
32
33    /// Executes a synchronous job in the thread pool.
34    ///
35    /// # Arguments
36    ///
37    /// - `F` - The synchronous function to execute.
38    ///
39    /// # Returns
40    ///
41    /// - `SendResult` - Result of the job submission.
42    pub fn execute<F>(&self, job: F) -> SendResult
43    where
44        F: RecoverableFunction,
45    {
46        let job_with_handler: ThreadPoolJob = Box::new(move || {
47            let _ = run_function(job);
48        });
49        self.sender.send(job_with_handler)
50    }
51
52    /// Executes a synchronous job with error handling in the thread pool.
53    ///
54    /// # Arguments
55    ///
56    /// - `F` - The synchronous function to execute.
57    /// - `E` - The error handler function.
58    ///
59    /// # Returns
60    ///
61    /// - `SendResult` - Result of the job submission.
62    pub fn execute_with_catch<F, E>(&self, job: F, handle_error: E) -> SendResult
63    where
64        F: RecoverableFunction,
65        E: ErrorHandlerFunction,
66    {
67        let job_with_handler: ThreadPoolJob = Box::new(move || {
68            if let Err(err) = run_function(job) {
69                let err_string: String = spawn_error_to_string(&err);
70                let _ = run_error_handle_function(handle_error, &err_string);
71            }
72        });
73        self.sender.send(job_with_handler)
74    }
75
76    /// Executes a synchronous job with error handling and finalization in the thread pool.
77    ///
78    /// # Arguments
79    ///
80    /// - `F` - The synchronous function to execute.
81    /// - `E` - The error handler function.
82    /// - `L` - The finally handler function.
83    ///
84    /// # Returns
85    ///
86    /// - `SendResult` - Result of the job submission.
87    pub fn execute_with_catch_finally<F, E, L>(
88        &self,
89        job: F,
90        handle_error: E,
91        finally: L,
92    ) -> SendResult
93    where
94        F: RecoverableFunction,
95        E: ErrorHandlerFunction,
96        L: RecoverableFunction,
97    {
98        let job_with_handler: ThreadPoolJob = Box::new(move || {
99            if let Err(err) = run_function(job) {
100                let err_string: String = spawn_error_to_string(&err);
101                let _ = run_error_handle_function(handle_error, &err_string);
102            }
103            let _ = run_function(finally);
104        });
105        self.sender.send(job_with_handler)
106    }
107}