From 1e1e08a910ed42c537ff116b9077c4d8dc45ebae Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Sun, 23 Feb 2020 15:24:02 +0100 Subject: [PATCH] Add yielding to scheduler loop. We yield after num_thread failed steals in a row. This parameter can be tuned for better performance, but we stick to a sensible default just to prevent massive spinning. --- lib/pls/src/internal/scheduling/scheduler.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/pls/src/internal/scheduling/scheduler.cpp b/lib/pls/src/internal/scheduling/scheduler.cpp index 484337a..f3d9773 100644 --- a/lib/pls/src/internal/scheduling/scheduler.cpp +++ b/lib/pls/src/internal/scheduling/scheduler.cpp @@ -68,6 +68,7 @@ void scheduler::work_thread_work_section() { main_thread_starter_function_->run(); } + unsigned int failed_steals = 0; while (!work_section_done_) { PLS_ASSERT(my_task_manager.check_task_chain(), "Must start stealing with a clean task chain."); @@ -111,6 +112,12 @@ void scheduler::work_thread_work_section() { stolen_task->is_synchronized_ = false; context_switcher::switch_context(std::move(stolen_task->continuation_)); // We will continue execution in this line when we finished the stolen work. + failed_steals = 0; + } else { + failed_steals++; + if (failed_steals >= num_threads) { + base::this_thread::yield(); + } } } } -- libgit2 0.26.0