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

Commit 82f17d9e authored by Weilin Xu's avatar Weilin Xu
Browse files

Fix timeout issue for bcradio worker thread

Accessed mIsTerminating inside the same lock as what is used in while
loop in worker thread class of broadcast radio HAL utils lib. This fixed
the race condition that mIsTerminating is accessed as true in threadLoop
while the destructor is setting mIsTerminating as false, which causes the
thread waits forever for lock after lock is released in the desctructor.

Bug: 314100017
Test: atest VtsHalBroadcastradioAidlTargetTest WorkerThreadTest
Change-Id: I885e1487ac39525fc2d1ee2134d24409264ca0fc
parent f162180e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -86,4 +86,7 @@ cc_test {
    ],
    static_libs: ["android.hardware.broadcastradio@common-utils-lib"],
    test_suites: ["general-tests"],
    shared_libs: [
        "libbase",
    ],
}
+4 −1
Original line number Diff line number Diff line
@@ -69,8 +69,11 @@ void WorkerThread::cancelAll() {
}

void WorkerThread::threadLoop() {
    while (!mIsTerminating) {
    while (true) {
        unique_lock<mutex> lk(mMut);
        if (mIsTerminating) {
            return;
        }
        if (mTasks.empty()) {
            mCond.wait(lk);
            continue;
+5 −3
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@
#include <queue>
#include <thread>

#include <android-base/thread_annotations.h>

namespace android {

class WorkerThread {
@@ -40,11 +42,11 @@ class WorkerThread {
    };
    friend bool operator<(const Task& lhs, const Task& rhs);

    std::atomic<bool> mIsTerminating;
    std::mutex mMut;
    std::condition_variable mCond;
    bool mIsTerminating GUARDED_BY(mMut);
    std::condition_variable mCond GUARDED_BY(mMut);
    std::thread mThread;
    std::priority_queue<Task> mTasks;
    std::priority_queue<Task> mTasks GUARDED_BY(mMut);

    void threadLoop();
};