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