MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ seqlock_read()

template<std::invocable Fn>
auto MayaFlux::Memory::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.

Retries the read up to max_attempts times if a concurrent write is detected. On each attempt the functor is called with no arguments; its return value is forwarded on success.

Returns std::nullopt if max_attempts is exhausted without a clean read. Callers in frame loops should treat nullopt as "use previous frame" rather than blocking.

The functor must be callable with no arguments and must return a value (or void — see seqlock_read_void for void functors).

Template Parameters
FnCallable type, must satisfy std::invocable<Fn>.
Parameters
lockThe Seqlock guarding the data region.
max_attemptsMaximum number of read attempts before giving up. 0 means unbounded (spin until clean).
fnFunctor that reads and returns the guarded data.
Returns
std::optional<return type of fn>, empty on exhaustion.
auto pixels = seqlock_read(m_layer_locks[layer], 8, [&] {
return std::span<const uint8_t>(buf.data(), buf.size());
});
if (pixels) upload_to_gpu(*pixels);
const std::vector< float > * pixels
Definition Decoder.cpp:65
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

Definition at line 191 of file SeqLock.hpp.

195{
196 uint32_t attempts = 0;
197 while (max_attempts == 0 || attempts < max_attempts) {
198 uint32_t snap = lock.read_begin();
199 auto result = std::invoke(std::forward<Fn>(fn));
200 if (!lock.read_retry(snap))
201 return result;
202 ++attempts;
203 }
204 return std::nullopt;
205}
uint32_t read_begin() const noexcept
Begin a read: spin until the counter is even, return the snapshot.
Definition SeqLock.hpp:82
bool read_retry(uint32_t snapshot) const noexcept
Check whether a write overlapped the read just completed.
Definition SeqLock.hpp:97