/* Esta clase encapsula las funciones para la utilizacion de variables de condición
*
* Autor: Programacion Paralela y Concurrente
* Fecha: 2020/Set/03
*
**/
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include "Condition.h"
/*
* Creates a new condition variable
*
* Uses an internal structure to make workers wait for resources
*
**/
Condition::Condition() {
this->waitingWorkers = 0;
}
/**
* Destroys our condition variable
**/
Condition::~Condition() {
}
/**
* Wait for the condition controlled by our variable
*
**/
void Condition::Wait( Lock & affectedLock ) {
this->waitingWorkers++;
//printf( "process %d is waiting on lock 0x%x waiting %d\n", getpid(), this, this->waitingWorkers );
affectedLock.Release();
this->internalWaitMechanism.Acquire();
affectedLock.Acquire();
}
/**
* Notify one worker from the queue, if empty has no effect
*
**/
void Condition::NotifyOne() {
//printf( "process %d Condition 0x%x waitingWorkers %d\n", getpid(), this, this->waitingWorkers );
if ( this->waitingWorkers > 0 ) {
this->waitingWorkers--; // One
this->internalWaitMechanism.Release();
//printf( "Releasing lock 0x%x\n", this );
}
}
/**
* Same method as notify one, declared for compatibility with many examples
*
**/
void Condition::Signal() {
this->NotifyOne();
}
/**
* Signal all workers from the queue, if empty has no effect
*
**/
void Condition::NotifyAll() {
while ( this->waitingWorkers > 0 ) {
this->waitingWorkers--;
this->internalWaitMechanism.Release();
}
}