Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions libs/core/threadmanager/src/threadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <hpx/modules/type_support.hpp>
#include <hpx/modules/util.hpp>

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <exception>
Expand All @@ -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 <typename Predicate>
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");
Comment thread
hkaiser marked this conversation as resolved.
Resolved
}
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)
Expand Down Expand Up @@ -1717,17 +1769,22 @@ namespace hpx::threads {
{
auto const shutdown_check_count = util::get_entry_as<std::size_t>(
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(
hpx::chrono::steady_duration const& rel_time) const
{
auto const shutdown_check_count = util::get_entry_as<std::size_t>(
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
Expand Down
Loading