1

adapt to scheduler rework

This commit is contained in:
2022-07-21 20:36:12 +02:00
parent 2ac36cda83
commit 017f7de650
13 changed files with 59 additions and 53 deletions

View File

@ -30,7 +30,7 @@ public:
void run() override {
// Idle-Thread läuft, ab jetzt ist der Scheduler fertig initialisiert
log << INFO << "IdleThread enabled preemption" << endl;
scheduler.enable_preemption();
scheduler.enable_preemption(this->tid);
while (true) {
// kout << "Idle!" << endl;

View File

@ -31,7 +31,7 @@ extern "C" {
}
Logger Thread::log = Logger("Thread");
unsigned int ThreadCnt = 0;
unsigned int ThreadCnt = 1; // Skip tid 0 as the scheduler indicates no preemption with 0
/*****************************************************************************
* Prozedur: Coroutine_init *

View File

@ -11,7 +11,7 @@ void Semaphore::p() {
this->lock.release();
} else {
// Block and manage thread in semaphore queue until it's woken up by v() again
this->waitQueue.push_back(scheduler.get_active());
this->wait_queue.push_back(scheduler.get_active());
this->lock.release();
scheduler.block(); // Moves to next thread
}
@ -20,16 +20,16 @@ void Semaphore::p() {
void Semaphore::v() {
this->lock.acquire();
if (!this->waitQueue.empty()) {
if (!this->wait_queue.empty()) {
// Semaphore stays busy and unblocks next thread to work in critical section
if (this->waitQueue.empty()) {
if (this->wait_queue.empty()) {
this->lock.release();
return;
}
Thread* next = this->waitQueue[0];
this->waitQueue.erase(waitQueue.begin());
unsigned int tid = this->wait_queue.front();
this->wait_queue.erase(wait_queue.begin());
this->lock.release();
scheduler.deblock(next);
scheduler.deblock(tid);
} else {
// No more threads want to work so free semaphore
this->counter = this->counter + 1;

View File

@ -20,7 +20,7 @@ private:
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
// Queue fuer wartende Threads.
bse::Vector<Thread*> waitQueue;
bse::Vector<unsigned int> wait_queue;
SpinLock lock;
int counter;

View File

@ -62,8 +62,8 @@ int main() {
waitForReturn();
// Scheduler starten (schedule() erzeugt den Idle-Thread)
scheduler.ready(new MainMenu()); // NOTE: A thread that manages other threads has to be added before scheduler.schedule(),
// because scheduler.schedule() doesn't return, only threads get cpu time
scheduler.ready<MainMenu>(); // NOTE: A thread that manages other threads has to be added before scheduler.schedule(),
// because scheduler.schedule() doesn't return, only threads get cpu time
scheduler.schedule();
// NOTE: Enforced ToDo's (needed)
@ -119,7 +119,7 @@ int main() {
// TODO: Use singleton pattern for some device classes/classes used only in globals
// TODO: Introduce name to threads?
// DONE: Remove Iterator from List.h
// TODO: Move Array/ArrayList/LinkedList/List to bse namespace
// DONE: Move Array/ArrayList/LinkedList/List to bse namespace
// TODO: Remove the Input.h file and replace functionality with kevman
// TODO: Fix the output locking, lock for every kout till endl? (kout << ... << endl)

View File

@ -29,52 +29,47 @@ void MainMenu::run() {
print_demo_menu();
char input = '\0';
Thread* choosen_demo = NULL;
unsigned int running_demo = 0;
while (true) {
input = this->listener.waitForKeyEvent();
if (choosen_demo == NULL) {
if (running_demo == 0) {
switch (input) {
case '1':
choosen_demo = new TextDemo();
running_demo = scheduler.ready<TextDemo>();
break;
case '2':
choosen_demo = new PCSPKdemo(&PCSPK::aerodynamic);
running_demo = scheduler.ready<PCSPKdemo>(&PCSPK::aerodynamic);
break;
case '3':
choosen_demo = new KeyboardDemo();
running_demo = scheduler.ready<KeyboardDemo>();
break;
case '4':
choosen_demo = new HeapDemo();
running_demo = scheduler.ready<HeapDemo>();
break;
case '5':
choosen_demo = new VBEdemo();
running_demo = scheduler.ready<VBEdemo>();
break;
case '6':
choosen_demo = new BlueScreenDemo();
running_demo = scheduler.ready<BlueScreenDemo>();
break;
case '7':
choosen_demo = new PreemptiveThreadDemo(3);
running_demo = scheduler.ready<PreemptiveThreadDemo>(3);
break;
case 'q':
choosen_demo = new VectorDemo();
running_demo = scheduler.ready<VectorDemo>();
break;
case 'w':
choosen_demo = new ArrayDemo();
running_demo = scheduler.ready<ArrayDemo>();
break;
case 'e':
choosen_demo = new SmartPointerDemo();
running_demo = scheduler.ready<SmartPointerDemo>();
break;
}
if (choosen_demo != NULL) {
// We actually chose something
scheduler.ready(choosen_demo);
}
} else if (input == 'K') {
scheduler.kill(choosen_demo); // NOTE: If thread exits itself this will throw error
choosen_demo = NULL;
scheduler.kill(running_demo); // NOTE: If thread exits itself this will throw error
running_demo = 0;
print_demo_menu();
}

View File

@ -12,7 +12,7 @@ private:
KeyEventListener listener;
public:
MainMenu() : listener(*this) {
MainMenu() : listener(this->tid) {
log << INFO << "MainMenu initialized with ID: " << dec << this->tid << endl;
kevman.subscribe(this->listener);
}

View File

@ -22,7 +22,7 @@ private:
KeyEventListener listener;
public:
KeyboardDemo() : listener(*this) {
KeyboardDemo() : listener(this->tid) {
log << INFO << "Initialized KeyboardDemo with ID: " << dec << this->tid << endl;
kevman.subscribe(this->listener);
}

View File

@ -7,6 +7,8 @@ void PreemptiveLoopThread::run() {
// Basic synchronization by semaphore
// NOTE: I placed the semaphore inside the CGA_Stream so multiple demos can synchronize, not
// only this one. This is optional so disruptions can still occur because of preemption
// (I only use this for the user output (demos), anywhere else could become problematic
// quickly...)
kout.lock();
// Saving + restoring kout position doesn't help much as preemption still occurs
@ -19,17 +21,13 @@ void PreemptiveLoopThread::run() {
void PreemptiveThreadDemo::run() {
kout << "Preemptive Thread Demo" << endl;
Thread* threads[this->number_of_threads];
kout << "Allocating LoopThreads" << endl;
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
threads[i] = new PreemptiveLoopThread(i);
}
kout << "Adding threads to ready queue" << endl;
kout << "Readying LoopThreads" << endl;
for (unsigned int i = 0; i < this->number_of_threads; ++i) {
scheduler.ready(threads[i]);
threads.push_back(scheduler.ready<PreemptiveLoopThread>(i));
}
kout << "Exiting main thread" << endl;
while (listener.waitForKeyEvent() != 'L') {}
scheduler.exit();
}

View File

@ -25,12 +25,20 @@ private:
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
unsigned int number_of_threads;
bse::Vector<unsigned int> threads;
KeyEventListener listener;
public:
PreemptiveThreadDemo(unsigned int n) : number_of_threads(n) {
PreemptiveThreadDemo(unsigned int n) : number_of_threads(n), listener(this->tid) {
kout << "Initialized PreemptiveThreadDemo" << endl;
}
~PreemptiveThreadDemo() override {
for (unsigned int tid : threads) {
scheduler.kill(tid);
}
}
// Thread-Startmethode
void run() override;
};

View File

@ -10,9 +10,9 @@ private:
char lastChar = '\0';
public:
Thread& thread; // Thread which contains this listener, so the listener can block the thread
unsigned int tid; // Thread which contains this listener, so the listener can block the thread
KeyEventListener(Thread& thread) : thread(thread) {}
KeyEventListener(unsigned int tid) : tid(tid) {}
char waitForKeyEvent() const; // Blocks the thread until woken up by manager
void trigger(char c); // Gets called from KeyEventManager

View File

@ -1,21 +1,26 @@
#include "user/event/KeyEventManager.h"
#include "kernel/Globals.h"
void KeyEventManager::subscribe(KeyEventListener& listener) {
log << DEBUG << "Subscribe, Thread ID: " << dec << listener.thread.tid << endl;
this->listeners.insert_last(&listener);
void KeyEventManager::subscribe(KeyEventListener& sub) {
log << DEBUG << "Subscribe, Thread ID: " << dec << sub.tid << endl;
this->listeners.push_back(&sub);
}
void KeyEventManager::unsubscribe(KeyEventListener& listener) {
log << DEBUG << "Unsubscribe, Thread ID: " << dec << listener.thread.tid << endl;
this->listeners.remove(&listener);
void KeyEventManager::unsubscribe(KeyEventListener& unsub) {
log << DEBUG << "Unsubscribe, Thread ID: " << dec << unsub.tid << endl;
for (bse::Vector<KeyEventListener*>::Iterator it = listeners.begin(); it != listeners.end(); ++it) {
if ((*it)->tid == unsub.tid) {
this->listeners.erase(it);
return;
}
}
}
void KeyEventManager::broadcast(char c) {
log << TRACE << "Beginning Broadcast" << endl;
for (KeyEventListener* listener : this->listeners) {
log << TRACE << "Broadcasting " << c << " to Thread ID: " << dec << listener->thread.tid << endl;
log << TRACE << "Broadcasting " << c << " to Thread ID: " << dec << listener->tid << endl;
listener->trigger(c);
scheduler.deblock(&listener->thread);
scheduler.deblock(listener->tid);
}
}

View File

@ -18,8 +18,8 @@ private:
public:
KeyEventManager() : log("KEvMan") {}
void subscribe(KeyEventListener& listener);
void unsubscribe(KeyEventListener& listener);
void subscribe(KeyEventListener& sub);
void unsubscribe(KeyEventListener& unsub);
void broadcast(char c); // Unblocks all input waiting threads, I don't have a method to direct input
};