diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-08-02 06:55:13 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-08-02 06:55:13 +0200 |
commit | c800a400b04b9ba25773f26e402a734a2ef3fe83 (patch) | |
tree | 7330351988f0269922b9130d6412733ba2fe8208 /src/sem.h | |
parent | 4f25bdd2590f65586931ec71f798a91443e13674 (diff) | |
download | fastd-c800a400b04b9ba25773f26e402a734a2ef3fe83.tar fastd-c800a400b04b9ba25773f26e402a734a2ef3fe83.zip |
Add MacOS X semaphore implementation
Diffstat (limited to 'src/sem.h')
-rw-r--r-- | src/sem.h | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -34,8 +34,36 @@ #include "log.h" -#include <semaphore.h> +#ifdef __APPLE__ + +#include <dispatch/dispatch.h> + +/** 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 <semaphore.h> /** 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 |