CMSIS-RTOS2: Thread Flags

| CMSIS-RTOS2 Version 2.1.1 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
Functions
Thread Flags
CMSIS-RTOS2 API

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.

Usage Example

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

The behavior is the following:

- app_main starts executing
- statement A sets thread flags to 0x02 (flags = 0x02 – after set)
- app_main enters delay
- execution switches to threadX
- statement B waits for flag 0x01 and blocks since flag is not set
- execution switches to app_main
- statement C sets thread flags to 0x07
- threadX wakes-up and clears flag 0x01, thread flags = 0x06, return value set to 0x07 (before clear), note: all this happens during statement C
- statement C returns with flags = 0x06
- app_main enters delay
- execution switches to threadX
- statement B returns with flagsX = 0x07

#include "cmsis_os2.h"
osThreadId_t tid;
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

uint32_t osThreadFlagsSet | ( | osThreadId_t | thread_id,
|  | uint32_t | flags
| ) |  |

Parameters

  [in] | thread_id | thread ID obtained by osThreadNew or osThreadGetId.
  [in] | flags | specifies 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.

Possible Flags Functions Error Codes return values:

- osFlagsErrorUnknown: Unspecified error.
- osFlagsErrorResource: Thread specified by parameter thread_id is not active to receive flags.
- osFlagsErrorParameter: Parameter thread_id is not a valid thread or flags has highest bit set.

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, 0x0100); /* set signal to clock thread */
osDelay(500); /* delay 500ms */
osThreadFlagsSet(tid_clock, 0x0100); /* set signal to clock thread */
osDelay(500); /* delay 500ms */
osThreadFlagsSet(tid, 0x0001); /* set signal to thread 'thread' */
osDelay(500); /* delay 500ms */
}

uint32_t osThreadFlagsClear | ( | uint32_t | flags | ) |

Parameters

  [in] | flags | specifies 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.
- osFlagsErrorResource: Running thread is not active to receive flags.
- osFlagsErrorParameter: Parameter flags has highest bit set.

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. If called without a active and currently running thread osThreadFlagsGet return zero.

Note
  This function cannot be called from Interrupt Service Routines.

uint32_t osThreadFlagsWait | ( | uint32_t | flags,
|  | uint32_t | options,
|  | uint32_t | timeout
| ) |  |

Parameters

  [in] | flags | specifies the flags to wait for.
  [in] | options | specifies flags options (osFlagsXxxx).
  [in] | timeout | Timeout 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 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: The awaited flags has not been set during given timeout.
- osFlagsErrorResource: Running thread is not active to receive flags.
- 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, 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.
}