kernel/sched: Optimize handling for suspend(_current)

k_thread_suspend() is an async API intended to stop any thread in any
state from any context.  Some apps just want to use it to "suspend
myself", which is a much (!) simpler operation.  Detect that specific
usage as a performance case.

Signed-off-by: Andy Ross <andyross@google.com>
This commit is contained in:
Andy Ross 2024-11-21 13:28:57 -08:00 committed by Benjamin Cabé
commit b3ff9ae82b

View file

@ -499,6 +499,19 @@ void z_impl_k_thread_suspend(k_tid_t thread)
{
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_thread, suspend, thread);
/* Special case "suspend the current thread" as it doesn't
* need the async complexity below.
*/
if (thread == _current && !arch_is_in_isr() && !IS_ENABLED(CONFIG_SMP)) {
k_spinlock_key_t key = k_spin_lock(&_sched_spinlock);
z_mark_thread_as_suspended(thread);
dequeue_thread(thread);
update_cache(1);
z_swap(&_sched_spinlock, key);
return;
}
(void)z_abort_thread_timeout(thread);
k_spinlock_key_t key = k_spin_lock(&_sched_spinlock);