1

first (buggy) scheduler unique_ptr rework

This commit is contained in:
2022-07-21 20:35:52 +02:00
parent 02c75e483d
commit 2ac36cda83
2 changed files with 146 additions and 162 deletions

View File

@ -23,35 +23,26 @@
#include "kernel/threads/Scheduler.h" #include "kernel/threads/Scheduler.h"
#include "kernel/threads/IdleThread.h" #include "kernel/threads/IdleThread.h"
#include <optional> #include <utility>
/***************************************************************************** void Scheduler::lock() {
* Methode: Dispatcher::start * cpu.disable_int();
*---------------------------------------------------------------------------* }
* Beschreibung: Thread starten. *
* * void Scheduler::unlock() {
* Parameter: * cpu.enable_int();
* first Zu startender Thread. *
*****************************************************************************/
void Scheduler::start(Thread& first) {
if (active == nullptr) {
active = &first;
active->start();
}
} }
/***************************************************************************** /*****************************************************************************
* Methode: Dispatcher::dispatch * * Methode: Dispatcher::dispatch *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
* Beschreibung: Auf einen gegebenen Thread wechseln. * * Beschreibung: Auf den active thread wechseln. *
* * * *
* Parameter: * * Parameter: *
* next Thread der die CPU erhalten soll. * * next Thread der die CPU erhalten soll. *
*****************************************************************************/ *****************************************************************************/
void Scheduler::dispatch(Thread& next) { void Scheduler::dispatch(Thread* prev_raw) {
Thread* current = active; prev_raw->switchTo(**active); // First dereference the Iterator, then the unique_ptr to get Thread
active = &next;
current->switchTo(next);
} }
/***************************************************************************** /*****************************************************************************
@ -64,30 +55,13 @@ void Scheduler::schedule() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// We need to start the idle thread first as this one sets the scheduler to initialized // We need to start the idle thread first as this one sets the scheduler to initialized
// Other wise preemption will be blocked and nothing will happen if the first threads // and enables preemption.
// ready() function is blocking // Otherwise preemption will be blocked and nothing will happen if the first threads
this->start(*(Thread*)new IdleThread()); // The idle thread set initialized to true, so preemption // run() function is blocking
// only starts after this
}
/***************************************************************************** ready_queue.push_back(std::move(bse::make_unique<IdleThread>()));
* Methode: Scheduler::ready * active = ready_queue.end() - 1;
*---------------------------------------------------------------------------* (*active)->start();
* Beschreibung: Thread in readyQueue eintragen. *
* *
* Parameter: *
* that Einzutragender Thread *
*****************************************************************************/
void Scheduler::ready(Thread* that) {
/* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern
cpu.disable_int();
this->ready_queue.insert_last(that);
log << DEBUG << "Adding to ready_queue, ID: " << dec << that->tid << endl;
cpu.enable_int();
} }
/***************************************************************************** /*****************************************************************************
@ -103,24 +77,22 @@ void Scheduler::exit() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern // Thread-Wechsel durch PIT verhindern
cpu.disable_int(); lock();
if (this->ready_queue.empty()) { if (ready_queue.size() == 1) {
log << ERROR << "Can't exit last thread, active ID: " << dec << this->active->tid << endl; log << ERROR << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl;
cpu.enable_int(); unlock();
return; return;
} }
Thread* next = this->ready_queue.remove_first().value_or(nullptr); Thread* prev_raw = (*active).get();
if (next == nullptr) { log << DEBUG << "Exiting thread, ID: " << dec << (*active)->tid << endl;
log << ERROR << "(Exit) Couldn't remove thread from ready_queue" << endl;
cpu.enable_int(); active = ready_queue.erase(active);
return; if (active == ready_queue.end()) {
active = ready_queue.begin();
} }
log << DEBUG << "Exiting thread, ID: " << dec << this->active->tid << " => " << next->tid << endl; dispatch(prev_raw);
delete this->active;
this->dispatch(*next);
// Interrupts werden in Thread_switch in Thread.asm wieder zugelassen // Interrupts werden in Thread_switch in Thread.asm wieder zugelassen
// dispatch kehr nicht zurueck // dispatch kehr nicht zurueck
@ -137,54 +109,34 @@ void Scheduler::exit() {
* Parameter: * * Parameter: *
* that Zu terminierender Thread * * that Zu terminierender Thread *
*****************************************************************************/ *****************************************************************************/
void Scheduler::kill(Thread* that) { void Scheduler::kill(unsigned int tid) {
unsigned int (*pred)(const bse::unique_ptr<Thread>&) =
[](const bse::unique_ptr<Thread>& ptr) -> unsigned int { return ptr->tid; };
/* hier muss Code eingefuegt werden */ lock();
Thread* prev_raw = (*active).get();
std::size_t erased_els = bse::erase_if(ready_queue, pred, tid);
erased_els += bse::erase_if(block_queue, pred, tid);
// Thread-Wechsel durch PIT verhindern if (erased_els == 0) {
cpu.disable_int(); log << ERROR << "Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl;
if (!this->ready_queue.remove(that)) { unlock();
// Not in ready queue
if (!this->block_queue.remove(that)) {
// Not in block queue
log << ERROR << "Can't kill thread that is not in any queue, ID: " << dec << that->tid << endl;
cpu.enable_int();
return; return;
} }
}
log << DEBUG << "Killing thread, ID: " << dec << that->tid << endl; if (erased_els > 1) {
delete that; log << ERROR << "Killed more than 1 thread (oops)" << endl;
cpu.enable_int(); unlock();
}
void Scheduler::kill(unsigned int id) {
cpu.disable_int();
Thread* to_remove = NULL;
for (Thread* thread : this->ready_queue) {
if (thread->tid == id) {
this->ready_queue.remove(to_remove);
log << DEBUG << "Killing thread, ID: " << dec << id << endl;
delete to_remove;
cpu.enable_int();
return; return;
} }
}
for (Thread* thread : this->block_queue) { // Active thread could have been killed
if (thread->tid == id) { if (active >= ready_queue.end()) {
this->block_queue.remove(to_remove); active = ready_queue.begin();
log << DEBUG << "Killing thread, ID: " << dec << id << endl;
delete to_remove;
cpu.enable_int();
return;
}
} }
dispatch(prev_raw);
log << ERROR << "Can't kill thread that is not in any queue, ID: " << dec << id << endl; unlock();
cpu.enable_int();
} }
/***************************************************************************** /*****************************************************************************
@ -203,25 +155,21 @@ void Scheduler::yield() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Thread-Wechsel durch PIT verhindern // Thread-Wechsel durch PIT verhindern
cpu.disable_int(); lock();
if (this->ready_queue.empty()) { if (ready_queue.size() == 1) {
// log << TRACE << "Skipping yield as no thread is waiting, active ID: " << dec << this->active->tid << endl; // log << TRACE << "Skipping yield as no thread is waiting, active ID: " << dec << active->tid << endl;
cpu.enable_int(); unlock();
return; return;
} }
Thread* next = this->ready_queue.remove_first().value_or(nullptr); // log << TRACE << "Yielding, ID: " << dec << active->tid << " => " << next.tid << endl;
if (next == nullptr) {
log << ERROR << "(Yield) Couldn't remove thread from ready_queue" << endl; Thread* prev_raw = (*active).get();
cpu.enable_int(); ++active;
return; if (active == ready_queue.end()) {
active = ready_queue.begin();
} }
dispatch(prev_raw);
this->ready_queue.insert_last(this->active);
// log << TRACE << "Yielding, ID: " << dec << this->active->tid << " => " << next.tid << endl;
this->dispatch(*next);
} }
/***************************************************************************** /*****************************************************************************
@ -235,8 +183,8 @@ void Scheduler::preempt() {
/* Hier muss Code eingefuegt werden */ /* Hier muss Code eingefuegt werden */
cpu.disable_int(); lock();
this->yield(); yield();
} }
/***************************************************************************** /*****************************************************************************
@ -253,28 +201,23 @@ void Scheduler::block() {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Basically the same as exit() lock();
if (ready_queue.size() == 1) {
cpu.disable_int(); log << ERROR << "Can't block last thread, active ID: " << dec << (*active)->tid << endl;
if (this->ready_queue.empty()) { unlock();
log << ERROR << "Can't block last thread, active ID: " << dec << this->active->tid << endl;
cpu.enable_int();
return; return;
} }
this->block_queue.insert_last(this->active); // Thread that will be blocked waits in block_queue, so the scheduler can also Thread* prev_raw = (*active).get();
// kill blocked threads (for example keyboard demo needs this) std::size_t active_idx = bse::distance(ready_queue.begin(), active);
block_queue.push_back(std::move(ready_queue[active_idx]));
Thread* next = this->ready_queue.remove_first().value_or(nullptr); active = ready_queue.erase(active);
if (next == nullptr) { if (active == ready_queue.end()) {
log << ERROR << "(Block) Couldn't remove thread from ready_queue" << endl; active = ready_queue.begin();
cpu.enable_int();
return;
} }
log << TRACE << "Blocking thread, ID: " << dec << this->active->tid << " => " << next->tid << endl; dispatch(prev_raw);
this->dispatch(*next);
} }
/***************************************************************************** /*****************************************************************************
@ -288,18 +231,30 @@ void Scheduler::block() {
* * * *
* Parameter: that: Thread der deblockiert werden soll. * * Parameter: that: Thread der deblockiert werden soll. *
*****************************************************************************/ *****************************************************************************/
void Scheduler::deblock(Thread* that) { void Scheduler::deblock(unsigned int tid) {
/* hier muss Code eingefuegt werden */ /* hier muss Code eingefuegt werden */
// Basically the same as ready() lock();
cpu.disable_int(); bse::unique_ptr<Thread> thread;
if (!this->block_queue.remove(that)) { for (bse::Vector<bse::unique_ptr<Thread>>::Iterator it = block_queue.begin(); it != block_queue.end(); ++it) {
log << ERROR << "Unblocked thread wasn't in block_queue" << endl; if ((*it)->tid == tid) {
// Found thread with correct tid
std::size_t pos = bse::distance(block_queue.begin(), it);
thread = std::move(block_queue[pos]);
block_queue.erase(it);
break;
}
} }
this->ready_queue.insert_at(that, 0); // Prefer deblocked
log << TRACE << "Adding to start of ready_queue, ID: " << dec << that->tid << endl; if (!thread) {
cpu.enable_int(); log << ERROR << "Couldn't deblock thread with id: " << tid << endl;
unlock();
return;
}
ready_queue.push_back(std::move(thread));
unlock();
} }

