11#[ doc( no_inline) ]
22pub use std:: os:: fd:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
3- use std:: { cell :: RefCell , collections:: VecDeque , io, mem:: MaybeUninit , time:: Duration } ;
3+ use std:: { collections:: VecDeque , io, mem:: MaybeUninit , time:: Duration } ;
44
55use io_uring:: {
66 cqueue,
@@ -24,8 +24,8 @@ pub trait OpCode {
2424/// Low-level driver of io-uring.
2525pub struct Driver {
2626 inner : IoUring ,
27- squeue : RefCell < VecDeque < squeue:: Entry > > ,
28- cqueue : RefCell < VecDeque < Entry > > ,
27+ squeue : VecDeque < squeue:: Entry > ,
28+ cqueue : VecDeque < Entry > ,
2929}
3030
3131impl Driver {
@@ -38,25 +38,27 @@ impl Driver {
3838 pub fn with_entries ( entries : u32 ) -> io:: Result < Self > {
3939 Ok ( Self {
4040 inner : IoUring :: new ( entries) ?,
41- squeue : RefCell :: new ( VecDeque :: with_capacity ( entries as usize ) ) ,
42- cqueue : RefCell :: new ( VecDeque :: with_capacity ( entries as usize ) ) ,
41+ squeue : VecDeque :: with_capacity ( entries as usize ) ,
42+ cqueue : VecDeque :: with_capacity ( entries as usize ) ,
4343 } )
4444 }
4545
46- unsafe fn submit ( & self , timeout : Option < Duration > ) -> io:: Result < ( ) > {
46+ fn submit ( & mut self , timeout : Option < Duration > ) -> io:: Result < ( ) > {
4747 // Anyway we need to submit once, no matter there are entries in squeue.
4848 loop {
49- let mut inner_squeue = self . inner . submission_shared ( ) ;
50- while !inner_squeue. is_full ( ) {
51- if let Some ( entry) = self . squeue . borrow_mut ( ) . pop_front ( ) {
52- inner_squeue. push ( & entry) . unwrap ( ) ;
53- } else {
54- break ;
49+ {
50+ let mut inner_squeue = self . inner . submission ( ) ;
51+ while !inner_squeue. is_full ( ) {
52+ if let Some ( entry) = self . squeue . pop_front ( ) {
53+ unsafe { inner_squeue. push ( & entry) } . unwrap ( ) ;
54+ } else {
55+ break ;
56+ }
5557 }
58+ inner_squeue. sync ( ) ;
5659 }
57- inner_squeue. sync ( ) ;
5860
59- let res = if self . squeue . borrow ( ) . is_empty ( ) {
61+ let res = if self . squeue . is_empty ( ) {
6062 // Last part of submission queue, wait till timeout.
6163 if let Some ( duration) = timeout {
6264 let timespec = timespec ( duration) ;
@@ -68,7 +70,6 @@ impl Driver {
6870 } else {
6971 self . inner . submit ( )
7072 } ;
71- inner_squeue. sync ( ) ;
7273 match res {
7374 Ok ( _) => Ok ( ( ) ) ,
7475 Err ( e) => match e. raw_os_error ( ) {
@@ -78,7 +79,7 @@ impl Driver {
7879 } ,
7980 } ?;
8081
81- for entry in self . inner . completion_shared ( ) {
82+ for entry in self . inner . completion ( ) {
8283 let entry = create_entry ( entry) ;
8384 if entry. user_data ( ) == u64:: MAX as _ {
8485 // This is a cancel operation.
@@ -90,45 +91,47 @@ impl Driver {
9091 continue ;
9192 }
9293 }
93- self . cqueue . borrow_mut ( ) . push_back ( entry) ;
94+ self . cqueue . push_back ( entry) ;
9495 }
9596
96- if self . squeue . borrow ( ) . is_empty ( ) && inner_squeue . is_empty ( ) {
97+ if self . squeue . is_empty ( ) && self . inner . submission ( ) . is_empty ( ) {
9798 break ;
9899 }
99100 }
100101 Ok ( ( ) )
101102 }
102103
103- fn poll_entries ( & self , entries : & mut [ MaybeUninit < Entry > ] ) -> usize {
104- let mut cqueue = self . cqueue . borrow_mut ( ) ;
105- let len = cqueue. len ( ) . min ( entries. len ( ) ) ;
104+ fn poll_entries ( & mut self , entries : & mut [ MaybeUninit < Entry > ] ) -> usize {
105+ let len = self . cqueue . len ( ) . min ( entries. len ( ) ) ;
106106 for entry in & mut entries[ ..len] {
107- entry. write ( cqueue. pop_front ( ) . unwrap ( ) ) ;
107+ entry. write ( self . cqueue . pop_front ( ) . unwrap ( ) ) ;
108108 }
109109 len
110110 }
111111}
112112
113113impl Poller for Driver {
114- fn attach ( & self , _fd : RawFd ) -> io:: Result < ( ) > {
114+ fn attach ( & mut self , _fd : RawFd ) -> io:: Result < ( ) > {
115115 Ok ( ( ) )
116116 }
117117
118- unsafe fn push ( & self , op : & mut ( impl OpCode + ' static ) , user_data : usize ) -> io:: Result < ( ) > {
118+ unsafe fn push (
119+ & mut self ,
120+ op : & mut ( impl OpCode + ' static ) ,
121+ user_data : usize ,
122+ ) -> io:: Result < ( ) > {
119123 let entry = op. create_entry ( ) . user_data ( user_data as _ ) ;
120- self . squeue . borrow_mut ( ) . push_back ( entry) ;
124+ self . squeue . push_back ( entry) ;
121125 Ok ( ( ) )
122126 }
123127
124- fn cancel ( & self , user_data : usize ) {
128+ fn cancel ( & mut self , user_data : usize ) {
125129 self . squeue
126- . borrow_mut ( )
127130 . push_back ( AsyncCancel :: new ( user_data as _ ) . build ( ) . user_data ( u64:: MAX ) ) ;
128131 }
129132
130133 fn poll (
131- & self ,
134+ & mut self ,
132135 timeout : Option < Duration > ,
133136 entries : & mut [ MaybeUninit < Entry > ] ,
134137 ) -> io:: Result < usize > {
@@ -139,12 +142,18 @@ impl Poller for Driver {
139142 if len > 0 {
140143 return Ok ( len) ;
141144 }
142- unsafe { self . submit ( timeout) } ?;
145+ self . submit ( timeout) ?;
143146 let len = self . poll_entries ( entries) ;
144147 Ok ( len)
145148 }
146149}
147150
151+ impl AsRawFd for Driver {
152+ fn as_raw_fd ( & self ) -> RawFd {
153+ self . inner . as_raw_fd ( )
154+ }
155+ }
156+
148157fn create_entry ( entry : cqueue:: Entry ) -> Entry {
149158 let result = entry. result ( ) ;
150159 let result = if result < 0 {
0 commit comments