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.
5 #include "components/usb_service/usb_context.h"
7 #include "base/logging.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "base/threading/platform_thread.h"
10 #include "third_party/libusb/src/libusb/interrupt.h"
11 #include "third_party/libusb/src/libusb/libusb.h"
13 namespace usb_service
{
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::PlatformThread::Delegate
{
20 explicit UsbEventHandler(libusb_context
* context
);
21 virtual ~UsbEventHandler();
23 // base::PlatformThread::Delegate
24 virtual void ThreadMain() OVERRIDE
;
27 volatile bool running_
;
28 libusb_context
* context_
;
29 base::PlatformThreadHandle thread_handle_
;
30 base::WaitableEvent start_polling_
;
31 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler
);
34 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context
* context
)
38 start_polling_(false, false) {
39 bool success
= base::PlatformThread::Create(0, this, &thread_handle_
);
40 DCHECK(success
) << "Failed to create USB IO handling thread.";
41 start_polling_
.Wait();
44 UsbContext::UsbEventHandler::~UsbEventHandler() {
46 // Spreading running_ to the UsbEventHandler thread.
47 base::subtle::MemoryBarrier();
48 libusb_interrupt_handle_event(context_
);
49 base::PlatformThread::Join(thread_handle_
);
52 void UsbContext::UsbEventHandler::ThreadMain() {
53 base::PlatformThread::SetName("UsbEventHandler");
54 VLOG(1) << "UsbEventHandler started.";
56 start_polling_
.Signal();
57 libusb_handle_events(context_
);
60 libusb_handle_events(context_
);
61 VLOG(1) << "UsbEventHandler shutting down.";
64 UsbContext::UsbContext(PlatformUsbContext context
) : context_(context
) {
65 DCHECK(thread_checker_
.CalledOnValidThread());
66 event_handler_
= new UsbEventHandler(context_
);
69 UsbContext::~UsbContext() {
70 // destruction of UsbEventHandler is a blocking operation.
71 DCHECK(thread_checker_
.CalledOnValidThread());
72 delete event_handler_
;
73 event_handler_
= NULL
;
74 libusb_exit(context_
);
77 } // namespace usb_service