diff --git a/c_os/user/PreemptiveLoopThread.cc b/c_os/user/PreemptiveLoopThread.cc index 5dd87f8..ebb2646 100644 --- a/c_os/user/PreemptiveLoopThread.cc +++ b/c_os/user/PreemptiveLoopThread.cc @@ -6,12 +6,12 @@ void PreemptiveLoopThread::run() { int cnt = 0; while (true) { - // Basic synchronization by disabling PIT interrupts - cpu.disable_int(); + // Basic synchronization by semaphore + sem->p(); kout.setpos(55, this->id); kout << this->id << ": " << dec << cnt++ << endl; - cpu.enable_int(); + sem->v(); } } diff --git a/c_os/user/PreemptiveLoopThread.h b/c_os/user/PreemptiveLoopThread.h index c73e7f0..0816c5f 100644 --- a/c_os/user/PreemptiveLoopThread.h +++ b/c_os/user/PreemptiveLoopThread.h @@ -3,17 +3,19 @@ #include "kernel/Globals.h" #include "kernel/threads/Thread.h" +#include "lib/Semaphore.h" class PreemptiveLoopThread : public Thread { private: int id; + Semaphore* sem; PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren public: // 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. void run() override; diff --git a/c_os/user/PreemptiveThreadDemo.cc b/c_os/user/PreemptiveThreadDemo.cc index c29ab80..1a860cd 100644 --- a/c_os/user/PreemptiveThreadDemo.cc +++ b/c_os/user/PreemptiveThreadDemo.cc @@ -1,12 +1,18 @@ #include "user/PreemptiveThreadDemo.h" +#include "lib/Semaphore.h" 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; - Thread* cntA = new PreemptiveLoopThread(0); + Thread* cntA = new PreemptiveLoopThread(0, sem); kout << "Allocating LoopThread B" << endl; - Thread* cntB = new PreemptiveLoopThread(1); + Thread* cntB = new PreemptiveLoopThread(1, sem); kout << "Allocating LoopThread C" << endl; - Thread* cntC = new PreemptiveLoopThread(2); + Thread* cntC = new PreemptiveLoopThread(2, sem); kout << "Adding threads to ready queue" << endl; scheduler.ready(cntA);