Struct mio::deprecated::EventLoop
[−]
[src]
pub struct EventLoop<H: Handler> { /* fields omitted */ }
Single threaded IO event loop.
Methods
impl<H: Handler> EventLoop<H>
[src]
fn new() -> Result<EventLoop<H>>
Constructs a new EventLoop
using the default configuration values.
The EventLoop
will not be running.
fn channel(&self) -> Sender<H::Message>
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::deprecated::{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.
fn timeout(&mut self, token: H::Timeout, delay: Duration) -> Result<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::deprecated::{EventLoop, Handler}; use std::time::Duration; 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(123, Duration::from_millis(300)).unwrap(); let _ = event_loop.run(&mut MyHandler);
fn clear_timeout(&mut self, timeout: &Timeout) -> bool
If the supplied timeout has not been triggered, cancel it such that it will not be triggered in the future.
fn shutdown(&mut self)
Tells the event loop to exit after it is done handling all events in the current iteration.
fn is_running(&self) -> bool
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.
fn register<E: ?Sized>(&mut self,
io: &E,
token: Token,
interest: Ready,
opt: PollOpt)
-> Result<()> where E: Evented
io: &E,
token: Token,
interest: Ready,
opt: PollOpt)
-> Result<()> where E: Evented
Registers an IO handle with the event loop.
fn reregister<E: ?Sized>(&mut self,
io: &E,
token: Token,
interest: Ready,
opt: PollOpt)
-> Result<()> where E: Evented
io: &E,
token: Token,
interest: Ready,
opt: PollOpt)
-> Result<()> where E: Evented
Re-Registers an IO handle with the event loop.
fn run(&mut self, handler: &mut H) -> Result<()>
Keep spinning the event loop indefinitely, and notify the handler whenever any of the registered handles are ready.
fn deregister<E: ?Sized>(&mut self, io: &E) -> Result<()> where E: Evented
Deregisters an IO handle with the event loop.
Both kqueue and epoll will automatically clear any pending events when closing a file descriptor (socket). In that case, this method does not need to be called prior to dropping a connection from the slab.
Warning: kqueue effectively builds in deregister when using edge-triggered mode with
oneshot. Calling deregister()
on the socket will cause a TcpStream error.
fn run_once(&mut self, handler: &mut H, timeout: Option<Duration>) -> Result<()>
Spin the event loop once, with a given timeout (forever if None
),
and notify the handler if any of the registered handles become ready
during that time.