1

add semaphore to cga_stream + use it in demos

This commit is contained in:
2022-07-22 18:12:56 +02:00
parent e3dcadcc3c
commit e56e76ea4e
14 changed files with 39 additions and 13 deletions

View File

@ -45,15 +45,22 @@ class CGA_Stream : public OutStream, public CGA {
private:
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
Semaphore sem;
public:
CGA::color color_fg;
CGA::color color_bg;
bool blink;
CGA_Stream() : color_fg(CGA::LIGHT_GREY), color_bg(CGA::BLACK), blink(false) {
CGA_Stream() : sem(1), color_fg(CGA::LIGHT_GREY), color_bg(CGA::BLACK), blink(false) {
flush();
}
void lock() { sem.p(); }
void unlock() { sem.v(); }
// void lock() {}
// void unlock() {}
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
void flush() override;

View File

@ -71,7 +71,6 @@ inline void PCSPK::delay(int time) {
// systime is incremented in 10ms steps
while ((systime - start_time) * 10 < time) {}
}
/*****************************************************************************

View File

@ -11,6 +11,7 @@
#include "user/demo/VectorDemo.h"
void print_demo_menu() {
kout.lock();
kout.clear();
kout << "Demo Menu, press number to start, K to kill:\n"
<< "1 - Text Demo\n"
@ -21,6 +22,7 @@ void print_demo_menu() {
<< "6 - Bluescreen Demo\n"
<< "7 - Preemption Demo\n"
<< endl;
kout.unlock();
}
void MainMenu::run() {

View File

@ -5,6 +5,7 @@ void ArrayDemo::run() {
bse::Array<int, 10> arr2 {};
bse::Array<Thread*, 10> arr3 {};
kout.lock();
kout.clear();
kout << "Adding..." << endl;
@ -35,5 +36,6 @@ void ArrayDemo::run() {
// arr1.swap(arr3); // Not possible as type/size has to match
kout.unlock();
scheduler.exit();
}

View File

@ -11,6 +11,7 @@
#include "user/demo/HeapDemo.h"
void HeapDemo::run() {
kout.lock();
kout.clear();
kout << "HEAP_DEMO ===================================================================" << endl;
@ -72,5 +73,6 @@ void HeapDemo::run() {
kout << "HEAP_DEMO END ===============================================================" << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -16,14 +16,16 @@ void KeyboardDemo::run() {
kout << "Keyboard Demo: " << endl;
// kout.lock();
kout.lock();
kout.clear();
kout << "Info: Die Keyboard Demo sperrt den Output Stream:\n"
<< " Wenn die Preemption Demo laeuft wird diese also erst\n"
<< " fortfahren wenn die Keyboard Demo wieder beendet ist." << endl;
kout << "\nInput: ";
kout.flush();
while (true) {
kout << listener.waitForKeyEvent();
kout.flush();
}
// kout.unlock();
scheduler.exit();
}

View File

@ -31,7 +31,7 @@ public:
~KeyboardDemo() override {
log << INFO << "Uninitialized KeyboardDemo" << endl;
kevman.unsubscribe(this->listener);
// kout.unlock();
kout.unlock();
}
void run() override;

View File

@ -1,9 +1,16 @@
#include "user/demo/PCSPKdemo.h"
void PCSPKdemo::run() {
kout.lock();
kout.clear();
kout << "Playing..." << endl;
kout.unlock();
(pcspk.*this->melody)(); // This syntax is confusing as hell
kout.lock();
kout << "Finished" << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -7,6 +7,7 @@
class PCSPKdemo : public Thread {
private:
PCSPKdemo(const PCSPKdemo& copy) = delete;
void (PCSPK::*melody)(void); // Allow to pass a melody to play when initializing the demo
public:

View File

@ -1,23 +1,22 @@
#include "user/demo/PreemptiveThreadDemo.h"
Semaphore PreemptiveLoopThread::sem(1);
void PreemptiveLoopThread::run() {
int cnt = 0;
while (true) {
// Basic synchronization by semaphore
sem.p();
kout.lock();
// Saving + restoring kout position doesn't help much as preemption still occurs
kout.setpos(55, this->id);
kout << fillw(3) << this->id << fillw(0) << ": " << dec << cnt++ << endl;
sem.v();
kout.unlock();
}
}
void PreemptiveThreadDemo::run() {
kout.lock();
kout.clear();
kout << "Preemptive Thread Demo:" << endl;
@ -28,5 +27,6 @@ void PreemptiveThreadDemo::run() {
}
kout << "Exiting main thread" << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -8,8 +8,6 @@
class PreemptiveLoopThread : public Thread {
private:
int id;
static Semaphore sem;
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
public:

View File

@ -4,6 +4,7 @@
#include "user/lib/mem/UniquePointer.h"
void SmartPointerDemo::run() {
kout.lock();
kout.clear();
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
@ -118,5 +119,6 @@ void SmartPointerDemo::run() {
}
log << INFO << "Should be deleted by now..." << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -14,6 +14,7 @@ void TextDemo::run() {
/* Hier muess Code eingefuegt werden */
kout.lock();
kout.clear();
kout << "TextDemo\n"
<< endl;
@ -37,5 +38,6 @@ void TextDemo::run() {
kout << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -11,6 +11,7 @@ void print(bse::Vector<int> list) {
void VectorDemo::run() {
bse::Vector<int> list;
kout.lock();
kout.clear();
kout << "Initial list size: " << dec << list.size() << endl;
@ -102,5 +103,6 @@ void VectorDemo::run() {
kout << "List contains element: " << dec << i << endl;
}
kout.unlock();
scheduler.exit();
}