Skip to content

Add items_waiting/is_empty introspection to ring buffers#68

Merged
kahrendt merged 2 commits into
mainfrom
ring-buffer-introspection
Jun 11, 2026
Merged

Add items_waiting/is_empty introspection to ring buffers#68
kahrendt merged 2 commits into
mainfrom
ring-buffer-introspection

Conversation

@kahrendt

@kahrendt kahrendt commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

What

Exposes two introspection methods on the ring buffer types:

  • SpscRingBuffer::items_waiting() / is_empty(): count of committed items written but not yet received by the consumer, on both the ESP (FreeRTOS vRingbufferGetInfo) and host implementations. The host implementation gains an items_waiting_ counter (incremented on commit, decremented on receive) and mtx_ is made mutable so the const accessors can lock.
  • AudioRingBuffer::chunks_waiting() / is_empty(): thin wrappers delegating to the underlying SPSC ring buffer.

Why

These accessors let the consumer query how much audio is queued so it can detect an empty/near-empty buffer and stuff zeros to prevent underflow to prevent audible glitches at the start of playback.

Notes for reviewers

  • No behavioral change to existing code — these are purely additive read-only accessors.
  • The host counter is maintained under the existing buffer mutex; the ESP path reads directly from the FreeRTOS ring buffer info, so no extra bookkeeping is needed there.

Expose committed-item counts and emptiness checks on the SPSC ring
buffer (ESP and host) and the audio ring buffer wrapper.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds lightweight introspection APIs to the ring buffer abstractions so consumers can query whether buffered audio (or other items) are pending and how many are queued, enabling underflow-avoidance logic.

Changes:

  • Add SpscRingBuffer::items_waiting() and SpscRingBuffer::is_empty() for both ESP (via vRingbufferGetInfo) and host (via a new items_waiting_ counter).
  • Track items_waiting_ in the host implementation (increment on commit, decrement on receive) and make mtx_ mutable to allow locking in const accessors.
  • Add SendspinAudioRingBuffer::chunks_waiting() and SendspinAudioRingBuffer::is_empty() as thin wrappers over the underlying SPSC ring buffer.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/platform/spsc_ring_buffer.h Adds new introspection methods; host implementation adds items_waiting_ counter and updates synchronization to support const accessors.
src/audio_ring_buffer.h Exposes audio-specific wrappers for the underlying ring buffer introspection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/platform/spsc_ring_buffer.h
Return 0 when the ring buffer has not been created, matching the host
implementation's safe default and avoiding a null-handle dereference in
vRingbufferGetInfo.
@kahrendt kahrendt enabled auto-merge (squash) June 11, 2026 12:56
@kahrendt kahrendt merged commit 7891256 into main Jun 11, 2026
5 checks passed
@kahrendt kahrendt deleted the ring-buffer-introspection branch June 11, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants