Public Member Functions | Private Attributes | List of all members
cras::SemaphoreGuard< T > Class Template Reference

RAII guard for operations with a semaphore. On creation, the semaphore is acquired, and on destruction, it is released. More...

#include <semaphore.hpp>

Public Member Functions

bool acquired () const
 Whether the semaphore acquisition succeeded when constructing this guard. More...
 
 SemaphoreGuard (T &semaphore)
 
 ~SemaphoreGuard ()
 

Private Attributes

bool acquireSucceeded {false}
 Whether the acquire succeeded. More...
 
T & semaphore
 The guarded semaphore. More...
 

Detailed Description

template<typename T>
class cras::SemaphoreGuard< T >

RAII guard for operations with a semaphore. On creation, the semaphore is acquired, and on destruction, it is released.

Template Parameters
TType of the semaphore. Can be any type with methods bool acquire() and void release().
Note
The usage pattern is slightly different than the normal lock_guard - because acquire() can return false (e.g. if the semaphore is blocked for new acquisitions), the pattern should be the following: `SemaphoreGuard<ReverseSemaphore> guard(sem); if (!guard.acquired()) return;

Definition at line 109 of file semaphore.hpp.

Constructor & Destructor Documentation

◆ SemaphoreGuard()

template<typename T >
cras::SemaphoreGuard< T >::SemaphoreGuard ( T &  semaphore)
inlineexplicit

Definition at line 112 of file semaphore.hpp.

◆ ~SemaphoreGuard()

template<typename T >
cras::SemaphoreGuard< T >::~SemaphoreGuard ( )
inline

Definition at line 117 of file semaphore.hpp.

Member Function Documentation

◆ acquired()

template<typename T >
bool cras::SemaphoreGuard< T >::acquired ( ) const
inline

Whether the semaphore acquisition succeeded when constructing this guard.

Returns
Success value.
Note
Always check the result of this function after constructing a guard. The semaphore may be disabled.

Definition at line 128 of file semaphore.hpp.

Member Data Documentation

◆ acquireSucceeded

template<typename T >
bool cras::SemaphoreGuard< T >::acquireSucceeded {false}
private

Whether the acquire succeeded.

Definition at line 138 of file semaphore.hpp.

◆ semaphore

template<typename T >
T& cras::SemaphoreGuard< T >::semaphore
private

The guarded semaphore.

Definition at line 135 of file semaphore.hpp.


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


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53