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

C API semaphore definitions. More...

Classes

struct  os_semaphore_attr_s
 Semaphore attributes. More...
 
struct  os_semaphore_s
 Semaphore object storage. More...
 

Typedefs

typedef struct os_semaphore_attr_s os_semaphore_attr_t
 Semaphore attributes.
 
typedef int16_t os_semaphore_count_t
 Type of variables holding semaphore counts.
 
typedef struct os_semaphore_s os_semaphore_t
 Semaphore object storage.
 

Semaphore Attributes Functions

void os_semaphore_attr_init (os_semaphore_attr_t *attr)
 Initialise the counting semaphore attributes.
 
void os_semaphore_attr_binary_init (os_semaphore_attr_t *attr, const os_semaphore_count_t initial_value)
 Initialise the binary semaphore attributes.
 
void os_semaphore_attr_counting_init (os_semaphore_attr_t *attr, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
 Initialise the counting semaphore attributes.
 
const os_semaphore_attr_tos_semaphore_attr_get_binary (void)
 Get a binary semaphore attributes object instance.
 

Semaphore Creation Functions

void os_semaphore_construct (os_semaphore_t *semaphore, const char *name, const os_semaphore_attr_t *attr)
 Construct a statically allocated semaphore object instance.
 
void os_semaphore_binary_construct (os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t initial_value)
 Construct a statically allocated binary semaphore object instance.
 
void os_semaphore_counting_construct (os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
 Construct a statically allocated counting semaphore object instance.
 
void os_semaphore_destruct (os_semaphore_t *semaphore)
 Destruct the statically allocated semaphore object instance.
 
os_semaphore_tos_semaphore_new (const char *name, const os_semaphore_attr_t *attr)
 Allocated a semaphore object instance and construct it.
 
os_semaphore_tos_semaphore_binary_new (const char *name, const os_semaphore_count_t initial_value)
 Allocate a binary semaphore object instance and construct it.
 
os_semaphore_tos_semaphore_counting_new (const char *name, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
 Allocate a counting semaphore object instance and construct it.
 
void os_semaphore_delete (os_semaphore_t *semaphore)
 Destruct the semaphore object instance.
 

Semaphore Functions

const char * os_semaphore_get_name (os_semaphore_t *semaphore)
 Get the semaphore name.
 
os_result_t os_semaphore_post (os_semaphore_t *semaphore)
 Post (unlock) the semaphore.
 
os_result_t os_semaphore_wait (os_semaphore_t *semaphore)
 Lock the semaphore, possibly waiting.
 
os_result_t os_semaphore_try_wait (os_semaphore_t *semaphore)
 Try to lock the semaphore.
 
os_result_t os_semaphore_timed_wait (os_semaphore_t *semaphore, os_clock_duration_t timeout)
 Timed wait to lock the semaphore.
 
os_semaphore_count_t os_semaphore_get_value (os_semaphore_t *semaphore)
 Get the semaphore count value.
 
os_result_t os_semaphore_reset (os_semaphore_t *semaphore)
 Reset the semaphore.
 
os_semaphore_count_t os_semaphore_get_initial_value (os_semaphore_t *semaphore)
 Get the semaphore initial count value.
 
os_semaphore_count_t os_semaphore_get_max_value (os_semaphore_t *semaphore)
 Get the semaphore maximum count value.
 

Compatibility Macros

#define os_semaphore_create   os_semaphore_construct
 
#define os_semaphore_binary_create   os_semaphore_binary_construct
 
#define os_semaphore_counting_create   os_semaphore_counting_construct
 
#define os_semaphore_destroy   os_semaphore_destruct
 

Detailed Description

C API semaphore definitions.

For the complete definition, see
RTOS C++ API
Examples
int
os_main (int argc, char* argv[])
{
{
// Binary semaphore, start at 0.
name = os_semaphore_get_name (&sp1);
}
{
// Custom semaphore.
asp2.sm_initial_value = 3;
asp2.sm_max_value = 7;
os_semaphore_construct (&sp2, "sp2", &asp2);
}
{
// Counting semaphore, 7 resources, start at 7.
os_semaphore_counting_construct (&sp3, "sp3", 7, 7);
}
{
// Custom binary semaphore.
os_semaphore_construct (&sp4, "sp4", &asp4);
}
}
os_clock_t * os_clock_get_rtclock(void)
Get rtclock (the real-time clock).
os_result_t os_semaphore_timed_wait(os_semaphore_t *semaphore, os_clock_duration_t timeout)
Timed wait to lock the semaphore.
os_semaphore_count_t os_semaphore_get_initial_value(os_semaphore_t *semaphore)
Get the semaphore initial count value.
const char * os_semaphore_get_name(os_semaphore_t *semaphore)
Get the semaphore name.
void os_semaphore_attr_binary_init(os_semaphore_attr_t *attr, const os_semaphore_count_t initial_value)
Initialise the binary semaphore attributes.
os_result_t os_semaphore_post(os_semaphore_t *semaphore)
Post (unlock) the semaphore.
os_result_t os_semaphore_wait(os_semaphore_t *semaphore)
Lock the semaphore, possibly waiting.
os_semaphore_count_t os_semaphore_get_value(os_semaphore_t *semaphore)
Get the semaphore count value.
void os_semaphore_destruct(os_semaphore_t *semaphore)
Destruct the statically allocated semaphore object instance.
void os_semaphore_binary_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t initial_value)
Construct a statically allocated binary semaphore object instance.
void os_semaphore_counting_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
Construct a statically allocated counting semaphore object instance.
os_result_t os_semaphore_reset(os_semaphore_t *semaphore)
Reset the semaphore.
void os_semaphore_attr_init(os_semaphore_attr_t *attr)
Initialise the counting semaphore attributes.
void os_semaphore_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_attr_t *attr)
Construct a statically allocated semaphore object instance.
os_result_t os_semaphore_try_wait(os_semaphore_t *semaphore)
Try to lock the semaphore.
int os_main(int argc, char *argv[])
Application entry point, running on the main thread context.
Semaphore attributes.
void * clock
Pointer to clock object instance.
os_semaphore_count_t sm_max_value
Semaphore max count value.
os_semaphore_count_t sm_initial_value
Semaphore initial count value.
Semaphore object storage.

Macro Definition Documentation

◆ os_semaphore_binary_create

#define os_semaphore_binary_create   os_semaphore_binary_construct

Definition at line 1926 of file os-c-api.h.

◆ os_semaphore_counting_create

#define os_semaphore_counting_create   os_semaphore_counting_construct

Definition at line 1927 of file os-c-api.h.

◆ os_semaphore_create

#define os_semaphore_create   os_semaphore_construct

Definition at line 1925 of file os-c-api.h.

◆ os_semaphore_destroy

#define os_semaphore_destroy   os_semaphore_destruct

Definition at line 1928 of file os-c-api.h.

Typedef Documentation

◆ os_semaphore_attr_t

Semaphore attributes.

Initialise this structure with os_semaphore_attr_init() and then set any of the individual members directly.

See also
os::rtos::semaphore::attributes

◆ os_semaphore_count_t

typedef int16_t os_semaphore_count_t

Type of variables holding semaphore counts.

See also
os::rtos::semaphore::count_t

Definition at line 1088 of file os-c-decls.h.

◆ os_semaphore_t

Semaphore object storage.

This C structure has the same size as the C++ os::rtos::semaphore object and must be initialised with os_semaphore_create().

Later on a pointer to it can be used both in C and C++ to refer to the semaphore object instance.

The members of this structure are hidden and should not be used directly, but only through specific functions.

See also
os::rtos::semaphore

Function Documentation

◆ os_semaphore_attr_binary_init()

void os_semaphore_attr_binary_init ( os_semaphore_attr_t attr,
const os_semaphore_count_t  initial_value 
)

Initialise the binary semaphore attributes.

Parameters
[in]attrPointer to semaphore attributes object instance.
[in]initial_valueInitial count value.
Returns
Nothing.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::attributes_binary

Definition at line 1959 of file os-c-wrapper.cpp.

◆ os_semaphore_attr_counting_init()

void os_semaphore_attr_counting_init ( os_semaphore_attr_t attr,
const os_semaphore_count_t  max_value,
const os_semaphore_count_t  initial_value 
)

Initialise the counting semaphore attributes.

Parameters
[in]attrPointer to semaphore attributes object instance.
[in]max_valueMaximum count value.
[in]initial_valueInitial count value.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::attributes_counting

Definition at line 1974 of file os-c-wrapper.cpp.

◆ os_semaphore_attr_get_binary()

const os_semaphore_attr_t * os_semaphore_attr_get_binary ( void  )

Get a binary semaphore attributes object instance.

Returns
Pointer to semaphore attributes object instance.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::initializer_binary

Definition at line 1990 of file os-c-wrapper.cpp.

◆ os_semaphore_attr_init()

void os_semaphore_attr_init ( os_semaphore_attr_t attr)

Initialise the counting semaphore attributes.

Parameters
[in]attrPointer to semaphore attributes object instance.
Returns
Nothing.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::attributes

Definition at line 1945 of file os-c-wrapper.cpp.

◆ os_semaphore_binary_construct()

void os_semaphore_binary_construct ( os_semaphore_t semaphore,
const char *  name,
const os_semaphore_count_t  initial_value 
)

Construct a statically allocated binary semaphore object instance.

Parameters
[in]semaphorePointer to semaphore object instance storage.
[in]namePointer to name (may be NULL).
[in]initial_valueInitial count value.
Returns
Nothing.
Note
Must be paired with os_semaphore_destruct().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore_binary

Definition at line 2025 of file os-c-wrapper.cpp.

◆ os_semaphore_binary_new()

os_semaphore_t * os_semaphore_binary_new ( const char *  name,
const os_semaphore_count_t  initial_value 
)

Allocate a binary semaphore object instance and construct it.

Parameters
[in]namePointer to name (may be NULL).
[in]initial_valueInitial count value.
Returns
Pointer to new semaphore object instance.

Dynamically allocate the binary semaphore object instance using the RTOS system allocator and construct it.

Note
Equivalent of C++ new semaphore_binary(...).
Must be paired with os_semaphore_delete().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore_binary

Definition at line 2105 of file os-c-wrapper.cpp.

◆ os_semaphore_construct()

void os_semaphore_construct ( os_semaphore_t semaphore,
const char *  name,
const os_semaphore_attr_t attr 
)

Construct a statically allocated semaphore object instance.

Parameters
[in]semaphorePointer to semaphore object instance storage.
[in]namePointer to name (may be NULL).
[in]attrPointer to attributes (may be NULL).
Returns
Nothing.
Note
Must be paired with os_semaphore_destruct().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore

Definition at line 2004 of file os-c-wrapper.cpp.

◆ os_semaphore_counting_construct()

void os_semaphore_counting_construct ( os_semaphore_t semaphore,
const char *  name,
const os_semaphore_count_t  max_value,
const os_semaphore_count_t  initial_value 
)

Construct a statically allocated counting semaphore object instance.

Parameters
[in]semaphorePointer to semaphore object instance storage.
[in]namePointer to name (may be NULL).
[in]max_valueMaximum count value.
[in]initial_valueInitial count value.
Returns
Nothing.
Note
Must be paired with os_semaphore_destruct().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore_counting

Definition at line 2042 of file os-c-wrapper.cpp.

◆ os_semaphore_counting_new()

os_semaphore_t * os_semaphore_counting_new ( const char *  name,
const os_semaphore_count_t  max_value,
const os_semaphore_count_t  initial_value 
)

Allocate a counting semaphore object instance and construct it.

Parameters
[in]namePointer to name (may be NULL).
[in]max_valueMaximum count value.
[in]initial_valueInitial count value.
Returns
Pointer to new semaphore object instance.

Dynamically allocate the counting semaphore object instance using the RTOS system allocator and construct it.

Note
Equivalent of C++ new semaphore_counting(...).
Must be paired with os_semaphore_delete().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore_counting

Definition at line 2126 of file os-c-wrapper.cpp.

◆ os_semaphore_delete()

void os_semaphore_delete ( os_semaphore_t semaphore)

Destruct the semaphore object instance.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
Nothing.

Destruct the semaphore and deallocate the dynamically allocated space using the RTOS system allocator.

Note
Equivalent of C++ delete ptr_semaphore.
Must be paired with os_semaphore_new() or os_semaphore_binary_new() or os_semaphore_counting_new().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore

Definition at line 2149 of file os-c-wrapper.cpp.

◆ os_semaphore_destruct()

void os_semaphore_destruct ( os_semaphore_t semaphore)

Destruct the statically allocated semaphore object instance.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
Nothing.
Note
Must be paired with os_semaphore_construct() or os_semaphore_binary_construct() or os_semaphore_counting_construct().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore

Definition at line 2061 of file os-c-wrapper.cpp.

◆ os_semaphore_get_initial_value()

os_semaphore_count_t os_semaphore_get_initial_value ( os_semaphore_t semaphore)

Get the semaphore initial count value.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
The numeric value set from attributes.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::initial_value()

Definition at line 2254 of file os-c-wrapper.cpp.

◆ os_semaphore_get_max_value()

os_semaphore_count_t os_semaphore_get_max_value ( os_semaphore_t semaphore)

Get the semaphore maximum count value.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
The numeric value set from attributes.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::max_value()

Definition at line 2267 of file os-c-wrapper.cpp.

◆ os_semaphore_get_name()

const char * os_semaphore_get_name ( os_semaphore_t semaphore)

Get the semaphore name.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
Null terminated string.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::name()

Definition at line 2162 of file os-c-wrapper.cpp.

◆ os_semaphore_get_value()

os_semaphore_count_t os_semaphore_get_value ( os_semaphore_t semaphore)

Get the semaphore count value.

Parameters
[in]semaphorePointer to semaphore object instance.
Returns
The semaphore count value.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::value()

Definition at line 2228 of file os-c-wrapper.cpp.

◆ os_semaphore_new()

os_semaphore_t * os_semaphore_new ( const char *  name,
const os_semaphore_attr_t attr 
)

Allocated a semaphore object instance and construct it.

Parameters
[in]namePointer to name (may be NULL).
[in]attrPointer to attributes (may be NULL).
Returns
Pointer to new semaphore object instance.

Dynamically allocate the semaphore object instance using the RTOS system allocator and construct it.

Note
Equivalent of C++ new semaphore(...).
Must be paired with os_semaphore_delete().
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore

Definition at line 2081 of file os-c-wrapper.cpp.

◆ os_semaphore_post()

os_result_t os_semaphore_post ( os_semaphore_t semaphore)

Post (unlock) the semaphore.

Parameters
[in]semaphorePointer to semaphore object instance.
Return values
os_okThe semaphore was posted.
EAGAINThe maximum count value was exceeded.
ENOTRECOVERABLEThe semaphore could not be posted (extension to POSIX).
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::post()

Definition at line 2175 of file os-c-wrapper.cpp.

◆ os_semaphore_reset()

os_result_t os_semaphore_reset ( os_semaphore_t semaphore)

Reset the semaphore.

Parameters
[in]semaphorePointer to semaphore object instance.
Return values
os_okThe semaphore was reset.
EPERMCannot be invoked from an Interrupt Service Routines.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::reset()

Definition at line 2241 of file os-c-wrapper.cpp.

◆ os_semaphore_timed_wait()

os_result_t os_semaphore_timed_wait ( os_semaphore_t semaphore,
os_clock_duration_t  timeout 
)

Timed wait to lock the semaphore.

Parameters
[in]semaphorePointer to semaphore object instance.
[in]timeoutTimeout to wait.
Return values
os_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.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::timed_wait()

Definition at line 2214 of file os-c-wrapper.cpp.

◆ os_semaphore_try_wait()

os_result_t os_semaphore_try_wait ( os_semaphore_t semaphore)

Try to lock the semaphore.

Parameters
[in]semaphorePointer to semaphore object instance.
Return values
os_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.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::try_wait()

Definition at line 2201 of file os-c-wrapper.cpp.

◆ os_semaphore_wait()

os_result_t os_semaphore_wait ( os_semaphore_t semaphore)

Lock the semaphore, possibly waiting.

Parameters
[in]semaphorePointer to semaphore object instance.
Return values
os_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.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::semaphore::wait()

Definition at line 2188 of file os-c-wrapper.cpp.