diff options
author | David Robillard <d@drobilla.net> | 2012-01-30 04:46:09 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-01-30 04:46:09 +0000 |
commit | 678d469abc1ef2720279ed4080395daa090d602f (patch) | |
tree | 7749eb09780db96c50442edd8d096ad1b8b3ddd5 /test | |
parent | f431110131bfe88f830386b573bdcaca3774de0d (diff) | |
download | zix-678d469abc1ef2720279ed4080395daa090d602f.tar.gz zix-678d469abc1ef2720279ed4080395daa090d602f.tar.bz2 zix-678d469abc1ef2720279ed4080395daa090d602f.zip |
Add ZixSem.
git-svn-id: http://svn.drobilla.net/zix/trunk@45 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'test')
-rw-r--r-- | test/sem_test.c | 86 |
1 files changed, 86 insertions, 0 deletions
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 <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. +*/ + +#include <limits.h> +#include <pthread.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#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; +} |