From 925249077b5871f2664ac869ead627964e02430d Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Wed, 17 May 2023 10:09:14 +0200 Subject: [PATCH 1/2] Relax locks in detail condition_variable notify_all Only hold lock while swapping the queue of waiting threads into a local queue. Retake lock if there's an error and the queue has to be spliced back into the member variable queue. --- libs/pika/synchronization/src/detail/condition_variable.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/pika/synchronization/src/detail/condition_variable.cpp b/libs/pika/synchronization/src/detail/condition_variable.cpp index 82b1ec5f5..5f967ba9c 100644 --- a/libs/pika/synchronization/src/detail/condition_variable.cpp +++ b/libs/pika/synchronization/src/detail/condition_variable.cpp @@ -100,6 +100,7 @@ namespace pika::detail { // swap the list queue_type queue; queue.swap(queue_); + lock.unlock(); if (!queue.empty()) { @@ -117,6 +118,7 @@ namespace pika::detail { if (PIKA_UNLIKELY(!ctx)) { + lock.lock(); prepend_entries(lock, queue); lock.unlock(); @@ -125,8 +127,6 @@ namespace pika::detail { return; } - [[maybe_unused]] util::ignore_while_checking il(&lock); - ctx.resume(); } while (!queue.empty()); From a46a7ce60d2aa4efd93a4eb9649e35443f4ec295 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Wed, 17 May 2023 10:34:08 +0200 Subject: [PATCH 2/2] Allow set_thread_state to yield again if other thread is active This change was only introduced to never yield while holding a lock in condition_variable::notify_all. However, that was relaxed to not hold a lock while resuming threads so we can again allow yielding in set_thread_state. --- libs/pika/threading_base/src/set_thread_state.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/pika/threading_base/src/set_thread_state.cpp b/libs/pika/threading_base/src/set_thread_state.cpp index 07b27be9a..134fb08e6 100644 --- a/libs/pika/threading_base/src/set_thread_state.cpp +++ b/libs/pika/threading_base/src/set_thread_state.cpp @@ -133,7 +133,7 @@ namespace pika::threads::detail { else { pika::execution::this_thread::detail::yield_k( - k % 16, "pika::threads::detail::set_thread_state"); + k, "pika::threads::detail::set_thread_state"); ++k; // NOLINTNEXTLINE(bugprone-branch-clone)