MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Memory::Seqlock Class Reference

Single-writer multiple-reader sequence lock for fixed-size data regions. More...

#include <SeqLock.hpp>

+ Collaboration diagram for MayaFlux::Memory::Seqlock:

Public Member Functions

void begin_write () noexcept
 Begin a write: advances the counter to an odd value.
 
void end_write () noexcept
 End a write: advances the counter back to an even value.
 
Seqlockoperator= (const Seqlock &)=delete
 
Seqlockoperator= (Seqlock &&)=delete
 
uint32_t read_begin () const noexcept
 Begin a read: spin until the counter is even, return the snapshot.
 
bool read_retry (uint32_t snapshot) const noexcept
 Check whether a write overlapped the read just completed.
 
 Seqlock () noexcept=default
 
 Seqlock (const Seqlock &)=delete
 
 Seqlock (Seqlock &&)=delete
 
uint32_t sequence () const noexcept
 Return the raw sequence counter value.
 
 ~Seqlock ()=default
 

Private Attributes

std::atomic< uint32_t > m_seq { 0U }
 

Detailed Description

Single-writer multiple-reader sequence lock for fixed-size data regions.

Protects a data region that is written infrequently and read frequently. Readers never block writers; a reader that observes a write in progress simply retries. Writers are serialized externally (e.g. by the container's existing state machine or a higher-level mutex on the write path only).

Sequence counter convention: even — data is stable, safe to read odd — write in progress, readers must retry

Usage — writer side:

SeqlockWriteGuard guard(lock);
std::memcpy(dest, src, bytes);
RAII guard that brackets a Seqlock write region.
Definition SeqLock.hpp:136

Usage — reader side (unbounded):

uint32_t snap;
do {
snap = lock.read_begin();
// copy or sample the guarded data
} while (lock.read_retry(snap));

Usage — reader side (bounded, preferred in frame loops):

auto result = seqlock_read(lock, max_attempts, [&]{ return read_data(); });
if (!result) {
// stale read accepted or skip frame
}
auto seqlock_read(const Seqlock &lock, uint32_t max_attempts, Fn &&fn) -> std::optional< std::invoke_result_t< Fn > >
Invoke a read functor under a Seqlock with a bounded retry count.
Definition SeqLock.hpp:191

Alignment to a cache line prevents false sharing when multiple Seqlock instances are stored in a contiguous array (e.g. one per texture layer).

Definition at line 44 of file SeqLock.hpp.


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