From 678d469abc1ef2720279ed4080395daa090d602f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 30 Jan 2012 04:46:09 +0000 Subject: Add ZixSem. git-svn-id: http://svn.drobilla.net/zix/trunk@45 df6676b4-ccc9-40e5-b5d6-7c4628a128e3 --- test/sem_test.c | 86 ++++++++++++++++++++++++++ wscript | 1 + zix/sem.h | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 test/sem_test.c create mode 100644 zix/sem.h diff --git a/test/sem_test.c b/test/sem_test.c new file mode 100644 index 0000000..793f41b --- /dev/null +++ b/test/sem_test.c @@ -0,0 +1,86 @@ +/* + Copyright 2012 David Robillard + + 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. +*/ + +#include +#include +#include +#include +#include +#include + +#include "zix/sem.h" + +ZixSem sem; +size_t n_signals = 0; + +static void* +reader(void* arg) +{ + printf("Reader starting\n"); + + for (size_t i = 0; i < n_signals; ++i) { + zix_sem_wait(&sem); + } + + printf("Reader finished\n"); + return NULL; +} + +static void* +writer(void* arg) +{ + printf("Writer starting\n"); + + for (size_t i = 0; i < n_signals; ++i) { + zix_sem_post(&sem); + } + + printf("Writer finished\n"); + return NULL; +} + +int +main(int argc, char** argv) +{ + if (argc != 2 || argv[1][0] == '-') { + printf("Usage: %s N_SIGNALS\n", argv[0]); + return 1; + } + + n_signals = atoi(argv[1]); + + printf("Testing %zu signals...\n", n_signals); + + zix_sem_init(&sem, 0); + + pthread_t reader_thread; + if (pthread_create(&reader_thread, NULL, reader, NULL)) { + fprintf(stderr, "Failed to create reader thread\n"); + return 1; + } + + pthread_t writer_thread; + if (pthread_create(&writer_thread, NULL, writer, NULL)) { + fprintf(stderr, "Failed to create writer thread\n"); + return 1; + } + + pthread_join(reader_thread, NULL); + pthread_join(writer_thread, NULL); + + zix_sem_destroy(&sem); + return 0; +} diff --git a/wscript b/wscript index 27eb91f..3c9183c 100644 --- a/wscript +++ b/wscript @@ -66,6 +66,7 @@ tests = [ 'hash_test', 'patree_test', 'ring_test', + 'sem_test', 'sorted_array_test', 'strindex_test', 'tree_test' diff --git a/zix/sem.h b/zix/sem.h new file mode 100644 index 0000000..6b66a51 --- /dev/null +++ b/zix/sem.h @@ -0,0 +1,183 @@ +/* + Copyright 2012 David Robillard + + 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 + +#include + +#ifdef __APPLE__ +# include +# include +#else +# include +#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); + +#ifdef __APPLE__ + +struct ZixSemImpl { + MPSemaphoreID sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + MPCreateSemaphore(UINT_MAX, initial, &_sem); +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + MPDeleteSemaphore(_sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + MPSignalSemaphore(_sem); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + MPWaitOnSemaphore(sem->sem, kDurationForever); +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + return MPWaitOnSemaphore(sem->sem, kDurationImmediate) == noErr; +} + +#else /* !defined(__APPLE__) */ + +struct ZixSemImpl { + sem_t sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + return sem_init(&sem->sem, 0, initial) + ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +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 + +/** + @} + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ZIX_SEM_H */ -- cgit v1.2.1