µ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_counting Class Reference

POSIX compliant counting semaphore. More...

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

+ Inheritance diagram for os::rtos::semaphore_counting:

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.
 

Detailed Description

POSIX compliant counting semaphore.

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

Constructor & Destructor Documentation

◆ semaphore_counting() [1/2]

os::rtos::semaphore_counting::semaphore_counting ( const count_t  max_value,
const count_t  initial_value 
)
inline

Construct a binary semaphore object instance.

Parameters
[in]max_valueMaximum count value.
[in]initial_valueInitial 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.

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 873 of file os-semaphore.h.

◆ semaphore_counting() [2/2]

os::rtos::semaphore_counting::semaphore_counting ( const char *  name,
const count_t  max_value,
const count_t  initial_value 
)
inline

Construct a named binary semaphore object instance.

Parameters
[in]namePointer to name.
[in]max_valueMaximum count value.
[in]initial_valueInitial 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.

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 899 of file os-semaphore.h.

◆ ~semaphore_counting()

os::rtos::semaphore_counting::~semaphore_counting ( )
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.

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 926 of file os-semaphore.h.

Member Function Documentation

◆ initial_value()

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

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 759 of file os-semaphore.h.

◆ max_value()

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

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 771 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 759 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 122 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 143 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 44 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 81 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 62 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 98 of file os-inlines.h.

◆ operator==() [1/2]

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

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 747 of file os-semaphore.h.

◆ operator==() [2/2]

bool os::rtos::semaphore_counting::operator== ( const semaphore_counting rhs) const

Compare semaphores.

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

◆ post()

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

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 335 of file os-semaphore.cpp.

◆ reset()

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

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 742 of file os-semaphore.cpp.

◆ timed_wait()

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

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 583 of file os-semaphore.cpp.

◆ try_wait()

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

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 513 of file os-semaphore.cpp.

◆ value()

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

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 723 of file os-semaphore.cpp.

◆ wait()

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

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 416 of file os-semaphore.cpp.


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