From e4c4e8d04382b2b42d34ab6f3c765f845bb13eca Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sat, 14 May 2022 20:55:06 +0200 Subject: [PATCH] remain: use C11 mutex and add EALREADY thread test --- src/remain.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/remain.c b/src/remain.c index e2187dbf..711edb21 100644 --- a/src/remain.c +++ b/src/remain.c @@ -6,9 +6,6 @@ #include #include -#ifdef HAVE_PTHREAD -#include -#endif #include "test.h" @@ -17,12 +14,9 @@ #include -#ifdef HAVE_PTHREAD - - struct data { thrd_t tid; - pthread_mutex_t mutex; + mtx_t mutex; bool thread_started; bool thread_exited; unsigned tmr_called; @@ -35,7 +29,7 @@ static void tmr_handler(void *arg) struct data *data = arg; int err = 0; - pthread_mutex_lock(&data->mutex); + mtx_lock(&data->mutex); /* verify that timer is called from the new thread */ TEST_ASSERT(0 != thrd_equal(data->tid, thrd_current())); @@ -46,7 +40,7 @@ static void tmr_handler(void *arg) if (err) data->err = err; - pthread_mutex_unlock(&data->mutex); + mtx_unlock(&data->mutex); re_cancel(); } @@ -69,14 +63,18 @@ static int thread_handler(void *arg) return 0; } + err = re_thread_init(); + TEST_EQUALS(EALREADY, err); + tmr_start(&tmr, 1, tmr_handler, data); /* run the main loop now */ err = re_main(NULL); + +out: if (err) { data->err = err; } - tmr_cancel(&tmr); /* cleanup */ @@ -96,7 +94,7 @@ static int test_remain_thread(void) memset(&data, 0, sizeof(data)); - pthread_mutex_init(&data.mutex, NULL); + mtx_init(&data.mutex, mtx_plain); err = thrd_create(&data.tid, thread_handler, &data); if (err) @@ -105,14 +103,14 @@ static int test_remain_thread(void) /* wait for timer to be called */ for (i=0; i<500; i++) { - pthread_mutex_lock(&data.mutex); + mtx_lock(&data.mutex); if (data.tmr_called || data.err) { - pthread_mutex_unlock(&data.mutex); + mtx_unlock(&data.mutex); break; } - pthread_mutex_unlock(&data.mutex); + mtx_unlock(&data.mutex); sys_msleep(1); } @@ -133,18 +131,13 @@ static int test_remain_thread(void) } -#endif - - int test_remain(void) { int err = 0; -#ifdef HAVE_PTHREAD err = test_remain_thread(); if (err) return err; -#endif return err; }