Skip to content

Commit d4d51dc

Browse files
TaiJuWujhedberg
authored andcommitted
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 <[email protected]>
1 parent abeef13 commit d4d51dc

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

kernel/sched.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,10 @@ void *z_get_next_switch_handle(void *interrupted)
861861

862862
K_SPINLOCK(&_sched_spinlock) {
863863
struct k_thread *old_thread = _current, *new_thread;
864-
old_thread->switch_handle = NULL;
864+
865+
__ASSERT(old_thread->switch_handle == NULL,
866+
"old thread handle should be null.");
867+
865868
new_thread = next_up();
866869

867870
z_sched_usage_switch(new_thread);

kernel/thread.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,10 @@ void z_dummy_thread_init(struct k_thread *dummy_thread)
12131213
dummy_thread->resource_pool = NULL;
12141214
#endif /* K_HEAP_MEM_POOL_SIZE */
12151215

1216+
#ifdef CONFIG_USE_SWITCH
1217+
dummy_thread->switch_handle = NULL;
1218+
#endif /* CONFIG_USE_SWITCH */
1219+
12161220
#ifdef CONFIG_TIMESLICE_PER_THREAD
12171221
dummy_thread->base.slice_ticks = 0;
12181222
#endif /* CONFIG_TIMESLICE_PER_THREAD */

0 commit comments

Comments
 (0)