CIRCT 23.0.0git
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
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 >:
Inheritance graph
[legend]
Collaboration diagram for esi::utils::ReadyIdSet< ID >:
Collaboration graph
[legend]

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

◆ 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 esi::utils::ReadyIdSet< ID >::cv, esi::utils::ReadyIdSet< ID >::mutex, and esi::utils::ReadyIdSet< ID >::ready.

Referenced by esi::cosim::RpcServer::Impl::handleSubscribe().

◆ 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 esi::utils::ReadyIdSet< ID >::cv, esi::utils::ReadyIdSet< ID >::mutex, and esi::utils::ReadyIdSet< ID >::shutdown.

Referenced by esi::cosim::RpcServer::Impl::stop().

◆ 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.

Referenced by esi::cosim::RpcServer::Impl::transportLoop().

Member Data Documentation

◆ cv

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

◆ mutex

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

◆ 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 esi::utils::ReadyIdSet< ID >::markReady().

◆ shutdown

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

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