From 171aa2a60a8deeaf7b7692a0ecb6de56df1607f8 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 19 Aug 2022 14:05:26 -0400 Subject: Simplify errno handling --- include/zix/thread.h | 11 ++++++----- src/sem.c | 19 ++++++++----------- src/status.c | 8 ++++---- 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 #endif +#include #include #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 diff --git a/src/sem.c b/src/sem.c index 066c592..a577b08 100644 --- a/src/sem.c +++ b/src/sem.c @@ -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; @@ -56,6 +52,10 @@ zix_errno_status(const int e) #ifdef EPERM 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 -- cgit v1.2.1