View File

@ -12,66 +12,92 @@
#ifndef __Scheduler_include__ #ifndef __Scheduler_include__
#define __Scheduler_include__ #define __Scheduler_include__
#include "devices/CGA_Stream.h"
#include "kernel/threads/Thread.h" #include "kernel/threads/Thread.h"
#include "lib/Queue.h"
#include "lib/SpinLock.h" #include "lib/SpinLock.h"
#include "user/lib/ArrayList.h"
#include "user/lib/Logger.h" #include "user/lib/Logger.h"
#include "user/lib/mem/UniquePointer.h"
#include "user/lib/Vector.h"
class Scheduler { class Scheduler {
private: private:
Scheduler(const Scheduler& copy) = delete; // Verhindere Kopieren Scheduler(const Scheduler& copy) = delete; // Verhindere Kopieren
// Queue readyQueue; // auf die CPU wartende Threads Logger log;
// NOTE: Using this instead of the Queue is a side effect, I added the ArrayList for different reasons
// but my Queue was shit so I replaced it
// I want to use the queue again to get rid of the Vector overhead (copy + dynamic memory) but
// this is problematic with the unique_ptr, I guess I would have to wrap the threads in the "Chain"
// elements instead of a thread being a "Chain"
bse::Vector<bse::unique_ptr<Thread>> ready_queue;
bse::Vector<bse::unique_ptr<Thread>> block_queue;
// NOTE: It makes sense to keep track of the active thread through this as it makes handling the
// unique_ptr easier and reduces the copying in the vector when cycling through the threads
bse::Vector<bse::unique_ptr<Thread>>::Iterator active = ready_queue.begin();
// Scheduler wird evt. von einer Unterbrechung vom Zeitgeber gerufen, // Scheduler wird evt. von einer Unterbrechung vom Zeitgeber gerufen,
// bevor er initialisiert wurde // bevor er initialisiert wurde
Thread* active; unsigned int idle_tid = 0;
bool has_idle_thread;
Logger log;
// NOTE: I would have to release the lock when switching threads but I don't know exactly how to do this // NOTE: I would have to release the lock when switching threads but I don't know exactly how to do this
// in the assembly function // in the assembly function
// SpinLock lock; // Use spinlock instead of cpu.disable_int() because it still allows preemption // SpinLock lock; // Use spinlock instead of cpu.disable_int() because it still allows preemption
// // for threads that don't use the scheduler // // for threads that don't use the scheduler
// NOTE: Using this instead of the Queue is a side effect, I added the ArrayList for different reasons // Make it easy to switch locking mechanism
// but my Queue was shit so I replaced it (and didn't fix the Queue) static void lock();
ArrayList<Thread*> ready_queue; static void unlock();
ArrayList<Thread*> block_queue;
void start(Thread& first); void dispatch(Thread* prev); // Switches from prev to current active
void dispatch(Thread& next);
// ruft nur der Idle-Thread (erster Thread der vom Scheduler gestartet wird)
void enable_preemption(unsigned int tid) { idle_tid = tid; }
friend class IdleThread;
public: public:
Scheduler() : has_idle_thread(false), log("SCHED") {} Scheduler() : log("SCHED") {}
Thread* get_active() const { unsigned int get_active() const {
return this->active; return (*active)->tid;
} }
// Scheduler initialisiert? // Scheduler initialisiert?
// Zeitgeber-Unterbrechung kommt evt. bevor der Scheduler fertig // Zeitgeber-Unterbrechung kommt evt. bevor der Scheduler fertig
// intiialisiert wurde! // intiialisiert wurde!
bool preemption_enabled() const { return has_idle_thread; } bool preemption_enabled() const { return idle_tid != 0U; }
// ruft nur der Idle-Thread (erster Thread der vom Scheduler gestartet wird)
void enable_preemption() { has_idle_thread = true; }
// Scheduler starten // Scheduler starten
void schedule(); void schedule();
// Thread in readyQueue eintragen /*****************************************************************************
void ready(Thread* that); * Methode: Scheduler::ready *
*---------------------------------------------------------------------------*
* Beschreibung: Thread in readyQueue eintragen. *
*****************************************************************************/
template<typename T, typename... Args>
unsigned int ready(Args... args) {
lock();
bse::unique_ptr<T> thread = bse::make_unique<T>(std::forward<Args>(args)...);
unsigned int tid = thread->tid;
log << DEBUG << "Adding to ready_queue, ID: " << dec << tid << endl;
ready_queue.push_back(std::move(thread));
unlock();
return tid;
}
// Thread terminiert sich selbst // Thread terminiert sich selbst
// NOTE: When a thread exits itself it will disappear...
// Maybe put exited threads in an exited queue?
// Then they would have to be acquired from there to exit...
void exit(); void exit();
// Thread mit 'Gewalt' terminieren // Thread mit 'Gewalt' terminieren
void kill(Thread* that); void kill(unsigned int tid);
void kill(unsigned int id);
// CPU freiwillig abgeben und Auswahl des naechsten Threads // CPU freiwillig abgeben und Auswahl des naechsten Threads
void yield(); void yield();
@ -79,8 +105,11 @@ public:
// Thread umschalten; wird aus der ISR des PITs gerufen // Thread umschalten; wird aus der ISR des PITs gerufen
void preempt(); void preempt();
// Blocks current thread (move to block_queue)
void block(); void block();
void deblock(Thread* that);
// Deblock by tid (move to ready_queue)
void deblock(unsigned int tid);
}; };
#endif #endif