CMSIS-RTOS2  Version 2.1.0
Real-Time Operating System: API and RTX Reference Implementation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Thread Flags

Synchronize threads using flags. More...

Functions

uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags)
 Set the specified Thread Flags of a thread. More...
 
uint32_t osThreadFlagsClear (uint32_t flags)
 Clear the specified Thread Flags of current running thread. More...
 
uint32_t osThreadFlagsGet (void)
 Get the current Thread Flags of current running thread. More...
 
uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout)
 Wait for one or more Thread Flags of the current running thread to become signaled. More...
 

Description

Thread Flags are a more specialized version of the Event Flags. See Event Flags. While Event Flags can be used to globally signal a number of threads, thread flags are only send to a single specific thread. Every thread instance can receive thread flags without any additional allocation of a thread flags object.

Note
Thread flag management functions cannot be called from Interrupt Service Routines, except for osThreadFlagsSet.

Function Documentation

uint32_t osThreadFlagsSet ( osThreadId_t  thread_id,
uint32_t  flags 
)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
[in]flagsspecifies the flags of the thread that shall be set.
Returns
thread flags after setting or error code if highest bit set.

The function osThreadFlagsSet sets the thread flags for a thread specified by parameter thread_id. It returns the flags set, or an error code if highest bit is set (refer to Flags Functions Error Codes). This function may be used also within interrupt service routines. Threads waiting for a flag to be set will resume from BLOCKED state.

Note
This function may be called from Interrupt Service Routines.
uint32_t osThreadFlagsClear ( uint32_t  flags)
Parameters
[in]flagsspecifies the flags of the thread that shall be cleared.
Returns
thread flags before clearing or error code if highest bit set.

The function osThreadFlagsClear clears the specified flags for the currently running thread. It returns the flags before clearing, or an error code if highest bit is set (refer to Flags Functions Error Codes).

Note
This function cannot be called from Interrupt Service Routines.
uint32_t osThreadFlagsGet ( void  )
Returns
current thread flags.

The function osThreadFlagsGet returns the flags currently set for the currently running thread.

Note
This function cannot be called from Interrupt Service Routines.
uint32_t osThreadFlagsWait ( uint32_t  flags,
uint32_t  options,
uint32_t  timeout 
)
Parameters
[in]flagsspecifies the flags to wait for.
[in]optionsspecifies flags options (osFlagsXxxx).
[in]timeoutTimeout Value or 0 in case of no time-out.
Returns
thread flags before clearing or error code if highest bit set.

The function osThreadFlagsWait suspends the execution of the currently RUNNING thread until any or all of the thread flags specified with the parameter flags are set. When these thread flags are already set, the function returns instantly. Otherwise the thread is put into the state BLOCKED. The function then returns flags before clearing, or an error code if highest bit is set (refer to Flags Functions Error Codes).

The parameter options specifies the wait condition:

Option
osFlagsWaitAny Wait for any flag (default).
osFlagsWaitAll Wait for all flags.
osFlagsNoClear Do not clear flags which have been specified to wait for.

If osFlagsNoClear is set in the options osThreadFlagsClear can be used to clear flags manually.

The parameter timeout represents a number of timer ticks and is an upper bound. The exact time delay depends on the actual time elapsed since the last timer tick.

Note
This function cannot be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h"
void Thread (void* arg) {
;
osThreadFlagsWait (0x00000001U, osFlagsWaitAny, osWaitForever); // Wait forever until thread flag 1 is set.
;
osThreadFlagsWait (0x00000003U, osFlagsWaitAny, osWaitForever); // Wait forever until either thread flag 0 or 1 is set.
;
osThreadFlagsWait (0x00000003U, osFlagsWaitAll, 10); // Wait for 10 timer ticks until thread flags 0 and 1 are set. Timeout afterwards.
;
osThreadFlagsWait (0x00000003U, osFlagsWaitAll | osFlagsNoClear, osWaitForever); // Same as the above, but the flags will not be cleared.
}