io_engine/
callback_worker.rs

1use crate::tasks::{IOCallback, IOEvent};
2use crossfire::{MTx, mpmc};
3
4pub struct IOWorkers<C: IOCallback>(pub(crate) MTx<mpmc::Array<IOEvent<C>>>);
5
6impl<C: IOCallback> IOWorkers<C> {
7    pub fn new(workers: usize) -> Self {
8        let (tx, rx) = mpmc::bounded_blocking::<IOEvent<C>>(100000);
9        for _i in 0..workers {
10            let _rx = rx.clone();
11            std::thread::spawn(move || {
12                loop {
13                    match _rx.recv() {
14                        Ok(event) => event.callback_merged(),
15                        Err(_) => {
16                            debug!("IOWorkers exit");
17                            return;
18                        }
19                    }
20                }
21            });
22        }
23        Self(tx)
24    }
25
26    #[inline(always)]
27    pub fn send(&self, event: IOEvent<C>) {
28        let _ = self.0.send(event);
29    }
30}
31
32impl<C: IOCallback> Clone for IOWorkers<C> {
33    fn clone(&self) -> Self {
34        Self(self.0.clone())
35    }
36}