Skip to content

Commit 4521e0c

Browse files
Andy Rossnashif
Andy Ross
authored andcommitted
kernel/sched: Mark sleeping threads suspended
On SMP, there was a bug where the logic that re-adds _current to the run queue at swap time would accidentally reschedule threads that had just gone to sleep, because the is_thread_prevented_from_running() predicate only tests for threads that are "suspended" or "pending" and not sleeping. Overload _THREAD_SUSPENDED to indicate "sleeping" also. Simple fix for an immediate bug, though long term we really want to unify all the blocked conditions to prevent this kind of state bug. Signed-off-by: Andy Ross <[email protected]>
1 parent 223bbec commit 4521e0c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

kernel/sched.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ void z_thread_timeout(struct _timeout *to)
423423
z_unpend_thread_no_timeout(th);
424424
}
425425
z_mark_thread_as_started(th);
426+
z_mark_thread_as_not_suspended(th);
426427
z_ready_thread(th);
427428
}
428429
#endif
@@ -944,9 +945,12 @@ s32_t z_impl_k_sleep(s32_t duration)
944945
#endif
945946
z_remove_thread_from_ready_q(_current);
946947
z_add_thread_timeout(_current, ticks);
948+
z_mark_thread_as_suspended(_current);
947949

948950
(void)z_swap(&local_lock, key);
949951

952+
__ASSERT(!z_is_thread_state_set(_current, _THREAD_SUSPENDED), "");
953+
950954
ticks = expected_wakeup_time - z_tick_get_32();
951955
if (ticks > 0) {
952956
return __ticks_to_ms(ticks);
@@ -979,6 +983,7 @@ void z_impl_k_wakeup(k_tid_t thread)
979983
return;
980984
}
981985

986+
z_mark_thread_as_not_suspended(thread);
982987
z_ready_thread(thread);
983988

984989
if (!z_is_in_isr()) {

0 commit comments

Comments
 (0)