From f3e964f7d01dac9a5e285a1c6c579f75df6b3c57 Mon Sep 17 00:00:00 2001 From: seteiro <276597748+cms19859230182-lang@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:55:44 +0800 Subject: [PATCH] [kernel][ipc] preserve FIFO order for mb/mq handoff Fix #4940 Reserve the mailbox value or message queue node for the first FIFO waiter before resuming it, preventing race conditions where subsequent receivers steal in-flight handoffs. When a receiver exits or is deleted/detached before consuming its reserved handoff, safely reclaim the record and return capacity or the message back to the object under object spinlock protection, avoiding permanently wedged capacity. In addition, when urgent messages arrive, prioritize them ahead of in-flight normal handoffs, and preserve message priority order when returning unconsumed nodes or delivering in-flight handoffs under RT_USING_MESSAGEQUEUE_PRIORITY. On wakeup, receivers take any pending RX handoff before checking thread error. Follow-up for review comments: - Register the handoff record before a receiver becomes runnable, and do the registration, the suspend-list removal and the ready transition in one scheduler-locked step, so a receiver that is detached or exits in the wakeup window is still seen by rt_ipc_handoff_thread_cleanup() instead of being handed a value after it closed. Every mb/mq direct handoff path (RX/TX, send, urgent, and the re-delivery from a retiring receiver or sender) uses the same primitive. - Keep the urgent flag on the directly handed-off message and on a handoff re-delivered to the next waiter, and mark the urgent replacement record, so a later priority message can no longer overtake an urgent handoff when _mq_take_handoff() re-sorts it by its stale priority. - Cover it in utest: an urgent handoff followed by a higher priority message before the receiver runs, and the handoff reclaimed when the receiver is detached. Enable RT_USING_MESSAGEQUEUE_PRIORITY in kernel/ipc.cfg so CI builds and runs those cases. - Initialize sender before the FIFO-only handoff wakeup at the end of _rt_mb_recv(): the non-FIFO wakeup path never assigned it, which the strict builds reported as -Werror=maybe-uninitialized. - Keep the tcpip thread's stack headroom: the handoff wakeup added one frame to the mailbox receive path and tipped the 1024-byte lwIP thread over (A9 lwip/sal utest: "thread:tcpip stack overflow" in test_gethostbyname). Drop the extra rollback local and let _ipc_handoff_take_ex() skip a NULL value pointer, and raise RT_LWIP_TCPTHREAD_STACKSIZE to 2048 in the A9 lwip/sal test configs, which already measured ~1032 bytes worst case before this PR. - Hold an object reference while an in-flight handoff is reclaimed. rt_ipc_handoff_thread_cleanup() drops the handoff table lock before it takes the object lock, so the delete path on another core could free a mailbox/message queue in between and the reclaim would lock freed memory. The reclaim now takes in_flight (rt_ipc_object) under the table lock and drops it after the object work is done; rt_mb_delete()/rt_mq_delete() wait for in_flight to drain after clearing the table entries and before freeing the object or its pool. - Wait for in_flight in rt_mb_detach() and rt_mq_detach() too. The caller of a detach may reuse the static object as soon as it returns, while a reclaim that is already past the handoff table lock still takes that object's lock and updates its queues. Both detach paths now drain in_flight after clearing the table entries and releasing the object lock, before returning - symmetric with rt_mb_delete()/rt_mq_delete(). That drain blocks, so detach is now checked as thread-context-only (RT_DEBUG_NOT_IN_INTERRUPT), like delete always was. - Take a handoff record together with its object reference before touching the object. A sender that resumes from a timeout/interrupt and a receiver resumed by delete/detach used to take the object lock first, so a delete/detach that had already cleared the record and released or reused the object made them lock freed memory. Every reclaim entry point (_mb_tx_return(), _mb_rx_return(), _mq_tx_return(), _mq_rx_return() and both wakeup resumes) now takes the record and in_flight in the same handoff-lock critical section, and touches the object only when the take succeeded; rt_ipc_handoff_thread_cleanup() drops its own reference dance and relies on the same helper. Co-Authored-By: DeepSeek Flash 1017 Co-Authored-By: Gemini 3.8 Flash - Follow-up for the 2026-11-29 review: - Keep every handoff capacity change inside the object lock: a reclaimed record is claimed under the handoff table lock (holding an object reference) and only removed under the object lock, so a concurrent sender can no longer take the freed slot before the reserved value is returned. - Mark the receiver of a reclaimed handoff with -RT_ERROR while clearing the records on detach/delete/reset, so a receiver that was already woken by a handoff no longer locks a released mailbox or message queue. - Fold the mb/mq TX/RX reclaim paths into one helper and walk the handoff table by pointer, shrinking the FIFO handoff code enough for the 64 KiB at32f421-start and at32f425-start builds to link again. - Add mailbox/messagequeue utest cases for detach/delete while the woken receiver has not run yet. - Follow-up for the 2026-12-01 review: - Keep one handoff record per thread instead of a fixed 64-entry table. The table could run out while several receivers were woken and not yet run, and a waiting sender then had to fall back to the shared queue, where a higher priority receiver overtook the reserved FIFO handoff. With the record in the TCB the registry can no longer fill up, and a flood of more waiters than the old table size still keeps FIFO order. - Release the object reference taken by a TX claim on every exit of _rt_mb_send_wait()/_rt_mq_send_wait(). A sender that resumed with a reserved slot and sent successfully used to keep in_flight, so a later rt_mb_delete()/rt_mq_delete()/detach never saw it drain. - Reclaim a claimed record from rt_ipc_handoff_thread_cleanup() as well: it used to only match its own unclaimed records, so a thread that exited while holding a claim left in_flight set and the drain in the delete/detach path would never finish. --- .github/utest/configs/components/lwip.cfg | 5 +- .github/utest/configs/components/sal.cfg | 5 +- .github/utest/configs/kernel/ipc.cfg | 1 + include/rtdef.h | 16 + include/rtthread.h | 3 + src/ipc.c | 1049 ++++++++++++++++++++- src/thread.c | 17 + src/utest/mailbox_tc.c | 381 ++++++++ src/utest/messagequeue_tc.c | 393 ++++++++ 9 files changed, 1841 insertions(+), 29 deletions(-) diff --git a/.github/utest/configs/components/lwip.cfg b/.github/utest/configs/components/lwip.cfg index 7f8b6e53e7a6..4548e3d9a059 100644 --- a/.github/utest/configs/components/lwip.cfg +++ b/.github/utest/configs/components/lwip.cfg @@ -17,4 +17,7 @@ CONFIG_RT_UTEST_LWIP_TCP_PORT=1234 CONFIG_RT_UTEST_LWIP_UDP_PORT=1235 CONFIG_RT_UTEST_LWIP_TEST_URL="www.rt-thread.org" CONFIG_RT_UTEST_LWIP_TEST_ADDR="180.163.146.111" -CONFIG_BSP_DRV_EMAC=y \ No newline at end of file +CONFIG_BSP_DRV_EMAC=y +# The tcpip thread runs lwIP API callbacks on a debug (-O0) build; the 1024 +# default is not enough once the mailbox handoff sits in its receive path. +CONFIG_RT_LWIP_TCPTHREAD_STACKSIZE=2048 diff --git a/.github/utest/configs/components/sal.cfg b/.github/utest/configs/components/sal.cfg index fd6dcdb585e8..9324c8d900e2 100644 --- a/.github/utest/configs/components/sal.cfg +++ b/.github/utest/configs/components/sal.cfg @@ -5,4 +5,7 @@ CONFIG_RT_USING_CI_ACTION=y CONFIG_RT_UTEST_TC_USING_SAL=y CONFIG_RT_LWIP_NETIF_LOOPBACK=y -CONFIG_BSP_DRV_EMAC=y \ No newline at end of file +CONFIG_BSP_DRV_EMAC=y +# The tcpip thread runs lwIP API callbacks on a debug (-O0) build; the 1024 +# default is not enough once the mailbox handoff sits in its receive path. +CONFIG_RT_LWIP_TCPTHREAD_STACKSIZE=2048 diff --git a/.github/utest/configs/kernel/ipc.cfg b/.github/utest/configs/kernel/ipc.cfg index 7ad79b0e259e..921df4f44aec 100644 --- a/.github/utest/configs/kernel/ipc.cfg +++ b/.github/utest/configs/kernel/ipc.cfg @@ -15,6 +15,7 @@ CONFIG_RT_UTEST_WORKQUEUE=y CONFIG_RT_USING_SEMAPHORE=y CONFIG_RT_USING_EVENT=y CONFIG_RT_USING_MESSAGEQUEUE=y +CONFIG_RT_USING_MESSAGEQUEUE_PRIORITY=y CONFIG_RT_USING_SIGNALS=y CONFIG_RT_USING_MUTEX=y CONFIG_RT_USING_MAILBOX=y diff --git a/include/rtdef.h b/include/rtdef.h index d2cda8650811..f370ed2148b7 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -857,6 +857,18 @@ struct rt_user_context typedef void (*rt_thread_cleanup_t)(struct rt_thread *tid); +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) +struct rt_thread_handoff +{ + rt_list_t node; + void *ipc; + rt_ubase_t value; + rt_ubase_t thread; + rt_uint8_t kind; + rt_uint8_t urgent; +}; +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ + /** * @brief Thread Control Block */ @@ -959,6 +971,9 @@ struct rt_thread struct rt_spinlock spinlock; rt_ubase_t user_data; /**< private user data beyond this thread */ +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) + struct rt_thread_handoff handoff; +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ }; typedef struct rt_thread *rt_thread_t; @@ -998,6 +1013,7 @@ struct rt_ipc_object struct rt_object parent; /**< inherit from rt_object */ rt_list_t suspend_thread; /**< threads pended on this resource */ + rt_atomic_t in_flight; /**< in-flight handoff reclaims referencing this object */ }; /** diff --git a/include/rtthread.h b/include/rtthread.h index 0854d3bca1e9..08ea049849cc 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -421,6 +421,9 @@ rt_err_t rt_susp_list_resume_all_irq(rt_list_t *susp_list, rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int ipc_flags, int suspend_flag); /* only for a suspended thread, and caller must hold the scheduler lock */ rt_err_t rt_susp_list_enqueue(rt_list_t *susp_list, rt_thread_t thread, int ipc_flags); +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) +void rt_ipc_handoff_thread_cleanup(rt_thread_t thread); +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ /** * @addtogroup group_semaphore Semaphore diff --git a/src/ipc.c b/src/ipc.c index 0a690d65b38b..82d085acf751 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -62,6 +62,615 @@ extern void (*rt_object_take_hook)(struct rt_object *object); extern void (*rt_object_put_hook)(struct rt_object *object); #endif /* RT_USING_HOOK */ +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) +void rt_ipc_handoff_thread_cleanup(rt_thread_t thread); + +#define _IPC_HANDOFF_MB_RX 0 +#define _IPC_HANDOFF_MB_TX 1 +#define _IPC_HANDOFF_MQ_RX 2 +#define _IPC_HANDOFF_MQ_TX 3 + +#define _IPC_HANDOFF_CLAIMED ((rt_ubase_t)1) +#define _IPC_HANDOFF_TAKE 0 +#define _IPC_HANDOFF_CLAIM 1 +#define _IPC_HANDOFF_FINISH 2 + +static rt_list_t _ipc_handoffs = RT_LIST_OBJECT_INIT(_ipc_handoffs); +RT_DEFINE_SPINLOCK(_ipc_handoff_lock); + +/* The handoff table records only a raw object pointer. A thread that reclaims + * an in-flight handoff leaves the table lock before it takes the object lock, + * so it must hold an object reference across that window: an object delete + * waits for in_flight to drain before it frees the object. */ +rt_inline void _ipc_handoff_object_get(void *ipc) +{ + rt_atomic_add(&((struct rt_ipc_object *)ipc)->in_flight, 1); +} + +rt_inline void _ipc_handoff_object_put(void *ipc) +{ + rt_atomic_sub(&((struct rt_ipc_object *)ipc)->in_flight, 1); +} + +static rt_bool_t _ipc_handoff_put_ex(void *ipc, rt_thread_t thread, + rt_ubase_t value, rt_uint8_t kind, + rt_bool_t urgent) +{ + rt_base_t level; + struct rt_thread_handoff *handoff; + + handoff = &thread->handoff; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + if (handoff->thread != RT_NULL) + { + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_FALSE; + } + + handoff->ipc = ipc; + handoff->value = value; + handoff->kind = kind; + handoff->urgent = urgent ? 1 : 0; + handoff->thread = (rt_ubase_t)thread; + rt_list_insert_before(&_ipc_handoffs, &handoff->node); + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return RT_TRUE; +} + +static rt_bool_t _ipc_handoff_take_ex(void *ipc, rt_thread_t thread, + rt_uint8_t kind, rt_ubase_t *value, + rt_bool_t *urgent, rt_uint8_t op) +{ + rt_base_t level; + rt_list_t *node; + rt_list_t *next; + struct rt_thread_handoff *handoff; + rt_bool_t taken = RT_FALSE; + rt_ubase_t claimed = (rt_ubase_t)thread | _IPC_HANDOFF_CLAIMED; + rt_ubase_t match = (op == _IPC_HANDOFF_FINISH) ? claimed : (rt_ubase_t)thread; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (node = _ipc_handoffs.next; node != &_ipc_handoffs; node = next) + { + next = node->next; + handoff = rt_container_of(node, struct rt_thread_handoff, node); + + if (handoff->thread == match && + handoff->ipc == ipc && + handoff->kind == kind) + { + if (value != RT_NULL) + { + *value = handoff->value; + } + if (urgent != RT_NULL) + { + *urgent = handoff->urgent ? RT_TRUE : RT_FALSE; + } + if (op == _IPC_HANDOFF_CLAIM) + { + handoff->thread = claimed; + _ipc_handoff_object_get(ipc); + } + else + { + rt_list_remove(&handoff->node); + handoff->thread = RT_NULL; + handoff->urgent = 0; + } + taken = RT_TRUE; + break; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return taken; +} + +/* Register the handoff record and make the first FIFO waiter runnable as one + * atomic step. + * + * The record has to exist before the receiver can run: on SMP the receiver + * becomes schedulable as soon as it is ready, and if it is detached or exits + * in that window rt_ipc_handoff_thread_cleanup() would not see the record and + * the value would be handed to a closed thread (and the record slot lost). + * Registering first, with the suspend-list removal and the ready transition + * under the scheduler lock, keeps the record and the wakeup in the same + * critical section and makes the cleanup path able to reclaim it. + * + * @param susp_list suspend list of the IPC object, caller holds its spinlock + * @param urgent mark the handoff as urgent (message queue priority order) + * @return the woken thread, or RT_NULL when no record could be registered or + * the waiter lost the race with its timeout; the caller then falls + * back to the plain suspend-list wakeup or the shared queue. + */ +static struct rt_thread *_ipc_handoff_wakeup(rt_list_t *susp_list, void *ipc, + rt_ubase_t value, rt_uint8_t kind, + rt_bool_t urgent) +{ + rt_sched_lock_level_t slvl; + struct rt_thread *thread; + + rt_sched_lock(&slvl); + + if (rt_list_isempty(susp_list)) + { + rt_sched_unlock(slvl); + return RT_NULL; + } + + thread = RT_THREAD_LIST_NODE_ENTRY(susp_list->next); + + if (!_ipc_handoff_put_ex(ipc, thread, value, kind, urgent)) + { + rt_sched_unlock(slvl); + return RT_NULL; + } + + if (rt_sched_thread_ready(thread) != RT_EOK) + { + /* lost the race with the thread timer: give the record back */ + _ipc_handoff_take_ex(ipc, thread, kind, RT_NULL, RT_NULL, _IPC_HANDOFF_TAKE); + rt_sched_unlock(slvl); + return RT_NULL; + } + + thread->error = RT_EOK; + rt_sched_unlock(slvl); + + return thread; +} + +static void _ipc_handoff_clear(void *ipc) +{ + rt_base_t level; + rt_list_t *node; + rt_list_t *next; + struct rt_thread_handoff *handoff; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (node = _ipc_handoffs.next; node != &_ipc_handoffs; node = next) + { + next = node->next; + handoff = rt_container_of(node, struct rt_thread_handoff, node); + + if (handoff->ipc == ipc) + { + rt_ubase_t waiter = handoff->thread; + +#ifdef RT_USING_MESSAGEQUEUE + if (handoff->kind == _IPC_HANDOFF_MQ_RX || + handoff->kind == _IPC_HANDOFF_MQ_TX) + { + struct rt_mq_message *msg = (struct rt_mq_message *)handoff->value; + msg->next = (struct rt_mq_message *)((rt_mq_t)ipc)->msg_queue_free; + ((rt_mq_t)ipc)->msg_queue_free = msg; + } +#endif + if ((waiter & _IPC_HANDOFF_CLAIMED) == 0) + { + ((rt_thread_t)waiter)->error = -RT_ERROR; + } + rt_list_remove(&handoff->node); + handoff->thread = RT_NULL; + handoff->urgent = 0; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); +} + +static rt_bool_t _ipc_handoff_replace_urgent(void *ipc, rt_uint8_t kind, + rt_ubase_t value, rt_ubase_t *prev) +{ + rt_base_t level; + rt_list_t *node; + struct rt_thread_handoff *handoff; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (node = _ipc_handoffs.next; node != &_ipc_handoffs; node = node->next) + { + handoff = rt_container_of(node, struct rt_thread_handoff, node); + + if (handoff->thread != RT_NULL && + handoff->ipc == ipc && + handoff->kind == kind && + (handoff->thread & _IPC_HANDOFF_CLAIMED) == 0) + { + *prev = handoff->value; + handoff->value = value; + handoff->urgent = 1; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_TRUE; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_FALSE; +} + +#ifdef RT_USING_MAILBOX +static rt_uint32_t _ipc_handoff_count(void *ipc) +{ + rt_base_t level; + rt_list_t *node; + struct rt_thread_handoff *handoff; + rt_uint32_t count = 0; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (node = _ipc_handoffs.next; node != &_ipc_handoffs; node = node->next) + { + handoff = rt_container_of(node, struct rt_thread_handoff, node); + + if (handoff->ipc == ipc) + { + count++; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return count; +} + +static rt_uint32_t _mb_occupied_count(rt_mailbox_t mb) +{ + return mb->entry + _ipc_handoff_count(mb); +} + +static rt_bool_t _mb_rx_handoff_replace_urgent(rt_mailbox_t mb, rt_ubase_t urgent_val) +{ + rt_ubase_t prev_val; + + if (!_ipc_handoff_replace_urgent(mb, _IPC_HANDOFF_MB_RX, urgent_val, &prev_val)) + { + return RT_FALSE; + } + + /* insert previous normal value to queue head */ + if (mb->out_offset > 0) + { + mb->out_offset--; + } + else + { + mb->out_offset = mb->size - 1; + } + mb->msg_pool[mb->out_offset] = prev_val; + mb->entry++; + + return RT_TRUE; +} +#endif /* RT_USING_MAILBOX */ + +#ifdef RT_USING_MESSAGEQUEUE +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY +/* The messages reclaimed from a handoff record or from a retiring receiver + * arrived before everything still queued, so they must be placed ahead of + * messages sharing the same priority. */ +static void _mq_prio_insert(rt_mq_t mq, struct rt_mq_message *msg) +{ + struct rt_mq_message *curr; + struct rt_mq_message *prev = RT_NULL; + + for (curr = mq->msg_queue_head; curr != RT_NULL; curr = curr->next) + { + if (curr->prio <= msg->prio) + { + break; + } + prev = curr; + } + + msg->next = curr; + if (prev == RT_NULL) + { + mq->msg_queue_head = msg; + } + else + { + prev->next = msg; + } + + if (curr == RT_NULL) + { + mq->msg_queue_tail = msg; + } +} +#endif /* RT_USING_MESSAGEQUEUE_PRIORITY */ + +static rt_bool_t _mq_rx_handoff_replace_urgent(rt_mq_t mq, struct rt_mq_message *urgent_msg) +{ + rt_ubase_t prev; + struct rt_mq_message *prev_msg; + + if (!_ipc_handoff_replace_urgent(mq, _IPC_HANDOFF_MQ_RX, (rt_ubase_t)urgent_msg, &prev)) + { + return RT_FALSE; + } + + prev_msg = (struct rt_mq_message *)prev; + /* the urgent message is delivered before the reserved one, so put + * the normal message back into priority order */ +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + _mq_prio_insert(mq, prev_msg); +#else + prev_msg->next = (struct rt_mq_message *)mq->msg_queue_head; + mq->msg_queue_head = prev_msg; + if (mq->msg_queue_tail == RT_NULL) + { + mq->msg_queue_tail = prev_msg; + } +#endif + mq->entry++; + + return RT_TRUE; +} + +/* Take back the message reserved for this receiver. Under message queue + * priority the reserved message was sent before anything still queued, so it + * is re-inserted and the receiver gets the real queue head instead; otherwise + * a higher priority message sent afterwards could never be received first. + * The caller holds the object lock. */ +static struct rt_mq_message *_mq_handoff_reorder(rt_mq_t mq, + struct rt_mq_message *msg, + rt_bool_t urgent) +{ +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + if (!urgent && mq->msg_queue_head != RT_NULL && + mq->entry < mq->max_msgs) + { + _mq_prio_insert(mq, msg); + mq->entry++; + + msg = (struct rt_mq_message *)mq->msg_queue_head; + mq->msg_queue_head = msg->next; + if (mq->msg_queue_tail == msg) + { + mq->msg_queue_tail = RT_NULL; + } + if (mq->entry > 0) + { + mq->entry--; + } + } +#else + RT_UNUSED(urgent); +#endif + + return msg; +} + +static rt_bool_t _mq_take_handoff(rt_mq_t mq, rt_thread_t thread, + struct rt_mq_message **msg_out) +{ + rt_ubase_t value; + rt_bool_t urgent = RT_FALSE; + + if (!_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, &value, &urgent, _IPC_HANDOFF_TAKE)) + { + return RT_FALSE; + } + + *msg_out = _mq_handoff_reorder(mq, (struct rt_mq_message *)value, urgent); + + return RT_TRUE; +} +#endif /* RT_USING_MESSAGEQUEUE */ + +static void _ipc_handoff_release(void *ipc, rt_thread_t thread, rt_uint8_t kind, + rt_ubase_t value, rt_bool_t urgent) +{ + rt_base_t level; + struct rt_thread *next; + +#ifdef RT_USING_MAILBOX + if (kind == _IPC_HANDOFF_MB_RX || kind == _IPC_HANDOFF_MB_TX) + { + rt_mailbox_t mb = (rt_mailbox_t)ipc; + + level = rt_spin_lock_irqsave(&(mb->spinlock)); + if (!_ipc_handoff_take_ex(mb, thread, kind, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + _ipc_handoff_object_put(mb); + return; + } + if (kind == _IPC_HANDOFF_MB_TX) + { + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO && + !rt_list_isempty(&(mb->suspend_sender_thread)) && + _mb_occupied_count(mb) < mb->size) + { + next = _ipc_handoff_wakeup(&(mb->suspend_sender_thread), mb, 0, + kind, RT_FALSE); + if (next == RT_NULL) + { + rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + } + } + } + else + { + if (!rt_list_isempty(&(mb->parent.suspend_thread))) + { + next = _ipc_handoff_wakeup(&(mb->parent.suspend_thread), mb, value, + kind, urgent); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + _ipc_handoff_object_put(mb); + rt_schedule(); + return; + } + } + if (mb->entry < mb->size) + { + if (mb->out_offset == 0) + { + mb->out_offset = mb->size - 1; + } + else + { + mb->out_offset--; + } + mb->msg_pool[mb->out_offset] = value; + mb->entry++; + } + if (!rt_list_isempty(&(mb->parent.suspend_thread))) + { + rt_susp_list_dequeue(&(mb->parent.suspend_thread), RT_EOK); + } + } + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + _ipc_handoff_object_put(mb); + rt_schedule(); + return; + } +#endif +#ifdef RT_USING_MESSAGEQUEUE + if (kind == _IPC_HANDOFF_MQ_RX || kind == _IPC_HANDOFF_MQ_TX) + { + rt_mq_t mq = (rt_mq_t)ipc; + struct rt_mq_message *msg; + + level = rt_spin_lock_irqsave(&(mq->spinlock)); + if (!_ipc_handoff_take_ex(mq, thread, kind, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + return; + } + msg = (struct rt_mq_message *)value; + if (kind == _IPC_HANDOFF_MQ_TX) + { + if (mq->parent.parent.flag == RT_IPC_FLAG_FIFO && + !rt_list_isempty(&(mq->suspend_sender_thread))) + { + next = _ipc_handoff_wakeup(&(mq->suspend_sender_thread), mq, value, + kind, RT_FALSE); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + rt_schedule(); + return; + } + rt_susp_list_dequeue(&(mq->suspend_sender_thread), RT_EOK); + } + msg->next = (struct rt_mq_message *)mq->msg_queue_free; + mq->msg_queue_free = msg; + } + else + { + if (!rt_list_isempty(&(mq->parent.suspend_thread))) + { + next = _ipc_handoff_wakeup(&(mq->parent.suspend_thread), mq, value, + kind, urgent); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + rt_schedule(); + return; + } + } + if (mq->entry < mq->max_msgs) + { +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + if (!urgent) + { + _mq_prio_insert(mq, msg); + } + else + { + msg->next = (struct rt_mq_message *)mq->msg_queue_head; + mq->msg_queue_head = msg; + if (mq->msg_queue_tail == RT_NULL) + { + mq->msg_queue_tail = msg; + } + } +#else + msg->next = (struct rt_mq_message *)mq->msg_queue_head; + mq->msg_queue_head = msg; + if (mq->msg_queue_tail == RT_NULL) + { + mq->msg_queue_tail = msg; + } +#endif + mq->entry++; + } + else + { + msg->next = (struct rt_mq_message *)mq->msg_queue_free; + mq->msg_queue_free = msg; + } + } + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + rt_schedule(); + return; + } +#endif +} + +static void _ipc_handoff_return(void *ipc, rt_thread_t thread, rt_uint8_t kind) +{ + rt_ubase_t value; + rt_bool_t urgent = RT_FALSE; + + if (!_ipc_handoff_take_ex(ipc, thread, kind, &value, &urgent, _IPC_HANDOFF_CLAIM)) + { + return; + } + + _ipc_handoff_release(ipc, thread, kind, value, urgent); +} + +void rt_ipc_handoff_thread_cleanup(rt_thread_t thread) +{ + rt_base_t level; + struct rt_thread_handoff *handoff; + rt_ubase_t owner; + void *ipc; + rt_ubase_t value; + rt_bool_t urgent; + rt_bool_t claimed; + rt_uint8_t kind; + + if (thread == RT_NULL) + { + return; + } + + handoff = &thread->handoff; + owner = (rt_ubase_t)thread; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + if (handoff->thread != owner && + handoff->thread != (owner | _IPC_HANDOFF_CLAIMED)) + { + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return; + } + ipc = handoff->ipc; + kind = handoff->kind; + value = handoff->value; + urgent = handoff->urgent ? RT_TRUE : RT_FALSE; + claimed = (handoff->thread & _IPC_HANDOFF_CLAIMED) ? RT_TRUE : RT_FALSE; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + if (claimed) + { + _ipc_handoff_release(ipc, thread, kind, value, urgent); + } + else + { + _ipc_handoff_return(ipc, thread, kind); + } +} +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ + /** * @addtogroup group_thread_comm * @{ @@ -83,6 +692,7 @@ rt_inline rt_err_t _ipc_object_init(struct rt_ipc_object *ipc) { /* initialize ipc object */ rt_list_init(&(ipc->suspend_thread)); + rt_atomic_store(&(ipc->in_flight), 0); return RT_EOK; } @@ -2442,13 +3052,27 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb) RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox); RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent)); + /* the handoff drain below blocks, like the one in rt_mb_delete() */ + RT_DEBUG_NOT_IN_INTERRUPT; + level = rt_spin_lock_irqsave(&(mb->spinlock)); /* resume all suspended thread */ rt_susp_list_resume_all(&(mb->parent.suspend_thread), RT_ERROR); /* also resume all mailbox private suspended thread */ rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mb); rt_spin_unlock_irqrestore(&(mb->spinlock), level); + /* A handoff reclaim that left the handoff table before the clear above + * still takes this object's lock (rt_ipc_handoff_thread_cleanup()): the + * static object is about to be reusable by the caller, so its in-flight + * references must be gone before detach returns - symmetric with + * rt_mb_delete(). */ + while (rt_atomic_load(&(mb->parent.in_flight)) != 0) + { + rt_thread_mdelay(1); + } + /* detach mailbox object */ rt_object_detach(&(mb->parent.parent)); @@ -2565,8 +3189,18 @@ rt_err_t rt_mb_delete(rt_mailbox_t mb) /* also resume all mailbox private suspended thread */ rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mb); + rt_spin_unlock(&(mb->spinlock)); + /* Wait for handoff reclaims that are already past the table lock: they + * still take this object's lock (rt_ipc_handoff_thread_cleanup()), so the + * object must not be freed while in_flight is non-zero. */ + while (rt_atomic_load(&(mb->parent.in_flight)) != 0) + { + rt_thread_mdelay(1); + } + /* free mailbox pool */ RT_KERNEL_FREE(mb->msg_pool); @@ -2607,9 +3241,13 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, int suspend_flag) { struct rt_thread *thread; + struct rt_thread *receiver = RT_NULL; rt_base_t level; rt_uint32_t tick_delta; rt_err_t ret; + rt_bool_t has_tx_slot = RT_FALSE; + rt_bool_t held_ref = RT_FALSE; + rt_ubase_t handoff; /* parameter check */ RT_ASSERT(mb != RT_NULL); @@ -2629,14 +3267,15 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, level = rt_spin_lock_irqsave(&(mb->spinlock)); /* for non-blocking call */ - if (mb->entry == mb->size && timeout == 0) + if (_mb_occupied_count(mb) >= mb->size && timeout == 0) { rt_spin_unlock_irqrestore(&(mb->spinlock), level); return -RT_EFULL; } /* mailbox is full */ - while (mb->entry == mb->size) + while ((_mb_occupied_count(mb) >= mb->size) && + !has_tx_slot) { /* reset error number in thread */ thread->error = -RT_EINTR; @@ -2681,13 +3320,32 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, rt_schedule(); /* resume from suspend state */ - if (thread->error != RT_EOK) + if (_ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_TX, &handoff, RT_NULL, _IPC_HANDOFF_CLAIM)) { - /* return error */ - return thread->error; - } + if (thread->error != RT_EOK) + { + _ipc_handoff_release(mb, thread, _IPC_HANDOFF_MB_TX, handoff, RT_FALSE); + return thread->error; + } - level = rt_spin_lock_irqsave(&(mb->spinlock)); + level = rt_spin_lock_irqsave(&(mb->spinlock)); + if (!_ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_TX, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + _ipc_handoff_object_put(mb); + return -RT_ERROR; + } + has_tx_slot = RT_TRUE; + held_ref = RT_TRUE; + } + else + { + if (thread->error != RT_EOK) + { + return thread->error; + } + level = rt_spin_lock_irqsave(&(mb->spinlock)); + } /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) @@ -2701,6 +3359,24 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, } } + /* hand the value directly to the first FIFO receiver, if possible */ + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO && mb->entry == 0 && + !rt_list_isempty(&(mb->parent.suspend_thread))) + { + receiver = _ipc_handoff_wakeup(&(mb->parent.suspend_thread), mb, value, + _IPC_HANDOFF_MB_RX, RT_FALSE); + if (receiver != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mb); + } + rt_schedule(); + return RT_EOK; + } + } + /* set ptr */ mb->msg_pool[mb->in_offset] = value; /* increase input offset */ @@ -2718,22 +3394,41 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, else { rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mb); + } return -RT_EFULL; /* value overflowed */ } /* resume suspended thread */ - if (!rt_list_isempty(&mb->parent.suspend_thread)) + if (receiver == RT_NULL && !rt_list_isempty(&mb->parent.suspend_thread)) { rt_susp_list_dequeue(&(mb->parent.suspend_thread), RT_EOK); rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mb); + } + rt_schedule(); return RT_EOK; } rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mb); + } + + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } @@ -2813,6 +3508,7 @@ RTM_EXPORT(rt_mb_send_killable); */ rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value) { + struct rt_thread *receiver = RT_NULL; rt_base_t level; /* parameter check */ @@ -2823,12 +3519,34 @@ rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value) level = rt_spin_lock_irqsave(&(mb->spinlock)); - if (mb->entry == mb->size) + if (_mb_occupied_count(mb) >= mb->size) { rt_spin_unlock_irqrestore(&(mb->spinlock), level); return -RT_EFULL; } + /* hand the urgent value directly to the first FIFO receiver or replace normal in-flight handoff */ + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + if (_mb_rx_handoff_replace_urgent(mb, value)) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + return RT_EOK; + } + + if (mb->entry == 0 && !rt_list_isempty(&(mb->parent.suspend_thread))) + { + receiver = _ipc_handoff_wakeup(&(mb->parent.suspend_thread), mb, value, + _IPC_HANDOFF_MB_RX, RT_TRUE); + if (receiver != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + rt_schedule(); + return RT_EOK; + } + } + } + /* rewind to the previous position */ if (mb->out_offset > 0) { @@ -2846,7 +3564,7 @@ rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value) mb->entry++; /* resume suspended thread */ - if (!rt_list_isempty(&mb->parent.suspend_thread)) + if (receiver == RT_NULL && !rt_list_isempty(&mb->parent.suspend_thread)) { rt_susp_list_dequeue(&(mb->parent.suspend_thread), RT_EOK); @@ -2858,6 +3576,11 @@ rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value) } rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } RTM_EXPORT(rt_mb_urgent); @@ -2890,9 +3613,11 @@ RTM_EXPORT(rt_mb_urgent); static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout, int suspend_flag) { struct rt_thread *thread; + struct rt_thread *sender; rt_base_t level; rt_uint32_t tick_delta; rt_err_t ret; + rt_ubase_t handoff; /* parameter check */ RT_ASSERT(mb != RT_NULL); @@ -2910,6 +3635,34 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo level = rt_spin_lock_irqsave(&(mb->spinlock)); + if (_ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_RX, &handoff, RT_NULL, _IPC_HANDOFF_TAKE)) + { + *value = handoff; + sender = RT_NULL; + if (!rt_list_isempty(&(mb->suspend_sender_thread))) + { + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + sender = _ipc_handoff_wakeup(&(mb->suspend_sender_thread), mb, 0, + _IPC_HANDOFF_MB_TX, RT_FALSE); + } + if (sender == RT_NULL) + { + sender = rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + } + } + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + + RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent))); + + if (sender) + { + rt_schedule(); + } + + return RT_EOK; + } + /* for non-blocking call */ if (mb->entry == 0 && timeout == 0) { @@ -2965,12 +3718,53 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo /* re-schedule */ rt_schedule(); + /* A delete/detach that woke this thread has already cleared the + * record and may have released the object, so take the reserved value + * with an object reference before touching the object. */ + if (_ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_RX, &handoff, RT_NULL, _IPC_HANDOFF_CLAIM)) + { + level = rt_spin_lock_irqsave(&(mb->spinlock)); + if (!_ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_RX, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + _ipc_handoff_object_put(mb); + return -RT_ERROR; + } + + *value = handoff; + sender = RT_NULL; + if (!rt_list_isempty(&(mb->suspend_sender_thread))) + { + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + sender = _ipc_handoff_wakeup(&(mb->suspend_sender_thread), mb, 0, + _IPC_HANDOFF_MB_TX, RT_FALSE); + } + if (sender == RT_NULL) + { + sender = rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + } + } + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + + RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent))); + + _ipc_handoff_object_put(mb); + + if (sender) + { + rt_schedule(); + } + + return RT_EOK; + } + /* resume from suspend state */ if (thread->error != RT_EOK) { - /* return error */ return thread->error; } + level = rt_spin_lock_irqsave(&(mb->spinlock)); /* if it's not waiting forever and then re-calculate timeout tick */ @@ -3004,7 +3798,16 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo /* resume suspended thread */ if (!rt_list_isempty(&(mb->suspend_sender_thread))) { - rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + sender = RT_NULL; + if (mb->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + sender = _ipc_handoff_wakeup(&(mb->suspend_sender_thread), mb, 0, + _IPC_HANDOFF_MB_TX, RT_FALSE); + } + if (sender == RT_NULL) + { + sender = rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + } rt_spin_unlock_irqrestore(&(mb->spinlock), level); @@ -3072,6 +3875,8 @@ rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg) /* also resume all mailbox private suspended thread */ rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mb); + /* re-init mailbox */ mb->entry = 0; mb->in_offset = 0; @@ -3228,13 +4033,27 @@ rt_err_t rt_mq_detach(rt_mq_t mq) RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue); RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent)); + /* the handoff drain below blocks, like the one in rt_mq_delete() */ + RT_DEBUG_NOT_IN_INTERRUPT; + level = rt_spin_lock_irqsave(&(mq->spinlock)); /* resume all suspended thread */ rt_susp_list_resume_all(&mq->parent.suspend_thread, RT_ERROR); /* also resume all message queue private suspended thread */ rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mq); rt_spin_unlock_irqrestore(&(mq->spinlock), level); + /* The object lock is released above: a handoff reclaim that is already + * past the handoff table lock still takes it + * (rt_ipc_handoff_thread_cleanup()), so the caller may only reuse the + * static object once in-flight references drained - symmetric with + * rt_mq_delete(). */ + while (rt_atomic_load(&(mq->parent.in_flight)) != 0) + { + rt_thread_mdelay(1); + } + /* detach message queue object */ rt_object_detach(&(mq->parent.parent)); @@ -3376,9 +4195,18 @@ rt_err_t rt_mq_delete(rt_mq_t mq) rt_susp_list_resume_all(&(mq->parent.suspend_thread), RT_ERROR); /* also resume all message queue private suspended thread */ rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mq); rt_spin_unlock(&(mq->spinlock)); + /* Wait for handoff reclaims that are already past the table lock: they + * still take this object's lock, so the object must not be freed while + * in_flight is non-zero. */ + while (rt_atomic_load(&(mq->parent.in_flight)) != 0) + { + rt_thread_mdelay(1); + } + /* free message queue pool */ RT_KERNEL_FREE(mq->msg_pool); @@ -3432,9 +4260,13 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, { rt_base_t level; struct rt_mq_message *msg; - rt_uint32_t tick_delta; struct rt_thread *thread; + struct rt_thread *receiver = RT_NULL; + rt_uint32_t tick_delta; rt_err_t ret; + rt_bool_t has_tx_slot = RT_FALSE; + rt_bool_t held_ref = RT_FALSE; + rt_ubase_t handoff; RT_UNUSED(prio); @@ -3473,7 +4305,8 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, } /* message queue is full */ - while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL) + while (!has_tx_slot && + (msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL) { /* reset error number in thread */ thread->error = -RT_EINTR; @@ -3518,12 +4351,33 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, rt_schedule(); /* resume from suspend state */ - if (thread->error != RT_EOK) + if (_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_TX, &handoff, RT_NULL, _IPC_HANDOFF_CLAIM)) { - /* return error */ - return thread->error; + if (thread->error != RT_EOK) + { + _ipc_handoff_release(mq, thread, _IPC_HANDOFF_MQ_TX, handoff, RT_FALSE); + return thread->error; + } + + level = rt_spin_lock_irqsave(&(mq->spinlock)); + if (!_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_TX, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + return -RT_ERROR; + } + has_tx_slot = RT_TRUE; + held_ref = RT_TRUE; + msg = (struct rt_mq_message *)handoff; + } + else + { + if (thread->error != RT_EOK) + { + return thread->error; + } + level = rt_spin_lock_irqsave(&(mq->spinlock)); } - level = rt_spin_lock_irqsave(&(mq->spinlock)); /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) @@ -3538,7 +4392,10 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, } /* move free list pointer */ - mq->msg_queue_free = msg->next; + if (!has_tx_slot) + { + mq->msg_queue_free = msg->next; + } rt_spin_unlock_irqrestore(&(mq->spinlock), level); @@ -3552,8 +4409,29 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, /* disable interrupt */ level = rt_spin_lock_irqsave(&(mq->spinlock)); + #ifdef RT_USING_MESSAGEQUEUE_PRIORITY msg->prio = prio; +#endif + if (mq->parent.parent.flag == RT_IPC_FLAG_FIFO && + mq->entry == 0 && + !rt_list_isempty(&(mq->parent.suspend_thread))) + { + receiver = _ipc_handoff_wakeup(&(mq->parent.suspend_thread), mq, + (rt_ubase_t)msg, _IPC_HANDOFF_MQ_RX, RT_FALSE); + if (receiver != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + rt_schedule(); + return RT_EOK; + } + } + +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY if (mq->msg_queue_head == RT_NULL) { mq->msg_queue_head = msg; @@ -3611,22 +4489,41 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, else { rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } return -RT_EFULL; /* value overflowed */ } /* resume suspended thread */ - if (!rt_list_isempty(&mq->parent.suspend_thread)) + if (receiver == RT_NULL && !rt_list_isempty(&mq->parent.suspend_thread)) { rt_susp_list_dequeue(&(mq->parent.suspend_thread), RT_EOK); rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + rt_schedule(); return RT_EOK; } rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } @@ -3717,6 +4614,7 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size) { rt_base_t level; struct rt_mq_message *msg; + struct rt_thread *receiver = RT_NULL; /* parameter check */ RT_ASSERT(mq != RT_NULL); @@ -3755,6 +4653,27 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size) level = rt_spin_lock_irqsave(&(mq->spinlock)); + if (mq->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + if (_mq_rx_handoff_replace_urgent(mq, msg)) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + return RT_EOK; + } + + if (mq->entry == 0 && !rt_list_isempty(&(mq->parent.suspend_thread))) + { + receiver = _ipc_handoff_wakeup(&(mq->parent.suspend_thread), mq, + (rt_ubase_t)msg, _IPC_HANDOFF_MQ_RX, RT_TRUE); + if (receiver != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + rt_schedule(); + return RT_EOK; + } + } + } + /* link msg to the beginning of message queue */ msg->next = (struct rt_mq_message *)mq->msg_queue_head; mq->msg_queue_head = msg; @@ -3777,7 +4696,7 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size) } /* resume suspended thread */ - if (!rt_list_isempty(&mq->parent.suspend_thread)) + if (receiver == RT_NULL && !rt_list_isempty(&mq->parent.suspend_thread)) { rt_susp_list_dequeue(&(mq->parent.suspend_thread), RT_EOK); @@ -3790,6 +4709,11 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size) rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } RTM_EXPORT(rt_mq_urgent); @@ -3832,6 +4756,10 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, int suspend_flag) { struct rt_thread *thread; + struct rt_thread *sender; + rt_ubase_t handoff; + rt_bool_t urgent = RT_FALSE; + rt_bool_t held_ref = RT_FALSE; rt_base_t level; struct rt_mq_message *msg; rt_uint32_t tick_delta; @@ -3857,6 +4785,11 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, level = rt_spin_lock_irqsave(&(mq->spinlock)); + if (_mq_take_handoff(mq, thread, &msg)) + { + goto _mq_recv_copy; + } + /* for non-blocking call */ if (mq->entry == 0 && timeout == 0) { @@ -3913,10 +4846,26 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, /* re-schedule */ rt_schedule(); + /* A delete/detach that woke this thread has already cleared the + * record and may have released the object, so take the reserved + * message with an object reference before touching the object. */ + if (_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, &handoff, &urgent, _IPC_HANDOFF_CLAIM)) + { + level = rt_spin_lock_irqsave(&(mq->spinlock)); + if (!_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, RT_NULL, RT_NULL, _IPC_HANDOFF_FINISH)) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + _ipc_handoff_object_put(mq); + return -RT_ERROR; + } + msg = _mq_handoff_reorder(mq, (struct rt_mq_message *)handoff, urgent); + held_ref = RT_TRUE; + goto _mq_recv_copy; + } + /* recv message */ if (thread->error != RT_EOK) { - /* return error */ return thread->error; } @@ -3951,6 +4900,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, mq->entry--; } +_mq_recv_copy: rt_spin_unlock_irqrestore(&(mq->spinlock), level); /* get real message length */ @@ -3970,28 +4920,72 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, } #endif level = rt_spin_lock_irqsave(&(mq->spinlock)); - /* put message to free list */ - msg->next = (struct rt_mq_message *)mq->msg_queue_free; - mq->msg_queue_free = msg; - /* resume suspended thread */ + sender = RT_NULL; if (!rt_list_isempty(&(mq->suspend_sender_thread))) { - rt_susp_list_dequeue(&(mq->suspend_sender_thread), RT_EOK); + if (mq->parent.parent.flag == RT_IPC_FLAG_FIFO) + { + sender = _ipc_handoff_wakeup(&(mq->suspend_sender_thread), mq, + (rt_ubase_t)msg, _IPC_HANDOFF_MQ_TX, RT_FALSE); + if (sender != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + + RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent))); + + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + + rt_schedule(); + + return len; + } + } + + /* put message to free list */ + msg->next = (struct rt_mq_message *)mq->msg_queue_free; + mq->msg_queue_free = msg; + + if (sender == RT_NULL) + { + sender = rt_susp_list_dequeue(&(mq->suspend_sender_thread), RT_EOK); + } rt_spin_unlock_irqrestore(&(mq->spinlock), level); RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent))); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + rt_schedule(); return len; } + /* put message to free list */ + msg->next = (struct rt_mq_message *)mq->msg_queue_free; + mq->msg_queue_free = msg; + rt_spin_unlock_irqrestore(&(mq->spinlock), level); RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent))); + if (held_ref) + { + _ipc_handoff_object_put(mq); + } + + if (sender) + { + rt_schedule(); + } + return len; } @@ -4074,6 +5068,7 @@ rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg) rt_susp_list_resume_all(&mq->parent.suspend_thread, RT_ERROR); /* also resume all message queue private suspended thread */ rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR); + _ipc_handoff_clear(mq); /* release all message in the queue */ while (mq->msg_queue_head != RT_NULL) diff --git a/src/thread.c b/src/thread.c index 8a557a128d67..c35de51d3735 100644 --- a/src/thread.c +++ b/src/thread.c @@ -43,6 +43,11 @@ #define DBG_TAG "kernel.thread" #define DBG_LVL DBG_INFO + +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) +void rt_ipc_handoff_thread_cleanup(rt_thread_t thread); +#endif + #include #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) @@ -130,6 +135,10 @@ static void _thread_exit(void) _thread_detach_from_mutex(thread); +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) + rt_ipc_handoff_thread_cleanup(thread); +#endif + /* insert to defunct thread list */ rt_thread_defunct_enqueue(thread); @@ -231,6 +240,10 @@ static rt_err_t _thread_init(struct rt_thread *thread, thread->event_info = 0; #endif /* RT_USING_EVENT */ +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) + thread->handoff.thread = RT_NULL; +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ + /* error and flags */ thread->error = RT_EOK; @@ -520,6 +533,10 @@ static rt_err_t _thread_detach(rt_thread_t thread) _thread_detach_from_mutex(thread); +#if defined(RT_USING_MAILBOX) || defined(RT_USING_MESSAGEQUEUE) + rt_ipc_handoff_thread_cleanup(thread); +#endif + /* insert to defunct thread list */ rt_thread_defunct_enqueue(thread); diff --git a/src/utest/mailbox_tc.c b/src/utest/mailbox_tc.c index 6d7ce1328788..5e79d147c765 100644 --- a/src/utest/mailbox_tc.c +++ b/src/utest/mailbox_tc.c @@ -387,6 +387,381 @@ static void test_dynamic_mailbox_send_recv(void) uassert_true(1); } +static struct rt_mailbox handoff_mb; +static char handoff_mb_pool[8]; +static rt_err_t handoff_mb_result; +rt_align(RT_ALIGN_SIZE) static char handoff_mb_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread handoff_mb_thread; + +static void _mb_handoff_waiter(void *param) +{ + rt_ubase_t value = 0; + + handoff_mb_result = rt_mb_recv((rt_mailbox_t)param, &value, 5); +} + +static rt_bool_t _mb_handoff_waiter_blocked(rt_mailbox_t mb) +{ + int wait; + + for (wait = 0; wait < 200; wait++) + { + if (!rt_list_isempty(&(mb->parent.suspend_thread))) + { + return RT_TRUE; + } + + rt_thread_mdelay(1); + } + + return RT_FALSE; +} + +static void _mb_handoff_delete_case(rt_bool_t detach) +{ + rt_err_t ret; + rt_base_t critical; + rt_mailbox_t mb; + + if (detach) + { + ret = rt_mb_init(&handoff_mb, "mbh", handoff_mb_pool, 1, RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + mb = &handoff_mb; + } + else + { + mb = rt_mb_create("mbh", 1, RT_IPC_FLAG_FIFO); + uassert_true(mb != RT_NULL); + } + + handoff_mb_result = RT_EOK; + + ret = rt_thread_init(&handoff_mb_thread, "mb_handoff", _mb_handoff_waiter, mb, + handoff_mb_stack, sizeof(handoff_mb_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&handoff_mb_thread, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + ret = rt_thread_startup(&handoff_mb_thread); + uassert_true(ret == RT_EOK); + + uassert_true(_mb_handoff_waiter_blocked(mb)); + + critical = rt_enter_critical(); + ret = rt_mb_send(mb, 0x1234); + rt_exit_critical_safe(critical); + uassert_true(ret == RT_EOK); + ret = detach ? rt_mb_detach(mb) : rt_mb_delete(mb); + uassert_true(ret == RT_EOK); + + rt_thread_mdelay(20); + + uassert_int_equal(handoff_mb_result, -RT_ERROR); +} + +static void test_mb_handoff_delete_after_wakeup(void) +{ + _mb_handoff_delete_case(RT_FALSE); +} + +static void test_mb_handoff_detach_after_wakeup(void) +{ + _mb_handoff_delete_case(RT_TRUE); +} + +#define MB_HANDOFF_FLOOD_COUNT 65 + +static struct rt_mailbox mb_flood; +static char mb_flood_pool[MB_HANDOFF_FLOOD_COUNT * sizeof(rt_ubase_t)]; +static rt_uint32_t mb_flood_value[MB_HANDOFF_FLOOD_COUNT]; +static rt_bool_t mb_flood_done[MB_HANDOFF_FLOOD_COUNT]; +static rt_err_t mb_flood_thief; +static struct rt_semaphore mb_flood_go; + +static void _mb_flood_receiver(void *param) +{ + rt_ubase_t value = 0; + int idx = (int)(rt_ubase_t)param; + + if (rt_mb_recv(&mb_flood, &value, RT_WAITING_FOREVER) == RT_EOK) + { + mb_flood_value[idx] = (rt_uint32_t)value; + } + mb_flood_done[idx] = RT_TRUE; +} + +static void _mb_flood_thief(void *param) +{ + rt_ubase_t value = 0; + + RT_UNUSED(param); + rt_sem_take(&mb_flood_go, RT_WAITING_FOREVER); + mb_flood_thief = rt_mb_recv(&mb_flood, &value, RT_WAITING_NO); +} + +static int _mb_suspend_count(struct rt_mailbox *mb) +{ + rt_list_t *node; + int count = 0; + + for (node = mb->parent.suspend_thread.next; + node != &mb->parent.suspend_thread; + node = node->next) + { + count++; + } + + return count; +} + +static void test_mb_handoff_flood_fifo(void) +{ + rt_err_t ret; + rt_thread_t thread; + rt_base_t critical; + int wait; + int i; + + for (i = 0; i < MB_HANDOFF_FLOOD_COUNT; i++) + { + mb_flood_done[i] = RT_FALSE; + } + mb_flood_thief = RT_EOK; + + ret = rt_sem_init(&mb_flood_go, "mbf_go", 0, RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + ret = rt_mb_init(&mb_flood, "mbf", mb_flood_pool, MB_HANDOFF_FLOOD_COUNT, + RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + + for (i = 0; i < MB_HANDOFF_FLOOD_COUNT; i++) + { + thread = rt_thread_create("mbf_r", _mb_flood_receiver, + (void *)(rt_ubase_t)i, + UTEST_THR_STACK_SIZE, 24, 20); + uassert_true(thread != RT_NULL); + rt_thread_startup(thread); + } + + for (wait = 0; wait < 200; wait++) + { + if (_mb_suspend_count(&mb_flood) >= MB_HANDOFF_FLOOD_COUNT) + { + break; + } + rt_thread_mdelay(1); + } + uassert_int_equal(_mb_suspend_count(&mb_flood), MB_HANDOFF_FLOOD_COUNT); + + thread = rt_thread_create("mbf_t", _mb_flood_thief, RT_NULL, + UTEST_THR_STACK_SIZE, 15, 20); + uassert_true(thread != RT_NULL); + rt_thread_startup(thread); + rt_thread_mdelay(10); + + critical = rt_enter_critical(); + for (i = 0; i < MB_HANDOFF_FLOOD_COUNT; i++) + { + ret = rt_mb_send(&mb_flood, (rt_ubase_t)(i + 1)); + if (ret != RT_EOK) + { + break; + } + } + rt_sem_release(&mb_flood_go); + rt_exit_critical_safe(critical); + uassert_int_equal(i, MB_HANDOFF_FLOOD_COUNT); + + for (wait = 0; wait < 200; wait++) + { + if (mb_flood_thief != RT_EOK) + { + break; + } + rt_thread_mdelay(1); + } + uassert_int_equal(mb_flood_thief, -RT_ETIMEOUT); + + for (wait = 0; wait < 500; wait++) + { + rt_bool_t done = RT_TRUE; + + for (i = 0; i < MB_HANDOFF_FLOOD_COUNT; i++) + { + if (!mb_flood_done[i]) + { + done = RT_FALSE; + break; + } + } + if (done) + { + break; + } + rt_thread_mdelay(1); + } + + for (i = 0; i < MB_HANDOFF_FLOOD_COUNT; i++) + { + uassert_true(mb_flood_done[i]); + uassert_int_equal(mb_flood_value[i], (rt_uint32_t)(i + 1)); + } + + ret = rt_mb_detach(&mb_flood); + uassert_true(ret == RT_EOK); + rt_sem_detach(&mb_flood_go); +} + +static rt_err_t mb_tx_handoff_result; +static rt_ubase_t mb_tx_handoff_value; +rt_align(RT_ALIGN_SIZE) static char mb_tx_handoff_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread mb_tx_handoff_thread; + +static void _mb_tx_handoff_sender(void *param) +{ + mb_tx_handoff_result = rt_mb_send_wait((rt_mailbox_t)param, 0x5678, + RT_WAITING_FOREVER); +} + +static rt_bool_t _mb_sender_blocked(rt_mailbox_t mb) +{ + int wait; + + for (wait = 0; wait < 200; wait++) + { + if (!rt_list_isempty(&(mb->suspend_sender_thread))) + { + return RT_TRUE; + } + + rt_thread_mdelay(1); + } + + return RT_FALSE; +} + +static void _mb_tx_handoff_delete_case(rt_bool_t detach) +{ + rt_err_t ret; + rt_err_t recv_ret; + rt_base_t critical; + rt_mailbox_t mb; + + if (detach) + { + ret = rt_mb_init(&handoff_mb, "mbt", handoff_mb_pool, 1, RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + mb = &handoff_mb; + } + else + { + mb = rt_mb_create("mbt", 1, RT_IPC_FLAG_FIFO); + uassert_true(mb != RT_NULL); + } + + mb_tx_handoff_result = RT_EOK; + ret = rt_mb_send(mb, 0x1111); + uassert_true(ret == RT_EOK); + + ret = rt_thread_init(&mb_tx_handoff_thread, "mb_tx_hb", _mb_tx_handoff_sender, mb, + mb_tx_handoff_stack, sizeof(mb_tx_handoff_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&mb_tx_handoff_thread, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + ret = rt_thread_startup(&mb_tx_handoff_thread); + uassert_true(ret == RT_EOK); + + uassert_true(_mb_sender_blocked(mb)); + + critical = rt_enter_critical(); + recv_ret = rt_mb_recv(mb, &mb_tx_handoff_value, RT_WAITING_FOREVER); + ret = detach ? rt_mb_detach(mb) : rt_mb_delete(mb); + rt_exit_critical_safe(critical); + uassert_true(recv_ret == RT_EOK); + uassert_true(ret == RT_EOK); + + rt_thread_mdelay(20); + + uassert_int_equal(mb_tx_handoff_result, -RT_ERROR); +} + +static void test_mb_handoff_tx_delete_after_wakeup(void) +{ + _mb_tx_handoff_delete_case(RT_FALSE); +} + +static void test_mb_handoff_tx_detach_after_wakeup(void) +{ + _mb_tx_handoff_delete_case(RT_TRUE); +} + +static struct rt_mailbox mb_fifo_order_mb; +static char mb_fifo_order_pool[8]; +static rt_ubase_t mb_fifo_order_val[2]; +static rt_bool_t mb_fifo_order_done; +static rt_err_t mb_fifo_order_result; +rt_align(RT_ALIGN_SIZE) static char mb_fifo_order_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread mb_fifo_order_thread; + +static void _mb_fifo_order_receiver(void *param) +{ + rt_ubase_t value = 0; + + RT_UNUSED(param); + mb_fifo_order_result = rt_mb_recv(&mb_fifo_order_mb, &value, RT_WAITING_FOREVER); + mb_fifo_order_val[0] = value; + mb_fifo_order_result = rt_mb_recv(&mb_fifo_order_mb, &value, RT_WAITING_FOREVER); + mb_fifo_order_val[1] = value; + mb_fifo_order_done = RT_TRUE; +} + +static void test_mb_fifo_order(void) +{ + rt_err_t ret; + rt_base_t critical; + int wait; + + mb_fifo_order_done = RT_FALSE; + mb_fifo_order_val[0] = 0; + mb_fifo_order_val[1] = 0; + + ret = rt_mb_init(&mb_fifo_order_mb, "mbo", mb_fifo_order_pool, 2, RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + + ret = rt_thread_init(&mb_fifo_order_thread, "mb_fifo", _mb_fifo_order_receiver, RT_NULL, + mb_fifo_order_stack, sizeof(mb_fifo_order_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&mb_fifo_order_thread, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + ret = rt_thread_startup(&mb_fifo_order_thread); + uassert_true(ret == RT_EOK); + + uassert_true(_mb_handoff_waiter_blocked(&mb_fifo_order_mb)); + + critical = rt_enter_critical(); + ret = rt_mb_send(&mb_fifo_order_mb, 111); + uassert_true(ret == RT_EOK); + ret = rt_mb_send(&mb_fifo_order_mb, 222); + rt_exit_critical_safe(critical); + uassert_true(ret == RT_EOK); + + for (wait = 0; wait < 200 && !mb_fifo_order_done; wait++) + { + rt_thread_mdelay(1); + } + + uassert_true(mb_fifo_order_done); + uassert_int_equal(mb_fifo_order_result, RT_EOK); + uassert_int_equal(mb_fifo_order_val[0], 111); + uassert_int_equal(mb_fifo_order_val[1], 222); + + ret = rt_mb_detach(&mb_fifo_order_mb); + uassert_true(ret == RT_EOK); +} + static rt_err_t utest_tc_init(void) { /* This testcase may be run more than once in a single boot. The finish @@ -416,5 +791,11 @@ static void testcase(void) UTEST_UNIT_RUN(test_mailbox_delete); UTEST_UNIT_RUN(test_static_mailbox_send_recv); UTEST_UNIT_RUN(test_dynamic_mailbox_send_recv); + UTEST_UNIT_RUN(test_mb_handoff_delete_after_wakeup); + UTEST_UNIT_RUN(test_mb_handoff_detach_after_wakeup); + UTEST_UNIT_RUN(test_mb_fifo_order); + UTEST_UNIT_RUN(test_mb_handoff_tx_delete_after_wakeup); + UTEST_UNIT_RUN(test_mb_handoff_tx_detach_after_wakeup); + UTEST_UNIT_RUN(test_mb_handoff_flood_fifo); } UTEST_TC_EXPORT(testcase, "core.ipc_mailbox", utest_tc_init, utest_tc_cleanup, 60); diff --git a/src/utest/messagequeue_tc.c b/src/utest/messagequeue_tc.c index 118ead210a36..198360a2046a 100644 --- a/src/utest/messagequeue_tc.c +++ b/src/utest/messagequeue_tc.c @@ -29,6 +29,9 @@ * - Reset the message queue and verify it is empty * - Detach and delete message queues, ensuring resources are released * - Use multiple threads to simulate concurrent send/receive operations + * - When message priority is enabled, verify an urgent handoff is not overtaken + * by a higher priority message queued before the receiver runs, and that a + * detached receiver hands an in-flight urgent handoff back to the queue * * Verification Metrics: * - All uassert assertions pass without failure @@ -233,6 +236,215 @@ static void test_mq_delete(void) #endif /* RT_USING_HEAP */ } +static struct rt_messagequeue fifo_mq; +static rt_uint8_t fifo_mq_buf[RT_MQ_BUF_SIZE(MSG_SIZE, MAX_MSGS)]; +static rt_uint32_t fifo_recv_val[2]; +/* Keep the receiver stack off the utest thread stack: the utest thread uses + * UTEST_THR_STACK_SIZE itself, so a local array of that size overflows it. */ +static rt_uint8_t fifo_recv_stack[UTEST_THR_STACK_SIZE]; + +static struct rt_thread fifo_recv_th; +static rt_uint32_t v1 = 111, v2 = 222; + +static void _fifo_recv_entry(void *param) +{ + rt_mq_recv(&fifo_mq, &fifo_recv_val[0], sizeof(fifo_recv_val[0]), RT_WAITING_FOREVER); + rt_mq_recv(&fifo_mq, &fifo_recv_val[1], sizeof(fifo_recv_val[1]), RT_WAITING_FOREVER); +} + +static void test_mq_fifo_order(void) +{ + rt_err_t ret; + + ret = rt_mq_init(&fifo_mq, "mq_fifo", fifo_mq_buf, MSG_SIZE, sizeof(fifo_mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + + fifo_recv_val[0] = 0; + fifo_recv_val[1] = 0; + + ret = rt_thread_init(&fifo_recv_th, "th_recv", _fifo_recv_entry, RT_NULL, fifo_recv_stack, sizeof(fifo_recv_stack), 24, 20); + uassert_true(ret == RT_EOK); + rt_thread_startup(&fifo_recv_th); + + /* allow recv thread to block on empty mq */ + rt_thread_mdelay(20); + + /* send messages after receiver has blocked */ + ret = rt_mq_send(&fifo_mq, &v1, sizeof(v1)); + uassert_true(ret == RT_EOK); + ret = rt_mq_send(&fifo_mq, &v2, sizeof(v2)); + uassert_true(ret == RT_EOK); + + rt_thread_mdelay(20); + + uassert_int_equal(fifo_recv_val[0], 111); + uassert_int_equal(fifo_recv_val[1], 222); + + rt_mq_detach(&fifo_mq); +} + +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY +static struct rt_messagequeue urgent_mq; +static rt_uint8_t urgent_mq_buf[RT_MQ_BUF_SIZE(MSG_SIZE, 2)]; +static rt_uint32_t urgent_recv_val[2]; +static rt_int32_t urgent_recv_prio[2]; +static struct rt_thread urgent_recv_th; +static rt_uint8_t urgent_recv_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread urgent_cleanup_th; +static rt_uint8_t urgent_cleanup_stack[UTEST_THR_STACK_SIZE]; +static rt_uint32_t urgent_cleanup_val; + +static void _urgent_recv_entry(void *param) +{ + rt_mq_recv_prio(&urgent_mq, &urgent_recv_val[0], sizeof(urgent_recv_val[0]), + &urgent_recv_prio[0], RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE); + rt_mq_recv_prio(&urgent_mq, &urgent_recv_val[1], sizeof(urgent_recv_val[1]), + &urgent_recv_prio[1], RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE); +} + +static void _urgent_cleanup_entry(void *param) +{ + rt_mq_recv(&urgent_mq, &urgent_cleanup_val, sizeof(urgent_cleanup_val), + RT_WAITING_FOREVER); +} + +/* wait until the receiver is actually blocked on the queue */ +static rt_bool_t _wait_receiver_blocked(void) +{ + int wait; + + for (wait = 0; wait < 200; wait++) + { + if (!rt_list_isempty(&(urgent_mq.parent.suspend_thread))) + { + return RT_TRUE; + } + + rt_thread_mdelay(1); + } + + return RT_FALSE; +} + +static void test_mq_urgent_handoff_priority(void) +{ + rt_err_t ret; + rt_err_t urgent_ret; + rt_err_t normal_ret; + rt_base_t critical; + rt_ssize_t len; + rt_uint32_t seed = 0; + rt_uint32_t urgent_val = 111; + rt_uint32_t normal_val = 222; + rt_int32_t normal_prio = 7; + int i; + + ret = rt_mq_init(&urgent_mq, "mq_urgent", urgent_mq_buf, MSG_SIZE, + sizeof(urgent_mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + uassert_int_equal(urgent_mq.max_msgs, 2); + + /* Leave a stale low priority on both message nodes: rt_mq_urgent() takes a + * free node without touching its priority, so that value is what a + * non-urgent handoff would be re-sorted with. */ + for (i = 0; i < 2; i++) + { + ret = rt_mq_send_wait_prio(&urgent_mq, &seed, sizeof(seed), 0, 0, + RT_UNINTERRUPTIBLE); + uassert_true(ret == RT_EOK); + } + + for (i = 0; i < 2; i++) + { + len = rt_mq_recv_prio(&urgent_mq, &seed, sizeof(seed), RT_NULL, + RT_WAITING_FOREVER, RT_UNINTERRUPTIBLE); + uassert_true(len >= 0); + } + uassert_int_equal(urgent_mq.entry, 0); + + urgent_recv_val[0] = 0; + urgent_recv_val[1] = 0; + + ret = rt_thread_init(&urgent_recv_th, "th_urgent", _urgent_recv_entry, RT_NULL, + urgent_recv_stack, sizeof(urgent_recv_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&urgent_recv_th, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + rt_thread_startup(&urgent_recv_th); + + uassert_true(_wait_receiver_blocked()); + + /* urgent handoff first, then a higher priority normal message, both before + * the receiver can run: scheduling stays blocked on this CPU. */ + critical = rt_enter_critical(); + urgent_ret = rt_mq_urgent(&urgent_mq, &urgent_val, sizeof(urgent_val)); + normal_ret = rt_mq_send_wait_prio(&urgent_mq, &normal_val, sizeof(normal_val), + normal_prio, 0, RT_UNINTERRUPTIBLE); + rt_exit_critical_safe(critical); + + uassert_true(urgent_ret == RT_EOK); + uassert_true(normal_ret == RT_EOK); + + rt_thread_mdelay(50); + + /* the urgent handoff must not be overtaken by the priority message */ + uassert_int_equal(urgent_recv_val[0], urgent_val); + uassert_int_equal(urgent_recv_val[1], normal_val); + + rt_mq_detach(&urgent_mq); +} + +static void test_mq_urgent_handoff_cleanup(void) +{ + rt_err_t ret; + rt_err_t detach_ret; + rt_base_t critical; + rt_ssize_t len; + rt_uint32_t urgent_val = 333; + rt_uint32_t recv_val = 0; + + ret = rt_mq_init(&urgent_mq, "mq_uc", urgent_mq_buf, MSG_SIZE, + sizeof(urgent_mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + + urgent_cleanup_val = 0; + + ret = rt_thread_init(&urgent_cleanup_th, "th_uc", _urgent_cleanup_entry, RT_NULL, + urgent_cleanup_stack, sizeof(urgent_cleanup_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&urgent_cleanup_th, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + rt_thread_startup(&urgent_cleanup_th); + + uassert_true(_wait_receiver_blocked()); + + /* the handoff record exists before the receiver becomes ready */ + ret = rt_mq_urgent(&urgent_mq, &urgent_val, sizeof(urgent_val)); + uassert_true(ret == RT_EOK); + + /* detach the receiver before it ever runs: the thread cleanup must find the + * record and put the urgent message back at the queue head */ + critical = rt_enter_critical(); + detach_ret = rt_thread_detach(&urgent_cleanup_th); + rt_exit_critical_safe(critical); + + uassert_true(detach_ret == RT_EOK); + + /* let the idle thread reclaim the detached thread object */ + rt_thread_mdelay(50); + + uassert_int_equal(urgent_mq.entry, 1); + + len = rt_mq_recv(&urgent_mq, &recv_val, sizeof(recv_val), RT_WAITING_FOREVER); + uassert_true(len >= 0); + uassert_int_equal(recv_val, urgent_val); + + rt_mq_detach(&urgent_mq); +} +#endif /* RT_USING_MESSAGEQUEUE_PRIORITY */ + static rt_err_t utest_tc_init(void) { rt_err_t ret; @@ -262,6 +474,178 @@ static rt_err_t utest_tc_init(void) return RT_EOK; } +static struct rt_messagequeue handoff_mq; +static rt_uint8_t handoff_mq_buf[64]; +static rt_err_t handoff_mq_result; +rt_align(RT_ALIGN_SIZE) static char handoff_mq_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread handoff_mq_thread; + +static void _mq_handoff_waiter(void *param) +{ + rt_uint32_t value = 0; + + handoff_mq_result = rt_mq_recv((rt_mq_t)param, &value, sizeof(value), 5); +} + +static rt_bool_t _mq_handoff_waiter_blocked(rt_mq_t mq) +{ + int wait; + + for (wait = 0; wait < 200; wait++) + { + if (!rt_list_isempty(&(mq->parent.suspend_thread))) + { + return RT_TRUE; + } + + rt_thread_mdelay(1); + } + + return RT_FALSE; +} + +static void _mq_handoff_delete_case(rt_bool_t detach) +{ + rt_err_t ret; + rt_base_t critical; + rt_mq_t mq; + rt_uint32_t value = 0x1234; + + if (detach) + { + ret = rt_mq_init(&handoff_mq, "mqh", handoff_mq_buf, 4, + sizeof(handoff_mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + mq = &handoff_mq; + } + else + { + mq = rt_mq_create("mqh", 4, 1, RT_IPC_FLAG_FIFO); + uassert_true(mq != RT_NULL); + } + + handoff_mq_result = RT_EOK; + + ret = rt_thread_init(&handoff_mq_thread, "mq_handoff", _mq_handoff_waiter, mq, + handoff_mq_stack, sizeof(handoff_mq_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&handoff_mq_thread, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + ret = rt_thread_startup(&handoff_mq_thread); + uassert_true(ret == RT_EOK); + + uassert_true(_mq_handoff_waiter_blocked(mq)); + + critical = rt_enter_critical(); + ret = rt_mq_send(mq, &value, sizeof(value)); + rt_exit_critical_safe(critical); + uassert_true(ret == RT_EOK); + ret = detach ? rt_mq_detach(mq) : rt_mq_delete(mq); + uassert_true(ret == RT_EOK); + + rt_thread_mdelay(20); + + uassert_int_equal(handoff_mq_result, -RT_ERROR); +} + +static void test_mq_handoff_delete_after_wakeup(void) +{ + _mq_handoff_delete_case(RT_FALSE); +} + +static void test_mq_handoff_detach_after_wakeup(void) +{ + _mq_handoff_delete_case(RT_TRUE); +} + +static rt_err_t mq_tx_handoff_result; +rt_align(RT_ALIGN_SIZE) static char mq_tx_handoff_stack[UTEST_THR_STACK_SIZE]; +static struct rt_thread mq_tx_handoff_thread; + +static void _mq_tx_handoff_sender(void *param) +{ + rt_uint32_t value = 0x5678; + + mq_tx_handoff_result = rt_mq_send_wait((rt_mq_t)param, &value, sizeof(value), + RT_WAITING_FOREVER); +} + +static rt_bool_t _mq_sender_blocked(rt_mq_t mq) +{ + int wait; + + for (wait = 0; wait < 200; wait++) + { + if (!rt_list_isempty(&(mq->suspend_sender_thread))) + { + return RT_TRUE; + } + + rt_thread_mdelay(1); + } + + return RT_FALSE; +} + +static void _mq_tx_handoff_delete_case(rt_bool_t detach) +{ + rt_err_t ret; + rt_base_t critical; + rt_mq_t mq; + rt_uint32_t value = 0x1111; + rt_ssize_t len; + + if (detach) + { + ret = rt_mq_init(&handoff_mq, "mqt", handoff_mq_buf, 4, + sizeof(handoff_mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); + mq = &handoff_mq; + } + else + { + mq = rt_mq_create("mqt", 4, 1, RT_IPC_FLAG_FIFO); + uassert_true(mq != RT_NULL); + } + + mq_tx_handoff_result = RT_EOK; + ret = rt_mq_send(mq, &value, sizeof(value)); + uassert_true(ret == RT_EOK); + + ret = rt_thread_init(&mq_tx_handoff_thread, "mq_tx_hb", _mq_tx_handoff_sender, mq, + mq_tx_handoff_stack, sizeof(mq_tx_handoff_stack), 24, 20); + uassert_true(ret == RT_EOK); +#ifdef RT_USING_SMP + rt_thread_control(&mq_tx_handoff_thread, RT_THREAD_CTRL_BIND_CPU, (void *)0); +#endif + ret = rt_thread_startup(&mq_tx_handoff_thread); + uassert_true(ret == RT_EOK); + + uassert_true(_mq_sender_blocked(mq)); + + critical = rt_enter_critical(); + len = rt_mq_recv(mq, &value, sizeof(value), RT_WAITING_FOREVER); + ret = detach ? rt_mq_detach(mq) : rt_mq_delete(mq); + rt_exit_critical_safe(critical); + uassert_true(len >= 0); + uassert_true(ret == RT_EOK); + + rt_thread_mdelay(20); + + uassert_int_equal(mq_tx_handoff_result, -RT_ERROR); +} + +static void test_mq_handoff_tx_delete_after_wakeup(void) +{ + _mq_tx_handoff_delete_case(RT_FALSE); +} + +static void test_mq_handoff_tx_detach_after_wakeup(void) +{ + _mq_tx_handoff_delete_case(RT_TRUE); +} + static rt_err_t utest_tc_cleanup(void) { /* wait for threads to finish */ @@ -278,5 +662,14 @@ static void testcase(void) UTEST_UNIT_RUN(test_mq_testcase); UTEST_UNIT_RUN(test_mq_detach); UTEST_UNIT_RUN(test_mq_delete); + UTEST_UNIT_RUN(test_mq_fifo_order); + UTEST_UNIT_RUN(test_mq_handoff_delete_after_wakeup); + UTEST_UNIT_RUN(test_mq_handoff_detach_after_wakeup); + UTEST_UNIT_RUN(test_mq_handoff_tx_delete_after_wakeup); + UTEST_UNIT_RUN(test_mq_handoff_tx_detach_after_wakeup); +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + UTEST_UNIT_RUN(test_mq_urgent_handoff_priority); + UTEST_UNIT_RUN(test_mq_urgent_handoff_cleanup); +#endif } UTEST_TC_EXPORT(testcase, "core.messagequeue", utest_tc_init, utest_tc_cleanup, 1000);