CMSIS-RTOS
Version 1.03
Real-Time Operating System: API and RTX Reference Implementation.
|
Synchronize resource access using Mutual Exclusion (Mutex). More...
Macros | |
#define | osMutexDef(name) const osMutexDef_t os_mutex_def_##name = { 0 } |
Define a Mutex. More... | |
#define | osMutex(name) &os_mutex_def_##name |
Access a Mutex definition. More... | |
Functions | |
osMutexId | osMutexCreate (const osMutexDef_t *mutex_def) |
Create and Initialize a Mutex object. More... | |
osStatus | osMutexWait (osMutexId mutex_id, uint32_t millisec) |
Wait until a Mutex becomes available. More... | |
osStatus | osMutexRelease (osMutexId mutex_id) |
Release a Mutex that was obtained by osMutexWait. More... | |
osStatus | osMutexDelete (osMutexId mutex_id) |
Delete a Mutex that was created by osMutexCreate. More... | |
Mutual exclusion (widely known as Mutex) is used in various operating systems for resource management. Many resources in a microcontroller device can be used repeatedly, but only by one thread at a time (for example communication channels, memory, and files). Mutexes are used to protect access to a shared resource. A mutex is created and then passed between the threads (they can acquire and release the mutex).
A mutex is a special version of a semaphore. Like the semaphore, it is a container for tokens. But instead of being able to have multiple tokens, a mutex can only carry one (representing the resource). Thus, a mutex token is binary and bounded. The advantage of a mutex is that it introduces thread ownership. When a thread acquires a mutex and becomes its owner, subsequent mutex acquires from that thread will succeed immediately without any latency. Thus, mutex acquires/releases can be nested.
To use mutexes, you need to follow these steps for creating and using them:
#define osMutex | ( | name | ) | &os_mutex_def_##name |
Access to mutex object for the functions osMutexCreate.
name | name of the mutex object. |
#define osMutexDef | ( | name | ) | const osMutexDef_t os_mutex_def_##name = { 0 } |
Define a mutex object that is referenced by osMutex.
name | name of the mutex object. |
osMutexId osMutexCreate | ( | const osMutexDef_t * | mutex_def | ) |
[in] | mutex_def | mutex definition referenced with osMutex. |
Create and initialize a Mutex object.
Code Example
[in] | mutex_id | mutex ID obtained by osMutexCreate. |
Delete a Mutex object. The function releases internal memory obtained for Mutex handling. After this call the mutex_id is no longer valid and cannot be used. The Mutex may be created again using the function osMutexCreate.
Code Example
[in] | mutex_id | mutex ID obtained by osMutexCreate. |
Release a Mutex that was obtained with osMutexWait. Other threads that currently wait for the same mutex will be now put into the state READY.
Code Example
[in] | mutex_id | mutex ID obtained by osMutexCreate. |
[in] | millisec | Timout Value or 0 in case of no time-out. |
Wait until a Mutex becomes available. If no other thread has obtained the Mutex, the function instantly returns and blocks the mutex object.
The argument millisec specifies how long the system waits for a mutex. While the system waits the thread that is calling this function is put into the state WAITING. The millisec timeout can have the following values:
Code Example