diff --git a/adb/fdevent/fdevent.cpp b/adb/fdevent/fdevent.cpp index d6e3a660d..698b29353 100644 --- a/adb/fdevent/fdevent.cpp +++ b/adb/fdevent/fdevent.cpp @@ -20,7 +20,9 @@ #include +#include #include +#include #include "fdevent.h" #include "fdevent_poll.h" @@ -46,6 +48,12 @@ std::string dump_fde(const fdevent* fde) { state.c_str()); } +void fdevent_context::CheckMainThread() { + if (main_thread_id_) { + CHECK_EQ(*main_thread_id_, android::base::GetThreadId()); + } +} + void fdevent_context::Run(std::function fn) { { std::lock_guard lock(run_queue_mutex_); diff --git a/adb/fdevent/fdevent.h b/adb/fdevent/fdevent.h index 0147ad5d6..c007e518b 100644 --- a/adb/fdevent/fdevent.h +++ b/adb/fdevent/fdevent.h @@ -75,8 +75,9 @@ struct fdevent_context { // Implementations should call FlushRunQueue on every iteration. virtual void Loop() = 0; - // Assert that the caller is running on the context's main thread. - virtual void CheckMainThread() = 0; + // Assert that the caller is either running on the context's main thread, or that there is no + // active main thread. + void CheckMainThread(); // Queue an operation to be run on the main thread. void Run(std::function fn); @@ -92,6 +93,8 @@ struct fdevent_context { // Run all pending functions enqueued via Run(). void FlushRunQueue() EXCLUDES(run_queue_mutex_); + std::optional main_thread_id_ = std::nullopt; + private: std::mutex run_queue_mutex_; std::deque> run_queue_ GUARDED_BY(run_queue_mutex_); diff --git a/adb/fdevent/fdevent_poll.cpp b/adb/fdevent/fdevent_poll.cpp index f7863379c..8a1216283 100644 --- a/adb/fdevent/fdevent_poll.cpp +++ b/adb/fdevent/fdevent_poll.cpp @@ -75,16 +75,9 @@ fdevent_context_poll::fdevent_context_poll() { } fdevent_context_poll::~fdevent_context_poll() { - main_thread_valid_ = false; this->Destroy(this->interrupt_fde_); } -void fdevent_context_poll::CheckMainThread() { - if (main_thread_valid_) { - CHECK_EQ(main_thread_id_, android::base::GetThreadId()); - } -} - fdevent* fdevent_context_poll::Create(unique_fd fd, std::variant func, void* arg) { CheckMainThread(); @@ -373,13 +366,12 @@ static void fdevent_check_spin(fdevent_context_poll* ctx, uint64_t cycle) { } void fdevent_context_poll::Loop() { - this->main_thread_id_ = android::base::GetThreadId(); - this->main_thread_valid_ = true; + main_thread_id_ = android::base::GetThreadId(); uint64_t cycle = 0; while (true) { if (terminate_loop_) { - return; + break; } D("--- --- waiting for events"); @@ -396,6 +388,8 @@ void fdevent_context_poll::Loop() { this->FlushRunQueue(); } + + main_thread_id_.reset(); } void fdevent_context_poll::TerminateLoop() { diff --git a/adb/fdevent/fdevent_poll.h b/adb/fdevent/fdevent_poll.h index 1b505a7e0..b0465b959 100644 --- a/adb/fdevent/fdevent_poll.h +++ b/adb/fdevent/fdevent_poll.h @@ -58,8 +58,6 @@ struct fdevent_context_poll : public fdevent_context { virtual void Loop() final; - virtual void CheckMainThread() final; - virtual void TerminateLoop() final; virtual size_t InstalledCount() final; @@ -71,8 +69,6 @@ struct fdevent_context_poll : public fdevent_context { // That's why we don't need a lock for fdevent. std::unordered_map poll_node_map_; std::list pending_list_; - bool main_thread_valid_ = false; - uint64_t main_thread_id_ = 0; uint64_t fdevent_id_ = 0; unique_fd interrupt_fd_;