adb: move all cleanup to a function with defined ordering.

We want to explicitly define the order in which we teardown adb, so
move all of the at_quick_exits sprinkled throughout into one function
containing all of the cleanup functions.

Bug: http://b/37104408
Test: adb kill-server; adb start-server
Change-Id: I394f5782eb147e394d4b87df1ba364c061de4b90
diff --git a/client/usb_libusb.cpp b/client/usb_libusb.cpp
index 5a65865..18a8ff2 100644
--- a/client/usb_libusb.cpp
+++ b/client/usb_libusb.cpp
@@ -415,15 +415,21 @@
 
     // Spawn a thread to do device enumeration.
     // TODO: Use libusb_hotplug_* instead?
+    std::unique_lock<std::mutex> lock(device_poll_mutex);
     device_poll_thread = new std::thread(poll_for_devices);
-    android::base::at_quick_exit([]() {
-        {
-            std::unique_lock<std::mutex> lock(device_poll_mutex);
-            terminate_device_poll_thread = true;
+}
+
+void usb_cleanup() {
+    {
+        std::unique_lock<std::mutex> lock(device_poll_mutex);
+        terminate_device_poll_thread = true;
+
+        if (!device_poll_thread) {
+            return;
         }
-        device_poll_cv.notify_all();
-        device_poll_thread->join();
-    });
+    }
+    device_poll_cv.notify_all();
+    device_poll_thread->join();
 }
 
 // Dispatch a libusb transfer, unlock |device_lock|, and then wait for the result.