| CMSIS-RTOS Version 1.00 CMSIS-RTOS API: Generic RTOS interface for Cortex-M processor-based devices.

- CMSIS
- CORE
- DSP
- RTOS API
- SVD

- Main Page
- Usage and Description
- Reference

Defines | Enumerations | Functions
Thread Management
CMSIS-RTOS API

Define, create, and control thread functions. More...

## Defines
#define | osThreadDef(name, priority, instances, stacksz)
| Create a Thread Definition with function, priority, and stack requirements.
#define | osThread(name) &os_thread_def_##name
| Access a Thread defintion.
## Enumerations
enum | osPriority { osPriorityIdle = -3, osPriorityLow = -2, osPriorityBelowNormal = -1, osPriorityNormal = 0, osPriorityAboveNormal = +1, osPriorityHigh = +2, osPriorityRealtime = +3, osPriorityError = 0x84 }
## Functions
osThreadId | osThreadCreate (osThreadDef_t *thread_def, void *argument)
| Create a thread and add it to Active Threads and set it to state READY.
osThreadId | osThreadGetId (void)
| Return the thread ID of the current running thread.
osStatus | osThreadTerminate (osThreadId thread_id)
| Terminate execution of a thread and remove it from Active Threads.
osStatus | osThreadSetPriority (osThreadId thread_id, osPriority priority)
| Change priority of an active thread.
osPriority | osThreadGetPriority (osThreadId thread_id)
| Get current priority of an active thread.
osStatus | osThreadYield (void)
| Pass control to next thread that is in state READY.

## Description

The Thread Management function group allow defining, creating, and controlling thread functions in the system. The function main is a special thread function that is started at system initialization and has the initial priority osPriorityNormal.

Threads can be in the following states:

- RUNNING: The thread that is currently running is in the RUNNING state. Only one thread at a time can be in this state.
- READY: Threads which are ready to run are in the READY state. Once the RUNNING thread has terminated or is WAITING the next READY thread with the highest priority becomes the RUNNING thread.
- WAITING: Threads that are waiting for an event to occur are in the WAITING state.
- INACTIVE: Threads that are not created or terminated are in the INACTIVE state. These threads typically consume no system resources.

Thread State and State Transitions

The CMSIS-RTOS assumes that threads are scheduled as shown in the figure Thread State and State Transitions. The thread states change as described below:

- A thread is created using the function osThreadCreate. This puts the thread into the READY or RUNNING state (depending on the thread priority).
- CMSIS-RTOS is pre-emptive. The active thread with the highest priority becomes the RUNNING thread provided it does not wait for any event. The initial priority of a thread is defined with the osThreadDef but may be changed during execution using the function osThreadSetPriority.
- The RUNNING thread transfers into the WAITING state when it is waiting for an event.
- Active threads can be terminated any time using the function osThreadTerminate. Threads can terminate also by just returning from the thread function. Threads that are terminated are in the INACTIVE state and typically do not consume any dynamic memory resources.

## Define Documentation

#define osThread | ( |  | name | ) | &os_thread_def_##name

Access to the thread definition for the function osThreadCreate.

Parameters:

  name | name of the thread definition object.

Note:
  CAN BE CHANGED: The parameter to osThread shall be consistent but the macro body is implementation specific in every CMSIS-RTOS.

#define osThreadDef | ( |  | name,
|  |  | priority,
|  |  | instances,
|  |  | stacksz
| ) |  |

Define the attributes of a thread functions that can be created by the function osThreadCreate using osThread.

Parameters:

  name | name of the thread function.
  priority | initial priority of the thread function.
  instances | number of possible thread instances.
  stacksz | stack size (in bytes) requirements for the thread function.

Note:
  CAN BE CHANGED: The parameters to osThreadDef shall be consistent but the macro body is implementation specific in every CMSIS-RTOS.

## Enumeration Type Documentation

enum osPriority

Note:
  MUST REMAIN UNCHANGED: osPriority shall be consistent in every CMSIS-RTOS.

