CMSIS-RTOS RTX: Thread Configuration

| CMSIS-RTOS RTX Version 4.80 CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices

- Main Page
- Usage and Description
- Reference

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Thread Configuration

The CMSIS-RTOS RTX provides several parameters for the thread configuration.

- Configuration of Thread count and Stack Space
- Stack Overflow Checking
- Processor Mode for Thread Execution

# Configuration of Thread count and Stack Space

osThreadDef defines a thread function. The parameter stacksz specifies thereby the stack requirements of this thread function. CMSIS-RTOS RTX defines two methods for defining the stack requirements:

- when stacksz is 0, a fixed-size memory pool is used to for the thread stack. In this case OS_STKSIZE specifies the stack size for the thread function.
- when stacksz is not 0, the thread stack is allocated from a user space. The size of this user space is specified with OS_PRIVSTKSIZE.

Name | #define | Description
--- | --- | ---
Number of concurrent running user threads | `OS_TASKCNT` | Indicates the maximum number of threads that will run at the same time (including main).
Default Thread stack size [bytes] | `OS_STKSIZE` | Specifies the default stack size (in words) for threads that are defined with osThreadDef stacksz = 0.
Main Thread stack size [bytes] | `OS_MAINSTKSIZE` | Is the stack requirement (in words) for the main function that is started by default as an RTOS thread.
Number of threads with user-provided stack size | `OS_PRIVCNT` | Indicates the number of threads that are defined with osThreadDef stacksz != 0 (excluding main). stacksz specifies the stack size requirement of that thread.
Total stack size [bytes] for threads with user-provided stack size | `OS_PRIVSTKSIZE` | Is the combined stack requirement (in words) of all threads that are defined with with osThreadDef stacksz != 0 (excluding main).
Stack overflow checking | `OS_STKCHECK` | If a stack overflow is detected at a thread switch, the function os_error with error code = 1 is called. By default, this function is implemented as endless loop and will practically stop code execution.
Stack usage watermark | `OS_STKINIT` | Initializes the thread stack with a watermark pattern that can be used to detect stack overflows within each thread.
Processor mode for thread execution | `OS_RUNPRIV` | Control the processor mode (privileged/unprivileged)

# Stack Overflow Checking

CMSIS-RTOS RTX implements a software stack overflow checking that traps stack overruns. Stack is used for return addresses and automatic variables and extensive usage or incorrect stack configuration may cause a stack overflow. Software stack overflow checking is controlled with the #define OS_STKCHECK.

If a stack overflow is detected, the function os_error with error code = 1 is called. By default, this function is implemented as endless loop and will practically stop code execution.

# Stack Usage Watermark

CMSIS-RTOS RTX initializes thread stack with watermark pattern (0xCC) when thread is created. This allows the debugger to determine maximum stack usage for each thread. It is typically used during development but removed from the final application. Stack usage watermark is controlled with the #define OS_STKINIT.

Enabling this option increases significantly the execution time of osThreadCreate (depends on thread stack size).

# Processor Mode for Thread Execution

CMSIS-RTOS RTX allows to execute threads in unprivileged or privileged processor mode. The processor mode is controlled with the #define OS_RUNPRIV.

In unprivileged processor mode, the software:

- has limited access to the MSR and MRS instructions, and cannot use the CPS instruction.
- cannot access the system timer, NVIC, or system control block.
- might have restricted access to memory or peripherals.

In privileged processor mode the software can use all the instructions and has access to all resources.

Note
  It is recommended to use the privileged processor mode.