blob: e082080bc1cc43fc4957f51bfbb1af61a757c726 [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>
16#include <debug/object_tracing_common.h>
17#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
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020058#ifdef CONFIG_OBJECT_TRACING
59
Maciek Borzecki059544d2017-05-18 12:16:45 +020060struct k_queue *_trace_list_k_queue;
61
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020062/*
63 * Complete initialization of statically defined queues.
64 */
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020065static int init_queue_module(const struct device *dev)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020066{
67 ARG_UNUSED(dev);
68
Nicolas Pitreaa9228852019-06-03 13:01:43 -040069 Z_STRUCT_SECTION_FOREACH(k_queue, queue) {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020070 SYS_TRACING_OBJ_INIT(k_queue, queue);
71 }
72 return 0;
73}
74
75SYS_INIT(init_queue_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
76
77#endif /* CONFIG_OBJECT_TRACING */
78
Patrik Flykt4344e272019-03-08 14:19:05 -070079void z_impl_k_queue_init(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020080{
Andrew Boie2b9b4b22018-04-27 13:21:22 -070081 sys_sflist_init(&queue->data_q);
Andy Ross603ea422018-07-25 13:01:54 -070082 queue->lock = (struct k_spinlock) {};
Patrik Flykt4344e272019-03-08 14:19:05 -070083 z_waitq_init(&queue->wait_q);
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030084#if defined(CONFIG_POLL)
85 sys_dlist_init(&queue->poll_events);
86#endif
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020087
88 SYS_TRACING_OBJ_INIT(k_queue, queue);
Patrik Flykt4344e272019-03-08 14:19:05 -070089 z_object_init(queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +020090}
91
Andrew Boie2b9b4b22018-04-27 13:21:22 -070092#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -070093static inline void z_vrfy_k_queue_init(struct k_queue *queue)
Andrew Boie2b9b4b22018-04-27 13:21:22 -070094{
Andrew Boie8345e5e2018-05-04 15:57:57 -070095 Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(queue, K_OBJ_QUEUE));
Patrik Flykt4344e272019-03-08 14:19:05 -070096 z_impl_k_queue_init(queue);
Andrew Boie2b9b4b22018-04-27 13:21:22 -070097}
Andy Ross65649742019-08-06 13:34:31 -070098#include <syscalls/k_queue_init_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070099#endif
100
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200101static void prepare_thread_to_run(struct k_thread *thread, void *data)
102{
Andrew Boie4ad9f682019-09-21 16:25:56 -0700103 z_thread_return_value_set_with_data(thread, 0, data);
Andy Ross4c2fc2a2020-01-17 10:43:26 -0800104 z_ready_thread(thread);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200105}
106
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500107static inline void handle_poll_events(struct k_queue *queue, uint32_t state)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200108{
Andy Ross99c2d2d2020-06-02 08:34:12 -0700109#ifdef CONFIG_POLL
Patrik Flykt4344e272019-03-08 14:19:05 -0700110 z_handle_obj_poll_events(&queue->poll_events, state);
Anas Nashif80e6a972018-06-23 08:20:34 -0500111#endif
Andy Ross99c2d2d2020-06-02 08:34:12 -0700112}
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200113
Patrik Flykt4344e272019-03-08 14:19:05 -0700114void z_impl_k_queue_cancel_wait(struct k_queue *queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300115{
Andy Ross603ea422018-07-25 13:01:54 -0700116 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300117 struct k_thread *first_pending_thread;
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300118
Patrik Flykt4344e272019-03-08 14:19:05 -0700119 first_pending_thread = z_unpend_first_thread(&queue->wait_q);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300120
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700121 if (first_pending_thread != NULL) {
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300122 prepare_thread_to_run(first_pending_thread, NULL);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300123 }
124
Andy Ross99c2d2d2020-06-02 08:34:12 -0700125 handle_poll_events(queue, K_POLL_STATE_CANCELLED);
Patrik Flykt4344e272019-03-08 14:19:05 -0700126 z_reschedule(&queue->lock, key);
Paul Sokolovsky3f507072017-04-25 17:54:31 +0300127}
128
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700129#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -0700130static inline void z_vrfy_k_queue_cancel_wait(struct k_queue *queue)
131{
132 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
133 z_impl_k_queue_cancel_wait(queue);
134}
135#include <syscalls/k_queue_cancel_wait_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700136#endif
137
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500138static int32_t queue_insert(struct k_queue *queue, void *prev, void *data,
iva kik687f7992020-10-21 18:07:57 -0700139 bool alloc, bool is_append)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200140{
141 struct k_thread *first_pending_thread;
iva kik687f7992020-10-21 18:07:57 -0700142 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200143
iva kik687f7992020-10-21 18:07:57 -0700144 if (is_append) {
145 prev = sys_sflist_peek_tail(&queue->data_q);
146 }
Patrik Flykt4344e272019-03-08 14:19:05 -0700147 first_pending_thread = z_unpend_first_thread(&queue->wait_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200148
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700149 if (first_pending_thread != NULL) {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200150 prepare_thread_to_run(first_pending_thread, data);
Patrik Flykt4344e272019-03-08 14:19:05 -0700151 z_reschedule(&queue->lock, key);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700152 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200153 }
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +0300154
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700155 /* Only need to actually allocate if no threads are pending */
156 if (alloc) {
157 struct alloc_node *anode;
158
159 anode = z_thread_malloc(sizeof(*anode));
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700160 if (anode == NULL) {
Andy Ross603ea422018-07-25 13:01:54 -0700161 k_spin_unlock(&queue->lock, key);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700162 return -ENOMEM;
163 }
164 anode->data = data;
165 sys_sfnode_init(&anode->node, 0x1);
166 data = anode;
167 } else {
168 sys_sfnode_init(data, 0x0);
169 }
Andy Ross99c2d2d2020-06-02 08:34:12 -0700170
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700171 sys_sflist_insert(&queue->data_q, prev, data);
Andy Ross8606fab2018-03-26 10:54:40 -0700172 handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
Patrik Flykt4344e272019-03-08 14:19:05 -0700173 z_reschedule(&queue->lock, key);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700174 return 0;
175}
176
177void k_queue_insert(struct k_queue *queue, void *prev, void *data)
178{
iva kik687f7992020-10-21 18:07:57 -0700179 (void)queue_insert(queue, prev, data, false, false);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200180}
181
182void k_queue_append(struct k_queue *queue, void *data)
183{
iva kik687f7992020-10-21 18:07:57 -0700184 (void)queue_insert(queue, NULL, data, false, true);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200185}
186
187void k_queue_prepend(struct k_queue *queue, void *data)
188{
iva kik687f7992020-10-21 18:07:57 -0700189 (void)queue_insert(queue, NULL, data, false, false);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200190}
191
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500192int32_t z_impl_k_queue_alloc_append(struct k_queue *queue, void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700193{
iva kik687f7992020-10-21 18:07:57 -0700194 return queue_insert(queue, NULL, data, true, true);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700195}
196
197#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500198static inline int32_t z_vrfy_k_queue_alloc_append(struct k_queue *queue,
iva kik687f7992020-10-21 18:07:57 -0700199 void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700200{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700201 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
Andy Ross65649742019-08-06 13:34:31 -0700202 return z_impl_k_queue_alloc_append(queue, data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700203}
Andy Ross65649742019-08-06 13:34:31 -0700204#include <syscalls/k_queue_alloc_append_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700205#endif
206
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500207int32_t z_impl_k_queue_alloc_prepend(struct k_queue *queue, void *data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700208{
iva kik687f7992020-10-21 18:07:57 -0700209 return queue_insert(queue, NULL, data, true, false);
210
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_prepend(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_prepend(queue, data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700219}
Andy Ross65649742019-08-06 13:34:31 -0700220#include <syscalls/k_queue_alloc_prepend_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700221#endif
222
Anas Nashif756d8b02019-06-16 09:53:55 -0400223int k_queue_append_list(struct k_queue *queue, void *head, void *tail)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200224{
Anas Nashif756d8b02019-06-16 09:53:55 -0400225 /* invalid head or tail of list */
226 CHECKIF(head == NULL || tail == NULL) {
227 return -EINVAL;
228 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200229
Andy Ross603ea422018-07-25 13:01:54 -0700230 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Flavio Ceolina42de642018-11-13 16:26:56 -0800231 struct k_thread *thread = NULL;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200232
Flavio Ceolin76b35182018-12-16 12:48:29 -0800233 if (head != NULL) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700234 thread = z_unpend_first_thread(&queue->wait_q);
Flavio Ceolina42de642018-11-13 16:26:56 -0800235 }
236
237 while ((head != NULL) && (thread != NULL)) {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200238 prepare_thread_to_run(thread, head);
239 head = *(void **)head;
Patrik Flykt4344e272019-03-08 14:19:05 -0700240 thread = z_unpend_first_thread(&queue->wait_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200241 }
242
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700243 if (head != NULL) {
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700244 sys_sflist_append_list(&queue->data_q, head, tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200245 }
246
Andy Ross8606fab2018-03-26 10:54:40 -0700247 handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
Patrik Flykt4344e272019-03-08 14:19:05 -0700248 z_reschedule(&queue->lock, key);
Anas Nashif756d8b02019-06-16 09:53:55 -0400249 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200250}
251
Anas Nashif756d8b02019-06-16 09:53:55 -0400252int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200253{
Anas Nashif756d8b02019-06-16 09:53:55 -0400254 int ret;
255
256 /* list must not be empty */
257 CHECKIF(sys_slist_is_empty(list)) {
258 return -EINVAL;
259 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200260
261 /*
262 * note: this works as long as:
263 * - the slist implementation keeps the next pointer as the first
264 * field of the node object type
265 * - list->tail->next = NULL.
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700266 * - sflist implementation only differs from slist by stuffing
267 * flag bytes in the lower order bits of the data pointer
268 * - source list is really an slist and not an sflist with flags set
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200269 */
Anas Nashif756d8b02019-06-16 09:53:55 -0400270 ret = k_queue_append_list(queue, list->head, list->tail);
271 CHECKIF(ret != 0) {
272 return ret;
273 }
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200274 sys_slist_init(list);
Anas Nashif756d8b02019-06-16 09:53:55 -0400275
276 return 0;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200277}
278
Andy Ross78327382020-03-05 15:18:14 -0800279void *z_impl_k_queue_get(struct k_queue *queue, k_timeout_t timeout)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200280{
Andy Ross603ea422018-07-25 13:01:54 -0700281 k_spinlock_key_t key = k_spin_lock(&queue->lock);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200282 void *data;
283
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700284 if (likely(!sys_sflist_is_empty(&queue->data_q))) {
285 sys_sfnode_t *node;
286
287 node = sys_sflist_get_not_empty(&queue->data_q);
288 data = z_queue_node_peek(node, true);
Andy Ross603ea422018-07-25 13:01:54 -0700289 k_spin_unlock(&queue->lock, key);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200290 return data;
291 }
292
Andy Ross78327382020-03-05 15:18:14 -0800293 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Andy Ross603ea422018-07-25 13:01:54 -0700294 k_spin_unlock(&queue->lock, key);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200295 return NULL;
296 }
297
Patrik Flykt4344e272019-03-08 14:19:05 -0700298 int ret = z_pend_curr(&queue->lock, key, &queue->wait_q, timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200299
Adithya Baglody2a78b8d2018-10-25 12:09:04 +0530300 return (ret != 0) ? NULL : _current->base.swap_data;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200301}
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700302
303#ifdef CONFIG_USERSPACE
Andy Ross78327382020-03-05 15:18:14 -0800304static inline void *z_vrfy_k_queue_get(struct k_queue *queue,
305 k_timeout_t timeout)
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700306{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700307 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
Andy Ross65649742019-08-06 13:34:31 -0700308 return z_impl_k_queue_get(queue, timeout);
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700309}
Andy Ross65649742019-08-06 13:34:31 -0700310#include <syscalls/k_queue_get_mrsh.c>
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700311
Andy Ross65649742019-08-06 13:34:31 -0700312static inline int z_vrfy_k_queue_is_empty(struct k_queue *queue)
313{
314 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
315 return z_impl_k_queue_is_empty(queue);
316}
317#include <syscalls/k_queue_is_empty_mrsh.c>
318
319static inline void *z_vrfy_k_queue_peek_head(struct k_queue *queue)
320{
321 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
322 return z_impl_k_queue_peek_head(queue);
323}
324#include <syscalls/k_queue_peek_head_mrsh.c>
325
326static inline void *z_vrfy_k_queue_peek_tail(struct k_queue *queue)
327{
328 Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
329 return z_impl_k_queue_peek_tail(queue);
330}
331#include <syscalls/k_queue_peek_tail_mrsh.c>
332
Andrew Boie2b9b4b22018-04-27 13:21:22 -0700333#endif /* CONFIG_USERSPACE */