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 | * @brief Mailboxes. |
| 9 | */ |
| 10 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 11 | #include <zephyr/kernel.h> |
| 12 | #include <zephyr/kernel_structs.h> |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 13 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 14 | #include <zephyr/toolchain.h> |
| 15 | #include <zephyr/linker/sections.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 16 | #include <string.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 17 | #include <ksched.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 18 | #include <zephyr/wait_q.h> |
| 19 | #include <zephyr/sys/dlist.h> |
| 20 | #include <zephyr/init.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 21 | |
| 22 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 23 | |
| 24 | /* asynchronous message descriptor type */ |
| 25 | struct k_mbox_async { |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 26 | struct _thread_base thread; /* dummy thread object */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 27 | struct k_mbox_msg tx_msg; /* transmit message descriptor */ |
| 28 | }; |
| 29 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 30 | /* stack of unused asynchronous message descriptors */ |
| 31 | K_STACK_DEFINE(async_msg_free, CONFIG_NUM_MBOX_ASYNC_MSGS); |
| 32 | |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 33 | /* allocate an asynchronous message descriptor */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 34 | static inline void mbox_async_alloc(struct k_mbox_async **async) |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 35 | { |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 36 | (void)k_stack_pop(&async_msg_free, (stack_data_t *)async, K_FOREVER); |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* free an asynchronous message descriptor */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 40 | static inline void mbox_async_free(struct k_mbox_async *async) |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 41 | { |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 42 | k_stack_push(&async_msg_free, (stack_data_t)async); |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */ |
| 46 | |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 47 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Do run-time initialization of mailbox object subsystem. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 51 | */ |
Gerard Marull-Paretas | a5fd0d1 | 2022-10-19 09:33:44 +0200 | [diff] [blame] | 52 | static int init_mbox_module(void) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 53 | { |
Flavio Ceolin | ac14685 | 2018-11-01 17:42:07 -0700 | [diff] [blame] | 54 | /* array of asynchronous message descriptors */ |
| 55 | static struct k_mbox_async __noinit async_msg[CONFIG_NUM_MBOX_ASYNC_MSGS]; |
| 56 | |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 57 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 58 | /* |
| 59 | * Create pool of asynchronous message descriptors. |
| 60 | * |
| 61 | * A dummy thread requires minimal initialization, since it never gets |
Benjamin Walsh | a8978ab | 2017-01-22 11:41:59 -0500 | [diff] [blame] | 62 | * to execute. The _THREAD_DUMMY flag is sufficient to distinguish a |
| 63 | * dummy thread from a real one. The threads are *not* added to the |
| 64 | * kernel's list of known threads. |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 65 | * |
| 66 | * Once initialized, the address of each descriptor is added to a stack |
| 67 | * that governs access to them. |
| 68 | */ |
| 69 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 70 | int i; |
| 71 | |
| 72 | for (i = 0; i < CONFIG_NUM_MBOX_ASYNC_MSGS; i++) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 73 | z_init_thread_base(&async_msg[i].thread, 0, _THREAD_DUMMY, 0); |
Nicolas Pitre | 3d51f7c | 2019-05-17 22:48:26 -0400 | [diff] [blame] | 74 | k_stack_push(&async_msg_free, (stack_data_t)&async_msg[i]); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 75 | } |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 76 | #endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */ |
| 77 | |
| 78 | /* Complete initialization of statically defined mailboxes. */ |
| 79 | |
Dmitriy Korovkin | 284042d | 2016-09-09 11:24:27 -0400 | [diff] [blame] | 80 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 83 | SYS_INIT(init_mbox_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Dmitriy Korovkin | 284042d | 2016-09-09 11:24:27 -0400 | [diff] [blame] | 84 | |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 85 | #endif /* CONFIG_NUM_MBOX_ASYNC_MSGS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 86 | |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 87 | void k_mbox_init(struct k_mbox *mbox) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 88 | { |
Anas Nashif | 25c87db | 2021-03-29 10:54:23 -0400 | [diff] [blame] | 89 | z_waitq_init(&mbox->tx_msg_queue); |
| 90 | z_waitq_init(&mbox->rx_msg_queue); |
| 91 | mbox->lock = (struct k_spinlock) {}; |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 92 | |
| 93 | SYS_PORT_TRACING_OBJ_INIT(k_mbox, mbox); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @brief Check compatibility of sender's and receiver's message descriptors. |
| 98 | * |
| 99 | * Compares sender's and receiver's message descriptors to see if they are |
| 100 | * compatible. If so, the descriptor fields are updated to reflect that a |
| 101 | * match has occurred. |
| 102 | * |
| 103 | * @param tx_msg Pointer to transmit message descriptor. |
| 104 | * @param rx_msg Pointer to receive message descriptor. |
| 105 | * |
| 106 | * @return 0 if successfully matched, otherwise -1. |
| 107 | */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 108 | static int mbox_message_match(struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 109 | struct k_mbox_msg *rx_msg) |
| 110 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 111 | uint32_t temp_info; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 112 | |
| 113 | if (((tx_msg->tx_target_thread == (k_tid_t)K_ANY) || |
| 114 | (tx_msg->tx_target_thread == rx_msg->tx_target_thread)) && |
| 115 | ((rx_msg->rx_source_thread == (k_tid_t)K_ANY) || |
| 116 | (rx_msg->rx_source_thread == tx_msg->rx_source_thread))) { |
| 117 | |
| 118 | /* update thread identifier fields for both descriptors */ |
| 119 | rx_msg->rx_source_thread = tx_msg->rx_source_thread; |
| 120 | tx_msg->tx_target_thread = rx_msg->tx_target_thread; |
| 121 | |
| 122 | /* update application info fields for both descriptors */ |
| 123 | temp_info = rx_msg->info; |
| 124 | rx_msg->info = tx_msg->info; |
| 125 | tx_msg->info = temp_info; |
| 126 | |
| 127 | /* update data size field for receiver only */ |
| 128 | if (rx_msg->size > tx_msg->size) { |
| 129 | rx_msg->size = tx_msg->size; |
| 130 | } |
| 131 | |
| 132 | /* update data location fields for receiver only */ |
| 133 | rx_msg->tx_data = tx_msg->tx_data; |
| 134 | rx_msg->tx_block = tx_msg->tx_block; |
| 135 | if (rx_msg->tx_data != NULL) { |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 136 | rx_msg->tx_block.data = NULL; |
| 137 | } else if (rx_msg->tx_block.data != NULL) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 138 | rx_msg->tx_data = rx_msg->tx_block.data; |
Flavio Ceolin | 3e97acc | 2018-09-25 11:24:28 -0700 | [diff] [blame] | 139 | } else { |
| 140 | /* no data */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* update syncing thread field for receiver only */ |
| 144 | rx_msg->_syncing_thread = tx_msg->_syncing_thread; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @brief Dispose of received message. |
| 154 | * |
| 155 | * Releases any memory pool block still associated with the message, |
| 156 | * then notifies the sender that message processing is complete. |
| 157 | * |
| 158 | * @param rx_msg Pointer to receive message descriptor. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 159 | */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 160 | static void mbox_message_dispose(struct k_mbox_msg *rx_msg) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 161 | { |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 162 | struct k_thread *sending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 163 | struct k_mbox_msg *tx_msg; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 164 | |
| 165 | /* do nothing if message was disposed of when it was received */ |
| 166 | if (rx_msg->_syncing_thread == NULL) { |
| 167 | return; |
| 168 | } |
| 169 | |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 170 | if (rx_msg->tx_block.data != NULL) { |
Andy Ross | 73cb958 | 2017-05-09 10:42:39 -0700 | [diff] [blame] | 171 | rx_msg->tx_block.data = NULL; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* recover sender info */ |
| 175 | sending_thread = rx_msg->_syncing_thread; |
| 176 | rx_msg->_syncing_thread = NULL; |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 177 | tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 178 | |
| 179 | /* update data size field for sender */ |
| 180 | tx_msg->size = rx_msg->size; |
| 181 | |
| 182 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 183 | /* |
| 184 | * asynchronous send: free asynchronous message descriptor + |
| 185 | * dummy thread pair, then give semaphore (if needed) |
| 186 | */ |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 187 | if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 188 | struct k_sem *async_sem = tx_msg->_async_sem; |
| 189 | |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 190 | mbox_async_free((struct k_mbox_async *)sending_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 191 | if (async_sem != NULL) { |
| 192 | k_sem_give(async_sem); |
| 193 | } |
| 194 | return; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | /* synchronous send: wake up sending thread */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 199 | arch_thread_return_value_set(sending_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 200 | z_mark_thread_as_not_pending(sending_thread); |
| 201 | z_ready_thread(sending_thread); |
| 202 | z_reschedule_unlocked(); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @brief Send a mailbox message. |
| 207 | * |
| 208 | * Helper routine that handles both synchronous and asynchronous sends. |
| 209 | * |
| 210 | * @param mbox Pointer to the mailbox object. |
| 211 | * @param tx_msg Pointer to transmit message descriptor. |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 212 | * @param timeout Maximum time (milliseconds) to wait for the message to be |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 213 | * received (although not necessarily completely processed). |
| 214 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long |
| 215 | * as necessary. |
| 216 | * |
| 217 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 218 | */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 219 | static int mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 220 | k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 221 | { |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 222 | struct k_thread *sending_thread; |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 223 | struct k_thread *receiving_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 224 | struct k_mbox_msg *rx_msg; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 225 | k_spinlock_key_t key; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 226 | |
| 227 | /* save sender id so it can be used during message matching */ |
| 228 | tx_msg->rx_source_thread = _current; |
| 229 | |
| 230 | /* finish readying sending thread (actual or dummy) for send */ |
| 231 | sending_thread = tx_msg->_syncing_thread; |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 232 | sending_thread->base.swap_data = tx_msg; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 233 | |
| 234 | /* search mailbox's rx queue for a compatible receiver */ |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 235 | key = k_spin_lock(&mbox->lock); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 236 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 237 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, message_put, mbox, timeout); |
| 238 | |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 239 | _WAIT_Q_FOR_EACH(&mbox->rx_msg_queue, receiving_thread) { |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 240 | rx_msg = (struct k_mbox_msg *)receiving_thread->base.swap_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 241 | |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 242 | if (mbox_message_match(tx_msg, rx_msg) == 0) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 243 | /* take receiver out of rx queue */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 244 | z_unpend_thread(receiving_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 245 | |
| 246 | /* ready receiver for execution */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 247 | arch_thread_return_value_set(receiving_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 248 | z_ready_thread(receiving_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 249 | |
| 250 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 251 | /* |
| 252 | * asynchronous send: swap out current thread |
| 253 | * if receiver has priority, otherwise let it continue |
| 254 | * |
| 255 | * note: dummy sending thread sits (unqueued) |
| 256 | * until the receiver consumes the message |
| 257 | */ |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 258 | if ((sending_thread->base.thread_state & _THREAD_DUMMY) |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 259 | != 0U) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 260 | z_reschedule(&mbox->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | #endif |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 264 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * synchronous send: pend current thread (unqueued) |
| 268 | * until the receiver consumes the message |
| 269 | */ |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 270 | int ret = z_pend_curr(&mbox->lock, key, NULL, K_FOREVER); |
Andy Ross | e0a572b | 2018-03-26 11:58:10 -0700 | [diff] [blame] | 271 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 272 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret); |
| 273 | |
| 274 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | /* didn't find a matching receiver: don't wait for one */ |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 279 | if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 280 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, -ENOMSG); |
| 281 | |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 282 | k_spin_unlock(&mbox->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 283 | return -ENOMSG; |
| 284 | } |
| 285 | |
| 286 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 287 | /* asynchronous send: dummy thread waits on tx queue for receiver */ |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 288 | if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 289 | z_pend_thread(sending_thread, &mbox->tx_msg_queue, K_FOREVER); |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 290 | k_spin_unlock(&mbox->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | #endif |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 294 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 295 | |
| 296 | /* synchronous send: sender waits on tx queue for receiver or timeout */ |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 297 | int ret = z_pend_curr(&mbox->lock, key, &mbox->tx_msg_queue, timeout); |
| 298 | |
| 299 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret); |
| 300 | |
| 301 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 304 | int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
| 305 | k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 306 | { |
| 307 | /* configure things for a synchronous send, then send the message */ |
| 308 | tx_msg->_syncing_thread = _current; |
| 309 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 310 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, put, mbox, timeout); |
| 311 | |
| 312 | int ret = mbox_message_put(mbox, tx_msg, timeout); |
| 313 | |
| 314 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, put, mbox, timeout, ret); |
| 315 | |
| 316 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 320 | void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
| 321 | struct k_sem *sem) |
| 322 | { |
| 323 | struct k_mbox_async *async; |
| 324 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 325 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, async_put, mbox, sem); |
| 326 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 327 | /* |
| 328 | * allocate an asynchronous message descriptor, configure both parts, |
| 329 | * then send the message asynchronously |
| 330 | */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 331 | mbox_async_alloc(&async); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 332 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 333 | async->thread.prio = _current->base.prio; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 334 | |
| 335 | async->tx_msg = *tx_msg; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 336 | async->tx_msg._syncing_thread = (struct k_thread *)&async->thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 337 | async->tx_msg._async_sem = sem; |
| 338 | |
Flavio Ceolin | 0a44784 | 2018-09-13 11:24:30 -0700 | [diff] [blame] | 339 | (void)mbox_message_put(mbox, &async->tx_msg, K_FOREVER); |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 340 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, async_put, mbox, sem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 341 | } |
| 342 | #endif |
| 343 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 344 | void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer) |
| 345 | { |
| 346 | /* handle case where data is to be discarded */ |
| 347 | if (buffer == NULL) { |
| 348 | rx_msg->size = 0; |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 349 | mbox_message_dispose(rx_msg); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* copy message data to buffer, then dispose of message */ |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 354 | if ((rx_msg->tx_data != NULL) && (rx_msg->size > 0U)) { |
Flavio Ceolin | 6699423 | 2018-08-13 15:17:04 -0700 | [diff] [blame] | 355 | (void)memcpy(buffer, rx_msg->tx_data, rx_msg->size); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 356 | } |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 357 | mbox_message_dispose(rx_msg); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 360 | /** |
| 361 | * @brief Handle immediate consumption of received mailbox message data. |
| 362 | * |
| 363 | * Checks to see if received message data should be kept for later retrieval, |
| 364 | * or if the data should consumed immediately and the message disposed of. |
| 365 | * |
| 366 | * The data is consumed immediately in either of the following cases: |
Stefan Eicher | fbe7d72 | 2021-08-23 11:36:22 +0000 | [diff] [blame] | 367 | * 1) The receiver requested immediate retrieval by supplying a buffer |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 368 | * to receive the data. |
| 369 | * 2) There is no data to be retrieved. (i.e. Data size is 0 bytes.) |
| 370 | * |
| 371 | * @param rx_msg Pointer to receive message descriptor. |
| 372 | * @param buffer Pointer to buffer to receive data. |
| 373 | * |
| 374 | * @return 0 |
| 375 | */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 376 | static int mbox_message_data_check(struct k_mbox_msg *rx_msg, void *buffer) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 377 | { |
| 378 | if (buffer != NULL) { |
| 379 | /* retrieve data now, then dispose of message */ |
| 380 | k_mbox_data_get(rx_msg, buffer); |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 381 | } else if (rx_msg->size == 0U) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 382 | /* there is no data to get, so just dispose of message */ |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 383 | mbox_message_dispose(rx_msg); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 384 | } else { |
| 385 | /* keep message around for later data retrieval */ |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 391 | int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer, |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 392 | k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 393 | { |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 394 | struct k_thread *sending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 395 | struct k_mbox_msg *tx_msg; |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 396 | k_spinlock_key_t key; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 397 | int result; |
| 398 | |
| 399 | /* save receiver id so it can be used during message matching */ |
| 400 | rx_msg->tx_target_thread = _current; |
| 401 | |
| 402 | /* search mailbox's tx queue for a compatible sender */ |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 403 | key = k_spin_lock(&mbox->lock); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 404 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 405 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, get, mbox, timeout); |
| 406 | |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame] | 407 | _WAIT_Q_FOR_EACH(&mbox->tx_msg_queue, sending_thread) { |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 408 | tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 409 | |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 410 | if (mbox_message_match(tx_msg, rx_msg) == 0) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 411 | /* take sender out of mailbox's tx queue */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 412 | z_unpend_thread(sending_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 413 | |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 414 | k_spin_unlock(&mbox->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 415 | |
| 416 | /* consume message data immediately, if needed */ |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 417 | result = mbox_message_data_check(rx_msg, buffer); |
| 418 | |
| 419 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, result); |
| 420 | return result; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | /* didn't find a matching sender */ |
| 425 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 426 | if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 427 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, -ENOMSG); |
| 428 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 429 | /* don't wait for a matching sender to appear */ |
Andy Ross | 9eeb6b8 | 2018-07-25 15:06:24 -0700 | [diff] [blame] | 430 | k_spin_unlock(&mbox->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 431 | return -ENOMSG; |
| 432 | } |
| 433 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 434 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, get, mbox, timeout); |
| 435 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 436 | /* wait until a matching sender appears or a timeout occurs */ |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 437 | _current->base.swap_data = rx_msg; |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 438 | result = z_pend_curr(&mbox->lock, key, &mbox->rx_msg_queue, timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 439 | |
| 440 | /* consume message data immediately, if needed */ |
| 441 | if (result == 0) { |
Leandro Pereira | a1ae845 | 2018-03-06 15:08:55 -0800 | [diff] [blame] | 442 | result = mbox_message_data_check(rx_msg, buffer); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Torbjörn Leksell | d2e7de5 | 2021-03-26 12:47:13 +0100 | [diff] [blame] | 445 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, result); |
| 446 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 447 | return result; |
| 448 | } |