MayaFlux 1.0.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:48
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:237

Definition at line 237 of file SeqLock.hpp.

241{
242 uint32_t attempts = 0;
243 while (max_attempts == 0 || attempts < max_attempts) {
244 uint32_t snap = lock.read_begin();
245 auto result = std::invoke(std::forward<Fn>(fn));
246 if (!lock.read_retry(snap))
247 return result;
248 ++attempts;
249 }
250 return std::nullopt;
251}
uint32_t read_begin() const noexcept
Begin a read: spin until the counter is even, return the snapshot.
Definition SeqLock.hpp:101
bool read_retry(uint32_t snapshot) const noexcept
Check whether a write overlapped the read just completed.
Definition SeqLock.hpp:116