POSIX compliant semaphore. More...
#include <cmsis-plus/rtos/os.h>
Classes | |
class | attributes |
Semaphore attributes. More... | |
class | attributes_binary |
Binary semaphore attributes. More... | |
class | attributes_counting |
Counting semaphore attributes. More... | |
Public Types | |
using | count_t = int16_t |
Type of semaphore counter storage. | |
Public Member Functions | |
Constructors & Destructor | |
semaphore (const attributes &attr=initializer_binary) | |
Construct a semaphore object instance. | |
semaphore (const char *name, const attributes &attr=initializer_binary) | |
Construct a named semaphore object instance. | |
~semaphore () | |
Destruct the semaphore object instance. | |
Operators | |
bool | operator== (const semaphore &rhs) const |
Compare semaphores. | |
Public Member Functions | |
result_t | post (void) |
Post (unlock) the semaphore. | |
result_t | wait (void) |
Lock the semaphore, possibly waiting. | |
result_t | try_wait (void) |
Try to lock the semaphore. | |
result_t | timed_wait (clock::duration_t timeout) |
Timed wait to lock the semaphore. | |
count_t | value (void) const |
Get the semaphore count value. | |
result_t | reset (void) |
Reset the semaphore. | |
count_t | initial_value (void) const |
Get the semaphore initial count value. | |
count_t | max_value (void) const |
Get the semaphore maximum count value. | |
Public Member Functions | |
const char * | name (void) const |
Get object name. | |
Static Public Member Functions | |
Operators | |
static void * | operator new (std::size_t bytes) |
Allocate space for a new object instance using the RTOS system allocator. | |
static void * | operator new (std::size_t bytes, void *ptr) |
Emplace a new object instance. | |
static void * | operator new[] (std::size_t bytes) |
Allocate space for an array of new object instances using the RTOS system allocator. | |
static void * | operator new[] (std::size_t bytes, void *ptr) |
Emplace an array of new object instances. | |
static void | operator delete (void *ptr, std::size_t bytes) |
Deallocate the dynamically allocated object instance. using the RTOS system allocator. | |
static void | operator delete[] (void *ptr, std::size_t bytes) |
Deallocate the dynamically allocated array of object. instances using the RTOS system allocator. | |
Static Public Attributes | |
static const attributes_binary | initializer_binary { 0 } |
Default binary semaphore initialiser. | |
static constexpr count_t | max_count_value = 0x7FFF |
Maximum semaphore value. | |
POSIX compliant semaphore.
Semaphores allow threads and interrupts to synchronise their actions.
A useful way to think of a semaphore as used in the real-world systems is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and, if necessary, wait until a unit of the resource becomes available. Semaphores are a useful tool in the prevention of race conditions; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores.
Semaphores should generally be used to synchronise threads with events occurring on interrupts.
For inter-thread synchronisation, to avoid cases of priority inversion, more suitable are mutexes.
sem_t
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition). Definition at line 64 of file os-semaphore.h.
|
inline |
Construct a semaphore object instance.
[in] | attr | Reference to attributes. |
This constructor shall initialise a generic semaphore object with attributes referenced by attr. If the attributes specified by attr are modified later, the semaphore attributes shall not be affected. Upon successful initialisation, the state of the semaphore object shall become initialised.
Only the semaphore object itself may be used for performing synchronisation. It is not allowed to make copies of semaphore objects.
In cases where default semaphore attributes are appropriate, the variable semaphore::initializer_binary
can be used to initialise semaphores. The effect shall be equivalent to creating a semaphore object with the default constructor.
sem_init()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 748 of file os-semaphore.h.
os::rtos::semaphore::semaphore | ( | const char * | name, |
const attributes & | attr = initializer_binary |
||
) |
Construct a named semaphore object instance.
[in] | name | Pointer to name. |
[in] | attr | Reference to attributes. |
This constructor shall initialise a named generic semaphore object with attributes referenced by attr. If the attributes specified by attr are modified later, the semaphore attributes shall not be affected. Upon successful initialisation, the state of the semaphore object shall become initialised.
Only the semaphore object itself may be used for performing synchronisation. It is not allowed to make copies of semaphore objects.
In cases where default semaphore attributes are appropriate, the variable semaphore::initializer_binary
can be used to initialise semaphores. The effect shall be equivalent to creating a semaphore object with the default constructor.
If the attr attributes are modified after the semaphore creation, the semaphore attributes shall not be affected.
sem_init()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 161 of file os-semaphore.cpp.
os::rtos::semaphore::~semaphore | ( | ) |
Destruct the semaphore object instance.
This destructor shall destroy the semaphore object; the object becomes, in effect, uninitialised. An implementation may cause the destructor to set the object to an invalid value.
It is safe to destroy an initialised semaphore upon which no threads are currently blocked. The effect of destroying a semaphore upon which other threads are currently blocked is undefined.
sem_destroy()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 236 of file os-semaphore.cpp.
|
inline |
Get the semaphore initial count value.
Definition at line 772 of file os-semaphore.h.
|
inline |
Get the semaphore maximum count value.
Definition at line 784 of file os-semaphore.h.
|
inlineinherited |
Get object name.
All objects return a non-null string; anonymous objects return "-"
.
Definition at line 774 of file os-decls.h.
|
inlinestaticinherited |
Deallocate the dynamically allocated object instance. using the RTOS system allocator.
ptr | Pointer to object. |
bytes | Number of bytes to deallocate. |
The deallocation function (3.7.4.2) called by a delete-expression to render the value of ptr invalid.
ptr shall be a null pointer or its value shall be a value returned by an earlier call to the (possibly replaced) operator new() which has not been invalidated by an intervening call to operator delete(void*).
If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.
The storage is deallocated using the RTOS system allocator.
Definition at line 137 of file os-inlines.h.
|
inlinestaticinherited |
Deallocate the dynamically allocated array of object. instances using the RTOS system allocator.
ptr | Pointer to array of objects. |
bytes | Number of bytes to deallocate. |
The deallocation function (3.7.4.2) called by the array form of a delete-expression to render the value of ptr invalid.
If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.
The storage is deallocated using the RTOS system allocator.
Definition at line 158 of file os-inlines.h.
|
inlinestaticinherited |
Allocate space for a new object instance using the RTOS system allocator.
bytes | Number of bytes to allocate. |
The allocation function (3.7.4.1) called by a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).
The storage is allocated using the RTOS system allocator.
Definition at line 59 of file os-inlines.h.
|
inlinestaticinherited |
Emplace a new object instance.
bytes | Number of bytes to emplace. |
ptr | Pointer to location to emplace the object. |
The allocation function (3.7.4.1) called by a placement new-expression to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).
The storage is allocated using the RTOS system allocator.
Definition at line 96 of file os-inlines.h.
|
inlinestaticinherited |
Allocate space for an array of new object instances using the RTOS system allocator.
bytes | Number of bytes to allocate. |
The allocation function (3.7.4.1) called by the array form of a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.
The storage is allocated using the RTOS system allocator.
Definition at line 77 of file os-inlines.h.
|
inlinestaticinherited |
Emplace an array of new object instances.
bytes | Number of bytes to emplace. |
ptr | Pointer to location to emplace the object. |
The allocation function (3.7.4.1) called by the array form of a placement new-expression to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.
The storage is allocated using the RTOS system allocator.
Definition at line 113 of file os-inlines.h.
|
inline |
Compare semaphores.
true | The given semaphore is the same as this semaphore. |
false | The semaphores are different. |
Identical semaphores should have the same memory address.
Definition at line 760 of file os-semaphore.h.
result_t os::rtos::semaphore::post | ( | void | ) |
Post (unlock) the semaphore.
result::ok | The semaphore was posted. |
EAGAIN | The maximum count value was exceeded. |
ENOTRECOVERABLE | The semaphore could not be posted (extension to POSIX). |
Perform a post operation on the semaphore, informing the waiting consumers that one more resource is available. The semaphore count is incremented, up to max_value.
If the semaphore count resulting from this operation is positive, then no threads were blocked waiting for the semaphore to become unlocked.
If the count of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore shall be allowed to return successfully from its call to wait()
.
If the Process Scheduling option is supported, the thread to be unblocked shall be chosen in a manner appropriate to the scheduling policies and parameters in effect for the blocked threads. In the case of the schedulers SCHED_FIFO and SCHED_RR, the highest priority waiting thread shall be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest shall be unblocked. If the Process Scheduling option is not defined, the choice of a thread to unblock is unspecified. If the scheduling policy is SCHED_SPORADIC, the semantics are as per SCHED_FIFO.
sem_post()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 345 of file os-semaphore.cpp.
result_t os::rtos::semaphore::reset | ( | void | ) |
Reset the semaphore.
result::ok | The semaphore was reset. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
Reset the counter to the initial value.
Definition at line 732 of file os-semaphore.cpp.
result_t os::rtos::semaphore::timed_wait | ( | clock::duration_t | timeout | ) |
Timed wait to lock the semaphore.
[in] | timeout | Timeout to wait. |
result::ok | The calling process successfully performed the semaphore lock operation. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
EINVAL | Invalid timeout (POSIX limits the timeout to 1000 million ns) |
ETIMEDOUT | The semaphore could not be locked before the specified timeout expired. |
ENOTRECOVERABLE | Semaphore wait failed (extension to POSIX). |
EDEADLK | A deadlock condition was detected. |
EINTR | The operation was interrupted. |
Try to perform a lock operation; if the semaphore cannot be locked without waiting for another process or thread to unlock the semaphore by performing a post()
function, this wait shall be terminated when the specified timeout expires.
The timeout shall expire after the number of time units (that is when the value of that clock equals or exceeds (now()+duration). The resolution of the timeout shall be the resolution of the clock on which it is based.
Under no circumstance shall the function fail with a timeout if the semaphore can be locked immediately. The validity of the timeout need not be checked if the semaphore can be locked immediately.
The clock used for timeouts can be specified via the clock
attribute. By default, the clock derived from the scheduler timer is used, and the durations are expressed in ticks.
sem_timedwait()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition). Definition at line 591 of file os-semaphore.cpp.
result_t os::rtos::semaphore::try_wait | ( | void | ) |
Try to lock the semaphore.
result::ok | The calling process successfully performed the semaphore lock operation. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
EWOULDBLOCK | The semaphore was already locked. |
ENOTRECOVERABLE | Semaphore wait failed (extension to POSIX). |
EDEADLK | A deadlock condition was detected. |
EINTR | The operation was interrupted. |
Tries to perform a lock operation only if the semaphore is currently not locked; that is, if the semaphore value is currently positive it decrements it and returns success. Otherwise, it shall not lock the semaphore.
Upon successful return, the state of the semaphore shall be locked and shall remain locked until the post()
function is executed and returns successfully.
sem_trywait()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 521 of file os-semaphore.cpp.
semaphore::count_t os::rtos::semaphore::value | ( | void | ) | const |
Get the semaphore count value.
The value()
function shall return the value of the semaphore without affecting the state of the semaphore. The value represents an actual semaphore value that occurred at some unspecified time during the call, but it need not be the actual value of the semaphore when it is returned to the calling process. This value reflects the number of available resources.
If the semaphore is locked, then the returned value shall either be set to zero or to a negative number whose absolute value represents the number of processes waiting for the semaphore at some unspecified time during the call.
sem_getvalue()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 713 of file os-semaphore.cpp.
result_t os::rtos::semaphore::wait | ( | void | ) |
Lock the semaphore, possibly waiting.
result::ok | The calling process successfully performed the semaphore lock operation. |
EPERM | Cannot be invoked from an Interrupt Service Routines. |
ENOTRECOVERABLE | Semaphore wait failed (extension to POSIX). |
EDEADLK | A deadlock condition was detected. |
EINTR | The operation was interrupted. |
Perform a lock operation on the semaphore.
If the current value is positive, it is decremented, and the call returns immediately
If the semaphore value is currently zero, then the calling thread shall not return from the call to wait() until it either locks the semaphore or the call is interrupted by a signal.
Upon successful return, the state of the semaphore shall be locked and shall remain locked until the post()
function is executed and returns successfully.
The function is interruptible by the delivery of an external event (signal, thread cancel, etc).
sem_wait()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 424 of file os-semaphore.cpp.