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>
This commit is contained in:
TaiJu Wu
2025-09-27 09:38:38 +08:00
committed by Johan Hedberg
parent abeef13a2f
commit d4d51dc062
2 changed files with 8 additions and 1 deletions

View File

@@ -861,7 +861,10 @@ void *z_get_next_switch_handle(void *interrupted)
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);

View File

@@ -1213,6 +1213,10 @@ void z_dummy_thread_init(struct k_thread *dummy_thread)
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 */