Files
bsb2/kernel/thread/scheduler.cc
Niklas Gollenstede 4245798955 fixes for A2
mostly include some things that should and remove some things that shouldn't have been included in the handout (yes this does hint at some places that need to be touched for A2)
2025-11-24 15:04:26 +01:00

62 lines
1.3 KiB
C++

// vim: set noet ts=4 sw=4:
#include "scheduler.h"
#include "../arch/core.h"
#include "../debug/assert.h" // IWYU pragma: keep
Scheduler::Scheduler() {}
Thread *Scheduler::getNext() {
Thread *next = readylist.dequeue();
if (next == nullptr) {
next = &idleThread;
}
assert(next != nullptr && "No thread available!");
return next;
}
void Scheduler::schedule() { dispatcher.go(getNext()); }
void Scheduler::ready(Thread *that) {
assert(that != nullptr && "nullptr pointer not allowed for ready list!");
assert(static_cast<Thread *>(&idleThread) != that &&
"IdleThread must not be placed in ready list!");
readylist.enqueue(*that);
}
void Scheduler::resume(bool ready) {
Thread *me = dispatcher.active();
assert(me != nullptr && "Pointer to active thread should never be nullptr");
// Be careful, never put the idle thread into the ready list
bool is_idle_thread = static_cast<Thread *>(&idleThread) == me;
if (ready && readylist.is_empty()) {
return;
} else if (!is_idle_thread) {
if (ready) readylist.enqueue(*me);
}
dispatcher.dispatch(getNext());
}
void Scheduler::exit() {
Thread *next = getNext();
dispatcher.dispatch(next);
}
void Scheduler::kill(Thread *that) {
if (dispatcher.active() == that) {
exit();
}
readylist.remove(that);
}
bool Scheduler::isEmpty() const { return readylist.is_empty(); }