CMSIS-RTOS2
Version 2.1.3
Real-Time Operating System: API and RTX Reference Implementation
|
Define, create, and control thread functions. More...
Data Structures | |
struct | osThreadAttr_t |
Attributes structure for thread. More... | |
Macros | |
#define | osThreadJoinable 0x00000001U |
Thread created in joinable mode. More... | |
#define | osThreadDetached 0x00000000U |
Thread created in detached mode (default) More... | |
Typedefs | |
typedef void(* | osThreadFunc_t )(void *argument) |
Entry point of a thread. More... | |
typedef void * | osThreadId_t |
Functions | |
osThreadId_t | osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) |
Create a thread and add it to Active Threads. More... | |
const char * | osThreadGetName (osThreadId_t thread_id) |
Get name of a thread. More... | |
osThreadId_t | osThreadGetId (void) |
Return the thread ID of the current running thread. More... | |
osThreadState_t | osThreadGetState (osThreadId_t thread_id) |
Get current thread state of a thread. More... | |
osStatus_t | osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) |
Change priority of a thread. More... | |
osPriority_t | osThreadGetPriority (osThreadId_t thread_id) |
Get current priority of a thread. More... | |
osStatus_t | osThreadYield (void) |
Pass control to next thread that is in state READY. More... | |
osStatus_t | osThreadSuspend (osThreadId_t thread_id) |
Suspend execution of a thread. More... | |
osStatus_t | osThreadResume (osThreadId_t thread_id) |
Resume execution of a thread. More... | |
osStatus_t | osThreadDetach (osThreadId_t thread_id) |
Detach a thread (thread storage can be reclaimed when thread terminates). More... | |
osStatus_t | osThreadJoin (osThreadId_t thread_id) |
Wait for specified thread to terminate. More... | |
__NO_RETURN void | osThreadExit (void) |
Terminate execution of current running thread. More... | |
osStatus_t | osThreadTerminate (osThreadId_t thread_id) |
Terminate execution of a thread. More... | |
uint32_t | osThreadGetStackSize (osThreadId_t thread_id) |
Get stack size of a thread. More... | |
uint32_t | osThreadGetStackSpace (osThreadId_t thread_id) |
Get available stack space of a thread based on stack watermark recording during execution. More... | |
uint32_t | osThreadGetCount (void) |
Get number of active threads. More... | |
uint32_t | osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) |
Enumerate active threads. More... | |
The Thread Management function group allows defining, creating, and controlling thread functions in the system.
Threads can be in the following states:
A CMSIS-RTOS assumes that threads are scheduled as shown in the figure Thread State and State Transitions. The thread states change as follows:
The following examples show various scenarios to create threads:
Example 1 - Create a simple thread
Create a thread out of the function thread1 using all default values for thread attributes and memory from the Global Memory Pool.
Example 2 - Create thread with stack non-default stack size
Similar to the simple thread all attributes are default. The stack is dynamically allocated from the Global Memory Pool
osThreadAttr_t::stack_size is used to pass the stack size in Bytes to osThreadNew.
Example 3 - Create thread with statically allocated stack
Similar to the simple thread all attributes are default. The stack is statically allocated using the uint64_t
array thread1_stk_1
. This allocates 64*8 Bytes (=512 Bytes) with an alignment of 8 Bytes (mandatory for Cortex-M stack memory).
osThreadAttr_t::stack_mem holds a pointer to the stacks lowest address.
osThreadAttr_t::stack_size is used to pass the stack size in Bytes to osThreadNew.
Example 4 - Thread with statically allocated task control block
Typically this method is chosen together with a statically allocated stack as shown in Example 2.
Example 5 - Create thread with a different priority
The default priority of RTX is osPriorityNormal. Often you want to run a task with a higher or lower priority. Using the osThreadAttr_t control structure you can set any initial priority required.
In this example a master thread creates four threads with the osThreadJoinable attribute. These will do some work and return using the osThreadExit call after finished. osThreadJoin is used to synchronize the thread termination.
struct osThreadAttr_t |
Specifies the following attributes for the osThreadNew function.
Data Fields | ||
---|---|---|
const char * | name |
name of the thread Pointer to a constant string with a human readable name (displayed during debugging) of the thread object. Default: NULL no name specified (debugger may display function name instead). |
uint32_t | attr_bits |
attribute bits The following bit masks can be used to set options:
|
void * | cb_mem |
memory for control block Pointer to a memory for the thread control block object. Refer to Static Object Memory for more information. Default: NULL to use Automatic Dynamic Allocation for the thread control block. |
uint32_t | cb_size |
size of provided memory for control block The size (in bytes) of memory block passed with cb_mem. For RTX, the minimum value is defined with osRtxThreadCbSize (higher values are permitted). Default: 0 as the default is no memory provided with cb_mem. |
void * | stack_mem |
memory for stack Pointer to a memory location for the thread stack (64-bit aligned). Default: NULL to allocate stack from a fixed-size memory pool using Thread Stack Management. |
uint32_t | stack_size |
size of stack The size (in bytes) of the stack specified by stack_mem. Default: 0 as the default is no memory provided with stack_mem. /** |
osPriority_t | priority |
initial thread priority (default: osPriorityNormal) Specifies the initial thread priority with a value from osPriority_t. Default: osPriorityNormal. |
TZ_ModuleId_t | tz_module |
TrustZone module identifier. TrustZone Thread Context Management Identifier to allocate context memory for threads. The RTOS kernel that runs in non-secure state calls the interface functions defined by the header file TZ_context.h. Can safely be set to zero for threads not using secure calls at all. See TrustZone RTOS Context Management. Default: 0 not thread context specified. /** |
uint32_t | reserved |
reserved (must be 0) Reserved for future use. |
#define osThreadJoinable 0x00000001U |
A thread in this state can be joined using osThreadJoin.
#define osThreadDetached 0x00000000U |
A thread in this state cannot be joined using osThreadJoin.
void(* osThreadFunc_t)(void *argument) |
Entry function for threads. Setting up a new thread (osThreadNew) will start execution with a call into this entry function. The optional argument can be used to hand over arbitrary user data to the thread, i.e. to identify the thread or for runtime parameters.
[in] | argument | Arbitrary user data set on osThreadNew. |
Thread ID identifies the thread.
Returned by:
enum osThreadState_t |
State of a thread as retrieved by osThreadGetState. In case osThreadGetState fails or if it is called from an ISR, it will return osThreadError
, otherwise it returns the thread state.
Enumerator | |
---|---|
osThreadInactive |
Inactive. The thread is created but not actively used, or has been terminated (returned for static control block allocation, when memory pools are used osThreadError is returned as the control block is no longer valid) |
osThreadReady |
Ready. The thread is ready for execution but not currently running. |
osThreadRunning |
Running. The thread is currently running. |
osThreadBlocked |
Blocked. The thread is currently blocked (delayed, waiting for an event or suspended). |
osThreadTerminated |
Terminated. The thread is terminated and all its resources are not yet freed (applies to joinable threads). |
osThreadError |
Error. The thread does not exist (has raised an error condition) and cannot be scheduled. |
osThreadReserved |
Prevents enum down-size compiler optimization. |
enum osPriority_t |
The osPriority_t value specifies the priority for a thread. The default thread priority should be osPriorityNormal. If an active thread becomes ready that has a higher priority than the currently running thread then a thread switch occurs immediately. The system continues executing the thread with the higher priority.
To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a priority inheritance method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a thread with a lower priority. Thus causing the high priority thread potentially beeing blocked forever by another thread with lower priority. To come over this issue the low priority thread controlling the resource should be treated as having the higher priority until it releases the resource.
osThreadId_t osThreadNew | ( | osThreadFunc_t | func, |
void * | argument, | ||
const osThreadAttr_t * | attr | ||
) |
[in] | func | thread function. |
[in] | argument | pointer that is passed to the thread function as start argument. |
[in] | attr | thread attributes; NULL: default values. |
The function osThreadNew starts a thread function by adding it to the list of active threads and sets it to state READY. Arguments for the thread function are passed using the parameter pointer *argument. When the priority of the created thread function is higher than the current RUNNING thread, the created thread function starts instantly and becomes the new RUNNING thread. Thread attributes are defined with the parameter pointer attr. Attributes include settings for thread priority, stack size, or memory allocation.
The function can be safely called before the RTOS is started (call to osKernelStart), but not before it is initialized (call to osKernelInitialize).
The function osThreadNew returns the pointer to the thread object identifier or NULL in case of an error.
Code Example
Refer to the Thread Examples section.
const char * osThreadGetName | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetName returns the pointer to the name string of the thread identified by parameter thread_id or NULL in case of an error.
Code Example
osThreadId_t osThreadGetId | ( | void | ) |
The function osThreadGetId returns the thread object ID of the currently running thread or NULL in case of an error.
Code Example
osThreadState_t osThreadGetState | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetState returns the state of the thread identified by parameter thread_id. In case it fails or if it is called from an ISR, it will return osThreadError
, otherwise it returns the thread state (refer to osThreadState_t for the list of thread states).
osStatus_t osThreadSetPriority | ( | osThreadId_t | thread_id, |
osPriority_t | priority | ||
) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
[in] | priority | new priority value for the thread function. |
The function osThreadSetPriority changes the priority of an active thread specified by the parameter thread_id to the priority specified by the parameter priority.
Possible osStatus_t return values:
Code Example
osPriority_t osThreadGetPriority | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetPriority returns the priority of an active thread specified by the parameter thread_id.
Possible osPriority_t return values:
Code Example
osStatus_t osThreadYield | ( | void | ) |
The function osThreadYield passes control to the next thread with the same priority that is in the READY state. If there is no other thread with the same priority in state READY, then the current thread continues execution and no thread switch occurs. osThreadYield does not set the thread to state BLOCKED. Thus no thread with a lower priority will be scheduled even if threads in state READY are available.
Possible osStatus_t return values:
Code Example
osStatus_t osThreadSuspend | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadSuspend suspends the execution of the thread identified by parameter thread_id. The thread is put into the BLOCKED state (osThreadBlocked). Suspending the running thread will cause a context switch to another thread in READY state immediately. The suspended thread is not executed until explicitly resumed with the function osThreadResume.
Threads that are already BLOCKED are removed from any wait list and become ready when they are resumed. Thus it is not recommended to suspend an already blocked thread.
Possible osStatus_t return values:
osStatus_t osThreadResume | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadResume puts the thread identified by parameter thread_id (which has to be in BLOCKED state) back to the READY state. If the resumed thread has a higher priority than the running thread a context switch occurs immediately.
The thread becomes ready regardless of the reason why the thread was blocked. Thus it is not recommended to resume a thread not suspended by osThreadSuspend.
Functions that will put a thread into BLOCKED state are: osEventFlagsWait and osThreadFlagsWait, osDelay and osDelayUntil, osMutexAcquire and osSemaphoreAcquire, osMessageQueueGet, osMemoryPoolAlloc, osThreadJoin, osThreadSuspend.
Possible osStatus_t return values:
osStatus_t osThreadDetach | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadDetach changes the attribute of a thread (specified by thread_id) to osThreadDetached. Detached threads are not joinable with osThreadJoin. When a detached thread is terminated, all resources are returned to the system. The behavior of osThreadDetach on an already detached thread is undefined.
Possible osStatus_t return values:
osStatus_t osThreadJoin | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadJoin waits for the thread specified by thread_id to terminate. If that thread has already terminated, then osThreadJoin returns immediately. The thread must be joinable. By default threads are created with the attribute osThreadDetached.
Possible osStatus_t return values:
__NO_RETURN void osThreadExit | ( | void | ) |
The function osThreadExit terminates the calling thread. This allows the thread to be synchronized with osThreadJoin.
Code Example
osStatus_t osThreadTerminate | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadTerminate removes the thread specified by parameter thread_id from the list of active threads. If the thread is currently RUNNING, the thread terminates and the execution continues with the next READY thread. If no such thread exists, the function will not terminate the running thread, but return osErrorResource.
Possible osStatus_t return values:
Code Example
uint32_t osThreadGetStackSize | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetStackSize returns the stack size of the thread specified by parameter thread_id. In case of an error, it returns 0.
uint32_t osThreadGetStackSpace | ( | osThreadId_t | thread_id | ) |
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetStackSpace returns the size of unused stack space for the thread specified by parameter thread_id. Stack watermark recording during execution needs to be enabled (refer to Thread Configuration). In case of an error, it returns 0.
uint32_t osThreadGetCount | ( | void | ) |
The function osThreadGetCount returns the number of active threads or 0 in case of an error.
uint32_t osThreadEnumerate | ( | osThreadId_t * | thread_array, |
uint32_t | array_items | ||
) |
[out] | thread_array | pointer to array for retrieving thread IDs. |
[in] | array_items | maximum number of items in array for retrieving thread IDs. |
The function osThreadEnumerate returns the number of enumerated threads or 0 in case of an error.