Struct mio::Waker

source ·
pub struct Waker { /* private fields */ }
Expand description

Waker allows cross-thread waking of Poll.

When created it will cause events with readable readiness and the provided token if wake is called, possibly from another thread.

Notes

Waker events are only guaranteed to be delivered while the Waker value is alive.

Only a single Waker can be active per Poll, if multiple threads need access to the Waker it can be shared via for example an Arc. What happens if multiple Wakers are registered with the same Poll is unspecified.

Implementation notes

On platforms that support kqueue this will use the EVFILT_USER event filter, see implementation notes of Poll to see what platforms support kqueue. On Linux it uses eventfd.

Examples

Wake a Poll instance from another thread.

use std::thread;
use std::time::Duration;
use std::sync::Arc;

use mio::{Events, Token, Poll, Waker};

const WAKE_TOKEN: Token = Token(10);

let mut poll = Poll::new()?;
let mut events = Events::with_capacity(2);

let waker = Arc::new(Waker::new(poll.registry(), WAKE_TOKEN)?);

// We need to keep the Waker alive, so we'll create a clone for the
// thread we create below.
let waker1 = waker.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.
    waker1.wake().expect("unable to wake");
});

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

// After about 500 milliseconds we should be awoken by the other thread and
// get a single event.
assert!(!events.is_empty());
let waker_event = events.iter().next().unwrap();
assert!(waker_event.is_readable());
assert_eq!(waker_event.token(), WAKE_TOKEN);

Implementations§

source§

impl Waker

source

pub fn new(registry: &Registry, token: Token) -> Result<Waker>

Create a new Waker.

source

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

Wake up the Poll associated with this Waker.

Trait Implementations§

source§

impl Debug for Waker

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Waker

§

impl Send for Waker

§

impl Sync for Waker

§

impl Unpin for Waker

§

impl UnwindSafe for Waker

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.