1

use static logger

This commit is contained in:
2022-07-22 23:32:46 +02:00
parent ec09b0e6d2
commit bd95c02a08
24 changed files with 139 additions and 151 deletions

View File

@ -23,10 +23,10 @@ public:
void run() override {
// Idle-Thread läuft, ab jetzt ist der Scheduler fertig initialisiert
log << INFO << "IdleThread enabled preemption" << endl;
log.info() << "IdleThread enabled preemption" << endl;
scheduler.enable_preemption(this->tid);
if (!scheduler.preemption_enabled()) {
log << ERROR << "Preemption disabled" << endl;
log.error() << "Preemption disabled" << endl;
}
while (true) {

View File

@ -39,10 +39,10 @@ void Scheduler::start(bse::Vector<bse::unique_ptr<Thread>>::Iterator next) {
active = next;
if (active >= ready_queue.end()) {
active = ready_queue.begin();
log << DEBUG << "Scheduler::start started different thread than passed" << endl;
log.debug() << "Scheduler::start started different thread than passed" << endl;
}
if constexpr (INSANE_TRACE) {
log << TRACE << "Starting Thread with id: " << dec << (*active)->tid << endl;
log.trace() << "Starting Thread with id: " << dec << (*active)->tid << endl;
}
(*active)->start(); // First dereference the Iterator, then the unique_ptr to get Thread
}
@ -51,10 +51,10 @@ void Scheduler::switch_to(Thread* prev_raw, bse::Vector<bse::unique_ptr<Thread>>
active = next;
if (active >= ready_queue.end()) {
active = ready_queue.begin();
// log << DEBUG << "Scheduler::switch_to started different thread than passed" << endl;
// log.debug() << "Scheduler::switch_to started different thread than passed" << endl;
}
if constexpr (INSANE_TRACE) {
log << TRACE << "Switching to Thread with id: " << dec << (*active)->tid << endl;
log.trace() << "Switching to Thread with id: " << dec << (*active)->tid << endl;
}
prev_raw->switchTo(**active);
}
@ -74,7 +74,7 @@ void Scheduler::schedule() {
// run() function is blocking
ready_queue.push_back(bse::make_unique<IdleThread>());
log << INFO << "Starting scheduling: starting thread with id: " << dec << (*(ready_queue.end() - 1))->tid << endl;
log.info() << "Starting scheduling: starting thread with id: " << dec << (*(ready_queue.end() - 1))->tid << endl;
start(ready_queue.end() - 1);
}
@ -85,7 +85,7 @@ void Scheduler::schedule() {
*****************************************************************************/
void Scheduler::ready(bse::unique_ptr<Thread>&& thread) {
cpu.disable_int();
log << DEBUG << "Adding to ready_queue, ID: " << dec << thread->tid << endl;
log.debug() << "Adding to ready_queue, ID: " << dec << thread->tid << endl;
ready_queue.push_back(std::move(thread));
cpu.enable_int();
}
@ -106,12 +106,12 @@ void Scheduler::exit() {
cpu.disable_int();
if (ready_queue.size() == 1) {
log << ERROR << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl;
log.error() << "Can't exit last thread, active ID: " << dec << (*active)->tid << endl;
cpu.enable_int();
return;
}
log << DEBUG << "Exiting thread, ID: " << dec << (*active)->tid << endl;
log.debug() << "Exiting thread, ID: " << dec << (*active)->tid << endl;
start(ready_queue.erase(active)); // erase returns the next iterator after the erased element
// cannot use switch_to here as the previous thread no longer
// exists (was deleted by erase)
@ -149,7 +149,7 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
// Just erase from queue, do not need to switch
block_queue.erase(it);
log << INFO << "Killed thread from block_queue with id: " << tid << endl;
log.info() << "Killed thread from block_queue with id: " << tid << endl;
cpu.enable_int();
return;
@ -158,7 +158,7 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
// Ready queue, can't kill last one
if (ready_queue.size() == 1) {
log << ERROR << "Kill: Can't kill last thread in ready_queue with id: " << tid << endl;
log.error() << "Kill: Can't kill last thread in ready_queue with id: " << tid << endl;
cpu.enable_int();
return;
}
@ -175,7 +175,7 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
if (tid == prev_tid) {
// If we killed the active thread we need to switch to another one
log << INFO << "Killed active thread from ready_queue with id: " << tid << endl;
log.info() << "Killed active thread from ready_queue with id: " << tid << endl;
// Switch to current active after old active was removed
start(ready_queue.erase(it));
@ -183,14 +183,14 @@ void Scheduler::kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
// Just erase from queue, do not need to switch
ready_queue.erase(it);
log << INFO << "Killed thread from ready_queue with id: " << tid << endl;
log.info() << "Killed thread from ready_queue with id: " << tid << endl;
cpu.enable_int();
return;
}
}
log << ERROR << "Kill: Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl;
log.error() << "Kill: Couldn't find thread with id: " << tid << " in ready- or block-queue" << endl;
cpu.enable_int();
}
@ -202,7 +202,7 @@ void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
for (bse::unique_ptr<Thread>& thread : block_queue) {
if (thread->tid == tid) {
thread->suicide();
log << INFO << "Nice killed thread in block_queue with id: " << tid << endl;
log.info() << "Nice killed thread in block_queue with id: " << tid << endl;
deblock(tid);
cpu.enable_int();
return;
@ -212,13 +212,13 @@ void Scheduler::nice_kill(unsigned int tid, bse::unique_ptr<Thread>* ptr) {
for (bse::unique_ptr<Thread>& thread : ready_queue) {
if (thread->tid == tid) {
thread->suicide();
log << INFO << "Nice killed thread in ready_queue with id: " << tid << endl;
log.info() << "Nice killed thread in ready_queue with id: " << tid << endl;
cpu.enable_int();
return;
}
}
log << ERROR << "Can't nice kill thread (not found) with id: " << tid << endl;
log.error() << "Can't nice kill thread (not found) with id: " << tid << endl;
cpu.enable_int();
}
@ -242,13 +242,13 @@ void Scheduler::yield() {
if (ready_queue.size() == 1) {
if constexpr (INSANE_TRACE) {
log << TRACE << "Skipping yield as no thread is waiting, active ID: " << dec << (*active)->tid << endl;
log.trace() << "Skipping yield as no thread is waiting, active ID: " << dec << (*active)->tid << endl;
}
cpu.enable_int();
return;
}
if constexpr (INSANE_TRACE) {
log << TRACE << "Yielding, ID: " << dec << (*active)->tid << endl;
log.trace() << "Yielding, ID: " << dec << (*active)->tid << endl;
}
switch_to((*active).get(), active + 1); // prev_raw is valid since no thread was killed/deleted
}
@ -285,7 +285,7 @@ void Scheduler::block() {
cpu.disable_int();
if (ready_queue.size() == 1) {
log << ERROR << "Can't block last thread, active ID: " << dec << (*active)->tid << endl;
log.error() << "Can't block last thread, active ID: " << dec << (*active)->tid << endl;
cpu.enable_int();
return;
}
@ -295,7 +295,7 @@ void Scheduler::block() {
block_queue.push_back(std::move(ready_queue[pos]));
if constexpr (INSANE_TRACE) {
log << TRACE << "Blocked thread with id: " << prev_raw->tid << endl;
log.trace() << "Blocked thread with id: " << prev_raw->tid << endl;
}
switch_to(prev_raw, ready_queue.erase(active)); // prev_raw is valid as thread was moved before vector erase
@ -327,13 +327,13 @@ void Scheduler::deblock(unsigned int tid) {
// thread to prefer deblocked threads
block_queue.erase(it);
if constexpr (INSANE_TRACE) {
log << TRACE << "Deblocked thread with id: " << tid << endl;
log.trace() << "Deblocked thread with id: " << tid << endl;
}
cpu.enable_int();
return;
}
}
log << ERROR << "Couldn't deblock thread with id: " << tid << endl;
log.error() << "Couldn't deblock thread with id: " << tid << endl;
cpu.enable_int();
}

View File

@ -22,7 +22,7 @@ class Scheduler {
private:
Scheduler(const Scheduler& copy) = delete; // Verhindere Kopieren
Logger log;
NamedLogger 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

View File

@ -30,7 +30,6 @@ extern "C" {
void Thread_switch(struct ThreadState* regs_now, struct ThreadState* reg_then);
}
Logger Thread::log = Logger("Thread");
unsigned int ThreadCnt = 1; // Skip tid 0 as the scheduler indicates no preemption with 0
/*****************************************************************************
@ -107,8 +106,8 @@ void kickoff(Thread* object) {
* Parameter: *
* stack Stack für die neue Koroutine *
*****************************************************************************/
Thread::Thread(char* name) : name(name), stack(new unsigned int[1024]), tid(ThreadCnt++) {
Thread::log << INFO << "Initialized thread with ID: " << this->tid << " (" << name << ")" << endl;
Thread::Thread(char* name) : stack(new unsigned int[1024]), log(name), name(name), tid(ThreadCnt++) {
log.info() << "Initialized thread with ID: " << this->tid << " (" << name << ")" << endl;
Thread_init(&regs, stack + 1024, kickoff, this); // Stack grows from top to bottom
}

View File

@ -41,7 +41,7 @@ private:
protected:
Thread(char* name);
static Logger log;
NamedLogger log;
bool running = true; // For soft exit, if thread uses infinite loop inside run(), use this as condition
char* name; // For logging
@ -50,7 +50,7 @@ protected:
public:
virtual ~Thread() {
log << INFO << "Uninitialized thread, ID: " << dec << this->tid << " (" << name << ")" << endl;
log.info() << "Uninitialized thread, ID: " << dec << this->tid << " (" << name << ")" << endl;
delete[] this->stack;
}