summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-09-01 23:14:03 -0400
committerDavid Robillard <d@drobilla.net>2022-09-01 23:59:38 -0400
commit7cd6a5437c263a4e2b64bafdf780d60ce51f941f (patch)
treea87d20b772ddc0e864cc2768a999af2d8f92cf13
parent16a8597f4d52de948291825698b7d2458998a510 (diff)
downloadzix-7cd6a5437c263a4e2b64bafdf780d60ce51f941f.tar.gz
zix-7cd6a5437c263a4e2b64bafdf780d60ce51f941f.tar.bz2
zix-7cd6a5437c263a4e2b64bafdf780d60ce51f941f.zip
Simplify thread and semaphore status codes
-rw-r--r--include/zix/common.h1
-rw-r--r--include/zix/sem.h5
-rw-r--r--include/zix/thread.h4
-rw-r--r--src/sem.c7
-rw-r--r--src/status.c6
-rw-r--r--test/test_sem.c4
-rw-r--r--test/test_status.c2
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"));
}