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