Skip to content

Commit

Permalink
fix: 修复 wait_all 死锁问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 3, 2024
1 parent e085972 commit 93a1a70
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions source/MaaFramework/Base/AsyncRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class AsyncRunner : public NonCopyable
ProcessFunc process_;

std::list<std::pair<Id, Item>> queue_;
std::mutex mutex_;
std::condition_variable cond_;
std::mutex queue_mutex_;
std::condition_variable queue_cond_;
std::atomic_bool running_ = false;

mutable std::shared_mutex status_mutex_;
Expand Down Expand Up @@ -82,12 +82,12 @@ inline void AsyncRunner<Item>::release()
exit_ = true;

{
std::unique_lock<std::mutex> lock(mutex_);
cond_.notify_all();
std::unique_lock<std::mutex> queue_lock(queue_mutex_);
queue_cond_.notify_all();
}

{
std::unique_lock<std::mutex> lock(compl_mutex_);
std::unique_lock<std::mutex> compl_lock(compl_mutex_);
compl_cond_.notify_all();
}

Expand All @@ -102,11 +102,12 @@ inline void AsyncRunner<Item>::working()
// LogFunc;

while (!exit_) {
std::unique_lock<std::mutex> lock(mutex_);
std::unique_lock<std::mutex> queue_lock(queue_mutex_);

if (queue_.empty()) {
running_ = false;
cond_.wait(lock);
compl_cond_.notify_all();
queue_cond_.wait(lock);
continue;
}

Expand Down Expand Up @@ -139,7 +140,7 @@ inline typename AsyncRunner<Item>::Id AsyncRunner<Item>::post(Item item, bool bl

Id id = MaaInvalidId;
{
std::unique_lock<std::mutex> lock(mutex_);
std::unique_lock<std::mutex> queue_lock(queue_mutex_);
id = ++cross_inst_id_;
queue_.emplace_back(id, std::move(item));

Expand All @@ -149,7 +150,7 @@ inline typename AsyncRunner<Item>::Id AsyncRunner<Item>::post(Item item, bool bl
}

running_ = true;
cond_.notify_one();
queue_cond_.notify_one();
}

if (block) {
Expand All @@ -165,7 +166,7 @@ inline void AsyncRunner<Item>::wait(Id id) const
// LogFunc << VAR(id);

while (!exit_) {
std::unique_lock<std::mutex> lock(compl_mutex_);
std::unique_lock<std::mutex> compl_lock(compl_mutex_);
if (id <= compl_id_) {
return;
}
Expand All @@ -180,7 +181,7 @@ inline void AsyncRunner<Item>::wait_all() const
LogFunc;

while (!exit_) {
std::unique_lock<std::mutex> lock(compl_mutex_);
std::unique_lock<std::mutex> compl_lock(compl_mutex_);
if (!running_) {
return;
}
Expand All @@ -207,13 +208,13 @@ inline void AsyncRunner<Item>::clear()
// LogFunc;

{
std::unique_lock<std::mutex> lock(mutex_);
std::unique_lock<std::mutex> queue_lock(queue_mutex_);
queue_ = {};
cond_.notify_all();
queue_cond_.notify_all();
}

{
std::unique_lock<std::mutex> lock(compl_mutex_);
std::unique_lock<std::mutex> compl_lock(compl_mutex_);
compl_id_ = cross_inst_id_;
compl_cond_.notify_all();
}
Expand Down

0 comments on commit 93a1a70

Please sign in to comment.