From 5012a35382a181dd259423b0a845c59c59f9db85 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Tue, 23 Jan 2024 14:57:46 +0100 Subject: [PATCH] Change notification of waiting threads in condition_variable --- .../src/detail/condition_variable.cpp | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/libs/pika/synchronization/src/detail/condition_variable.cpp b/libs/pika/synchronization/src/detail/condition_variable.cpp index 1a05b9a309..531a60c39d 100644 --- a/libs/pika/synchronization/src/detail/condition_variable.cpp +++ b/libs/pika/synchronization/src/detail/condition_variable.cpp @@ -99,34 +99,22 @@ namespace pika::detail { // swap the list queue_type queue; queue.swap(queue_); - lock.unlock(); - if (!queue.empty()) - { - // update reference to queue for all queue entries - for (queue_entry& qe : queue) qe.q_ = &queue; - - do { - auto ctx = queue.front().ctx_; - - // remove item from queue before error handling - queue.front().ctx_.reset(); - queue.pop_front(); + // update reference to queue for all queue entries + for (queue_entry& qe : queue) qe.q_ = &queue; - if (PIKA_UNLIKELY(!ctx)) - { - lock.lock(); - prepend_entries(lock, queue); - lock.unlock(); - - PIKA_THROWS_IF(ec, pika::error::null_thread_id, - "condition_variable::notify_all", "null thread id encountered"); - return; - } - - ctx.resume(); - - } while (!queue.empty()); + while (!queue.empty()) + { + PIKA_ASSERT(queue.front().ctx_); + queue_entry& qe = queue.front(); + auto ctx = qe.ctx_; + qe.ctx_.reset(); + queue.pop_front(); + + // Resume without holding lock (since resuming may yield, waiting for thread to be + // inactive) + pika::detail::unlock_guard> ul(lock); + ctx.resume(); } if (&ec != &throws) ec = make_success_code();