diff options
author | David Robillard <d@drobilla.net> | 2022-08-10 05:32:34 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-08-17 13:51:03 -0400 |
commit | 06b49a3419f35ee8c802f1f15b52cc45b546efb3 (patch) | |
tree | 6a331324a73a83d6ff4d10087cdc740c3f1af8aa /src/zix/sem.h | |
parent | c17c67cdba3045be3c4a0f65dac0039987e0d8cf (diff) | |
download | jalv-06b49a3419f35ee8c802f1f15b52cc45b546efb3.tar.gz jalv-06b49a3419f35ee8c802f1f15b52cc45b546efb3.tar.bz2 jalv-06b49a3419f35ee8c802f1f15b52cc45b546efb3.zip |
Update zix
Diffstat (limited to 'src/zix/sem.h')
-rw-r--r-- | src/zix/sem.h | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/src/zix/sem.h b/src/zix/sem.h index fdd57ab..0337f03 100644 --- a/src/zix/sem.h +++ b/src/zix/sem.h @@ -4,6 +4,7 @@ #ifndef ZIX_SEM_H #define ZIX_SEM_H +#include "zix/attributes.h" #include "zix/common.h" #ifdef __APPLE__ @@ -50,31 +51,29 @@ struct ZixSemImpl; */ typedef struct ZixSemImpl ZixSem; -/** - Create and initialize `sem` to `initial`. -*/ +/// Create and initialize `sem` to `initial` static inline ZixStatus -zix_sem_init(ZixSem* sem, unsigned initial); +zix_sem_init(ZixSem* ZIX_NONNULL sem, unsigned initial); -/** - Destroy `sem`. -*/ +/// Destroy `sem` static inline void -zix_sem_destroy(ZixSem* sem); +zix_sem_destroy(ZixSem* ZIX_NONNULL sem); /** - Increment (and signal any waiters). + Increment and signal any waiters. + Realtime safe. */ static inline void -zix_sem_post(ZixSem* sem); +zix_sem_post(ZixSem* ZIX_NONNULL sem); /** Wait until count is > 0, then decrement. + Obviously not realtime safe. */ static inline ZixStatus -zix_sem_wait(ZixSem* sem); +zix_sem_wait(ZixSem* ZIX_NONNULL sem); /** Non-blocking version of wait(). @@ -82,7 +81,7 @@ zix_sem_wait(ZixSem* sem); @return true if decrement was successful (lock was acquired). */ static inline bool -zix_sem_try_wait(ZixSem* sem); +zix_sem_try_wait(ZixSem* ZIX_NONNULL sem); /** @cond @@ -95,27 +94,28 @@ struct ZixSemImpl { }; static inline ZixStatus -zix_sem_init(ZixSem* sem, unsigned val) +zix_sem_init(ZixSem* ZIX_NONNULL sem, unsigned val) { - return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, val) + return semaphore_create( + mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, (int)val) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; } static inline void -zix_sem_destroy(ZixSem* sem) +zix_sem_destroy(ZixSem* ZIX_NONNULL sem) { semaphore_destroy(mach_task_self(), sem->sem); } static inline void -zix_sem_post(ZixSem* sem) +zix_sem_post(ZixSem* ZIX_NONNULL sem) { semaphore_signal(sem->sem); } static inline ZixStatus -zix_sem_wait(ZixSem* sem) +zix_sem_wait(ZixSem* ZIX_NONNULL sem) { if (semaphore_wait(sem->sem) != KERN_SUCCESS) { return ZIX_STATUS_ERROR; @@ -124,7 +124,7 @@ zix_sem_wait(ZixSem* sem) } static inline bool -zix_sem_try_wait(ZixSem* sem) +zix_sem_try_wait(ZixSem* ZIX_NONNULL sem) { const mach_timespec_t zero = {0, 0}; return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS; @@ -137,26 +137,26 @@ struct ZixSemImpl { }; static inline ZixStatus -zix_sem_init(ZixSem* sem, unsigned initial) +zix_sem_init(ZixSem* ZIX_NONNULL sem, unsigned initial) { - sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL); + sem->sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL); return (sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; } static inline void -zix_sem_destroy(ZixSem* sem) +zix_sem_destroy(ZixSem* ZIX_NONNULL sem) { CloseHandle(sem->sem); } static inline void -zix_sem_post(ZixSem* sem) +zix_sem_post(ZixSem* ZIX_NONNULL sem) { ReleaseSemaphore(sem->sem, 1, NULL); } static inline ZixStatus -zix_sem_wait(ZixSem* sem) +zix_sem_wait(ZixSem* ZIX_NONNULL sem) { if (WaitForSingleObject(sem->sem, INFINITE) != WAIT_OBJECT_0) { return ZIX_STATUS_ERROR; @@ -165,7 +165,7 @@ zix_sem_wait(ZixSem* sem) } static inline bool -zix_sem_try_wait(ZixSem* sem) +zix_sem_try_wait(ZixSem* ZIX_NONNULL sem) { return WaitForSingleObject(sem->sem, 0) == WAIT_OBJECT_0; } @@ -177,26 +177,26 @@ struct ZixSemImpl { }; static inline ZixStatus -zix_sem_init(ZixSem* sem, unsigned initial) +zix_sem_init(ZixSem* ZIX_NONNULL sem, unsigned initial) { return sem_init(&sem->sem, 0, initial) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; } static inline void -zix_sem_destroy(ZixSem* sem) +zix_sem_destroy(ZixSem* ZIX_NONNULL sem) { sem_destroy(&sem->sem); } static inline void -zix_sem_post(ZixSem* sem) +zix_sem_post(ZixSem* ZIX_NONNULL sem) { sem_post(&sem->sem); } static inline ZixStatus -zix_sem_wait(ZixSem* sem) +zix_sem_wait(ZixSem* ZIX_NONNULL sem) { while (sem_wait(&sem->sem)) { if (errno != EINTR) { @@ -209,7 +209,7 @@ zix_sem_wait(ZixSem* sem) } static inline bool -zix_sem_try_wait(ZixSem* sem) +zix_sem_try_wait(ZixSem* ZIX_NONNULL sem) { return (sem_trywait(&sem->sem) == 0); } |