diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-26 17:47:49 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-29 10:56:41 +0200 |
commit | 5ab67696cc314dc9ca463114ecee826bcafbe9e7 (patch) | |
tree | ee8ed9c35701543c5a50192378f4cd97a8179b27 /src | |
parent | e736991eea0b5a1080281bd00e8267d1b97ff212 (diff) | |
download | justbuild-5ab67696cc314dc9ca463114ecee826bcafbe9e7.tar.gz |
Fix race condition in task queue
To avoid lock contention, our task system queues tasks in several
queues. In order to know when there is no more work to be done, a
global counter keeps track of the total work. Here, it is important
that this counter be incremented before a value is added to the
queue, for otherwise some other thread could pick it up and decrement
the total work load, hence getting us in a state where the counter
underestimates the total amount of work to be done. Fix the order
of those operations.
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/multithreading/notification_queue.hpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/buildtool/multithreading/notification_queue.hpp b/src/buildtool/multithreading/notification_queue.hpp index f84e47c6..a9f476c2 100644 --- a/src/buildtool/multithreading/notification_queue.hpp +++ b/src/buildtool/multithreading/notification_queue.hpp @@ -126,11 +126,11 @@ class NotificationQueue { // finished) template <typename FunctionType> void push(FunctionType&& f) { + total_workload_->Increment(); { std::unique_lock lock{mutex_}; queue_.emplace_back(std::forward<FunctionType>(f)); } - total_workload_->Increment(); ready_.notify_one(); } @@ -143,9 +143,9 @@ class NotificationQueue { if (!lock) { return false; } + total_workload_->Increment(); queue_.emplace_back(std::forward<FunctionType>(f)); } - total_workload_->Increment(); ready_.notify_one(); return true; } |