Fix compiler warnings in util/io and upgrade to edition 2018 Upgrade mio to latest (#10953)

* Initial cleanup

* Fix compiler warnings in util/io and upgrade to edition 2018
Upgrade mio to latest version

* cleanup

* Update util/io/src/service_mio.rs

Co-Authored-By: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* review suggestion
This commit is contained in:
David 2019-08-08 09:41:22 +02:00 committed by Niklas Adolfsson
parent 45978bc2bd
commit 49f219451b
6 changed files with 111 additions and 103 deletions

31
Cargo.lock generated
View File

@ -1024,7 +1024,7 @@ dependencies = [
"fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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-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-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)", "parity-path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2380,14 +2380,13 @@ dependencies = [
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.6.16" version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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 = [ dependencies = [
"lazycell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.4.6 (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)",
"miow 0.3.3 (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)", "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 = [ dependencies = [
"iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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]] [[package]]
@ -2666,7 +2665,7 @@ dependencies = [
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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]] [[package]]
@ -4134,7 +4133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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-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-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)", "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 = [ dependencies = [
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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-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)", "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)", "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)", "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)", "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-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-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)", "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)", "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)", "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)", "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)", "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-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)", "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)", "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)", "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)", "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)", "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)", "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)", "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 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 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 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-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-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" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125"

View File

@ -1,14 +1,15 @@
[package] [package]
name = "ethcore-io"
description = "Ethcore IO library" description = "Ethcore IO library"
version = "1.12.0"
homepage = "http://parity.io" homepage = "http://parity.io"
license = "GPL-3.0" license = "GPL-3.0"
name = "ethcore-io"
version = "1.12.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies] [dependencies]
fnv = "1.0" fnv = "1.0"
mio = { version = "0.6.8", optional = true } mio = { version = "0.6.19", optional = true }
crossbeam-deque = "0.6" crossbeam-deque = "0.6"
parking_lot = "0.8" parking_lot = "0.8"
log = "0.4" log = "0.4"
@ -18,3 +19,4 @@ timer = "0.2"
time = "0.1" time = "0.1"
tokio = "0.1" tokio = "0.1"
futures = "0.1" futures = "0.1"

View File

