Skip to content

hal_internal: add function to reset ring buffers#5456

Open
i509VCB wants to merge 1 commit intoembassy-rs:mainfrom
i509VCB:ringbuf-clear
Open

hal_internal: add function to reset ring buffers#5456
i509VCB wants to merge 1 commit intoembassy-rs:mainfrom
i509VCB:ringbuf-clear

Conversation

@i509VCB
Copy link
Copy Markdown
Member

@i509VCB i509VCB commented Feb 13, 2026

This allows a ring buffer to be reset to initial state without de-initializing buffers.

I use this to allow a buffered uart to read back LIN data over the wire to detect bus contention without needing a 2nd user buffer for readback (use the buffered bits).

@sourcebox
Copy link
Copy Markdown
Contributor

Wouldn't it be better to call this function clear? This is how it is called e.g. in embassy-sync.

@i509VCB
Copy link
Copy Markdown
Member Author

i509VCB commented Feb 14, 2026

Wouldn't it be better to call this function clear? This is how it is called e.g. in embassy-sync.

Yeah clear matches better with the other data structures in rust as well.

@i509VCB i509VCB changed the title hal_internal: add function to empty ring buffers hal_internal: add function to reset ring buffers Feb 25, 2026
@i509VCB
Copy link
Copy Markdown
Member Author

i509VCB commented Feb 25, 2026

Changed to reset since I ended up needing to restart the entire ring buffer.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a public API to clear an embassy-hal-internal atomic ring buffer without deinitializing its backing storage, intended to support buffered UART workflows (e.g., LIN readback/bus contention checks) while reusing the existing buffer.

Changes:

  • Add RingBuffer::reset(&self) to reset/clear the ring buffer state without touching the backing buffer.
  • Add a unit test validating push → reset → push/pop behavior.

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

Comment on lines +176 to +180
// Ordering: write `start` and `end`, with Release ordering.
//
// This ensures that the reset will wait until previous values are written.
self.end.store(0, Ordering::Release);
self.start.store(0, Ordering::Release);
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

reset() updates end and start as two independent atomics. With a concurrent reader/writer (which this type explicitly supports), this can create transient inconsistent states (e.g. writer observes start=0 and an old end, and may treat the buffer as full; reader may pop stale data after a reset). Consider implementing reset as a single-index operation (e.g. discard all unread data by setting start = end using an Acquire load of end and Release store to start), or make reset unsafe + document that it must not be called concurrently with any reader/writer activity.

Suggested change
// Ordering: write `start` and `end`, with Release ordering.
//
// This ensures that the reset will wait until previous values are written.
self.end.store(0, Ordering::Release);
self.start.store(0, Ordering::Release);
// Discard all unread data by moving `start` up to the current `end`.
//
// Ordering: load `end` with Acquire and store to `start` with Release.
// This ensures prior writes are visible before the reset is observed,
// and avoids transient inconsistent `(start, end)` states.
let end = self.end.load(Ordering::Acquire);
self.start.store(end, Ordering::Release);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@i509VCB I think maybe a CS would be the safest option here.

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.

I agree, all other AI suggestions are kinda bad. But a CS is warranted so nothing can come inbetween

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

CS makes sense, although I'd probably make the function unsafe and tell you to use a CS externally.

@i509VCB
Copy link
Copy Markdown
Member Author

i509VCB commented Feb 26, 2026

I'm not sure I trust AI anymore than myself to judge the correctness of the atomics.

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.

5 participants