From 15a275de23d93c4f3ae8a2e52184d4910674ed02 Mon Sep 17 00:00:00 2001 From: Hartmut Kaiser Date: Sat, 21 Nov 2026 16:20:38 -0600 Subject: [PATCH] Let threadmanager::wait() and wait_for() suspend while other work exists Both waited for the other HPX threads by yielding. A yield puts the waiting thread back into the queue for its own priority, and the shutdown actions (shutdown_all, the Dijkstra termination detection) run with a high priority, which the priority schedulers always pick before normal threads. On a single worker the waiter was then picked again and again while the threads it waited for never ran. This made concurrent_collectives hang with --hpx:threads=1 over MPI: locality 1 finished first and sent shutdown_all to locality 0 while locality 0 still ran the local part of the test, whose threads never got to run. An HPX thread now sleeps for 100 microseconds between the checks while other work exists, other threads keep yielding as before. Signed-off-by: Hartmut Kaiser --- libs/core/threadmanager/src/threadmanager.cpp | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/libs/core/threadmanager/src/threadmanager.cpp b/libs/core/threadmanager/src/threadmanager.cpp index 88118cddfc1..a5d684c3703 100644 --- a/libs/core/threadmanager/src/threadmanager.cpp +++ b/libs/core/threadmanager/src/threadmanager.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,57 @@ namespace hpx::threads { + namespace { + + // Waits until busy() returned false required_count times in a row, + // or until abs_time (if given) has passed. The waiter has to let the + // threads it waits for run. A yield puts an HPX thread back into the + // queue for its own priority, and the schedulers pick a thread with a + // high priority (as the shutdown actions have) before any thread with + // a normal priority. On a single worker a yielding waiter would then + // starve the threads it waits for, so an HPX thread suspends for a + // short time instead. + template + bool wait_while_busy(Predicate&& busy, std::size_t const required_count, + std::chrono::steady_clock::time_point const* abs_time) + { + bool const is_hpx_thread = threads::get_self_ptr() != nullptr; + + std::size_t count = 0; + for (std::size_t k = 0; /**/; ++k) + { + if (abs_time != nullptr && + *abs_time <= std::chrono::steady_clock::now()) + { + return false; + } + + if (!busy()) + { + if (++count > required_count) + { + return true; + } + } + else + { + count = 0; + if (is_hpx_thread) + { + hpx::execution_base::this_thread::sleep_for( + std::chrono::microseconds(100), + "threadmanager::wait"); + } + else + { + hpx::execution_base::this_thread::yield_k( + k, "threadmanager::wait"); + } + } + } + } + } // namespace + namespace detail { void check_num_high_priority_queues(std::size_t const num_threads, std::size_t const num_high_priority_queues) @@ -1717,8 +1769,8 @@ namespace hpx::threads { { auto const shutdown_check_count = util::get_entry_as( rtcfg_, "hpx.shutdown_check_count", 10); - hpx::util::detail::yield_while_count( - [this]() { return is_busy(); }, shutdown_check_count); + wait_while_busy( + [this]() { return is_busy(); }, shutdown_check_count, nullptr); } bool threadmanager::wait_for( @@ -1726,8 +1778,13 @@ namespace hpx::threads { { auto const shutdown_check_count = util::get_entry_as( rtcfg_, "hpx.shutdown_check_count", 10); - return hpx::util::detail::yield_while_count_timeout( - [this]() { return is_busy(); }, shutdown_check_count, rel_time); + // A zero duration means no timeout. + bool const use_timeout = + rel_time.value() != std::chrono::steady_clock::duration(0); + std::chrono::steady_clock::time_point const abs_time = + rel_time.from_now(); + return wait_while_busy([this]() { return is_busy(); }, + shutdown_check_count, use_timeout ? &abs_time : nullptr); } void threadmanager::suspend() const