blob: cd9016feb9e166b976cea2963f1f08237aa3feb9 [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
11#include <kernel.h>
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050012#include <kernel_structs.h>
Anas Nashif569f0b42016-12-17 13:18:45 -050013#include <debug/object_tracing_common.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040014#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040015#include <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>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018#include <wait_q.h>
Anas Nashifee9dd1a2019-06-26 10:33:41 -040019#include <sys/dlist.h>
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040020#include <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
Maciek Borzecki059544d2017-05-18 12:16:45 +020047#ifdef CONFIG_OBJECT_TRACING
Allan Stephense7d2cc22016-10-19 16:10:46 -050048struct k_mbox *_trace_list_k_mbox;
Maciek Borzecki059544d2017-05-18 12:16:45 +020049#endif /* CONFIG_OBJECT_TRACING */
Allan Stephense7d2cc22016-10-19 16:10:46 -050050
51#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) || \
Anas Nashif2f203c22016-12-18 06:57:45 -050052 defined(CONFIG_OBJECT_TRACING)
Allan Stephense7d2cc22016-10-19 16:10:46 -050053
54/*
55 * Do run-time initialization of mailbox object subsystem.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040056 */
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040057static int init_mbox_module(struct device *dev)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058{
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040059 ARG_UNUSED(dev);
60
Flavio Ceolinac146852018-11-01 17:42:07 -070061 /* array of asynchronous message descriptors */
62 static struct k_mbox_async __noinit async_msg[CONFIG_NUM_MBOX_ASYNC_MSGS];
63
Allan Stephense7d2cc22016-10-19 16:10:46 -050064#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 Walsha8978ab2017-01-22 11:41:59 -050069 * 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 Stephense7d2cc22016-10-19 16:10:46 -050072 *
73 * Once initialized, the address of each descriptor is added to a stack
74 * that governs access to them.
75 */
76
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077 int i;
78
79 for (i = 0; i < CONFIG_NUM_MBOX_ASYNC_MSGS; i++) {
Patrik Flykt4344e272019-03-08 14:19:05 -070080 z_init_thread_base(&async_msg[i].thread, 0, _THREAD_DUMMY, 0);
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -040081 k_stack_push(&async_msg_free, (stack_data_t)&async_msg[i]);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082 }
Allan Stephense7d2cc22016-10-19 16:10:46 -050083#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */
84
85 /* Complete initialization of statically defined mailboxes. */
86
Anas Nashif2f203c22016-12-18 06:57:45 -050087#ifdef CONFIG_OBJECT_TRACING
Nicolas Pitreaa9228852019-06-03 13:01:43 -040088 Z_STRUCT_SECTION_FOREACH(k_mbox, mbox) {
Allan Stephense7d2cc22016-10-19 16:10:46 -050089 SYS_TRACING_OBJ_INIT(k_mbox, mbox);
90 }
Anas Nashif2f203c22016-12-18 06:57:45 -050091#endif /* CONFIG_OBJECT_TRACING */
Allan Stephense7d2cc22016-10-19 16:10:46 -050092
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040093 return 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094}
95
Andrew Boie0b474ee2016-11-08 11:06:55 -080096SYS_INIT(init_mbox_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
Dmitriy Korovkin284042d2016-09-09 11:24:27 -040097
Anas Nashif2f203c22016-12-18 06:57:45 -050098#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS or CONFIG_OBJECT_TRACING */
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100void k_mbox_init(struct k_mbox *mbox_ptr)
101{
Patrik Flykt4344e272019-03-08 14:19:05 -0700102 z_waitq_init(&mbox_ptr->tx_msg_queue);
103 z_waitq_init(&mbox_ptr->rx_msg_queue);
Andy Ross9eeb6b82018-07-25 15:06:24 -0700104 mbox_ptr->lock = (struct k_spinlock) {};
Allan Stephense7d2cc22016-10-19 16:10:46 -0500105 SYS_TRACING_OBJ_INIT(k_mbox, mbox_ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400106}
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 Pereiraa1ae8452018-03-06 15:08:55 -0800120static int mbox_message_match(struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121 struct k_mbox_msg *rx_msg)
122{
Kumar Galacc334c72017-04-21 10:55:34 -0500123 u32_t temp_info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124
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 Ross73cb9582017-05-09 10:42:39 -0700148 rx_msg->tx_block.data = NULL;
149 } else if (rx_msg->tx_block.data != NULL) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400150 rx_msg->tx_data = rx_msg->tx_block.data;
Flavio Ceolin3e97acc2018-09-25 11:24:28 -0700151 } else {
152 /* no data */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400153 }
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 Pereiraa1ae8452018-03-06 15:08:55 -0800174static void mbox_message_dispose(struct k_mbox_msg *rx_msg)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400175{
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400176 struct k_thread *sending_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400177 struct k_mbox_msg *tx_msg;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400178
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 Ross73cb9582017-05-09 10:42:39 -0700185 if (rx_msg->tx_block.data != NULL) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400186 k_mem_pool_free(&rx_msg->tx_block);
Andy Ross73cb9582017-05-09 10:42:39 -0700187 rx_msg->tx_block.data = NULL;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400188 }
189
190 /* recover sender info */
191 sending_thread = rx_msg->_syncing_thread;
192 rx_msg->_syncing_thread = NULL;
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500193 tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194
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 Flykt24d71432019-03-26 19:57:45 -0600203 if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204 struct k_sem *async_sem = tx_msg->_async_sem;
205
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800206 mbox_async_free((struct k_mbox_async *)sending_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207 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 Boie4f77c2a2019-11-07 12:43:29 -0800215 arch_thread_return_value_set(sending_thread, 0);
Patrik Flykt4344e272019-03-08 14:19:05 -0700216 z_mark_thread_as_not_pending(sending_thread);
217 z_ready_thread(sending_thread);
218 z_reschedule_unlocked();
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400219}
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 Mitsis40680f62016-10-14 10:04:55 -0400228 * @param timeout Maximum time (milliseconds) to wait for the message to be
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400229 * 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 Pereiraa1ae8452018-03-06 15:08:55 -0800235static int mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Andy Ross78327382020-03-05 15:18:14 -0800236 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400237{
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400238 struct k_thread *sending_thread;
Andy Rossccf3bf72018-05-10 11:10:34 -0700239 struct k_thread *receiving_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400240 struct k_mbox_msg *rx_msg;
Andy Ross9eeb6b82018-07-25 15:06:24 -0700241 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400242
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 Walshf6ca7de2016-11-08 10:36:50 -0500248 sending_thread->base.swap_data = tx_msg;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249
250 /* search mailbox's rx queue for a compatible receiver */
Andy Ross9eeb6b82018-07-25 15:06:24 -0700251 key = k_spin_lock(&mbox->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400252
Andy Rossccf3bf72018-05-10 11:10:34 -0700253 _WAIT_Q_FOR_EACH(&mbox->rx_msg_queue, receiving_thread) {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500254 rx_msg = (struct k_mbox_msg *)receiving_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400255
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800256 if (mbox_message_match(tx_msg, rx_msg) == 0) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400257 /* take receiver out of rx queue */
Patrik Flykt4344e272019-03-08 14:19:05 -0700258 z_unpend_thread(receiving_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259
260 /* ready receiver for execution */
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800261 arch_thread_return_value_set(receiving_thread, 0);
Patrik Flykt4344e272019-03-08 14:19:05 -0700262 z_ready_thread(receiving_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400263
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 Ceolin76b35182018-12-16 12:48:29 -0800272 if ((sending_thread->base.thread_state & _THREAD_DUMMY)
Patrik Flykt24d71432019-03-26 19:57:45 -0600273 != 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700274 z_reschedule(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275 return 0;
276 }
277#endif
278
279 /*
280 * synchronous send: pend current thread (unqueued)
281 * until the receiver consumes the message
282 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700283 return z_pend_curr(&mbox->lock, key, NULL, K_FOREVER);
Andy Rosse0a572b2018-03-26 11:58:10 -0700284
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400285 }
286 }
287
288 /* didn't find a matching receiver: don't wait for one */
Andy Ross78327382020-03-05 15:18:14 -0800289 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Andy Ross9eeb6b82018-07-25 15:06:24 -0700290 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400291 return -ENOMSG;
292 }
293
294#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
295 /* asynchronous send: dummy thread waits on tx queue for receiver */
Patrik Flykt24d71432019-03-26 19:57:45 -0600296 if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700297 z_pend_thread(sending_thread, &mbox->tx_msg_queue, K_FOREVER);
Andy Ross9eeb6b82018-07-25 15:06:24 -0700298 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400299 return 0;
300 }
301#endif
302
303 /* synchronous send: sender waits on tx queue for receiver or timeout */
Patrik Flykt4344e272019-03-08 14:19:05 -0700304 return z_pend_curr(&mbox->lock, key, &mbox->tx_msg_queue, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400305}
306
Andy Ross78327382020-03-05 15:18:14 -0800307int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
308 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400309{
310 /* configure things for a synchronous send, then send the message */
311 tx_msg->_syncing_thread = _current;
312
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800313 return mbox_message_put(mbox, tx_msg, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314}
315
316#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400317void 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 Pereiraa1ae8452018-03-06 15:08:55 -0800326 mbox_async_alloc(&async);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400327
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500328 async->thread.prio = _current->base.prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400329
330 async->tx_msg = *tx_msg;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400331 async->tx_msg._syncing_thread = (struct k_thread *)&async->thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332 async->tx_msg._async_sem = sem;
333
Flavio Ceolin0a447842018-09-13 11:24:30 -0700334 (void)mbox_message_put(mbox, &async->tx_msg, K_FOREVER);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400335}
336#endif
337
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400338void 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 Pereiraa1ae8452018-03-06 15:08:55 -0800343 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400344 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 Ceolin66994232018-08-13 15:17:04 -0700349 (void)memcpy(buffer, rx_msg->tx_data, rx_msg->size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400350 }
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800351 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400352}
353
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400354int k_mbox_data_block_get(struct k_mbox_msg *rx_msg, struct k_mem_pool *pool,
Andy Ross78327382020-03-05 15:18:14 -0800355 struct k_mem_block *block, k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356{
357 int result;
358
359 /* handle case where data is to be discarded */
360 if (pool == NULL) {
361 rx_msg->size = 0;
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800362 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400363 return 0;
364 }
365
366 /* handle case where data is already in a memory pool block */
Andy Ross73cb9582017-05-09 10:42:39 -0700367 if (rx_msg->tx_block.data != NULL) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400368 /* give ownership of the block to receiver */
369 *block = rx_msg->tx_block;
Andy Ross73cb9582017-05-09 10:42:39 -0700370 rx_msg->tx_block.data = NULL;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400371
372 /* now dispose of message */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800373 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400374 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 Pereiraa1ae8452018-03-06 15:08:55 -0800404static int mbox_message_data_check(struct k_mbox_msg *rx_msg, void *buffer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400405{
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 Pereiraa1ae8452018-03-06 15:08:55 -0800411 mbox_message_dispose(rx_msg);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400412 } else {
413 /* keep message around for later data retrieval */
414 }
415
416 return 0;
417}
418
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400419int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer,
Andy Ross78327382020-03-05 15:18:14 -0800420 k_timeout_t timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421{
Andy Rossccf3bf72018-05-10 11:10:34 -0700422 struct k_thread *sending_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400423 struct k_mbox_msg *tx_msg;
Andy Ross9eeb6b82018-07-25 15:06:24 -0700424 k_spinlock_key_t key;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400425 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 Ross9eeb6b82018-07-25 15:06:24 -0700431 key = k_spin_lock(&mbox->lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400432
Andy Rossccf3bf72018-05-10 11:10:34 -0700433 _WAIT_Q_FOR_EACH(&mbox->tx_msg_queue, sending_thread) {
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500434 tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400435
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800436 if (mbox_message_match(tx_msg, rx_msg) == 0) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400437 /* take sender out of mailbox's tx queue */
Patrik Flykt4344e272019-03-08 14:19:05 -0700438 z_unpend_thread(sending_thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439
Andy Ross9eeb6b82018-07-25 15:06:24 -0700440 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400441
442 /* consume message data immediately, if needed */
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800443 return mbox_message_data_check(rx_msg, buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400444 }
445 }
446
447 /* didn't find a matching sender */
448
Andy Ross78327382020-03-05 15:18:14 -0800449 if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400450 /* don't wait for a matching sender to appear */
Andy Ross9eeb6b82018-07-25 15:06:24 -0700451 k_spin_unlock(&mbox->lock, key);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400452 return -ENOMSG;
453 }
454
455 /* wait until a matching sender appears or a timeout occurs */
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500456 _current->base.swap_data = rx_msg;
Patrik Flykt4344e272019-03-08 14:19:05 -0700457 result = z_pend_curr(&mbox->lock, key, &mbox->rx_msg_queue, timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400458
459 /* consume message data immediately, if needed */
460 if (result == 0) {
Leandro Pereiraa1ae8452018-03-06 15:08:55 -0800461 result = mbox_message_data_check(rx_msg, buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400462 }
463
464 return result;
465}