µ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

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

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
See also
os::rtos::semaphore::count_t

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

◆ os_semaphore_t

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 
)
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 2140 of file os-c-wrapper.cpp.

2142{
2143 assert (attr != nullptr);
2144 new (attr) semaphore::attributes_binary{ initial_value };
2145}
Binary semaphore attributes.

◆ 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 
)
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 2154 of file os-c-wrapper.cpp.

2157{
2158 assert (attr != nullptr);
2159 new (attr) semaphore::attributes_counting{ max_value, initial_value };
2160}
Counting semaphore attributes.

◆ os_semaphore_attr_get_binary()

const os_semaphore_attr_t * os_semaphore_attr_get_binary ( void  )
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 2169 of file os-c-wrapper.cpp.

2170{
2172}
static const attributes_binary initializer_binary
Default binary semaphore initialiser.

References os::rtos::semaphore::initializer_binary.

◆ os_semaphore_attr_init()

void os_semaphore_attr_init ( os_semaphore_attr_t attr)
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 2127 of file os-c-wrapper.cpp.

2128{
2129 assert (attr != nullptr);
2130 new (attr) semaphore::attributes{};
2131}
Semaphore attributes.

◆ os_semaphore_binary_construct()

void os_semaphore_binary_construct ( os_semaphore_t semaphore,
const char *  name,
const os_semaphore_count_t  initial_value 
)
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 2203 of file os-c-wrapper.cpp.

2205{
2206 assert (semaphore != nullptr);
2207 new (semaphore) rtos::semaphore_binary{ name, initial_value };
2208}
POSIX compliant binary semaphore.
POSIX compliant semaphore.

◆ os_semaphore_binary_new()

os_semaphore_t * os_semaphore_binary_new ( const char *  name,
const os_semaphore_count_t  initial_value 
)
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 2281 of file os-c-wrapper.cpp.

2283{
2284 return reinterpret_cast<os_semaphore_t*> (
2285 new rtos::semaphore_binary{ name, initial_value });
2286}

◆ os_semaphore_construct()

void os_semaphore_construct ( os_semaphore_t semaphore,
const char *  name,
const os_semaphore_attr_t attr 
)
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 2183 of file os-c-wrapper.cpp.

2185{
2186 assert (semaphore != nullptr);
2187 if (attr == nullptr)
2188 {
2190 }
2191 new (semaphore) rtos::semaphore{ name, (const semaphore::attributes&)*attr };
2192}

References os::rtos::semaphore::initializer_binary.

◆ 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 
)
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 2219 of file os-c-wrapper.cpp.

2222{
2223 assert (semaphore != nullptr);
2224 new (semaphore) rtos::semaphore_counting{ name, max_value, initial_value };
2225}
POSIX compliant counting semaphore.

◆ 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 
)
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 2302 of file os-c-wrapper.cpp.

2305{
2306 return reinterpret_cast<os_semaphore_t*> (
2307 new rtos::semaphore_counting{ name, max_value, initial_value });
2308}

◆ os_semaphore_delete()

void os_semaphore_delete ( os_semaphore_t semaphore)
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 2325 of file os-c-wrapper.cpp.

2326{
2327 assert (semaphore != nullptr);
2328 delete reinterpret_cast<rtos::semaphore*> (semaphore);
2329}

◆ os_semaphore_destruct()

void os_semaphore_destruct ( os_semaphore_t semaphore)
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 2237 of file os-c-wrapper.cpp.

2238{
2239 assert (semaphore != nullptr);
2240 (reinterpret_cast<rtos::semaphore&> (*semaphore)).~semaphore ();
2241}

◆ os_semaphore_get_initial_value()

os_semaphore_count_t os_semaphore_get_initial_value ( os_semaphore_t semaphore)
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 2437 of file os-c-wrapper.cpp.

2438{
2439 assert (semaphore != nullptr);
2440 return (os_semaphore_count_t)(reinterpret_cast<rtos::semaphore&> (
2441 *semaphore))
2442 .initial_value ();
2443}
int16_t os_semaphore_count_t
Type of variables holding semaphore counts.

◆ os_semaphore_get_max_value()

os_semaphore_count_t os_semaphore_get_max_value ( os_semaphore_t semaphore)
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 2452 of file os-c-wrapper.cpp.

2453{
2454 assert (semaphore != nullptr);
2455 return (os_semaphore_count_t)(reinterpret_cast<rtos::semaphore&> (
2456 *semaphore))
2457 .max_value ();
2458}

◆ os_semaphore_get_name()

const char * os_semaphore_get_name ( os_semaphore_t semaphore)
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 2338 of file os-c-wrapper.cpp.

2339{
2340 assert (semaphore != nullptr);
2341 return (reinterpret_cast<rtos::semaphore&> (*semaphore)).name ();
2342}

◆ os_semaphore_get_value()

os_semaphore_count_t os_semaphore_get_value ( os_semaphore_t semaphore)
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 2408 of file os-c-wrapper.cpp.

2409{
2410 assert (semaphore != nullptr);
2411 return (os_semaphore_count_t)(reinterpret_cast<rtos::semaphore&> (
2412 *semaphore))
2413 .value ();
2414}

◆ os_semaphore_new()

os_semaphore_t * os_semaphore_new ( const char *  name,
const os_semaphore_attr_t attr 
)
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 2257 of file os-c-wrapper.cpp.

2258{
2259 if (attr == nullptr)
2260 {
2262 }
2263 return reinterpret_cast<os_semaphore_t*> (
2264 new rtos::semaphore{ name, (const semaphore::attributes&)*attr });
2265}

References os::rtos::semaphore::initializer_binary.

◆ os_semaphore_post()

os_result_t os_semaphore_post ( os_semaphore_t 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 2351 of file os-c-wrapper.cpp.

2352{
2353 assert (semaphore != nullptr);
2354 return (os_result_t)(reinterpret_cast<rtos::semaphore&> (*semaphore))
2355 .post ();
2356}
uint32_t os_result_t
Type of values returned by RTOS functions.
Definition os-c-decls.h:94

◆ os_semaphore_reset()

os_result_t os_semaphore_reset ( os_semaphore_t 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 2423 of file os-c-wrapper.cpp.

2424{
2425 assert (semaphore != nullptr);
2426 return (os_result_t)(reinterpret_cast<rtos::semaphore&> (*semaphore))
2427 .reset ();
2428}

◆ os_semaphore_timed_wait()

os_result_t os_semaphore_timed_wait ( os_semaphore_t semaphore,
os_clock_duration_t  timeout 
)
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 2393 of file os-c-wrapper.cpp.

2395{
2396 assert (semaphore != nullptr);
2397 return (os_result_t)(reinterpret_cast<rtos::semaphore&> (*semaphore))
2398 .timed_wait (timeout);
2399}

◆ os_semaphore_try_wait()

os_result_t os_semaphore_try_wait ( os_semaphore_t 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 2379 of file os-c-wrapper.cpp.

2380{
2381 assert (semaphore != nullptr);
2382 return (os_result_t)(reinterpret_cast<rtos::semaphore&> (*semaphore))
2383 .try_wait ();
2384}

◆ os_semaphore_wait()

os_result_t os_semaphore_wait ( os_semaphore_t 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.
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 2365 of file os-c-wrapper.cpp.

2366{
2367 assert (semaphore != nullptr);
2368 return (os_result_t)(reinterpret_cast<rtos::semaphore&> (*semaphore))
2369 .wait ();
2370}
pid_t wait(int *stat_loc)

References wait().