Universidad de Costa Rica

Escuela de Ciencias de la Computación e Informática

CI-0122 Sistemas operativos

Ejemplos

CI0122 / Ejemplos / pthreads / h2o


#include <stdio.h>
#include <pthread.h>
#include "synch.h"

SemPT::SemPT( int InitValue ) {

   sem = new sem_t;
   sem_init( sem, 0, InitValue );

}

SemPT::~SemPT() {

   sem_destroy( sem );
   delete sem;

}

int SemPT::Signal() {

   return sem_post( sem );

}

int SemPT::Wait() {

   return sem_wait( sem );

}

Mutex::Mutex() {

   mutex = new pthread_mutex_t;
   pthread_mutex_init( mutex, NULL );

}

Mutex::~Mutex() {

   pthread_mutex_destroy( mutex );
   delete mutex;

}

int Mutex::Lock() {

   return pthread_mutex_lock( mutex );

}

int Mutex::Unlock() {

   return pthread_mutex_unlock( mutex );

}


Lock::Lock() {

   lock = new pthread_mutex_t;
   pthread_mutex_init( lock, NULL );

}

Lock::~Lock() {

   pthread_mutex_destroy( lock );
   delete lock;

}

int Lock::Acquire() {

   return pthread_mutex_lock( lock );

}

int Lock::Release() {

   return pthread_mutex_unlock( lock );

}


Condition::Condition() {

   vc = new pthread_cond_t;
   pthread_cond_init( vc, NULL );

}

Condition::~Condition() {

   pthread_cond_destroy( vc );
   delete vc;

}

int Condition::Wait( Lock * conditionLock ) {

   return pthread_cond_wait( vc, conditionLock->getMutex() );

}

int Condition::Signal( Lock * conditionLock ) {

   return pthread_cond_signal( vc );

}

int Condition::Broadcast( Lock * conditionLock ) {

   return pthread_cond_broadcast( vc );

}