From 49398cfbf64e8ee78faab581544e846f2ca86f48 Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Wed, 21 Aug 2019 20:49:32 -0700 Subject: [PATCH] tests/kernel/sched/schedule_api: Relax preemption order constraints This test was testing for an undocumented and somewhat hyperspecific behavior: when a process reaches a reschedule point and yields to a higher priority thread, and there is another equal priority thread active, which thread gets to run when the higher priority thread finishes its work? The original scheduler (because it leaves the older thread in place in the list) implements the preemption like an interrupt and returns to the original thread, despite the fact that this then resets is time slice quantum unfairly. In SMP mode, where the current threads cannot live in the active list, the thread gets added back to the end of the queue and the other thread runs. In effect, in UP mode "yield" and "reschedule" mean very slightly different things where in SMP they act the same. We don't document either behavior, as it happens. Relax the test constraints by adding a single deliberate k_yield() to unify behavior. Signed-off-by: Andy Ross --- .../src/test_sched_timeslice_and_lock.c | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/kernel/sched/schedule_api/src/test_sched_timeslice_and_lock.c b/tests/kernel/sched/schedule_api/src/test_sched_timeslice_and_lock.c index 401f27db7f5..f0193dba08d 100644 --- a/tests/kernel/sched/schedule_api/src/test_sched_timeslice_and_lock.c +++ b/tests/kernel/sched/schedule_api/src/test_sched_timeslice_and_lock.c @@ -356,11 +356,15 @@ void test_unlock_preemptible(void) k_busy_wait(100000); k_sched_unlock(); - /* checkpoint: higher threads get executed */ + + /* ensure threads of equal priority can run */ + k_yield(); + + /* checkpoint: higher and equal threads get executed */ zassert_true(tdata[0].executed == 1, NULL); - for (int i = 1; i < THREADS_NUM; i++) { - zassert_true(tdata[i].executed == 0, NULL); - } + zassert_true(tdata[1].executed == 1, NULL); + zassert_true(tdata[2].executed == 0, NULL); + /* restore environment */ teardown_threads(); } @@ -404,11 +408,13 @@ void test_unlock_nested_sched_lock(void) /* unlock another; this let the higher thread to run */ k_sched_unlock(); + /* Ensure threads of equal priority run */ + k_yield(); + /* checkpoint: higher threads NOT get executed */ zassert_true(tdata[0].executed == 1, NULL); - for (int i = 1; i < THREADS_NUM; i++) { - zassert_true(tdata[i].executed == 0, NULL); - } + zassert_true(tdata[1].executed == 1, NULL); + zassert_true(tdata[2].executed == 0, NULL); /* restore environment */ teardown_threads();