mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-01 05:49:27 +00:00
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
This commit is contained in:
@@ -86,4 +86,7 @@ cc_test {
|
||||
],
|
||||
static_libs: ["android.hardware.broadcastradio@common-utils-lib"],
|
||||
test_suites: ["general-tests"],
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
],
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user