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