|
| 1 | +#[cfg(feature = "once_cell_try")] |
| 2 | +use std::sync::OnceLock; |
| 3 | +use std::{ |
| 4 | + io, |
| 5 | + os::windows::io::{AsRawHandle, RawHandle}, |
| 6 | + time::Duration, |
| 7 | +}; |
| 8 | + |
| 9 | +use compio_log::*; |
| 10 | +#[cfg(not(feature = "once_cell_try"))] |
| 11 | +use once_cell::sync::OnceCell as OnceLock; |
| 12 | +use windows_sys::Win32::System::IO::PostQueuedCompletionStatus; |
| 13 | + |
| 14 | +use super::CompletionPort; |
| 15 | +use crate::{syscall, Entry, Overlapped, RawFd}; |
| 16 | + |
| 17 | +struct GlobalPort { |
| 18 | + port: CompletionPort, |
| 19 | +} |
| 20 | + |
| 21 | +impl GlobalPort { |
| 22 | + pub fn new() -> io::Result<Self> { |
| 23 | + Ok(Self { |
| 24 | + port: CompletionPort::new()?, |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + pub fn attach(&self, fd: RawFd) -> io::Result<()> { |
| 29 | + self.port.attach(fd) |
| 30 | + } |
| 31 | + |
| 32 | + pub fn post<T: ?Sized>( |
| 33 | + &self, |
| 34 | + res: io::Result<usize>, |
| 35 | + optr: *mut Overlapped<T>, |
| 36 | + ) -> io::Result<()> { |
| 37 | + self.port.post(res, optr) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> { |
| 41 | + self.port.post_raw(optr) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl AsRawHandle for GlobalPort { |
| 46 | + fn as_raw_handle(&self) -> RawHandle { |
| 47 | + self.port.as_raw_handle() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +static IOCP_PORT: OnceLock<GlobalPort> = OnceLock::new(); |
| 52 | + |
| 53 | +#[inline] |
| 54 | +fn iocp_port() -> io::Result<&'static GlobalPort> { |
| 55 | + IOCP_PORT.get_or_try_init(GlobalPort::new) |
| 56 | +} |
| 57 | + |
| 58 | +fn iocp_start() -> io::Result<()> { |
| 59 | + let port = iocp_port()?; |
| 60 | + std::thread::spawn(move || { |
| 61 | + instrument!(compio_log::Level::TRACE, "iocp_start"); |
| 62 | + loop { |
| 63 | + for entry in port.port.poll_raw(None)? { |
| 64 | + // Any thin pointer is OK because we don't use the type of opcode. |
| 65 | + let overlapped_ptr: *mut Overlapped<()> = entry.lpOverlapped.cast(); |
| 66 | + let overlapped = unsafe { &*overlapped_ptr }; |
| 67 | + if let Err(_e) = syscall!( |
| 68 | + BOOL, |
| 69 | + PostQueuedCompletionStatus( |
| 70 | + overlapped.driver as _, |
| 71 | + entry.dwNumberOfBytesTransferred, |
| 72 | + entry.lpCompletionKey, |
| 73 | + entry.lpOverlapped, |
| 74 | + ) |
| 75 | + ) { |
| 76 | + error!( |
| 77 | + "fail to dispatch entry ({}, {}, {:p}) to driver {:p}: {:?}", |
| 78 | + entry.dwNumberOfBytesTransferred, |
| 79 | + entry.lpCompletionKey, |
| 80 | + entry.lpOverlapped, |
| 81 | + overlapped.driver, |
| 82 | + _e |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + #[allow(unreachable_code)] |
| 88 | + io::Result::Ok(()) |
| 89 | + }); |
| 90 | + Ok(()) |
| 91 | +} |
| 92 | + |
| 93 | +static IOCP_INIT_ONCE: OnceLock<()> = OnceLock::new(); |
| 94 | + |
| 95 | +pub struct Port { |
| 96 | + port: CompletionPort, |
| 97 | + global_port: &'static GlobalPort, |
| 98 | +} |
| 99 | + |
| 100 | +impl Port { |
| 101 | + pub fn new() -> io::Result<Self> { |
| 102 | + IOCP_INIT_ONCE.get_or_try_init(iocp_start)?; |
| 103 | + |
| 104 | + Ok(Self { |
| 105 | + port: CompletionPort::new()?, |
| 106 | + global_port: iocp_port()?, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + pub fn attach(&mut self, fd: RawFd) -> io::Result<()> { |
| 111 | + self.global_port.attach(fd) |
| 112 | + } |
| 113 | + |
| 114 | + pub fn handle(&self) -> PortHandle { |
| 115 | + PortHandle::new(self.global_port) |
| 116 | + } |
| 117 | + |
| 118 | + pub fn poll(&self, timeout: Option<Duration>) -> io::Result<impl Iterator<Item = Entry> + '_> { |
| 119 | + self.port.poll(timeout, None) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl AsRawHandle for Port { |
| 124 | + fn as_raw_handle(&self) -> RawHandle { |
| 125 | + self.port.as_raw_handle() |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +pub struct PortHandle { |
| 130 | + port: &'static GlobalPort, |
| 131 | +} |
| 132 | + |
| 133 | +impl PortHandle { |
| 134 | + fn new(port: &'static GlobalPort) -> Self { |
| 135 | + Self { port } |
| 136 | + } |
| 137 | + |
| 138 | + pub fn post<T: ?Sized>( |
| 139 | + &self, |
| 140 | + res: io::Result<usize>, |
| 141 | + optr: *mut Overlapped<T>, |
| 142 | + ) -> io::Result<()> { |
| 143 | + self.port.post(res, optr) |
| 144 | + } |
| 145 | + |
| 146 | + pub fn post_raw<T: ?Sized>(&self, optr: *const Overlapped<T>) -> io::Result<()> { |
| 147 | + self.port.post_raw(optr) |
| 148 | + } |
| 149 | +} |
0 commit comments