[−][src]Struct mio::prelude::EventLoop
Single threaded IO event loop.
Methods
impl<H: Handler> EventLoop<H>
[src]
pub fn new() -> Result<EventLoop<H>>
[src]
Initializes a new event loop using default configuration settings. The event loop will not be running yet.
pub fn configured(config: EventLoopConfig) -> Result<EventLoop<H>>
[src]
pub fn channel(&self) -> Sender<H::Message>
[src]
Returns a sender that allows sending messages to the event loop in a thread-safe way, waking up the event loop if needed.
Example
use std::thread; use mio::{EventLoop, Handler}; struct MyHandler; impl Handler for MyHandler { type Timeout = (); type Message = u32; fn notify(&mut self, event_loop: &mut EventLoop<MyHandler>, msg: u32) { assert_eq!(msg, 123); event_loop.shutdown(); } } let mut event_loop = EventLoop::new().unwrap(); let sender = event_loop.channel(); // Send the notification from another thread thread::spawn(move || { let _ = sender.send(123); }); let _ = event_loop.run(&mut MyHandler);
Implementation Details
Each EventLoop contains a lock-free queue with a pre-allocated buffer size. The size can be changed by modifying EventLoopConfig.notify_capacity. When a message is sent to the EventLoop, it is first pushed on to the queue. Then, if the EventLoop is currently running, an atomic flag is set to indicate that the next loop iteration should be started without waiting.
If the loop is blocked waiting for IO events, then it is woken up. The strategy for waking up the event loop is platform dependent. For example, on a modern Linux OS, eventfd is used. On older OSes, a pipe is used.
The strategy of setting an atomic flag if the event loop is not already sleeping allows avoiding an expensive wakeup operation if at all possible.
pub fn timeout_ms(
&mut self,
token: H::Timeout,
delay: u64
) -> TimerResult<Timeout>
[src]
&mut self,
token: H::Timeout,
delay: u64
) -> TimerResult<Timeout>
Schedules a timeout after the requested time interval. When the duration has been reached, Handler::timeout will be invoked passing in the supplied token.
Returns a handle to the timeout that can be used to cancel the timeout using #clear_timeout.
Example
use mio::{EventLoop, Handler}; struct MyHandler; impl Handler for MyHandler { type Timeout = u32; type Message = (); fn timeout(&mut self, event_loop: &mut EventLoop<MyHandler>, timeout: u32) { assert_eq!(timeout, 123); event_loop.shutdown(); } } let mut event_loop = EventLoop::new().unwrap(); let timeout = event_loop.timeout_ms(123, 300).unwrap(); let _ = event_loop.run(&mut MyHandler);
pub fn clear_timeout(&mut self, timeout: Timeout) -> bool
[src]
If the supplied timeout has not been triggered, cancel it such that it will not be triggered in the future.
pub fn shutdown(&mut self)
[src]
Tells the event loop to exit after it is done handling all events in the current iteration.
pub fn is_running(&self) -> bool
[src]
Indicates whether the event loop is currently running. If it's not it has either stopped or is scheduled to stop on the next tick.
pub fn register<E: ?Sized>(
&mut self,
io: &E,
token: Token,
interest: EventSet,
opt: PollOpt
) -> Result<()> where
E: Evented,
[src]
&mut self,
io: &E,
token: Token,
interest: EventSet,
opt: PollOpt
) -> Result<()> where
E: Evented,
Registers an IO handle with the event loop.
pub fn reregister<E: ?Sized>(
&mut self,
io: &E,
token: Token,
interest: EventSet,
opt: PollOpt
) -> Result<()> where
E: Evented,
[src]
&mut self,
io: &E,
token: Token,
interest: EventSet,
opt: PollOpt
) -> Result<()> where
E: Evented,
Re-Registers an IO handle with the event loop.
pub fn run(&mut self, handler: &mut H) -> Result<()>
[src]
Keep spinning the event loop indefinitely, and notify the handler whenever any of the registered handles are ready.
pub fn deregister<E: ?Sized>(&mut self, io: &E) -> Result<()> where
E: Evented,
[src]
E: Evented,
Deregisters an IO handle with the event loop.
pub fn run_once(
&mut self,
handler: &mut H,
timeout_ms: Option<usize>
) -> Result<()>
[src]
&mut self,
handler: &mut H,
timeout_ms: Option<usize>
) -> Result<()>
Spin the event loop once, with a timeout of one second, and notify the handler if any of the registered handles become ready during that time.
Trait Implementations
impl<H: Handler> Sync for EventLoop<H>
[src]
impl<H: Handler> Drop for EventLoop<H>
[src]
impl<H: Debug + Handler> Debug for EventLoop<H> where
H::Timeout: Debug,
H::Message: Debug,
[src]
H::Timeout: Debug,
H::Message: Debug,
Auto Trait Implementations
impl<H> Send for EventLoop<H> where
<H as Handler>::Message: Send,
<H as Handler>::Timeout: Send,
<H as Handler>::Message: Send,
<H as Handler>::Timeout: Send,
impl<H> Unpin for EventLoop<H> where
<H as Handler>::Timeout: Unpin,
<H as Handler>::Timeout: Unpin,
impl<H> !UnwindSafe for EventLoop<H>
impl<H> !RefUnwindSafe for EventLoop<H>
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> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
ⓘImportant traits for &'_ mut Wfn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,