Skip to content

Commit 669f2c4

Browse files
peter-mitsiskartben
authored andcommitted
kernel: Add k_reschedule()
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 <peter.mitsis@intel.com>
1 parent 3f4ff78 commit 669f2c4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

include/zephyr/kernel.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,26 @@ __syscall void k_thread_priority_set(k_tid_t thread, int prio);
946946
__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
947947
#endif
948948

949+
/**
950+
* @brief Invoke the scheduler
951+
*
952+
* This routine invokes the scheduler to force a schedule point on the current
953+
* CPU. If invoked from within a thread, the scheduler will be invoked
954+
* immediately (provided interrupts were not locked when invoked). If invoked
955+
* from within an ISR, the scheduler will be invoked upon exiting the ISR.
956+
*
957+
* Invoking the scheduler allows the kernel to make an immediate determination
958+
* as to what the next thread to execute should be. Unlike yielding, this
959+
* routine is not guaranteed to switch to a thread of equal or higher priority
960+
* if any are available. For example, if the current thread is cooperative and
961+
* there is a still higher priority cooperative thread that is ready, then
962+
* yielding will switch to that higher priority thread whereas this routine
963+
* will not.
964+
*
965+
* Most applications will never use this routine.
966+
*/
967+
__syscall void k_reschedule(void);
968+
949969
#ifdef CONFIG_SCHED_CPU_MASK
950970
/**
951971
* @brief Sets all CPU enable masks to zero

kernel/sched.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,24 @@ static inline void z_vrfy_k_thread_deadline_set(k_tid_t tid, int deadline)
10501050
#endif /* CONFIG_USERSPACE */
10511051
#endif /* CONFIG_SCHED_DEADLINE */
10521052

1053+
void z_impl_k_reschedule(void)
1054+
{
1055+
k_spinlock_key_t key;
1056+
1057+
key = k_spin_lock(&_sched_spinlock);
1058+
1059+
update_cache(0);
1060+
1061+
z_reschedule(&_sched_spinlock, key);
1062+
}
1063+
1064+
#ifdef CONFIG_USERSPACE
1065+
static inline void z_vrfy_k_reschedule(void)
1066+
{
1067+
z_impl_k_reschedule();
1068+
}
1069+
#endif /* CONFIG_USERSPACE */
1070+
10531071
bool k_can_yield(void)
10541072
{
10551073
return !(k_is_pre_kernel() || k_is_in_isr() ||

0 commit comments

Comments
 (0)