From d76b3b5de91122340b4af340beed2db3d7ee02f3 Mon Sep 17 00:00:00 2001 From: Peter Mitsis Date: Tue, 20 Feb 2024 11:50:54 -0500 Subject: [PATCH] kernel: Update k_wakeup() This commit does two things to k_wakeup(): 1. It locks the scheduler before marking the thread as not suspended. As the the clearing of the _THREAD_SUSPENDED bit is not atomic, this helps ensure that neither another thread nor ISR interrupts this action (resulting in a corrupted thread_state). 2. The call to flag_ipi() has been removed as it is already being made within ready_thread(). Signed-off-by: Peter Mitsis --- kernel/sched.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/sched.c b/kernel/sched.c index bb009238d5db..13a7b14b8c8c 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -1653,13 +1653,18 @@ void z_impl_k_wakeup(k_tid_t thread) } } + k_spinlock_key_t key = k_spin_lock(&sched_spinlock); + z_mark_thread_as_not_suspended(thread); - z_ready_thread(thread); - flag_ipi(); + if (!thread_active_elsewhere(thread)) { + ready_thread(thread); + } - if (!arch_is_in_isr()) { - z_reschedule_unlocked(); + if (arch_is_in_isr()) { + k_spin_unlock(&sched_spinlock, key); + } else { + z_reschedule(&sched_spinlock, key); } }