Argon RTOS  1.3.0
Tiny embedded real-time kernel
Ar::Semaphore Class Reference

#include <ar_classes.h>

+ Inheritance diagram for Ar::Semaphore:

Description

Counting semaphore class.

See also
Semaphore::Guard
+ Collaboration diagram for Ar::Semaphore:

Public Member Functions

 Semaphore ()
 Default constructor.
 
 Semaphore (const char *name, unsigned count=1)
 Constructor.
 
 ~Semaphore ()
 Destructor. More...
 
ar_status_t get (uint32_t timeout=kArInfiniteTimeout)
 Acquire the semaphore. More...
 
unsigned getCount () const
 Returns the current semaphore count.
 
const char * getName () const
 Get the semaphore's name.
 
ar_status_t init (const char *name, unsigned count=1)
 Initialiser. More...
 
ar_status_t put ()
 Release the semaphore. More...
 

Constructor & Destructor Documentation

◆ ~Semaphore()

Ar::Semaphore::~Semaphore ( )
inline

Destructor.

Any threads on the blocked list will be unblocked immediately. Their return status from the get() method will be kArObjectDeletedError.

Member Function Documentation

◆ get()

ar_status_t Ar::Semaphore::get ( uint32_t  timeout = kArInfiniteTimeout)
inline

Acquire the semaphore.

The semaphore count is decremented. If the count is 0 upon entering this method then the caller thread is blocked until the count reaches 1. Threads are unblocked in the order in which they were blocked. Priority is not taken into consideration, so priority inversions are possible.

Note
This function may be called from interrupt context only if the timeout parameter is set to kArNoTimeout (or 0).
Parameters
timeoutThe maximum number of milliseconds that the caller is willing to wait in a blocked state before the semaphore can be obtained. If this value is 0, or kArNoTimeout, then this method will return immediately if the semaphore cannot be obtained. Setting the timeout to kArInfiniteTimeout will cause the thread to wait forever for a chance to get the semaphore.
Return values
kArSuccessThe semaphore was obtained without error.
kArTimeoutErrorThe specified amount of time has elapsed before the semaphore could be obtained.
kArObjectDeletedErrorAnother thread deleted the semaphore while the caller was blocked on it.
kArNotFromInterruptErrorA non-zero timeout is not alllowed from the interrupt context.

◆ init()

ar_status_t Ar::Semaphore::init ( const char *  name,
unsigned  count = 1 
)
inline

Initialiser.

Parameters
namePass a name for the semaphore. If NULL is passed the name will be set to an empty string.
countThe initial semaphore count. Setting this value to 0 will cause the first call to get() to block until put() is called. A value of 1 or greater will allow that many calls to get() to succeed.
Return values
kArSuccessSemaphore initialised successfully.

◆ put()

ar_status_t Ar::Semaphore::put ( )
inline

Release the semaphore.

The semaphore count is incremented.

Note
This call is safe from interrupt context.
Return values
kArSuccessThe semaphore was released without error.