adb: fdevent: move CheckMainThread logic to fdevent_context.

Test: adb_test
Change-Id: I10b93293af22d54ec739dc0f95c95df3ab082fb6
This commit is contained in:
Josh Gao 2019-07-08 18:05:16 -07:00
parent e546f6816e
commit 2c95bf73a5
4 changed files with 17 additions and 16 deletions

View file

@ -20,7 +20,9 @@
#include <inttypes.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/threads.h>
#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<void()> fn) {
{
std::lock_guard<std::mutex> lock(run_queue_mutex_);

View file

@ -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<void()> fn);
@ -92,6 +93,8 @@ struct fdevent_context {
// Run all pending functions enqueued via Run().
void FlushRunQueue() EXCLUDES(run_queue_mutex_);
std::optional<uint64_t> main_thread_id_ = std::nullopt;
private:
std::mutex run_queue_mutex_;
std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_);

View file

@ -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<fd_func, fd_func2> 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() {

View file

@ -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<int, PollNode> poll_node_map_;
std::list<fdevent*> pending_list_;
bool main_thread_valid_ = false;
uint64_t main_thread_id_ = 0;
uint64_t fdevent_id_ = 0;
unique_fd interrupt_fd_;