VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::Semaphore Class Referencefinal

In-process counting semaphore with 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 an initial counter value.
 ~Semaphore () noexcept
 Destructor. Wakes all blocked acquirers before destruction.
bool acquire (size_t n=1, int timeout_ms=kInfinite) noexcept
 Decrements the semaphore counter by n, blocking until permits are available.
void release (size_t n=1) noexcept
 Increments the semaphore counter by n, waking blocked acquirers.
void reset (bool interrupt_waiters=false) noexcept
 Resets the semaphore counter to zero.
size_t get_count () const noexcept
 Returns the current value of the semaphore counter.

Static Public Attributes

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

Detailed Description

In-process counting semaphore with optional acquire timeout.

The internal counter is initialised via the constructor and can be atomically decremented (acquire) or incremented (release).

Constructor & Destructor Documentation

◆ Semaphore()

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

Constructs a Semaphore with an initial counter value.

Parameters
countInitial number of available permits (default: 0).
Here is the caller graph for this function:

◆ ~Semaphore()

vlink::Semaphore::~Semaphore ( )
noexcept

Destructor. Wakes all blocked acquirers before destruction.

Here is the call graph for this function:

Member Function Documentation

◆ acquire()

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

Decrements the semaphore counter by n, blocking until permits are available.

If the counter is less than n, the caller is blocked until enough release() calls bring the counter up. A timeout causes the function to return false without decrementing the counter.

Parameters
nNumber of permits to acquire (default: 1).
timeout_msMaximum time to wait in milliseconds. Use kInfinite (-1) to wait indefinitely (default).
Returns
true if the permits were acquired, false on timeout or if the semaphore was reset with interrupt_waiters == true.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_count()

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

Returns the current value of the semaphore counter.

The returned value is a snapshot; it may change before the caller can use it. Intended for diagnostic and logging purposes only.

Returns
Current counter value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ release()

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

Increments the semaphore counter by n, waking blocked acquirers.

If threads are blocked in acquire(), they are notified. The notification is delivered to at most n waiting threads.

Parameters
nNumber of permits to release (default: 1).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

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

Resets the semaphore counter to zero.

If interrupt_waiters is true, all threads blocked in acquire() are woken and their calls return false. Use this during shutdown to unblock consumers cleanly.

Parameters
interrupt_waitersIf true, wake all blocked acquirers (default: false).
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ kInfinite

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

Sentinel value for acquire() meaning "wait indefinitely".


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