kernel: Replace redundant switch_handle assignment with assertion
The switch_handle for the outgoing thread is expected to be NULL
at the start of a context switch.
The previous code performed a redundant assignment to NULL.
This change replaces the assignment with an __ASSERT(). This makes the
code more robust by explicitly enforcing this precondition, helping to
catch potential scheduler bugs earlier.
Also, the switch_handle pointer is used to check a thread's state during a
context switch. For dummy threads, this pointer was left uninitialized,
potentially holding a unexpected value.
Set the handle to NULL during initialization to ensure these threads are
handled safely and predictably.
Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
diff --git a/kernel/sched.c b/kernel/sched.c
index 55984a5..aad3bfc 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -861,7 +861,10 @@
K_SPINLOCK(&_sched_spinlock) {
struct k_thread *old_thread = _current, *new_thread;
- old_thread->switch_handle = NULL;
+
+ __ASSERT(old_thread->switch_handle == NULL,
+ "old thread handle should be null.");
+
new_thread = next_up();
z_sched_usage_switch(new_thread);
diff --git a/kernel/thread.c b/kernel/thread.c
index 024b916..6d60356 100644
--- a/kernel/thread.c
+++ b/kernel/thread.c
@@ -1213,6 +1213,10 @@
dummy_thread->resource_pool = NULL;
#endif /* K_HEAP_MEM_POOL_SIZE */
+#ifdef CONFIG_USE_SWITCH
+ dummy_thread->switch_handle = NULL;
+#endif /* CONFIG_USE_SWITCH */
+
#ifdef CONFIG_TIMESLICE_PER_THREAD
dummy_thread->base.slice_ticks = 0;
#endif /* CONFIG_TIMESLICE_PER_THREAD */