struct k_mbox {
/** Transmit messages queue */
_wait_q_t tx_msg_queue;
/** Receive message queue */
_wait_q_t rx_msg_queue;
struct k_spinlock lock;
SYS_PORT_TRACING_TRACKING_FIELD(k_mbox)
};
struct k_mbox_msg {
/** internal use only - needed for legacy API support */
uint32_t _mailbox;
/** size of message (in bytes) */
size_t size;
/** application-defined information value */
uint32_t info;
/** sender's message data buffer */
void *tx_data;
/** internal use only - needed for legacy API support */
void *_rx_data;
/** message data block descriptor */
struct k_mem_block tx_block;
/** source thread id */
k_tid_t rx_source_thread;
/** target thread id */
k_tid_t tx_target_thread;
/** internal use only - thread waiting on send (may be a dummy) */
k_tid_t _syncing_thread;
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
/** internal use only - semaphore used during asynchronous send */
struct k_sem *_async_sem;
#endif
};
struct k_mbox_async {
struct _thread_base thread; /* dummy thread object */
struct k_mbox_msg tx_msg; /* transmit message descriptor */
};
使用异步模式初始化内存池:
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
static int init_mbox_module(const struct device *dev)
{
ARG_UNUSED(dev);
/* 创建异步消息描述符的内存池 */
static struct k_mbox_async __noinit async_msg[CONFIG_NUM_MBOX_ASYNC_MSGS];
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
/* dummy 线程仅需要执行最小初始化,未分配栈空间,
* 未指定入口地址,初始状态设置为 _THREAD_DUMMY,表明其本身不是一个真实的线程
* 初始化完成后将每一个描述符添加到栈中,栈中的描述符即为剩余未使用的描述符。
*/
int i;
for (i = 0; i < CONFIG_NUM_MBOX_ASYNC_MSGS; i++) {
z_init_thread_base(&async_msg[i].thread, 0, _THREAD_DUMMY, 0);
k_stack_push(&async_msg_free, (stack_data_t)&async_msg[i]);
}
#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS > 0 */
return 0;
}
SYS_INIT(init_mbox_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
#endif /* CONFIG_NUM_MBOX_ASYNC_MSGS */
/* stack of unused asynchronous message descriptors */
K_STACK_DEFINE(async_msg_free, CONFIG_NUM_MBOX_ASYNC_MSGS);
/* allocate an asynchronous message descriptor */
static inline void mbox_async_alloc(struct k_mbox_async **async)
{
(void)k_stack_pop(&async_msg_free, (stack_data_t *)async, K_FOREVER);
}
/* free an asynchronous message descriptor */
static inline void mbox_async_free(struct k_mbox_async *async)
{
k_stack_push(&async_msg_free, (stack_data_t)async);
}
static int mbox_message_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
k_timeout_t timeout)
{
struct k_thread *sending_thread;
struct k_thread *receiving_thread;
struct k_mbox_msg *rx_msg;
k_spinlock_key_t key;
/* 在发送邮件时设置发送者ID */
tx_msg->rx_source_thread = _current;
/* 将当前线程的 swap_data 指向 tx_msg,以便接收者从 swap_data 读取数据 */
sending_thread = tx_msg->_syncing_thread;
sending_thread->base.swap_data = tx_msg;
/* search mailbox's rx queue for a compatible receiver */
key = k_spin_lock(&mbox->lock);
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, message_put, mbox, timeout);
_WAIT_Q_FOR_EACH(&mbox->rx_msg_queue, receiving_thread) {
rx_msg = (struct k_mbox_msg *)receiving_thread->base.swap_data;
/* 从接收等待队列中查找符合要求的接收线程(接收地址和发送地址要匹配)
* 然后将消息从发送者拷贝到接收者,接收者会通过判断 _syncing_thread
* 是否为dummy 线程决定是否需要释放内存。
*/
if (mbox_message_match(tx_msg, rx_msg) == 0) {
/* 将接收线程从接收队列中取出并唤醒 */
z_unpend_thread(receiving_thread);
/* 将接收线程切换为就绪状态,设置返回值0 */
arch_thread_return_value_set(receiving_thread, 0);
z_ready_thread(receiving_thread);
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
/* 如果发送者为 dummy 线程,此处会触发一次上下文切换立即唤醒接收者 */
if ((sending_thread->base.thread_state & _THREAD_DUMMY)
!= 0U) {
z_reschedule(&mbox->lock, key);
return 0;
}
#endif
SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout);
/* 如果使用同步发送,将会把当前线程挂起,直到邮件被接收完成 */
int ret = z_pend_curr(&mbox->lock, key, NULL, K_FOREVER);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret);
return ret;
}
}
/* 找不到接收者,等待时间为0,返回-ENOMSG */
if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, -ENOMSG);
k_spin_unlock(&mbox->lock, key);
return -ENOMSG;
}
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
/* 如果使用异步发送,由于此时没有接收者, 需要将 dummy 线程添加到 tx_msg_queue 中,
* 发送者可以继续向下执行,返回0。
*/
if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
z_pend_thread(sending_thread, &mbox->tx_msg_queue, K_FOREVER);
k_spin_unlock(&mbox->lock, key);
return 0;
}
#endif
SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mbox, message_put, mbox, timeout);
/* 同步发送时,发送者会被阻塞一段时间,直到超时或者被接收,之后会从 tx_msg_queue 中取出 */
int ret = z_pend_curr(&mbox->lock, key, &mbox->tx_msg_queue, timeout);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, message_put, mbox, timeout, ret);
return ret;
}
int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
k_timeout_t timeout)
{
/* 采用同步发送模式时,被阻塞的线程为调用线程本身 */
tx_msg->_syncing_thread = _current;
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, put, mbox, timeout);
int ret = mbox_message_put(mbox, tx_msg, timeout);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, put, mbox, timeout, ret);
return ret;
}
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
struct k_sem *sem)
{
struct k_mbox_async *async;
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mbox, async_put, mbox, sem);
/* 从内存池分配异步消息描述符,然后将 dummy 线程设置为被阻塞对象,调用者继续向后执行 */
mbox_async_alloc(&async);
async->thread.prio = _current->base.prio;
async->tx_msg = *tx_msg;
async->tx_msg._syncing_thread = (struct k_thread *)&async->thread;
async->tx_msg._async_sem = sem;
(void)mbox_message_put(mbox, &async->tx_msg, K_FOREVER);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mbox, async_put, mbox, sem);
}
void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer)
{
/* handle case where data is to be discarded */
if (buffer == NULL) {
rx_msg->size = 0;
mbox_message_dispose(rx_msg);
return;
}
/* copy message data to buffer, then dispose of message */
if ((rx_msg->tx_data != NULL) && (rx_msg->size > 0U)) {
(void)memcpy(buffer, rx_msg->tx_data, rx_msg->size);
}
mbox_message_dispose(rx_msg);
}
static void mbox_message_dispose(struct k_mbox_msg *rx_msg)
{
struct k_thread *sending_thread;
struct k_mbox_msg *tx_msg;
/* do nothing if message was disposed of when it was received */
if (rx_msg->_syncing_thread == NULL) {
return;
}
if (rx_msg->tx_block.data != NULL) {
rx_msg->tx_block.data = NULL;
}
/* recover sender info */
sending_thread = rx_msg->_syncing_thread;
rx_msg->_syncing_thread = NULL;
tx_msg = (struct k_mbox_msg *)sending_thread->base.swap_data;
/* update data size field for sender */
tx_msg->size = rx_msg->size;
#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
/* 回收资源 */
if ((sending_thread->base.thread_state & _THREAD_DUMMY) != 0U) {
struct k_sem *async_sem = tx_msg->_async_sem;
mbox_async_free((struct k_mbox_async *)sending_thread);
if (async_sem != NULL) {
k_sem_give(async_sem);
}
return;
}
#endif
/* 唤醒发送线程*/
arch_thread_return_value_set(sending_thread, 0);
z_mark_thread_as_not_pending(sending_thread);
z_ready_thread(sending_thread);
z_reschedule_unlocked();
}