Skip to content

Commit 89bdb86

Browse files
committed
feat(driver): expose PortId instead of usize
1 parent ec8b7a6 commit 89bdb86

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender, TryRecvEr
1111
use crossbeam_skiplist::SkipMap;
1212
#[cfg(not(feature = "once_cell_try"))]
1313
use once_cell::sync::OnceCell as OnceLock;
14-
use windows_sys::Win32::Foundation::ERROR_TIMEOUT;
14+
use windows_sys::Win32::{
15+
Foundation::ERROR_TIMEOUT,
16+
System::IO::{PostQueuedCompletionStatus, OVERLAPPED},
17+
};
1518

1619
use super::CompletionPort;
17-
use crate::{Entry, Overlapped, RawFd};
20+
use crate::{syscall, Entry, Overlapped, RawFd};
1821

1922
struct GlobalPort {
2023
port: CompletionPort,
@@ -75,7 +78,7 @@ fn iocp_start() -> io::Result<()> {
7578
std::thread::spawn(move || {
7679
loop {
7780
for (driver, entry) in port.port.poll(None, None)? {
78-
port.push(driver, entry);
81+
port.push(driver.0, entry);
7982
}
8083
}
8184
#[allow(unreachable_code)]
@@ -103,8 +106,8 @@ impl Port {
103106
Ok(Self { id, port, receiver })
104107
}
105108

106-
pub fn id(&self) -> usize {
107-
self.id
109+
pub fn id(&self) -> PortId {
110+
PortId(self.id)
108111
}
109112

110113
pub fn attach(&mut self, fd: RawFd) -> io::Result<()> {
@@ -160,3 +163,18 @@ impl PortHandle {
160163
self.port.post(res, optr)
161164
}
162165
}
166+
167+
/// The unique ID of IOCP driver.
168+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169+
pub struct PortId(usize);
170+
171+
impl PortId {
172+
/// Post raw entry to IOCP.
173+
pub fn post_raw(&self, transferred: u32, key: usize, optr: *mut OVERLAPPED) -> io::Result<()> {
174+
syscall!(
175+
BOOL,
176+
PostQueuedCompletionStatus(iocp_port()?.as_raw_handle() as _, transferred, key, optr)
177+
)?;
178+
Ok(())
179+
}
180+
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ impl CompletionPort {
9191
pub fn poll(
9292
&self,
9393
timeout: Option<Duration>,
94-
current_driver: Option<usize>,
95-
) -> io::Result<impl Iterator<Item = (usize, Entry)>> {
94+
current_driver: Option<PortId>,
95+
) -> io::Result<impl Iterator<Item = (PortId, Entry)>> {
9696
const DEFAULT_CAPACITY: usize = 1024;
9797

9898
let mut entries = ArrayVec::<OVERLAPPED_ENTRY, { DEFAULT_CAPACITY }>::new();
@@ -123,16 +123,14 @@ impl CompletionPort {
123123
let overlapped = unsafe { &*overlapped_ptr };
124124
if let Some(current_driver) = current_driver {
125125
if overlapped.driver != current_driver {
126-
syscall!(
127-
BOOL,
128-
PostQueuedCompletionStatus(
129-
overlapped.driver as _,
126+
overlapped
127+
.driver
128+
.post_raw(
130129
entry.dwNumberOfBytesTransferred,
131130
entry.lpCompletionKey,
132-
entry.lpOverlapped
131+
entry.lpOverlapped,
133132
)
134-
)
135-
.ok();
133+
.ok();
136134
}
137135
}
138136
let res = if matches!(

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use std::{io, os::windows::io::AsRawHandle, sync::Arc, time::Duration};
22

3+
use windows_sys::Win32::{
4+
Foundation::HANDLE,
5+
System::IO::{PostQueuedCompletionStatus, OVERLAPPED},
6+
};
7+
38
use super::CompletionPort;
4-
use crate::{Entry, Overlapped, RawFd};
9+
use crate::{syscall, Entry, Overlapped, RawFd};
510

611
pub struct Port {
712
port: Arc<CompletionPort>,
@@ -14,8 +19,8 @@ impl Port {
1419
})
1520
}
1621

17-
pub fn id(&self) -> usize {
18-
self.port.as_raw_handle() as _
22+
pub fn id(&self) -> PortId {
23+
PortId(self.port.as_raw_handle() as _)
1924
}
2025

2126
pub fn attach(&mut self, fd: RawFd) -> io::Result<()> {
@@ -55,3 +60,18 @@ impl PortHandle {
5560
self.port.post(res, optr)
5661
}
5762
}
63+
64+
/// The unique ID of IOCP driver.
65+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66+
pub struct PortId(HANDLE);
67+
68+
impl PortId {
69+
/// Post raw entry to IOCP.
70+
pub fn post_raw(&self, transferred: u32, key: usize, optr: *mut OVERLAPPED) -> io::Result<()> {
71+
syscall!(
72+
BOOL,
73+
PostQueuedCompletionStatus(self.0, transferred, key, optr)
74+
)?;
75+
Ok(())
76+
}
77+
}

compio-driver/src/iocp/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub struct Overlapped<T: ?Sized> {
294294
/// The base [`OVERLAPPED`].
295295
pub base: OVERLAPPED,
296296
/// The unique ID of created driver.
297-
pub driver: usize,
297+
pub driver: cp::PortId,
298298
/// The registered user defined data.
299299
pub user_data: usize,
300300
/// The opcode.
@@ -303,7 +303,7 @@ pub struct Overlapped<T: ?Sized> {
303303
}
304304

305305
impl<T> Overlapped<T> {
306-
pub(crate) fn new(driver: usize, user_data: usize, op: T) -> Self {
306+
pub(crate) fn new(driver: cp::PortId, user_data: usize, op: T) -> Self {
307307
Self {
308308
base: unsafe { std::mem::zeroed() },
309309
driver,
@@ -326,7 +326,7 @@ pub(crate) struct RawOp {
326326
}
327327

328328
impl RawOp {
329-
pub(crate) fn new(driver: usize, user_data: usize, op: impl OpCode + 'static) -> Self {
329+
pub(crate) fn new(driver: cp::PortId, user_data: usize, op: impl OpCode + 'static) -> Self {
330330
let op = Overlapped::new(driver, user_data, op);
331331
let op = Box::new(op) as Box<Overlapped<dyn OpCode>>;
332332
Self {

0 commit comments

Comments
 (0)