From 0c001d5c7fd4c6c5d9ceeb55ba5fac1780b441af Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 18 Aug 2022 16:57:17 -0400 Subject: Fix or remove non-portable features in thread API Thread function return values are inconsistent between nearly every threading API out there. So, just ignore them entirely, and provide a typedef and sentinel value so user code can be portable. --- test/test_ring.c | 14 ++++++++------ test/test_sem.c | 14 ++++++++------ test/test_thread.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 test/test_thread.c (limited to 'test') diff --git a/test/test_ring.c b/test/test_ring.c index 79aa0f1..8016910 100644 --- a/test/test_ring.c +++ b/test/test_ring.c @@ -43,7 +43,8 @@ cmp_msg(const int* const msg1, const int* const msg2) return 1; } -static void* +ZIX_THREAD_FUNC +static ZixThreadResult reader(void* ZIX_UNUSED(arg)) { printf("Reader starting\n"); @@ -63,10 +64,11 @@ reader(void* ZIX_UNUSED(arg)) } printf("Reader finished\n"); - return NULL; + return ZIX_THREAD_RESULT; } -static void* +ZIX_THREAD_FUNC +static ZixThreadResult writer(void* ZIX_UNUSED(arg)) { printf("Writer starting\n"); @@ -82,7 +84,7 @@ writer(void* ZIX_UNUSED(arg)) } printf("Writer finished\n"); - return NULL; + return ZIX_THREAD_RESULT; } static int @@ -108,8 +110,8 @@ test_ring(const unsigned size) ZixThread writer_thread; // NOLINT assert(!zix_thread_create(&writer_thread, MSG_SIZE * 4UL, writer, NULL)); - zix_thread_join(reader_thread, NULL); - zix_thread_join(writer_thread, NULL); + assert(!zix_thread_join(reader_thread)); + assert(!zix_thread_join(writer_thread)); assert(!read_error); assert(ring); diff --git a/test/test_sem.c b/test/test_sem.c index 33859f1..bad0a44 100644 --- a/test/test_sem.c +++ b/test/test_sem.c @@ -14,7 +14,8 @@ static ZixSem sem; static unsigned n_signals = 1024; -static void* +ZIX_THREAD_FUNC +static ZixThreadResult reader(void* ZIX_UNUSED(arg)) { printf("Reader starting\n"); @@ -24,10 +25,11 @@ reader(void* ZIX_UNUSED(arg)) } printf("Reader finished\n"); - return NULL; + return ZIX_THREAD_RESULT; } -static void* +ZIX_THREAD_FUNC +static ZixThreadResult writer(void* ZIX_UNUSED(arg)) { printf("Writer starting\n"); @@ -37,7 +39,7 @@ writer(void* ZIX_UNUSED(arg)) } printf("Writer finished\n"); - return NULL; + return ZIX_THREAD_RESULT; } int @@ -62,8 +64,8 @@ main(int argc, char** argv) ZixThread writer_thread; // NOLINT assert(!zix_thread_create(&writer_thread, 128, writer, NULL)); - zix_thread_join(reader_thread, NULL); - zix_thread_join(writer_thread, NULL); + assert(!zix_thread_join(reader_thread)); + assert(!zix_thread_join(writer_thread)); zix_sem_destroy(&sem); return 0; diff --git a/test/test_thread.c b/test/test_thread.c new file mode 100644 index 0000000..6fe80a5 --- /dev/null +++ b/test/test_thread.c @@ -0,0 +1,41 @@ +// Copyright 2012-2022 David Robillard +// SPDX-License-Identifier: ISC + +#undef NDEBUG + +#include "zix/thread.h" + +#include +#include + +typedef struct { + int input; + int output; +} SharedData; + +ZIX_THREAD_FUNC +static ZixThreadResult +thread_func(void* const arg) +{ + SharedData* const data = (SharedData*)arg; + + data->output = data->input * 7; + + return ZIX_THREAD_RESULT; +} + +int +main(int argc, char** argv) +{ + (void)argv; + + ZixThread thread; // NOLINT + + SharedData data = {argc + (int)strlen(argv[0]), 0}; + + assert(!zix_thread_create(&thread, 128, thread_func, &data)); + assert(!zix_thread_join(thread)); + assert(data.output == data.input * 7); + + return 0; +} -- cgit v1.2.1