adb: reunregress waiting for device on start-server.
Move the invocation of adb_notify_device_scan_complete to
the end of device_connected, where we decrement connecting_devices.
Also, create a dedicated thread for handling hotplug events, instead of
reusing the main thread for this, since the main thread blocks until
device scan is complete.
Test: `adb kill-server; adb devices`
Change-Id: Ia73b1a57538174282a48ef73ab0a3e58152d6f83
diff --git a/adb/adb_utils.h b/adb/adb_utils.h
index c1d5549..20c63b3 100644
--- a/adb/adb_utils.h
+++ b/adb/adb_utils.h
@@ -17,7 +17,10 @@
#ifndef _ADB_UTILS_H_
#define _ADB_UTILS_H_
+#include <condition_variable>
+#include <mutex>
#include <string>
+#include <vector>
#include <android-base/macros.h>
@@ -53,4 +56,32 @@
bool forward_targets_are_valid(const std::string& source, const std::string& dest,
std::string* error);
+// A thread-safe blocking queue.
+template <typename T>
+class BlockingQueue {
+ std::mutex mutex;
+ std::condition_variable cv;
+ std::vector<T> queue;
+
+ public:
+ void Push(const T& t) {
+ {
+ std::unique_lock<std::mutex> lock(mutex);
+ queue.push_back(t);
+ }
+ cv.notify_one();
+ }
+
+ template <typename Fn>
+ void PopAll(Fn fn) {
+ std::unique_lock<std::mutex> lock(mutex);
+ cv.wait(lock, [this]() { return !queue.empty(); });
+
+ for (const T& t : queue) {
+ fn(t);
+ }
+ queue.clear();
+ }
+};
+
#endif