This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f216bf56de05f857b102ede1407203d6a297a7ec
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Nov 18 21:03:47 2026 +0800

    include/nuttx/mpsc.h: report empty only when the stub is the only node
    
    mpsc_empty() compared the consumer cursor with the tail:
    
      return head == tail;
    
    mpsc_dequeue() advances q->head to the node after the one it returns, so
    after the node before the last is returned, head is the last node, its
    next is NULL and head == tail - mpsc_empty() reports empty while one
    node is still dequeueable.  Observed on sim:nsh with four enqueues and
    three dequeues: mpsc_empty() returns true and the fourth dequeue returns
    the remaining node.
    
    The same early-true makes mpsc_dequeue_spin()/mpsc_dequeue_yield() return
    NULL one node early, and lets a caller using the documented
    mpsc_dequeue() + mpsc_empty() pattern sleep with a node left pending.
    
    The queue is empty only once the consumer is back on the stub and no
    producer is between the tail xchg and the prev->next store, so check
    head == &q->stub as well.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/mpsc.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/include/nuttx/mpsc.h b/include/nuttx/mpsc.h
index 882723e7421..d8ddfbe2cae 100644
--- a/include/nuttx/mpsc.h
+++ b/include/nuttx/mpsc.h
@@ -229,11 +229,19 @@ static inline_function bool mpsc_empty(FAR struct 
mpsc_queue_s *q)
       return false;
     }
 
-  /* No linked successor; verify no producer is in flight. */
+  /* No linked successor. Still not empty while the consumer sits on a
+   * node it has not returned yet (head != stub), or while a producer
+   * has won the xchg but not completed the prev->next store.
+   */
+
+  if (head != &q->stub)
+    {
+      return false;
+    }
 
   tail = (FAR struct mpsc_node_s *)
          (intptr_t)atomic_ptr_read(&q->tail);
-  return head == tail;
+  return tail == &q->stub;
 }
 
 /****************************************************************************

Reply via email to