µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os::rtos::semaphore Class Reference

POSIX compliant semaphore. More...

#include <cmsis-plus/rtos/os.h>

+ Inheritance diagram for os::rtos::semaphore:

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.
 

Detailed Description

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.

POSIX compatibility
Inspired by sem_t from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).

Definition at line 64 of file os-semaphore.h.

Constructor & Destructor Documentation

◆ semaphore() [1/2]

os::rtos::semaphore::semaphore ( const attributes attr = initializer_binary)
inline

Construct a semaphore object instance.

Parameters
[in]attrReference 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.

POSIX compatibility
Inspired by sem_init() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 748 of file os-semaphore.h.

◆ semaphore() [2/2]

os::rtos::semaphore::semaphore ( const char *  name,
const attributes attr = initializer_binary 
)

Construct a named semaphore object instance.

Parameters
[in]namePointer to name.
[in]attrReference 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.

POSIX compatibility
Inspired by sem_init() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 161 of file os-semaphore.cpp.

◆ ~semaphore()

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.

POSIX compatibility
Inspired by sem_destroy() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 236 of file os-semaphore.cpp.

Member Function Documentation

◆ initial_value()

semaphore::count_t os::rtos::semaphore::initial_value ( void  ) const
inline

Get the semaphore initial count value.

Parameters
None.
Returns
The numeric value set from attributes.
POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 772 of file os-semaphore.h.

◆ max_value()

semaphore::count_t os::rtos::semaphore::max_value ( void  ) const
inline

Get the semaphore maximum count value.

Parameters
None.
Returns
The numeric value set from attributes.
POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 784 of file os-semaphore.h.

◆ name()

const char * os::rtos::internal::object_named::name ( void  ) const
inlineinherited

Get object name.

Parameters
None.
Returns
A null terminated string.

All objects return a non-null string; anonymous objects return "-".

Note
Can be invoked from Interrupt Service Routines.

Definition at line 774 of file os-decls.h.

◆ operator delete()

void os::rtos::internal::object_named_system::operator delete ( void *  ptr,
std::size_t  bytes 
)
inlinestaticinherited

Deallocate the dynamically allocated object instance. using the RTOS system allocator.

Parameters
ptrPointer to object.
bytesNumber of bytes to deallocate.
Returns
Nothing.

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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 137 of file os-inlines.h.

◆ operator delete[]()

void os::rtos::internal::object_named_system::operator delete[] ( void *  ptr,
std::size_t  bytes 
)
inlinestaticinherited

Deallocate the dynamically allocated array of object. instances using the RTOS system allocator.

Parameters
ptrPointer to array of objects.
bytesNumber of bytes to deallocate.
Returns
Nothing.

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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 158 of file os-inlines.h.

◆ operator new() [1/2]

void * os::rtos::internal::object_named_system::operator new ( std::size_t  bytes)
inlinestaticinherited

Allocate space for a new object instance using the RTOS system allocator.

Parameters
bytesNumber of bytes to allocate.
Returns
Pointer to allocated object.

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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 59 of file os-inlines.h.

◆ operator new() [2/2]

void * os::rtos::internal::object_named_system::operator new ( std::size_t  bytes,
void *  ptr 
)
inlinestaticinherited

Emplace a new object instance.

Parameters
bytesNumber of bytes to emplace.
ptrPointer to location to emplace the object.
Returns
Pointer to emplaced 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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 96 of file os-inlines.h.

◆ operator new[]() [1/2]

void * os::rtos::internal::object_named_system::operator new[] ( std::size_t  bytes)
inlinestaticinherited

Allocate space for an array of new object instances using the RTOS system allocator.

Parameters
bytesNumber of bytes to allocate.
Returns
Pointer to allocated array.

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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 77 of file os-inlines.h.

◆ operator new[]() [2/2]

void * os::rtos::internal::object_named_system::operator new[] ( std::size_t  bytes,
void *  ptr 
)
inlinestaticinherited

Emplace an array of new object instances.

