Branch data Line data Source code
1 : : // Copyright 2012-2021 David Robillard <d@drobilla.net> 2 : : // SPDX-License-Identifier: ISC 3 : : 4 : : #undef NDEBUG 5 : : 6 : : #include <zix/attributes.h> 7 : : #include <zix/sem.h> 8 : : #include <zix/status.h> 9 : : #include <zix/thread.h> 10 : : 11 : : #include <assert.h> 12 : : #include <stdio.h> 13 : : #include <stdlib.h> 14 : : 15 : : static ZixSem sem; 16 : : static unsigned n_signals = 1024; 17 : : 18 : : static ZixThreadResult ZIX_THREAD_FUNC 19 : 2 : reader(void* ZIX_UNUSED(arg)) 20 : : { 21 : 2 : fprintf(stderr, "Reader starting\n"); 22 : : 23 [ + + ]: 1027 : for (unsigned i = 0; i < n_signals; ++i) { 24 [ - + ]: 1025 : assert(!zix_sem_wait(&sem)); 25 : : } 26 : : 27 : 2 : fprintf(stderr, "Reader finished\n"); 28 : 2 : return ZIX_THREAD_RESULT; 29 : : } 30 : : 31 : : static ZixThreadResult ZIX_THREAD_FUNC 32 : 2 : writer(void* ZIX_UNUSED(arg)) 33 : : { 34 : 2 : fprintf(stderr, "Writer starting\n"); 35 : : 36 [ + + ]: 1027 : for (unsigned i = 0; i < n_signals; ++i) { 37 [ - + ]: 1025 : assert(!zix_sem_post(&sem)); 38 : : } 39 : : 40 : 2 : fprintf(stderr, "Writer finished\n"); 41 : 2 : return ZIX_THREAD_RESULT; 42 : : } 43 : : 44 : : static void 45 : 2 : test_try_wait(void) 46 : : { 47 [ - + ]: 2 : assert(!zix_sem_init(&sem, 0)); 48 [ - + ]: 2 : assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE); 49 [ - + ]: 2 : assert(!zix_sem_post(&sem)); 50 [ - + ]: 2 : assert(!zix_sem_try_wait(&sem)); 51 [ - + ]: 2 : assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE); 52 [ - + ]: 2 : assert(!zix_sem_destroy(&sem)); 53 : 2 : } 54 : : 55 : : static void 56 : 2 : test_timed_wait(void) 57 : : { 58 [ - + ]: 2 : assert(!zix_sem_init(&sem, 0)); 59 [ - + ]: 2 : assert(zix_sem_timed_wait(&sem, 0, 0) == ZIX_STATUS_TIMEOUT); 60 [ - + ]: 2 : assert(zix_sem_timed_wait(&sem, 0, 999999999) == ZIX_STATUS_TIMEOUT); 61 [ - + ]: 2 : assert(!zix_sem_post(&sem)); 62 [ - + ]: 2 : assert(!zix_sem_timed_wait(&sem, 5, 0)); 63 [ - + ]: 2 : assert(!zix_sem_post(&sem)); 64 [ - + ]: 2 : assert(!zix_sem_timed_wait(&sem, 1000, 0)); 65 [ - + ]: 2 : assert(!zix_sem_destroy(&sem)); 66 : 2 : } 67 : : 68 : : int 69 : 3 : main(int argc, char** argv) 70 : : { 71 [ + + ]: 3 : if (argc > 2) { 72 : 1 : fprintf(stderr, "Usage: %s N_SIGNALS\n", argv[0]); 73 : 1 : return 1; 74 : : } 75 : : 76 [ + + ]: 2 : if (argc > 1) { 77 : 1 : n_signals = (unsigned)strtol(argv[1], NULL, 10); 78 : : } 79 : : 80 : 2 : test_try_wait(); 81 : 2 : test_timed_wait(); 82 : : 83 : 2 : fprintf(stderr, "Testing %u signals...\n", n_signals); 84 : : 85 [ - + ]: 2 : assert(!zix_sem_init(&sem, 0)); 86 : : 87 : : ZixThread reader_thread; // NOLINT 88 [ - + ]: 2 : assert(!zix_thread_create(&reader_thread, 128, reader, NULL)); 89 : : 90 : : ZixThread writer_thread; // NOLINT 91 [ - + ]: 2 : assert(!zix_thread_create(&writer_thread, 128, writer, NULL)); 92 : : 93 [ - + ]: 2 : assert(!zix_thread_join(reader_thread)); 94 [ - + ]: 2 : assert(!zix_thread_join(writer_thread)); 95 [ - + ]: 2 : assert(!zix_sem_destroy(&sem)); 96 : 2 : return 0; 97 : : }