Files
tokio/src/reactor/mod.rs
T

686 lines
23 KiB
Rust
Raw Normal View History

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
//! happening in `tokio-core`. This reactor (or event loop) is used to run
//! futures, schedule tasks, issue I/O requests, etc.
2016-08-20 23:23:16 -07:00
use std::cell::RefCell;
use std::cmp;
2016-08-20 23:23:16 -07:00
use std::io::{self, ErrorKind};
use std::mem;
2016-09-02 11:07:52 -07:00
use std::rc::{Rc, Weak};
2016-08-20 23:23:16 -07:00
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::time::{Instant, Duration};
2016-09-26 16:52:54 -07:00
use futures::{self, Future, IntoFuture, Async};
2017-01-05 20:41:36 +05:30
use futures::executor::{self, Spawn, Unpark};
2017-01-10 00:02:53 -08:00
use futures::sync::mpsc;
2017-01-05 20:41:36 +05:30
use futures::task::Task;
2016-08-20 23:23:16 -07:00
use mio;
use slab::Slab;
2016-09-07 23:59:51 -07:00
use heap::{Heap, Slot};
2016-08-20 23:23:16 -07:00
2016-09-02 11:07:52 -07:00
mod io_token;
mod timeout_token;
2016-08-20 23:23:16 -07:00
2016-09-02 11:07:52 -07:00
mod poll_evented;
mod timeout;
2016-10-06 01:59:26 +03:00
mod interval;
2016-09-07 16:11:19 -07:00
pub use self::poll_evented::PollEvented;
pub use self::timeout::Timeout;
2016-10-06 01:59:26 +03:00
pub use self::interval::Interval;
2016-09-02 11:07:52 -07:00
2016-08-20 23:23:16 -07:00
static NEXT_LOOP_ID: AtomicUsize = ATOMIC_USIZE_INIT;
2016-09-02 11:07:52 -07:00
scoped_thread_local!(static CURRENT_LOOP: Core);
2016-08-20 23:23:16 -07:00
/// 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.
// TODO: expand this
2016-09-02 11:07:52 -07:00
pub struct Core {
2016-08-20 23:23:16 -07:00
events: mio::Events,
2017-01-10 00:02:53 -08:00
tx: mpsc::UnboundedSender<Message>,
rx: RefCell<Spawn<mpsc::UnboundedReceiver<Message>>>,
_rx_registration: mio::Registration,
rx_readiness: Arc<MySetReadiness>,
2016-09-07 16:11:19 -07:00
inner: Rc<RefCell<Inner>>,
2016-08-31 00:19:29 -07: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 {
id: usize,
io: mio::Poll,
// Dispatch slabs for I/O and futures events
io_dispatch: Slab<ScheduledIo>,
task_dispatch: Slab<ScheduledTask>,
2016-08-20 23:23:16 -07:00
// Timer wheel keeping track of all timeouts. The `usize` stored in the
// timer wheel is an index into the slab below.
//
// The slab below keeps track of the timeouts themselves as well as the
// state of the timeout itself. The `TimeoutToken` type is an index into the
// `timeouts` slab.
2016-09-07 23:59:51 -07:00
timer_heap: Heap<(Instant, usize)>,
2016-09-08 07:46:47 -07:00
timeouts: Slab<(Option<Slot>, TimeoutState)>,
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-01-10 00:02:53 -08:00
tx: mpsc::UnboundedSender<Message>,
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,
inner: Weak<RefCell<Inner>>,
2016-08-20 23:23:16 -07:00
}
2016-08-31 00:19:29 -07:00
struct ScheduledIo {
2016-08-20 23:23:16 -07:00
readiness: Arc<AtomicUsize>,
2016-08-31 00:19:29 -07:00
reader: Option<Task>,
writer: Option<Task>,
}
struct ScheduledTask {
_registration: mio::Registration,
spawn: Option<Spawn<Box<Future<Item=(), Error=()>>>>,
wake: Arc<MySetReadiness>,
}
2016-08-20 23:23:16 -07:00
enum TimeoutState {
NotFired,
Fired,
2016-08-31 00:19:29 -07:00
Waiting(Task),
2016-08-20 23:23:16 -07:00
}
enum Direction {
Read,
Write,
}
enum Message {
DropSource(usize),
2016-08-31 00:19:29 -07:00
Schedule(usize, Task, Direction),
UpdateTimeout(usize, Task),
2016-10-06 01:59:26 +03:00
ResetTimeout(usize, Instant),
2016-08-20 23:23:16 -07:00
CancelTimeout(usize),
2016-08-31 00:19:29 -07:00
Run(Box<FnBox>),
2016-08-20 23:23:16 -07:00
}
2016-08-31 00:19:29 -07:00
const TOKEN_MESSAGES: mio::Token = mio::Token(0);
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
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> {
2016-08-20 23:23:16 -07:00
let io = try!(mio::Poll::new());
2016-08-31 00:19:29 -07:00
let future_pair = mio::Registration::new(&io,
TOKEN_FUTURE,
mio::Ready::readable(),
mio::PollOpt::level());
2017-01-10 00:02:53 -08:00
let (tx, rx) = mpsc::unbounded();
let channel_pair = mio::Registration::new(&io,
TOKEN_MESSAGES,
mio::Ready::readable(),
mio::PollOpt::level());
let rx_readiness = Arc::new(MySetReadiness(channel_pair.1));
rx_readiness.unpark();
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-09-02 11:07:52 -07:00
tx: tx,
2017-01-10 00:02:53 -08:00
rx: RefCell::new(executor::spawn(rx)),
_rx_registration: channel_pair.0,
rx_readiness: rx_readiness,
2016-08-31 00:19:29 -07:00
_future_registration: future_pair.0,
future_readiness: Arc::new(MySetReadiness(future_pair.1)),
2016-09-07 16:11:19 -07:00
inner: Rc::new(RefCell::new(Inner {
id: NEXT_LOOP_ID.fetch_add(1, Ordering::Relaxed),
io: io,
2016-12-16 13:37:59 -05:00
io_dispatch: Slab::with_capacity(1),
task_dispatch: Slab::with_capacity(1),
timeouts: Slab::with_capacity(1),
2016-09-07 23:59:51 -07:00
timer_heap: Heap::new(),
2016-09-07 16:11:19 -07:00
})),
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 {
Handle {
2016-09-07 16:11:19 -07:00
remote: self.remote(),
inner: Rc::downgrade(&self.inner),
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 {
id: self.inner.borrow().id,
tx: self.tx.clone(),
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
/// the provided future is resolve. Note that the future argument here
/// 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.
///
/// This function will returns the value that the future resolves to once
/// 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-08-20 23:23:16 -07:00
let ready = self.future_readiness.clone();
2016-11-11 14:27:49 -08:00
let mut future_fired = true;
loop {
if future_fired {
let res = try!(CURRENT_LOOP.set(self, || {
task.poll_future(ready.clone())
}));
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
}
/// 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-11-11 14:27:49 -08:00
fn poll(&mut self, max_wait: Option<Duration>) -> bool {
// Given the `max_wait` variable specified, figure out the actual
// timeout that we're going to pass to `poll`. This involves taking a
// look at active timers on our heap as well.
let start = Instant::now();
2016-11-11 14:27:49 -08:00
let timeout = self.inner.borrow_mut().timer_heap.peek().map(|t| {
if t.0 < start {
Duration::new(0, 0)
} else {
2016-11-11 14:27:49 -08:00
t.0 - start
2016-08-20 23:23:16 -07:00
}
2016-11-11 14:27:49 -08:00
});
let timeout = match (max_wait, timeout) {
(Some(d1), Some(d2)) => Some(cmp::min(d1, d2)),
(max_wait, timeout) => max_wait.or(timeout),
};
// Block waiting for an event to happen, peeling out how many events
// happened.
let amt = match self.inner.borrow_mut().io.poll(&mut self.events, timeout) {
Ok(a) => a,
Err(ref e) if e.kind() == ErrorKind::Interrupted => return false,
Err(e) => panic!("error in poll: {}", e),
};
let after_poll = Instant::now();
debug!("loop poll - {:?}", after_poll - start);
debug!("loop time - {:?}", after_poll);
// Process all timeouts that may have just occurred, updating the
// current time since
self.consume_timeouts(after_poll);
// Process all the events that came in, dispatching appropriately
let mut fired = false;
for i in 0..self.events.len() {
let event = self.events.get(i).unwrap();
let token = event.token();
trace!("event {:?} {:?}", event.kind(), event.token());
if token == TOKEN_MESSAGES {
2017-01-10 00:02:53 -08:00
self.rx_readiness.0.set_readiness(mio::Ready::none()).unwrap();
CURRENT_LOOP.set(&self, || self.consume_queue());
} else if token == TOKEN_FUTURE {
self.future_readiness.0.set_readiness(mio::Ready::none()).unwrap();
2016-11-11 14:27:49 -08:00
fired = true;
} else {
self.dispatch(token, event.kind());
}
2016-08-20 23:23:16 -07:00
}
2016-11-11 14:27:49 -08:00
debug!("loop process - {} events, {:?}", amt, after_poll.elapsed());
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;
if token % 2 == 0 {
self.dispatch_io(token / 2, ready)
} else {
self.dispatch_task(token / 2)
}
}
2016-09-07 16:11:19 -07:00
fn dispatch_io(&mut self, token: usize, ready: mio::Ready) {
2016-08-31 00:19:29 -07:00
let mut reader = None;
let mut writer = None;
2016-09-07 16:11:19 -07:00
let mut inner = self.inner.borrow_mut();
if let Some(io) = inner.io_dispatch.get_mut(token) {
if ready.is_readable() || ready.is_hup() {
2016-08-31 00:19:29 -07:00
reader = io.reader.take();
io.readiness.fetch_or(1, Ordering::Relaxed);
}
if ready.is_writable() {
writer = io.writer.take();
io.readiness.fetch_or(2, Ordering::Relaxed);
}
}
2016-09-07 16:11:19 -07:00
drop(inner);
2016-08-31 00:19:29 -07:00
// TODO: don't notify the same task twice
if let Some(reader) = reader {
self.notify_handle(reader);
}
if let Some(writer) = writer {
self.notify_handle(writer);
}
}
2016-09-07 16:11:19 -07:00
fn dispatch_task(&mut self, token: usize) {
let mut inner = self.inner.borrow_mut();
let (task, wake) = match inner.task_dispatch.get_mut(token) {
2016-08-31 00:19:29 -07:00
Some(slot) => (slot.spawn.take(), slot.wake.clone()),
None => return,
};
wake.0.set_readiness(mio::Ready::none()).unwrap();
let mut task = match task {
Some(task) => task,
None => return,
};
2016-09-07 16:11:19 -07:00
drop(inner);
2016-08-31 00:19:29 -07:00
let res = CURRENT_LOOP.set(self, || task.poll_future(wake));
2016-09-07 16:11:19 -07:00
inner = self.inner.borrow_mut();
2016-08-31 00:19:29 -07:00
match res {
2016-09-01 16:42:48 -07:00
Ok(Async::NotReady) => {
2016-09-07 16:11:19 -07:00
assert!(inner.task_dispatch[token].spawn.is_none());
inner.task_dispatch[token].spawn = Some(task);
2016-08-31 00:19:29 -07:00
}
2016-09-01 16:42:48 -07:00
Ok(Async::Ready(())) |
Err(()) => {
2016-09-07 16:11:19 -07:00
inner.task_dispatch.remove(token).unwrap();
2016-08-31 00:19:29 -07:00
}
}
}
2016-08-20 23:23:16 -07:00
fn consume_timeouts(&mut self, now: Instant) {
2016-09-07 16:11:19 -07:00
loop {
let mut inner = self.inner.borrow_mut();
2016-09-07 23:59:51 -07:00
match inner.timer_heap.peek() {
Some(head) if head.0 <= now => {}
Some(_) => break,
2016-09-07 16:11:19 -07:00
None => break,
};
2016-09-07 23:59:51 -07:00
let (_, slab_idx) = inner.timer_heap.pop().unwrap();
trace!("firing timeout: {}", slab_idx);
2016-09-08 07:46:47 -07:00
inner.timeouts[slab_idx].0.take().unwrap();
2016-09-07 23:59:51 -07:00
let handle = inner.timeouts[slab_idx].1.fire();
2016-09-07 16:11:19 -07:00
drop(inner);
2016-08-20 23:23:16 -07:00
if let Some(handle) = handle {
self.notify_handle(handle);
}
}
}
/// Method used to notify a task handle.
///
2016-11-05 16:29:54 -04:00
/// Note that this should be used instead of `handle.unpark()` to ensure
2016-08-20 23:23:16 -07:00
/// that the `CURRENT_LOOP` variable is set appropriately.
2016-08-31 00:19:29 -07:00
fn notify_handle(&self, handle: Task) {
2016-08-20 23:23:16 -07:00
debug!("notifying a task handle");
CURRENT_LOOP.set(&self, || handle.unpark());
}
2016-09-07 16:11:19 -07:00
fn consume_queue(&self) {
debug!("consuming notification queue");
// TODO: can we do better than `.unwrap()` here?
2017-01-10 00:02:53 -08:00
let unpark = self.rx_readiness.clone();
loop {
match self.rx.borrow_mut().poll_stream(unpark.clone()).unwrap() {
Async::Ready(Some(msg)) => self.notify(msg),
Async::NotReady |
Async::Ready(None) => break,
}
2016-09-07 16:11:19 -07:00
}
}
fn notify(&self, msg: Message) {
match msg {
Message::DropSource(tok) => self.inner.borrow_mut().drop_source(tok),
Message::Schedule(tok, wake, dir) => {
let task = self.inner.borrow_mut().schedule(tok, wake, dir);
if let Some(task) = task {
self.notify_handle(task);
}
}
Message::UpdateTimeout(t, handle) => {
let task = self.inner.borrow_mut().update_timeout(t, handle);
if let Some(task) = task {
self.notify_handle(task);
}
}
2016-10-06 01:59:26 +03:00
Message::ResetTimeout(t, at) => {
self.inner.borrow_mut().reset_timeout(t, at);
}
2016-09-07 16:11:19 -07:00
Message::CancelTimeout(t) => {
self.inner.borrow_mut().cancel_timeout(t)
}
Message::Run(r) => r.call_box(self),
}
}
2017-01-15 15:15:37 +01:00
/// Get the ID of this loop
pub fn id(&self) -> CoreId {
CoreId(self.inner.borrow().id)
}
2016-09-07 16:11:19 -07:00
}
impl Inner {
fn add_source(&mut self, source: &mio::Evented)
2016-08-20 23:23:16 -07:00
-> io::Result<(Arc<AtomicUsize>, usize)> {
debug!("adding a new I/O source");
2016-08-31 00:19:29 -07:00
let sched = ScheduledIo {
2016-08-20 23:23:16 -07:00
readiness: Arc::new(AtomicUsize::new(0)),
reader: None,
writer: None,
};
2016-09-07 16:11:19 -07:00
if self.io_dispatch.vacant_entry().is_none() {
let amt = self.io_dispatch.len();
self.io_dispatch.reserve_exact(amt);
2016-08-20 23:23:16 -07:00
}
2016-09-07 16:11:19 -07:00
let entry = self.io_dispatch.vacant_entry().unwrap();
2016-08-20 23:23:16 -07:00
try!(self.io.register(source,
2016-08-31 00:19:29 -07:00
mio::Token(TOKEN_START + entry.index() * 2),
mio::Ready::readable() | mio::Ready::writable() | mio::Ready::hup(),
2016-08-20 23:23:16 -07:00
mio::PollOpt::edge()));
Ok((sched.readiness.clone(), entry.insert(sched).index()))
}
fn deregister_source(&mut self, source: &mio::Evented) -> io::Result<()> {
self.io.deregister(source)
}
2016-08-20 23:23:16 -07:00
2016-09-07 16:11:19 -07:00
fn drop_source(&mut self, token: usize) {
2016-08-20 23:23:16 -07:00
debug!("dropping I/O source: {}", token);
2016-09-07 16:11:19 -07:00
self.io_dispatch.remove(token).unwrap();
2016-08-20 23:23:16 -07:00
}
2016-09-07 16:11:19 -07:00
fn schedule(&mut self, token: usize, wake: Task, dir: Direction)
-> Option<Task> {
2016-08-20 23:23:16 -07:00
debug!("scheduling direction for: {}", token);
2016-09-07 16:11:19 -07:00
let sched = self.io_dispatch.get_mut(token).unwrap();
let (slot, bit) = match dir {
Direction::Read => (&mut sched.reader, 1),
Direction::Write => (&mut sched.writer, 2),
2016-08-20 23:23:16 -07:00
};
2016-09-07 16:11:19 -07:00
if sched.readiness.load(Ordering::SeqCst) & bit != 0 {
*slot = None;
Some(wake)
} else {
*slot = Some(wake);
None
2016-08-20 23:23:16 -07:00
}
}
2016-10-06 21:11:46 +03:00
fn add_timeout(&mut self, at: Instant) -> usize {
2016-09-07 16:11:19 -07:00
if self.timeouts.vacant_entry().is_none() {
let len = self.timeouts.len();
self.timeouts.reserve_exact(len);
2016-08-20 23:23:16 -07:00
}
2016-09-07 16:11:19 -07:00
let entry = self.timeouts.vacant_entry().unwrap();
2016-09-07 23:59:51 -07:00
let slot = self.timer_heap.push((at, entry.index()));
2016-09-08 07:46:47 -07:00
let entry = entry.insert((Some(slot), TimeoutState::NotFired));
2016-08-20 23:23:16 -07:00
debug!("added a timeout: {}", entry.index());
2016-10-06 21:11:46 +03:00
return entry.index();
2016-08-20 23:23:16 -07:00
}
2016-09-07 16:11:19 -07:00
fn update_timeout(&mut self, token: usize, handle: Task) -> Option<Task> {
2016-08-20 23:23:16 -07:00
debug!("updating a timeout: {}", token);
2016-09-07 16:11:19 -07:00
self.timeouts[token].1.block(handle)
2016-08-20 23:23:16 -07:00
}
2016-10-06 01:59:26 +03:00
fn reset_timeout(&mut self, token: usize, at: Instant) {
let pair = &mut self.timeouts[token];
// TODO: avoid remove + push and instead just do one sift of the heap?
// In theory we could update it in place and then do the percolation
// as necessary
if let Some(slot) = pair.0.take() {
self.timer_heap.remove(slot);
}
let slot = self.timer_heap.push((at, token));
*pair = (Some(slot), TimeoutState::NotFired);
debug!("set a timeout: {}", token);
}
2016-09-07 16:11:19 -07:00
fn cancel_timeout(&mut self, token: usize) {
2016-08-20 23:23:16 -07:00
debug!("cancel a timeout: {}", token);
2016-09-07 16:11:19 -07:00
let pair = self.timeouts.remove(token);
2016-09-08 07:46:47 -07:00
if let Some((Some(slot), _state)) = pair {
2016-09-07 23:59:51 -07:00
self.timer_heap.remove(slot);
2016-08-20 23:23:16 -07:00
}
}
2016-09-07 16:11:19 -07:00
fn spawn(&mut self, future: Box<Future<Item=(), Error=()>>) {
if self.task_dispatch.vacant_entry().is_none() {
let len = self.task_dispatch.len();
self.task_dispatch.reserve_exact(len);
2016-08-20 23:23:16 -07:00
}
2016-09-07 16:11:19 -07:00
let entry = self.task_dispatch.vacant_entry().unwrap();
let token = TOKEN_START + 2 * entry.index() + 1;
let pair = mio::Registration::new(&self.io,
mio::Token(token),
mio::Ready::readable(),
mio::PollOpt::level());
let unpark = Arc::new(MySetReadiness(pair.1));
let entry = entry.insert(ScheduledTask {
2017-01-05 20:41:36 +05:30
spawn: Some(executor::spawn(future)),
2016-09-07 16:11:19 -07:00
wake: unpark,
_registration: pair.0,
});
entry.get().wake.clone().unpark();
2016-08-20 23:23:16 -07:00
}
}
2016-09-07 16:11:19 -07:00
impl Remote {
2016-08-20 23:23:16 -07:00
fn send(&self, msg: Message) {
self.with_loop(|lp| {
match lp {
Some(lp) => {
// Need to execute all existing requests first, to ensure
// that our message is processed "in order"
lp.consume_queue();
lp.notify(msg);
}
None => {
2017-01-10 00:02:53 -08:00
// TODO: shouldn't have to `clone` here, can we upstream
// that &self works with `UnboundedSender`?
match mpsc::UnboundedSender::send(&mut self.tx.clone(), msg) {
2016-08-20 23:23:16 -07:00
Ok(()) => {}
2017-01-10 00:02:53 -08:00
// TODO: this error should punt upwards and we should
// notify the caller that the message wasn't
// received. This is tokio-core#17
Err(e) => drop(e),
2016-08-20 23:23:16 -07:00
}
}
}
})
}
fn with_loop<F, R>(&self, f: F) -> R
2016-09-02 11:07:52 -07:00
where F: FnOnce(Option<&Core>) -> R
2016-08-20 23:23:16 -07:00
{
if CURRENT_LOOP.is_set() {
CURRENT_LOOP.with(|lp| {
2016-09-07 16:11:19 -07:00
let same = lp.inner.borrow().id == self.id;
if same {
2016-08-20 23:23:16 -07:00
f(Some(lp))
} else {
f(None)
}
})
} else {
f(None)
}
}
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
/// scheduled on the event loop an run to completion.
///
/// Note that while the closure, `F`, requires the `Send` bound as it might
/// cross threads, the future `R` does not.
pub fn spawn<F, R>(&self, f: F)
2016-09-07 16:11:19 -07:00
where F: FnOnce(&Handle) -> R + Send + 'static,
2016-08-31 00:19:29 -07:00
R: IntoFuture<Item=(), Error=()>,
R::Future: 'static,
{
2016-09-02 11:07:52 -07:00
self.send(Message::Run(Box::new(|lp: &Core| {
2016-09-07 16:11:19 -07:00
let f = f(&lp.handle());
lp.inner.borrow_mut().spawn(Box::new(f.into_future()));
2016-08-31 00:19:29 -07:00
})));
}
2017-01-15 15:15:37 +01:00
/// Return the ID of the represented Core
pub fn id(&self) -> CoreId {
CoreId(self.id)
}
2016-08-20 23:23:16 -07:00
}
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
}
2016-09-21 18:27:25 +02:00
/// Spawns a new future on the event loop this handle is associated with.
2016-08-31 00:19:29 -07:00
pub fn spawn<F>(&self, f: F)
where F: Future<Item=(), Error=()> + 'static,
{
2016-09-07 16:11:19 -07:00
let inner = match self.inner.upgrade() {
2016-09-02 11:07:52 -07:00
Some(inner) => inner,
None => return,
};
2016-09-07 16:11:19 -07:00
inner.borrow_mut().spawn(Box::new(f));
2016-08-20 23:23:16 -07:00
}
2016-09-26 16:52:54 -07:00
/// Spawns a closure on this event loop.
///
/// This function is a convenience wrapper around the `spawn` function above
/// for running a closure wrapped in `futures::lazy`. It will spawn the
/// function `f` provided onto the event loop, and continue to run the
2016-09-28 03:20:33 +02:00
/// future returned by `f` on the event loop as well.
2016-09-26 16:52:54 -07:00
pub fn spawn_fn<F, R>(&self, f: F)
where F: FnOnce() -> R + 'static,
R: IntoFuture<Item=(), Error=()> + 'static,
{
self.spawn(futures::lazy(f))
}
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
}
impl TimeoutState {
2016-08-31 00:19:29 -07:00
fn block(&mut self, handle: Task) -> Option<Task> {
2016-08-20 23:23:16 -07:00
match *self {
TimeoutState::Fired => return Some(handle),
_ => {}
}
*self = TimeoutState::Waiting(handle);
None
}
2016-08-31 00:19:29 -07:00
fn fire(&mut self) -> Option<Task> {
2016-08-20 23:23:16 -07:00
match mem::replace(self, TimeoutState::Fired) {
TimeoutState::NotFired => None,
TimeoutState::Fired => panic!("fired twice?"),
TimeoutState::Waiting(handle) => Some(handle),
}
}
}
2016-08-31 00:19:29 -07:00
struct MySetReadiness(mio::SetReadiness);
impl Unpark for MySetReadiness {
fn unpark(&self) {
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
}
}