From 8844ed9697a385e1182bd58ed8a1068d52c370de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 21 Aug 2019 10:31:33 +0300 Subject: switch to jod threads --- crates/thread_worker/src/lib.rs | 39 +++++---------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) (limited to 'crates/thread_worker/src/lib.rs') diff --git a/crates/thread_worker/src/lib.rs b/crates/thread_worker/src/lib.rs index d8d0d9bf2..68e5c124d 100644 --- a/crates/thread_worker/src/lib.rs +++ b/crates/thread_worker/src/lib.rs @@ -1,39 +1,7 @@ //! Small utility to correctly spawn crossbeam-channel based worker threads. -use std::thread; - use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; -/// Like `std::thread::JoinHandle<()>`, but joins thread in drop automatically. -pub struct ScopedThread { - // Option for drop - inner: Option>, -} - -impl Drop for ScopedThread { - fn drop(&mut self) { - let inner = self.inner.take().unwrap(); - let name = inner.thread().name().unwrap().to_string(); - log::info!("waiting for {} to finish...", name); - let res = inner.join(); - log::info!(".. {} terminated with {}", name, if res.is_ok() { "ok" } else { "err" }); - - // escalate panic, but avoid aborting the process - if let Err(e) = res { - if !thread::panicking() { - panic!(e) - } - } - } -} - -impl ScopedThread { - pub fn spawn(name: &'static str, f: impl FnOnce() + Send + 'static) -> ScopedThread { - let inner = thread::Builder::new().name(name.into()).spawn(f).unwrap(); - ScopedThread { inner: Some(inner) } - } -} - /// A wrapper around event-processing thread with automatic shutdown semantics. pub struct Worker { // XXX: field order is significant here. @@ -48,7 +16,7 @@ pub struct Worker { // single client, so, if we are shutting down, nobody is interested in the // unfinished work anyway! sender: Sender, - _thread: ScopedThread, + _thread: jod_thread::JoinHandle<()>, receiver: Receiver, } @@ -63,7 +31,10 @@ impl Worker { // and output buffers to a fixed size, a worker might get stuck. let (sender, input_receiver) = bounded::(buf); let (output_sender, receiver) = unbounded::(); - let _thread = ScopedThread::spawn(name, move || f(input_receiver, output_sender)); + let _thread = jod_thread::Builder::new() + .name(name.to_string()) + .spawn(move || f(input_receiver, output_sender)) + .expect("failed to spawn a thread"); Worker { sender, _thread, receiver } } } -- cgit v1.2.3