diff --git a/Cargo.lock b/Cargo.lock index f7f8e4dc2..84147182d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1024,7 +1024,7 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1167,7 +1167,7 @@ dependencies = [ "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "parity-bytes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "parity-crypto 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "parity-path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2380,14 +2380,13 @@ dependencies = [ [[package]] name = "mio" -version = "0.6.16" +version = "0.6.19" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "lazycell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2403,7 +2402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazycell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2413,7 +2412,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2425,7 +2424,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2666,7 +2665,7 @@ dependencies = [ "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -4134,7 +4133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-current-thread 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4202,7 +4201,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4216,7 +4215,7 @@ dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4251,7 +4250,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4298,7 +4297,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4314,7 +4313,7 @@ dependencies = [ "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4732,7 +4731,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4964,7 +4963,7 @@ dependencies = [ "checksum memzero 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "93c0d11ac30a033511ae414355d80f70d9f29a44a49140face477117a1ee90db" "checksum mime 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)" = "0a907b83e7b9e987032439a387e187119cddafc92d5c2aaeb1d92580a793f630" "checksum mime_guess 2.0.0-alpha.6 (registry+https://github.com/rust-lang/crates.io-index)" = "30de2e4613efcba1ec63d8133f344076952090c122992a903359be5a4f99c3ed" -"checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" +"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23" "checksum mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "46e73a04c2fa6250b8d802134d56d554a9ec2922bf977777c805ea5def61ce40" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" diff --git a/util/io/Cargo.toml b/util/io/Cargo.toml index 2158ec2f2..1b9293527 100644 --- a/util/io/Cargo.toml +++ b/util/io/Cargo.toml @@ -1,14 +1,15 @@ [package] +name = "ethcore-io" description = "Ethcore IO library" +version = "1.12.0" homepage = "http://parity.io" license = "GPL-3.0" -name = "ethcore-io" -version = "1.12.0" authors = ["Parity Technologies "] +edition = "2018" [dependencies] fnv = "1.0" -mio = { version = "0.6.8", optional = true } +mio = { version = "0.6.19", optional = true } crossbeam-deque = "0.6" parking_lot = "0.8" log = "0.4" @@ -18,3 +19,4 @@ timer = "0.2" time = "0.1" tokio = "0.1" futures = "0.1" + diff --git a/util/io/src/lib.rs b/util/io/src/lib.rs index 997edd7c4..a957233f3 100644 --- a/util/io/src/lib.rs +++ b/util/io/src/lib.rs @@ -69,20 +69,6 @@ //TODO: use Poll from mio #![allow(deprecated)] -#[cfg(feature = "mio")] -extern crate mio; -#[macro_use] -extern crate log as rlog; -extern crate slab; -extern crate crossbeam_deque as deque; -extern crate parking_lot; -extern crate num_cpus; -extern crate timer; -extern crate fnv; -extern crate time; -extern crate tokio; -extern crate futures; - #[cfg(feature = "mio")] mod service_mio; #[cfg(not(feature = "mio"))] @@ -170,7 +156,7 @@ pub trait IoHandler: Send + Sync where Message: Send + Sync + 'static { /// Re-register a stream with the event loop #[cfg(feature = "mio")] fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop>) {} - /// Deregister a stream. Called whenstream is removed from event loop + /// Deregister a stream. Called when a stream is removed from the event loop #[cfg(feature = "mio")] fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop>) {} } @@ -178,14 +164,15 @@ pub trait IoHandler: Send + Sync where Message: Send + Sync + 'static { #[cfg(feature = "mio")] pub use service_mio::{TimerToken, StreamToken, IoContext, IoService, IoChannel, IoManager, TOKENS_PER_HANDLER}; #[cfg(not(feature = "mio"))] -pub use service_non_mio::{TimerToken, IoContext, IoService, IoChannel, TOKENS_PER_HANDLER}; +pub use crate::service_non_mio::{TimerToken, IoContext, IoService, IoChannel, TOKENS_PER_HANDLER}; #[cfg(test)] mod tests { - use std::sync::Arc; - use std::sync::atomic; - use std::thread; - use std::time::Duration; + use std::{ + sync::{Arc, atomic}, + thread, + time::Duration, + }; use super::*; // Mio's behaviour is too unstable for this test. Sometimes we have to wait a few milliseconds, diff --git a/util/io/src/service_mio.rs b/util/io/src/service_mio.rs index f1f19bfc2..bd260a97e 100644 --- a/util/io/src/service_mio.rs +++ b/util/io/src/service_mio.rs @@ -14,19 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . +use std::collections::HashMap; use std::sync::{Arc, Weak}; use std::thread::{self, JoinHandle}; -use std::collections::HashMap; -use mio::*; -use mio::timer::{Timeout}; -use mio::deprecated::{EventLoop, Handler, Sender, EventLoopBuilder}; -use deque; -use slab::Slab; -use {IoError, IoHandler}; -use worker::{Worker, Work, WorkType}; -use parking_lot::{Condvar, RwLock, Mutex}; use std::time::Duration; +use crossbeam_deque as deque; +use log::{trace, debug, warn}; +use mio::*; +use mio::deprecated::{EventLoop, EventLoopBuilder, Handler, Sender}; +use mio::timer::Timeout; +use parking_lot::{Condvar, Mutex, RwLock}; +use slab::Slab; + +use crate::{ + IoError, IoHandler, + worker::{Work, Worker, WorkType} +}; + /// Timer ID pub type TimerToken = usize; /// Timer ID @@ -45,7 +50,7 @@ pub enum IoMessage where Message: Send + Sized { Shutdown, /// Register a new protocol handler. AddHandler { - handler: Arc+Send>, + handler: Arc + Send>, }, RemoveHandler { handler_id: HandlerId, @@ -182,7 +187,7 @@ struct UserTimer { /// Root IO handler. Manages user handlers, messages and IO timers. pub struct IoManager where Message: Send + Sync { timers: Arc>>, - handlers: Arc>>>>, + handlers: Arc>>>>, workers: Vec, worker_channel: deque::Worker>, work_ready: Arc, @@ -192,7 +197,7 @@ impl IoManager where Message: Send + Sync + 'static { /// Creates a new instance and registers it with the event loop. pub fn start( event_loop: &mut EventLoop>, - handlers: Arc>>>> + handlers: Arc>>>> ) -> Result<(), IoError> { let (worker, stealer) = deque::fifo(); let num_workers = 4; @@ -243,24 +248,6 @@ impl Handler for IoManager where Message: Send + Sync + 'stati } } - fn timeout(&mut self, event_loop: &mut EventLoop, token: Token) { - let handler_index = token.0 / TOKENS_PER_HANDLER; - let token_id = token.0 % TOKENS_PER_HANDLER; - if let Some(handler) = self.handlers.read().get(handler_index) { - let maybe_timer = self.timers.read().get(&token.0).cloned(); - if let Some(timer) = maybe_timer { - if timer.once { - self.timers.write().remove(&token_id); - event_loop.clear_timeout(&timer.timeout); - } else { - event_loop.timeout(token, timer.delay).expect("Error re-registering user timer"); - } - self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler.clone(), handler_id: handler_index }); - self.work_ready.notify_all(); - } - } - } - fn notify(&mut self, event_loop: &mut EventLoop, msg: Self::Message) { match msg { IoMessage::Shutdown => { @@ -331,11 +318,34 @@ impl Handler for IoManager where Message: Send + Sync + 'stati } } } + + fn timeout(&mut self, event_loop: &mut EventLoop, token: Token) { + let handler_index = token.0 / TOKENS_PER_HANDLER; + let token_id = token.0 % TOKENS_PER_HANDLER; + if let Some(handler) = self.handlers.read().get(handler_index) { + let maybe_timer = self.timers.read().get(&token.0).cloned(); + if let Some(timer) = maybe_timer { + if timer.once { + self.timers.write().remove(&token_id); + event_loop.clear_timeout(&timer.timeout); + } else { + event_loop.timeout(token, timer.delay).expect("Error re-registering user timer"); + } + self.worker_channel.push(Work { + work_type: WorkType::Timeout, + token: token_id, + handler: handler.clone(), + handler_id: handler_index + }); + self.work_ready.notify_all(); + } + } + } } enum Handlers where Message: Send { - SharedCollection(Weak>>>>), - Single(Weak>), + SharedCollection(Weak>>>>), + Single(Weak>), } impl Clone for Handlers { @@ -413,13 +423,13 @@ impl IoChannel where Message: Send + Sync + 'static { } /// Create a new synchronous channel to a given handler. - pub fn to_handler(handler: Weak>) -> IoChannel { + pub fn to_handler(handler: Weak>) -> IoChannel { IoChannel { channel: None, handlers: Handlers::Single(handler), } } - fn new(channel: Sender>, handlers: Weak>>>>) -> IoChannel { + fn new(channel: Sender>, handlers: Weak>>>>) -> IoChannel { IoChannel { channel: Some(channel), handlers: Handlers::SharedCollection(handlers), @@ -432,7 +442,7 @@ impl IoChannel where Message: Send + Sync + 'static { pub struct IoService where Message: Send + Sync + 'static { thread: Option>, host_channel: Mutex>>, - handlers: Arc>>>>, + handlers: Arc>>>>, } impl IoService where Message: Send + Sync + 'static { @@ -469,7 +479,7 @@ impl IoService where Message: Send + Sync + 'static { } /// Regiter an IO handler with the event loop. - pub fn register_handler(&self, handler: Arc+Send>) -> Result<(), IoError> { + pub fn register_handler(&self, handler: Arc+Send>) -> Result<(), IoError> { self.host_channel.lock().send(IoMessage::AddHandler { handler: handler, })?; diff --git a/util/io/src/service_non_mio.rs b/util/io/src/service_non_mio.rs index 1cef3574e..9be1d5971 100644 --- a/util/io/src/service_non_mio.rs +++ b/util/io/src/service_non_mio.rs @@ -16,15 +16,18 @@ use std::sync::{Arc, Weak}; use std::thread; -use deque; -use slab::Slab; -use fnv::FnvHashMap; -use {IoError, IoHandler}; -use parking_lot::{RwLock, Mutex}; -use num_cpus; use std::time::Duration; -use timer::{Timer, Guard as TimerGuard}; + +use crossbeam_deque as deque; +use fnv::FnvHashMap; +use log::{trace, debug}; +use num_cpus; +use parking_lot::{Mutex, RwLock}; +use slab::Slab; use time::Duration as TimeDuration; +use timer::{Guard as TimerGuard, Timer}; + +use crate::{IoError, IoHandler}; /// Timer ID pub type TimerToken = usize; @@ -48,7 +51,7 @@ impl IoContext where Message: Send + Sync + 'static { let msg = WorkTask::TimerTrigger { handler_id: self.handler, - token: token, + token, }; let delay = TimeDuration::from_std(delay) @@ -68,7 +71,7 @@ impl IoContext where Message: Send + Sync + 'static { let msg = WorkTask::TimerTrigger { handler_id: self.handler, - token: token, + token, }; let delay = TimeDuration::from_std(delay) @@ -189,7 +192,7 @@ pub struct IoService where Message: Send + Sync + 'static { // Struct shared throughout the whole implementation. struct Shared where Message: Send + Sync + 'static { // All the I/O handlers that have been registered. - handlers: RwLock>>>, + handlers: RwLock>>>, // All the background threads, so that we can unpark them. threads: RwLock>, // Used to create timeouts. @@ -273,7 +276,7 @@ impl IoService where Message: Send + Sync + 'static { } /// Register an IO handler with the event loop. - pub fn register_handler(&self, handler: Arc+Send>) -> Result<(), IoError> { + pub fn register_handler(&self, handler: Arc+Send>) -> Result<(), IoError> { let id = self.shared.handlers.write().insert(handler.clone()); assert!(id <= MAX_HANDLERS, "Too many handlers registered"); let ctxt = IoContext { handler: id, shared: self.shared.clone() }; diff --git a/util/io/src/worker.rs b/util/io/src/worker.rs index 458882a85..a57f3b200 100644 --- a/util/io/src/worker.rs +++ b/util/io/src/worker.rs @@ -14,17 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use futures::future::{self, Loop}; -use std::sync::Arc; -use std::thread::{JoinHandle, self}; -use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; -use deque; -use service_mio::{HandlerId, IoChannel, IoContext}; -use tokio::{self}; -use IoHandler; -use LOCAL_STACK_SIZE; +use std::{ + sync::{Arc, atomic::{AtomicBool, Ordering as AtomicOrdering}}, + thread::{self, JoinHandle}, +}; +use crossbeam_deque as deque; +use futures::future::{self, Loop}; +use log::{trace, error}; use parking_lot::{Condvar, Mutex}; +use tokio; + +use crate::{ + IoHandler, + LOCAL_STACK_SIZE, + service_mio::{HandlerId, IoChannel, IoContext}, +}; const STACK_SIZE: usize = 16*1024*1024; @@ -40,7 +45,7 @@ pub struct Work { pub work_type: WorkType, pub token: usize, pub handler_id: HandlerId, - pub handler: Arc>, + pub handler: Arc>, } /// An IO worker thread @@ -54,13 +59,15 @@ pub struct Worker { impl Worker { /// Creates a new worker instance. - pub fn new(index: usize, - stealer: deque::Stealer>, - channel: IoChannel, - wait: Arc, - wait_mutex: Arc>, - ) -> Worker - where Message: Send + Sync + 'static { + pub fn new( + index: usize, + stealer: deque::Stealer>, + channel: IoChannel, + wait: Arc, + wait_mutex: Arc>, + ) -> Worker + where Message: Send + Sync + 'static + { let deleting = Arc::new(AtomicBool::new(false)); let mut worker = Worker { thread: None,