diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index bcad929..a68f7fb 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -21,7 +21,6 @@ fnv = "1.0" futures-preview = { version = "0.3.0-alpha.14", features = ["compat"] } humantime = "1.0" log = "0.4" -parking_lot = "0.7" pin-utils = "0.1.0-alpha.4" rand = "0.6" tokio-timer = "0.2" diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 7ed4f0a..46ff8b2 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -45,13 +45,7 @@ use futures::{ task::{Poll, Spawn, SpawnError, SpawnExt}, Future, }; -use parking_lot::Mutex; -use std::{ - cell::RefCell, - io, - sync::{Arc, Once}, - time::SystemTime, -}; +use std::{cell::RefCell, io, sync::Once, time::SystemTime}; /// A message from a client to a server. #[derive(Debug)] @@ -155,14 +149,14 @@ impl Request { pub(crate) type PollIo = Poll>>; static INIT: Once = Once::new(); -static mut SEED_SPAWN: Option>> = None; +static mut SEED_SPAWN: Option> = None; thread_local! { - static SPAWN: RefCell>> = { + static SPAWN: RefCell> = { unsafe { // INIT must always be called before accessing SPAWN. // Otherwise, accessing SPAWN can trigger undefined behavior due to race conditions. INIT.call_once(|| {}); - RefCell::new(SEED_SPAWN.clone().expect("init() must be called.")) + RefCell::new(SEED_SPAWN.as_ref().expect("init() must be called.").box_clone()) } }; } @@ -172,14 +166,24 @@ thread_local! { /// /// Init only has an effect the first time it is called. If called previously, successive calls to /// init are noops. -pub fn init(spawn: impl Spawn + 'static) { +pub fn init(spawn: impl Spawn + Clone + 'static) { unsafe { INIT.call_once(|| { - SEED_SPAWN = Some(Arc::new(Mutex::new(spawn))); + SEED_SPAWN = Some(Box::new(spawn)); }); } } pub(crate) fn spawn(future: impl Future + Send + 'static) -> Result<(), SpawnError> { - SPAWN.with(|spawn| spawn.borrow_mut().lock().spawn(future)) + SPAWN.with(|spawn| spawn.borrow_mut().spawn(future)) +} + +trait CloneSpawn: Spawn { + fn box_clone(&self) -> Box; +} + +impl CloneSpawn for S { + fn box_clone(&self) -> Box { + Box::new(self.clone()) + } }