POSIX compliant counting semaphore. More...
#include <cmsis-plus/rtos/os.h>
Public Types | |
using | count_t = int16_t |
Type of semaphore counter storage. | |
Public Member Functions | |
Constructors & Destructor | |
semaphore_counting (const count_t max_value, const count_t initial_value) | |
Construct a binary semaphore object instance. | |
semaphore_counting (const char *name, const count_t max_value, const count_t initial_value) | |
Construct a named binary semaphore object instance. | |
~semaphore_counting () | |
Destruct the semaphore object instance. | |
Operators | |
bool | operator== (const semaphore_counting &rhs) const |
Compare semaphores. | |
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 counting semaphore.
Definition at line 592 of file os-semaphore.h.
|
inline |
Construct a binary semaphore object instance.
[in] | max_value | Maximum count value. |
[in] | initial_value | Initial count value; 0 if missing. |
This constructor shall initialise a counting semaphore object with the given max_value and initial_value. 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.
sem_init()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 889 of file os-semaphore.h.
|
inline |
Construct a named binary semaphore object instance.
[in] | name | Pointer to name. |
[in] | max_value | Maximum count value. |
[in] | initial_value | Initial count value; 0 if missing. |
This constructor shall initialise a named counting semaphore object with the given max_value and initial_value. 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.
sem_init()
from <semaphore.h>
(IEEE Std 1003.1, 2013 Edition).Definition at line 916 of file os-semaphore.h.
|
inline |
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 944 of file os-semaphore.h.
|
inlineinherited |
Get the semaphore initial count value.
Definition at line 772 of file os-semaphore.h.
|
inlineinherited |
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.
|
inlineinherited |
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.
bool os::rtos::semaphore_counting::operator== | ( | const semaphore_counting & | rhs | ) | const |
Compare semaphores.
true | The given semaphore is the same as this semaphore. |
false | The semaphores are different. |
|
inherited |
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.
|
inherited |
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.
|
inherited |
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.
|
inherited |
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.
|
inherited |
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.
|
inherited |
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.