diff options
-rw-r--r-- | include/zix/thread.h | 11 | ||||
-rw-r--r-- | src/sem.c | 19 | ||||
-rw-r--r-- | src/status.c | 8 | ||||
-rw-r--r-- | test/test_status.c | 6 |
4 files changed, 21 insertions, 23 deletions
diff --git a/include/zix/thread.h b/include/zix/thread.h index 9c213de..bad939f 100644 --- a/include/zix/thread.h +++ b/include/zix/thread.h @@ -12,6 +12,7 @@ # include <pthread.h> #endif +#include <errno.h> #include <stddef.h> #ifdef __cplusplus @@ -92,10 +93,10 @@ zix_thread_join(ZixThread thread) #else /* !defined(_WIN32) */ static inline ZixStatus -zix_thread_create(ZixThread* thread, - size_t stack_size, - void* (*function)(void*), - void* arg) +zix_thread_create(ZixThread* thread, + size_t stack_size, + ZixThreadFunc function, + void* arg) { pthread_attr_t attr; pthread_attr_init(&attr); @@ -104,7 +105,7 @@ zix_thread_create(ZixThread* thread, const int ret = pthread_create(thread, NULL, function, arg); pthread_attr_destroy(&attr); - return zix_errno_status(ret); + return ret == EAGAIN ? ZIX_STATUS_NO_MEM : zix_errno_status(ret); } static inline ZixStatus @@ -186,20 +186,17 @@ zix_sem_timed_wait(ZixSem* sem, struct timespec ts = {0, 0}; - if (clock_gettime(CLOCK_REALTIME, &ts)) { - return ZIX_STATUS_ERROR; - } - - ts.tv_sec += (time_t)seconds; - ts.tv_nsec += (long)nanoseconds; - int r = 0; - while ((r = sem_timedwait(&sem->sem, &ts)) && errno == EINTR) { - // Interrupted, try again + if (!(r = clock_gettime(CLOCK_REALTIME, &ts))) { + ts.tv_sec += (time_t)seconds; + ts.tv_nsec += (long)nanoseconds; + + while ((r = sem_timedwait(&sem->sem, &ts)) && errno == EINTR) { + // Interrupted, try again + } } - return r ? (errno == ETIMEDOUT ? ZIX_STATUS_TIMEOUT : zix_errno_status(errno)) - : ZIX_STATUS_SUCCESS; + return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS; # endif } diff --git a/src/status.c b/src/status.c index a0bb17e..ed48952 100644 --- a/src/status.c +++ b/src/status.c @@ -41,10 +41,6 @@ zix_errno_status(const int e) switch (e) { case 0: return ZIX_STATUS_SUCCESS; -#ifdef EAGAIN - case EAGAIN: - return ZIX_STATUS_NO_MEM; -#endif #ifdef EEXIST case EEXIST: return ZIX_STATUS_EXISTS; @@ -57,6 +53,10 @@ zix_errno_status(const int e) case EPERM: return ZIX_STATUS_BAD_PERMS; #endif +#ifdef ETIMEDOUT + case ETIMEDOUT: + return ZIX_STATUS_TIMEOUT; +#endif default: break; } diff --git a/test/test_status.c b/test/test_status.c index 6405ec8..298b520 100644 --- a/test/test_status.c +++ b/test/test_status.c @@ -17,9 +17,6 @@ test_errno_status(void) assert(zix_errno_status(0) == ZIX_STATUS_SUCCESS); assert(zix_errno_status(INT_MAX) == ZIX_STATUS_ERROR); -#ifdef EAGAIN - assert(zix_errno_status(EAGAIN) == ZIX_STATUS_NO_MEM); -#endif #ifdef EEXIST assert(zix_errno_status(EEXIST) == ZIX_STATUS_EXISTS); #endif @@ -29,6 +26,9 @@ test_errno_status(void) #ifdef EPERM assert(zix_errno_status(EPERM) == ZIX_STATUS_BAD_PERMS); #endif +#ifdef ETIMEDOUT + assert(zix_errno_status(ETIMEDOUT) == ZIX_STATUS_TIMEOUT); +#endif } static void |