Awakener

Struct Awakener 

Source
pub struct Awakener { /* private fields */ }
Expand description

Awakener allows cross-thread waking of a Poller instance.

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 Poller instance not being woken up.

Only a single Awakener should active per Poller instance, the Awakener can be cloned using try_clone if more are needed. What happens if multiple Awakeners are registered with the same Poller instance is undefined.

§Examples

Wake a Poller instance from another thread.

use std::thread;
use std::time::Duration;

use mio_st::event::{Events, EventedId, Ready};
use mio_st::poll::{Poller, Awakener};

const WAKE_ID: EventedId = EventedId(10);

let mut poller = Poller::new()?;
let mut events = Events::new();

let awakener = Awakener::new(&mut poller, 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 poller instance on the other thread.
    awakener1.wake().expect("unable to wake");
});

// On our current thread we'll poll for events, without a timeout.
poller.poll(&mut events, None)?;

// After about 500 milliseconds we should we awoken by the other thread,
// getting a single event.
assert_eq!(events.len(), 1);
let event = (&mut events).next().unwrap();
assert_eq!(event.id(), WAKE_ID);
assert_eq!(event.readiness(), Ready::READABLE);

Implementations§

Source§

impl Awakener

Source

pub fn new(poller: &mut Poller, id: EventedId) -> Result<Awakener>

Create a new Awakener.

Source

pub fn try_clone(&self) -> Result<Awakener>

Attempts to clone the Awakener.

Source

pub fn wake(&self) -> Result<()>

Wake up the Poller instance associated with this Awakener.

Trait Implementations§

Source§

impl Debug for Awakener

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.