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
event_queue.hpp
Go to the documentation of this file.
1/**
2 * @file event_queue.hpp
3 * @brief Events from another context into the control tick
4 */
5
6#ifndef SBL_CONTROL_EVENT_QUEUE_HPP_
7#define SBL_CONTROL_EVENT_QUEUE_HPP_
8
9#include <atomic>
10#include <cstdint>
11
13
14namespace sbl::control {
15
16/**
17 * @brief Lock-free SPSC event queue from one producing context into the control tick
18 *
19 * One producing context pushes (the main loop, which parses USB MIDI); the
20 * control tick drains in Stage::Drain, so control state is only written from
21 * the control context.
22 *
23 * A full queue drops the newest event: the push is rejected and counted.
24 * Only the consumer may move the tail, so dropping the oldest would need a
25 * lock. Size the queue so that overflow means a stalled tick. drain() pops at most capacity() events per call, so a producer that
26 * keeps pushing cannot stretch the tick.
27 *
28 * @tparam T Event type (copied in and out)
29 * @tparam Size Slot count, a power of 2 greater than 1; capacity() is Size - 1
30 *
31 * @context push() from the producer context only; drain() and pop() from
32 * the control tick only; dropped() from any context.
33 *
34 * @code
35 * sbl::control::EventQueue<sbl::midi::MidiEvent, 64> midi_q;
36 *
37 * // main loop (producer)
38 * midi_q.push(event);
39 *
40 * // Stage::Drain subscriber (consumer)
41 * midi_q.drain([](const sbl::midi::MidiEvent& e) { handle(e); });
42 * @endcode
43 */
44template<typename T, uint32_t Size>
46public:
47 /**
48 * @brief Push one event (producer side)
49 * @param event Event to copy in
50 * @return false, and a counted drop, if the queue is full
51 */
52 bool push(const T& event) {
53 if (ring_.push(event)) {
54 return true;
55 }
56 dropped_.store(dropped_.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
57 return false;
58 }
59
60 /**
61 * @brief Call fn(event) for up to capacity() queued events, oldest first (consumer side)
62 * @param fn Callable taking (const T&)
63 * @return Events handled
64 */
65 template<typename Fn>
66 uint32_t drain(Fn&& fn) {
67 T event{};
68 uint32_t handled = 0;
69 while (handled < capacity() && ring_.pop(event)) {
70 fn(static_cast<const T&>(event));
71 ++handled;
72 }
73 return handled;
74 }
75
76 /**
77 * @brief Pop one event (consumer side)
78 * @param[out] event Receives the oldest event
79 * @return false if the queue is empty
80 */
81 bool pop(T& event) { return ring_.pop(event); }
82
83 /** @brief True if no event is queued; a snapshot from either side */
84 bool empty() const { return ring_.empty(); }
85
86 /** @brief Pushes rejected because the queue was full, since boot */
87 uint32_t dropped() const { return dropped_.load(std::memory_order_relaxed); }
88
89 /** @brief Events the queue holds: Size - 1 */
90 static constexpr uint32_t capacity() { return Size - 1; }
91
92private:
94 std::atomic<uint32_t> dropped_{0}; // producer writes, anyone reads
95};
96
97} // namespace sbl::control
98
99#endif // SBL_CONTROL_EVENT_QUEUE_HPP_
Lock-free SPSC event queue from one producing context into the control tick.
static constexpr uint32_t capacity()
Events the queue holds: Size - 1.
bool empty() const
True if no event is queued; a snapshot from either side.
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)
uint32_t dropped() const
Pushes rejected because the queue was full, since boot.
bool push(const T &event)
Push one event (producer side)
Lock-free single-producer, single-consumer ring buffer.
The control stack: scheduler, event queue, merge, CC mapping, calibration.
Lock-free single-producer, single-consumer ring buffer.