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 b3454b3f98c41a97b407472d6e6069c301ba4b2b
Author: chenzihan1 <[email protected]>
AuthorDate: Tue Jun 2 17:55:23 2026 +0800

    include/nuttx/ring: add ring_mpsc_skipto/ring_mpmc_skipto API
    
    Add ring_sc_skipto()/ring_mc_skipto() that advances cons_tail to a target 
index
    directly, computing the skip count internally. This avoids exposing
    RTS_IDX encoding details to callers.
    
    Signed-off-by: chenzihan1 <[email protected]>
---
 include/nuttx/ring.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/include/nuttx/ring.h b/include/nuttx/ring.h
index 12c860614e2..27dfdb93c72 100644
--- a/include/nuttx/ring.h
+++ b/include/nuttx/ring.h
@@ -70,6 +70,13 @@
  *     ring_spmc_dequeue_skip(&g_ring, n);
  *     ring_mpmc_dequeue_skip(&g_ring, n);
  *
+ *    Skip-to (advance cons_tail to a specific target index):
+ *
+ *     ring_spsc_skipto(&g_ring, target_index);
+ *     ring_mpsc_skipto(&g_ring, target_index);
+ *     ring_spmc_skipto(&g_ring, target_index);
+ *     ring_mpmc_skipto(&g_ring, target_index);
+ *
  * 4. Overwrite Mode (SPMC/MPMC only, discards oldest on full):
  *
  *     ring_spmc_enqueue_overwrite(&g_ring, &val, 1);
@@ -371,6 +378,18 @@
 #define ring_mpsc_dequeue_skip(r, count) \
   ring_sc_dequeue_skip(RING_BASE(r), count)
 
+#define ring_mpsc_skipto(r, target) \
+  ring_sc_skipto(RING_BASE(r), target)
+
+#define ring_spsc_skipto(r, target) \
+   ring_sc_skipto(RING_BASE(r), target)
+
+#define ring_spmc_skipto(r, target) \
+  ring_mc_skipto(RING_BASE(r), target)
+
+#define ring_mpmc_skipto(r, target) \
+  ring_mc_skipto(RING_BASE(r), target)
+
 /* Overwrite enqueue: when the ring is full, discard oldest data to
  * make room.  The discard path acts as a virtual multi-consumer,
  * so the dequeue side MUST use MC protocol (ring_spmc_dequeue or
@@ -1312,6 +1331,30 @@ ring_sc_dequeue_skip(FAR struct ring_s *r, uint32_t 
count)
   return buf.cnt;
 }
 
+/****************************************************************************
+ * Name: ring_sc_skipto
+ *
+ * Description:
+ *   Single-consumer skip-to. Advance cons_tail to the specified target
+ *   index. Computes the skip count from current cons_tail internally.
+ *   If target is behind cons_tail (already consumed), does nothing.
+ *
+ ****************************************************************************/
+
+static inline_function uint32_t
+ring_sc_skipto(FAR struct ring_s *r, uint32_t target)
+{
+  uint32_t cons_index = RING_FIELD(&r->cons_tail);
+  uint32_t count = RTS_IDX(target - cons_index);
+
+  if (count != 0)
+    {
+      return ring_sc_dequeue_skip(r, count);
+    }
+
+  return 0;
+}
+
 /****************************************************************************
  * Name: ring_mc_dequeue_skip
  *
@@ -1332,6 +1375,31 @@ ring_mc_dequeue_skip(FAR struct ring_s *r, uint32_t 
count)
   return buf.cnt;
 }
 
+/****************************************************************************
+ * Name: ring_mc_skipto
+ *
+ * Description:
+ *   Multi-consumer skip-to. Advance cons_tail to the specified target
+ *   index. This is a convenience wrapper that computes the skip count
+ *   from the current cons_tail and then calls ring_mc_dequeue_skip.
+ *   If target is behind cons_tail (already consumed), does nothing.
+ *
+ ****************************************************************************/
+
+static inline_function uint32_t
+ring_mc_skipto(FAR struct ring_s *r, uint32_t target)
+{
+  uint32_t cons_index = RTS_IDX(atomic_read(&r->cons_tail));
+  uint32_t count = RTS_IDX(target - cons_index);
+
+  if (count != 0)
+    {
+      return ring_mc_dequeue_skip(r, count);
+    }
+
+  return 0;
+}
+
 /****************************************************************************
  * Name: ring_sp_enqueue_overwrite_start
  *

Reply via email to