Class Semaphore
Defined in File semaphore.hpp
Inheritance Relationships
Base Type
public DesignPattern::Creation< Semaphore, SemaphoreError >(Template Class Creation)
Class Documentation
-
class Semaphore : public DesignPattern::Creation<Semaphore, SemaphoreError>
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; }
Public 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(const Semaphore&) = delete
We are denying Semaphore copy since it manages the semaphore resource and the underlying concept did not include copying.
-
Semaphore &operator=(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.
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:
value – [in] reference 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
-
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.
- Returns:
if post fails the expected contains the error which occurred
-
cxx::expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration abs_timeout) noexcept
see wait()
- Parameters:
abs_timeout – [in] timeout of the wait
- Returns:
when successful the SemaphoreWaitState states if a timeout happened or not otherwise the SemaphoreError contains the error
-
cxx::expected<bool, SemaphoreError> 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
-
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.
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
Friends
- friend class DesignPattern::Creation< Semaphore, SemaphoreError >
-
Semaphore() noexcept