Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::control::EventQueue< T, Size > Class Template Reference

Lock-free SPSC event queue from one producing context into the control tick. More...

Detailed Description

template<typename T, uint32_t Size>
class sbl::control::EventQueue< T, Size >

Lock-free SPSC event queue from one producing context into the control tick.

One producing context pushes (the main loop, which parses USB MIDI); the control tick drains in Stage::Drain, so control state is only written from the control context.

A full queue drops the newest event: the push is rejected and counted. Only the consumer may move the tail, so dropping the oldest would need a lock. Size the queue so that overflow means a stalled tick. drain() pops at most capacity() events per call, so a producer that keeps pushing cannot stretch the tick.

Template Parameters
TEvent type (copied in and out)
SizeSlot count, a power of 2 greater than 1; capacity() is Size - 1
Context
push() from the producer context only; drain() and pop() from the control tick only; dropped() from any context.
// main loop (producer)
midi_q.push(event);
// Stage::Drain subscriber (consumer)
midi_q.drain([](const sbl::midi::MidiEvent& e) { handle(e); });
Lock-free SPSC event queue from one producing context into the control tick.
uint32_t drain(Fn &&fn)
Call fn(event) for up to capacity() queued events, oldest first (consumer side)
bool push(const T &event)
Push one event (producer side)

Definition at line 45 of file event_queue.hpp.

#include <event_queue.hpp>

Public Member Functions

bool push (const T &event)
 Push one event (producer side)
 
template<typename Fn >
uint32_t drain (Fn &&fn)
 Call fn(event) for up to capacity() queued events, oldest first (consumer side)
 
bool pop (T &event)
 Pop one event (consumer side)
 
bool empty () const
 True if no event is queued; a snapshot from either side.
 
uint32_t dropped () const
 Pushes rejected because the queue was full, since boot.
 

Static Public Member Functions

static constexpr uint32_t capacity ()
 Events the queue holds: Size - 1.
 

Member Function Documentation

◆ push()

template<typename T , uint32_t Size>
bool sbl::control::EventQueue< T, Size >::push ( const T &  event)
inline

Push one event (producer side)

Parameters
eventEvent to copy in
Returns
false, and a counted drop, if the queue is full

Definition at line 52 of file event_queue.hpp.

◆ drain()

template<typename T , uint32_t Size>
template<typename Fn >
uint32_t sbl::control::EventQueue< T, Size >::drain ( Fn &&  fn)
inline

Call fn(event) for up to capacity() queued events, oldest first (consumer side)

Parameters
fnCallable taking (const T&)
Returns
Events handled

Definition at line 66 of file event_queue.hpp.

References sbl::control::EventQueue< T, Size >::capacity().

◆ pop()

template<typename T , uint32_t Size>
bool sbl::control::EventQueue< T, Size >::pop ( T &  event)
inline

Pop one event (consumer side)

Parameters
[out]eventReceives the oldest event
Returns
false if the queue is empty

Definition at line 81 of file event_queue.hpp.

◆ empty()

template<typename T , uint32_t Size>
bool sbl::control::EventQueue< T, Size >::empty ( ) const
inline

True if no event is queued; a snapshot from either side.

Definition at line 84 of file event_queue.hpp.

◆ dropped()

template<typename T , uint32_t Size>
uint32_t sbl::control::EventQueue< T, Size >::dropped ( ) const
inline

Pushes rejected because the queue was full, since boot.

Definition at line 87 of file event_queue.hpp.

◆ capacity()

template<typename T , uint32_t Size>
static constexpr uint32_t sbl::control::EventQueue< T, Size >::capacity ( )
inlinestaticconstexpr

Events the queue holds: Size - 1.

Definition at line 90 of file event_queue.hpp.

Referenced by sbl::control::EventQueue< T, Size >::drain().


The documentation for this class was generated from the following file: