CIRCT 24.0.0git
Loading...
Searching...
No Matches
esi::utils::ReadyIdSet< ID > Class Template Reference

Multi-producer / single-consumer dirty-set of channel ids, with CV-style blocking drain semantics. More...

#include <Utils.h>

Inheritance diagram for esi::utils::ReadyIdSet< ID >:
Collaboration diagram for esi::utils::ReadyIdSet< ID >:

Public Member Functions

void markReady (ID id)
 Add id to the dirty set and wake the consumer (if any).
bool waitDrain (std::unordered_set< ID > &out, std::optional< std::chrono::milliseconds > backoff={})
 Block until either requestShutdown() is called or the set is non-empty, then atomically swap the current set into out.
void requestShutdown ()
 Signal a clean shutdown: wakes every current and future waitDrain caller, which will then observe false.
bool isShutdown () const
 True once requestShutdown() has been called.

Private Attributes

std::mutex mutex
std::condition_variable cv
std::unordered_set< ID > ready
bool shutdown = false

Detailed Description

template<typename ID = uint16_t>
class esi::utils::ReadyIdSet< ID >

Multi-producer / single-consumer dirty-set of channel ids, with CV-style blocking drain semantics.

Producers call markReady(id); the consumer thread calls waitDrain() to atomically swap out the current set.

Use this when many independent per-channel producers feed a single transport thread that needs to know which channels have work to do without polling each in turn. The CV pattern (lock-around-shutdown-set, notify-after-release, predicate captures both shutdown and !empty) is fiddly enough that centralising it here avoids subtle copy/paste bugs in each backend.

Definition at line 177 of file Utils.h.

Member Function Documentation

◆ isShutdown()

template<typename ID = uint16_t>
bool esi::utils::ReadyIdSet< ID >::isShutdown ( ) const
inline

True once requestShutdown() has been called.

Definition at line 219 of file Utils.h.

References mutex, and shutdown.

◆ markReady()

template<typename ID = uint16_t>
void esi::utils::ReadyIdSet< ID >::markReady ( ID id)
inline

Add id to the dirty set and wake the consumer (if any).

Idempotent w.r.t. an id that is already in the set.

Definition at line 181 of file Utils.h.

References cv, mutex, and ready.

◆ requestShutdown()

template<typename ID = uint16_t>
void esi::utils::ReadyIdSet< ID >::requestShutdown ( )
inline

Signal a clean shutdown: wakes every current and future waitDrain caller, which will then observe false.

Idempotent.

Definition at line 210 of file Utils.h.

References cv, mutex, and shutdown.

◆ waitDrain()

template<typename ID = uint16_t>
bool esi::utils::ReadyIdSet< ID >::waitDrain ( std::unordered_set< ID > & out,
std::optional< std::chrono::milliseconds > backoff = {} )
inline

Block until either requestShutdown() is called or the set is non-empty, then atomically swap the current set into out.

With backoff non-nullopt, returns after the timeout even if neither condition holds (useful when the caller maintains its own retry set it wants to re-process periodically). Returns false once shutdown has been signalled, so consumer loops can write while (set.waitDrain(ids, backoff)) { ... }.

Definition at line 196 of file Utils.h.

Member Data Documentation

◆ cv

template<typename ID = uint16_t>
std::condition_variable esi::utils::ReadyIdSet< ID >::cv
private

Definition at line 226 of file Utils.h.

Referenced by markReady(), and requestShutdown().

◆ mutex

template<typename ID = uint16_t>
std::mutex esi::utils::ReadyIdSet< ID >::mutex
mutableprivate

Definition at line 225 of file Utils.h.

Referenced by isShutdown(), markReady(), and requestShutdown().

◆ ready

template<typename ID = uint16_t>
std::unordered_set<ID> esi::utils::ReadyIdSet< ID >::ready
private

Definition at line 227 of file Utils.h.

Referenced by markReady().

◆ shutdown

template<typename ID = uint16_t>
bool esi::utils::ReadyIdSet< ID >::shutdown = false
private

Definition at line 228 of file Utils.h.

Referenced by isShutdown(), and requestShutdown().


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