From c800a400b04b9ba25773f26e402a734a2ef3fe83 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 1 Aug 2014 21:55:13 -0700 Subject: Add MacOS X semaphore implementation --- src/sem.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/sem.h') diff --git a/src/sem.h b/src/sem.h index f0b7183..d557f22 100644 --- a/src/sem.h +++ b/src/sem.h @@ -34,8 +34,36 @@ #include "log.h" -#include +#ifdef __APPLE__ + +#include + +/** Generic semaphore type */ +typedef dispatch_semaphore_t fastd_sem_t; + + +/** Initializes a semaphore with a given value */ +static inline void fastd_sem_init(fastd_sem_t *sem, unsigned value) { + *sem = dispatch_semaphore_create(value); + if (!*sem) + exit_errno("dispatch_semaphore_create"); +} + +/** Increments the semaphore */ +static inline void fastd_sem_post(fastd_sem_t *sem) { + if (dispatch_semaphore_signal(*sem)) + exit_errno("sem_post"); +} + +/** Tries to decrement the semaphore */ +static inline bool fastd_sem_trywait(fastd_sem_t *sem) { + return !dispatch_semaphore_wait(*sem, DISPATCH_TIME_NOW); +} + +#else + +#include /** Generic semaphore type */ typedef sem_t fastd_sem_t; @@ -57,3 +85,5 @@ static inline void fastd_sem_post(fastd_sem_t *sem) { static inline bool fastd_sem_trywait(fastd_sem_t *sem) { return !sem_trywait(sem); } + +#endif -- cgit v1.2.3