The osPriority value specifies the priority for a thread. The default thread priority should be osPriorityNormal. If a Thread is active that has a higher priority than the currently executing thread, then a thread switch occurs immediately to execute the new task.

To prevent from a priority inversion, a CMSIS-RTOS complained 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.

Enumerator:

  osPriorityIdle | priority: idle (lowest)
  osPriorityLow | priority: low
  osPriorityBelowNormal | priority: below normal
  osPriorityNormal | priority: normal (default)
  osPriorityAboveNormal | priority: above normal
  osPriorityHigh | priority: high
  osPriorityRealtime | priority: realtime (highest)
  osPriorityError | system cannot determine priority or thread has illegal priority

## Function Documentation

osThreadId osThreadCreate | ( | osThreadDef_t * | thread_def,
|  | void * | argument
| ) |  |

Parameters:

  [in] | thread_def | thread definition referenced with osThread.
  [in] | argument | pointer that is passed to the thread function as start argument.

Returns:
  thread ID for reference by other functions or NULL in case of error.

Note:
  MUST REMAIN UNCHANGED: osThreadCreate shall be consistent in every CMSIS-RTOS.

Start a thread function by adding it to the Active Threads list and set it to state READY. The thread function receives the argument pointer as function argument when the function is started. 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.

osThreadId osThreadGetId | ( | void |  | ) |

Returns:
  thread ID for reference by other functions or NULL in case of error.

Note:
  MUST REMAIN UNCHANGED: osThreadGetId shall be consistent in every CMSIS-RTOS.

Get the thread ID of the current running thread.

osPriority osThreadGetPriority | ( | osThreadId | thread_id | ) |

Parameters:

  [in] | thread_id | thread ID obtained by osThreadCreate or osThreadGetId.

Returns:
  current priority value of the thread function.

Note:
  MUST REMAIN UNCHANGED: osThreadGetPriority shall be consistent in every CMSIS-RTOS.

Get the priority of an active thread. In case of a failure the value osPriorityError is returned.

osStatus osThreadSetPriority | ( | osThreadId | thread_id,
|  | osPriority | priority
| ) |  |

Parameters:

  [in] | thread_id | thread ID obtained by osThreadCreate or osThreadGetId.
  [in] | priority | new priority value for the thread function.

Returns:
  status code that indicates the execution status of the function.

Note:
  MUST REMAIN UNCHANGED: osThreadSetPriority shall be consistent in every CMSIS-RTOS.

Change the priority of an active thread.

Status and Error Codes

- osOK: the prioirty of the specified thread has been successfully changed.
- osErrorParameter: thread_id is incorrect.
- osErrorValue: incorrect priority value.
- osErrorResource: thread_id refers to a thread that is not an active thread.
- osErrorISR: osThreadSetPriority cannot be called from interrupt service routines.

osStatus osThreadTerminate | ( | osThreadId | thread_id | ) |

Parameters:

  [in] | thread_id | thread ID obtained by osThreadCreate or osThreadGetId.

Returns:
  status code that indicates the execution status of the function.

Note:
  MUST REMAIN UNCHANGED: osThreadTerminate shall be consistent in every CMSIS-RTOS.

Remove the thread function from the active thread list. If the thread is currently RUNNING the execution will stop.

Note:
  In case that osThreadTerminate terminates the currently running task, the function never returns and other threads that are in the READY state are started.

Status and Error Codes

- osOK: the specified thread has been successfully terminated.
- osErrorParameter: thread_id is incorrect.
- osErrorResource: thread_id refers to a thread that is not an active thread.
- osErrorISR: osThreadTerminate cannot be called from interrupt service routines.

osStatus osThreadYield | ( | void |  | ) |

Returns:
  status code that indicates the execution status of the function.

Note:
  MUST REMAIN UNCHANGED: osThreadYield shall be consistent in every CMSIS-RTOS.

Pass control to next thread that is in state READY. If there is no other thread in the state READY, the current thread continues execution and no thread switching occurs.

Status and Error Codes

- osOK: the function has been correctly executed.
- osErrorISR: osThreadYield cannot be called from interrupt service routines.