OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "chrome/browser/usb/usb_context.h" | |
6 | |
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" | |
12 | |
13 // The UsbEventHandler works around a design flaw in the libusb interface. There | |
14 // is currently no way to signal to libusb that any caller into one of the event | |
15 // handler calls should return without handling any events. | |
16 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { | |
17 public: | |
18 explicit UsbEventHandler(libusb_context* context); | |
19 virtual ~UsbEventHandler(); | |
20 | |
21 // base::PlatformThread::Delegate | |
22 virtual void ThreadMain() OVERRIDE; | |
23 | |
24 private: | |
25 volatile bool running_; | |
26 libusb_context* context_; | |
27 base::PlatformThreadHandle thread_handle_; | |
28 base::WaitableEvent start_polling_; | |
29 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); | |
30 }; | |
31 | |
32 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) | |
33 : running_(true), | |
34 context_(context), | |
35 thread_handle_(0), | |
36 start_polling_(false, false) { | |
37 bool success = base::PlatformThread::Create(0, this, &thread_handle_); | |
38 DCHECK(success) << "Failed to create USB IO handling thread."; | |
39 start_polling_.Wait(); | |
40 } | |
41 | |
42 UsbContext::UsbEventHandler::~UsbEventHandler() { | |
43 running_ = false; | |
44 // Spreading running_ to the UsbEventHandler thread. | |
45 base::subtle::MemoryBarrier(); | |
46 libusb_interrupt_handle_event(context_); | |
47 base::PlatformThread::Join(thread_handle_); | |
48 } | |
49 | |
50 void UsbContext::UsbEventHandler::ThreadMain() { | |
51 base::PlatformThread::SetName("UsbEventHandler"); | |
52 VLOG(1) << "UsbEventHandler started."; | |
53 if (running_) { | |
54 start_polling_.Signal(); | |
55 libusb_handle_events(context_); | |
56 } | |
57 while (running_) | |
58 libusb_handle_events(context_); | |
59 VLOG(1) << "UsbEventHandler shutting down."; | |
60 } | |
61 | |
62 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { | |
63 DCHECK(thread_checker_.CalledOnValidThread()); | |
64 event_handler_ = new UsbEventHandler(context_); | |
65 } | |
66 | |
67 UsbContext::~UsbContext() { | |
68 // destruction of UsbEventHandler is a blocking operation. | |
69 DCHECK(thread_checker_.CalledOnValidThread()); | |
70 delete event_handler_; | |
71 event_handler_ = NULL; | |
72 libusb_exit(context_); | |
73 } | |
OLD | NEW |