blob: 42e7723538297cd040a6945b417e27d12e70a0c8 [file] [log] [blame]
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001/*
2 * Copyright (c) 2010-2016 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/**
8 * @file
9 *
10 * @brief dynamic-size QUEUE object.
11 */
12
13
14#include <kernel.h>
15#include <kernel_structs.h>
Anas Nashif4d994af2021-04-18 23:24:40 -040016
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020017#include <toolchain.h>
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020018#include <wait_q.h>
19#include <ksched.h>
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020020#include <init.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070021#include <syscall_handler.h>
Andy Ross4f911e12018-09-05 10:13:38 -070022#include <kernel_internal.h>
Anas Nashif756d8b02019-06-16 09:53:55 -040023#include <sys/check.h>
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020024
Andrew Boie2b9b4b22018-04-27 13:21:22 -070025struct alloc_node {
26 sys_sfnode_t node;
27 void *data;
28};
29
30void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free)
31{
32 void *ret;
33
Kumar Galaa1b77fd2020-05-27 11:26:57 -050034 if ((node != NULL) && (sys_sfnode_flags_get(node) != (uint8_t)0)) {
Andrew Boie2b9b4b22018-04-27 13:21:22 -070035 /* If the flag is set, then the enqueue operation for this item
36 * did a behind-the scenes memory allocation of an alloc_node
37 * struct, which is what got put in the queue. Free it and pass
38 * back the data pointer.
39 */
40 struct alloc_node *anode;
41
42 anode = CONTAINER_OF(node, struct alloc_node, node);
43 ret = anode->data;
44 if (needs_free) {
45 k_free(anode);
46 }
47 } else {
Nicolas Pitre659fa0d2019-05-21 22:13:01 -040048 /* Data was directly placed in the queue, the first word
Andrew Boie2b9b4b22018-04-27 13:21:22 -070049 * reserved for the linked list. User mode isn't allowed to
50 * do this, although it can get data sent this way.
51 */
52 ret = (void *)node;
53 }
54
55 return ret;
56}
57
Patrik Flykt4344e272019-03-08 14:19:05 -070058void z_impl_k_queue_init(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020059{
Andrew Boie2b9b4b22018-04-27 13:21:22 -070060 sys_sflist_init(&queue->data_q);
Andy Ross603ea422018-07-25 13:01:54 -070061 queue->lock = (struct k_spinlock) {};
Patrik Flykt4344e272019-03-08 14:19:05 -070062 z_waitq_init(&queue->wait_q);
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030063#if defined(CONFIG_POLL)
64 sys_dlist_init(&queue->poll_events);
65#endif
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020066
Torbjörn Leksellf9848232021-03-26 11:19:35 +010067 SYS_PORT_TRACING_OBJ_INIT(k_queue, queue);
68
Patrik Flykt4344e272019-03-08 14:19:05 -070069 z_object_init(queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020070}
71
Andrew Boie2b9b4b22018-04-27 13:21:22 -070072#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -070073static inline void z_vrfy_k_queue_init(struct k_queue *queue)
Andrew Boie2b9b4b22018-04-27 13:21:22 -070074{
Andrew Boie8345e5e2018-05-04 15:57:57 -070075 Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(queue, K_OBJ_QUEUE));
Patrik Flykt4344e272019-03-08 14:19:05 -070076 z_impl_k_queue_init(queue);
Andrew Boie2b9b4b22018-04-27 13:21:22 -070077}
Andy Ross65649742019-08-06 13:34:31 -070078#include <syscalls/k_queue_init_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070079#endif
80
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020081static void prepare_thread_to_run(struct k_thread *thread, void *data)
82{
Andrew Boie4ad9f682019-09-21 16:25:56 -070083 z_thread_return_value_set_with_data(thread, 0, data);
Andy Ross4c2fc2a2020-01-17 10:43:26 -080084 z_ready_thread(thread);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020085}
86
Kumar Galaa1b77fd2020-05-27 11:26:57 -050087static inline void handle_poll_events(struct k_queue *queue, uint32_t state)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020088{
Andy Ross99c2d2d2020-06-02 08:34:12 -070089#ifdef CONFIG_POLL
Patrik Flykt4344e272019-03-08 14:19:05 -070090 z_handle_obj_poll_events(&queue->poll_events, state);
Anas Nashif80e6a972018-06-23 08:20:34 -050091#endif
Andy Ross99c2d2d2020-06-02 08:34:12 -070092}
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020093
Patrik Flykt4344e272019-03-08 14:19:05 -070094void z_impl_k_queue_cancel_wait(struct k_queue *queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +030095{
Torbjörn Leksellf9848232021-03-26 11:19:35 +010096 SYS_PORT_TRACING_OBJ_FUNC(k_queue, cancel_wait, queue);
97
Andy Ross603ea422018-07-25 13:01:54 -070098 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Paul Sokolovsky3f507072017-04-25 17:54:31 +030099 struct k_thread *first_pending_thread;
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300100
Patrik Flykt4344e272019-03-08 14:19:05 -0700101 first_pending_thread = z_unpend_first_thread(&queue->wait_q);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300102
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700103 if (first_pending_thread != NULL) {
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300104 prepare_thread_to_run(first_pending_thread, NULL);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300105 }
106
Andy Ross99c2d2d2020-06-02 08:34:12 -0700107 handle_poll_events(queue, K_POLL_STATE_CANCELLED);
Patrik Flykt4344e272019-03-08 14:19:05 -0700108 z_reschedule(&queue->lock, key);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300109}
110
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700111#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -0700112static inline void z_vrfy_k_queue_cancel_wait(struct k_queue *queue)
113{
114 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
115 z_impl_k_queue_cancel_wait(queue);
116}
117#include <syscalls/k_queue_cancel_wait_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700118#endif
119
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500120static int32_t queue_insert(struct k_queue *queue, void *prev, void *data,
iva kik687f7992020-10-21 18:07:57 -0700121 bool alloc, bool is_append)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200122{
123 struct k_thread *first_pending_thread;
iva kik687f7992020-10-21 18:07:57 -0700124 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200125
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100126 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, queue_insert, queue, alloc);
127
iva kik687f7992020-10-21 18:07:57 -0700128 if (is_append) {
129 prev = sys_sflist_peek_tail(&queue->data_q);
130 }
Patrik Flykt4344e272019-03-08 14:19:05 -0700131 first_pending_thread = z_unpend_first_thread(&queue->wait_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200132
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700133 if (first_pending_thread != NULL) {
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100134 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, queue_insert, queue, alloc, K_FOREVER);
135
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200136 prepare_thread_to_run(first_pending_thread, data);
Patrik Flykt4344e272019-03-08 14:19:05 -0700137 z_reschedule(&queue->lock, key);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100138
139 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc, 0);
140
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700141 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200142 }
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +0300143
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700144 /* Only need to actually allocate if no threads are pending */
145 if (alloc) {
146 struct alloc_node *anode;
147
148 anode = z_thread_malloc(sizeof(*anode));
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700149 if (anode == NULL) {
Andy Ross603ea422018-07-25 13:01:54 -0700150 k_spin_unlock(&queue->lock, key);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100151
152 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc,
153 -ENOMEM);
154
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700155 return -ENOMEM;
156 }
157 anode->data = data;
158 sys_sfnode_init(&anode->node, 0x1);
159 data = anode;
160 } else {
161 sys_sfnode_init(data, 0x0);
162 }
Andy Ross99c2d2d2020-06-02 08:34:12 -0700163
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100164 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, queue_insert, queue, alloc, K_FOREVER);
165
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700166 sys_sflist_insert(&queue->data_q, prev, data);
Andy Ross8606fab2018-03-26 10:54:40 -0700167 handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
Patrik Flykt4344e272019-03-08 14:19:05 -0700168 z_reschedule(&queue->lock, key);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100169
170 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc, 0);
171
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700172 return 0;
173}
174
175void k_queue_insert(struct k_queue *queue, void *prev, void *data)
176{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100177 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, insert, queue);
178
iva kik687f7992020-10-21 18:07:57 -0700179 (void)queue_insert(queue, prev, data, false, false);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100180
181 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, insert, queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200182}
183
184void k_queue_append(struct k_queue *queue, void *data)
185{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100186 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, append, queue);
187
iva kik687f7992020-10-21 18:07:57 -0700188 (void)queue_insert(queue, NULL, data, false, true);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100189
190 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append, queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200191}
192
193void k_queue_prepend(struct k_queue *queue, void *data)
194{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100195 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, prepend, queue);
196
iva kik687f7992020-10-21 18:07:57 -0700197 (void)queue_insert(queue, NULL, data, false, false);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100198
199 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, prepend, queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200200}
201
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500202int32_t z_impl_k_queue_alloc_append(struct k_queue *queue, void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700203{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100204 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, alloc_append, queue);
205
206 int32_t ret = queue_insert(queue, NULL, data, true, true);
207
208 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, alloc_append, queue, ret);
209
210 return ret;
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700211}
212
213#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500214static inline int32_t z_vrfy_k_queue_alloc_append(struct k_queue *queue,
iva kik687f7992020-10-21 18:07:57 -0700215 void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700216{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700217 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
Andy Ross65649742019-08-06 13:34:31 -0700218 return z_impl_k_queue_alloc_append(queue, data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700219}
Andy Ross65649742019-08-06 13:34:31 -0700220#include <syscalls/k_queue_alloc_append_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700221#endif
222
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500223int32_t z_impl_k_queue_alloc_prepend(struct k_queue *queue, void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700224{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100225 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, alloc_prepend, queue);
iva kik687f7992020-10-21 18:07:57 -0700226
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100227 int32_t ret = queue_insert(queue, NULL, data, true, false);
228
229 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, alloc_prepend, queue, ret);
230
231 return ret;
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700232}
233
234#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500235static inline int32_t z_vrfy_k_queue_alloc_prepend(struct k_queue *queue,
iva kik687f7992020-10-21 18:07:57 -0700236 void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700237{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700238 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
Andy Ross65649742019-08-06 13:34:31 -0700239 return z_impl_k_queue_alloc_prepend(queue, data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700240}
Andy Ross65649742019-08-06 13:34:31 -0700241#include <syscalls/k_queue_alloc_prepend_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700242#endif
243
Anas Nashif756d8b02019-06-16 09:53:55 -0400244int k_queue_append_list(struct k_queue *queue, void *head, void *tail)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200245{
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100246 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, append_list, queue);
247
Anas Nashif756d8b02019-06-16 09:53:55 -0400248 /* invalid head or tail of list */
249 CHECKIF(head == NULL || tail == NULL) {
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100250 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append_list, queue, -EINVAL);
251
Anas Nashif756d8b02019-06-16 09:53:55 -0400252 return -EINVAL;
253 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200254
Andy Ross603ea422018-07-25 13:01:54 -0700255 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Flavio Ceolina42de642018-11-13 16:26:56 -0800256 struct k_thread *thread = NULL;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200257
Flavio Ceolin76b35182018-12-16 12:48:29 -0800258 if (head != NULL) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700259 thread = z_unpend_first_thread(&queue->wait_q);
Flavio Ceolina42de642018-11-13 16:26:56 -0800260 }
261
262 while ((head != NULL) && (thread != NULL)) {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200263 prepare_thread_to_run(thread, head);
264 head = *(void **)head;
Patrik Flykt4344e272019-03-08 14:19:05 -0700265 thread = z_unpend_first_thread(&queue->wait_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200266 }
267
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700268 if (head != NULL) {
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700269 sys_sflist_append_list(&queue->data_q, head, tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200270 }
271
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100272 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append_list, queue, 0);
273
Andy Ross8606fab2018-03-26 10:54:40 -0700274 handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
Patrik Flykt4344e272019-03-08 14:19:05 -0700275 z_reschedule(&queue->lock, key);
Anas Nashif756d8b02019-06-16 09:53:55 -0400276 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200277}
278
Anas Nashif756d8b02019-06-16 09:53:55 -0400279int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200280{
Anas Nashif756d8b02019-06-16 09:53:55 -0400281 int ret;
282
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100283 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, merge_slist, queue);
284
Anas Nashif756d8b02019-06-16 09:53:55 -0400285 /* list must not be empty */
286 CHECKIF(sys_slist_is_empty(list)) {
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100287 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, -EINVAL);
288
Anas Nashif756d8b02019-06-16 09:53:55 -0400289 return -EINVAL;
290 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200291
292 /*
293 * note: this works as long as:
294 * - the slist implementation keeps the next pointer as the first
295 * field of the node object type
296 * - list->tail->next = NULL.
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700297 * - sflist implementation only differs from slist by stuffing
298 * flag bytes in the lower order bits of the data pointer
299 * - source list is really an slist and not an sflist with flags set
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200300 */
Anas Nashif756d8b02019-06-16 09:53:55 -0400301 ret = k_queue_append_list(queue, list->head, list->tail);
302 CHECKIF(ret != 0) {
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100303 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, ret);
304
Anas Nashif756d8b02019-06-16 09:53:55 -0400305 return ret;
306 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200307 sys_slist_init(list);
Anas Nashif756d8b02019-06-16 09:53:55 -0400308
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100309 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, 0);
310
Anas Nashif756d8b02019-06-16 09:53:55 -0400311 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200312}
313
Andy Ross78327382020-03-05 15:18:14 -0800314void *z_impl_k_queue_get(struct k_queue *queue, k_timeout_t timeout)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200315{
Andy Ross603ea422018-07-25 13:01:54 -0700316 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200317 void *data;
318
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100319 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, get, queue, timeout);
320
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700321 if (likely(!sys_sflist_is_empty(&queue->data_q))) {
322 sys_sfnode_t *node;
323
324 node = sys_sflist_get_not_empty(&queue->data_q);
325 data = z_queue_node_peek(node, true);
Andy Ross603ea422018-07-25 13:01:54 -0700326 k_spin_unlock(&queue->lock, key);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100327
328 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout, data);
329
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200330 return data;
331 }
332
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100333 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, get, queue, timeout);
334
Andy Ross78327382020-03-05 15:18:14 -0800335 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Andy Ross603ea422018-07-25 13:01:54 -0700336 k_spin_unlock(&queue->lock, key);
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100337
338 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout, NULL);
339
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200340 return NULL;
341 }
342
Patrik Flykt4344e272019-03-08 14:19:05 -0700343 int ret = z_pend_curr(&queue->lock, key, &queue->wait_q, timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200344
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100345 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout,
346 (ret != 0) ? NULL : _current->base.swap_data);
347
Adithya Baglody2a78b8d2018-10-25 12:09:04 +0530348 return (ret != 0) ? NULL : _current->base.swap_data;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200349}
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700350
Torbjörn Leksellf9848232021-03-26 11:19:35 +0100351bool k_queue_remove(struct k_queue *queue, void *data)
352{
353 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, remove, queue);
354
355 bool ret = sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
356
357 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, remove, queue, ret);
358
359 return ret;
360}
361
362bool k_queue_unique_append(struct k_queue *queue, void *data)
363{
364 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, unique_append, queue);
365
366 sys_sfnode_t *test;
367
368 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
369 if (test == (sys_sfnode_t *) data) {
370 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, unique_append, queue, false);
371
372 return false;
373 }
374 }
375
376 k_queue_append(queue, data);
377
378 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, unique_append, queue, true);
379
380 return true;
381}
382
383void *z_impl_k_queue_peek_head(struct k_queue *queue)
384{
385 void *ret = z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
386
387 SYS_PORT_TRACING_OBJ_FUNC(k_queue, peek_head, queue, ret);
388
389 return ret;
390}
391
392void *z_impl_k_queue_peek_tail(struct k_queue *queue)
393{
394 void *ret = z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
395
396 SYS_PORT_TRACING_OBJ_FUNC(k_queue, peek_tail, queue, ret);
397
398 return ret;
399}
400
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700401#ifdef CONFIG_USERSPACE
Andy Ross78327382020-03-05 15:18:14 -0800402static inline void *z_vrfy_k_queue_get(struct k_queue *queue,
403 k_timeout_t timeout)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700404{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700405 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
Andy Ross65649742019-08-06 13:34:31 -0700406 return z_impl_k_queue_get(queue, timeout);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700407}
Andy Ross65649742019-08-06 13:34:31 -0700408#include <syscalls/k_queue_get_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700409
Andy Ross65649742019-08-06 13:34:31 -0700410static inline int z_vrfy_k_queue_is_empty(struct k_queue *queue)
411{
412 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
413 return z_impl_k_queue_is_empty(queue);
414}
415#include <syscalls/k_queue_is_empty_mrsh.c>
416
417static inline void *z_vrfy_k_queue_peek_head(struct k_queue *queue)
418{
419 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
420 return z_impl_k_queue_peek_head(queue);
421}
422#include <syscalls/k_queue_peek_head_mrsh.c>
423
424static inline void *z_vrfy_k_queue_peek_tail(struct k_queue *queue)
425{
426 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
427 return z_impl_k_queue_peek_tail(queue);
428}
429#include <syscalls/k_queue_peek_tail_mrsh.c>
430
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700431#endif /* CONFIG_USERSPACE */