ext/asyncengine/libuv/src/unix/thread.c in asyncengine-0.0.1.testing1 vs ext/asyncengine/libuv/src/unix/thread.c in asyncengine-0.0.2.alpha1

- old
+ new

@@ -24,67 +24,75 @@ #include <pthread.h> #include <assert.h> #include <errno.h> -#ifdef NDEBUG -# define CHECK(r) ((void) (r)) -#else -# include <stdio.h> -# include <stdlib.h> -# define CHECK(r) \ - do { \ - int __r = (r); \ - if (__r) errno = __r, perror(#r), abort(); \ - } \ - while (0) -#endif - int uv_thread_join(uv_thread_t *tid) { if (pthread_join(*tid, NULL)) return -1; else return 0; } int uv_mutex_init(uv_mutex_t* mutex) { +#ifdef NDEBUG if (pthread_mutex_init(mutex, NULL)) return -1; else return 0; +#else + pthread_mutexattr_t attr; + int r; + + if (pthread_mutexattr_init(&attr)) + abort(); + + if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)) + abort(); + + r = pthread_mutex_init(mutex, &attr); + + if (pthread_mutexattr_destroy(&attr)) + abort(); + + return r ? -1 : 0; +#endif } void uv_mutex_destroy(uv_mutex_t* mutex) { - CHECK(pthread_mutex_destroy(mutex)); + if (pthread_mutex_destroy(mutex)) + abort(); } void uv_mutex_lock(uv_mutex_t* mutex) { - CHECK(pthread_mutex_lock(mutex)); + if (pthread_mutex_lock(mutex)) + abort(); } int uv_mutex_trylock(uv_mutex_t* mutex) { int r; r = pthread_mutex_trylock(mutex); if (r && r != EAGAIN) - CHECK(r); + abort(); if (r) return -1; else return 0; } void uv_mutex_unlock(uv_mutex_t* mutex) { - CHECK(pthread_mutex_unlock(mutex)); + if (pthread_mutex_unlock(mutex)) + abort(); } int uv_rwlock_init(uv_rwlock_t* rwlock) { if (pthread_rwlock_init(rwlock, NULL)) @@ -93,62 +101,111 @@ return 0; } void uv_rwlock_destroy(uv_rwlock_t* rwlock) { - CHECK(pthread_rwlock_destroy(rwlock)); + if (pthread_rwlock_destroy(rwlock)) + abort(); } void uv_rwlock_rdlock(uv_rwlock_t* rwlock) { - CHECK(pthread_rwlock_rdlock(rwlock)); + if (pthread_rwlock_rdlock(rwlock)) + abort(); } int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock) { int r; r = pthread_rwlock_tryrdlock(rwlock); if (r && r != EAGAIN) - CHECK(r); + abort(); if (r) return -1; else return 0; } void uv_rwlock_rdunlock(uv_rwlock_t* rwlock) { - CHECK(pthread_rwlock_unlock(rwlock)); + if (pthread_rwlock_unlock(rwlock)) + abort(); } void uv_rwlock_wrlock(uv_rwlock_t* rwlock) { - CHECK(pthread_rwlock_wrlock(rwlock)); + if (pthread_rwlock_wrlock(rwlock)) + abort(); } int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) { int r; r = pthread_rwlock_trywrlock(rwlock); if (r && r != EAGAIN) - CHECK(r); + abort(); if (r) return -1; else return 0; } void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) { - CHECK(pthread_rwlock_unlock(rwlock)); + if (pthread_rwlock_unlock(rwlock)) + abort(); } void uv_once(uv_once_t* guard, void (*callback)(void)) { - CHECK(pthread_once(guard, callback)); + if (pthread_once(guard, callback)) + abort(); +} + + +int uv_sem_init(uv_sem_t* sem, unsigned int value) { + return sem_init(sem, 0, value); +} + + +void uv_sem_destroy(uv_sem_t* sem) { + if (sem_destroy(sem)) + abort(); +} + + +void uv_sem_post(uv_sem_t* sem) { + if (sem_post(sem)) + abort(); +} + + +void uv_sem_wait(uv_sem_t* sem) { + int r; + + do + r = sem_wait(sem); + while (r == -1 && errno == EINTR); + + if (r) + abort(); +} + + +int uv_sem_trywait(uv_sem_t* sem) { + int r; + + do + r = sem_trywait(sem); + while (r == -1 && errno == EINTR); + + if (r && errno != EAGAIN) + abort(); + + return r; }