From b76cbe9af9ac562be00c9e34034cbf9a07480fa7 Mon Sep 17 00:00:00 2001 From: Carlo Caione Date: Mon, 8 Mar 2021 12:49:12 +0100 Subject: [PATCH] test: dynamic_thread: Fix race condition There is a race between k_sem_take() and k_object_access_grant() so it is possible (especially when testing SMP) that the thread tries to take the semaphore before the originating thread has had the chance to grant it permission. Signed-off-by: Carlo Caione --- tests/kernel/threads/dynamic_thread/src/main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/kernel/threads/dynamic_thread/src/main.c b/tests/kernel/threads/dynamic_thread/src/main.c index 8c5863e477f..20f60f884d5 100644 --- a/tests/kernel/threads/dynamic_thread/src/main.c +++ b/tests/kernel/threads/dynamic_thread/src/main.c @@ -50,11 +50,13 @@ static void create_dynamic_thread(void) tid = k_thread_create(dyn_thread, dyn_thread_stack, STACKSIZE, dyn_thread_entry, NULL, NULL, NULL, - K_PRIO_PREEMPT(0), K_USER, K_NO_WAIT); + K_PRIO_PREEMPT(0), K_USER, K_FOREVER); k_object_access_grant(&start_sem, tid); k_object_access_grant(&end_sem, tid); + k_thread_start(tid); + k_sem_give(&start_sem); zassert_true(k_sem_take(&end_sem, K_SECONDS(1)) == 0, @@ -76,10 +78,12 @@ static void permission_test(void) tid = k_thread_create(dyn_thread, dyn_thread_stack, STACKSIZE, dyn_thread_entry, NULL, NULL, NULL, - K_PRIO_PREEMPT(0), K_USER, K_NO_WAIT); + K_PRIO_PREEMPT(0), K_USER, K_FOREVER); k_object_access_grant(&start_sem, tid); + k_thread_start(tid); + /* * Notice dyn_thread will not have permission to access * end_sem, which will cause kernel oops.