refactor (hardware wallet) : reduce the number of threads#9644
Conversation
| pub fn new(hidapi: Arc<Mutex<hidapi::HidApi>>, exiting: Arc<AtomicBool>) -> Result<Arc<Self>, libusb::Error> { | ||
| let manager = Arc::new(Self { | ||
| usb: hidapi, | ||
| pub fn new(usb: Arc<Mutex<hidapi::HidApi>>) -> Arc<Self> { |
There was a problem hiding this comment.
doesn't make sense that this function returns Result anymore because it can't fail anymore
| }) | ||
| } | ||
|
|
||
| // Transport Protocol: |
There was a problem hiding this comment.
fix formatting!!!
|
@niklasad1 what's the status of this? Do you want feedback from @dvdplm @debris ? |
|
@5chdn Need grab to hardware wallets and test this first. I will change the label when it is good for reviewing hopefully later today |
013abd3 to
aacb057
Compare
334c6fb to
c26a70f
Compare
c26a70f to
79ba32d
Compare
|
👍 needs a 2nd review |
dvdplm
left a comment
There was a problem hiding this comment.
A few nits, but overall looks good.
| /// Hardware wallet event handler | ||
| /// | ||
| /// Note, that this run to completion and race-conditions can't occur but this can | ||
| /// therefore starve other events for being process with a spinlock or similar |
There was a problem hiding this comment.
| /// therefore starve other events for being process with a spinlock or similar | |
| /// stop other events from being processed with a spinlock or similar. |
(but I'm not sure what you mean by "with spinlock or similar" tbh)
There was a problem hiding this comment.
Spinlock is a bit blurry in this context, should probably say spinning instead.
I mean that if the thread is in an infinite loop or looping for a condition it will prevent other events from being processed. I can rephrase it but not really introduced in this PR
| impl libusb::Hotplug for EventHandler { | ||
| fn device_arrived(&mut self, device: libusb::Device) { | ||
| // Upgrade reference to an Arc | ||
| if let (Some(ledger), Some(trezor)) = (self.ledger.upgrade(), self.trezor.upgrade()) { |
There was a problem hiding this comment.
q: is it impossible to have one upgrade call return Some and the other None? Wouldn't that cause the whole block to be skipped? In other words, are both upgrade() calls going to return Some if one of them does?
There was a problem hiding this comment.
Good question, because the HardwareWalletManager keeps an Arc for both Ledger and Trezor for the entire lifetime of Parity unless it is disabled by the cli arg --no-hardware-wallet! upgrade()will always succeed as long theHardwareWalletManageris enabled (the strong count will be at least 1). So, either bothTrezorandLedger` is enabled or none is enabled.
However, I admit it is a bit sloppy. Theoretically, interleaving "could" happen if one of the hw-wallets has been dropped and the other has not been dropped but this is matter in granularity in terms of microseconds and I don't worry too much about this.
I can change it if you like? :P
|
|
||
| fn device_left(&mut self, device: libusb::Device) { | ||
| // Upgrade reference to an Arc | ||
| if let (Some(ledger), Some(trezor)) = (self.ledger.upgrade(), self.trezor.upgrade()) { |
09649d6 to
fa2324a
Compare
fa2324a to
f099f3d
Compare
|
Please resolve conflicts :) |
This changes parity to use one thread instead of two to subscribe USB events via the hardware-wallets.
Co-Authored-By: niklasad1 <niklasadolfsson1@gmail.com>
Co-Authored-By: niklasad1 <niklasadolfsson1@gmail.com>
Co-Authored-By: niklasad1 <niklasadolfsson1@gmail.com>
* Clarify bad explaination of `run-to-completion` * Replace magic numbers with constants
f099f3d to
0329802
Compare
Attempt to close #9622 along with fixing some minor formatting things!
It introduces some boiler-plate code though such as:
Tested successfully both on
TrezorandLedgeron LinuxProof that number of threads have been reduced: