summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-08-19 14:05:26 -0400
committerDavid Robillard <d@drobilla.net>2022-08-19 14:21:32 -0400
commit171aa2a60a8deeaf7b7692a0ecb6de56df1607f8 (patch)
tree9656d3faa9f982c688188911b658962bf8d618c0
parent1069c63c6ca2713cce2d6153acc1a1ef9f2b7f8f (diff)
downloadzix-171aa2a60a8deeaf7b7692a0ecb6de56df1607f8.tar.gz
zix-171aa2a60a8deeaf7b7692a0ecb6de56df1607f8.tar.bz2
zix-171aa2a60a8deeaf7b7692a0ecb6de56df1607f8.zip
Simplify errno handling
-rw-r--r--include/zix/thread.h11
-rw-r--r--src/sem.c19
-rw-r--r--src/status.c8
-rw-r--r--test/test_status.c6
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
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;
@@ -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