diff --git a/include/kernel.h b/include/kernel.h index 71461d0ded4..ad8d76d1a4f 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -420,6 +420,20 @@ extern void k_sched_time_slice_set(int32_t slice, int prio); */ extern int k_is_in_isr(void); +/** + * @brief Determine if code is running in a preemptible thread. + * + * Returns a 'true' value if these conditions are all met: + * + * - the code is not running in an ISR + * - the thread's priority is in the preemptible range + * - the thread has not locked the scheduler + * + * @return 0 if invoked by either an ISR or a cooperative thread. + * @return Non-zero if invoked by a preemptible thread. + */ +extern int k_is_preempt_thread(void); + /* * @brief Lock the scheduler * diff --git a/kernel/unified/sched.c b/kernel/unified/sched.c index ce1575a74ca..b25d62ae851 100644 --- a/kernel/unified/sched.c +++ b/kernel/unified/sched.c @@ -363,3 +363,8 @@ void k_sched_time_slice_set(int32_t duration_in_ms, int prio) _time_slice_prio_ceiling = prio; } #endif /* CONFIG_TIMESLICING */ + +int k_is_preempt_thread(void) +{ + return !_is_in_isr() && _is_preempt(_current); +}