Skip to content

Commit

Permalink
Change notification of waiting threads in condition_variable
Browse files Browse the repository at this point in the history
  • Loading branch information
msimberg committed Jan 23, 2024
1 parent 1df7133 commit 5012a35
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions libs/pika/synchronization/src/detail/condition_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_lock<mutex_type>> ul(lock);
ctx.resume();
}

if (&ec != &throws) ec = make_success_code();
Expand Down

0 comments on commit 5012a35

Please sign in to comment.