From 88af14b528ebbe9ed1aa7300aaa8d34409c43363 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Wed, 30 Jan 2019 17:15:06 +0100 Subject: [PATCH] fixup! tests: add tests to reproduce #10881 --- tests/thread_msg_block_race/Makefile | 1 + tests/thread_msg_block_race/main.c | 53 +++++++++++----------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/tests/thread_msg_block_race/Makefile b/tests/thread_msg_block_race/Makefile index 25ad0a900146..6bfda01fa8ac 100644 --- a/tests/thread_msg_block_race/Makefile +++ b/tests/thread_msg_block_race/Makefile @@ -4,6 +4,7 @@ BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6 DISABLE_MODULE += auto_init FEATURES_REQUIRED += periph_timer +USEMODULE += random TEST_ON_CI_WHITELIST += all diff --git a/tests/thread_msg_block_race/main.c b/tests/thread_msg_block_race/main.c index e3cf84dd9a07..bcc42e9cde48 100644 --- a/tests/thread_msg_block_race/main.c +++ b/tests/thread_msg_block_race/main.c @@ -21,53 +21,45 @@ #include #include +#include +#include "random.h" #include "thread.h" #include "msg.h" #include "xtimer.h" -#define CANARY_TYPE (0x21fd) +#define CANARY_TYPE (0x21fd) -#define TIMER_FREQ (1000000LU) -#define TIMER_TIMEOUT (1U) +#define TIMER_FREQ (1000000LU) +#define TIMER_TIMEOUT_MIN (1U) +#define TIMER_TIMEOUT_MAX (100U) -static char _t1_stack[THREAD_STACKSIZE_DEFAULT]; -static char _t2_stack[THREAD_STACKSIZE_DEFAULT]; - -static mutex_t _mutex = MUTEX_INIT_LOCKED; +static char _stack[THREAD_STACKSIZE_DEFAULT]; static kernel_pid_t _pid_main = KERNEL_PID_UNDEF; -static void _timer(void *arg, int channel) +static void _sched_next(void) { - (void)arg; - (void)channel; - /* do context switch */ - mutex_unlock(&_mutex); - timer_set(TIMER_DEV(0), 0, TIMER_TIMEOUT); + timer_set(TIMER_DEV(0), 0, random_uint32_range(TIMER_TIMEOUT_MIN, + TIMER_TIMEOUT_MAX)); } -static void *_thread1(void *arg) +static void _timer(void *arg, int channel) { - (void) arg; - - while (1) { - mutex_lock(&_mutex); - puts("nr1"); - } - - return NULL; + (void)arg; + (void)channel; + _sched_next(); } -static void *_thread2(void *arg) +static void *_thread(void *arg) { (void) arg; while (1) { msg_t msg = { .type = 0U }; - msg_try_send(&msg, _pid_main); - puts("nr2"); + write(STDOUT_FILENO, ".", 1U); + msg_send(&msg, _pid_main); } return NULL; @@ -78,14 +70,12 @@ int main(void) kernel_pid_t pid; timer_init(TIMER_DEV(0), TIMER_FREQ, _timer, NULL); + random_init(timer_read(TIMER_DEV(0))); puts("Test is \"successful\" if it runs forever without crashing"); _pid_main = sched_active_pid; - timer_set(TIMER_DEV(0), 0, TIMER_TIMEOUT); - pid = thread_create(_t1_stack, sizeof(_t1_stack), THREAD_PRIORITY_MAIN - 2, - THREAD_CREATE_STACKTEST, _thread1, NULL, "nr1"); - assert(pid != KERNEL_PID_UNDEF); - pid = thread_create(_t2_stack, sizeof(_t2_stack), THREAD_PRIORITY_MAIN - 1, - THREAD_CREATE_STACKTEST, _thread2, NULL, "nr2"); + _sched_next(); + pid = thread_create(_stack, sizeof(_stack), THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_STACKTEST, _thread, NULL, "nr2"); assert(pid != KERNEL_PID_UNDEF); while (1) { @@ -93,7 +83,6 @@ int main(void) msg_receive(&msg); assert(msg.type != CANARY_TYPE); - puts("main"); } return 0; }