Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * @brief Message queues. |
| 10 | */ |
| 11 | |
| 12 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 13 | #include <zephyr/kernel.h> |
| 14 | #include <zephyr/kernel_structs.h> |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 15 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 16 | #include <zephyr/toolchain.h> |
| 17 | #include <zephyr/linker/sections.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 18 | #include <string.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 19 | #include <ksched.h> |
Anas Nashif | 8634c3b | 2023-08-29 17:03:12 +0000 | [diff] [blame] | 20 | #include <wait_q.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 21 | #include <zephyr/sys/dlist.h> |
| 22 | #include <zephyr/sys/math_extras.h> |
| 23 | #include <zephyr/init.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 24 | #include <zephyr/internal/syscall_handler.h> |
Andy Ross | 4f911e1 | 2018-09-05 10:13:38 -0700 | [diff] [blame] | 25 | #include <kernel_internal.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 26 | #include <zephyr/sys/check.h> |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 27 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 28 | #ifdef CONFIG_OBJ_CORE_MSGQ |
| 29 | static struct k_obj_type obj_type_msgq; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 30 | #endif /* CONFIG_OBJ_CORE_MSGQ */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 31 | |
Nick Graves | b445f13 | 2021-04-12 12:35:18 -0700 | [diff] [blame] | 32 | #ifdef CONFIG_POLL |
| 33 | static inline void handle_poll_events(struct k_msgq *msgq, uint32_t state) |
| 34 | { |
| 35 | z_handle_obj_poll_events(&msgq->poll_events, state); |
| 36 | } |
| 37 | #endif /* CONFIG_POLL */ |
| 38 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 39 | void k_msgq_init(struct k_msgq *msgq, char *buffer, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 40 | uint32_t max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 41 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 42 | msgq->msg_size = msg_size; |
| 43 | msgq->max_msgs = max_msgs; |
| 44 | msgq->buffer_start = buffer; |
| 45 | msgq->buffer_end = buffer + (max_msgs * msg_size); |
| 46 | msgq->read_ptr = buffer; |
| 47 | msgq->write_ptr = buffer; |
| 48 | msgq->used_msgs = 0; |
| 49 | msgq->flags = 0; |
| 50 | z_waitq_init(&msgq->wait_q); |
| 51 | msgq->lock = (struct k_spinlock) {}; |
Nick Graves | b445f13 | 2021-04-12 12:35:18 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_POLL |
| 53 | sys_dlist_init(&msgq->poll_events); |
| 54 | #endif /* CONFIG_POLL */ |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 55 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 56 | #ifdef CONFIG_OBJ_CORE_MSGQ |
| 57 | k_obj_core_init_and_link(K_OBJ_CORE(msgq), &obj_type_msgq); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 58 | #endif /* CONFIG_OBJ_CORE_MSGQ */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 59 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 60 | SYS_PORT_TRACING_OBJ_INIT(k_msgq, msgq); |
| 61 | |
Anas Nashif | c91cad7 | 2023-09-26 21:32:13 +0000 | [diff] [blame] | 62 | k_object_init(msgq); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 65 | int z_impl_k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 66 | uint32_t max_msgs) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 67 | { |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 68 | void *buffer; |
| 69 | int ret; |
| 70 | size_t total_size; |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 71 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 72 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_msgq, alloc_init, msgq); |
| 73 | |
Jakob Olesen | c8708d9 | 2019-05-07 10:17:35 -0700 | [diff] [blame] | 74 | if (size_mul_overflow(msg_size, max_msgs, &total_size)) { |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 75 | ret = -EINVAL; |
| 76 | } else { |
| 77 | buffer = z_thread_malloc(total_size); |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 78 | if (buffer != NULL) { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 79 | k_msgq_init(msgq, buffer, msg_size, max_msgs); |
| 80 | msgq->flags = K_MSGQ_FLAG_ALLOC; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 81 | ret = 0; |
| 82 | } else { |
| 83 | ret = -ENOMEM; |
| 84 | } |
| 85 | } |
| 86 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 87 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, alloc_init, msgq, ret); |
| 88 | |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 93 | int z_vrfy_k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 94 | uint32_t max_msgs) |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 95 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 96 | K_OOPS(K_SYSCALL_OBJ_NEVER_INIT(msgq, K_OBJ_MSGQ)); |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 97 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 98 | return z_impl_k_msgq_alloc_init(msgq, msg_size, max_msgs); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 99 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 100 | #include <zephyr/syscalls/k_msgq_alloc_init_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 101 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 102 | |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 103 | int k_msgq_cleanup(struct k_msgq *msgq) |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 104 | { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 105 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_msgq, cleanup, msgq); |
| 106 | |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 107 | CHECKIF(z_waitq_head(&msgq->wait_q) != NULL) { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 108 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, cleanup, msgq, -EBUSY); |
| 109 | |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 110 | return -EBUSY; |
| 111 | } |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 112 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 113 | if ((msgq->flags & K_MSGQ_FLAG_ALLOC) != 0U) { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 114 | k_free(msgq->buffer_start); |
| 115 | msgq->flags &= ~K_MSGQ_FLAG_ALLOC; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 116 | } |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 117 | |
| 118 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, cleanup, msgq, 0); |
| 119 | |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 120 | return 0; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Lauren Murphy | f29a2d1 | 2020-09-16 21:13:40 -0500 | [diff] [blame] | 124 | int z_impl_k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 125 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 126 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 127 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 128 | struct k_thread *pending_thread; |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 129 | k_spinlock_key_t key; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 130 | int result; |
| 131 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 132 | key = k_spin_lock(&msgq->lock); |
| 133 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 134 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_msgq, put, msgq, timeout); |
| 135 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 136 | if (msgq->used_msgs < msgq->max_msgs) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 137 | /* message queue isn't full */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 138 | pending_thread = z_unpend_first_thread(&msgq->wait_q); |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 139 | if (pending_thread != NULL) { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 140 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, put, msgq, timeout, 0); |
| 141 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 142 | /* give message to waiting thread */ |
Flavio Ceolin | 6699423 | 2018-08-13 15:17:04 -0700 | [diff] [blame] | 143 | (void)memcpy(pending_thread->base.swap_data, data, |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 144 | msgq->msg_size); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 145 | /* wake up waiting thread */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 146 | arch_thread_return_value_set(pending_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 147 | z_ready_thread(pending_thread); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 148 | z_reschedule(&msgq->lock, key); |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 149 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 150 | } else { |
| 151 | /* put message in queue */ |
Armin Brauns | 0bc342f | 2023-05-08 11:12:06 +0000 | [diff] [blame] | 152 | __ASSERT_NO_MSG(msgq->write_ptr >= msgq->buffer_start && |
| 153 | msgq->write_ptr < msgq->buffer_end); |
Hess Nathan | c30a9c4c | 2024-04-29 14:49:11 +0200 | [diff] [blame] | 154 | (void)memcpy(msgq->write_ptr, (char *)data, msgq->msg_size); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 155 | msgq->write_ptr += msgq->msg_size; |
| 156 | if (msgq->write_ptr == msgq->buffer_end) { |
| 157 | msgq->write_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 158 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 159 | msgq->used_msgs++; |
Nick Graves | b445f13 | 2021-04-12 12:35:18 -0700 | [diff] [blame] | 160 | #ifdef CONFIG_POLL |
| 161 | handle_poll_events(msgq, K_POLL_STATE_MSGQ_DATA_AVAILABLE); |
| 162 | #endif /* CONFIG_POLL */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 163 | } |
| 164 | result = 0; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 165 | } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 166 | /* don't wait for message space to become available */ |
| 167 | result = -ENOMSG; |
| 168 | } else { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 169 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_msgq, put, msgq, timeout); |
| 170 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 171 | /* wait for put message success, failure, or timeout */ |
Lauren Murphy | f29a2d1 | 2020-09-16 21:13:40 -0500 | [diff] [blame] | 172 | _current->base.swap_data = (void *) data; |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 173 | |
| 174 | result = z_pend_curr(&msgq->lock, key, &msgq->wait_q, timeout); |
| 175 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, put, msgq, timeout, result); |
| 176 | return result; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 179 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, put, msgq, timeout, result); |
| 180 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 181 | k_spin_unlock(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 182 | |
| 183 | return result; |
| 184 | } |
| 185 | |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 186 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 187 | static inline int z_vrfy_k_msgq_put(struct k_msgq *msgq, const void *data, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 188 | k_timeout_t timeout) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 189 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 190 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
| 191 | K_OOPS(K_SYSCALL_MEMORY_READ(data, msgq->msg_size)); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 192 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 193 | return z_impl_k_msgq_put(msgq, data, timeout); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 194 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 195 | #include <zephyr/syscalls/k_msgq_put_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 196 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 197 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 198 | void z_impl_k_msgq_get_attrs(struct k_msgq *msgq, struct k_msgq_attrs *attrs) |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 199 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 200 | attrs->msg_size = msgq->msg_size; |
| 201 | attrs->max_msgs = msgq->max_msgs; |
| 202 | attrs->used_msgs = msgq->used_msgs; |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 206 | static inline void z_vrfy_k_msgq_get_attrs(struct k_msgq *msgq, |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 207 | struct k_msgq_attrs *attrs) |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 208 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 209 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
| 210 | K_OOPS(K_SYSCALL_MEMORY_WRITE(attrs, sizeof(struct k_msgq_attrs))); |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 211 | z_impl_k_msgq_get_attrs(msgq, attrs); |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 212 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 213 | #include <zephyr/syscalls/k_msgq_get_attrs_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 214 | #endif /* CONFIG_USERSPACE */ |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 215 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 216 | int z_impl_k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 217 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 218 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 219 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 220 | k_spinlock_key_t key; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 221 | struct k_thread *pending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 222 | int result; |
| 223 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 224 | key = k_spin_lock(&msgq->lock); |
| 225 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 226 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_msgq, get, msgq, timeout); |
| 227 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 228 | if (msgq->used_msgs > 0U) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 229 | /* take first available message from queue */ |
Hess Nathan | c30a9c4c | 2024-04-29 14:49:11 +0200 | [diff] [blame] | 230 | (void)memcpy((char *)data, msgq->read_ptr, msgq->msg_size); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 231 | msgq->read_ptr += msgq->msg_size; |
| 232 | if (msgq->read_ptr == msgq->buffer_end) { |
| 233 | msgq->read_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 234 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 235 | msgq->used_msgs--; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 236 | |
| 237 | /* handle first thread waiting to write (if any) */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 238 | pending_thread = z_unpend_first_thread(&msgq->wait_q); |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 239 | if (pending_thread != NULL) { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 240 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_msgq, get, msgq, timeout); |
| 241 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 242 | /* add thread's message to queue */ |
Armin Brauns | 0bc342f | 2023-05-08 11:12:06 +0000 | [diff] [blame] | 243 | __ASSERT_NO_MSG(msgq->write_ptr >= msgq->buffer_start && |
| 244 | msgq->write_ptr < msgq->buffer_end); |
Hess Nathan | c30a9c4c | 2024-04-29 14:49:11 +0200 | [diff] [blame] | 245 | (void)memcpy(msgq->write_ptr, (char *)pending_thread->base.swap_data, |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 246 | msgq->msg_size); |
| 247 | msgq->write_ptr += msgq->msg_size; |
| 248 | if (msgq->write_ptr == msgq->buffer_end) { |
| 249 | msgq->write_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 250 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 251 | msgq->used_msgs++; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 252 | |
| 253 | /* wake up waiting thread */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 254 | arch_thread_return_value_set(pending_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 255 | z_ready_thread(pending_thread); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 256 | z_reschedule(&msgq->lock, key); |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 257 | |
| 258 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, get, msgq, timeout, 0); |
| 259 | |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 260 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 261 | } |
| 262 | result = 0; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 263 | } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 264 | /* don't wait for a message to become available */ |
| 265 | result = -ENOMSG; |
| 266 | } else { |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 267 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_msgq, get, msgq, timeout); |
| 268 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 269 | /* wait for get message success or timeout */ |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 270 | _current->base.swap_data = data; |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 271 | |
| 272 | result = z_pend_curr(&msgq->lock, key, &msgq->wait_q, timeout); |
| 273 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, get, msgq, timeout, result); |
| 274 | return result; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 277 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_msgq, get, msgq, timeout, result); |
| 278 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 279 | k_spin_unlock(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 280 | |
| 281 | return result; |
| 282 | } |
| 283 | |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 284 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 285 | static inline int z_vrfy_k_msgq_get(struct k_msgq *msgq, void *data, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 286 | k_timeout_t timeout) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 287 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 288 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
| 289 | K_OOPS(K_SYSCALL_MEMORY_WRITE(data, msgq->msg_size)); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 290 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 291 | return z_impl_k_msgq_get(msgq, data, timeout); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 292 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 293 | #include <zephyr/syscalls/k_msgq_get_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 294 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 295 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 296 | int z_impl_k_msgq_peek(struct k_msgq *msgq, void *data) |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 297 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 298 | k_spinlock_key_t key; |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 299 | int result; |
| 300 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 301 | key = k_spin_lock(&msgq->lock); |
| 302 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 303 | if (msgq->used_msgs > 0U) { |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 304 | /* take first available message from queue */ |
Hess Nathan | c30a9c4c | 2024-04-29 14:49:11 +0200 | [diff] [blame] | 305 | (void)memcpy((char *)data, msgq->read_ptr, msgq->msg_size); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 306 | result = 0; |
| 307 | } else { |
| 308 | /* don't wait for a message to become available */ |
| 309 | result = -ENOMSG; |
| 310 | } |
| 311 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 312 | SYS_PORT_TRACING_OBJ_FUNC(k_msgq, peek, msgq, result); |
| 313 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 314 | k_spin_unlock(&msgq->lock, key); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
| 319 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 320 | static inline int z_vrfy_k_msgq_peek(struct k_msgq *msgq, void *data) |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 321 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 322 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
| 323 | K_OOPS(K_SYSCALL_MEMORY_WRITE(data, msgq->msg_size)); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 324 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 325 | return z_impl_k_msgq_peek(msgq, data); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 326 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 327 | #include <zephyr/syscalls/k_msgq_peek_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 328 | #endif /* CONFIG_USERSPACE */ |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 329 | |
romain pelletant | 14bcc85 | 2022-12-28 13:06:16 +0100 | [diff] [blame] | 330 | int z_impl_k_msgq_peek_at(struct k_msgq *msgq, void *data, uint32_t idx) |
| 331 | { |
| 332 | k_spinlock_key_t key; |
| 333 | int result; |
| 334 | uint32_t bytes_to_end; |
| 335 | uint32_t byte_offset; |
| 336 | char *start_addr; |
| 337 | |
| 338 | key = k_spin_lock(&msgq->lock); |
| 339 | |
| 340 | if (msgq->used_msgs > idx) { |
| 341 | bytes_to_end = (msgq->buffer_end - msgq->read_ptr); |
| 342 | byte_offset = idx * msgq->msg_size; |
| 343 | start_addr = msgq->read_ptr; |
| 344 | /* check item available in start/end of ring buffer */ |
| 345 | if (bytes_to_end <= byte_offset) { |
| 346 | /* Tweak the values in case */ |
| 347 | byte_offset -= bytes_to_end; |
| 348 | /* wrap-around is required */ |
| 349 | start_addr = msgq->buffer_start; |
| 350 | } |
| 351 | (void)memcpy(data, start_addr + byte_offset, msgq->msg_size); |
| 352 | result = 0; |
| 353 | } else { |
| 354 | /* don't wait for a message to become available */ |
| 355 | result = -ENOMSG; |
| 356 | } |
| 357 | |
| 358 | SYS_PORT_TRACING_OBJ_FUNC(k_msgq, peek, msgq, result); |
| 359 | |
| 360 | k_spin_unlock(&msgq->lock, key); |
| 361 | |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | #ifdef CONFIG_USERSPACE |
| 366 | static inline int z_vrfy_k_msgq_peek_at(struct k_msgq *msgq, void *data, uint32_t idx) |
| 367 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 368 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
| 369 | K_OOPS(K_SYSCALL_MEMORY_WRITE(data, msgq->msg_size)); |
romain pelletant | 14bcc85 | 2022-12-28 13:06:16 +0100 | [diff] [blame] | 370 | |
| 371 | return z_impl_k_msgq_peek_at(msgq, data, idx); |
| 372 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 373 | #include <zephyr/syscalls/k_msgq_peek_at_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 374 | #endif /* CONFIG_USERSPACE */ |
romain pelletant | 14bcc85 | 2022-12-28 13:06:16 +0100 | [diff] [blame] | 375 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 376 | void z_impl_k_msgq_purge(struct k_msgq *msgq) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 377 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 378 | k_spinlock_key_t key; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 379 | struct k_thread *pending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 380 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 381 | key = k_spin_lock(&msgq->lock); |
| 382 | |
Torbjörn Leksell | 9ab447b | 2021-03-26 12:39:53 +0100 | [diff] [blame] | 383 | SYS_PORT_TRACING_OBJ_FUNC(k_msgq, purge, msgq); |
| 384 | |
Peter Mitsis | 340d00a | 2016-09-22 13:59:00 -0400 | [diff] [blame] | 385 | /* wake up any threads that are waiting to write */ |
Hess Nathan | 20b5542 | 2024-05-02 14:02:20 +0200 | [diff] [blame] | 386 | for (pending_thread = z_unpend_first_thread(&msgq->wait_q); pending_thread != NULL; |
| 387 | pending_thread = z_unpend_first_thread(&msgq->wait_q)) { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 388 | arch_thread_return_value_set(pending_thread, -ENOMSG); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 389 | z_ready_thread(pending_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 392 | msgq->used_msgs = 0; |
| 393 | msgq->read_ptr = msgq->write_ptr; |
Peter Mitsis | 340d00a | 2016-09-22 13:59:00 -0400 | [diff] [blame] | 394 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 395 | z_reschedule(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 396 | } |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 397 | |
| 398 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 399 | static inline void z_vrfy_k_msgq_purge(struct k_msgq *msgq) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 400 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 401 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 402 | z_impl_k_msgq_purge(msgq); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 403 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 404 | #include <zephyr/syscalls/k_msgq_purge_mrsh.c> |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 405 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 406 | static inline uint32_t z_vrfy_k_msgq_num_free_get(struct k_msgq *msgq) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 407 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 408 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 409 | return z_impl_k_msgq_num_free_get(msgq); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 410 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 411 | #include <zephyr/syscalls/k_msgq_num_free_get_mrsh.c> |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 412 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 413 | static inline uint32_t z_vrfy_k_msgq_num_used_get(struct k_msgq *msgq) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 414 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 415 | K_OOPS(K_SYSCALL_OBJ(msgq, K_OBJ_MSGQ)); |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 416 | return z_impl_k_msgq_num_used_get(msgq); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 417 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 418 | #include <zephyr/syscalls/k_msgq_num_used_get_mrsh.c> |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 419 | |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 420 | #endif /* CONFIG_USERSPACE */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 421 | |
| 422 | #ifdef CONFIG_OBJ_CORE_MSGQ |
| 423 | static int init_msgq_obj_core_list(void) |
| 424 | { |
| 425 | /* Initialize msgq object type */ |
| 426 | |
| 427 | z_obj_type_init(&obj_type_msgq, K_OBJ_TYPE_MSGQ_ID, |
| 428 | offsetof(struct k_msgq, obj_core)); |
| 429 | |
| 430 | /* Initialize and link statically defined message queues */ |
| 431 | |
| 432 | STRUCT_SECTION_FOREACH(k_msgq, msgq) { |
| 433 | k_obj_core_init_and_link(K_OBJ_CORE(msgq), &obj_type_msgq); |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | }; |
| 438 | |
| 439 | SYS_INIT(init_msgq_obj_core_list, PRE_KERNEL_1, |
| 440 | CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
| 441 | |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 442 | #endif /* CONFIG_OBJ_CORE_MSGQ */ |