]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Handle util::TaskPool enqueue/shut_down called by different threads
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Oct 2025 11:36:26 +0000 (12:36 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Oct 2025 17:24:21 +0000 (18:24 +0100)
src/ccache/util/threadpool.cpp

index 107213a57008727739e44928d7606a4f3b45559b..bffe5ae9224bb37efa4358520884ce26c89f93ec 100644 (file)
@@ -39,9 +39,11 @@ ThreadPool::enqueue(std::function<void()> function)
 {
   {
     std::unique_lock<std::mutex> lock(m_mutex);
-    if (m_task_queue.size() >= m_task_queue_max_size) {
-      m_task_popped_condition.wait(
-        lock, [this] { return m_task_queue.size() < m_task_queue_max_size; });
+    m_task_popped_condition.wait(lock, [this] {
+      return m_shutting_down || m_task_queue.size() < m_task_queue_max_size;
+    });
+    if (m_shutting_down) {
+      return;
     }
     m_task_queue.emplace(function);
   }
@@ -60,6 +62,7 @@ ThreadPool::shut_down()
     m_shutting_down = true;
   }
   m_task_enqueued_or_shutting_down_condition.notify_all();
+  m_task_popped_condition.notify_all();
   for (auto& thread : m_worker_threads) {
     if (thread.joinable()) {
       thread.join();