diff options
Diffstat (limited to 'src/zix')
-rw-r--r-- | src/zix/common.h | 83 | ||||
-rw-r--r-- | src/zix/sem.h | 226 |
2 files changed, 309 insertions, 0 deletions
diff --git a/src/zix/common.h b/src/zix/common.h new file mode 100644 index 0000000..59e1f55 --- /dev/null +++ b/src/zix/common.h @@ -0,0 +1,83 @@ +/* + Copyright 2011 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_COMMON_H +#define ZIX_COMMON_H + +/** + @addtogroup zix + @{ +*/ + +/** @cond */ +#ifdef ZIX_SHARED +# ifdef _WIN32 +# define ZIX_LIB_IMPORT __declspec(dllimport) +# define ZIX_LIB_EXPORT __declspec(dllexport) +# else +# define ZIX_LIB_IMPORT __attribute__((visibility("default"))) +# define ZIX_LIB_EXPORT __attribute__((visibility("default"))) +# endif +# ifdef ZIX_INTERNAL +# define ZIX_API ZIX_LIB_EXPORT +# else +# define ZIX_API ZIX_LIB_IMPORT +# endif +#else +# define ZIX_API +#endif +/** @endcond */ + +#ifdef __cplusplus +extern "C" { +#else +# include <stdbool.h> +#endif + +typedef enum { + ZIX_STATUS_SUCCESS, + ZIX_STATUS_ERROR, + ZIX_STATUS_NO_MEM, + ZIX_STATUS_NOT_FOUND, + ZIX_STATUS_EXISTS, + ZIX_STATUS_BAD_ARG, + ZIX_STATUS_BAD_PERMS, +} ZixStatus; + +/** + Function for comparing two elements. +*/ +typedef int (*ZixComparator)(const void* a, const void* b, void* user_data); + +/** + Function for testing equality of two elements. +*/ +typedef bool (*ZixEqualFunc)(const void* a, const void* b); + +/** + Function to destroy an element. +*/ +typedef void (*ZixDestroyFunc)(void* ptr); + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_COMMON_H */ diff --git a/src/zix/sem.h b/src/zix/sem.h new file mode 100644 index 0000000..98117c8 --- /dev/null +++ b/src/zix/sem.h @@ -0,0 +1,226 @@ +/* + Copyright 2012 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef ZIX_SEM_H +#define ZIX_SEM_H + +#ifdef __APPLE__ +# include <mach/mach.h> +#elif defined(_WIN32) +# include <windows.h> +#else +# include <semaphore.h> +#endif + +#include "zix/common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @addtogroup zix + @{ + @name Semaphore + @{ +*/ + +/** + A counting semaphore. + + This is an integer that is always positive, and has two main operations: + increment (post) and decrement (wait). If a decrement can not be performed + (i.e. the value is 0) the caller will be blocked until another thread posts + and the operation can succeed. + + Semaphores can be created with any starting value, but typically this will + be 0 so the semaphore can be used as a simple signal where each post + corresponds to one wait. + + Semaphores are very efficient (much moreso than a mutex/cond pair). In + particular, at least on Linux, post is async-signal-safe, which means it + does not block and will not be interrupted. If you need to signal from + a realtime thread, this is the most appropriate primitive to use. +*/ +typedef struct ZixSemImpl ZixSem; + +/** + Create and initialize @c sem to @c initial. +*/ +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial); + +/** + Destroy @c sem. +*/ +static inline void +zix_sem_destroy(ZixSem* sem); + +/** + Increment (and signal any waiters). + Realtime safe. +*/ +static inline void +zix_sem_post(ZixSem* sem); + +/** + Wait until count is > 0, then decrement. + Obviously not realtime safe. +*/ +static inline void +zix_sem_wait(ZixSem* sem); + +/** + Non-blocking version of wait(). + + @return true if decrement was successful (lock was acquired). +*/ +static inline bool +zix_sem_try_wait(ZixSem* sem); + +/** + @cond +*/ + +#ifdef __APPLE__ + +struct ZixSemImpl { + semaphore_t sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, 0) + ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + semaphore_destroy(mach_task_self(), sem->sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + semaphore_signal(sem->sem); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + semaphore_wait(sem->sem); +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + const mach_timespec_t zero = { 0, 0 }; + return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS; +} + +#elif defined(_WIN32) + +struct ZixSemImpl { + HANDLE sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL); + return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + CloseHandle(sem->sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + ReleaseSemaphore(sem->sem, 1, NULL); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, INFINITE); +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, 0); +} + +#else /* !defined(__APPLE__) && !defined(_WIN32) */ + +struct ZixSemImpl { + sem_t sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + return sem_init(&sem->sem, 0, initial) + ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + sem_destroy(&sem->sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + sem_post(&sem->sem); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + /* Note that sem_wait always returns 0 in practice, except in + gdb (at least), where it returns nonzero, so the while is + necessary (and is the correct/safe solution in any case). + */ + while (sem_wait(&sem->sem) != 0) {} +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + return (sem_trywait(&sem->sem) == 0); +} + +#endif + +/** + @endcond + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_SEM_H */ |