Trait polling::os::iocp::PollerIocpExt
source · pub trait PollerIocpExt: PollerSealed {
// Required methods
fn post(&self, packet: CompletionPacket) -> Result<()>;
unsafe fn add_waitable(
&self,
handle: impl AsRawWaitable,
interest: Event,
mode: PollMode,
) -> Result<()>;
fn modify_waitable(
&self,
handle: impl AsWaitable,
interest: Event,
mode: PollMode,
) -> Result<()>;
fn remove_waitable(&self, handle: impl AsWaitable) -> Result<()>;
}
Expand description
Extension trait for the Poller
type that provides functionality specific to IOCP-based
platforms.
Required Methods§
sourcefn post(&self, packet: CompletionPacket) -> Result<()>
fn post(&self, packet: CompletionPacket) -> Result<()>
Post a new Event
to the poller.
§Examples
use polling::{Poller, Event, Events};
use polling::os::iocp::{CompletionPacket, PollerIocpExt};
use std::thread;
use std::sync::Arc;
use std::time::Duration;
// Spawn a thread to wake us up after 100ms.
let poller = Arc::new(Poller::new()?);
thread::spawn({
let poller = poller.clone();
move || {
let packet = CompletionPacket::new(Event::readable(0));
thread::sleep(Duration::from_millis(100));
poller.post(packet).unwrap();
}
});
// Wait for the event.
let mut events = Events::new();
poller.wait(&mut events, None)?;
assert_eq!(events.len(), 1);
sourceunsafe fn add_waitable(
&self,
handle: impl AsRawWaitable,
interest: Event,
mode: PollMode,
) -> Result<()>
unsafe fn add_waitable( &self, handle: impl AsRawWaitable, interest: Event, mode: PollMode, ) -> Result<()>
Add a waitable handle to this poller.
Some handles in Windows are “waitable”, which means that they emit a “readiness” signal after some event occurs. This function can be used to wait for such events to occur on a handle. This function can be used in addition to regular socket polling.
Waitable objects include the following:
- Console inputs
- Waitable events
- Mutexes
- Processes
- Semaphores
- Threads
- Timer
Once the object has been signalled, the poller will emit the interest
event.
§Safety
The added handle must not be dropped before it is deleted.
§Examples
use polling::{Poller, Event, Events, PollMode};
use polling::os::iocp::PollerIocpExt;
use std::process::Command;
// Spawn a new process.
let mut child = Command::new("echo")
.arg("Hello, world!")
.spawn()
.unwrap();
// Create a new poller.
let poller = Poller::new().unwrap();
// Add the child process to the poller.
unsafe {
poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap();
}
// Wait for the child process to exit.
let mut events = Events::new();
poller.wait(&mut events, None).unwrap();
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap(), Event::all(0));
sourcefn modify_waitable(
&self,
handle: impl AsWaitable,
interest: Event,
mode: PollMode,
) -> Result<()>
fn modify_waitable( &self, handle: impl AsWaitable, interest: Event, mode: PollMode, ) -> Result<()>
Modify an existing waitable handle.
This function can be used to change the emitted event and/or mode of an existing waitable
handle. The handle must have been previously added to the poller using add_waitable
.
§Examples
use polling::{Poller, Event, Events, PollMode};
use polling::os::iocp::PollerIocpExt;
use std::process::Command;
// Spawn a new process.
let mut child = Command::new("echo")
.arg("Hello, world!")
.spawn()
.unwrap();
// Create a new poller.
let poller = Poller::new().unwrap();
// Add the child process to the poller.
unsafe {
poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap();
}
// Wait for the child process to exit.
let mut events = Events::new();
poller.wait(&mut events, None).unwrap();
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap(), Event::all(0));
// Modify the waitable handle.
poller.modify_waitable(&child, Event::readable(0), PollMode::Oneshot).unwrap();
sourcefn remove_waitable(&self, handle: impl AsWaitable) -> Result<()>
fn remove_waitable(&self, handle: impl AsWaitable) -> Result<()>
Remove a waitable handle from this poller.
This function can be used to remove a waitable handle from the poller. The handle must
have been previously added to the poller using add_waitable
.
§Examples
use polling::{Poller, Event, Events, PollMode};
use polling::os::iocp::PollerIocpExt;
use std::process::Command;
// Spawn a new process.
let mut child = Command::new("echo")
.arg("Hello, world!")
.spawn()
.unwrap();
// Create a new poller.
let poller = Poller::new().unwrap();
// Add the child process to the poller.
unsafe {
poller.add_waitable(&child, Event::all(0), PollMode::Oneshot).unwrap();
}
// Wait for the child process to exit.
let mut events = Events::new();
poller.wait(&mut events, None).unwrap();
assert_eq!(events.len(), 1);
assert_eq!(events.iter().next().unwrap(), Event::all(0));
// Remove the waitable handle.
poller.remove_waitable(&child).unwrap();