forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_work.c
59 lines (48 loc) · 1.42 KB
/
user_work.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2016 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
static void z_work_user_q_main(void *work_q_ptr, void *p2, void *p3)
{
struct k_work_user_q *work_q = work_q_ptr;
ARG_UNUSED(p2);
ARG_UNUSED(p3);
while (true) {
struct k_work_user *work;
k_work_user_handler_t handler;
work = k_queue_get(&work_q->queue, K_FOREVER);
if (work == NULL) {
continue;
}
handler = work->handler;
__ASSERT(handler != NULL, "handler must be provided");
/* Reset pending state so it can be resubmitted by handler */
if (atomic_test_and_clear_bit(&work->flags,
K_WORK_USER_STATE_PENDING)) {
handler(work);
}
/* Make sure we don't hog up the CPU if the FIFO never (or
* very rarely) gets empty.
*/
k_yield();
}
}
void k_work_user_queue_start(struct k_work_user_q *work_q, k_thread_stack_t *stack,
size_t stack_size, int prio, const char *name)
{
k_queue_init(&work_q->queue);
/* Created worker thread will inherit object permissions and memory
* domain configuration of the caller
*/
k_thread_create(&work_q->thread, stack, stack_size, z_work_user_q_main,
work_q, NULL, NULL, prio, K_USER | K_INHERIT_PERMS,
K_FOREVER);
k_object_access_grant(&work_q->queue, &work_q->thread);
if (name != NULL) {
k_thread_name_set(&work_q->thread, name);
}
k_thread_start(&work_q->thread);
}