1 //! A thin wrapper around [`stdx::thread::Pool`] which threads a sender through spawned jobs. 2 //! It is used in [`crate::global_state::GlobalState`] throughout the main loop. 3 4 use crossbeam_channel::Sender; 5 use stdx::thread::{Pool, ThreadIntent}; 6 7 pub(crate) struct TaskPool<T> { 8 sender: Sender<T>, 9 pool: Pool, 10 } 11 12 impl<T> TaskPool<T> { new_with_threads(sender: Sender<T>, threads: usize) -> TaskPool<T>13 pub(crate) fn new_with_threads(sender: Sender<T>, threads: usize) -> TaskPool<T> { 14 TaskPool { sender, pool: Pool::new(threads) } 15 } 16 spawn<F>(&mut self, intent: ThreadIntent, task: F) where F: FnOnce() -> T + Send + 'static, T: Send + 'static,17 pub(crate) fn spawn<F>(&mut self, intent: ThreadIntent, task: F) 18 where 19 F: FnOnce() -> T + Send + 'static, 20 T: Send + 'static, 21 { 22 self.pool.spawn(intent, { 23 let sender = self.sender.clone(); 24 move || sender.send(task()).unwrap() 25 }) 26 } 27 spawn_with_sender<F>(&mut self, intent: ThreadIntent, task: F) where F: FnOnce(Sender<T>) + Send + 'static, T: Send + 'static,28 pub(crate) fn spawn_with_sender<F>(&mut self, intent: ThreadIntent, task: F) 29 where 30 F: FnOnce(Sender<T>) + Send + 'static, 31 T: Send + 'static, 32 { 33 self.pool.spawn(intent, { 34 let sender = self.sender.clone(); 35 move || task(sender) 36 }) 37 } 38 len(&self) -> usize39 pub(crate) fn len(&self) -> usize { 40 self.pool.len() 41 } 42 } 43