| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/usb/usb_context.h" | 5 #include "device/usb/usb_context.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 11 #include "device/usb/usb_error.h" | 10 #include "device/usb/usb_error.h" |
| 12 #include "third_party/libusb/src/libusb/interrupt.h" | 11 #include "third_party/libusb/src/libusb/interrupt.h" |
| 13 #include "third_party/libusb/src/libusb/libusb.h" | 12 #include "third_party/libusb/src/libusb/libusb.h" |
| 14 | 13 |
| 15 namespace device { | 14 namespace device { |
| 16 | 15 |
| 17 // The UsbEventHandler works around a design flaw in the libusb interface. There | 16 // The UsbEventHandler works around a design flaw in the libusb interface. There |
| 18 // is currently no way to signal to libusb that any caller into one of the event | 17 // is currently no way to signal to libusb that any caller into one of the event |
| 19 // handler calls should return without handling any events. | 18 // handler calls should return without handling any events. |
| 20 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { | 19 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { |
| 21 public: | 20 public: |
| 22 explicit UsbEventHandler(libusb_context* context); | 21 explicit UsbEventHandler(libusb_context* context); |
| 23 ~UsbEventHandler() override; | 22 ~UsbEventHandler() override; |
| 24 | 23 |
| 25 // base::PlatformThread::Delegate | 24 // base::PlatformThread::Delegate |
| 26 void ThreadMain() override; | 25 void ThreadMain() override; |
| 27 | 26 |
| 27 void Stop(); |
| 28 |
| 28 private: | 29 private: |
| 29 base::subtle::Atomic32 running_; | 30 base::subtle::Atomic32 running_; |
| 30 libusb_context* context_; | 31 libusb_context* context_; |
| 31 base::PlatformThreadHandle thread_handle_; | 32 base::PlatformThreadHandle thread_handle_; |
| 32 base::WaitableEvent start_polling_; | |
| 33 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); | 33 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) | 36 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) |
| 37 : context_(context), thread_handle_(0), start_polling_(false, false) { | 37 : context_(context), thread_handle_(0) { |
| 38 base::subtle::Release_Store(&running_, 1); | 38 base::subtle::Release_Store(&running_, 1); |
| 39 bool success = base::PlatformThread::Create(0, this, &thread_handle_); | 39 bool success = base::PlatformThread::Create(0, this, &thread_handle_); |
| 40 DCHECK(success) << "Failed to create USB IO handling thread."; | 40 DCHECK(success) << "Failed to create USB IO handling thread."; |
| 41 start_polling_.Wait(); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 UsbContext::UsbEventHandler::~UsbEventHandler() { | 43 UsbContext::UsbEventHandler::~UsbEventHandler() { |
| 45 base::subtle::Release_Store(&running_, 0); | 44 libusb_exit(context_); |
| 46 libusb_interrupt_handle_event(context_); | |
| 47 base::PlatformThread::Join(thread_handle_); | |
| 48 } | 45 } |
| 49 | 46 |
| 50 void UsbContext::UsbEventHandler::ThreadMain() { | 47 void UsbContext::UsbEventHandler::ThreadMain() { |
| 51 base::PlatformThread::SetName("UsbEventHandler"); | 48 base::PlatformThread::SetName("UsbEventHandler"); |
| 52 VLOG(1) << "UsbEventHandler started."; | 49 VLOG(1) << "UsbEventHandler started."; |
| 53 | 50 |
| 54 if (base::subtle::Acquire_Load(&running_)) { | |
| 55 start_polling_.Signal(); | |
| 56 } | |
| 57 while (base::subtle::Acquire_Load(&running_)) { | 51 while (base::subtle::Acquire_Load(&running_)) { |
| 58 const int rv = libusb_handle_events(context_); | 52 const int rv = libusb_handle_events(context_); |
| 59 if (rv != LIBUSB_SUCCESS) { | 53 if (rv != LIBUSB_SUCCESS) { |
| 60 VLOG(1) << "Failed to handle events: " | 54 VLOG(1) << "Failed to handle events: " |
| 61 << ConvertPlatformUsbErrorToString(rv); | 55 << ConvertPlatformUsbErrorToString(rv); |
| 62 } | 56 } |
| 63 } | 57 } |
| 58 |
| 64 VLOG(1) << "UsbEventHandler shutting down."; | 59 VLOG(1) << "UsbEventHandler shutting down."; |
| 60 delete this; |
| 61 } |
| 62 |
| 63 void UsbContext::UsbEventHandler::Stop() { |
| 64 base::subtle::Release_Store(&running_, 0); |
| 65 libusb_interrupt_handle_event(context_); |
| 65 } | 66 } |
| 66 | 67 |
| 67 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { | 68 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 69 // Ownership of the PlatformUsbContext is passed to the event handler thread. |
| 69 event_handler_ = new UsbEventHandler(context_); | 70 event_handler_ = new UsbEventHandler(context_); |
| 70 } | 71 } |
| 71 | 72 |
| 72 UsbContext::~UsbContext() { | 73 UsbContext::~UsbContext() { |
| 73 // destruction of UsbEventHandler is a blocking operation. | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 delete event_handler_; | 75 event_handler_->Stop(); |
| 76 event_handler_ = NULL; | |
| 77 libusb_exit(context_); | |
| 78 } | 76 } |
| 79 | 77 |
| 80 } // namespace device | 78 } // namespace device |
| OLD | NEW |