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