1 #include "psemaphore.h"
4 int psem_init( psemaphore* sem ) {
5 if( pthread_mutex_init( &(sem->lock), NULL ) == -1 ) { return -1; }
6 if( pthread_cond_init ( &(sem->cond), NULL ) == -1 ) { return -1; }
12 int psem_take( psemaphore* sem ) {
13 if( pthread_mutex_lock ( &(sem->lock) ) == -1 ) { return -1; }
14 while( !sem->signaled ) {
15 if( pthread_cond_wait ( &(sem->cond), &(sem->lock) ) == -1 ) { return -1; }
17 if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; }
22 int psem_give( psemaphore* sem ) {
23 if( pthread_mutex_lock ( &(sem->lock) ) == -1 ) { return -1; }
25 if( pthread_cond_signal ( &(sem->cond) ) == -1 ) { return -1; }
26 if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; }