changes.
[IRC.git] / Robust / src / Runtime / psemaphore.c
1 #include "psemaphore.h"
2
3
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; }
7   sem->signaled = 0;
8   return 0;
9 }
10
11
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; }
16   }
17   if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; }
18   return 0;
19 }
20
21
22 int psem_give( psemaphore* sem ) {
23   if( pthread_mutex_lock  ( &(sem->lock) ) == -1 ) { return -1; }
24   sem->signaled = 1;
25   if( pthread_cond_signal ( &(sem->cond) ) == -1 ) { return -1; }
26   if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; }
27
28 }