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::Snapshot< T > Class Template Reference

Double-buffered struct with a sequence number, for one writer and one reader. More...

Detailed Description

template<typename T>
class sbl::control::Snapshot< T >

Double-buffered struct with a sequence number, for one writer and one reader.

The bridge between the control tick and the audio callback for a set of values that belong together. The writer publishes a struct; the reader takes all of it or none of it, so a block never works from values of two ticks.

Two copies of T and a sequence number. publish() writes the copy that is not current and then makes it current with one 32-bit store. read() copies the current one, and does nothing when the sequence has not moved since its last read, so a patch at rest costs a load and a compare a block.

Context
One writer and one reader. The writer never writes the copy the sequence names, so a writer interrupted by the reader, part way through a publish, leaves the reader the last whole copy. A publish that completes during a read frees the writer to start its next one into the copy being read, so read() checks the sequence after its copy and copies again if it has moved. On a target that never happens: the audio callback preempts the control tick and the tick cannot preempt it (system/interrupts.hpp). The check is for a host, where the two contexts are threads. The reader must not be starved: the writer must leave gaps longer than a copy of T takes, as a tick of a millisecond does.

Ordering between a copy and the sequence comes from the fences system/ring_buffer.hpp uses: compiler-only on a single core, a barrier on a host. Each is needed there: on a weakly ordered machine a core may satisfy read()'s copy after its second look at the sequence, and a writer's stores into a copy may be seen before its earlier store of the sequence, and either lets a reader accept a copy that was being written (seen on AArch64 in the threaded unit test).

T is trivially copyable. publish() compares by bytes. The copies are byte copies of what the writer passed, so a struct the writer keeps and publishes again compares exactly; padding that differs, as it can on the first publish, only costs a publish that changed nothing.

struct Published { float pitch = 0.0f; float width[2] = {0.5f, 0.5f}; };
bridge.publish(staged); // the tick's Publish stage
if (bridge.read(live)) { ... } // the start of an audio block
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
Template Parameters
TTrivially copyable struct

Definition at line 67 of file snapshot.hpp.

#include <snapshot.hpp>

Public Member Functions

bool publish (const T &value)
 
bool read (T &into)
 
uint32_t sequence () const
 

Member Function Documentation

◆ publish()

template<typename T >
bool sbl::control::Snapshot< T >::publish ( const T &  value)
inline

Writer. Make value the current copy. Returns false, and writes nothing, when it equals the current copy byte for byte.

Definition at line 75 of file snapshot.hpp.

References sbl::control::Snapshot< T >::sequence().

◆ read()

template<typename T >
bool sbl::control::Snapshot< T >::read ( T &  into)
inline

Reader. Copy the current value into into if it has been published since the last read, or if this is the first read. Returns whether into was written.

Definition at line 93 of file snapshot.hpp.

References sbl::control::Snapshot< T >::sequence().

◆ sequence()

template<typename T >
uint32_t sbl::control::Snapshot< T >::sequence ( ) const
inline

How many times a value has been published. Wraps at 2^32.

Definition at line 114 of file snapshot.hpp.

Referenced by sbl::control::Snapshot< T >::publish(), and sbl::control::Snapshot< T >::read().


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