switch semaphore to arraylist because queue buggy
This commit is contained in:
@ -11,7 +11,7 @@ void Semaphore::p() {
|
|||||||
this->lock.release();
|
this->lock.release();
|
||||||
} else {
|
} else {
|
||||||
// Block and manage thread in semaphore queue until it's woken up by v() again
|
// 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();
|
this->lock.release();
|
||||||
scheduler.block(); // Moves to next thread
|
scheduler.block(); // Moves to next thread
|
||||||
}
|
}
|
||||||
@ -20,9 +20,9 @@ void Semaphore::p() {
|
|||||||
void Semaphore::v() {
|
void Semaphore::v() {
|
||||||
this->lock.acquire();
|
this->lock.acquire();
|
||||||
|
|
||||||
if (!this->waitQueue.isEmpty()) {
|
if (!this->waitQueue.empty()) {
|
||||||
// Semaphore stays busy and unblocks next thread to work in critical section
|
// 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();
|
this->lock.release();
|
||||||
scheduler.deblock(next);
|
scheduler.deblock(next);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -11,8 +11,9 @@
|
|||||||
#ifndef __Semaphore_include__
|
#ifndef __Semaphore_include__
|
||||||
#define __Semaphore_include__
|
#define __Semaphore_include__
|
||||||
|
|
||||||
#include "lib/Queue.h"
|
#include "kernel/threads/Thread.h"
|
||||||
#include "lib/SpinLock.h"
|
#include "lib/SpinLock.h"
|
||||||
|
#include "user/lib/ArrayList.h"
|
||||||
|
|
||||||
class Semaphore {
|
class Semaphore {
|
||||||
|
|
||||||
@ -20,14 +21,16 @@ private:
|
|||||||
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
|
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
|
||||||
|
|
||||||
// Queue fuer wartende Threads.
|
// Queue fuer wartende Threads.
|
||||||
Queue waitQueue;
|
ArrayList<Thread*> waitQueue;
|
||||||
SpinLock lock;
|
SpinLock lock;
|
||||||
|
|
||||||
int counter;
|
int counter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Konstruktor: Initialisieren des Semaphorzaehlers
|
// Konstruktor: Initialisieren des Semaphorzaehlers
|
||||||
Semaphore(int c) : counter(c) {}
|
Semaphore(int c) : counter(c) {
|
||||||
|
waitQueue.init();
|
||||||
|
}
|
||||||
|
|
||||||
// 'Passieren': Warten auf das Freiwerden eines kritischen Abschnitts.
|
// 'Passieren': Warten auf das Freiwerden eines kritischen Abschnitts.
|
||||||
void p();
|
void p();
|
||||||
|
|||||||
Reference in New Issue
Block a user