CMSIS-RTOS2  
Real-Time Operating System API
 
Loading...
Searching...
No Matches
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.
 
uint32_t osThreadFlagsClear (uint32_t flags)
 Clear the specified Thread Flags of current running thread.
 
uint32_t osThreadFlagsGet (void)
 Get the current Thread Flags of current running thread.
 
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.
 

Description

Synchronize threads using flags.

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

Usage Example

The following (incomplete) code excerpt sketches the usage principals for Thread Flags.

The behavior is the following:

#include "cmsis_os2.h"
uint32_t flagsX;
uint32_t flags;
void threadX (void *argument) {
osDelay(1U);
for (;;) {
flagsX = osThreadFlagsWait(0x0001U, osFlagsWaitAny, osWaitForever); /* B */
}
}
void app_main (void *argument) {
tid = osThreadNew(threadX, NULL, NULL);
flags = osThreadFlagsSet(tid, 0x0002U); /* A */
osDelay(2U);
flags = osThreadFlagsSet(tid, 0x0005U); /* C */
osDelay(2U);
for(;;);
}

Function Documentation

◆ osThreadFlagsSet()

uint32_t osThreadFlagsSet ( osThreadId_t  thread_id,
uint32_t  flags 
)

Set the specified Thread Flags of a thread.

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. The thread returns the flags stored in the thread control block, or an error code if highest bit is set (refer to Flags Functions Error Codes). Refer to Usage Examples below to understand how the return value is computed.

The target thread waiting for a flag to be set will resume from BLOCKED state.

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error.
  • osFlagsErrorParameter: parameter thread_id is not a valid thread or flags has highest bit set.
  • osFlagsErrorResource: the thread is in invalid state.
  • osFlagsErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
Note
This function may be called from Interrupt Service Routines.

Code Example

/*----------------------------------------------------------------------------
* Function 'signal_func' called from multiple threads
*---------------------------------------------------------------------------*/
void signal_func (osThreadId_t tid) {
osThreadFlagsSet(tid_clock, 0x0100U); /* set signal to clock thread */
osDelay(500U); /* delay 500ms */
osThreadFlagsSet(tid_clock, 0x0100U); /* set signal to clock thread */
osDelay(500U); /* delay 500ms */
osThreadFlagsSet(tid, 0x0001U); /* set signal to thread 'thread' */
osDelay(500U); /* delay 500ms */
}

Usage Examples

The following diagram assumes that in the control block of Thread1 the flag 1 is already set. Thread2 now sets flag 2 and Thread1 returns the updated value immediately:

msc_inline_mscgraph_1

Depending on thread scheduling, the flag status can be modified before returning:

msc_inline_mscgraph_2
Note
* In this case osThreadFlagsWait auto-clears the flag.

◆ osThreadFlagsClear()

uint32_t osThreadFlagsClear ( uint32_t  flags)

Clear the specified Thread Flags of current running thread.

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

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error, i.e. not called from a running threads context.
  • osFlagsErrorParameter: parameter flags has highest bit set.
  • osFlagsErrorISR: the function osThreadFlagsClear cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.

◆ osThreadFlagsGet()

uint32_t osThreadFlagsGet ( void  )

Get the current Thread Flags of current running thread.

Returns
current thread flags.

The function osThreadFlagsGet returns the flags currently set for the currently running thread. If called without a active and currently running thread osThreadFlagsGet return zero.

Note
This function cannot be called from Interrupt Service Routines.

◆ osThreadFlagsWait()

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.

Parameters
[in]flagsspecifies the flags to wait for.
[in]optionsspecifies flags options (osFlagsXxxx).
[in]timeoutTimeout Values 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 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. Otherwise osThreadFlagsWait automatically clears the flags waited for.

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.

The function returns the flags before clearing, or an error code if highest bit is set (refer to Flags Functions Error Codes).

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error, i.e. not called from a running threads context.
  • osFlagsErrorTimeout: awaited flags have not been set in the given time.
  • osFlagsErrorResource: awaited flags have not been set when no timeout was specified.
  • osFlagsErrorParameter: Parameter flags has highest bit set.
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, 10U); // 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.
}