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