usb_context.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/usb/usb_context.h"
  5. #include <memory>
  6. #include "base/atomicops.h"
  7. #include "base/logging.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/threading/simple_thread.h"
  10. #include "base/threading/thread_restrictions.h"
  11. #include "services/device/usb/usb_error.h"
  12. #include "third_party/libusb/src/libusb/interrupt.h"
  13. #include "third_party/libusb/src/libusb/libusb.h"
  14. namespace device {
  15. // The UsbEventHandler works around a design flaw in the libusb interface. There
  16. // is currently no way to signal to libusb that any caller into one of the event
  17. // handler calls should return without handling any events.
  18. class UsbContext::UsbEventHandler : public base::SimpleThread {
  19. public:
  20. explicit UsbEventHandler(libusb_context* context);
  21. UsbEventHandler(const UsbEventHandler&) = delete;
  22. UsbEventHandler& operator=(const UsbEventHandler&) = delete;
  23. ~UsbEventHandler() override;
  24. // base::SimpleThread
  25. void Run() override;
  26. void Stop();
  27. private:
  28. base::subtle::Atomic32 running_;
  29. raw_ptr<libusb_context> context_;
  30. };
  31. UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context)
  32. : base::SimpleThread("UsbEventHandler"), context_(context) {
  33. base::subtle::Release_Store(&running_, 1);
  34. }
  35. UsbContext::UsbEventHandler::~UsbEventHandler() {
  36. libusb_exit(context_);
  37. }
  38. void UsbContext::UsbEventHandler::Run() {
  39. VLOG(1) << "UsbEventHandler started.";
  40. while (base::subtle::Acquire_Load(&running_)) {
  41. const int rv = libusb_handle_events(context_);
  42. if (rv != LIBUSB_SUCCESS) {
  43. VLOG(1) << "Failed to handle events: "
  44. << ConvertPlatformUsbErrorToString(rv);
  45. }
  46. }
  47. VLOG(1) << "UsbEventHandler shutting down.";
  48. }
  49. void UsbContext::UsbEventHandler::Stop() {
  50. base::subtle::Release_Store(&running_, 0);
  51. libusb_interrupt_handle_event(context_);
  52. }
  53. UsbContext::UsbContext(PlatformUsbContext context) : context_(context) {
  54. // Ownership of the PlatformUsbContext is passed to the event handler thread.
  55. event_handler_ = std::make_unique<UsbEventHandler>(context_);
  56. event_handler_->Start();
  57. }
  58. UsbContext::~UsbContext() {
  59. event_handler_->Stop();
  60. // Temporary workaround for https://crbug.com/1150182 until the libusb backend
  61. // is removed in https://crbug.com/1096743. The last outstanding transfer can
  62. // cause this class to be released on a worker thread where blocking is not
  63. // typically allowed. Make an exception here as this will only occur during
  64. // shutdown.
  65. base::ScopedAllowBaseSyncPrimitives allow_sync;
  66. event_handler_->Join();
  67. }
  68. } // namespace device