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 <ccaione@baylibre.com>
This commit is contained in:
Carlo Caione 2021-03-08 12:49:12 +01:00 committed by Anas Nashif
commit b76cbe9af9

View file

@ -50,11 +50,13 @@ static void create_dynamic_thread(void)
tid = k_thread_create(dyn_thread, dyn_thread_stack, STACKSIZE, tid = k_thread_create(dyn_thread, dyn_thread_stack, STACKSIZE,
dyn_thread_entry, NULL, NULL, NULL, 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(&start_sem, tid);
k_object_access_grant(&end_sem, tid); k_object_access_grant(&end_sem, tid);
k_thread_start(tid);
k_sem_give(&start_sem); k_sem_give(&start_sem);
zassert_true(k_sem_take(&end_sem, K_SECONDS(1)) == 0, 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, tid = k_thread_create(dyn_thread, dyn_thread_stack, STACKSIZE,
dyn_thread_entry, NULL, NULL, NULL, 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(&start_sem, tid);
k_thread_start(tid);
/* /*
* Notice dyn_thread will not have permission to access * Notice dyn_thread will not have permission to access
* end_sem, which will cause kernel oops. * end_sem, which will cause kernel oops.