#include "Semaphore.hh" Semaphore::Semaphore() { mutex = new pthread_mutex_t[1]; condition = new pthread_cond_t[1]; pthread_mutex_init( mutex, NULL ); pthread_cond_init( condition, NULL ); this->value = 1; } Semaphore::Semaphore( int value ) { mutex = new pthread_mutex_t[1]; condition = new pthread_cond_t[1]; pthread_mutex_init( mutex, NULL ); pthread_cond_init( condition, NULL ); this->value = value; } Semaphore::~Semaphore() { delete mutex; delete condition; } void Semaphore::up() { pthread_mutex_lock( mutex ); value++; pthread_cond_signal( condition ); pthread_mutex_unlock( mutex ); } void Semaphore::bdown() { pthread_mutex_lock( mutex ); value--; if( value < 0 ) { pthread_cond_wait( condition, mutex ); } pthread_mutex_unlock( mutex ); } void Semaphore::nbdown() { pthread_mutex_lock( mutex ); value--; pthread_mutex_unlock( mutex ); } void Semaphore::set( int value ) { pthread_mutex_lock( mutex ); this->value = value; pthread_mutex_unlock( mutex ); } int Semaphore::get() { int rv; pthread_mutex_lock( mutex ); rv = this->value; pthread_mutex_unlock( mutex ); return rv; }