1

synchronize with semaphore

This commit is contained in:
2022-07-11 15:37:45 +02:00
parent 66b3b914d0
commit 597b823ac3
3 changed files with 15 additions and 7 deletions

View File

@ -6,12 +6,12 @@ void PreemptiveLoopThread::run() {
int cnt = 0; int cnt = 0;
while (true) { while (true) {
// Basic synchronization by disabling PIT interrupts // Basic synchronization by semaphore
cpu.disable_int(); sem->p();
kout.setpos(55, this->id); kout.setpos(55, this->id);
kout << this->id << ": " << dec << cnt++ << endl; kout << this->id << ": " << dec << cnt++ << endl;
cpu.enable_int(); sem->v();
} }
} }

View File

@ -3,17 +3,19 @@
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include "kernel/threads/Thread.h" #include "kernel/threads/Thread.h"
#include "lib/Semaphore.h"
class PreemptiveLoopThread : public Thread { class PreemptiveLoopThread : public Thread {
private: private:
int id; int id;
Semaphore* sem;
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
public: public:
// Gibt der Loop einen Stack und eine Id. // Gibt der Loop einen Stack und eine Id.
PreemptiveLoopThread(int i) : id(i) {} PreemptiveLoopThread(int i, Semaphore* sem) : id(i), sem(sem) {}
// Zaehlt einen Zaehler hoch und gibt ihn auf dem Bildschirm aus. // Zaehlt einen Zaehler hoch und gibt ihn auf dem Bildschirm aus.
void run() override; void run() override;

View File

@ -1,12 +1,18 @@
#include "user/PreemptiveThreadDemo.h" #include "user/PreemptiveThreadDemo.h"
#include "lib/Semaphore.h"
void PreemptiveThreadDemo::run() { void PreemptiveThreadDemo::run() {
kout << "PreemptiveThreadDemo ====================================" << endl;
kout << "Initializing Semaphore" << endl;
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
// so stack allocated objects will be lost
kout << "Allocating LoopThread A" << endl; kout << "Allocating LoopThread A" << endl;
Thread* cntA = new PreemptiveLoopThread(0); Thread* cntA = new PreemptiveLoopThread(0, sem);
kout << "Allocating LoopThread B" << endl; kout << "Allocating LoopThread B" << endl;
Thread* cntB = new PreemptiveLoopThread(1); Thread* cntB = new PreemptiveLoopThread(1, sem);
kout << "Allocating LoopThread C" << endl; kout << "Allocating LoopThread C" << endl;
Thread* cntC = new PreemptiveLoopThread(2); Thread* cntC = new PreemptiveLoopThread(2, sem);
kout << "Adding threads to ready queue" << endl; kout << "Adding threads to ready queue" << endl;
scheduler.ready(cntA); scheduler.ready(cntA);