CMSIS-RTOS: Thread Configuration

| CMSIS-RTOS Version 1.03 Real-Time Operating System: API and RTX Reference Implementation.

- 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.

The CMSIS-RTOS RTX kernel uses a separate stack for each thread it creates. However, before the kernel is started by the osKernelInitialize() function, the main stack size that is configured in the file startup_device.s is used.

Main stack is also used when:

- the user application calls the majority of RTX functions from Thread mode (ending up in an SVC call)
- running from handlers (user interrupt of exception handlers like SVCm PendSV, Faults, etc.)

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 and osTimerThread (if OS_TIMERS set to 1)).
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 determine the maximum stack usage within each thread.
Processor Mode for Thread Execution | `OS_RUNPRIV` | Controls 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

The total stack size of an application needs to be as small as possible in a memory restricted embedded system. To be able to set the smallest stack size for every thread, the developer needs to know the maximum stack usage over the runtime of the application.

The Stack Usage Watermark feature support this by initializing the thread stack with a watermark pattern (0xCC) when a thread is created. This allows the debugger to determine the maximum stack usage for each thread.

System and Thread Viewer showing current and maximum stack usage

Stack usage watermark is controlled with the #define OS_STKINIT. Setting this `#define` increases significantly the execution time of osThreadCreate (depending 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.