2016-09-02 11:07:52 -07:00
|
|
|
//! The core reactor driving all I/O
|
|
|
|
|
//!
|
|
|
|
|
//! This module contains the `Core` type which is the reactor for all I/O
|
2017-10-24 17:20:46 -07:00
|
|
|
//! happening in `tokio-core`. This reactor (or event loop) is used to drive I/O
|
|
|
|
|
//! resources.
|
2016-09-02 11:07:52 -07:00
|
|
|
|
2017-03-06 12:15:29 -08:00
|
|
|
use std::fmt;
|
2016-08-20 23:23:16 -07:00
|
|
|
use std::io::{self, ErrorKind};
|
2017-11-17 12:51:23 -08:00
|
|
|
use std::sync::{Arc, Weak, RwLock};
|
2016-08-20 23:23:16 -07:00
|
|
|
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
2017-11-17 12:51:23 -08:00
|
|
|
use std::time::{Duration};
|
2016-08-20 23:23:16 -07:00
|
|
|
|
2017-10-25 10:54:54 -07:00
|
|
|
use futures::{Future, Async};
|
2017-11-17 12:51:23 -08:00
|
|
|
use futures::executor::{self, Notify};
|
|
|
|
|
use futures::task::{AtomicTask};
|
2016-08-20 23:23:16 -07:00
|
|
|
use mio;
|
2017-02-05 17:06:57 -08:00
|
|
|
use mio::event::Evented;
|
2016-08-20 23:23:16 -07:00
|
|
|
use slab::Slab;
|
|
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
mod io_token;
|
2016-08-20 23:23:16 -07:00
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
mod poll_evented;
|
2016-09-07 16:11:19 -07:00
|
|
|
pub use self::poll_evented::PollEvented;
|
2016-09-02 11:07:52 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Global counter used to assign unique IDs to reactor instances.
|
2016-08-20 23:23:16 -07:00
|
|
|
static NEXT_LOOP_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
|
|
|
|
|
/// An event loop.
|
|
|
|
|
///
|
|
|
|
|
/// The event loop is the main source of blocking in an application which drives
|
|
|
|
|
/// all other I/O events and notifications happening. Each event loop can have
|
|
|
|
|
/// multiple handles pointing to it, each of which can then be used to create
|
|
|
|
|
/// various I/O objects to interact with the event loop in interesting ways.
|
2016-09-02 11:07:52 -07:00
|
|
|
pub struct Core {
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Reuse the `mio::Events` value across calls to poll.
|
2016-08-20 23:23:16 -07:00
|
|
|
events: mio::Events,
|
2017-01-10 00:02:53 -08:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// State shared between the reactor and the handles.
|
|
|
|
|
inner: Arc<Inner>,
|
2016-08-31 00:19:29 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Used for determining when the future passed to `run` is ready. Once the
|
|
|
|
|
/// registration is passed to `io` above we never touch it again, just keep
|
|
|
|
|
/// it alive.
|
2016-08-20 23:23:16 -07:00
|
|
|
_future_registration: mio::Registration,
|
2016-08-31 00:19:29 -07:00
|
|
|
future_readiness: Arc<MySetReadiness>,
|
2016-09-07 16:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Inner {
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Unique identifier referencing this reactor.
|
2016-09-07 16:11:19 -07:00
|
|
|
id: usize,
|
2017-11-17 12:51:23 -08:00
|
|
|
|
|
|
|
|
/// The underlying system event queue.
|
2016-09-07 16:11:19 -07:00
|
|
|
io: mio::Poll,
|
|
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Dispatch slabs for I/O and futures events
|
|
|
|
|
io_dispatch: RwLock<Slab<ScheduledIo>>,
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-15 15:15:37 +01:00
|
|
|
/// An unique ID for a Core
|
|
|
|
|
///
|
|
|
|
|
/// An ID by which different cores may be distinguished. Can be compared and used as an index in
|
|
|
|
|
/// a `HashMap`.
|
|
|
|
|
///
|
|
|
|
|
/// The ID is globally unique and never reused.
|
|
|
|
|
#[derive(Clone,Copy,Eq,PartialEq,Hash,Debug)]
|
|
|
|
|
pub struct CoreId(usize);
|
|
|
|
|
|
2016-08-20 23:23:16 -07:00
|
|
|
/// Handle to an event loop, used to construct I/O objects, send messages, and
|
|
|
|
|
/// otherwise interact indirectly with the event loop itself.
|
|
|
|
|
///
|
|
|
|
|
/// Handles can be cloned, and when cloned they will still refer to the
|
|
|
|
|
/// same underlying event loop.
|
|
|
|
|
#[derive(Clone)]
|
2016-09-07 16:11:19 -07:00
|
|
|
pub struct Remote {
|
2016-08-20 23:23:16 -07:00
|
|
|
id: usize,
|
2017-11-17 12:51:23 -08:00
|
|
|
inner: Weak<Inner>,
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A non-sendable handle to an event loop, useful for manufacturing instances
|
|
|
|
|
/// of `LoopData`.
|
|
|
|
|
#[derive(Clone)]
|
2016-09-07 16:11:19 -07:00
|
|
|
pub struct Handle {
|
|
|
|
|
remote: Remote,
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-31 00:19:29 -07:00
|
|
|
struct ScheduledIo {
|
2017-11-17 12:51:23 -08:00
|
|
|
readiness: AtomicUsize,
|
|
|
|
|
reader: AtomicTask,
|
|
|
|
|
writer: AtomicTask,
|
2016-08-31 00:19:29 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-20 23:23:16 -07:00
|
|
|
enum Direction {
|
|
|
|
|
Read,
|
|
|
|
|
Write,
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 00:19:29 -07:00
|
|
|
const TOKEN_FUTURE: mio::Token = mio::Token(1);
|
2016-09-07 16:11:19 -07:00
|
|
|
const TOKEN_START: usize = 2;
|
2016-08-31 00:19:29 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
fn _assert_kinds() {
|
|
|
|
|
fn _assert<T: Send + Sync>() {}
|
|
|
|
|
|
|
|
|
|
_assert::<Handle>();
|
|
|
|
|
_assert::<Remote>();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
impl Core {
|
2016-08-20 23:23:16 -07:00
|
|
|
/// Creates a new event loop, returning any error that happened during the
|
|
|
|
|
/// creation.
|
2016-09-02 11:07:52 -07:00
|
|
|
pub fn new() -> io::Result<Core> {
|
2017-11-17 12:51:23 -08:00
|
|
|
// Create the I/O poller
|
2016-08-20 23:23:16 -07:00
|
|
|
let io = try!(mio::Poll::new());
|
2017-11-17 12:51:23 -08:00
|
|
|
|
|
|
|
|
// Create a registration for unblocking the reactor when the "run"
|
|
|
|
|
// future becomes ready.
|
2017-02-05 17:06:57 -08:00
|
|
|
let future_pair = mio::Registration::new2();
|
|
|
|
|
try!(io.register(&future_pair.0,
|
|
|
|
|
TOKEN_FUTURE,
|
|
|
|
|
mio::Ready::readable(),
|
|
|
|
|
mio::PollOpt::level()));
|
2017-01-10 00:02:53 -08:00
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
Ok(Core {
|
2016-08-30 14:45:29 -07:00
|
|
|
events: mio::Events::with_capacity(1024),
|
2016-08-31 00:19:29 -07:00
|
|
|
_future_registration: future_pair.0,
|
|
|
|
|
future_readiness: Arc::new(MySetReadiness(future_pair.1)),
|
2017-11-17 12:51:23 -08:00
|
|
|
inner: Arc::new(Inner {
|
2016-09-07 16:11:19 -07:00
|
|
|
id: NEXT_LOOP_ID.fetch_add(1, Ordering::Relaxed),
|
|
|
|
|
io: io,
|
2017-11-17 12:51:23 -08:00
|
|
|
io_dispatch: RwLock::new(Slab::with_capacity(1)),
|
|
|
|
|
}),
|
2016-08-20 23:23:16 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
/// Returns a handle to this event loop which cannot be sent across threads
|
|
|
|
|
/// but can be used as a proxy to the event loop itself.
|
2016-08-20 23:23:16 -07:00
|
|
|
///
|
2016-09-07 16:11:19 -07:00
|
|
|
/// Handles are cloneable and clones always refer to the same event loop.
|
|
|
|
|
/// This handle is typically passed into functions that create I/O objects
|
|
|
|
|
/// to bind them to this event loop.
|
2016-09-02 11:07:52 -07:00
|
|
|
pub fn handle(&self) -> Handle {
|
2017-11-17 12:51:23 -08:00
|
|
|
let remote = self.remote();
|
|
|
|
|
Handle { remote }
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
/// Generates a remote handle to this event loop which can be used to spawn
|
|
|
|
|
/// tasks from other threads into this event loop.
|
|
|
|
|
pub fn remote(&self) -> Remote {
|
|
|
|
|
Remote {
|
2017-11-17 12:51:23 -08:00
|
|
|
id: self.inner.id,
|
|
|
|
|
inner: Arc::downgrade(&self.inner),
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Runs a future until completion, driving the event loop while we're
|
|
|
|
|
/// otherwise waiting for the future to complete.
|
|
|
|
|
///
|
|
|
|
|
/// This function will begin executing the event loop and will finish once
|
2017-02-27 11:07:19 +01:00
|
|
|
/// the provided future is resolved. Note that the future argument here
|
2016-08-20 23:23:16 -07:00
|
|
|
/// crucially does not require the `'static` nor `Send` bounds. As a result
|
|
|
|
|
/// the future will be "pinned" to not only this thread but also this stack
|
|
|
|
|
/// frame.
|
|
|
|
|
///
|
2017-03-03 18:38:56 +00:00
|
|
|
/// This function will return the value that the future resolves to once
|
2016-08-20 23:23:16 -07:00
|
|
|
/// the future has finished. If the future never resolves then this function
|
|
|
|
|
/// will never return.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// This method will **not** catch panics from polling the future `f`. If
|
|
|
|
|
/// the future panics then it's the responsibility of the caller to catch
|
|
|
|
|
/// that panic and handle it as appropriate.
|
2016-08-31 00:19:29 -07:00
|
|
|
pub fn run<F>(&mut self, f: F) -> Result<F::Item, F::Error>
|
2016-08-20 23:23:16 -07:00
|
|
|
where F: Future,
|
|
|
|
|
{
|
2017-01-05 20:41:36 +05:30
|
|
|
let mut task = executor::spawn(f);
|
2016-11-11 14:27:49 -08:00
|
|
|
let mut future_fired = true;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
if future_fired {
|
2017-11-17 12:51:23 -08:00
|
|
|
let res = task.poll_future_notify(&self.future_readiness, 0)?;
|
2016-11-11 14:27:49 -08:00
|
|
|
if let Async::Ready(e) = res {
|
|
|
|
|
return Ok(e)
|
|
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
2016-11-11 14:27:49 -08:00
|
|
|
future_fired = self.poll(None);
|
|
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-09 02:27:26 +01:00
|
|
|
/// Performs one iteration of the event loop, blocking on waiting for events
|
|
|
|
|
/// for at most `max_wait` (forever if `None`).
|
|
|
|
|
///
|
|
|
|
|
/// It only makes sense to call this method if you've previously spawned
|
|
|
|
|
/// a future onto this event loop.
|
|
|
|
|
///
|
|
|
|
|
/// `loop { lp.turn(None) }` is equivalent to calling `run` with an
|
|
|
|
|
/// empty future (one that never finishes).
|
|
|
|
|
pub fn turn(&mut self, max_wait: Option<Duration>) {
|
2016-11-11 14:27:49 -08:00
|
|
|
self.poll(max_wait);
|
2016-09-09 02:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-11 14:27:49 -08:00
|
|
|
fn poll(&mut self, max_wait: Option<Duration>) -> bool {
|
|
|
|
|
// Block waiting for an event to happen, peeling out how many events
|
|
|
|
|
// happened.
|
2017-11-17 12:51:23 -08:00
|
|
|
match self.inner.io.poll(&mut self.events, max_wait) {
|
|
|
|
|
Ok(_) => {}
|
2016-11-11 14:27:49 -08:00
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => return false,
|
2017-11-17 12:51:23 -08:00
|
|
|
// TODO: This should return an io::Result instead of panic.
|
2016-11-11 14:27:49 -08:00
|
|
|
Err(e) => panic!("error in poll: {}", e),
|
2017-11-17 12:51:23 -08:00
|
|
|
}
|
2016-11-11 14:27:49 -08:00
|
|
|
|
|
|
|
|
// Process all the events that came in, dispatching appropriately
|
|
|
|
|
let mut fired = false;
|
2016-09-09 02:27:26 +01:00
|
|
|
for i in 0..self.events.len() {
|
|
|
|
|
let event = self.events.get(i).unwrap();
|
|
|
|
|
let token = event.token();
|
2017-02-05 17:06:57 -08:00
|
|
|
trace!("event {:?} {:?}", event.readiness(), event.token());
|
2016-09-09 02:27:26 +01:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
if token == TOKEN_FUTURE {
|
2017-02-05 17:06:57 -08:00
|
|
|
self.future_readiness.0.set_readiness(mio::Ready::empty()).unwrap();
|
2016-11-11 14:27:49 -08:00
|
|
|
fired = true;
|
2016-09-09 02:27:26 +01:00
|
|
|
} else {
|
2017-02-05 17:06:57 -08:00
|
|
|
self.dispatch(token, event.readiness());
|
2016-09-09 02:27:26 +01:00
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
2017-11-17 12:51:23 -08:00
|
|
|
|
2016-11-11 14:27:49 -08:00
|
|
|
return fired
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
fn dispatch(&mut self, token: mio::Token, ready: mio::Ready) {
|
2016-08-31 00:19:29 -07:00
|
|
|
let token = usize::from(token) - TOKEN_START;
|
2017-11-17 12:51:23 -08:00
|
|
|
let io_dispatch = self.inner.io_dispatch.read().unwrap();
|
2016-08-31 00:19:29 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
if let Some(io) = io_dispatch.get(token) {
|
2017-05-19 09:56:35 -07:00
|
|
|
io.readiness.fetch_or(ready2usize(ready), Ordering::Relaxed);
|
2016-08-31 00:19:29 -07:00
|
|
|
if ready.is_writable() {
|
2017-11-17 12:51:23 -08:00
|
|
|
io.writer.notify();
|
2017-05-19 09:56:35 -07:00
|
|
|
}
|
|
|
|
|
if !(ready & (!mio::Ready::writable())).is_empty() {
|
2017-11-17 12:51:23 -08:00
|
|
|
io.reader.notify();
|
2016-09-07 16:11:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-15 15:15:37 +01:00
|
|
|
|
|
|
|
|
/// Get the ID of this loop
|
|
|
|
|
pub fn id(&self) -> CoreId {
|
2017-11-17 12:51:23 -08:00
|
|
|
CoreId(self.inner.id)
|
2017-01-15 15:15:37 +01:00
|
|
|
}
|
2016-09-07 16:11:19 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-06 12:15:29 -08:00
|
|
|
impl fmt::Debug for Core {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.debug_struct("Core")
|
|
|
|
|
.field("id", &self.id())
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
impl Inner {
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Register an I/O resource with the reactor.
|
|
|
|
|
///
|
|
|
|
|
/// The registration token is returned.
|
|
|
|
|
fn add_source(&self, source: &Evented)
|
|
|
|
|
-> io::Result<usize>
|
|
|
|
|
{
|
|
|
|
|
// Acquire a write lock
|
|
|
|
|
let key = self.io_dispatch.write().unwrap()
|
|
|
|
|
.insert(ScheduledIo {
|
|
|
|
|
readiness: AtomicUsize::new(0),
|
|
|
|
|
reader: AtomicTask::new(),
|
|
|
|
|
writer: AtomicTask::new(),
|
|
|
|
|
});
|
|
|
|
|
|
2016-08-20 23:23:16 -07:00
|
|
|
try!(self.io.register(source,
|
2017-10-25 10:54:54 -07:00
|
|
|
mio::Token(TOKEN_START + key),
|
2017-02-05 17:06:57 -08:00
|
|
|
mio::Ready::readable() |
|
|
|
|
|
mio::Ready::writable() |
|
2017-05-21 11:01:48 -06:00
|
|
|
platform::all(),
|
2016-08-20 23:23:16 -07:00
|
|
|
mio::PollOpt::edge()));
|
2017-11-17 12:51:23 -08:00
|
|
|
|
|
|
|
|
Ok(key)
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
2016-11-04 09:12:00 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
fn deregister_source(&self, source: &Evented) -> io::Result<()> {
|
2016-11-04 09:12:00 -07:00
|
|
|
self.io.deregister(source)
|
|
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
fn drop_source(&self, token: usize) {
|
2016-08-20 23:23:16 -07:00
|
|
|
debug!("dropping I/O source: {}", token);
|
2017-11-17 12:51:23 -08:00
|
|
|
self.io_dispatch.write().unwrap().remove(token);
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Registers interest in the I/O resource associated with `token`.
|
|
|
|
|
fn schedule(&self, token: usize, dir: Direction) {
|
2016-08-20 23:23:16 -07:00
|
|
|
debug!("scheduling direction for: {}", token);
|
2017-11-17 12:51:23 -08:00
|
|
|
let io_dispatch = self.io_dispatch.read().unwrap();
|
|
|
|
|
let sched = io_dispatch.get(token).unwrap();
|
|
|
|
|
|
|
|
|
|
let (task, ready) = match dir {
|
|
|
|
|
Direction::Read => (&sched.reader, !mio::Ready::writable()),
|
|
|
|
|
Direction::Write => (&sched.writer, mio::Ready::writable()),
|
2016-08-20 23:23:16 -07:00
|
|
|
};
|
2017-11-17 12:51:23 -08:00
|
|
|
|
|
|
|
|
task.register();
|
|
|
|
|
|
2017-05-19 09:56:35 -07:00
|
|
|
if sched.readiness.load(Ordering::SeqCst) & ready2usize(ready) != 0 {
|
2017-11-17 12:51:23 -08:00
|
|
|
task.notify();
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
impl Remote {
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Return the ID of the represented Core
|
|
|
|
|
pub fn id(&self) -> CoreId {
|
|
|
|
|
CoreId(self.id)
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-17 12:51:23 -08:00
|
|
|
/// Attempts to "promote" this remote to a handle, if possible.
|
|
|
|
|
///
|
|
|
|
|
/// This function is intended for structures which typically work through a
|
|
|
|
|
/// `Remote` but want to optimize runtime when the remote doesn't actually
|
|
|
|
|
/// leave the thread of the original reactor. This will attempt to return a
|
|
|
|
|
/// handle if the `Remote` is on the same thread as the event loop and the
|
|
|
|
|
/// event loop is running.
|
|
|
|
|
///
|
|
|
|
|
/// If this `Remote` has moved to a different thread or if the event loop is
|
|
|
|
|
/// running, then `None` may be returned. If you need to guarantee access to
|
|
|
|
|
/// a `Handle`, then you can call this function and fall back to using
|
|
|
|
|
/// `spawn` above if it returns `None`.
|
|
|
|
|
pub fn handle(&self) -> Option<Handle> {
|
|
|
|
|
let remote = self.clone();
|
|
|
|
|
Some(Handle { remote } )
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
2016-08-31 00:19:29 -07:00
|
|
|
|
2016-09-21 18:27:25 +02:00
|
|
|
/// Spawns a new future into the event loop this remote is associated with.
|
2016-08-31 00:19:29 -07:00
|
|
|
///
|
|
|
|
|
/// This function takes a closure which is executed within the context of
|
|
|
|
|
/// the I/O loop itself. The future returned by the closure will be
|
2017-05-12 17:58:57 +08:00
|
|
|
/// scheduled on the event loop and run to completion.
|
2016-08-31 00:19:29 -07:00
|
|
|
///
|
|
|
|
|
/// Note that while the closure, `F`, requires the `Send` bound as it might
|
|
|
|
|
/// cross threads, the future `R` does not.
|
2017-07-26 17:33:09 +02:00
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// This method will **not** catch panics from polling the future `f`. If
|
|
|
|
|
/// the future panics then it's the responsibility of the caller to catch
|
|
|
|
|
/// that panic and handle it as appropriate.
|
2017-10-25 10:54:54 -07:00
|
|
|
pub(crate) fn run<F>(&self, f: F)
|
|
|
|
|
where F: FnOnce(&Handle) + Send + 'static,
|
2016-08-31 00:19:29 -07:00
|
|
|
{
|
2017-11-17 12:51:23 -08:00
|
|
|
let handle = self.handle().unwrap();
|
|
|
|
|
f(&handle);
|
2017-01-23 20:11:37 -08:00
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-06 12:15:29 -08:00
|
|
|
impl fmt::Debug for Remote {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.debug_struct("Remote")
|
|
|
|
|
.field("id", &self.id())
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 16:11:19 -07:00
|
|
|
impl Handle {
|
|
|
|
|
/// Returns a reference to the underlying remote handle to the event loop.
|
|
|
|
|
pub fn remote(&self) -> &Remote {
|
|
|
|
|
&self.remote
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-15 15:15:37 +01:00
|
|
|
/// Return the ID of the represented Core
|
|
|
|
|
pub fn id(&self) -> CoreId {
|
|
|
|
|
self.remote.id()
|
|
|
|
|
}
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-06 12:15:29 -08:00
|
|
|
impl fmt::Debug for Handle {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.debug_struct("Handle")
|
|
|
|
|
.field("id", &self.id())
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 00:19:29 -07:00
|
|
|
struct MySetReadiness(mio::SetReadiness);
|
|
|
|
|
|
2017-05-16 08:56:30 -07:00
|
|
|
impl Notify for MySetReadiness {
|
|
|
|
|
fn notify(&self, _id: usize) {
|
2016-08-31 00:19:29 -07:00
|
|
|
self.0.set_readiness(mio::Ready::readable())
|
|
|
|
|
.expect("failed to set readiness");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait FnBox: Send + 'static {
|
2016-09-02 11:07:52 -07:00
|
|
|
fn call_box(self: Box<Self>, lp: &Core);
|
2016-08-31 00:19:29 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
impl<F: FnOnce(&Core) + Send + 'static> FnBox for F {
|
|
|
|
|
fn call_box(self: Box<Self>, lp: &Core) {
|
2016-08-31 00:19:29 -07:00
|
|
|
(*self)(lp)
|
2016-08-20 23:23:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-05 17:06:57 -08:00
|
|
|
|
2017-05-19 09:56:35 -07:00
|
|
|
fn read_ready() -> mio::Ready {
|
|
|
|
|
mio::Ready::readable() | platform::hup()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const READ: usize = 1 << 0;
|
|
|
|
|
const WRITE: usize = 1 << 1;
|
|
|
|
|
|
|
|
|
|
fn ready2usize(ready: mio::Ready) -> usize {
|
|
|
|
|
let mut bits = 0;
|
|
|
|
|
if ready.is_readable() {
|
|
|
|
|
bits |= READ;
|
|
|
|
|
}
|
|
|
|
|
if ready.is_writable() {
|
|
|
|
|
bits |= WRITE;
|
|
|
|
|
}
|
|
|
|
|
bits | platform::ready2usize(ready)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn usize2ready(bits: usize) -> mio::Ready {
|
|
|
|
|
let mut ready = mio::Ready::empty();
|
|
|
|
|
if bits & READ != 0 {
|
|
|
|
|
ready.insert(mio::Ready::readable());
|
|
|
|
|
}
|
|
|
|
|
if bits & WRITE != 0 {
|
|
|
|
|
ready.insert(mio::Ready::writable());
|
|
|
|
|
}
|
|
|
|
|
ready | platform::usize2ready(bits)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 14:08:26 -07:00
|
|
|
#[cfg(all(unix, not(target_os = "fuchsia")))]
|
2017-02-05 17:06:57 -08:00
|
|
|
mod platform {
|
|
|
|
|
use mio::Ready;
|
|
|
|
|
use mio::unix::UnixReady;
|
|
|
|
|
|
2017-05-21 10:24:08 -06:00
|
|
|
pub fn aio() -> Ready {
|
|
|
|
|
UnixReady::aio().into()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 11:01:48 -06:00
|
|
|
pub fn all() -> Ready {
|
|
|
|
|
hup() | aio()
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-05 17:06:57 -08:00
|
|
|
pub fn hup() -> Ready {
|
|
|
|
|
UnixReady::hup().into()
|
|
|
|
|
}
|
2017-05-19 09:56:35 -07:00
|
|
|
|
|
|
|
|
const HUP: usize = 1 << 2;
|
|
|
|
|
const ERROR: usize = 1 << 3;
|
2017-05-21 10:24:08 -06:00
|
|
|
const AIO: usize = 1 << 4;
|
2017-05-19 09:56:35 -07:00
|
|
|
|
|
|
|
|
pub fn ready2usize(ready: Ready) -> usize {
|
|
|
|
|
let ready = UnixReady::from(ready);
|
|
|
|
|
let mut bits = 0;
|
2017-05-21 10:24:08 -06:00
|
|
|
if ready.is_aio() {
|
|
|
|
|
bits |= AIO;
|
|
|
|
|
}
|
2017-05-19 09:56:35 -07:00
|
|
|
if ready.is_error() {
|
|
|
|
|
bits |= ERROR;
|
|
|
|
|
}
|
|
|
|
|
if ready.is_hup() {
|
|
|
|
|
bits |= HUP;
|
|
|
|
|
}
|
|
|
|
|
bits
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn usize2ready(bits: usize) -> Ready {
|
|
|
|
|
let mut ready = UnixReady::from(Ready::empty());
|
2017-05-21 10:24:08 -06:00
|
|
|
if bits & AIO != 0 {
|
|
|
|
|
ready.insert(UnixReady::aio());
|
|
|
|
|
}
|
2017-05-19 09:56:35 -07:00
|
|
|
if bits & HUP != 0 {
|
|
|
|
|
ready.insert(UnixReady::hup());
|
|
|
|
|
}
|
|
|
|
|
if bits & ERROR != 0 {
|
|
|
|
|
ready.insert(UnixReady::error());
|
|
|
|
|
}
|
|
|
|
|
ready.into()
|
|
|
|
|
}
|
2017-02-05 17:06:57 -08:00
|
|
|
}
|
|
|
|
|
|
2017-07-31 14:08:26 -07:00
|
|
|
#[cfg(any(windows, target_os = "fuchsia"))]
|
2017-02-05 17:06:57 -08:00
|
|
|
mod platform {
|
|
|
|
|
use mio::Ready;
|
|
|
|
|
|
2017-05-21 11:01:48 -06:00
|
|
|
pub fn all() -> Ready {
|
|
|
|
|
// No platform-specific Readinesses for Windows
|
|
|
|
|
Ready::empty()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 09:56:35 -07:00
|
|
|
pub fn hup() -> Ready {
|
|
|
|
|
Ready::empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ready2usize(_r: Ready) -> usize {
|
|
|
|
|
0
|
2017-02-05 17:06:57 -08:00
|
|
|
}
|
|
|
|
|
|
2017-05-19 09:56:35 -07:00
|
|
|
pub fn usize2ready(_r: usize) -> Ready {
|
2017-02-05 17:06:57 -08:00
|
|
|
Ready::empty()
|
|
|
|
|
}
|
|
|
|
|
}
|