From aaff9344fd58968590a91405045ca10d122f3a70 Mon Sep 17 00:00:00 2001 From: niklasad1 Date: Thu, 22 Feb 2018 23:21:37 +0100 Subject: [PATCH] Address feedback * Remove thread joining in HardwareWalletManager * Remove thread handlers in HardwareWalletManager because this makes them unused --- hw/src/lib.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/hw/src/lib.rs b/hw/src/lib.rs index af428cc5a65..828c15d7085 100644 --- a/hw/src/lib.rs +++ b/hw/src/lib.rs @@ -130,7 +130,6 @@ impl From for Error { /// Hardware wallet management interface. pub struct HardwareWalletManager { - active_threads: Vec>>, exiting: Arc, ledger: Arc, trezor: Arc, @@ -142,7 +141,6 @@ impl HardwareWalletManager { pub fn new() -> Result { let usb_context_trezor = Arc::new(libusb::Context::new()?); let usb_context_ledger = Arc::new(libusb::Context::new()?); - let mut active_threads: Vec>> = Vec::new(); let hidapi = Arc::new(Mutex::new(hidapi::HidApi::new().map_err(|e| Error::Hid(e.to_string().clone()))?)); let ledger = Arc::new(ledger::Manager::new(hidapi.clone())); let trezor = Arc::new(trezor::Manager::new(hidapi.clone())); @@ -169,7 +167,7 @@ impl HardwareWalletManager { let t = trezor.clone(); // Ledger event thread - active_threads.push(thread::Builder::new() + thread::Builder::new() .name("hw_wallet_ledger".to_string()) .spawn(move || { if let Err(e) = l.update_devices() { @@ -184,10 +182,10 @@ impl HardwareWalletManager { } } }) - .ok()); + .ok(); // Trezor event thread - active_threads.push(thread::Builder::new() + thread::Builder::new() .name("hw_wallet_trezor".to_string()) .spawn(move || { if let Err(e) = t.update_devices() { @@ -201,10 +199,9 @@ impl HardwareWalletManager { } } }) - .ok()); + .ok(); Ok(HardwareWalletManager { - active_threads: active_threads, exiting: exiting, ledger: ledger, trezor: trezor, @@ -258,12 +255,10 @@ impl HardwareWalletManager { impl Drop for HardwareWalletManager { fn drop(&mut self) { + // Indicate to the USB Hotplug handlers that they + // shall terminate but don't wait for them to terminate. + // If they don't terminate for some reason USB Hotplug events will be handled + // even if the HardwareWalletManger has been dropped self.exiting.store(true, atomic::Ordering::Release); - for thread in self.active_threads.iter_mut() { - if let Some(thread) = thread.take() { - thread.thread().unpark(); - thread.join().ok(); - } - } } }