summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/darwin/sem_darwin.c68
-rw-r--r--src/posix/sem_posix.c89
-rw-r--r--src/posix/thread_posix.c (renamed from src/thread.c)32
-rw-r--r--src/sem.c212
-rw-r--r--src/win32/sem_win32.c63
-rw-r--r--src/win32/thread_win32.c28
6 files changed, 250 insertions, 242 deletions
diff --git a/src/darwin/sem_darwin.c b/src/darwin/sem_darwin.c
new file mode 100644
index 0000000..0ace564
--- /dev/null
+++ b/src/darwin/sem_darwin.c
@@ -0,0 +1,68 @@
+// Copyright 2012-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "zix/sem.h"
+
+#include "zix/status.h"
+
+#include <mach/mach.h>
+
+#include <stdint.h>
+#include <time.h>
+
+ZixStatus
+zix_sem_init(ZixSem* sem, unsigned val)
+{
+ return semaphore_create(
+ mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, (int)val)
+ ? ZIX_STATUS_ERROR
+ : ZIX_STATUS_SUCCESS;
+}
+
+ZixStatus
+zix_sem_destroy(ZixSem* sem)
+{
+ return semaphore_destroy(mach_task_self(), sem->sem) ? ZIX_STATUS_ERROR
+ : ZIX_STATUS_SUCCESS;
+}
+
+ZixStatus
+zix_sem_post(ZixSem* sem)
+{
+ return semaphore_signal(sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+}
+
+ZixStatus
+zix_sem_wait(ZixSem* sem)
+{
+ kern_return_t r = 0;
+ while ((r = semaphore_wait(sem->sem)) && r == KERN_ABORTED) {
+ // Interrupted, try again
+ }
+
+ return r ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+}
+
+ZixStatus
+zix_sem_try_wait(ZixSem* sem)
+{
+ const mach_timespec_t zero = {0, 0};
+ const kern_return_t r = semaphore_timedwait(sem->sem, zero);
+
+ return (r == KERN_SUCCESS) ? ZIX_STATUS_SUCCESS
+ : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_UNAVAILABLE
+ : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_sem_timed_wait(ZixSem* sem,
+ const uint32_t seconds,
+ const uint32_t nanoseconds)
+{
+ const mach_timespec_t interval = {seconds, (clock_res_t)nanoseconds};
+ const kern_return_t r = semaphore_timedwait(sem->sem, interval);
+
+ return (r == KERN_SUCCESS) ? ZIX_STATUS_SUCCESS
+ : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_TIMEOUT
+ : ZIX_STATUS_ERROR;
+}
diff --git a/src/posix/sem_posix.c b/src/posix/sem_posix.c
new file mode 100644
index 0000000..452830d
--- /dev/null
+++ b/src/posix/sem_posix.c
@@ -0,0 +1,89 @@
+// Copyright 2012-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "zix/sem.h"
+
+#include "../errno_status.h"
+#include "../zix_config.h"
+
+#include "zix/status.h"
+
+#include <semaphore.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <time.h>
+
+ZixStatus
+zix_sem_init(ZixSem* sem, unsigned initial)
+{
+ return zix_errno_status_if(sem_init(&sem->sem, 0, initial));
+}
+
+ZixStatus
+zix_sem_destroy(ZixSem* sem)
+{
+ return zix_errno_status_if(sem_destroy(&sem->sem));
+}
+
+ZixStatus
+zix_sem_post(ZixSem* sem)
+{
+ return zix_errno_status_if(sem_post(&sem->sem));
+}
+
+ZixStatus
+zix_sem_wait(ZixSem* sem)
+{
+ int r = 0;
+ while ((r = sem_wait(&sem->sem)) && errno == EINTR) {
+ // Interrupted, try again
+ }
+
+ return zix_errno_status_if(r);
+}
+
+ZixStatus
+zix_sem_try_wait(ZixSem* sem)
+{
+ int r = 0;
+ while ((r = sem_trywait(&sem->sem)) && errno == EINTR) {
+ // Interrupted, try again
+ }
+
+ return zix_errno_status_if(r);
+}
+
+ZixStatus
+zix_sem_timed_wait(ZixSem* sem,
+ const uint32_t seconds,
+ const uint32_t nanoseconds)
+{
+#if !USE_CLOCK_GETTIME || !USE_SEM_TIMEDWAIT
+
+ return ZIX_STATUS_NOT_SUPPORTED;
+
+#else
+# define NS_PER_SECOND 1000000000L
+
+ struct timespec ts = {0, 0};
+
+ int r = 0;
+ if (!(r = clock_gettime(CLOCK_REALTIME, &ts))) {
+ ts.tv_sec += (time_t)seconds;
+ ts.tv_nsec += (long)nanoseconds;
+ if (ts.tv_nsec >= NS_PER_SECOND) {
+ ts.tv_nsec -= NS_PER_SECOND;
+ ts.tv_sec++;
+ }
+
+ while ((r = sem_timedwait(&sem->sem, &ts)) && errno == EINTR) {
+ // Interrupted, try again
+ }
+ }
+
+ return zix_errno_status_if(r);
+
+# undef NS_PER_SECOND
+#endif
+}
diff --git a/src/thread.c b/src/posix/thread_posix.c
index 9b8af20..2073448 100644
--- a/src/thread.c
+++ b/src/posix/thread_posix.c
@@ -3,40 +3,14 @@
#include "zix/thread.h"
-#include "errno_status.h"
+#include "../errno_status.h"
#include "zix/status.h"
-#ifdef _WIN32
-# include <windows.h>
-#else
-# include <pthread.h>
-#endif
+#include <pthread.h>
#include <stddef.h>
-#ifdef _WIN32
-
-ZixStatus
-zix_thread_create(ZixThread* thread,
- size_t stack_size,
- ZixThreadFunc function,
- void* arg)
-{
- *thread = CreateThread(NULL, stack_size, function, arg, 0, NULL);
- return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_thread_join(ZixThread thread)
-{
- return (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0)
- ? ZIX_STATUS_SUCCESS
- : ZIX_STATUS_ERROR;
-}
-
-#else // !defined(_WIN32)
-
ZixStatus
zix_thread_create(ZixThread* thread,
size_t stack_size,
@@ -58,5 +32,3 @@ zix_thread_join(ZixThread thread)
{
return pthread_join(thread, NULL) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}
-
-#endif
diff --git a/src/sem.c b/src/sem.c
deleted file mode 100644
index 4b87297..0000000
--- a/src/sem.c
+++ /dev/null
@@ -1,212 +0,0 @@
-// Copyright 2012-2022 David Robillard <d@drobilla.net>
-// SPDX-License-Identifier: ISC
-
-#include "zix/sem.h"
-
-#include "errno_status.h"
-#include "zix_config.h"
-
-#include "zix/status.h"
-
-#ifdef __APPLE__
-# include <mach/mach.h>
-#elif defined(_WIN32)
-# include <limits.h>
-# include <windows.h>
-#else
-# include <errno.h>
-# include <semaphore.h>
-#endif
-
-#include <stdint.h>
-#include <time.h>
-
-#ifdef __APPLE__
-
-ZixStatus
-zix_sem_init(ZixSem* sem, unsigned val)
-{
- return semaphore_create(
- mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, (int)val)
- ? ZIX_STATUS_ERROR
- : ZIX_STATUS_SUCCESS;
-}
-
-ZixStatus
-zix_sem_destroy(ZixSem* sem)
-{
- return semaphore_destroy(mach_task_self(), sem->sem) ? ZIX_STATUS_ERROR
- : ZIX_STATUS_SUCCESS;
-}
-
-ZixStatus
-zix_sem_post(ZixSem* sem)
-{
- return semaphore_signal(sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
-}
-
-ZixStatus
-zix_sem_wait(ZixSem* sem)
-{
- kern_return_t r = 0;
- while ((r = semaphore_wait(sem->sem)) && r == KERN_ABORTED) {
- // Interrupted, try again
- }
-
- return r ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
-}
-
-ZixStatus
-zix_sem_try_wait(ZixSem* sem)
-{
- const mach_timespec_t zero = {0, 0};
- const kern_return_t r = semaphore_timedwait(sem->sem, zero);
-
- return (r == KERN_SUCCESS) ? ZIX_STATUS_SUCCESS
- : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_UNAVAILABLE
- : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_sem_timed_wait(ZixSem* sem,
- const uint32_t seconds,
- const uint32_t nanoseconds)
-{
- const mach_timespec_t interval = {seconds, (clock_res_t)nanoseconds};
- const kern_return_t r = semaphore_timedwait(sem->sem, interval);
-
- return (r == KERN_SUCCESS) ? ZIX_STATUS_SUCCESS
- : (r == KERN_OPERATION_TIMED_OUT) ? ZIX_STATUS_TIMEOUT
- : ZIX_STATUS_ERROR;
-}
-
-#elif defined(_WIN32)
-
-ZixStatus
-zix_sem_init(ZixSem* sem, unsigned initial)
-{
- sem->sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL);
- return sem->sem ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_sem_destroy(ZixSem* sem)
-{
- return CloseHandle(sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_sem_post(ZixSem* sem)
-{
- return ReleaseSemaphore(sem->sem, 1, NULL) ? ZIX_STATUS_SUCCESS
- : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_sem_wait(ZixSem* sem)
-{
- return WaitForSingleObject(sem->sem, INFINITE) == WAIT_OBJECT_0
- ? ZIX_STATUS_SUCCESS
- : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-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_UNAVAILABLE
- : ZIX_STATUS_ERROR;
-}
-
-ZixStatus
-zix_sem_timed_wait(ZixSem* sem,
- const uint32_t seconds,
- const uint32_t nanoseconds)
-{
- const uint32_t milliseconds = seconds * 1000U + nanoseconds / 1000000U;
- const DWORD r = WaitForSingleObject(sem->sem, milliseconds);
-
- return (r == WAIT_OBJECT_0) ? ZIX_STATUS_SUCCESS
- : (r == WAIT_TIMEOUT) ? ZIX_STATUS_TIMEOUT
- : ZIX_STATUS_ERROR;
-}
-
-#else /* !defined(__APPLE__) && !defined(_WIN32) */
-
-ZixStatus
-zix_sem_init(ZixSem* sem, unsigned initial)
-{
- return zix_errno_status_if(sem_init(&sem->sem, 0, initial));
-}
-
-ZixStatus
-zix_sem_destroy(ZixSem* sem)
-{
- return zix_errno_status_if(sem_destroy(&sem->sem));
-}
-
-ZixStatus
-zix_sem_post(ZixSem* sem)
-{
- return zix_errno_status_if(sem_post(&sem->sem));
-}
-
-ZixStatus
-zix_sem_wait(ZixSem* sem)
-{
- int r = 0;
- while ((r = sem_wait(&sem->sem)) && errno == EINTR) {
- // Interrupted, try again
- }
-
- return zix_errno_status_if(r);
-}
-
-ZixStatus
-zix_sem_try_wait(ZixSem* sem)
-{
- int r = 0;
- while ((r = sem_trywait(&sem->sem)) && errno == EINTR) {
- // Interrupted, try again
- }
-
- return zix_errno_status_if(r);
-}
-
-ZixStatus
-zix_sem_timed_wait(ZixSem* sem,
- const uint32_t seconds,
- const uint32_t nanoseconds)
-{
-# define NS_PER_SECOND 1000000000L
-
-# if !USE_CLOCK_GETTIME || !USE_SEM_TIMEDWAIT
- return ZIX_STATUS_NOT_SUPPORTED;
-
-# else
-
- struct timespec ts = {0, 0};
-
- int r = 0;
- if (!(r = clock_gettime(CLOCK_REALTIME, &ts))) {
- ts.tv_sec += (time_t)seconds;
- ts.tv_nsec += (long)nanoseconds;
- if (ts.tv_nsec >= NS_PER_SECOND) {
- ts.tv_nsec -= NS_PER_SECOND;
- ts.tv_sec++;
- }
-
- while ((r = sem_timedwait(&sem->sem, &ts)) && errno == EINTR) {
- // Interrupted, try again
- }
- }
-
- return zix_errno_status_if(r);
-# endif
-
-# undef NS_PER_SECOND
-}
-
-#endif
diff --git a/src/win32/sem_win32.c b/src/win32/sem_win32.c
new file mode 100644
index 0000000..979390e
--- /dev/null
+++ b/src/win32/sem_win32.c
@@ -0,0 +1,63 @@
+// Copyright 2012-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "zix/sem.h"
+
+#include "zix/status.h"
+
+#include <windows.h>
+
+#include <limits.h>
+#include <stdint.h>
+#include <time.h>
+
+ZixStatus
+zix_sem_init(ZixSem* sem, unsigned initial)
+{
+ sem->sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL);
+ return sem->sem ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_sem_destroy(ZixSem* sem)
+{
+ return CloseHandle(sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_sem_post(ZixSem* sem)
+{
+ return ReleaseSemaphore(sem->sem, 1, NULL) ? ZIX_STATUS_SUCCESS
+ : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_sem_wait(ZixSem* sem)
+{
+ return WaitForSingleObject(sem->sem, INFINITE) == WAIT_OBJECT_0
+ ? ZIX_STATUS_SUCCESS
+ : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+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_UNAVAILABLE
+ : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_sem_timed_wait(ZixSem* sem,
+ const uint32_t seconds,
+ const uint32_t nanoseconds)
+{
+ const uint32_t milliseconds = seconds * 1000U + nanoseconds / 1000000U;
+ const DWORD r = WaitForSingleObject(sem->sem, milliseconds);
+
+ return (r == WAIT_OBJECT_0) ? ZIX_STATUS_SUCCESS
+ : (r == WAIT_TIMEOUT) ? ZIX_STATUS_TIMEOUT
+ : ZIX_STATUS_ERROR;
+}
diff --git a/src/win32/thread_win32.c b/src/win32/thread_win32.c
new file mode 100644
index 0000000..e4411b1
--- /dev/null
+++ b/src/win32/thread_win32.c
@@ -0,0 +1,28 @@
+// Copyright 2012-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "zix/thread.h"
+
+#include "zix/status.h"
+
+#include <windows.h>
+
+#include <stddef.h>
+
+ZixStatus
+zix_thread_create(ZixThread* thread,
+ size_t stack_size,
+ ZixThreadFunc function,
+ void* arg)
+{
+ *thread = CreateThread(NULL, stack_size, function, arg, 0, NULL);
+ return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_thread_join(ZixThread thread)
+{
+ return (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0)
+ ? ZIX_STATUS_SUCCESS
+ : ZIX_STATUS_ERROR;
+}