Skip to content

Commit 291f7db

Browse files
Jordan YatesNickolasLapp
authored andcommitted
kernel: add k_can_yield helper function
Implements a function that application and driver code can use to check whether it is valid to yield (or block) in the current context. This check is required for functions that can feasibly be run from multiple contexts. The primary intended use case is power management transition functions, which can be run by application code explicitly or automatically in the idle thread by system PM. Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
1 parent ca78597 commit 291f7db

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/zephyr/kernel.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,19 @@ __syscall int32_t k_usleep(int32_t us);
464464
*/
465465
__syscall void k_busy_wait(uint32_t usec_to_wait);
466466

467+
/**
468+
* @brief Check whether it is possible to yield in the current context.
469+
*
470+
* This routine checks whether the kernel is in a state where it is possible to
471+
* yield or call blocking API's. It should be used by code that needs to yield
472+
* to perform correctly, but can feasibly be called from contexts where that
473+
* is not possible. For example in the PRE_KERNEL initialization step, or when
474+
* being run from the idle thread.
475+
*
476+
* @return True if it is possible to yield in the current context, false otherwise.
477+
*/
478+
bool k_can_yield(void);
479+
467480
/**
468481
* @brief Yield the current thread.
469482
*

kernel/sched.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,12 @@ static inline void z_vrfy_k_thread_deadline_set(k_tid_t tid, int deadline)
13251325
#endif
13261326
#endif
13271327

1328+
bool k_can_yield(void)
1329+
{
1330+
return !(k_is_pre_kernel() || k_is_in_isr() ||
1331+
z_is_idle_thread_object(_current));
1332+
}
1333+
13281334
void z_impl_k_yield(void)
13291335
{
13301336
__ASSERT(!arch_is_in_isr(), "");

0 commit comments

Comments
 (0)