From 7cd6a5437c263a4e2b64bafdf780d60ce51f941f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 1 Sep 2022 23:14:03 -0400 Subject: Simplify thread and semaphore status codes --- include/zix/common.h | 1 + include/zix/sem.h | 5 +++-- include/zix/thread.h | 4 ++-- src/sem.c | 7 +++---- src/status.c | 6 ++++++ test/test_sem.c | 4 ++-- test/test_status.c | 2 +- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/zix/common.h b/include/zix/common.h index 0cefd55..a097edd 100644 --- a/include/zix/common.h +++ b/include/zix/common.h @@ -29,6 +29,7 @@ typedef enum { ZIX_STATUS_TIMEOUT, ZIX_STATUS_OVERFLOW, ZIX_STATUS_NOT_SUPPORTED, + ZIX_STATUS_UNAVAILABLE, } ZixStatus; /// Return a string describing a status code diff --git a/include/zix/sem.h b/include/zix/sem.h index 4c82f17..41b9860 100644 --- a/include/zix/sem.h +++ b/include/zix/sem.h @@ -97,8 +97,9 @@ zix_sem_wait(ZixSem* ZIX_NONNULL sem); /** Non-blocking version of wait(). - @return #ZIX_STATUS_SUCCESS if `sem` was decremented, #ZIX_STATUS_TIMEOUT if - it was already zero, or #ZIX_STATUS_BAD_ARG if `sem` is invalid. + @return #ZIX_STATUS_SUCCESS if `sem` was decremented, + #ZIX_STATUS_UNAVAILABLE if it was already zero, or #ZIX_STATUS_BAD_ARG if + `sem` is invalid. */ ZIX_API ZixStatus diff --git a/include/zix/thread.h b/include/zix/thread.h index bad939f..482315a 100644 --- a/include/zix/thread.h +++ b/include/zix/thread.h @@ -103,9 +103,9 @@ zix_thread_create(ZixThread* thread, pthread_attr_setstacksize(&attr, stack_size); const int ret = pthread_create(thread, NULL, function, arg); - pthread_attr_destroy(&attr); - return ret == EAGAIN ? ZIX_STATUS_NO_MEM : zix_errno_status(ret); + pthread_attr_destroy(&attr); + return zix_errno_status(ret); } static inline ZixStatus diff --git a/src/sem.c b/src/sem.c index a577b08..325eaa4 100644 --- a/src/sem.c +++ b/src/sem.c @@ -61,7 +61,7 @@ zix_sem_try_wait(ZixSem* sem) const kern_return_t r = semaphore_timedwait(sem->sem, zero); return (r == KERN_SUCCESS) ? ZIX_STATUS_SUCCESS - : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_TIMEOUT + : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_UNAVAILABLE : ZIX_STATUS_ERROR; } @@ -114,7 +114,7 @@ zix_sem_try_wait(ZixSem* sem) const DWORD r = WaitForSingleObject(sem->sem, 0); return (r == WAIT_OBJECT_0) ? ZIX_STATUS_SUCCESS - : (r == WAIT_TIMEOUT) ? ZIX_STATUS_TIMEOUT + : (r == WAIT_TIMEOUT) ? ZIX_STATUS_UNAVAILABLE : ZIX_STATUS_ERROR; } @@ -171,8 +171,7 @@ zix_sem_try_wait(ZixSem* sem) // Interrupted, try again } - return r ? (errno == EAGAIN ? ZIX_STATUS_TIMEOUT : zix_errno_status(errno)) - : ZIX_STATUS_SUCCESS; + return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS; } ZixStatus diff --git a/src/status.c b/src/status.c index ed48952..380d370 100644 --- a/src/status.c +++ b/src/status.c @@ -31,6 +31,8 @@ zix_strerror(const ZixStatus status) return "Overflow"; case ZIX_STATUS_NOT_SUPPORTED: return "Not supported"; + case ZIX_STATUS_UNAVAILABLE: + return "Resource unavailable"; } return "Unknown error"; } @@ -41,6 +43,10 @@ zix_errno_status(const int e) switch (e) { case 0: return ZIX_STATUS_SUCCESS; +#ifdef EAGAIN + case EAGAIN: + return ZIX_STATUS_UNAVAILABLE; +#endif #ifdef EEXIST case EEXIST: return ZIX_STATUS_EXISTS; diff --git a/test/test_sem.c b/test/test_sem.c index f2b2794..36d01c5 100644 --- a/test/test_sem.c +++ b/test/test_sem.c @@ -45,10 +45,10 @@ static void test_try_wait(void) { assert(!zix_sem_init(&sem, 0)); - assert(zix_sem_try_wait(&sem) == ZIX_STATUS_TIMEOUT); + assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE); assert(!zix_sem_post(&sem)); assert(!zix_sem_try_wait(&sem)); - assert(zix_sem_try_wait(&sem) == ZIX_STATUS_TIMEOUT); + assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE); assert(!zix_sem_destroy(&sem)); } diff --git a/test/test_status.c b/test/test_status.c index 298b520..1a76ac2 100644 --- a/test/test_status.c +++ b/test/test_status.c @@ -37,7 +37,7 @@ test_strerror(void) const char* msg = zix_strerror(ZIX_STATUS_SUCCESS); assert(!strcmp(msg, "Success")); - for (int i = ZIX_STATUS_ERROR; i <= ZIX_STATUS_NOT_SUPPORTED; ++i) { + for (int i = ZIX_STATUS_ERROR; i <= ZIX_STATUS_UNAVAILABLE; ++i) { msg = zix_strerror((ZixStatus)i); assert(strcmp(msg, "Success")); } -- cgit v1.2.1