From 019e092a5e62adeec53a7fdd7dfd391ebe14b654 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Sat, 16 Jul 2022 16:36:45 +0200 Subject: [PATCH] add some error handling to scheduler --- c_os/kernel/threads/Scheduler.cc | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/c_os/kernel/threads/Scheduler.cc b/c_os/kernel/threads/Scheduler.cc index 22dfc5e..cd6e0b9 100644 --- a/c_os/kernel/threads/Scheduler.cc +++ b/c_os/kernel/threads/Scheduler.cc @@ -55,7 +55,8 @@ void Scheduler::ready(Thread* that) { // Thread-Wechsel durch PIT verhindern cpu.disable_int(); this->ready_queue.insert(that); - log << TRACE << "Scheduler :: Adding to ready_queue" << endl; + + log << TRACE << "Adding to ready_queue, ID: " << dec << that->tid << endl; this->ready_queue.print(log << TRACE); cpu.enable_int(); } @@ -74,9 +75,16 @@ void Scheduler::exit() { // Thread-Wechsel durch PIT verhindern cpu.disable_int(); + if (this->ready_queue.empty()) { + log << ERROR << "Can't exit last thread, active ID: " << dec << this->get_active()->tid << endl; + cpu.enable_int(); + return; + } Thread& next = *(Thread*)this->ready_queue.remove_first(); - log << TRACE << "Scheduler :: Exiting thread" << endl; + + log << TRACE << "Exiting thread, ID: " << dec << this->get_active()->tid << " => " << next.tid << endl; this->ready_queue.print(log << TRACE); + this->dispatch(next); // Interrupts werden in Thread_switch in Thread.asm wieder zugelassen @@ -100,8 +108,13 @@ void Scheduler::kill(Thread* that) { // Thread-Wechsel durch PIT verhindern cpu.disable_int(); - this->ready_queue.remove(that); - log << TRACE << "Scheduler :: Killing thread" << endl; + if (this->ready_queue.remove(that) == -1) { + log << ERROR << "Can't kill thread that is not in ready_queue, ID: " << dec << that->tid << endl; + cpu.enable_int(); + return; + } + + log << TRACE << "Killing thread, ID: " << dec << that->tid << endl; this->ready_queue.print(log << TRACE); cpu.enable_int(); } @@ -123,10 +136,18 @@ void Scheduler::yield() { // Thread-Wechsel durch PIT verhindern cpu.disable_int(); - this->ready_queue.insert(this->get_active()); + if (this->ready_queue.empty()) { + log << TRACE << "Skipping yield as no thread is waiting, active ID: " << dec << this->get_active()->tid << endl; + cpu.enable_int(); + return; + } + Thread& next = *(Thread*)this->ready_queue.remove_first(); - log << TRACE << "Scheduler :: Yield" << endl; + this->ready_queue.insert(this->get_active()); + + log << TRACE << "Yielding, ID: " << dec << this->get_active()->tid << " => " << next.tid << endl; this->ready_queue.print(log << TRACE); + this->dispatch(next); }