1 /*
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 
17 #include <toolchain.h>
18 #include <wait_q.h>
19 #include <ksched.h>
20 #include <init.h>
21 #include <syscall_handler.h>
22 #include <kernel_internal.h>
23 #include <sys/check.h>
24 
25 struct alloc_node {
26 	sys_sfnode_t node;
27 	void *data;
28 };
29 
z_queue_node_peek(sys_sfnode_t * node,bool needs_free)30 void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free)
31 {
32 	void *ret;
33 
34 	if ((node != NULL) && (sys_sfnode_flags_get(node) != (uint8_t)0)) {
35 		/* 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 {
48 		/* Data was directly placed in the queue, the first word
49 		 * 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 
z_impl_k_queue_init(struct k_queue * queue)58 void z_impl_k_queue_init(struct k_queue *queue)
59 {
60 	sys_sflist_init(&queue->data_q);
61 	queue->lock = (struct k_spinlock) {};
62 	z_waitq_init(&queue->wait_q);
63 #if defined(CONFIG_POLL)
64 	sys_dlist_init(&queue->poll_events);
65 #endif
66 
67 	SYS_PORT_TRACING_OBJ_INIT(k_queue, queue);
68 
69 	z_object_init(queue);
70 }
71 
72 #ifdef CONFIG_USERSPACE
z_vrfy_k_queue_init(struct k_queue * queue)73 static inline void z_vrfy_k_queue_init(struct k_queue *queue)
74 {
75 	Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(queue, K_OBJ_QUEUE));
76 	z_impl_k_queue_init(queue);
77 }
78 #include <syscalls/k_queue_init_mrsh.c>
79 #endif
80 
prepare_thread_to_run(struct k_thread * thread,void * data)81 static void prepare_thread_to_run(struct k_thread *thread, void *data)
82 {
83 	z_thread_return_value_set_with_data(thread, 0, data);
84 	z_ready_thread(thread);
85 }
86 
handle_poll_events(struct k_queue * queue,uint32_t state)87 static inline void handle_poll_events(struct k_queue *queue, uint32_t state)
88 {
89 #ifdef CONFIG_POLL
90 	z_handle_obj_poll_events(&queue->poll_events, state);
91 #endif
92 }
93 
z_impl_k_queue_cancel_wait(struct k_queue * queue)94 void z_impl_k_queue_cancel_wait(struct k_queue *queue)
95 {
96 	SYS_PORT_TRACING_OBJ_FUNC(k_queue, cancel_wait, queue);
97 
98 	k_spinlock_key_t key = k_spin_lock(&queue->lock);
99 	struct k_thread *first_pending_thread;
100 
101 	first_pending_thread = z_unpend_first_thread(&queue->wait_q);
102 
103 	if (first_pending_thread != NULL) {
104 		prepare_thread_to_run(first_pending_thread, NULL);
105 	}
106 
107 	handle_poll_events(queue, K_POLL_STATE_CANCELLED);
108 	z_reschedule(&queue->lock, key);
109 }
110 
111 #ifdef CONFIG_USERSPACE
z_vrfy_k_queue_cancel_wait(struct k_queue * queue)112 static 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>
118 #endif
119 
queue_insert(struct k_queue * queue,void * prev,void * data,bool alloc,bool is_append)120 static int32_t queue_insert(struct k_queue *queue, void *prev, void *data,
121 			    bool alloc, bool is_append)
122 {
123 	struct k_thread *first_pending_thread;
124 	k_spinlock_key_t key = k_spin_lock(&queue->lock);
125 
126 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, queue_insert, queue, alloc);
127 
128 	if (is_append) {
129 		prev = sys_sflist_peek_tail(&queue->data_q);
130 	}
131 	first_pending_thread = z_unpend_first_thread(&queue->wait_q);
132 
133 	if (first_pending_thread != NULL) {
134 		SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, queue_insert, queue, alloc, K_FOREVER);
135 
136 		prepare_thread_to_run(first_pending_thread, data);
137 		z_reschedule(&queue->lock, key);
138 
139 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc, 0);
140 
141 		return 0;
142 	}
143 
144 	/* 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));
149 		if (anode == NULL) {
150 			k_spin_unlock(&queue->lock, key);
151 
152 			SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc,
153 				-ENOMEM);
154 
155 			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 	}
163 
164 	SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, queue_insert, queue, alloc, K_FOREVER);
165 
166 	sys_sflist_insert(&queue->data_q, prev, data);
167 	handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
168 	z_reschedule(&queue->lock, key);
169 
170 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, queue_insert, queue, alloc, 0);
171 
172 	return 0;
173 }
174 
k_queue_insert(struct k_queue * queue,void * prev,void * data)175 void k_queue_insert(struct k_queue *queue, void *prev, void *data)
176 {
177 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, insert, queue);
178 
179 	(void)queue_insert(queue, prev, data, false, false);
180 
181 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, insert, queue);
182 }
183 
k_queue_append(struct k_queue * queue,void * data)184 void k_queue_append(struct k_queue *queue, void *data)
185 {
186 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, append, queue);
187 
188 	(void)queue_insert(queue, NULL, data, false, true);
189 
190 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append, queue);
191 }
192 
k_queue_prepend(struct k_queue * queue,void * data)193 void k_queue_prepend(struct k_queue *queue, void *data)
194 {
195 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, prepend, queue);
196 
197 	(void)queue_insert(queue, NULL, data, false, false);
198 
199 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, prepend, queue);
200 }
201 
z_impl_k_queue_alloc_append(struct k_queue * queue,void * data)202 int32_t z_impl_k_queue_alloc_append(struct k_queue *queue, void *data)
203 {
204 	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;
211 }
212 
213 #ifdef CONFIG_USERSPACE
z_vrfy_k_queue_alloc_append(struct k_queue * queue,void * data)214 static inline int32_t z_vrfy_k_queue_alloc_append(struct k_queue *queue,
215 						  void *data)
216 {
217 	Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
218 	return z_impl_k_queue_alloc_append(queue, data);
219 }
220 #include <syscalls/k_queue_alloc_append_mrsh.c>
221 #endif
222 
z_impl_k_queue_alloc_prepend(struct k_queue * queue,void * data)223 int32_t z_impl_k_queue_alloc_prepend(struct k_queue *queue, void *data)
224 {
225 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, alloc_prepend, queue);
226 
227 	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;
232 }
233 
234 #ifdef CONFIG_USERSPACE
z_vrfy_k_queue_alloc_prepend(struct k_queue * queue,void * data)235 static inline int32_t z_vrfy_k_queue_alloc_prepend(struct k_queue *queue,
236 						   void *data)
237 {
238 	Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
239 	return z_impl_k_queue_alloc_prepend(queue, data);
240 }
241 #include <syscalls/k_queue_alloc_prepend_mrsh.c>
242 #endif
243 
k_queue_append_list(struct k_queue * queue,void * head,void * tail)244 int k_queue_append_list(struct k_queue *queue, void *head, void *tail)
245 {
246 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, append_list, queue);
247 
248 	/* invalid head or tail of list */
249 	CHECKIF(head == NULL || tail == NULL) {
250 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append_list, queue, -EINVAL);
251 
252 		return -EINVAL;
253 	}
254 
255 	k_spinlock_key_t key = k_spin_lock(&queue->lock);
256 	struct k_thread *thread = NULL;
257 
258 	if (head != NULL) {
259 		thread = z_unpend_first_thread(&queue->wait_q);
260 	}
261 
262 	while ((head != NULL) && (thread != NULL)) {
263 		prepare_thread_to_run(thread, head);
264 		head = *(void **)head;
265 		thread = z_unpend_first_thread(&queue->wait_q);
266 	}
267 
268 	if (head != NULL) {
269 		sys_sflist_append_list(&queue->data_q, head, tail);
270 	}
271 
272 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, append_list, queue, 0);
273 
274 	handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
275 	z_reschedule(&queue->lock, key);
276 	return 0;
277 }
278 
k_queue_merge_slist(struct k_queue * queue,sys_slist_t * list)279 int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list)
280 {
281 	int ret;
282 
283 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, merge_slist, queue);
284 
285 	/* list must not be empty */
286 	CHECKIF(sys_slist_is_empty(list)) {
287 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, -EINVAL);
288 
289 		return -EINVAL;
290 	}
291 
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.
297 	 * - 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
300 	 */
301 	ret = k_queue_append_list(queue, list->head, list->tail);
302 	CHECKIF(ret != 0) {
303 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, ret);
304 
305 		return ret;
306 	}
307 	sys_slist_init(list);
308 
309 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, merge_slist, queue, 0);
310 
311 	return 0;
312 }
313 
z_impl_k_queue_get(struct k_queue * queue,k_timeout_t timeout)314 void *z_impl_k_queue_get(struct k_queue *queue, k_timeout_t timeout)
315 {
316 	k_spinlock_key_t key = k_spin_lock(&queue->lock);
317 	void *data;
318 
319 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_queue, get, queue, timeout);
320 
321 	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);
326 		k_spin_unlock(&queue->lock, key);
327 
328 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout, data);
329 
330 		return data;
331 	}
332 
333 	SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_queue, get, queue, timeout);
334 
335 	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
336 		k_spin_unlock(&queue->lock, key);
337 
338 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout, NULL);
339 
340 		return NULL;
341 	}
342 
343 	int ret = z_pend_curr(&queue->lock, key, &queue->wait_q, timeout);
344 
345 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_queue, get, queue, timeout,
346 		(ret != 0) ? NULL : _current->base.swap_data);
347 
348 	return (ret != 0) ? NULL : _current->base.swap_data;
349 }
350 
k_queue_remove(struct k_queue * queue,void * data)351 bool 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 
k_queue_unique_append(struct k_queue * queue,void * data)362 bool 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 
z_impl_k_queue_peek_head(struct k_queue * queue)383 void *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 
z_impl_k_queue_peek_tail(struct k_queue * queue)392 void *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 
401 #ifdef CONFIG_USERSPACE
z_vrfy_k_queue_get(struct k_queue * queue,k_timeout_t timeout)402 static inline void *z_vrfy_k_queue_get(struct k_queue *queue,
403 				       k_timeout_t timeout)
404 {
405 	Z_OOPS(Z_SYSCALL_OBJ(queue, K_OBJ_QUEUE));
406 	return z_impl_k_queue_get(queue, timeout);
407 }
408 #include <syscalls/k_queue_get_mrsh.c>
409 
z_vrfy_k_queue_is_empty(struct k_queue * queue)410 static 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 
z_vrfy_k_queue_peek_head(struct k_queue * queue)417 static 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 
z_vrfy_k_queue_peek_tail(struct k_queue * queue)424 static 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 
431 #endif /* CONFIG_USERSPACE */
432