Define, create, and control thread functions. More...
Data Structures | |
struct | osThreadAttr_t |
Attributes structure for thread. More... | |
Macros | |
#define | osErrorId 0xFFFFFFFFU |
osError (-1). | |
#define | osThreadJoinable 0x00000001U |
Thread created in joinable mode. | |
#define | osThreadDetached 0x00000000U |
Thread created in detached mode (default) | |
#define | osThreadUnprivileged 0x00000002U |
Thread runs in unprivileged mode. | |
#define | osThreadPrivileged 0x00000004U |
Thread runs in privileged mode. | |
#define | osThreadZone(n) |
MPU zone value in attribute bit field format. | |
#define | osThreadProcessor(n) (1UL << (n)) |
Thread processor number for SMP systems. | |
Typedefs | |
typedef void(* | osThreadFunc_t) (void *argument) |
Entry point of a thread. | |
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. | |
const char * | osThreadGetName (osThreadId_t thread_id) |
Get name of a thread. | |
uint32_t | osThreadGetClass (osThreadId_t thread_id) |
Get safety class of a thread. | |
uint32_t | osThreadGetZone (osThreadId_t thread_id) |
Get MPU protected zone of a thread. | |
osThreadId_t | osThreadGetId (void) |
Return the thread ID of the current running thread. | |
osThreadState_t | osThreadGetState (osThreadId_t thread_id) |
Get current thread state of a thread. | |
osStatus_t | osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) |
Change priority of a thread. | |
osPriority_t | osThreadGetPriority (osThreadId_t thread_id) |
Get current priority of a thread. | |
osStatus_t | osThreadYield (void) |
Pass control to next thread that is in state READY. | |
osStatus_t | osThreadSuspend (osThreadId_t thread_id) |
Suspend execution of a thread. | |
osStatus_t | osThreadResume (osThreadId_t thread_id) |
Resume execution of a thread. | |
osStatus_t | osThreadDetach (osThreadId_t thread_id) |
Detach a thread (thread storage can be reclaimed when thread terminates). | |
osStatus_t | osThreadJoin (osThreadId_t thread_id) |
Wait for specified thread to terminate. | |
__NO_RETURN void | osThreadExit (void) |
Terminate execution of current running thread. | |
osStatus_t | osThreadTerminate (osThreadId_t thread_id) |
Terminate execution of a thread. | |
uint32_t | osThreadGetStackSize (osThreadId_t thread_id) |
Get stack size of a thread. | |
uint32_t | osThreadGetStackSpace (osThreadId_t thread_id) |
Get available stack space of a thread based on stack watermark recording during execution. | |
uint32_t | osThreadGetCount (void) |
Get number of active threads. | |
uint32_t | osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) |
Enumerate active threads. | |
osStatus_t | osThreadFeedWatchdog (uint32_t ticks) |
Feed watchdog of the current running thread. | |
osStatus_t | osThreadProtectPrivileged (void) |
Protect creation of privileged threads. | |
osStatus_t | osThreadSuspendClass (uint32_t safety_class, uint32_t mode) |
Suspend execution of threads for specified safety classes. | |
osStatus_t | osThreadResumeClass (uint32_t safety_class, uint32_t mode) |
Resume execution of threads for specified safety classes. | |
osStatus_t | osThreadTerminateZone (uint32_t zone) |
Terminate execution of threads assigned to a specified MPU protected zone. | |
osStatus_t | osThreadSetAffinityMask (osThreadId_t thread_id, uint32_t affinity_mask) |
Set processor affinity mask of a thread. | |
uint32_t | osThreadGetAffinityMask (osThreadId_t thread_id) |
Get current processor affinity mask of a thread. | |
uint32_t | osWatchdogAlarm_Handler (osThreadId_t thread_id) |
Handler for expired thread watchdogs. | |
void | osZoneSetup_Callback (uint32_t zone) |
Setup MPU protected zone (called when zone changes). | |
Define, create, and control thread functions.
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:
When creating user threads with osThreadNew it is possible to specify for each thread whether it shall be executed in privileged or unprivileged mode. For that the thread attributes argument shall have in its osThreadAttr_t::attr_bits flags either osThreadPrivileged or osThreadUnprivileged set respectively. If not set then the default operation mode will be used according to kernel configuration.
For detailed differences between privileged and unprivileged mode, please refer to the User's Guide of the target processor. But typically following differences are specified:
In unprivileged processor mode, the thread :
In privileged processor mode, the application software can use all the instructions and has access to all resources.
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 allocated by the system.
Example 2 - Create thread with stack non-default stack size
Similar to the simple thread all attributes are default. The stack size is requested of size 1024 Bytes with with corresponding value passed as osThreadAttr_t::stack_size to osThreadNew. The memory for the stack is then allocated by the system.
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 kernel is osPriorityNormal. Often you want to run a task with a higher or lower priority. Using the osThreadAttr_t::priority field you can assign 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 |
Attributes structure for thread.
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:
Default: 0 no options set. Safety class and MPU Zone are inherited from running thread. Thread privilege mode is set based on configuration threadConfig_procmode. |
void * | cb_mem |
memory for control block Pointer to a memory for the thread control block object. Refer to Manual User-defined Allocation 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. Required value depends on the underlying kernel implementation. 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 - the memory for the stack is provided by the system based on the configuration of underlying RTOS kernel . |
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 | affinity_mask |
processor affinity mask for binding the thread to a CPU in a SMP system (0 when not used) Use the osThreadProcessor macro to create the mask value. Multiple processors can be specified by OR-ing values. Default: value 0 is RTOS implementation specific and may map to running on processor #0 or on any processor. |
#define osErrorId 0xFFFFFFFFU |
osError (-1).
Error return code from osThreadGetClass and osThreadGetZone.
#define osThreadJoinable 0x00000001U |
Thread created in joinable mode.
Bitmask for a thread that can be joined. Intended for use in attr_bits of osThreadAttr_t type argument for osThreadNew function.
A thread in this state can be joined using osThreadJoin.
#define osThreadDetached 0x00000000U |
Thread created in detached mode (default)
Bitmask for a thread that cannot be joined. Intended for use in attr_bits of osThreadAttr_t type argument for osThreadNew function.
A thread in this state cannot be joined using osThreadJoin.
#define osThreadUnprivileged 0x00000002U |
Thread runs in unprivileged mode.
Bitmask for a thread that runs in unprivileged mode. Intended for use in attr_bits of osThreadAttr_t type argument for osThreadNew function.
In unprivileged processor mode, a thread:
Refer to the target processor User's Guide for details.
#define osThreadPrivileged 0x00000004U |
Thread runs in privileged mode.
Bitmask for a thread that runs in privileged mode. Intended for use in attr_bits of osThreadAttr_t type argument for osThreadNew function.
In privileged processor mode, the application software can use all the instructions and has access to all resources.
#define osThreadZone | ( | n | ) |
MPU zone value in attribute bit field format.
n | MPU Protected Zone value. |
The preprocessor macro osThreadZone constructs attribute bitmask with MPU zone bits set to n.
Code Example:
#define osThreadProcessor | ( | n | ) | (1UL << (n)) |
Thread processor number for SMP systems.
n | processor number, starting with n=0 for processor #0. The number of supported processors depend on the hardware. |
The preprocessor macro osThreadProcessor constructs the value for the osThreadAttr_t::affinity_mask derived from n.
void(* osThreadFunc_t)(void *argument) |
Entry point of a thread.
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 |
Thread state.
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. |
enum osPriority_t |
Priority values.
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 being 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 | ||
) |
Create a thread and add it to Active Threads.
[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 | ) |
Get name of a thread.
[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
uint32_t osThreadGetClass | ( | osThreadId_t | thread_id | ) |
Get safety class of a thread.
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetClass returns safety class assigned to the thread identified by parameter thread_id. In case of an error, it returns osErrorId.
uint32_t osThreadGetZone | ( | osThreadId_t | thread_id | ) |
Get MPU protected zone of a thread.
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetZone returns the MPU Protected Zone value assigned to the thread identified by parameter thread_id. In case of an error, it returns osErrorId.
osThreadId_t osThreadGetId | ( | void | ) |
Return the thread ID of the current running thread.
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 | ) |
Get current thread state of a thread.
[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 | ||
) |
Change priority of a thread.
[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 | ) |
Get current priority of a thread.
[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 | ) |
Pass control to next thread that is in state READY.
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 | ) |
Suspend execution of a thread.
[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 | ) |
Resume execution of a thread.
[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 | ) |
Detach a thread (thread storage can be reclaimed when thread terminates).
[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 | ) |
Wait for specified thread to terminate.
[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 | ) |
Terminate execution of current running thread.
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 | ) |
Terminate execution of a thread.
[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 | ) |
Get stack size of a thread.
[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 | ) |
Get available stack space of a thread based on stack watermark recording during execution.
[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. In case of an error, it returns 0.
uint32_t osThreadGetCount | ( | void | ) |
Get number of active threads.
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 | ||
) |
Enumerate active threads.
[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.
osStatus_t osThreadFeedWatchdog | ( | uint32_t | ticks | ) |
Feed watchdog of the current running thread.
[in] | ticks | interval in kernel ticks until the thread watchdog expires, or 0 to stop the watchdog |
The function osThreadFeedWatchdog restarts watchdog of the current running thread. If the thread watchdog is not fed again within the ticks interval osWatchdogAlarm_Handler will be called.
Possible osStatus_t return values:
Code Example:
osStatus_t osThreadProtectPrivileged | ( | void | ) |
Protect creation of privileged threads.
The function osThreadProtectPrivileged disables creation of new privileged threads. After its successful execution, no new threads with privilege execution mode (osThreadPrivileged attribute) can be created. Kernel shall be in ready state or running when osThreadProtectPrivileged is called.
Possible osStatus_t return values:
Code Example:
osStatus_t osThreadSuspendClass | ( | uint32_t | safety_class, |
uint32_t | mode | ||
) |
Suspend execution of threads for specified safety classes.
[in] | safety_class | safety class. |
[in] | mode | safety mode. |
The function osThreadSuspendClass suspends execution of threads based on safety class assignment. safety_class provides the reference safety class value, while mode is considered as a bitmap that additionally specifies the safety classes to be suspended.
If osSafetyWithSameClass is set in mode than the threads with safety class value equal to safety_class will be suspended.
If osSafetyWithLowerClass is set in mode than the threads with safety class value lower than safety_class will be suspended.
Possible osStatus_t return values:
Code Example:
osStatus_t osThreadResumeClass | ( | uint32_t | safety_class, |
uint32_t | mode | ||
) |
Resume execution of threads for specified safety classes.
[in] | safety_class | safety class. |
[in] | mode | safety mode. |
The function osThreadResumeClass resumes execution of threads based on safety class assignment. safety_class provides the reference safety class value, while mode is considered as a bitmap that additionally specifies the safety classes to be resumed.
If osSafetyWithSameClass is set in mode than the threads with safety class value equal to safety_class will be resumed.
If osSafetyWithLowerClass is set in mode than the threads with safety class value lower than safety_class will be resumed.
Possible osStatus_t return values:
Code Example:
osStatus_t osThreadTerminateZone | ( | uint32_t | zone | ) |
Terminate execution of threads assigned to a specified MPU protected zone.
[in] | zone | MPU protected zone. |
The function osThreadTerminateZone terminates execution of threads assigned to the MPU Protected Zone as given by zone parameter.
Possible osStatus_t return values:
Code Example:
osStatus_t osThreadSetAffinityMask | ( | osThreadId_t | thread_id, |
uint32_t | affinity_mask | ||
) |
Set processor affinity mask of a thread.
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
[in] | affinity_mask | processor affinity mask for the thread. |
The function osThreadSetAffinityMask sets the affinity mask of the thread specified by parameter thread_id to the mask specified by the parameter affinity_mask. The mask indicates on which processor(s) the thread should run (0 indicates on any processor).
Possible osStatus_t return values:
Code Example
uint32_t osThreadGetAffinityMask | ( | osThreadId_t | thread_id | ) |
Get current processor affinity mask of a thread.
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The function osThreadGetAffinityMask returns the affinity mask of the thread specified by parameter thread_id. In case of an error, it returns 0.
Code Example
uint32_t osWatchdogAlarm_Handler | ( | osThreadId_t | thread_id | ) |
Handler for expired thread watchdogs.
[in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId. |
The callback function osWatchdogAlarm_Handler is called by the kernel when a thread watchdog expires. Parameter thread_id identifies the thread which has the expired thread watchdog. The function needs to be implemented in user application.
Return new reload value to restart the watchdog. Return 0 to stop the thread watchdog.
Code Example:
void osZoneSetup_Callback | ( | uint32_t | zone | ) |
Setup MPU protected zone (called when zone changes).
[in] | zone | zone number. |
The callback function osZoneSetup_Callback is called by the kernel when MPU Protected Zone changes. The function shall be implemented in user application.
Code Example:
ZONES_NUM
is the total amount of zones allocated by the application. For ARM_MPU_
... functions refer to MPU Functions in CMSIS-Core documentation.