Skip to main content

Module event_buf

Module event_buf 

Source
Expand description

Bounded SPSC event buffer with backpressure — no heap, no alloc.

EventBuf is a fixed-size, lock-free, single-producer single-consumer ring buffer that rejects pushes when full instead of overwriting. This gives the producer explicit backpressure so no events are silently lost.

§When to use

Use EventBuf when every event matters and the producer can afford to handle a “buffer full” signal (retry, log, or apply its own policy). If losing old events is acceptable, prefer crate::SeqRing. If you only need a single-owner ring, see crate::RingBuf.

§Memory ordering

This is a classic Lamport SPSC queue:

  • The producer owns head (Relaxed load, Release store) and reads tail with Acquire to see consumer progress.
  • The consumer owns tail (Relaxed load, Release store) and reads head with Acquire to see producer progress.
  • A slot is written before head is advanced and read before tail is advanced, so the Release/Acquire pairs on the cursors act as the publication fence.

§Example

use ph_eventing::EventBuf;

let buf = EventBuf::<u32, 4>::new();
let producer = buf.producer();
let consumer = buf.consumer();

assert!(producer.push(1).is_ok());
assert!(producer.push(2).is_ok());
assert_eq!(consumer.pop(), Some(1));
assert_eq!(consumer.pop(), Some(2));
assert_eq!(consumer.pop(), None); // empty

Structs§

Consumer
Read handle for an EventBuf.
EventBuf
Bounded SPSC event buffer with backpressure.
Producer
Write handle for an EventBuf.