| 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/threading/platform_thread.h" | 9 #include "base/threading/simple_thread.h" |
| 10 #include "device/usb/usb_error.h" | 10 #include "device/usb/usb_error.h" |
| 11 #include "third_party/libusb/src/libusb/interrupt.h" | 11 #include "third_party/libusb/src/libusb/interrupt.h" |
| 12 #include "third_party/libusb/src/libusb/libusb.h" | 12 #include "third_party/libusb/src/libusb/libusb.h" |
| 13 | 13 |
| 14 namespace device { | 14 namespace device { |
| 15 | 15 |
| 16 // 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 |
| 17 // 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 |
| 18 // handler calls should return without handling any events. | 18 // handler calls should return without handling any events. |
| 19 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { | 19 class UsbContext::UsbEventHandler : public base::SimpleThread { |
| 20 public: | 20 public: |
| 21 explicit UsbEventHandler(libusb_context* context); | 21 explicit UsbEventHandler(libusb_context* context); |
| 22 ~UsbEventHandler() override; | 22 ~UsbEventHandler() override; |
| 23 | 23 |
| 24 // base::PlatformThread::Delegate | 24 // base::SimpleThread |
| 25 void ThreadMain() override; | 25 void Run() override; |
| 26 | 26 |
| 27 void Stop(); | 27 void Stop(); |
| 28 | 28 |
| 29 private: | 29 private: |
| 30 base::subtle::Atomic32 running_; | 30 base::subtle::Atomic32 running_; |
| 31 libusb_context* context_; | 31 libusb_context* context_; |
| 32 base::PlatformThreadHandle thread_handle_; | |
| 33 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); | 32 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); |
| 34 }; | 33 }; |
| 35 | 34 |
| 36 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) | 35 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) |
| 37 : context_(context), thread_handle_(0) { | 36 : base::SimpleThread("UsbEventHandler"), context_(context) { |
| 38 base::subtle::Release_Store(&running_, 1); | 37 base::subtle::Release_Store(&running_, 1); |
| 39 bool success = base::PlatformThread::Create(0, this, &thread_handle_); | |
| 40 DCHECK(success) << "Failed to create USB IO handling thread."; | |
| 41 } | 38 } |
| 42 | 39 |
| 43 UsbContext::UsbEventHandler::~UsbEventHandler() { | 40 UsbContext::UsbEventHandler::~UsbEventHandler() { |
| 44 libusb_exit(context_); | 41 libusb_exit(context_); |
| 45 } | 42 } |
| 46 | 43 |
| 47 void UsbContext::UsbEventHandler::ThreadMain() { | 44 void UsbContext::UsbEventHandler::Run() { |
| 48 base::PlatformThread::SetName("UsbEventHandler"); | |
| 49 VLOG(1) << "UsbEventHandler started."; | 45 VLOG(1) << "UsbEventHandler started."; |
| 50 | 46 |
| 51 while (base::subtle::Acquire_Load(&running_)) { | 47 while (base::subtle::Acquire_Load(&running_)) { |
| 52 const int rv = libusb_handle_events(context_); | 48 const int rv = libusb_handle_events(context_); |
| 53 if (rv != LIBUSB_SUCCESS) { | 49 if (rv != LIBUSB_SUCCESS) { |
| 54 VLOG(1) << "Failed to handle events: " | 50 VLOG(1) << "Failed to handle events: " |
| 55 << ConvertPlatformUsbErrorToString(rv); | 51 << ConvertPlatformUsbErrorToString(rv); |
| 56 } | 52 } |
| 57 } | 53 } |
| 58 | 54 |
| 59 VLOG(1) << "UsbEventHandler shutting down."; | 55 VLOG(1) << "UsbEventHandler shutting down."; |
| 60 delete this; | |
| 61 } | 56 } |
| 62 | 57 |
| 63 void UsbContext::UsbEventHandler::Stop() { | 58 void UsbContext::UsbEventHandler::Stop() { |
| 64 base::PlatformThreadHandle thread_handle = thread_handle_; | |
| 65 base::subtle::Release_Store(&running_, 0); | 59 base::subtle::Release_Store(&running_, 0); |
| 66 libusb_interrupt_handle_event(context_); | 60 libusb_interrupt_handle_event(context_); |
| 67 base::PlatformThread::Join(thread_handle); | |
| 68 } | 61 } |
| 69 | 62 |
| 70 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { | 63 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { |
| 71 // Ownership of the PlatformUsbContext is passed to the event handler thread. | 64 // Ownership of the PlatformUsbContext is passed to the event handler thread. |
| 72 event_handler_ = new UsbEventHandler(context_); | 65 event_handler_.reset(new UsbEventHandler(context_)); |
| 66 event_handler_->Start(); |
| 73 } | 67 } |
| 74 | 68 |
| 75 UsbContext::~UsbContext() { | 69 UsbContext::~UsbContext() { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 event_handler_->Stop(); | 71 event_handler_->Stop(); |
| 72 event_handler_->Join(); |
| 78 } | 73 } |
| 79 | 74 |
| 80 } // namespace device | 75 } // namespace device |
| OLD | NEW |