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

◆ sweep()

template<typename T >
template<typename Pred , typename Finish >
requires std::invocable<Pred, T&> && std::invocable<Finish, T&>
void MayaFlux::Memory::SweepList< T >::sweep ( Pred  ready,
Finish  finish 
)
inline

Non-blocking pass over every currently held item.

Atomically takes the entire chain, then for each item calls ready(item). If it returns true, calls finish(item) and frees the link. If false, the item is republished so a later sweep() or drain() will see it again.

Items pushed by other threads while this sweep is running are not part of the taken chain and are left untouched; they surface on a later sweep().

Template Parameters
PredCallable (T&) -> bool. True means the item is done and safe to finalize now.
FinishCallable (T&) -> void. Invoked exactly once per item for which ready returned true, before the link is freed.
Parameters
readyReadiness check.
finishFinalizer, invoked once per reclaimed item.

Definition at line 138 of file SweepList.hpp.

139 {
140 Link* chain = m_head.exchange(nullptr, std::memory_order_acquire);
141
142 Link* survivors = nullptr;
143 size_t reclaimed = 0;
144
145 while (chain) {
146 Link* next = chain->next;
147
148 if (std::invoke(ready, chain->value)) {
149 std::unique_ptr<Link> owned(chain);
150 std::invoke(finish, owned->value);
151 ++reclaimed;
152 } else {
153 chain->next = survivors;
154 survivors = chain;
155 }
156
157 chain = next;
158 }
159
160 if (survivors) {
161 reattach(survivors);
162 }
163
164 m_count.fetch_sub(reclaimed, std::memory_order_relaxed);
165 }
BufferProcessingChain * chain
void reattach(Link *survivors) noexcept
std::atomic< Link * > m_head
std::atomic< size_t > m_count

References chain, MayaFlux::Memory::SweepList< T >::m_count, MayaFlux::Memory::SweepList< T >::m_head, MayaFlux::Memory::SweepList< T >::Link::next, and MayaFlux::Memory::SweepList< T >::reattach().

+ Here is the call graph for this function: