//! A thin wrapper around [`stdx::thread::Pool`] which threads a sender through spawned jobs. //! It is used in [`crate::global_state::GlobalState`] throughout the main loop. use crossbeam_channel::Sender; use stdx::thread::{Pool, ThreadIntent}; pub(crate) struct TaskPool { sender: Sender, pool: Pool, } impl TaskPool { pub(crate) fn new_with_threads(sender: Sender, threads: usize) -> TaskPool { TaskPool { sender, pool: Pool::new(threads) } } pub(crate) fn spawn(&mut self, intent: ThreadIntent, task: F) where F: FnOnce() -> T + Send + 'static, T: Send + 'static, { self.pool.spawn(intent, { let sender = self.sender.clone(); move || sender.send(task()).unwrap() }) } pub(crate) fn spawn_with_sender(&mut self, intent: ThreadIntent, task: F) where F: FnOnce(Sender) + Send + 'static, T: Send + 'static, { self.pool.spawn(intent, { let sender = self.sender.clone(); move || task(sender) }) } pub(crate) fn len(&self) -> usize { self.pool.len() } }