From 4d0d26bfef684566a371ca7c87ba84df52f25ccc Mon Sep 17 00:00:00 2001 From: Douglas Rumbaugh Date: Mon, 22 Jan 2024 10:42:58 -0500 Subject: FIFOScheduler: added automake wakeup Sometimes, when the max thread count is exceeded, it is possible for the scheduler to lock up. This is because the scheduler is only run when a new job is put into the queue, and so it is possible for a job to be blocked by thread limitations and be left sitting in the queue. If the main program is waiting on this job to finish before scheduling a new one, then the system deadlocks. I added a second background thread to the scheduler that wakes the scheduler up every 20us to resolve this and prevent these deadlocks. --- include/framework/scheduling/FIFOScheduler.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/framework/scheduling/FIFOScheduler.h b/include/framework/scheduling/FIFOScheduler.h index 513a3a2..b77a8a1 100644 --- a/include/framework/scheduling/FIFOScheduler.h +++ b/include/framework/scheduling/FIFOScheduler.h @@ -10,7 +10,7 @@ #include #include - +#include #include "framework/scheduling/Task.h" #include "framework/scheduling/statistics.h" @@ -19,6 +19,8 @@ namespace de { +using namespace std::literals::chrono_literals; + class FIFOScheduler { private: @@ -33,6 +35,7 @@ public: , m_shutdown(false) { m_sched_thrd = std::thread(&FIFOScheduler::run, this); + m_sched_wakeup_thrd = std::thread(&FIFOScheduler::periodic_wakeup, this); m_thrd_pool.resize(m_thrd_cnt); } @@ -77,6 +80,7 @@ private: std::condition_variable m_cv; std::thread m_sched_thrd; + std::thread m_sched_wakeup_thrd; ctpl::thread_pool m_thrd_pool; std::atomic m_used_thrds; @@ -84,6 +88,13 @@ private: SchedulerStatistics m_stats; + void periodic_wakeup() { + do { + std::this_thread::sleep_for(10us); + m_cv.notify_all(); + } while (!m_shutdown.load()); + } + void schedule_next() { assert(m_task_queue.size() > 0); auto t = m_task_queue.pop(); -- cgit v1.2.3