From 6cd25fb9b1a65760f1e8af7c1d6589138f2e8029 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 7 Dec 2022 15:17:20 +0100 Subject: [PATCH] Check for runnable threads --- kernel/src/thread/Scheduler.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 04dd4e1f..c925d356 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -88,22 +88,29 @@ namespace Scheduler Thread* old = g_current; if (old->is_idle()) { - auto maybe_first = g_threads.last(); - if (maybe_first.has_error()) // No threads!! + auto maybe_last = g_threads.last(); + if (maybe_last.has_error()) // No threads!! return &g_idle; - g_current = old = maybe_first.value(); + g_current = old = maybe_last.value(); } + bool has_found_thread = false; + do { auto maybe_next = g_threads.next(g_current); if (maybe_next.has_error()) g_current = g_threads.first().value(); else g_current = maybe_next.value(); - if (true) // FIXME: Check if the current task is runnable. + if (g_current->state == ThreadState::Runnable) + { + has_found_thread = true; break; + } } while (g_current != old); + if (!has_found_thread) g_current = &g_idle; + return g_current; }