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
snapshot.hpp
Go to the documentation of this file.
1/**
2 * @file snapshot.hpp
3 * @brief A whole struct handed from one context to another, never half-written
4 */
5
6#ifndef SBL_CONTROL_SNAPSHOT_HPP_
7#define SBL_CONTROL_SNAPSHOT_HPP_
8
9#include <atomic>
10#include <cstdint>
11#include <cstring>
12#include <type_traits>
13
15
16namespace sbl::control {
17
18/**
19 * @brief Double-buffered struct with a sequence number, for one writer and one reader
20 *
21 * The bridge between the control tick and the audio callback for a set of values that belong together. The writer publishes a struct;
22 * the reader takes all of it or none of it, so a block never works from
23 * values of two ticks.
24 *
25 * Two copies of T and a sequence number. publish() writes the copy that is
26 * not current and then makes it current with one 32-bit store. read()
27 * copies the current one, and does nothing when the sequence has not moved
28 * since its last read, so a patch at rest costs a load and a compare a
29 * block.
30 *
31 * @context One writer and one reader. The writer never writes the copy the
32 * sequence names, so a writer interrupted by the reader, part way through a
33 * publish, leaves the reader the last whole copy. A publish that completes
34 * during a read frees the writer to start its next one into the copy being
35 * read, so read() checks the sequence after its copy and copies again if it
36 * has moved. On a target that never happens: the audio callback preempts
37 * the control tick and the tick cannot preempt it (system/interrupts.hpp).
38 * The check is for a host, where the two contexts are threads. The reader
39 * must not be starved: the writer must leave gaps longer than a copy of T
40 * takes, as a tick of a millisecond does.
41 *
42 * Ordering between a copy and the sequence comes from the fences
43 * system/ring_buffer.hpp uses: compiler-only on a single core, a barrier on
44 * a host. Each is needed there: on a weakly ordered machine a core may
45 * satisfy read()'s copy after its second look at the sequence, and a
46 * writer's stores into a copy may be seen before its earlier store of the
47 * sequence, and either lets a reader accept a copy that was being written
48 * (seen on AArch64 in the threaded unit test).
49 *
50 * T is trivially copyable. publish() compares by bytes. The copies are byte
51 * copies of what the writer passed, so a struct the writer keeps and
52 * publishes again compares exactly; padding that differs, as it can on the
53 * first publish, only costs a publish that changed nothing.
54 *
55 * @code
56 * struct Published { float pitch = 0.0f; float width[2] = {0.5f, 0.5f}; };
57 * sbl::control::Snapshot<Published> bridge;
58 *
59 * bridge.publish(staged); // the tick's Publish stage
60 *
61 * if (bridge.read(live)) { ... } // the start of an audio block
62 * @endcode
63 *
64 * @tparam T Trivially copyable struct
65 */
66template<typename T>
67class Snapshot {
68 static_assert(std::is_trivially_copyable_v<T>, "Snapshot carries a trivially copyable struct");
69
70public:
71 /**
72 * Writer. Make `value` the current copy. Returns false, and writes
73 * nothing, when it equals the current copy byte for byte.
74 */
75 bool publish(const T& value) {
76 const uint32_t sequence = sequence_.load(std::memory_order_relaxed);
77 // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison): equality by bytes is the contract, see the file comment
78 if (std::memcmp(&copies_[sequence & 1u], &value, sizeof(T)) == 0) {
79 return false;
80 }
81 sbl::system::detail::ring_release_fence(); // the last publish's sequence is out before this copy is touched
82 std::memcpy(&copies_[(sequence + 1u) & 1u], &value, sizeof(T));
83 sbl::system::detail::ring_release_fence(); // the copy is written before the sequence names it
84 sequence_.store(sequence + 1u, std::memory_order_release);
85 return true;
86 }
87
88 /**
89 * Reader. Copy the current value into `into` if it has been published
90 * since the last read, or if this is the first read. Returns whether
91 * `into` was written.
92 */
93 bool read(T& into) {
94 uint32_t sequence = sequence_.load(std::memory_order_acquire);
95 if (read_once_ && sequence == read_sequence_) {
96 return false;
97 }
98 for (;;) {
99 sbl::system::detail::ring_acquire_fence(); // the sequence is read before the copy it names
100 std::memcpy(&into, &copies_[sequence & 1u], sizeof(T));
101 sbl::system::detail::ring_acquire_fence(); // and the copy is read before the sequence is looked at again
102 const uint32_t after = sequence_.load(std::memory_order_acquire);
103 if (after == sequence) {
104 break; // no publish completed meanwhile, so nothing wrote this copy
105 }
106 sequence = after;
107 }
108 read_sequence_ = sequence;
109 read_once_ = true;
110 return true;
111 }
112
113 /** How many times a value has been published. Wraps at 2^32. */
114 uint32_t sequence() const { return sequence_.load(std::memory_order_relaxed); }
115
116private:
117 T copies_[2]{};
118 std::atomic<uint32_t> sequence_{0};
119 uint32_t read_sequence_ = 0; // reader only
120 bool read_once_ = false; // reader only
121};
122
123} // namespace sbl::control
124
125#endif // SBL_CONTROL_SNAPSHOT_HPP_
Double-buffered struct with a sequence number, for one writer and one reader.
Definition snapshot.hpp:67
bool publish(const T &value)
Definition snapshot.hpp:75
bool read(T &into)
Definition snapshot.hpp:93
uint32_t sequence() const
Definition snapshot.hpp:114
The control stack: scheduler, event queue, merge, CC mapping, calibration.
Lock-free single-producer, single-consumer ring buffer.