iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
iox::posix::Semaphore Class Reference

Posix semaphore C++ Wrapping class. More...

#include <iceoryx_hoofs/posix_wrapper/semaphore.hpp>

Inheritance diagram for iox::posix::Semaphore:
Inheritance graph
[legend]
Collaboration diagram for iox::posix::Semaphore:
Collaboration graph
[legend]

Public Member Functions

 Semaphore () noexcept
 Default constructor which creates an uninitialized semaphore. This semaphore object is unusable you need to reassign it with an object created by the semaphore factory methods.
 
 Semaphore (Semaphore &&rhs) noexcept
 Move constructor.
 
Semaphoreoperator= (Semaphore &&rhs) noexcept
 Move assignment operator.
 
 Semaphore (const Semaphore &)=delete
 We are denying Semaphore copy since it manages the semaphore resource and the underlying concept did not include copying.
 
Semaphoreoperator= (const Semaphore &)=delete
 We are denying Semaphore copy since it manages the semaphore resource and the underlying concept did not include copying.
 
 ~Semaphore () noexcept
 Destructor.
 
cxx::expected< int, SemaphoreError > getValue () const noexcept
 calls sem_getvalue which gets the value of a semaphore From the sem_getvalue manpage: sem_getvalue() places the current value of the semaphore pointed to sem into the integer pointed to by sval.
 
cxx::expected< SemaphoreError > post () noexcept
 calls sem_post which unlocks a semaphore From the sem_post manpage: sem_post() increments (unlocks) the semaphore pointed to by sem. If the semaphore's value consequently becomes greater than zero, then another process or thread blocked in a sem_wait(3) call will be woken up and proceed to lock the semaphore.
 
cxx::expected< SemaphoreWaitState, SemaphoreError > timedWait (const units::Duration abs_timeout) noexcept
 see wait()
 
cxx::expected< bool, SemaphoreError > tryWait () noexcept
 see wait()
 
cxx::expected< SemaphoreError > wait () noexcept
 calls sem_wait which locks a semaphore From the sem_wait manpage: sem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal handler interrupts the call.
 
- Public Member Functions inherited from DesignPattern::Creation< Semaphore, SemaphoreError >
 Creation (Creation &&rhs) noexcept
 
 Creation (const Creation &rhs) noexcept=default
 
Creationoperator= (Creation &&rhs) noexcept
 
Creationoperator= (const Creation &rhs) noexcept=default
 
bool isInitialized () const noexcept
 returns true if the object was constructed successfully, otherwise false
 

Friends

class DesignPattern::Creation< Semaphore, SemaphoreError >
 

Additional Inherited Members

- Public Types inherited from DesignPattern::Creation< Semaphore, SemaphoreError >
using CreationPattern_t = Creation< Semaphore, SemaphoreError >
 
using result_t = iox::cxx::expected< Semaphore, SemaphoreError >
 
using errorType_t = SemaphoreError
 
- Static Public Member Functions inherited from DesignPattern::Creation< Semaphore, SemaphoreError >
static result_t create (Targs &&... args) noexcept
 factory method which guarantees that either a working object is produced or an error value describing the error during construction
 
static result_t verify (Semaphore &&newObject) noexcept
 verifies if a class was created successfully
 
static iox::cxx::expected< SemaphoreError > placementCreate (void *const memory, Targs &&... args) noexcept
 factory method which guarantees that either a working object is produced or an error value describing the error during construction
 
- Protected Attributes inherited from DesignPattern::Creation< Semaphore, SemaphoreError >
bool m_isInitialized
 
SemaphoreError m_errorValue
 

Detailed Description

Posix semaphore C++ Wrapping class.

auto semaphore = posix::Semaphore::CreateUnnamed(false, 5);
int value;
if ( semaphore.getValue(value) ) // no error has occurred
{
std::cout << value << std::endl;
}

Member Function Documentation

◆ getValue()

cxx::expected< int, SemaphoreError > iox::posix::Semaphore::getValue ( ) const
noexcept

calls sem_getvalue which gets the value of a semaphore From the sem_getvalue manpage: sem_getvalue() places the current value of the semaphore pointed to sem into the integer pointed to by sval.

If one or more processes or threads are blocked waiting to lock the semaphore with sem_wait(3), POSIX.1 permits two possibilities for the value returned in sval: either 0 is returned; or a negative number whose absolute value is the count of the number of processes and threads currently blocked in sem_wait(3). Linux adopts the former behavior.

Parameters
[in]valuereference in which the value of the semaphore is written to
Returns
expected which contains either the value of the semaphore or the cause why the value could not be retrieved

◆ post()

cxx::expected< SemaphoreError > iox::posix::Semaphore::post ( )
noexcept

calls sem_post which unlocks a semaphore From the sem_post manpage: sem_post() increments (unlocks) the semaphore pointed to by sem. If the semaphore's value consequently becomes greater than zero, then another process or thread blocked in a sem_wait(3) call will be woken up and proceed to lock the semaphore.

Returns
if post fails the expected contains the error which occurred

◆ timedWait()

cxx::expected< SemaphoreWaitState, SemaphoreError > iox::posix::Semaphore::timedWait ( const units::Duration  abs_timeout)
noexcept

see wait()

Parameters
[in]abs_timeouttimeout of the wait
Returns
when successful the SemaphoreWaitState states if a timeout happened or not otherwise the SemaphoreError contains the error

◆ tryWait()

cxx::expected< bool, SemaphoreError > iox::posix::Semaphore::tryWait ( )
noexcept

see wait()

Returns
if the semaphore was decremented the expected contains the value true otherwise false. if an error occurred it is stored inside the expected

◆ wait()

cxx::expected< SemaphoreError > iox::posix::Semaphore::wait ( )
noexcept

calls sem_wait which locks a semaphore From the sem_wait manpage: sem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal handler interrupts the call.

iox_sem_trywait() is the same as sem_wait(), except that if the decrement cannot be immediately performed, then call returns an error (errno set to EAGAIN) instead of blocking.

iox_sem_timedwait() is the same as sem_wait(), except that abs_timeout specifies a limit on the amount of time that the call should block if the decrement cannot be immediately performed.

If the timeout has already expired by the time of the call, and the semaphore could not be locked immediately, then iox_sem_timedwait() fails with a timeout error (errno set to ETIMEDOUT).

If the operation can be performed immediately, then iox_sem_timedwait() never fails with a timeout error, regardless of the value of abs_timeout. Furthermore, the validity of abs_timeout is not checked in this case.

Returns
if an error during the call occurs the error value is set

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