From 22a63f6fd391a31ddc263d51bc0be5578b6288dc Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 9 Dec 2016 11:45:16 +0100 Subject: [PATCH 1/2] Added IO Service explicit stop --- util/io/src/service.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/util/io/src/service.rs b/util/io/src/service.rs index 5d773f525..d7bba5b4d 100644 --- a/util/io/src/service.rs +++ b/util/io/src/service.rs @@ -430,6 +430,20 @@ impl IoService where Message: Send + Sync + Clone + 'static { }) } + pub fn stop(&mut self) { + trace!(target: "shutdown", "[IoService] Closing..."); + // Clear handlers so that shared pointers are not stuck on stack + // in Channel::send_sync + self.handlers.write().clear(); + self.host_channel.lock().send(IoMessage::Shutdown).unwrap_or_else(|e| warn!("Error on IO service shutdown: {:?}", e)); + if let Some(thread) = self.thread.take() { + thread.join().unwrap_or_else(|e| { + debug!(target: "shutdown", "Error joining IO service event loop thread: {:?}", e); + }); + } + trace!(target: "shutdown", "[IoService] Closed."); + } + /// Regiter an IO handler with the event loop. pub fn register_handler(&self, handler: Arc+Send>) -> Result<(), IoError> { try!(self.host_channel.lock().send(IoMessage::AddHandler { @@ -452,17 +466,7 @@ impl IoService where Message: Send + Sync + Clone + 'static { impl Drop for IoService where Message: Send + Sync + Clone { fn drop(&mut self) { - trace!(target: "shutdown", "[IoService] Closing..."); - // Clear handlers so that shared pointers are not stuck on stack - // in Channel::send_sync - self.handlers.write().clear(); - self.host_channel.lock().send(IoMessage::Shutdown).unwrap_or_else(|e| warn!("Error on IO service shutdown: {:?}", e)); - if let Some(thread) = self.thread.take() { - thread.join().unwrap_or_else(|e| { - debug!(target: "shutdown", "Error joining IO service event loop thread: {:?}", e); - }); - } - trace!(target: "shutdown", "[IoService] Closed."); + self.stop() } } From 364dd9dda4b7d3c880323e593795a610612ba2ca Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 9 Dec 2016 13:25:02 +0100 Subject: [PATCH 2/2] Make stop const --- util/io/src/service.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/util/io/src/service.rs b/util/io/src/service.rs index d7bba5b4d..6086acadd 100644 --- a/util/io/src/service.rs +++ b/util/io/src/service.rs @@ -394,7 +394,7 @@ impl IoChannel where Message: Send + Clone + Sync + 'static { /// 'Message' is a notification message type pub struct IoService where Message: Send + Sync + Clone + 'static { panic_handler: Arc, - thread: Option>, + thread: Mutex>>, host_channel: Mutex>>, handlers: Arc>, HandlerId>>>, } @@ -424,19 +424,19 @@ impl IoService where Message: Send + Sync + Clone + 'static { }); Ok(IoService { panic_handler: panic_handler, - thread: Some(thread), + thread: Mutex::new(Some(thread)), host_channel: Mutex::new(channel), handlers: handlers, }) } - pub fn stop(&mut self) { + pub fn stop(&self) { trace!(target: "shutdown", "[IoService] Closing..."); // Clear handlers so that shared pointers are not stuck on stack // in Channel::send_sync self.handlers.write().clear(); self.host_channel.lock().send(IoMessage::Shutdown).unwrap_or_else(|e| warn!("Error on IO service shutdown: {:?}", e)); - if let Some(thread) = self.thread.take() { + if let Some(thread) = self.thread.lock().take() { thread.join().unwrap_or_else(|e| { debug!(target: "shutdown", "Error joining IO service event loop thread: {:?}", e); });