2024-03-10 18:03:12 +00:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
2024-03-04 20:18:49 +00:00
|
|
|
use threadpool::ThreadPool;
|
|
|
|
|
|
|
|
/// A multithreaded job
|
2024-03-10 18:03:12 +00:00
|
|
|
pub struct MultithreadedJob<E: Send + Sync + 'static> {
|
|
|
|
progress: Receiver<Result<(), E>>,
|
|
|
|
sender: Sender<Result<(), E>>,
|
|
|
|
pool: ThreadPool,
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 18:03:12 +00:00
|
|
|
impl<E: Send + Sync + 'static> Default for MultithreadedJob<E> {
|
|
|
|
fn default() -> Self {
|
2024-03-04 20:18:49 +00:00
|
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
|
|
let pool = ThreadPool::new(6);
|
|
|
|
|
2024-03-10 18:03:12 +00:00
|
|
|
Self {
|
|
|
|
progress: rx,
|
|
|
|
pool,
|
|
|
|
sender: tx.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Send + Sync + 'static> MultithreadedJob<E> {
|
|
|
|
/// Creates a new multithreaded job
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the progress of the job
|
|
|
|
pub fn progress(&self) -> &Receiver<Result<(), E>> {
|
|
|
|
&self.progress
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits for the job to finish
|
|
|
|
pub fn wait(self) -> Result<(), E> {
|
|
|
|
self.pool.join();
|
|
|
|
|
|
|
|
for result in self.progress {
|
|
|
|
result?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-10 18:03:12 +00:00
|
|
|
|
|
|
|
/// Executes a function on the thread pool
|
|
|
|
pub fn execute<F>(&self, f: F)
|
|
|
|
where
|
|
|
|
F: (FnOnce() -> Result<(), E>) + Send + 'static,
|
|
|
|
{
|
|
|
|
let sender = self.sender.clone();
|
|
|
|
|
|
|
|
self.pool.execute(move || {
|
|
|
|
let result = f();
|
|
|
|
sender.send(result).unwrap();
|
|
|
|
});
|
|
|
|
}
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|