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 780bbde2a0cf6c3ef23f07026bd628c906fa2f28
Author: wangjianyu3 <[email protected]>
AuthorDate: Mon Jun 1 18:39:51 2026 +0800

    include/ring: report drop count from ring_reset
    
    ring_reset() previously returned void and silently discarded all
    elements still in the ring at the time of the call.  Callers that
    care about loss accounting (note backends in particular) had no way
    to learn how many elements were dropped, forcing them to either
    sample ring_count() before the reset (racy) or stop tracking drops
    entirely on NOTE_CLEAR-style operations.
    
    Convert the macro to a GCC statement expression that snapshots the
    producer/consumer index delta before zeroing the four indices and
    yields it to the caller.  All existing void-context callers
    (ring_init_magic warm-restart fallback, ring_init_magic first-time
    init, ring_spsc_init, notebuf_ioctl NOTE_CLEAR) compile unchanged
    because C permits discarding expression values.
    
    Consumers that want to fold the discarded count into their own
    "dropped" metric can now write:
    
        dropped = ring_reset(buf->ring);
        atomic_add(&stats.drop_bytes, dropped);
    
    This is a generic ring extension; the first consumer (notebuf
    folding the count into drop_bytes on NOTE_CLEAR) is added in a
    separate patch.
    
    Signed-off-by: wangjianyu3 <[email protected]>
---
 include/nuttx/ring.h | 57 +++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 9 deletions(-)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 15324c3d140..12c860614e2 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -239,15 +239,18 @@
 
 /* Public API macros - accept RING_DECLARE/RING_DEFINE structs */
 
-#define ring_reset(r)                                    \
-  do                                                     \
-    {                                                    \
-      RING_FIELD(&RING_BASE(r)->prod_head) = 0;          \
-      RING_FIELD(&RING_BASE(r)->prod_tail) = 0;          \
-      RING_FIELD(&RING_BASE(r)->cons_head) = 0;          \
-      RING_FIELD(&RING_BASE(r)->cons_tail) = 0;          \
-    }                                                    \
-  while (0)
+/* ring_reset - clear all indices and return the count of elements that
+ * were present before the reset (i.e. the number of elements discarded
+ * by the reset operation).
+ *
+ * Callers may either use the return value (e.g. to bump a "dropped"
+ * counter) or discard it.  Safe to call on a freshly-zeroed ring whose
+ * indices have not been written yet: reading the volatile fields yields
+ * whatever happens to be in memory and the caller is expected to discard
+ * the value in that case.
+ */
+
+#define ring_reset(r)       ring_reset_indices(RING_BASE(r))
 
 /* ring_init - runtime initialization with warm-restart recovery.
  *
@@ -526,6 +529,42 @@ static inline_function uint32_t ring_idx(FAR struct ring_s 
*r, uint32_t idx)
   return r->mask ? (idx & r->mask) : (idx % r->capacity);
 }
 
+/****************************************************************************
+ * Name: ring_reset_indices
+ *
+ * Description:
+ *   Clear all indices and return the count of elements that were present
+ *   before the reset (i.e. the number of elements discarded by the reset
+ *   operation).  Called by the ring_reset() macro after RING_BASE()
+ *   conversion, and may also be invoked directly when the caller already
+ *   holds a FAR struct ring_s * (e.g. from inside other inline helpers
+ *   such as ring_init_magic).
+ *
+ *   Callers may either use the return value (e.g. to bump a "dropped"
+ *   counter) or discard it.  Safe to call on a freshly-zeroed ring whose
+ *   indices have not been written yet: reading the volatile fields yields
+ *   whatever happens to be in memory and the caller is expected to discard
+ *   the value in that case.
+ *
+ ****************************************************************************/
+
+static inline_function uint32_t ring_reset_indices(FAR struct ring_s *r)
+{
+  uint32_t dropped = ring_count(r);
+
+  if (dropped > r->capacity)
+    {
+      dropped = r->capacity;
+    }
+
+  RING_FIELD(&r->prod_head) = 0;
+  RING_FIELD(&r->prod_tail) = 0;
+  RING_FIELD(&r->cons_head) = 0;
+  RING_FIELD(&r->cons_tail) = 0;
+
+  return dropped;
+}
+
 /****************************************************************************
  * Name: ring_init_magic
  *

Reply via email to