00001 00030 #include <pthread.h> 00031 #include <sys/time.h> 00032 #include <errno.h> 00033 #include "tsemaphore.h" 00034 #include "user_debug_levels.h" 00035 00042 void tsem_init(tsem_t* tsem, unsigned int val) { 00043 pthread_cond_init(&tsem->condition, NULL); 00044 pthread_mutex_init(&tsem->mutex, NULL); 00045 tsem->semval = val; 00046 } 00047 00052 void tsem_deinit(tsem_t* tsem) { 00053 pthread_cond_destroy(&tsem->condition); 00054 pthread_mutex_destroy(&tsem->mutex); 00055 } 00056 00062 void tsem_down(tsem_t* tsem) { 00063 pthread_mutex_lock(&tsem->mutex); 00064 while (tsem->semval == 0) { 00065 pthread_cond_wait(&tsem->condition, &tsem->mutex); 00066 } 00067 tsem->semval--; 00068 pthread_mutex_unlock(&tsem->mutex); 00069 } 00070 00075 void tsem_up(tsem_t* tsem) { 00076 pthread_mutex_lock(&tsem->mutex); 00077 tsem->semval++; 00078 pthread_cond_signal(&tsem->condition); 00079 pthread_mutex_unlock(&tsem->mutex); 00080 } 00081 00086 void tsem_reset(tsem_t* tsem) { 00087 pthread_mutex_lock(&tsem->mutex); 00088 tsem->semval=0; 00089 pthread_mutex_unlock(&tsem->mutex); 00090 } 00091 00096 void tsem_wait(tsem_t* tsem) { 00097 pthread_mutex_lock(&tsem->mutex); 00098 pthread_cond_wait(&tsem->condition, &tsem->mutex); 00099 pthread_mutex_unlock(&tsem->mutex); 00100 } 00101 00106 void tsem_signal(tsem_t* tsem) { 00107 pthread_mutex_lock(&tsem->mutex); 00108 pthread_cond_signal(&tsem->condition); 00109 pthread_mutex_unlock(&tsem->mutex); 00110 } 00111