From bf3d8074646ed3b70606efe717fcb8323a291905 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Sat, 16 Jul 2022 03:30:41 +0200 Subject: [PATCH] switch semaphore to arraylist because queue buggy --- c_os/lib/Semaphore.cc | 6 +++--- c_os/lib/Semaphore.h | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/c_os/lib/Semaphore.cc b/c_os/lib/Semaphore.cc index bc4a556..ad67cbf 100644 --- a/c_os/lib/Semaphore.cc +++ b/c_os/lib/Semaphore.cc @@ -11,7 +11,7 @@ void Semaphore::p() { this->lock.release(); } else { // Block and manage thread in semaphore queue until it's woken up by v() again - this->waitQueue.enqueue(scheduler.get_active()); + this->waitQueue.insert(scheduler.get_active()); this->lock.release(); scheduler.block(); // Moves to next thread } @@ -20,9 +20,9 @@ void Semaphore::p() { void Semaphore::v() { this->lock.acquire(); - if (!this->waitQueue.isEmpty()) { + if (!this->waitQueue.empty()) { // Semaphore stays busy and unblocks next thread to work in critical section - Thread* next = (Thread*)this->waitQueue.dequeue(); + Thread* next = (Thread*)this->waitQueue.remove_first(); this->lock.release(); scheduler.deblock(next); } else { diff --git a/c_os/lib/Semaphore.h b/c_os/lib/Semaphore.h index 2fb682e..5e5b9e6 100755 --- a/c_os/lib/Semaphore.h +++ b/c_os/lib/Semaphore.h @@ -11,8 +11,9 @@ #ifndef __Semaphore_include__ #define __Semaphore_include__ -#include "lib/Queue.h" +#include "kernel/threads/Thread.h" #include "lib/SpinLock.h" +#include "user/lib/ArrayList.h" class Semaphore { @@ -20,14 +21,16 @@ private: Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren // Queue fuer wartende Threads. - Queue waitQueue; + ArrayList waitQueue; SpinLock lock; int counter; public: // Konstruktor: Initialisieren des Semaphorzaehlers - Semaphore(int c) : counter(c) {} + Semaphore(int c) : counter(c) { + waitQueue.init(); + } // 'Passieren': Warten auf das Freiwerden eines kritischen Abschnitts. void p();