diff --git a/.github/utest/configs/components/lwip.cfg b/.github/utest/configs/components/lwip.cfg index 7f8b6e53e7a..4548e3d9a05 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 fd6dcdb585e..9324c8d900e 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 7ad79b0e259..921df4f44ae 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 d2cda865081..258c6a10d4e 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -998,6 +998,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 177ea877831..b9bc9358805 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -420,6 +420,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 0a690d65b38..2120e93281f 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -62,6 +62,642 @@ 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) +#ifdef RT_USING_NANO +#define _IPC_HANDOFF_MAX 16 +#else +#define _IPC_HANDOFF_MAX 64 +#endif /* RT_USING_NANO */ + +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 + +struct _ipc_handoff +{ + void *ipc; + rt_thread_t thread; + rt_ubase_t value; + rt_uint8_t kind; + rt_uint8_t urgent; +}; + +/* FIFO handoff records cover the wakeup-to-dequeue window. If all records are + * busy, mb/mq keep the original shared-queue path instead of failing. */ +static struct _ipc_handoff _ipc_handoffs[_IPC_HANDOFF_MAX]; +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; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread == RT_NULL) + { + _ipc_handoffs[i].ipc = ipc; + _ipc_handoffs[i].thread = thread; + _ipc_handoffs[i].value = value; + _ipc_handoffs[i].kind = kind; + _ipc_handoffs[i].urgent = urgent ? 1 : 0; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_TRUE; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return RT_FALSE; +} + +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_base_t level; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread == thread && + _ipc_handoffs[i].ipc == ipc && + _ipc_handoffs[i].kind == kind) + { + if (value != RT_NULL) + { + *value = _ipc_handoffs[i].value; + } + if (urgent != RT_NULL) + { + *urgent = _ipc_handoffs[i].urgent ? RT_TRUE : RT_FALSE; + } + _ipc_handoffs[i].thread = RT_NULL; + _ipc_handoffs[i].urgent = 0; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_TRUE; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return RT_FALSE; +} + +static rt_bool_t _ipc_handoff_take(void *ipc, rt_thread_t thread, + rt_uint8_t kind, rt_ubase_t *value) +{ + return _ipc_handoff_take_ex(ipc, thread, kind, value, RT_NULL); +} + +/* 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); + rt_sched_unlock(slvl); + return RT_NULL; + } + + thread->error = RT_EOK; + rt_sched_unlock(slvl); + + return thread; +} + +#ifdef RT_USING_MAILBOX +static rt_uint32_t _ipc_handoff_count(void *ipc, rt_uint8_t kind) +{ + rt_base_t level; + rt_size_t i; + rt_uint32_t count = 0; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == ipc && + _ipc_handoffs[i].kind == kind) + { + count++; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + return count; +} + +static void _ipc_handoff_clear(void *ipc) +{ + rt_base_t level; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == ipc) + { + _ipc_handoffs[i].thread = RT_NULL; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); +} + +static rt_uint32_t _mb_occupied_count(rt_mailbox_t mb) +{ + return mb->entry + _ipc_handoff_count(mb, _IPC_HANDOFF_MB_TX) + + _ipc_handoff_count(mb, _IPC_HANDOFF_MB_RX); +} + +static void _mb_tx_return(rt_mailbox_t mb, rt_thread_t thread) +{ + struct rt_thread *next; + rt_ubase_t value; + rt_base_t level; + rt_bool_t taken; + + level = rt_spin_lock_irqsave(&(mb->spinlock)); + taken = _ipc_handoff_take(mb, thread, _IPC_HANDOFF_MB_TX, &value); + if (!taken) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + return; + } + + 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, + _IPC_HANDOFF_MB_TX, RT_FALSE); + if (next == RT_NULL) + { + /* no record available: wake the sender to retry the shared queue */ + rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK); + } + } + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + + rt_schedule(); +} + +static void _mb_rx_return(rt_mailbox_t mb, rt_thread_t thread) +{ + struct rt_thread *next; + rt_ubase_t value; + rt_base_t level; + rt_bool_t taken; + rt_bool_t urgent = RT_FALSE; + + level = rt_spin_lock_irqsave(&(mb->spinlock)); + taken = _ipc_handoff_take_ex(mb, thread, _IPC_HANDOFF_MB_RX, &value, &urgent); + if (!taken) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + return; + } + + if (!rt_list_isempty(&(mb->parent.suspend_thread))) + { + next = _ipc_handoff_wakeup(&(mb->parent.suspend_thread), mb, value, + _IPC_HANDOFF_MB_RX, urgent); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mb->spinlock), level); + 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); + + rt_schedule(); +} + +static rt_bool_t _mb_rx_handoff_replace_urgent(rt_mailbox_t mb, rt_ubase_t urgent_val) +{ + rt_base_t level; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == mb && + _ipc_handoffs[i].kind == _IPC_HANDOFF_MB_RX) + { + rt_ubase_t prev_val = _ipc_handoffs[i].value; + _ipc_handoffs[i].value = urgent_val; + _ipc_handoffs[i].urgent = 1; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + /* 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; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_FALSE; +} +#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 void _mq_handoff_clear(rt_mq_t mq) +{ + struct rt_mq_message *msg; + rt_base_t level; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == mq && + (_ipc_handoffs[i].kind == _IPC_HANDOFF_MQ_RX || + _ipc_handoffs[i].kind == _IPC_HANDOFF_MQ_TX)) + { + msg = (struct rt_mq_message *)_ipc_handoffs[i].value; + msg->next = (struct rt_mq_message *)mq->msg_queue_free; + mq->msg_queue_free = msg; + _ipc_handoffs[i].thread = RT_NULL; + } + else if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == mq) + { + _ipc_handoffs[i].thread = RT_NULL; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); +} + +static void _mq_tx_return(rt_mq_t mq, rt_thread_t thread) +{ + struct rt_thread *next; + struct rt_mq_message *msg; + rt_ubase_t value; + rt_base_t level; + rt_bool_t taken; + + level = rt_spin_lock_irqsave(&(mq->spinlock)); + taken = _ipc_handoff_take(mq, thread, _IPC_HANDOFF_MQ_TX, &value); + if (!taken) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + return; + } + + msg = (struct rt_mq_message *)value; + 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, + _IPC_HANDOFF_MQ_TX, RT_FALSE); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + rt_schedule(); + return; + } + /* no record available: wake the sender to retry the shared queue */ + 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; + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + + rt_schedule(); +} + +static void _mq_rx_return(rt_mq_t mq, rt_thread_t thread) +{ + struct rt_thread *next; + struct rt_mq_message *msg; + rt_ubase_t value; + rt_base_t level; + rt_bool_t taken; + rt_bool_t urgent = RT_FALSE; + + level = rt_spin_lock_irqsave(&(mq->spinlock)); + taken = _ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, &value, &urgent); + if (!taken) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + return; + } + + msg = (struct rt_mq_message *)value; + if (!rt_list_isempty(&(mq->parent.suspend_thread))) + { + next = _ipc_handoff_wakeup(&(mq->parent.suspend_thread), mq, value, + _IPC_HANDOFF_MQ_RX, urgent); + if (next != RT_NULL) + { + rt_spin_unlock_irqrestore(&(mq->spinlock), level); + rt_schedule(); + return; + } + } + if (mq->entry < mq->max_msgs) + { +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + if (!urgent) + { + _mq_prio_insert(mq, msg); + } + else + { + /* an urgent message always goes back to the front */ + 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); + + rt_schedule(); +} + +static rt_bool_t _mq_rx_handoff_replace_urgent(rt_mq_t mq, struct rt_mq_message *urgent_msg) +{ + rt_base_t level; + rt_size_t i; + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread != RT_NULL && + _ipc_handoffs[i].ipc == mq && + _ipc_handoffs[i].kind == _IPC_HANDOFF_MQ_RX) + { + struct rt_mq_message *prev_msg = (struct rt_mq_message *)_ipc_handoffs[i].value; + _ipc_handoffs[i].value = (rt_ubase_t)urgent_msg; + _ipc_handoffs[i].urgent = 1; + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + + /* 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; + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + return RT_FALSE; +} + +/* 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. */ +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; +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + rt_bool_t urgent = RT_FALSE; +#endif + struct rt_mq_message *msg; + +#ifdef RT_USING_MESSAGEQUEUE_PRIORITY + if (!_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, &value, &urgent)) +#else + if (!_ipc_handoff_take_ex(mq, thread, _IPC_HANDOFF_MQ_RX, &value, RT_NULL)) +#endif + { + return RT_FALSE; + } + + msg = (struct rt_mq_message *)value; + +#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--; + } + } +#endif + + *msg_out = msg; + + return RT_TRUE; +} +#endif /* RT_USING_MESSAGEQUEUE */ + +void rt_ipc_handoff_thread_cleanup(rt_thread_t thread) +{ + rt_base_t level; + rt_size_t i; + + if (thread == RT_NULL) + { + return; + } + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + for (i = 0; i < _IPC_HANDOFF_MAX; i++) + { + if (_ipc_handoffs[i].thread == thread) + { + void *ipc = _ipc_handoffs[i].ipc; + rt_uint8_t kind = _ipc_handoffs[i].kind; + + /* Hold an object reference across the window where the table lock + * is dropped: another core may delete the object and free it, and + * the reclaim below would then take the lock of freed memory. */ + _ipc_handoff_object_get(ipc); + + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); + +#ifdef RT_USING_MAILBOX + if (kind == _IPC_HANDOFF_MB_RX) + { + _mb_rx_return((rt_mailbox_t)ipc, thread); + } + else if (kind == _IPC_HANDOFF_MB_TX) + { + _mb_tx_return((rt_mailbox_t)ipc, thread); + } +#endif +#ifdef RT_USING_MESSAGEQUEUE + if (kind == _IPC_HANDOFF_MQ_RX) + { + _mq_rx_return((rt_mq_t)ipc, thread); + } + else if (kind == _IPC_HANDOFF_MQ_TX) + { + _mq_tx_return((rt_mq_t)ipc, thread); + } +#endif + /* The reclaim is over: drop the reference; an object delete that + * was waiting on it may now release the object. */ + _ipc_handoff_object_put(ipc); + + level = rt_spin_lock_irqsave(&_ipc_handoff_lock); + } + } + rt_spin_unlock_irqrestore(&_ipc_handoff_lock, level); +} +#endif /* RT_USING_MAILBOX || RT_USING_MESSAGEQUEUE */ + /** * @addtogroup group_thread_comm * @{ @@ -83,6 +719,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; } @@ -2447,6 +3084,7 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb) 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); /* detach mailbox object */ @@ -2565,8 +3203,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 +3255,12 @@ 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_ubase_t handoff; /* parameter check */ RT_ASSERT(mb != RT_NULL); @@ -2629,14 +3280,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; @@ -2684,10 +3336,12 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, if (thread->error != RT_EOK) { /* return error */ + _mb_tx_return(mb, thread); return thread->error; } level = rt_spin_lock_irqsave(&(mb->spinlock)); + has_tx_slot = _ipc_handoff_take(mb, thread, _IPC_HANDOFF_MB_TX, &handoff); /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) @@ -2701,6 +3355,20 @@ 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); + rt_schedule(); + return RT_EOK; + } + } + /* set ptr */ mb->msg_pool[mb->in_offset] = value; /* increase input offset */ @@ -2722,7 +3390,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, } /* 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); @@ -2734,6 +3402,11 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, } rt_spin_unlock_irqrestore(&(mb->spinlock), level); + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } @@ -2813,6 +3486,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 +3497,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 +3542,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 +3554,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 +3591,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 +3613,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(mb, thread, _IPC_HANDOFF_MB_RX, &handoff)) + { + *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,13 +3696,42 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo /* re-schedule */ rt_schedule(); + level = rt_spin_lock_irqsave(&(mb->spinlock)); + + if (_ipc_handoff_take(mb, thread, _IPC_HANDOFF_MB_RX, &handoff)) + { + *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; + } + /* resume from suspend state */ if (thread->error != RT_EOK) { - /* return error */ + rt_spin_unlock_irqrestore(&(mb->spinlock), level); 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) @@ -3004,7 +3764,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 +3841,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; @@ -3233,6 +4004,7 @@ rt_err_t rt_mq_detach(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); + _mq_handoff_clear(mq); rt_spin_unlock_irqrestore(&(mq->spinlock), level); /* detach message queue object */ @@ -3376,9 +4148,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); + _mq_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 +4213,12 @@ 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_ubase_t handoff; RT_UNUSED(prio); @@ -3473,7 +4257,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; @@ -3521,9 +4306,15 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, if (thread->error != RT_EOK) { /* return error */ + _mq_tx_return(mq, thread); return thread->error; } level = rt_spin_lock_irqsave(&(mq->spinlock)); + has_tx_slot = _ipc_handoff_take(mq, thread, _IPC_HANDOFF_MQ_TX, &handoff); + if (has_tx_slot) + { + msg = (struct rt_mq_message *)handoff; + } /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) @@ -3538,7 +4329,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,6 +4346,24 @@ 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); + rt_schedule(); + return RT_EOK; + } + } + #ifdef RT_USING_MESSAGEQUEUE_PRIORITY msg->prio = prio; if (mq->msg_queue_head == RT_NULL) @@ -3615,7 +4427,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, } /* 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); @@ -3627,6 +4439,11 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, } rt_spin_unlock_irqrestore(&(mq->spinlock), level); + if (receiver) + { + rt_schedule(); + } + return RT_EOK; } @@ -3717,6 +4534,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 +4573,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 +4616,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 +4629,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 +4676,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, int suspend_flag) { struct rt_thread *thread; + struct rt_thread *sender; rt_base_t level; struct rt_mq_message *msg; rt_uint32_t tick_delta; @@ -3857,6 +4702,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,15 +4763,20 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, /* re-schedule */ rt_schedule(); + level = rt_spin_lock_irqsave(&(mq->spinlock)); + + if (_mq_take_handoff(mq, thread, &msg)) + { + goto _mq_recv_copy; + } + /* recv message */ if (thread->error != RT_EOK) { - /* return error */ + rt_spin_unlock_irqrestore(&(mq->spinlock), level); return thread->error; } - level = rt_spin_lock_irqsave(&(mq->spinlock)); - /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) { @@ -3951,6 +4806,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,14 +4826,34 @@ 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))); + + 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); @@ -3988,10 +4864,19 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, 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 (sender) + { + rt_schedule(); + } + return len; } @@ -4074,6 +4959,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); + _mq_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 8a557a128d6..e0f6b16210c 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); @@ -520,6 +529,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/messagequeue_tc.c b/src/utest/messagequeue_tc.c index 118ead210a3..742b778de04 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; @@ -278,5 +490,10 @@ 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); +#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);