VLink  2.0.0
A high-performance communication middleware
vlink::Semaphore Class Referencefinal

In-process counting semaphore with an optional acquire timeout. More...

#include <semaphore.h>

Collaboration diagram for vlink::Semaphore:

Public Member Functions

 Semaphore (size_t count=0) noexcept
 Constructs a semaphore with the given initial permit count. More...
 
 ~Semaphore () noexcept
 Destructor. Wakes every blocked acquirer before tearing down internal state. More...
 
bool acquire (size_t n=1, int timeout_ms=kInfinite) noexcept
 Decrements the counter by n, blocking when not enough permits are available. More...
 
void release (size_t n=1) noexcept
 Increments the counter by n and wakes blocked acquirers. More...
 
void reset (bool interrupt_waiters=false) noexcept
 Restores the counter to the initial value supplied to the constructor. More...
 
size_t get_count () const noexcept
 Returns a non-atomic snapshot of the current counter value. More...
 

Static Public Attributes

static constexpr int kInfinite {-1}
 Sentinel value for acquire() meaning wait indefinitely. More...
 

Detailed Description

In-process counting semaphore with an optional acquire timeout.

The internal counter starts at the value supplied to the constructor; acquire decrements it (blocking when not enough permits are available) and release increments it while waking blocked acquirers.

Constructor & Destructor Documentation

◆ Semaphore()

vlink::Semaphore::Semaphore ( size_t  count = 0)
explicitnoexcept

Constructs a semaphore with the given initial permit count.

Parameters
countInitial permit count. Default: 0.

◆ ~Semaphore()

vlink::Semaphore::~Semaphore ( )
noexcept

Destructor. Wakes every blocked acquirer before tearing down internal state.

Member Function Documentation

◆ acquire()

bool vlink::Semaphore::acquire ( size_t  n = 1,
int  timeout_ms = kInfinite 
)
noexcept

Decrements the counter by n, blocking when not enough permits are available.

The call blocks while the counter is less than n. A timeout returns false without modifying the counter. When reset(true) is invoked while a thread is blocked, the wait returns false and the counter is restored to the initial value.

Parameters
nNumber of permits requested. Default: 1.
timeout_msMaximum wait in milliseconds; kInfinite waits forever.
Returns
true when the permits were acquired; false on timeout or reset.

◆ get_count()

size_t vlink::Semaphore::get_count ( ) const
noexcept

Returns a non-atomic snapshot of the current counter value.

The value may change immediately after the call returns; treat the result as a diagnostic-only hint.

Returns
Current permit count snapshot.

◆ release()

void vlink::Semaphore::release ( size_t  n = 1)
noexcept

Increments the counter by n and wakes blocked acquirers.

Broadcasts the condition variable so every blocked acquirer can re-evaluate the permit count. Safe to call from any thread.

Parameters
nNumber of permits to add. Default: 1.

◆ reset()

void vlink::Semaphore::reset ( bool  interrupt_waiters = false)
noexcept

Restores the counter to the initial value supplied to the constructor.

When interrupt_waiters is true every blocked acquirer is woken and its call returns false, which is the recommended way to release waiters during shutdown.

Parameters
interrupt_waitersWhen true, wakes blocked acquirers. Default: false.

Member Data Documentation

◆ kInfinite

constexpr int vlink::Semaphore::kInfinite {-1}
staticconstexpr

Sentinel value for acquire() meaning wait indefinitely.


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