Skip to content

Commit 3bbe188

Browse files
kernel: sys_workq: add k_is_in_sys_work API
Add k_is_in_sys_work() API which returns true if the thread context is the system workqueue thread and the thread is currently servicing a work item. Useful for checking if blocking is safe or required in the case of calling an API which uses the system work queue internally. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent bb1e9f9 commit 3bbe188

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
@@ -1181,6 +1181,19 @@ void k_thread_time_slice_set(struct k_thread *th, int32_t slice_ticks,
11811181
*/
11821182
bool k_is_in_isr(void);
11831183

1184+
/**
1185+
* @brief Determine if code is running from system work item
1186+
*
1187+
* This routine allows the caller to customize its actions, depending on
1188+
* whether it is running fom a system work item.
1189+
*
1190+
* @funcprops \isr_ok
1191+
*
1192+
* @return false if noit invoked from a system work item.
1193+
* @return true if invoked from a system work item.
1194+
*/
1195+
bool k_is_in_sys_work(void);
1196+
11841197
/**
11851198
* @brief Determine if code is running in a preemptible thread.
11861199
*

kernel/work.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,3 +1175,9 @@ bool k_work_flush_delayable(struct k_work_delayable *dwork,
11751175
}
11761176

11771177
#endif /* CONFIG_SYS_CLOCK_EXISTS */
1178+
1179+
bool k_is_in_sys_work(void)
1180+
{
1181+
return k_current_get() == k_work_queue_thread_get(&k_sys_work_q) &&
1182+
flag_test(&k_sys_work_q.flags, K_WORK_QUEUE_BUSY_BIT);
1183+
}

0 commit comments

Comments
 (0)