Parameters
bytesNumber of bytes to emplace.
ptrPointer to location to emplace the object.
Returns
Pointer to emplaced array.

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.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 113 of file os-inlines.h.

◆ operator==()

bool os::rtos::semaphore::operator== ( const semaphore rhs) const
inline

Compare semaphores.

Return values
trueThe given semaphore is the same as this semaphore.
falseThe semaphores are different.

Identical semaphores should have the same memory address.

Definition at line 760 of file os-semaphore.h.

◆ post()

result_t os::rtos::semaphore::post ( void  )

Post (unlock) the semaphore.

Parameters
None.
Return values
result::okThe semaphore was posted.
EAGAINThe maximum count value was exceeded.
ENOTRECOVERABLEThe 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.

POSIX compatibility
Inspired by sem_post() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Note
Can be invoked from Interrupt Service Routines.
Warning
Applications using these functions may be subject to priority inversion.

Definition at line 345 of file os-semaphore.cpp.

◆ reset()

result_t os::rtos::semaphore::reset ( void  )

Reset the semaphore.

Parameters
None.
Return values
result::okThe semaphore was reset.
EPERMCannot be invoked from an Interrupt Service Routines.

Reset the counter to the initial value.

POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 732 of file os-semaphore.cpp.

◆ timed_wait()

result_t os::rtos::semaphore::timed_wait ( clock::duration_t  timeout)

Timed wait to lock the semaphore.

Parameters
[in]timeoutTimeout to wait.
Return values
result::okThe calling process successfully performed the semaphore lock operation.
EPERMCannot be invoked from an Interrupt Service Routines.
EINVALInvalid timeout (POSIX limits the timeout to 1000 million ns)
ETIMEDOUTThe semaphore could not be locked before the specified timeout expired.
ENOTRECOVERABLESemaphore wait failed (extension to POSIX).
EDEADLKA deadlock condition was detected.
EINTRThe 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.

POSIX compatibility
Inspired by sem_timedwait() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Differences from the standard:
  • the timeout is not expressed as an absolute time point, but as a relative number of timer ticks (by default, the SysTick clock for CMSIS).
  • for consistency reasons, EWOULDBLOCK is used, instead of EAGAIN
Warning
Cannot be invoked from Interrupt Service Routines.
Applications using these functions may be subject to priority inversion.

Definition at line 591 of file os-semaphore.cpp.

◆ try_wait()

result_t os::rtos::semaphore::try_wait ( void  )

Try to lock the semaphore.

Parameters
None.
Return values
result::okThe calling process successfully performed the semaphore lock operation.
EPERMCannot be invoked from an Interrupt Service Routines.
EWOULDBLOCKThe semaphore was already locked.
ENOTRECOVERABLESemaphore wait failed (extension to POSIX).
EDEADLKA deadlock condition was detected.
EINTRThe 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.

POSIX compatibility
Inspired by sem_trywait() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Note
Can be invoked from Interrupt Service Routines.
Warning
Applications using these functions may be subject to priority inversion.

Definition at line 521 of file os-semaphore.cpp.

◆ value()

semaphore::count_t os::rtos::semaphore::value ( void  ) const

Get the semaphore count value.

Parameters
None.
Returns
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.

POSIX compatibility
Inspired by sem_getvalue() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Note
Can be invoked from Interrupt Service Routines.

Definition at line 713 of file os-semaphore.cpp.

◆ wait()

result_t os::rtos::semaphore::wait ( void  )

Lock the semaphore, possibly waiting.

Parameters
None.
Return values
result::okThe calling process successfully performed the semaphore lock operation.
EPERMCannot be invoked from an Interrupt Service Routines.
ENOTRECOVERABLESemaphore wait failed (extension to POSIX).
EDEADLKA deadlock condition was detected.
EINTRThe 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).

POSIX compatibility
Inspired by sem_wait() from <semaphore.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.
Applications using these functions may be subject to priority inversion.

Definition at line 424 of file os-semaphore.cpp.


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