@@ -5,7 +5,7 @@ use std::pin::Pin;
55use std:: rc:: Rc ;
66use std:: task:: { Context , Poll , Waker } ;
77
8- use io_uring:: squeue;
8+ use io_uring:: { cqueue , squeue} ;
99
1010use crate :: driver;
1111
@@ -23,7 +23,7 @@ pub(crate) struct Op<T: 'static> {
2323
2424pub ( crate ) trait Completable {
2525 type Output ;
26- fn complete ( self , result : io :: Result < u32 > , flags : u32 ) -> Self :: Output ;
26+ fn complete ( self , cqe : CqeResult ) -> Self :: Output ;
2727}
2828
2929pub ( crate ) enum Lifecycle {
@@ -37,16 +37,36 @@ pub(crate) enum Lifecycle {
3737 /// must be passed to the driver and held until the operation completes.
3838 Ignored ( Box < dyn std:: any:: Any > ) ,
3939
40- /// The operation has completed.
41- Completed ( io:: Result < u32 > , u32 ) ,
40+ /// The operation has completed with a single cqe result
41+ Completed ( CqeResult ) ,
42+ }
43+
44+ /// A single CQE entry
45+ pub ( crate ) struct CqeResult {
46+ pub ( crate ) result : io:: Result < u32 > ,
47+ #[ allow( dead_code) ]
48+ pub ( crate ) flags : u32 ,
49+ }
50+
51+ impl From < cqueue:: Entry > for CqeResult {
52+ fn from ( cqe : cqueue:: Entry ) -> Self {
53+ let res = cqe. result ( ) ;
54+ let flags = cqe. flags ( ) ;
55+ let result = if res >= 0 {
56+ Ok ( res as u32 )
57+ } else {
58+ Err ( io:: Error :: from_raw_os_error ( -res) )
59+ } ;
60+ CqeResult { result, flags }
61+ }
4262}
4363
4464impl < T > Op < T >
4565where
4666 T : Completable ,
4767{
4868 /// Create a new operation
49- fn new ( data : T , inner : & mut driver:: Inner , inner_rc : & Rc < RefCell < driver:: Inner > > ) -> Op < T > {
69+ fn new ( data : T , inner : & mut driver:: Inner , inner_rc : & Rc < RefCell < driver:: Inner > > ) -> Self {
5070 Op {
5171 driver : inner_rc. clone ( ) ,
5272 index : inner. ops . insert ( ) ,
5878 ///
5979 /// `state` is stored during the operation tracking any state submitted to
6080 /// the kernel.
61- pub ( super ) fn submit_with < F > ( data : T , f : F ) -> io:: Result < Op < T > >
81+ pub ( super ) fn submit_with < F > ( data : T , f : F ) -> io:: Result < Self >
6282 where
6383 F : FnOnce ( & mut T ) -> squeue:: Entry ,
6484 {
83103 }
84104
85105 /// Try submitting an operation to uring
86- pub ( super ) fn try_submit_with < F > ( data : T , f : F ) -> io:: Result < Op < T > >
106+ pub ( super ) fn try_submit_with < F > ( data : T , f : F ) -> io:: Result < Self >
87107 where
88108 F : FnOnce ( & mut T ) -> squeue:: Entry ,
89109 {
@@ -122,11 +142,10 @@ where
122142 Poll :: Pending
123143 }
124144 Lifecycle :: Ignored ( ..) => unreachable ! ( ) ,
125- Lifecycle :: Completed ( result , flags ) => {
145+ Lifecycle :: Completed ( cqe ) => {
126146 inner. ops . remove ( me. index ) ;
127147 me. index = usize:: MAX ;
128-
129- Poll :: Ready ( me. data . take ( ) . unwrap ( ) . complete ( result, flags) )
148+ Poll :: Ready ( me. data . take ( ) . unwrap ( ) . complete ( cqe) )
130149 }
131150 }
132151 }
@@ -153,16 +172,16 @@ impl<T> Drop for Op<T> {
153172}
154173
155174impl Lifecycle {
156- pub ( super ) fn complete ( & mut self , result : io :: Result < u32 > , flags : u32 ) -> bool {
175+ pub ( super ) fn complete ( & mut self , cqe : CqeResult ) -> bool {
157176 use std:: mem;
158177
159178 match mem:: replace ( self , Lifecycle :: Submitted ) {
160179 Lifecycle :: Submitted => {
161- * self = Lifecycle :: Completed ( result , flags ) ;
180+ * self = Lifecycle :: Completed ( cqe ) ;
162181 false
163182 }
164183 Lifecycle :: Waiting ( waker) => {
165- * self = Lifecycle :: Completed ( result , flags ) ;
184+ * self = Lifecycle :: Completed ( cqe ) ;
166185 waker. wake ( ) ;
167186 false
168187 }
@@ -190,10 +209,10 @@ mod test {
190209 impl Completable for Rc < ( ) > {
191210 type Output = Completion ;
192211
193- fn complete ( self , result : io :: Result < u32 > , flags : u32 ) -> Self :: Output {
212+ fn complete ( self , cqe : CqeResult ) -> Self :: Output {
194213 Completion {
195- result,
196- flags,
214+ result : cqe . result ,
215+ flags : cqe . flags ,
197216 data : self . clone ( ) ,
198217 }
199218 }
@@ -304,7 +323,11 @@ mod test {
304323 assert_eq ! ( 2 , Rc :: strong_count( & data) ) ;
305324
306325 assert_eq ! ( 1 , driver. num_operations( ) ) ;
307- driver. inner . borrow_mut ( ) . ops . complete ( index, Ok ( 1 ) , 0 ) ;
326+ let cqe = CqeResult {
327+ result : Ok ( 1 ) ,
328+ flags : 0 ,
329+ } ;
330+ driver. inner . borrow_mut ( ) . ops . complete ( index, cqe) ;
308331 assert_eq ! ( 1 , Rc :: strong_count( & data) ) ;
309332 assert_eq ! ( 0 , driver. num_operations( ) ) ;
310333 release ( driver) ;
@@ -326,11 +349,12 @@ mod test {
326349 }
327350
328351 fn complete ( op : & Op < Rc < ( ) > > , result : io:: Result < u32 > ) {
329- op. driver . borrow_mut ( ) . ops . complete ( op. index , result, 0 ) ;
352+ let cqe = CqeResult { result, flags : 0 } ;
353+ op. driver . borrow_mut ( ) . ops . complete ( op. index , cqe) ;
330354 }
331355
332356 fn release ( driver : crate :: driver:: Driver ) {
333357 // Clear ops, we aren't really doing any I/O
334- driver. inner . borrow_mut ( ) . ops . 0 . clear ( ) ;
358+ driver. inner . borrow_mut ( ) . ops . lifecycle . clear ( ) ;
335359 }
336360}
0 commit comments