add semaphore to cga_stream + use it in demos
This commit is contained in:
@ -45,15 +45,22 @@ class CGA_Stream : public OutStream, public CGA {
|
|||||||
private:
|
private:
|
||||||
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
|
CGA_Stream(CGA_Stream& copy) = delete; // Verhindere Kopieren
|
||||||
|
|
||||||
|
Semaphore sem;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CGA::color color_fg;
|
CGA::color color_fg;
|
||||||
CGA::color color_bg;
|
CGA::color color_bg;
|
||||||
bool blink;
|
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();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lock() { sem.p(); }
|
||||||
|
void unlock() { sem.v(); }
|
||||||
|
// void lock() {}
|
||||||
|
// void unlock() {}
|
||||||
|
|
||||||
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
|
// Methode zur Ausgabe des Pufferinhalts der Basisklasse StringBuffer.
|
||||||
void flush() override;
|
void flush() override;
|
||||||
|
|
||||||
|
|||||||
@ -71,7 +71,6 @@ inline void PCSPK::delay(int time) {
|
|||||||
|
|
||||||
// systime is incremented in 10ms steps
|
// systime is incremented in 10ms steps
|
||||||
while ((systime - start_time) * 10 < time) {}
|
while ((systime - start_time) * 10 < time) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "user/demo/VectorDemo.h"
|
#include "user/demo/VectorDemo.h"
|
||||||
|
|
||||||
void print_demo_menu() {
|
void print_demo_menu() {
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
kout << "Demo Menu, press number to start, K to kill:\n"
|
kout << "Demo Menu, press number to start, K to kill:\n"
|
||||||
<< "1 - Text Demo\n"
|
<< "1 - Text Demo\n"
|
||||||
@ -21,6 +22,7 @@ void print_demo_menu() {
|
|||||||
<< "6 - Bluescreen Demo\n"
|
<< "6 - Bluescreen Demo\n"
|
||||||
<< "7 - Preemption Demo\n"
|
<< "7 - Preemption Demo\n"
|
||||||
<< endl;
|
<< endl;
|
||||||
|
kout.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::run() {
|
void MainMenu::run() {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ void ArrayDemo::run() {
|
|||||||
bse::Array<int, 10> arr2 {};
|
bse::Array<int, 10> arr2 {};
|
||||||
bse::Array<Thread*, 10> arr3 {};
|
bse::Array<Thread*, 10> arr3 {};
|
||||||
|
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
|
|
||||||
kout << "Adding..." << endl;
|
kout << "Adding..." << endl;
|
||||||
@ -35,5 +36,6 @@ void ArrayDemo::run() {
|
|||||||
|
|
||||||
// arr1.swap(arr3); // Not possible as type/size has to match
|
// arr1.swap(arr3); // Not possible as type/size has to match
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "user/demo/HeapDemo.h"
|
#include "user/demo/HeapDemo.h"
|
||||||
|
|
||||||
void HeapDemo::run() {
|
void HeapDemo::run() {
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
kout << "HEAP_DEMO ===================================================================" << endl;
|
kout << "HEAP_DEMO ===================================================================" << endl;
|
||||||
|
|
||||||
@ -72,5 +73,6 @@ void HeapDemo::run() {
|
|||||||
|
|
||||||
kout << "HEAP_DEMO END ===============================================================" << endl;
|
kout << "HEAP_DEMO END ===============================================================" << endl;
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,14 +16,16 @@ void KeyboardDemo::run() {
|
|||||||
|
|
||||||
kout << "Keyboard Demo: " << endl;
|
kout << "Keyboard Demo: " << endl;
|
||||||
|
|
||||||
// kout.lock();
|
kout.lock();
|
||||||
kout.clear();
|
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) {
|
while (true) {
|
||||||
kout << listener.waitForKeyEvent();
|
kout << listener.waitForKeyEvent();
|
||||||
kout.flush();
|
kout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// kout.unlock();
|
|
||||||
scheduler.exit();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ public:
|
|||||||
~KeyboardDemo() override {
|
~KeyboardDemo() override {
|
||||||
log << INFO << "Uninitialized KeyboardDemo" << endl;
|
log << INFO << "Uninitialized KeyboardDemo" << endl;
|
||||||
kevman.unsubscribe(this->listener);
|
kevman.unsubscribe(this->listener);
|
||||||
// kout.unlock();
|
kout.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() override;
|
void run() override;
|
||||||
|
|||||||
@ -1,9 +1,16 @@
|
|||||||
#include "user/demo/PCSPKdemo.h"
|
#include "user/demo/PCSPKdemo.h"
|
||||||
|
|
||||||
void PCSPKdemo::run() {
|
void PCSPKdemo::run() {
|
||||||
|
kout.lock();
|
||||||
|
kout.clear();
|
||||||
kout << "Playing..." << endl;
|
kout << "Playing..." << endl;
|
||||||
|
kout.unlock();
|
||||||
|
|
||||||
(pcspk.*this->melody)(); // This syntax is confusing as hell
|
(pcspk.*this->melody)(); // This syntax is confusing as hell
|
||||||
|
|
||||||
|
kout.lock();
|
||||||
kout << "Finished" << endl;
|
kout << "Finished" << endl;
|
||||||
|
kout.unlock();
|
||||||
|
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
class PCSPKdemo : public Thread {
|
class PCSPKdemo : public Thread {
|
||||||
private:
|
private:
|
||||||
PCSPKdemo(const PCSPKdemo& copy) = delete;
|
PCSPKdemo(const PCSPKdemo& copy) = delete;
|
||||||
|
|
||||||
void (PCSPK::*melody)(void); // Allow to pass a melody to play when initializing the demo
|
void (PCSPK::*melody)(void); // Allow to pass a melody to play when initializing the demo
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -1,23 +1,22 @@
|
|||||||
#include "user/demo/PreemptiveThreadDemo.h"
|
#include "user/demo/PreemptiveThreadDemo.h"
|
||||||
|
|
||||||
Semaphore PreemptiveLoopThread::sem(1);
|
|
||||||
|
|
||||||
void PreemptiveLoopThread::run() {
|
void PreemptiveLoopThread::run() {
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Basic synchronization by semaphore
|
// Basic synchronization by semaphore
|
||||||
sem.p();
|
kout.lock();
|
||||||
|
|
||||||
// Saving + restoring kout position doesn't help much as preemption still occurs
|
// Saving + restoring kout position doesn't help much as preemption still occurs
|
||||||
kout.setpos(55, this->id);
|
kout.setpos(55, this->id);
|
||||||
kout << fillw(3) << this->id << fillw(0) << ": " << dec << cnt++ << endl;
|
kout << fillw(3) << this->id << fillw(0) << ": " << dec << cnt++ << endl;
|
||||||
|
|
||||||
sem.v();
|
kout.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PreemptiveThreadDemo::run() {
|
void PreemptiveThreadDemo::run() {
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
|
|
||||||
kout << "Preemptive Thread Demo:" << endl;
|
kout << "Preemptive Thread Demo:" << endl;
|
||||||
@ -28,5 +27,6 @@ void PreemptiveThreadDemo::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
kout << "Exiting main thread" << endl;
|
kout << "Exiting main thread" << endl;
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,8 +8,6 @@
|
|||||||
class PreemptiveLoopThread : public Thread {
|
class PreemptiveLoopThread : public Thread {
|
||||||
private:
|
private:
|
||||||
int id;
|
int id;
|
||||||
static Semaphore sem;
|
|
||||||
|
|
||||||
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
|
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include "user/lib/mem/UniquePointer.h"
|
#include "user/lib/mem/UniquePointer.h"
|
||||||
|
|
||||||
void SmartPointerDemo::run() {
|
void SmartPointerDemo::run() {
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
|
|
||||||
kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl;
|
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;
|
log << INFO << "Should be deleted by now..." << endl;
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ void TextDemo::run() {
|
|||||||
|
|
||||||
/* Hier muess Code eingefuegt werden */
|
/* Hier muess Code eingefuegt werden */
|
||||||
|
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
kout << "TextDemo\n"
|
kout << "TextDemo\n"
|
||||||
<< endl;
|
<< endl;
|
||||||
@ -37,5 +38,6 @@ void TextDemo::run() {
|
|||||||
|
|
||||||
kout << endl;
|
kout << endl;
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ void print(bse::Vector<int> list) {
|
|||||||
void VectorDemo::run() {
|
void VectorDemo::run() {
|
||||||
bse::Vector<int> list;
|
bse::Vector<int> list;
|
||||||
|
|
||||||
|
kout.lock();
|
||||||
kout.clear();
|
kout.clear();
|
||||||
kout << "Initial list size: " << dec << list.size() << endl;
|
kout << "Initial list size: " << dec << list.size() << endl;
|
||||||
|
|
||||||
@ -102,5 +103,6 @@ void VectorDemo::run() {
|
|||||||
kout << "List contains element: " << dec << i << endl;
|
kout << "List contains element: " << dec << i << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
scheduler.exit();
|
scheduler.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user