From cbeda68f4af8495e86ea41e776453be0a5c91150 Mon Sep 17 00:00:00 2001 From: Gerwin Klein Date: Fri, 27 Oct 2023 12:23:03 +1100 Subject: [PATCH] SCHED0021: decrease chances of a race condition Decrease the chance that the monitor thread is preempted just when it has released the preemption threads, but before it has yielded itself. (If that happens, the preemption threads get two time slices.) See also #42 Signed-off-by: Gerwin Klein --- apps/sel4test-tests/src/tests/scheduler.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/sel4test-tests/src/tests/scheduler.c b/apps/sel4test-tests/src/tests/scheduler.c index 3e6dcd83..b645bf68 100644 --- a/apps/sel4test-tests/src/tests/scheduler.c +++ b/apps/sel4test-tests/src/tests/scheduler.c @@ -1598,11 +1598,17 @@ static int test_simple_preempt(struct env *env) /* Set a timeout for the test. * Each thread should be run for one tick */ - uint64_t start = time_now(env); - uint64_t now = start; /* Start executing other threads */ ZF_LOGD("Releasing Threads"); + /* Release our time slice now to get a new one. That should ensure we are + * not interrupted due to bad luck by the preemptive scheduler right after + * setting test_simple_preempt_start to 1 and then voluntarily yielding + * (which gives the preemption threads 2 time slices instead of one). For + * the same reason we are also printing the ZF_LOGD() above already. + */ + seL4_Yield(); + uint64_t start = time_now(env); test_simple_preempt_start = 1; /* Yield should cause all other threads to execute before returning * to the current thread. */ @@ -1610,7 +1616,7 @@ static int test_simple_preempt(struct env *env) test_simple_preempt_start = 0; /* Get the total time taken to synchronise */ - now = time_now(env); + uint64_t now = time_now(env); uint64_t duration = now - start; for (size_t thread = 0; thread < PREEMPTION_THREADS; thread += 1) {