ORIGINAL
Loading...
Searching...
No Matches
Public Member Functions | List of all members
original::semaphore< MAX_CNT > Class Template Reference

Counting semaphore with maximum count constraint. More...

#include <semaphores.h>

Collaboration diagram for original::semaphore< MAX_CNT >:
Collaboration graph

Public Member Functions

 semaphore ()
 Constructs a semaphore with maximum count.
 
 semaphore (u_integer init_count)
 Constructs a semaphore with specified initial count.
 
void acquire ()
 Acquires one resource (decrements count)
 
bool tryAcquire ()
 Attempts to acquire one resource without blocking.
 
bool acquireFor (time::duration timeout)
 Attempts to acquire one resource with timeout.
 
void release (u_integer increase=1)
 Releases resources (increments count)
 
bool tryRelease (u_integer increase=1)
 Attempts to release resources without blocking.
 
bool releaseFor (u_integer increase, time::duration timeout)
 Attempts to release resources with timeout.
 
bool releaseFor (const time::duration &timeout)
 Attempts to release one resource with timeout.
 

Detailed Description

template<u_integer MAX_CNT = 1>
class original::semaphore< MAX_CNT >

Counting semaphore with maximum count constraint.

Specialization for unbounded semaphore (no maximum count)

Template Parameters
MAX_CNTMaximum allowed semaphore count (default: 1, binary semaphore)

A counting semaphore that controls access to a shared resource pool. The semaphore count represents the number of available resources. acquire() waits for an available resource, while release() returns a resource to the pool.

For MAX_CNT = 1, behaves as a binary semaphore/mutex. For MAX_CNT > 1, implements a counting semaphore for resource pools. For MAX_CNT = 0, see specialization below for unbounded semaphore.

A semaphore with no upper limit on the count. release() operations never block, as there's no constraint on the maximum semaphore value.

Constructor & Destructor Documentation

◆ semaphore() [1/2]

template<original::u_integer MAX_CNT>
original::semaphore< MAX_CNT >::semaphore ( )

Constructs a semaphore with maximum count.

Initializes the semaphore with MAX_CNT available resources

◆ semaphore() [2/2]

template<original::u_integer MAX_CNT>
original::semaphore< MAX_CNT >::semaphore ( u_integer  init_count)
explicit

Constructs a semaphore with specified initial count.

Parameters
init_countInitial semaphore count
Exceptions
valueErrorif init_count exceeds MAX_CNT

Member Function Documentation

◆ acquire()

template<original::u_integer MAX_CNT>
void original::semaphore< MAX_CNT >::acquire ( )

Acquires one resource (decrements count)

Blocks the calling thread until a resource becomes available (count > 0). Decrements the semaphore count by 1 upon successful acquisition.

◆ acquireFor()

template<original::u_integer MAX_CNT>
bool original::semaphore< MAX_CNT >::acquireFor ( time::duration  timeout)

Attempts to acquire one resource with timeout.

Parameters
timeoutMaximum duration to wait for resource availability
Returns
true if resource was acquired within timeout, false otherwise

◆ release()

template<original::u_integer MAX_CNT>
void original::semaphore< MAX_CNT >::release ( u_integer  increase = 1)

Releases resources (increments count)

Parameters
increaseNumber of resources to release (default: 1)
Exceptions
valueErrorif increase would exceed MAX_CNT

Blocks if releasing would exceed MAX_CNT, waiting until space becomes available. Notifies waiting threads after successful release.

◆ releaseFor() [1/2]

template<original::u_integer MAX_CNT>
bool original::semaphore< MAX_CNT >::releaseFor ( const time::duration timeout)

Attempts to release one resource with timeout.

Parameters
timeoutMaximum duration to wait for release condition
Returns
true if resource was released within timeout, false otherwise

◆ releaseFor() [2/2]

template<original::u_integer MAX_CNT>
bool original::semaphore< MAX_CNT >::releaseFor ( u_integer  increase,
time::duration  timeout 
)

Attempts to release resources with timeout.

Parameters
increaseNumber of resources to release
timeoutMaximum duration to wait for release condition
Returns
true if resources were released within timeout, false otherwise

◆ tryAcquire()

template<original::u_integer MAX_CNT>
bool original::semaphore< MAX_CNT >::tryAcquire ( )

Attempts to acquire one resource without blocking.

Returns
true if resource was acquired, false if no resources available

◆ tryRelease()

template<original::u_integer MAX_CNT>
bool original::semaphore< MAX_CNT >::tryRelease ( u_integer  increase = 1)

Attempts to release resources without blocking.

Parameters
increaseNumber of resources to release (default: 1)
Returns
true if resources were released, false if release would exceed MAX_CNT

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