blob: 3014af57063a3e63db2066e193be5ebf4a0dff32 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @brief Mailboxes.
9 */
10
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020011#include <zephyr/kernel.h>
12#include <zephyr/kernel_structs.h>
Anas Nashif4d994af2021-04-18 23:24:40 -040013
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020014#include <zephyr/toolchain.h>
15#include <zephyr/linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040016#include <string.h>
Stephanos Ioannidis2d746042019-10-25 00:08:21 +090017#include <ksched.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020018#include <zephyr/wait_q.h>
19#include <zephyr/sys/dlist.h>
20#include <zephyr/init.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
23
24/* asynchronous message descriptor type */
25struct k_mbox_async {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050026 struct _thread_base thread; /* dummy thread object */
Benjamin Walsh456c6da2016-09-02 18:55:39 -040027 struct k_mbox_msg tx_msg; /* transmit message descriptor */
28};
29
Benjamin Walsh456c6da2016-09-02 18:55:39 -040030/* stack of unused asynchronous message descriptors */
31K_STACK_DEFINE(async_msg_free, CONFIG_NUM_MBOX_ASYNC_MSGS);
32
Allan Stephense7d2cc22016-10-19 16:10:46 -050033/* allocate an asynchronous message descriptor */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -080034static inline void mbox_async_alloc(struct k_mbox_async **async)
Allan Stephense7d2cc22016-10-19 16:10:46 -050035{
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040036 (void)k_stack_pop(&async_msg_free, (stack_data_t *)async, K_FOREVER);
Allan Stephense7d2cc22016-10-19 16:10:46 -050037}
38
39/* free an asynchronous message descriptor */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -080040static inline void mbox_async_free(struct k_mbox_async *async)
Allan Stephense7d2cc22016-10-19 16:10:46 -050041{
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040042 k_stack_push(&async_msg_free, (stack_data_t)async);
Allan Stephense7d2cc22016-10-19 16:10:46 -050043}
44
45#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */
46
Anas Nashif4d994af2021-04-18 23:24:40 -040047#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
Allan Stephense7d2cc22016-10-19 16:10:46 -050048
49/*
50 * Do run-time initialization of mailbox object subsystem.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051 */
Gerard Marull-Paretasa5fd0d12022-10-19 09:33:44 +020052static int init_mbox_module(void)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053{
Flavio Ceolinac146852018-11-01 17:42:07 -070054 /* array of asynchronous message descriptors */
55 static struct k_mbox_async __noinit async_msg[CONFIG_NUM_MBOX_ASYNC_MSGS];
56
Allan Stephense7d2cc22016-10-19 16:10:46 -050057#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
58 /*
59 * Create pool of asynchronous message descriptors.
60 *
61 * A dummy thread requires minimal initialization, since it never gets
Benjamin Walsha8978ab2017-01-22 11:41:59 -050062 * to execute. The _THREAD_DUMMY flag is sufficient to distinguish a
63 * dummy thread from a real one. The threads are *not* added to the
64 * kernel's list of known threads.
Allan Stephense7d2cc22016-10-19 16:10:46 -050065 *
66 * Once initialized, the address of each descriptor is added to a stack
67 * that governs access to them.
68 */
69
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070 int i;
71
72 for (i = 0; i < CONFIG_NUM_MBOX_ASYNC_MSGS; i++) {
Patrik Flykt4344e272019-03-08 14:19:05 -070073 z_init_thread_base(&async_msg[i].thread, 0, _THREAD_DUMMY, 0);
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040074 k_stack_push(&async_msg_free, (stack_data_t)&async_msg[i]);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075 }
Allan Stephense7d2cc22016-10-19 16:10:46 -050076#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */
77
78 /* Complete initialization of statically defined mailboxes. */
79
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040080 return 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081}
82
Andrew Boie0b474ee2016-11-08 11:06:55 -080083SYS_INIT(init_mbox_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040084
Anas Nashif4d994af2021-04-18 23:24:40 -040085#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086
Anas Nashif25c87db2021-03-29 10:54:23 -040087void k_mbox_init(struct k_mbox *mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088{
Anas Nashif25c87db2021-03-29 10:54:23 -040089 z_waitq_init(&mbox->tx_msg_queue);
90 z_waitq_init(&mbox->rx_msg_queue);
91 mbox->lock = (struct k_spinlock) {};
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +010092
93 SYS_PORT_TRACING_OBJ_INIT(k_mbox, mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094}
95
96/**
97 * @brief Check compatibility of sender's and receiver's message descriptors.
98 *
99 * Compares sender's and receiver's message descriptors to see if they are
100 * compatible. If so, the descriptor fields are updated to reflect that a
101 * match has occurred.
102 *
103 * @param tx_msg Pointer to transmit message descriptor.
104 * @param rx_msg Pointer to receive message descriptor.
105 *
106 * @return 0 if successfully matched, otherwise -1.
107 */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800108static int mbox_message_match(struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109 struct k_mbox_msg *rx_msg)
110{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500111 uint32_t temp_info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112
113 if (((tx_msg->tx_target_thread == (k_tid_t)K_ANY) ||
114 (tx_msg->tx_target_thread == rx_msg->tx_target_thread)) &&
115 ((rx_msg->rx_source_thread == (k_tid_t)K_ANY) ||
116 (rx_msg->rx_source_thread == tx_msg->rx_source_thread))) {
117
118 /* update thread identifier fields for both descriptors */
119 rx_msg->rx_source_thread = tx_msg->rx_source_thread;
120 tx_msg->tx_target_thread = rx_msg->tx_target_thread;
121
122 /* update application info fields for both descriptors */
123 temp_info = rx_msg->info;
124 rx_msg->info = tx_msg->info;
125 tx_msg->info = temp_info;
126
127 /* update data size field for receiver only */
128 if (rx_msg->size > tx_msg->size) {
129 rx_msg->size = tx_msg->size;
130 }
131
132 /* update data location fields for receiver only */
133 rx_msg->tx_data = tx_msg->tx_data;
134 rx_msg->tx_block = tx_msg->tx_block;
135 if (rx_msg->tx_data != NULL) {
Andy Ross73cb9582017-05-09 10:42:39 -0700136 rx_msg->tx_block.data = NULL;
137 } else if (rx_msg->tx_block.data != NULL) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400138 rx_msg->tx_data = rx_msg->tx_block.data;
Flavio Ceolin3e97acc2018-09-25 11:24:28 -0700139 } else {
140 /* no data */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141 }
142
143 /* update syncing thread field for receiver only */
144 rx_msg->_syncing_thread = tx_msg->_syncing_thread;
145
146 return 0;
147 }
148
149 return -1;
150}
151
152/**
153 * @brief Dispose of received message.
154 *
155 * Releases any memory pool block still associated with the message,
156 * then notifies the sender that message processing is complete.
157 *
158 * @param rx_msg Pointer to receive message descriptor.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159 */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800160static void mbox_message_dispose(struct k_mbox_msg *rx_msg)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400161{
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400162 struct k_thread *sending_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400163 struct k_mbox_msg *tx_msg;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400164
165 /* do nothing if message was disposed of when it was received */
166 if (rx_msg->_syncing_thread == NULL) {
167 return;
168 }
169
Andy Ross73cb9582017-05-09 10:42:39 -0700170 if (rx_msg->tx_block.data != NULL) {
Andy Ross73cb9582017-05-09 10:42:39 -0700171 rx_msg->tx_block.data = NULL;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400172 }
173
174 /* recover sender info */
175 sending_thread = rx_msg->_syncing_thread;
176 rx_msg->_syncing_thread = NULL;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500177 tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400178
179 /* update data size field for sender */
180 tx_msg->size = rx_msg->size;
181
182#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
183 /*
184 * asynchronous send: free asynchronous message descriptor +
185 * dummy thread pair, then give semaphore (if needed)
186 */
Patrik Flykt24d71432019-03-26 19:57:45 -0600187 if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400188 struct k_sem *async_sem = tx_msg->_async_sem;
189
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800190 mbox_async_free((struct k_mbox_async *)sending_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400191 if (async_sem != NULL) {
192 k_sem_give(async_sem);
193 }
194 return;
195 }
196#endif
197
198 /* synchronous send: wake up sending thread */
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800199 arch_thread_return_value_set(sending_thread, 0);
Patrik Flykt4344e272019-03-08 14:19:05 -0700200 z_mark_thread_as_not_pending(sending_thread);
201 z_ready_thread(sending_thread);
202 z_reschedule_unlocked();
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400203}
204
205/**
206 * @brief Send a mailbox message.
207 *
208 * Helper routine that handles both synchronous and asynchronous sends.
209 *
210 * @param mbox Pointer to the mailbox object.
211 * @param tx_msg Pointer to transmit message descriptor.
Peter Mitsis40680f62016-10-14 10:04:55 -0400212 * @param timeout Maximum time (milliseconds) to wait for the message to be
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213 * received (although not necessarily completely processed).
214 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
215 * as necessary.
216 *
217 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
218 */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800219static int mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Andy Ross78327382020-03-05 15:18:14 -0800220 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400221{
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400222 struct k_thread *sending_thread;
Andy Rossccf3bf72018-05-10 11:10:34 -0700223 struct k_thread *receiving_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224 struct k_mbox_msg *rx_msg;
Andy Ross9eeb6b82018-07-25 15:06:24 -0700225 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226
227 /* save sender id so it can be used during message matching */
228 tx_msg->rx_source_thread = _current;
229
230 /* finish readying sending thread (actual or dummy) for send */
231 sending_thread = tx_msg->_syncing_thread;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500232 sending_thread->base.swap_data = tx_msg;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400233
234 /* search mailbox's rx queue for a compatible receiver */
Andy Ross9eeb6b82018-07-25 15:06:24 -0700235 key = k_spin_lock(&mbox->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400236
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100237 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, message_put, mbox, timeout);
238
Andy Rossccf3bf72018-05-10 11:10:34 -0700239 _WAIT_Q_FOR_EACH(&mbox->rx_msg_queue, receiving_thread) {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500240 rx_msg = (struct k_mbox_msg *)receiving_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400241
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800242 if (mbox_message_match(tx_msg, rx_msg) == 0) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400243 /* take receiver out of rx queue */
Patrik Flykt4344e272019-03-08 14:19:05 -0700244 z_unpend_thread(receiving_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245
246 /* ready receiver for execution */
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800247 arch_thread_return_value_set(receiving_thread, 0);
Patrik Flykt4344e272019-03-08 14:19:05 -0700248 z_ready_thread(receiving_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249
250#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
251 /*
252 * asynchronous send: swap out current thread
253 * if receiver has priority, otherwise let it continue
254 *
255 * note: dummy sending thread sits (unqueued)
256 * until the receiver consumes the message
257 */
Flavio Ceolin76b35182018-12-16 12:48:29 -0800258 if ((sending_thread->base.thread_state & _THREAD_DUMMY)
Patrik Flykt24d71432019-03-26 19:57:45 -0600259 != 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700260 z_reschedule(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261 return 0;
262 }
263#endif
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100264 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265
266 /*
267 * synchronous send: pend current thread (unqueued)
268 * until the receiver consumes the message
269 */
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100270 int ret = z_pend_curr(&mbox->lock, key, NULL, K_FOREVER);
Andy Rosse0a572b2018-03-26 11:58:10 -0700271
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100272 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret);
273
274 return ret;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275 }
276 }
277
278 /* didn't find a matching receiver: don't wait for one */
Andy Ross78327382020-03-05 15:18:14 -0800279 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100280 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, -ENOMSG);
281
Andy Ross9eeb6b82018-07-25 15:06:24 -0700282 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400283 return -ENOMSG;
284 }
285
286#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
287 /* asynchronous send: dummy thread waits on tx queue for receiver */
Patrik Flykt24d71432019-03-26 19:57:45 -0600288 if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700289 z_pend_thread(sending_thread, &mbox->tx_msg_queue, K_FOREVER);
Andy Ross9eeb6b82018-07-25 15:06:24 -0700290 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400291 return 0;
292 }
293#endif
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100294 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400295
296 /* synchronous send: sender waits on tx queue for receiver or timeout */
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100297 int ret = z_pend_curr(&mbox->lock, key, &mbox->tx_msg_queue, timeout);
298
299 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret);
300
301 return ret;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400302}
303
Andy Ross78327382020-03-05 15:18:14 -0800304int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
305 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400306{
307 /* configure things for a synchronous send, then send the message */
308 tx_msg->_syncing_thread = _current;
309
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100310 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, put, mbox, timeout);
311
312 int ret = mbox_message_put(mbox, tx_msg, timeout);
313
314 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, put, mbox, timeout, ret);
315
316 return ret;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400317}
318
319#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400320void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
321 struct k_sem *sem)
322{
323 struct k_mbox_async *async;
324
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100325 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, async_put, mbox, sem);
326
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400327 /*
328 * allocate an asynchronous message descriptor, configure both parts,
329 * then send the message asynchronously
330 */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800331 mbox_async_alloc(&async);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500333 async->thread.prio = _current->base.prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400334
335 async->tx_msg = *tx_msg;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400336 async->tx_msg._syncing_thread = (struct k_thread *)&async->thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400337 async->tx_msg._async_sem = sem;
338
Flavio Ceolin0a447842018-09-13 11:24:30 -0700339 (void)mbox_message_put(mbox, &async->tx_msg, K_FOREVER);
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100340 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, async_put, mbox, sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400341}
342#endif
343
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400344void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer)
345{
346 /* handle case where data is to be discarded */
347 if (buffer == NULL) {
348 rx_msg->size = 0;
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800349 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400350 return;
351 }
352
353 /* copy message data to buffer, then dispose of message */
Anas Nashifbbbc38b2021-03-29 10:03:49 -0400354 if ((rx_msg->tx_data != NULL) && (rx_msg->size > 0U)) {
Flavio Ceolin66994232018-08-13 15:17:04 -0700355 (void)memcpy(buffer, rx_msg->tx_data, rx_msg->size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356 }
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800357 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358}
359
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400360/**
361 * @brief Handle immediate consumption of received mailbox message data.
362 *
363 * Checks to see if received message data should be kept for later retrieval,
364 * or if the data should consumed immediately and the message disposed of.
365 *
366 * The data is consumed immediately in either of the following cases:
Stefan Eicherfbe7d722021-08-23 11:36:22 +0000367 * 1) The receiver requested immediate retrieval by supplying a buffer
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400368 * to receive the data.
369 * 2) There is no data to be retrieved. (i.e. Data size is 0 bytes.)
370 *
371 * @param rx_msg Pointer to receive message descriptor.
372 * @param buffer Pointer to buffer to receive data.
373 *
374 * @return 0
375 */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800376static int mbox_message_data_check(struct k_mbox_msg *rx_msg, void *buffer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400377{
378 if (buffer != NULL) {
379 /* retrieve data now, then dispose of message */
380 k_mbox_data_get(rx_msg, buffer);
Anas Nashifbbbc38b2021-03-29 10:03:49 -0400381 } else if (rx_msg->size == 0U) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400382 /* there is no data to get, so just dispose of message */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800383 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384 } else {
385 /* keep message around for later data retrieval */
386 }
387
388 return 0;
389}
390
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400391int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer,
Andy Ross78327382020-03-05 15:18:14 -0800392 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400393{
Andy Rossccf3bf72018-05-10 11:10:34 -0700394 struct k_thread *sending_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400395 struct k_mbox_msg *tx_msg;
Andy Ross9eeb6b82018-07-25 15:06:24 -0700396 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400397 int result;
398
399 /* save receiver id so it can be used during message matching */
400 rx_msg->tx_target_thread = _current;
401
402 /* search mailbox's tx queue for a compatible sender */
Andy Ross9eeb6b82018-07-25 15:06:24 -0700403 key = k_spin_lock(&mbox->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400404
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100405 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, get, mbox, timeout);
406
Andy Rossccf3bf72018-05-10 11:10:34 -0700407 _WAIT_Q_FOR_EACH(&mbox->tx_msg_queue, sending_thread) {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500408 tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400409
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800410 if (mbox_message_match(tx_msg, rx_msg) == 0) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400411 /* take sender out of mailbox's tx queue */
Patrik Flykt4344e272019-03-08 14:19:05 -0700412 z_unpend_thread(sending_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413
Andy Ross9eeb6b82018-07-25 15:06:24 -0700414 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400415
416 /* consume message data immediately, if needed */
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100417 result = mbox_message_data_check(rx_msg, buffer);
418
419 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, result);
420 return result;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421 }
422 }
423
424 /* didn't find a matching sender */
425
Andy Ross78327382020-03-05 15:18:14 -0800426 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100427 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, -ENOMSG);
428
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400429 /* don't wait for a matching sender to appear */
Andy Ross9eeb6b82018-07-25 15:06:24 -0700430 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400431 return -ENOMSG;
432 }
433
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100434 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, get, mbox, timeout);
435
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400436 /* wait until a matching sender appears or a timeout occurs */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500437 _current->base.swap_data = rx_msg;
Patrik Flykt4344e272019-03-08 14:19:05 -0700438 result = z_pend_curr(&mbox->lock, key, &mbox->rx_msg_queue, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439
440 /* consume message data immediately, if needed */
441 if (result == 0) {
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800442 result = mbox_message_data_check(rx_msg, buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400443 }
444
Torbjörn Lekselld2e7de52021-03-26 12:47:13 +0100445 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, get, mbox, timeout, result);
446
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400447 return result;
448}