[−][src]Struct gaea::os::Awakener
Awakener allows cross-thread waking of OsQueue
.
When created it will cause events with Ready::READABLE
and the provided
id
if wake
is called, possibly from another thread.
Notes
The Awakener
needs to be kept alive as long as wake up notifications are
required. This is due to an implementation detail where if all copies of the
Awakener
are dropped it will also drop all wake up notifications from the
system queue, including wake up notifications that have been added before
the Awakener
that was dropped, resulting the OsQueue
not being woken
up.
Only a single Awakener
should active per OsQueue
, the Awakener
can
be cloned using try_clone
if more are needed. What happens if multiple
Awakener
s are registered with the same OsQueue
is undefined.
Implementation notes
On platforms that support kqueue this will use the EVFILT_USER
event
filter, see implementation notes of the os
module to see what platform
supports kqueue. On Linux it uses eventfd.
Examples
Wake an OsQueue
from another thread.
use std::io; use std::thread; use std::time::Duration; use gaea::{event, poll}; use gaea::event::{Event, Ready}; use gaea::os::{OsQueue, Awakener}; const WAKE_ID: event::Id = event::Id(10); let mut os_queue = OsQueue::new()?; let mut events = Vec::new(); let awakener = Awakener::new(&mut os_queue, WAKE_ID)?; // We need to keep the Awakener alive, so we'll create a clone for the // thread we create below. let awakener1 = awakener.try_clone()?; let handle = thread::spawn(move || { // Working hard, or hardly working? thread::sleep(Duration::from_millis(500)); // Now we'll wake the queue on the other thread. awakener1.wake().expect("unable to wake"); }); // On our current thread we'll poll for events, without a timeout. poll::<_, io::Error>(&mut [&mut os_queue], &mut events, None)?; // After about 500 milliseconds we should we awoken by the other thread we // started, getting a single event. assert_eq!(events.len(), 1); assert_eq!(events[0], Event::new(WAKE_ID, Ready::READABLE));
Methods
impl Awakener
[src]
pub fn new(os_queue: &mut OsQueue, id: Id) -> Result<Awakener>
[src]
Create a new Awakener
.
pub fn try_clone(&self) -> Result<Awakener>
[src]
Attempts to clone the Awakener
.
pub fn wake(&self) -> Result<()>
[src]
Wake up the OsQueue
associated with this Awakener
.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,