Skip to content

Commit

Permalink
kernel: Add k_reschedule()
Browse files Browse the repository at this point in the history
The routine k_reschedule() allows an application to manually force
a schedule point. Although similar to k_yield(), it has different
properties. The most significant difference is that k_yield() if
invoked from a cooperative thread will voluntarily give up execution
control to the next thread of equal or higher priority while
k_reschedule() will not.

Applications that play with EDF deadlines via k_thread_deadline_set()
may need to use k_reschedule() to force a reschedule.

Signed-off-by: Peter Mitsis <[email protected]>
  • Loading branch information
peter-mitsis authored and kartben committed Dec 24, 2024
1 parent 3f4ff78 commit 669f2c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/zephyr/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,26 @@ __syscall void k_thread_priority_set(k_tid_t thread, int prio);
__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
#endif

/**
* @brief Invoke the scheduler
*
* This routine invokes the scheduler to force a schedule point on the current
* CPU. If invoked from within a thread, the scheduler will be invoked
* immediately (provided interrupts were not locked when invoked). If invoked
* from within an ISR, the scheduler will be invoked upon exiting the ISR.
*
* Invoking the scheduler allows the kernel to make an immediate determination
* as to what the next thread to execute should be. Unlike yielding, this
* routine is not guaranteed to switch to a thread of equal or higher priority
* if any are available. For example, if the current thread is cooperative and
* there is a still higher priority cooperative thread that is ready, then
* yielding will switch to that higher priority thread whereas this routine
* will not.
*
* Most applications will never use this routine.
*/
__syscall void k_reschedule(void);

#ifdef CONFIG_SCHED_CPU_MASK
/**
* @brief Sets all CPU enable masks to zero
Expand Down
18 changes: 18 additions & 0 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,24 @@ static inline void z_vrfy_k_thread_deadline_set(k_tid_t tid, int deadline)
#endif /* CONFIG_USERSPACE */
#endif /* CONFIG_SCHED_DEADLINE */

void z_impl_k_reschedule(void)
{
k_spinlock_key_t key;

key = k_spin_lock(&_sched_spinlock);

update_cache(0);

z_reschedule(&_sched_spinlock, key);
}

#ifdef CONFIG_USERSPACE
static inline void z_vrfy_k_reschedule(void)
{
z_impl_k_reschedule();
}
#endif /* CONFIG_USERSPACE */

bool k_can_yield(void)
{
return !(k_is_pre_kernel() || k_is_in_isr() ||
Expand Down

0 comments on commit 669f2c4

Please sign in to comment.