Skip to content

Commit dbccc93

Browse files
authored
Merge pull request #251 from Berrysoft/refactor/reduce-hashmap
feat(driver): move future state into RawOp
2 parents 14ef873 + 4780fd1 commit dbccc93

File tree

17 files changed

+451
-514
lines changed

17 files changed

+451
-514
lines changed

compio-driver/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ compio-log = { workspace = true }
3636
cfg-if = { workspace = true }
3737
crossbeam-channel = { workspace = true }
3838
futures-util = { workspace = true }
39-
slab = { workspace = true }
4039
socket2 = { workspace = true }
4140

4241
# Windows specific dependencies

compio-driver/src/fd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T> SharedFd<T> {
9595
impl<T> Drop for SharedFd<T> {
9696
fn drop(&mut self) {
9797
// It's OK to wake multiple times.
98-
if Arc::strong_count(&self.0) == 2 {
98+
if Arc::strong_count(&self.0) == 2 && self.0.waits.load(Ordering::Acquire) {
9999
self.0.waker.wake()
100100
}
101101
}

compio-driver/src/fusion/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ pub use driver_type::DriverType;
1414
pub(crate) use iour::{sockaddr_storage, socklen_t};
1515
pub use iour::{OpCode as IourOpCode, OpEntry};
1616
pub use poll::{Decision, OpCode as PollOpCode};
17-
use slab::Slab;
1817

19-
pub(crate) use crate::unix::RawOp;
20-
use crate::{OutEntries, ProactorBuilder};
18+
use crate::{Key, OutEntries, ProactorBuilder};
2119

2220
mod driver_type {
2321
use std::sync::atomic::{AtomicU8, Ordering};
@@ -136,10 +134,10 @@ impl Driver {
136134
}
137135
}
138136

139-
pub fn create_op<T: OpCode + 'static>(&self, user_data: usize, op: T) -> RawOp {
137+
pub fn create_op<T: OpCode + 'static>(&self, op: T) -> Key<T> {
140138
match &self.fuse {
141-
FuseDriver::Poll(driver) => driver.create_op(user_data, op),
142-
FuseDriver::IoUring(driver) => driver.create_op(user_data, op),
139+
FuseDriver::Poll(driver) => driver.create_op(op),
140+
FuseDriver::IoUring(driver) => driver.create_op(op),
143141
}
144142
}
145143

@@ -150,17 +148,17 @@ impl Driver {
150148
}
151149
}
152150

153-
pub fn cancel(&mut self, user_data: usize, registry: &mut Slab<RawOp>) {
151+
pub fn cancel<T>(&mut self, op: Key<T>) {
154152
match &mut self.fuse {
155-
FuseDriver::Poll(driver) => driver.cancel(user_data, registry),
156-
FuseDriver::IoUring(driver) => driver.cancel(user_data, registry),
153+
FuseDriver::Poll(driver) => driver.cancel(op),
154+
FuseDriver::IoUring(driver) => driver.cancel(op),
157155
}
158156
}
159157

160-
pub fn push(&mut self, user_data: usize, op: &mut RawOp) -> Poll<io::Result<usize>> {
158+
pub fn push<T: OpCode + 'static>(&mut self, op: &mut Key<T>) -> Poll<io::Result<usize>> {
161159
match &mut self.fuse {
162-
FuseDriver::Poll(driver) => driver.push(user_data, op),
163-
FuseDriver::IoUring(driver) => driver.push(user_data, op),
160+
FuseDriver::Poll(driver) => driver.push(op),
161+
FuseDriver::IoUring(driver) => driver.push(op),
164162
}
165163
}
166164

compio-driver/src/iocp/cp/global.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ impl GlobalPort {
2929
self.port.attach(fd)
3030
}
3131

32-
pub fn post<T: ?Sized>(
33-
&self,
34-
res: io::Result<usize>,
35-
optr: *mut Overlapped<T>,
36-
) -> io::Result<()> {
32+
pub fn post(&self, res: io::Result<usize>, optr: *mut Overlapped) -> io::Result<()> {
3733
self.port.post(res, optr)
3834
}
3935

40-
pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> {
36+
pub fn post_raw(&self, optr: *const Overlapped) -> io::Result<()> {
4137
self.port.post_raw(optr)
4238
}
4339
}
@@ -62,7 +58,7 @@ fn iocp_start() -> io::Result<()> {
6258
loop {
6359
for entry in port.port.poll_raw(None)? {
6460
// Any thin pointer is OK because we don't use the type of opcode.
65-
let overlapped_ptr: *mut Overlapped<()> = entry.lpOverlapped.cast();
61+
let overlapped_ptr: *mut Overlapped = entry.lpOverlapped.cast();
6662
let overlapped = unsafe { &*overlapped_ptr };
6763
if let Err(_e) = syscall!(
6864
BOOL,
@@ -135,15 +131,11 @@ impl PortHandle {
135131
Self { port }
136132
}
137133

138-
pub fn post<T: ?Sized>(
139-
&self,
140-
res: io::Result<usize>,
141-
optr: *mut Overlapped<T>,
142-
) -> io::Result<()> {
134+
pub fn post(&self, res: io::Result<usize>, optr: *mut Overlapped) -> io::Result<()> {
143135
self.port.post(res, optr)
144136
}
145137

146-
pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> {
138+
pub fn post_raw(&self, optr: *const Overlapped) -> io::Result<()> {
147139
self.port.post_raw(optr)
148140
}
149141
}

compio-driver/src/iocp/cp/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ impl CompletionPort {
7777
Ok(())
7878
}
7979

80-
pub fn post<T: ?Sized>(
81-
&self,
82-
res: io::Result<usize>,
83-
optr: *mut Overlapped<T>,
84-
) -> io::Result<()> {
80+
pub fn post(&self, res: io::Result<usize>, optr: *mut Overlapped) -> io::Result<()> {
8581
if let Some(overlapped) = unsafe { optr.as_mut() } {
8682
match &res {
8783
Ok(transferred) => {
@@ -97,7 +93,7 @@ impl CompletionPort {
9793
self.post_raw(optr)
9894
}
9995

100-
pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> {
96+
pub fn post_raw(&self, optr: *const Overlapped) -> io::Result<()> {
10197
syscall!(
10298
BOOL,
10399
PostQueuedCompletionStatus(self.port.as_raw_handle() as _, 0, 0, optr.cast())
@@ -143,7 +139,7 @@ impl CompletionPort {
143139
) -> io::Result<impl Iterator<Item = Entry>> {
144140
Ok(self.poll_raw(timeout)?.filter_map(move |entry| {
145141
// Any thin pointer is OK because we don't use the type of opcode.
146-
let overlapped_ptr: *mut Overlapped<()> = entry.lpOverlapped.cast();
142+
let overlapped_ptr: *mut Overlapped = entry.lpOverlapped.cast();
147143
let overlapped = unsafe { &*overlapped_ptr };
148144
if let Some(current_driver) = current_driver {
149145
if overlapped.driver != current_driver {
@@ -181,7 +177,7 @@ impl CompletionPort {
181177
_ => Err(io::Error::from_raw_os_error(error as _)),
182178
}
183179
};
184-
Some(Entry::new(overlapped.user_data, res))
180+
Some(Entry::new(overlapped_ptr as usize, res))
185181
}))
186182
}
187183
}

compio-driver/src/iocp/cp/multi.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,11 @@ impl PortHandle {
4848
Self { port }
4949
}
5050

51-
pub fn post<T: ?Sized>(
52-
&self,
53-
res: io::Result<usize>,
54-
optr: *mut Overlapped<T>,
55-
) -> io::Result<()> {
51+
pub fn post(&self, res: io::Result<usize>, optr: *mut Overlapped) -> io::Result<()> {
5652
self.port.post(res, optr)
5753
}
5854

59-
pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> {
55+
pub fn post_raw(&self, optr: *const Overlapped) -> io::Result<()> {
6056
self.port.post_raw(optr)
6157
}
6258
}

0 commit comments

Comments
 (0)