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