Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57c060a4 authored by Josh Gao's avatar Josh Gao Committed by android-build-merger
Browse files

adb: fdevent: move CheckMainThread logic to fdevent_context.

am: 2c95bf73

Change-Id: I27a9059d7009218551b65c1c59db082e87b86c60
parents f46ca64f 2c95bf73
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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_);
+5 −2
Original line number Diff line number Diff line
@@ -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_);
+4 −10
Original line number Diff line number Diff line
@@ -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() {
+0 −4
Original line number Diff line number Diff line
@@ -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_;