2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-12-22 18:26:39 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Tokio Core Reactor wrapper.
|
|
|
|
|
|
|
|
extern crate futures;
|
|
|
|
extern crate tokio_core;
|
|
|
|
|
2017-08-28 14:11:55 +02:00
|
|
|
use std::{fmt, thread};
|
2016-12-22 18:26:39 +01:00
|
|
|
use std::sync::mpsc;
|
2016-12-27 11:15:02 +01:00
|
|
|
use std::time::Duration;
|
2016-12-22 18:26:39 +01:00
|
|
|
use futures::{Future, IntoFuture};
|
2018-04-10 13:51:29 +02:00
|
|
|
pub use tokio_core::reactor::{Remote as TokioRemote, Handle, Timeout};
|
2016-12-22 18:26:39 +01:00
|
|
|
|
|
|
|
/// Event Loop for futures.
|
|
|
|
/// Wrapper around `tokio::reactor::Core`.
|
|
|
|
/// Runs in a separate thread.
|
|
|
|
pub struct EventLoop {
|
|
|
|
remote: Remote,
|
|
|
|
handle: EventLoopHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventLoop {
|
|
|
|
/// Spawns a new thread with `EventLoop` with given handler.
|
|
|
|
pub fn spawn() -> Self {
|
|
|
|
let (stop, stopped) = futures::oneshot();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
let mut el = tokio_core::reactor::Core::new().expect("Creating an event loop should not fail.");
|
|
|
|
tx.send(el.remote()).expect("Rx is blocking upper thread.");
|
|
|
|
let _ = el.run(futures::empty().select(stopped));
|
|
|
|
});
|
|
|
|
let remote = rx.recv().expect("tx is transfered to a newly spawned thread.");
|
|
|
|
|
|
|
|
EventLoop {
|
2017-03-22 07:02:14 +01:00
|
|
|
remote: Remote {
|
2016-12-22 18:26:39 +01:00
|
|
|
inner: Mode::Tokio(remote),
|
|
|
|
},
|
|
|
|
handle: EventLoopHandle {
|
|
|
|
close: Some(stop),
|
|
|
|
handle: Some(handle),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
/// Returns this event loop raw remote.
|
|
|
|
///
|
|
|
|
/// Deprecated: Exists only to connect with current JSONRPC implementation.
|
|
|
|
pub fn raw_remote(&self) -> TokioRemote {
|
|
|
|
if let Mode::Tokio(ref remote) = self.remote.inner {
|
|
|
|
remote.clone()
|
|
|
|
} else {
|
|
|
|
panic!("Event loop is never initialized in other mode then Tokio.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Returns event loop remote.
|
|
|
|
pub fn remote(&self) -> Remote {
|
|
|
|
self.remote.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum Mode {
|
|
|
|
Tokio(TokioRemote),
|
|
|
|
Sync,
|
2016-12-28 13:45:25 +01:00
|
|
|
ThreadPerFuture,
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2017-08-28 14:11:55 +02:00
|
|
|
impl fmt::Debug for Mode {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::Mode::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Tokio(_) => write!(fmt, "tokio"),
|
|
|
|
Sync => write!(fmt, "synchronous"),
|
|
|
|
ThreadPerFuture => write!(fmt, "thread per future"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2016-12-22 18:26:39 +01:00
|
|
|
pub struct Remote {
|
|
|
|
inner: Mode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Remote {
|
2017-01-11 20:02:27 +01:00
|
|
|
/// Remote for existing event loop.
|
|
|
|
///
|
|
|
|
/// Deprecated: Exists only to connect with current JSONRPC implementation.
|
|
|
|
pub fn new(remote: TokioRemote) -> Self {
|
|
|
|
Remote {
|
|
|
|
inner: Mode::Tokio(remote),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Synchronous remote, used mostly for tests.
|
|
|
|
pub fn new_sync() -> Self {
|
|
|
|
Remote {
|
|
|
|
inner: Mode::Sync,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 13:45:25 +01:00
|
|
|
/// Spawns a new thread for each future (use only for tests).
|
|
|
|
pub fn new_thread_per_future() -> Self {
|
|
|
|
Remote {
|
|
|
|
inner: Mode::ThreadPerFuture,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Spawn a future to this event loop
|
|
|
|
pub fn spawn<R>(&self, r: R) where
|
|
|
|
R: IntoFuture<Item=(), Error=()> + Send + 'static,
|
|
|
|
R::Future: 'static,
|
|
|
|
{
|
|
|
|
match self.inner {
|
|
|
|
Mode::Tokio(ref remote) => remote.spawn(move |_| r),
|
|
|
|
Mode::Sync => {
|
|
|
|
let _= r.into_future().wait();
|
|
|
|
},
|
2016-12-28 13:45:25 +01:00
|
|
|
Mode::ThreadPerFuture => {
|
|
|
|
thread::spawn(move || {
|
|
|
|
let _= r.into_future().wait();
|
|
|
|
});
|
|
|
|
},
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn a new future returned by given closure.
|
|
|
|
pub fn spawn_fn<F, R>(&self, f: F) where
|
2018-04-10 13:51:29 +02:00
|
|
|
F: FnOnce(&Handle) -> R + Send + 'static,
|
2016-12-22 18:26:39 +01:00
|
|
|
R: IntoFuture<Item=(), Error=()>,
|
|
|
|
R::Future: 'static,
|
|
|
|
{
|
|
|
|
match self.inner {
|
2018-04-10 13:51:29 +02:00
|
|
|
Mode::Tokio(ref remote) => remote.spawn(move |handle| f(handle)),
|
2016-12-22 18:26:39 +01:00
|
|
|
Mode::Sync => {
|
2018-04-10 13:51:29 +02:00
|
|
|
let mut core = tokio_core::reactor::Core::new().expect("Creating an event loop should not fail.");
|
|
|
|
let handle = core.handle();
|
|
|
|
let _ = core.run(f(&handle).into_future());
|
2016-12-22 18:26:39 +01:00
|
|
|
},
|
2016-12-28 13:45:25 +01:00
|
|
|
Mode::ThreadPerFuture => {
|
|
|
|
thread::spawn(move || {
|
2018-04-10 13:51:29 +02:00
|
|
|
let mut core = tokio_core::reactor::Core::new().expect("Creating an event loop should not fail.");
|
|
|
|
let handle = core.handle();
|
|
|
|
let _ = core.run(f(&handle).into_future());
|
2016-12-28 13:45:25 +01:00
|
|
|
});
|
|
|
|
},
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-27 11:15:02 +01:00
|
|
|
|
|
|
|
/// Spawn a new future and wait for it or for a timeout to occur.
|
|
|
|
pub fn spawn_with_timeout<F, R, T>(&self, f: F, duration: Duration, on_timeout: T) where
|
|
|
|
T: FnOnce() -> () + Send + 'static,
|
2018-04-10 13:51:29 +02:00
|
|
|
F: FnOnce(&Handle) -> R + Send + 'static,
|
2016-12-27 11:15:02 +01:00
|
|
|
R: IntoFuture<Item=(), Error=()>,
|
|
|
|
R::Future: 'static,
|
|
|
|
{
|
|
|
|
match self.inner {
|
|
|
|
Mode::Tokio(ref remote) => remote.spawn(move |handle| {
|
2018-04-10 13:51:29 +02:00
|
|
|
let future = f(handle).into_future();
|
2016-12-27 11:15:02 +01:00
|
|
|
let timeout = Timeout::new(duration, handle).expect("Event loop is still up.");
|
|
|
|
future.select(timeout.then(move |_| {
|
|
|
|
on_timeout();
|
|
|
|
Ok(())
|
|
|
|
})).then(|_| Ok(()))
|
|
|
|
}),
|
|
|
|
Mode::Sync => {
|
2018-04-10 13:51:29 +02:00
|
|
|
let mut core = tokio_core::reactor::Core::new().expect("Creating an event loop should not fail.");
|
|
|
|
let handle = core.handle();
|
|
|
|
let future = f(&handle).into_future();
|
|
|
|
let timeout = Timeout::new(duration, &handle).expect("Event loop is still up.");
|
|
|
|
let _: Result<(), ()> = core.run(future.select(timeout.then(move |_| {
|
|
|
|
on_timeout();
|
|
|
|
Ok(())
|
|
|
|
})).then(|_| Ok(())));
|
2016-12-27 11:15:02 +01:00
|
|
|
},
|
2016-12-28 13:45:25 +01:00
|
|
|
Mode::ThreadPerFuture => {
|
|
|
|
thread::spawn(move || {
|
2018-04-10 13:51:29 +02:00
|
|
|
let mut core = tokio_core::reactor::Core::new().expect("Creating an event loop should not fail.");
|
|
|
|
let handle = core.handle();
|
|
|
|
let future = f(&handle).into_future();
|
|
|
|
let timeout = Timeout::new(duration, &handle).expect("Event loop is still up.");
|
|
|
|
let _: Result<(), ()> = core.run(future.select(timeout.then(move |_| {
|
|
|
|
on_timeout();
|
|
|
|
Ok(())
|
|
|
|
})).then(|_| Ok(())));
|
2016-12-28 13:45:25 +01:00
|
|
|
});
|
|
|
|
},
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A handle to running event loop. Dropping the handle will cause event loop to finish.
|
|
|
|
pub struct EventLoopHandle {
|
|
|
|
close: Option<futures::Complete<()>>,
|
|
|
|
handle: Option<thread::JoinHandle<()>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EventLoop> for EventLoopHandle {
|
|
|
|
fn from(el: EventLoop) -> Self {
|
|
|
|
el.handle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for EventLoopHandle {
|
|
|
|
fn drop(&mut self) {
|
2017-03-22 07:02:14 +01:00
|
|
|
self.close.take().map(|v| v.send(()));
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventLoopHandle {
|
|
|
|
/// Blocks current thread and waits until the event loop is finished.
|
|
|
|
pub fn wait(mut self) -> thread::Result<()> {
|
|
|
|
self.handle.take()
|
|
|
|
.expect("Handle is taken only in `wait`, `wait` is consuming; qed").join()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finishes this event loop.
|
|
|
|
pub fn close(mut self) {
|
2017-03-22 07:02:14 +01:00
|
|
|
let _ = self.close.take()
|
|
|
|
.expect("Close is taken only in `close` and `drop`. `close` is consuming; qed")
|
|
|
|
.send(());
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|