-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add API to hook into Windows IOCP loop #1345
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
use std::{ | ||
sync::{Arc, Mutex}, | ||
time::Duration, | ||
io, | ||
fmt | ||
}; | ||
|
||
use winapi::shared::winerror; | ||
use miow::{ | ||
Overlapped, | ||
iocp::{ | ||
CompletionPort, | ||
CompletionStatus | ||
} | ||
}; | ||
|
||
use slab::Slab; | ||
|
||
use crate::{ | ||
Token, | ||
sys::windows::{ | ||
Event, | ||
afd, | ||
selector::AfdCompletionPortEventHandler, | ||
}, | ||
}; | ||
|
||
#[cfg(feature = "os-util")] | ||
use crate::sys::windows::selector::RawHandleCompletionHandler; | ||
|
||
pub trait IocpHandler: fmt::Debug + Send + Sync + 'static { | ||
fn handle_completion(&mut self, status: &CompletionStatus) -> Option<Event>; | ||
fn on_poll_finished(&mut self) { } | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum RegisteredHandler { | ||
AfdHandler(AfdCompletionPortEventHandler), | ||
WakerHandler(WakerHandler), | ||
#[cfg(feature = "os-util")] | ||
RawHandleHandler(RawHandleCompletionHandler) | ||
} | ||
|
||
impl From<AfdCompletionPortEventHandler> for RegisteredHandler { | ||
fn from(h: AfdCompletionPortEventHandler) -> Self { | ||
RegisteredHandler::AfdHandler(h) | ||
} | ||
} | ||
|
||
impl From<WakerHandler> for RegisteredHandler { | ||
fn from(h: WakerHandler) -> Self { | ||
RegisteredHandler::WakerHandler(h) | ||
} | ||
} | ||
|
||
#[cfg(feature = "os-util")] | ||
impl From<RawHandleCompletionHandler> for RegisteredHandler { | ||
fn from(h: RawHandleCompletionHandler) -> Self { | ||
RegisteredHandler::RawHandleHandler(h) | ||
} | ||
} | ||
|
||
impl IocpHandler for RegisteredHandler { | ||
fn handle_completion(&mut self, status: &CompletionStatus) -> Option<Event> { | ||
match self { | ||
RegisteredHandler::AfdHandler(handler) => handler.handle_completion(status), | ||
RegisteredHandler::WakerHandler(handler) => handler.handle_completion(status), | ||
#[cfg(feature = "os-util")] | ||
RegisteredHandler::RawHandleHandler(handler) => handler.handle_completion(status), | ||
} | ||
} | ||
|
||
fn on_poll_finished(&mut self) { | ||
match self { | ||
RegisteredHandler::AfdHandler(handler) => handler.on_poll_finished(), | ||
RegisteredHandler::WakerHandler(handler) => handler.on_poll_finished(), | ||
#[cfg(feature = "os-util")] | ||
RegisteredHandler::RawHandleHandler(handler) => handler.on_poll_finished(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct IocpWaker { | ||
token: usize, | ||
iocp_registry: Arc<IocpHandlerRegistry>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct WakerHandler { | ||
external_token: Token, | ||
} | ||
|
||
impl IocpHandler for WakerHandler { | ||
fn handle_completion(&mut self, _status: &CompletionStatus) -> Option<Event> { | ||
Some(Event { | ||
flags: afd::POLL_RECEIVE, | ||
data: self.external_token.0 as u64 | ||
}) | ||
} | ||
} | ||
|
||
impl IocpWaker { | ||
pub fn post(&self, bytes: u32, overlapped: *mut Overlapped) -> io::Result<()> { | ||
self.iocp_registry.cp.post(CompletionStatus::new(bytes, self.token, overlapped)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct IocpHandlerRegistry { | ||
cp: CompletionPort, | ||
handlers: Mutex<Slab<RegisteredHandler>>, | ||
} | ||
|
||
impl IocpHandlerRegistry { | ||
pub fn new() -> io::Result<Self> { | ||
CompletionPort::new(0).map(|cp| | ||
Self { | ||
cp, | ||
handlers: Mutex::new(Slab::new()) | ||
}) | ||
} | ||
|
||
pub fn register_waker(self: Arc<Self>, token: Token) -> IocpWaker { | ||
let handler = WakerHandler { | ||
external_token: token | ||
}; | ||
let slab_token = self.handlers.lock().unwrap() | ||
.insert(handler.into()); | ||
IocpWaker { | ||
token: slab_token, | ||
iocp_registry: self | ||
} | ||
} | ||
|
||
pub fn handle_pending_events(&self, | ||
statuses: &mut [CompletionStatus], | ||
mut events: Option<&mut Vec<Event>>, | ||
timeout: Option<Duration>) -> io::Result<usize> { | ||
let result = match self.cp.get_many(statuses, timeout) { | ||
Ok(iocp_events) => { | ||
let mut num_events = 0; | ||
let mut handlers = self.handlers.lock().unwrap(); | ||
for status in iocp_events { | ||
let key = status.token(); | ||
if let Some(handler) = handlers.get_mut(key) { | ||
if let Some(event) = handler.handle_completion(status) { | ||
if let Some(events) = &mut events { | ||
events.push(event); | ||
} | ||
num_events += 1; | ||
} | ||
} | ||
} | ||
|
||
Ok(num_events) | ||
}, | ||
|
||
Err(ref e) if e.raw_os_error() == Some(winerror::WAIT_TIMEOUT as i32) => Ok(0), | ||
|
||
Err(e) => Err(e) | ||
}; | ||
|
||
for (_, handler) in self.handlers.lock().unwrap().iter_mut() { | ||
handler.on_poll_finished(); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
cfg_any_os_util! { | ||
use std::os::windows::io::AsRawHandle; | ||
|
||
impl IocpHandlerRegistry { | ||
pub(crate) fn register_handle<T>(&self, handle: &T, handler: RegisteredHandler) -> io::Result<()> | ||
where T: AsRawHandle + ?Sized { | ||
let token = self.handlers.lock().unwrap().insert(handler); | ||
self.cp.add_handle(token, handle) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that
Binding
only hasregister_handle
, how would I implement de/re-register?Or this is that part simply not implemented yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not implemented yet because I'm unsure how that should/could be done as in Windows, there is no way of removing a handle from an IOCP except by closing that handle. So the only thing we should do here is to remove the handler from the list of IO handlers, which should be done by a Drop implementation of that type. reregistration would only support changing the token; I will add both functionalities.