Mistake on this page?
Report an issue in GitHub or email us
Public Member Functions
Semaphore Class Reference

The Semaphore class is used to manage and protect access to a set of shared resources. More...

#include <Semaphore.h>

Inheritance diagram for Semaphore:
NonCopyable< Semaphore >

Public Member Functions

 Semaphore (int32_t count=0)
 Create and Initialize a Semaphore object used for managing resources. More...
 
 Semaphore (int32_t count, uint16_t max_count)
 Create and Initialize a Semaphore object used for managing resources. More...
 
int32_t wait (uint32_t millisec=osWaitForever)
 Wait until a Semaphore resource becomes available. More...
 
int32_t wait_until (uint64_t millisec)
 Wait until a Semaphore resource becomes available. More...
 
void acquire ()
 Wait until a Semaphore resource becomes available. More...
 
bool try_acquire ()
 Try to acquire a Semaphore resource, and return immediately. More...
 
bool try_acquire_for (uint32_t millisec)
 Wait until a Semaphore resource becomes available. More...
 
bool try_acquire_until (uint64_t millisec)
 Wait until a Semaphore resource becomes available. More...
 
osStatus release (void)
 Release a Semaphore resource that was obtain with Semaphore::acquire. More...
 
 ~Semaphore ()
 Semaphore destructor. More...
 

Detailed Description

The Semaphore class is used to manage and protect access to a set of shared resources.

Note
Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).

Definition at line 47 of file Semaphore.h.

Constructor & Destructor Documentation

Semaphore ( int32_t  count = 0)

Create and Initialize a Semaphore object used for managing resources.

Parameters
countnumber of available resources; maximum index value is (count-1). (default: 0).
Note
You cannot call this function from ISR context.
Semaphore ( int32_t  count,
uint16_t  max_count 
)

Create and Initialize a Semaphore object used for managing resources.

Parameters
countnumber of available resources
max_countmaximum number of available resources
Note
You cannot call this function from ISR context.
~Semaphore ( )

Semaphore destructor.

Note
You cannot call this function from ISR context.

Member Function Documentation

void acquire ( )

Wait until a Semaphore resource becomes available.

Note
You cannot call this function from ISR context.
osStatus release ( void  )

Release a Semaphore resource that was obtain with Semaphore::acquire.

Returns
status code that indicates the execution status of the function: osOK the token has been correctly released. osErrorResource the maximum token count has been reached. osErrorParameter internal error.
Note
You may call this function from ISR context.
bool try_acquire ( )

Try to acquire a Semaphore resource, and return immediately.

Returns
true if a resource was acquired, false otherwise.
Note
equivalent to try_acquire_for(0)
You may call this function from ISR context.
bool try_acquire_for ( uint32_t  millisec)

Wait until a Semaphore resource becomes available.

Parameters
millisectimeout value.
Returns
true if a resource was acquired, false otherwise.
Note
You may call this function from ISR context if the millisec parameter is set to 0.
bool try_acquire_until ( uint64_t  millisec)

Wait until a Semaphore resource becomes available.

Parameters
millisecabsolute timeout time, referenced to Kernel::get_ms_count()
Returns
true if a resource was acquired, false otherwise.
Note
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the acquire attempt will time out earlier than specified.
You cannot call this function from ISR context.
int32_t wait ( uint32_t  millisec = osWaitForever)

Wait until a Semaphore resource becomes available.

Deprecated:
Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
Parameters
millisectimeout value. (default: osWaitForever).
Returns
number of available tokens, before taking one; or -1 in case of incorrect parameters
Note
You may call this function from ISR context if the millisec parameter is set to 0.
int32_t wait_until ( uint64_t  millisec)

Wait until a Semaphore resource becomes available.

Deprecated:
Do not use this function. This function has been replaced with try_acquire_until().
Parameters
millisecabsolute timeout time, referenced to Kernel::get_ms_count()
Returns
number of available tokens, before taking one; or -1 in case of incorrect parameters
Note
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the acquire attempt will time out earlier than specified.
You cannot call this function from ISR context.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.