adapt to scheduler rework
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user