@ -69,20 +69,6 @@
//TODO: use Poll from mio //TODO: use Poll from mio
#![allow(deprecated)] #![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")] #[cfg(feature = "mio")]
mod service_mio; mod service_mio;
#[cfg(not(feature = "mio"))] #[cfg(not(feature = "mio"))]
@ -170,7 +156,7 @@ pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + 'static {
/// Re-register a stream with the event loop /// Re-register a stream with the event loop
#[cfg(feature = "mio")] #[cfg(feature = "mio")]
fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {} fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
/// 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")] #[cfg(feature = "mio")]
fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop<IoManager<Message>>) {} fn deregister_stream(&self, _stream: StreamToken, _event_loop: &mut EventLoop<IoManager<Message>>) {}
} }
@ -178,14 +164,15 @@ pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + 'static {
#[cfg(feature = "mio")] #[cfg(feature = "mio")]
pub use service_mio::{TimerToken, StreamToken, IoContext, IoService, IoChannel, IoManager, TOKENS_PER_HANDLER}; pub use service_mio::{TimerToken, StreamToken, IoContext, IoService, IoChannel, IoManager, TOKENS_PER_HANDLER};
#[cfg(not(feature = "mio"))] #[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)] #[cfg(test)]
mod tests { mod tests {
use std::sync::Arc; use std::{
use std::sync::atomic; sync::{Arc, atomic},
use std::thread; thread,
use std::time::Duration; time::Duration,
};
use super::*; use super::*;
// Mio's behaviour is too unstable for this test. Sometimes we have to wait a few milliseconds, // Mio's behaviour is too unstable for this test. Sometimes we have to wait a few milliseconds,

View File

@ -14,19 +14,24 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::collections::HashMap;
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::thread::{self, JoinHandle}; 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 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 /// Timer ID
pub type TimerToken = usize; pub type TimerToken = usize;
/// Timer ID /// Timer ID
@ -45,7 +50,7 @@ pub enum IoMessage<Message> where Message: Send + Sized {
Shutdown, Shutdown,
/// Register a new protocol handler. /// Register a new protocol handler.
AddHandler { AddHandler {
handler: Arc<IoHandler<Message>+Send>, handler: Arc<dyn IoHandler<Message> + Send>,
}, },
RemoveHandler { RemoveHandler {
handler_id: HandlerId, handler_id: HandlerId,
@ -182,7 +187,7 @@ struct UserTimer {
/// Root IO handler. Manages user handlers, messages and IO timers. /// Root IO handler. Manages user handlers, messages and IO timers.
pub struct IoManager<Message> where Message: Send + Sync { pub struct IoManager<Message> where Message: Send + Sync {
timers: Arc<RwLock<HashMap<HandlerId, UserTimer>>>, timers: Arc<RwLock<HashMap<HandlerId, UserTimer>>>,
handlers: Arc<RwLock<Slab<Arc<IoHandler<Message>>>>>, handlers: Arc<RwLock<Slab<Arc<dyn IoHandler<Message>>>>>,
workers: Vec<Worker>, workers: Vec<Worker>,
worker_channel: deque::Worker<Work<Message>>, worker_channel: deque::Worker<Work<Message>>,
work_ready: Arc<Condvar>, work_ready: Arc<Condvar>,
@ -192,7 +197,7 @@ impl<Message> IoManager<Message> where Message: Send + Sync + 'static {
/// Creates a new instance and registers it with the event loop. /// Creates a new instance and registers it with the event loop.
pub fn start( pub fn start(
event_loop: &mut EventLoop<IoManager<Message>>, event_loop: &mut EventLoop<IoManager<Message>>,
handlers: Arc<RwLock<Slab<Arc<IoHandler<Message>>>>> handlers: Arc<RwLock<Slab<Arc<dyn IoHandler<Message>>>>>
) -> Result<(), IoError> { ) -> Result<(), IoError> {
let (worker, stealer) = deque::fifo(); let (worker, stealer) = deque::fifo();
let num_workers = 4; let num_workers = 4;
@ -243,24 +248,6 @@ impl<Message> Handler for IoManager<Message> where Message: Send + Sync + 'stati
} }
} }
fn timeout(&mut self, event_loop: &mut EventLoop<Self>, 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<Self>, msg: Self::Message) { fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Self::Message) {
match msg { match msg {
IoMessage::Shutdown => { IoMessage::Shutdown => {
@ -331,11 +318,34 @@ impl<Message> Handler for IoManager<Message> where Message: Send + Sync + 'stati
} }
} }
} }
fn timeout(&mut self, event_loop: &mut EventLoop<Self>, 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<Message> where Message: Send { enum Handlers<Message> where Message: Send {
SharedCollection(Weak<RwLock<Slab<Arc<IoHandler<Message>>>>>), SharedCollection(Weak<RwLock<Slab<Arc<dyn IoHandler<Message>>>>>),
Single(Weak<IoHandler<Message>>), Single(Weak<dyn IoHandler<Message>>),
} }
impl<Message: Send> Clone for Handlers<Message> { impl<Message: Send> Clone for Handlers<Message> {
@ -413,13 +423,13 @@ impl<Message> IoChannel<Message> where Message: Send + Sync + 'static {
} }
/// Create a new synchronous channel to a given handler. /// Create a new synchronous channel to a given handler.
pub fn to_handler(handler: Weak<IoHandler<Message>>) -> IoChannel<Message> { pub fn to_handler(handler: Weak<dyn IoHandler<Message>>) -> IoChannel<Message> {
IoChannel { IoChannel {
channel: None, channel: None,
handlers: Handlers::Single(handler), handlers: Handlers::Single(handler),
} }
} }
fn new(channel: Sender<IoMessage<Message>>, handlers: Weak<RwLock<Slab<Arc<IoHandler<Message>>>>>) -> IoChannel<Message> { fn new(channel: Sender<IoMessage<Message>>, handlers: Weak<RwLock<Slab<Arc<dyn IoHandler<Message>>>>>) -> IoChannel<Message> {
IoChannel { IoChannel {
channel: Some(channel), channel: Some(channel),
handlers: Handlers::SharedCollection(handlers), handlers: Handlers::SharedCollection(handlers),
@ -432,7 +442,7 @@ impl<Message> IoChannel<Message> where Message: Send + Sync + 'static {
pub struct IoService<Message> where Message: Send + Sync + 'static { pub struct IoService<Message> where Message: Send + Sync + 'static {
thread: Option<JoinHandle<()>>, thread: Option<JoinHandle<()>>,
host_channel: Mutex<Sender<IoMessage<Message>>>, host_channel: Mutex<Sender<IoMessage<Message>>>,
handlers: Arc<RwLock<Slab<Arc<IoHandler<Message>>>>>, handlers: Arc<RwLock<Slab<Arc<dyn IoHandler<Message>>>>>,
} }
impl<Message> IoService<Message> where Message: Send + Sync + 'static { impl<Message> IoService<Message> where Message: Send + Sync + 'static {
@ -469,7 +479,7 @@ impl<Message> IoService<Message> where Message: Send + Sync + 'static {
} }
/// Regiter an IO handler with the event loop. /// Regiter an IO handler with the event loop.
pub fn register_handler(&self, handler: Arc<IoHandler<Message>+Send>) -> Result<(), IoError> { pub fn register_handler(&self, handler: Arc<dyn IoHandler<Message>+Send>) -> Result<(), IoError> {
self.host_channel.lock().send(IoMessage::AddHandler { self.host_channel.lock().send(IoMessage::AddHandler {
handler: handler, handler: handler,
})?; })?;

View File

@ -16,15 +16,18 @@
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::thread; 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 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 time::Duration as TimeDuration;
use timer::{Guard as TimerGuard, Timer};
use crate::{IoError, IoHandler};
/// Timer ID /// Timer ID
pub type TimerToken = usize; pub type TimerToken = usize;
@ -48,7 +51,7 @@ impl<Message> IoContext<Message> where Message: Send + Sync + 'static {
let msg = WorkTask::TimerTrigger { let msg = WorkTask::TimerTrigger {
handler_id: self.handler, handler_id: self.handler,
token: token, token,
}; };
let delay = TimeDuration::from_std(delay) let delay = TimeDuration::from_std(delay)
@ -68,7 +71,7 @@ impl<Message> IoContext<Message> where Message: Send + Sync + 'static {
let msg = WorkTask::TimerTrigger { let msg = WorkTask::TimerTrigger {
handler_id: self.handler, handler_id: self.handler,
token: token, token,
}; };
let delay = TimeDuration::from_std(delay) let delay = TimeDuration::from_std(delay)
@ -189,7 +192,7 @@ pub struct IoService<Message> where Message: Send + Sync + 'static {
// Struct shared throughout the whole implementation. // Struct shared throughout the whole implementation.
struct Shared<Message> where Message: Send + Sync + 'static { struct Shared<Message> where Message: Send + Sync + 'static {
// All the I/O handlers that have been registered. // All the I/O handlers that have been registered.
handlers: RwLock<Slab<Arc<IoHandler<Message>>>>, handlers: RwLock<Slab<Arc<dyn IoHandler<Message>>>>,
// All the background threads, so that we can unpark them. // All the background threads, so that we can unpark them.
threads: RwLock<Vec<thread::Thread>>, threads: RwLock<Vec<thread::Thread>>,
// Used to create timeouts. // Used to create timeouts.
@ -273,7 +276,7 @@ impl<Message> IoService<Message> where Message: Send + Sync + 'static {
} }
/// Register an IO handler with the event loop. /// Register an IO handler with the event loop.
pub fn register_handler(&self, handler: Arc<IoHandler<Message>+Send>) -> Result<(), IoError> { pub fn register_handler(&self, handler: Arc<dyn IoHandler<Message>+Send>) -> Result<(), IoError> {
let id = self.shared.handlers.write().insert(handler.clone()); let id = self.shared.handlers.write().insert(handler.clone());
assert!(id <= MAX_HANDLERS, "Too many handlers registered"); assert!(id <= MAX_HANDLERS, "Too many handlers registered");
let ctxt = IoContext { handler: id, shared: self.shared.clone() }; let ctxt = IoContext { handler: id, shared: self.shared.clone() };

View File

@ -14,17 +14,22 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use futures::future::{self, Loop}; use std::{
use std::sync::Arc; sync::{Arc, atomic::{AtomicBool, Ordering as AtomicOrdering}},
use std::thread::{JoinHandle, self}; thread::{self, JoinHandle},
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 crossbeam_deque as deque;
use futures::future::{self, Loop};
use log::{trace, error};
use parking_lot::{Condvar, Mutex}; 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; const STACK_SIZE: usize = 16*1024*1024;
@ -40,7 +45,7 @@ pub struct Work<Message> {
pub work_type: WorkType<Message>, pub work_type: WorkType<Message>,
pub token: usize, pub token: usize,
pub handler_id: HandlerId, pub handler_id: HandlerId,
pub handler: Arc<IoHandler<Message>>, pub handler: Arc<dyn IoHandler<Message>>,
} }
/// An IO worker thread /// An IO worker thread
@ -54,13 +59,15 @@ pub struct Worker {
impl Worker { impl Worker {
/// Creates a new worker instance. /// Creates a new worker instance.
pub fn new<Message>(index: usize, pub fn new<Message>(
index: usize,
stealer: deque::Stealer<Work<Message>>, stealer: deque::Stealer<Work<Message>>,
channel: IoChannel<Message>, channel: IoChannel<Message>,
wait: Arc<Condvar>, wait: Arc<Condvar>,
wait_mutex: Arc<Mutex<()>>, wait_mutex: Arc<Mutex<()>>,
) -> Worker ) -> Worker
where Message: Send + Sync + 'static { where Message: Send + Sync + 'static
{
let deleting = Arc::new(AtomicBool::new(false)); let deleting = Arc::new(AtomicBool::new(false));
let mut worker = Worker { let mut worker = Worker {
thread: None, thread: None,