11use std:: os:: fd:: { AsRawFd , FromRawFd , OwnedFd , RawFd } ;
22use std:: { cell:: Cell , cell:: RefCell , cmp, collections:: VecDeque , io, mem, rc:: Rc , sync:: Arc } ;
33
4- use io_uring:: cqueue:: { self , Entry as CEntry , more } ;
4+ use io_uring:: cqueue:: { self , more , Entry as CEntry } ;
55use io_uring:: opcode:: { AsyncCancel , PollAdd } ;
66use io_uring:: squeue:: { Entry as SEntry , SubmissionQueue } ;
7- use io_uring:: { IoUring , Probe , types:: Fd } ;
7+ use io_uring:: { types:: Fd , IoUring , Probe } ;
88
99use crate :: pool:: Dispatchable ;
1010
@@ -17,6 +17,9 @@ pub trait Handler {
1717 /// Operation is canceled
1818 fn canceled ( & mut self , id : usize ) ;
1919
20+ /// Driver turn is completed
21+ fn tick ( & mut self ) ;
22+
2023 /// Cleanup before drop
2124 fn cleanup ( & mut self ) ;
2225}
@@ -97,10 +100,25 @@ pub struct Driver {
97100 fd : RawFd ,
98101 hid : Cell < u64 > ,
99102 notifier : Notifier ,
100- handlers : Cell < Option < Box < Vec < Box < dyn Handler > > > > > ,
103+ #[ allow( clippy:: box_collection) ]
104+ handlers : Cell < Option < Box < Vec < HandlerItem > > > > ,
101105 inner : Rc < DriverInner > ,
102106}
103107
108+ struct HandlerItem {
109+ hnd : Box < dyn Handler > ,
110+ modified : bool ,
111+ }
112+
113+ impl HandlerItem {
114+ fn tick ( & mut self ) {
115+ if self . modified {
116+ self . modified = false ;
117+ self . hnd . tick ( ) ;
118+ }
119+ }
120+ }
121+
104122bitflags:: bitflags! {
105123 #[ derive( Copy , Clone , Debug , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
106124 struct Flags : u8 {
@@ -189,10 +207,13 @@ impl Driver {
189207 {
190208 let id = self . hid . get ( ) ;
191209 let mut handlers = self . handlers . take ( ) . unwrap_or_default ( ) ;
192- handlers. push ( f ( DriverApi {
193- batch : id << Self :: BATCH ,
194- inner : self . inner . clone ( ) ,
195- } ) ) ;
210+ handlers. push ( HandlerItem {
211+ hnd : f ( DriverApi {
212+ batch : id << Self :: BATCH ,
213+ inner : self . inner . clone ( ) ,
214+ } ) ,
215+ modified : false ,
216+ } ) ;
196217 self . handlers . set ( Some ( handlers) ) ;
197218 self . hid . set ( id + 1 ) ;
198219 }
@@ -300,18 +321,25 @@ impl Driver {
300321
301322 let result = entry. result ( ) ;
302323 if result == -libc:: ECANCELED {
303- handlers[ batch] . canceled ( user_data) ;
324+ handlers[ batch] . modified = true ;
325+ handlers[ batch] . hnd . canceled ( user_data) ;
304326 } else {
305327 let result = if result < 0 {
306328 Err ( io:: Error :: from_raw_os_error ( -result) )
307329 } else {
308330 Ok ( result as _ )
309331 } ;
310- handlers[ batch] . completed ( user_data, entry. flags ( ) , result) ;
332+ handlers[ batch] . modified = true ;
333+ handlers[ batch]
334+ . hnd
335+ . completed ( user_data, entry. flags ( ) , result) ;
311336 }
312337 }
313338 }
314339 }
340+ for h in handlers. iter_mut ( ) {
341+ h. tick ( ) ;
342+ }
315343 self . handlers . set ( Some ( handlers) ) ;
316344 }
317345 }
@@ -323,7 +351,7 @@ impl Driver {
323351
324352 pub ( crate ) fn clear ( & self ) {
325353 for mut h in self . handlers . take ( ) . unwrap ( ) . into_iter ( ) {
326- h. cleanup ( )
354+ h. hnd . cleanup ( )
327355 }
328356 }
329357}
0 commit comments