SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::sync::EventFlags Class Reference

32-bit event flags group for multi-flag synchronization between tasks. More...

#include <stk_sync_eventflags.h>

Inheritance diagram for stk::sync::EventFlags:
Collaboration diagram for stk::sync::EventFlags:

Public Member Functions

 EventFlags (uint32_t initial_flags=0U)
 Constructor.
 ~EventFlags ()
 Destructor.
uint32_t Set (uint32_t flags)
 Set one or more flags.
uint32_t Clear (uint32_t flags)
 Clear one or more flags.
uint32_t Get () const
 Read the current flags word without modifying it.
uint32_t Wait (uint32_t flags, uint32_t options=OPT_WAIT_ANY, Timeout timeout=WAIT_INFINITE)
 Wait for one or more flags to be set.
uint32_t TryWait (uint32_t flags, uint32_t options=OPT_WAIT_ANY)
 Non-blocking flag poll.
void SetTraceName (const char *name)
 Set name.
const char * GetTraceName () const
 Get name.

Static Public Member Functions

static bool IsError (uint32_t result)
 Checks if a return value from Set(), Clear(), or Wait() is an error.

Static Public Attributes

static const uint32_t OPT_WAIT_ANY = 0x00000000U
 Wait for ANY of the specified flags to be set (OR semantics, default).
static const uint32_t OPT_WAIT_ALL = 0x00000001U
 Wait for ALL of the specified flags to be set simultaneously (AND semantics).
static const uint32_t OPT_NO_CLEAR = 0x00000002U
 Do not clear matched flags after a successful wait.
static const uint32_t ERROR_PARAMETER = 0x80000001U
 Return sentinel: invalid flags argument (bit 31 set).
static const uint32_t ERROR_TIMEOUT = 0x80000002U
 Return sentinel: wait timed out before the flags condition was met.
static const uint32_t ERROR_ISR = 0x80000004U
 Return sentinel: called from an ISR with a blocking timeout.
static const uint32_t ERROR_MASK = 0x80000000U
 Reserved error mask. Any return value with bit 31 set is an error.

Private Member Functions

 STK_NONCOPYABLE_CLASS (EventFlags)
bool IsSatisfied (uint32_t flags, uint32_t options) const

Private Attributes

volatile uint32_t m_flags
 32-bit flags word (bit 31 is reserved for errors)
ConditionVariable m_cv
 woken by Set() to re-evaluate waiting tasks

Detailed Description

32-bit event flags group for multi-flag synchronization between tasks.

EventFlags maintains a 32-bit word where each bit represents an independent boolean event. Tasks can set or clear any combination of flags, and wait for any subset to become set — either requiring all requested flags (AND semantics) or any one of them (OR semantics).

Unlike Event, which models a single binary signal, EventFlags allows fine-grained coordination across up to 32 independent conditions in a single object.

Note
Wait semantics: the caller selects the mode via the options parameter using WAIT_ANY (default) or WAIT_ALL.
Auto-clear: by default the matched flags are atomically cleared upon a successful Wait(). Pass NO_CLEAR to suppress this.
Direct Handover: Set() notifies all waiters so that every task re-evaluates its own predicate after waking. Each waiter clears only the flags it matched, so concurrent waiters with disjoint flag masks do not interfere with each other.
Maximum flag bit index is 30 (bits 0..30). Bit 31 is reserved for future error reporting and must never be set by the caller.
// Example: sensor fusion — wait for GPS and IMU data to both arrive
stk::sync::EventFlags g_SensorFlags;
static const uint32_t FLAG_GPS = (1U << 0);
static const uint32_t FLAG_IMU = (1U << 1);
void ISR_GPS() {
g_SensorFlags.Set(FLAG_GPS); // ISR-safe
}
void ISR_IMU() {
g_SensorFlags.Set(FLAG_IMU); // ISR-safe
}
void Task_Fusion() {
// block until BOTH flags are set simultaneously; clears them on return
uint32_t raised = g_SensorFlags.Wait(FLAG_GPS | FLAG_IMU,
EventFlags::WAIT_ALL, 5000);
if (raised & EventFlags::ERROR_TIMEOUT) {
// handle timeout
} else {
// process fused sensor data
}
}
32-bit event flags group for multi-flag synchronization between tasks.
uint32_t Wait(uint32_t flags, uint32_t options=OPT_WAIT_ANY, Timeout timeout=WAIT_INFINITE)
Wait for one or more flags to be set.
static const uint32_t ERROR_TIMEOUT
Return sentinel: wait timed out before the flags condition was met.
uint32_t Set(uint32_t flags)
Set one or more flags.
See also
Event, Semaphore, ConditionVariable
Note
Only available when kernel is compiled with KERNEL_SYNC mode enabled.

Definition at line 75 of file stk_sync_eventflags.h.

Constructor & Destructor Documentation

◆ EventFlags()

stk::sync::EventFlags::EventFlags ( uint32_t initial_flags = 0U)
inlineexplicit

Constructor.

Parameters
[in]initial_flagsInitial value of the 32-bit flags word. Only bits 0..30 are meaningful; bit 31 is reserved.

Definition at line 125 of file stk_sync_eventflags.h.

125 : m_flags(initial_flags)
126 {
127 STK_ASSERT((initial_flags & ERROR_MASK) == 0U); // API contract: bit 31 must not be set
128 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
volatile uint32_t m_flags
32-bit flags word (bit 31 is reserved for errors)
static const uint32_t ERROR_MASK
Reserved error mask. Any return value with bit 31 set is an error.

References ERROR_MASK, m_flags, and STK_ASSERT.

Referenced by STK_NONCOPYABLE_CLASS().

Here is the caller graph for this function:

◆ ~EventFlags()

stk::sync::EventFlags::~EventFlags ( )
inline

Destructor.

Note
If tasks are still waiting at destruction time it is a logical error (dangling waiters). An assertion is triggered in debug builds.
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

Definition at line 135 of file stk_sync_eventflags.h.

136 {
137 // API contract: must not be destroyed while tasks are waiting
138 // (checked inside Mutex/ConditionVariable destructors via their own assertions)
139 }

Member Function Documentation

◆ Clear()

uint32_t stk::sync::EventFlags::Clear ( uint32_t flags)
inline

Clear one or more flags.

Atomically clears the specified flags from the internal word.

Parameters
[in]flagsBitmask of flags to clear. Must not have bit 31 set.
Returns
The flags word value before clearing, or ERROR_PARAMETER if flags is 0 or has bit 31 set.
Note
ISR-safe.

Definition at line 255 of file stk_sync_eventflags.h.

256{
257 if ((flags == 0U) || ((flags & ERROR_MASK) != 0U))
258 return ERROR_PARAMETER;
259
260 ScopedCriticalSection cs_;
261
262 const uint32_t prev = m_flags;
263 m_flags &= ~flags;
264
265 return prev;
266}
static const uint32_t ERROR_PARAMETER
Return sentinel: invalid flags argument (bit 31 set).

References ERROR_MASK, ERROR_PARAMETER, and m_flags.

Referenced by stk_ef_clear().

Here is the caller graph for this function:

◆ Get()

uint32_t stk::sync::EventFlags::Get ( ) const
inline

Read the current flags word without modifying it.

Returns
Snapshot of the current 32-bit flags word.
Note
The returned value is a point-in-time snapshot; it may be stale by the time the caller acts on it.
ISR-safe.

Definition at line 272 of file stk_sync_eventflags.h.

273{
274 // atomic on 32-bit aligned targets, a critical section is not required for
275 // a single-word read, matching the approach used by Semaphore::GetCount()
276 return m_flags;
277}

References m_flags.

Referenced by stk_ef_get().

Here is the caller graph for this function:

◆ GetTraceName()

const char * stk::ITraceable::GetTraceName ( ) const
inlineinherited

Get name.

Returns
Name string, or NULL if not set or if STK_SYNC_DEBUG_NAMES is 0.

Definition at line 278 of file stk_common.h.

279 {
280 #if STK_SYNC_DEBUG_NAMES
281 return m_trace_name;
282 #else
283 return nullptr;
284 #endif
285 }

◆ IsError()

◆ IsSatisfied()

bool stk::sync::EventFlags::IsSatisfied ( uint32_t flags,
uint32_t options ) const
inlineprivate

Definition at line 219 of file stk_sync_eventflags.h.

220 {
221 if (options & OPT_WAIT_ALL)
222 return ((m_flags & flags) == flags);
223 else
224 return ((m_flags & flags) != 0U);
225 }
static const uint32_t OPT_WAIT_ALL
Wait for ALL of the specified flags to be set simultaneously (AND semantics).

References m_flags, and OPT_WAIT_ALL.

Referenced by Wait().

Here is the caller graph for this function:

◆ Set()

uint32_t stk::sync::EventFlags::Set ( uint32_t flags)
inline

Set one or more flags.

Performs an atomic OR of flags into the internal flags word, then wakes all tasks currently waiting on this object so that each can re-evaluate its own wait condition.

Parameters
[in]flagsBitmask of flags to set. Must not have bit 31 set.
Returns
The flags word value after setting, or ERROR_PARAMETER if flags is 0 or has bit 31 set.
Note
ISR-safe.

Definition at line 235 of file stk_sync_eventflags.h.

236{
237 if ((flags == 0U) || ((flags & ERROR_MASK) != 0U))
238 return ERROR_PARAMETER;
239
240 ScopedCriticalSection cs_;
241
242 m_flags |= flags;
243 const uint32_t result = m_flags;
244
245 // wake all waiters: each task will re-evaluate its own predicate
246 m_cv.NotifyAll();
247
248 return result;
249}
ConditionVariable m_cv
woken by Set() to re-evaluate waiting tasks

References ERROR_MASK, ERROR_PARAMETER, m_cv, and m_flags.

Referenced by stk_ef_set().

Here is the caller graph for this function:

◆ SetTraceName()

void stk::ITraceable::SetTraceName ( const char * name)
inlineinherited

Set name.

Parameters
[in]nameNull-terminated string or NULL.
Note
If STK_SYNC_DEBUG_NAMES is 0 then calling this function has no effect.

Definition at line 266 of file stk_common.h.

267 {
268 #if STK_SYNC_DEBUG_NAMES
269 m_trace_name = name;
270 #else
271 (void)name;
272 #endif
273 }

◆ STK_NONCOPYABLE_CLASS()

stk::sync::EventFlags::STK_NONCOPYABLE_CLASS ( EventFlags )
private

References EventFlags().

Here is the call graph for this function:

◆ TryWait()

uint32_t stk::sync::EventFlags::TryWait ( uint32_t flags,
uint32_t options = OPT_WAIT_ANY )
inline

Non-blocking flag poll.

Checks immediately whether the flag condition is satisfied. Clears matched flags on success unless NO_CLEAR is set.

Parameters
[in]flagsBitmask of flag bits to watch.
[in]optionsWAIT_ANY (default) or WAIT_ALL, optionally NO_CLEAR.
Returns
Matched flags bitmask, or an ERROR_* sentinel.
Note
ISR-safe.

Definition at line 209 of file stk_sync_eventflags.h.

210 {
211 return Wait(flags, options, NO_WAIT);
212 }
const Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:145

References stk::NO_WAIT, OPT_WAIT_ANY, and Wait().

Referenced by stk_ef_trywait().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Wait()

uint32_t stk::sync::EventFlags::Wait ( uint32_t flags,
uint32_t options = OPT_WAIT_ANY,
Timeout timeout = WAIT_INFINITE )
inline

Wait for one or more flags to be set.

Suspends the calling task until the requested flag condition is satisfied or the timeout expires.

On success the matched flags are atomically cleared (unless NO_CLEAR is passed in options).

Parameters
[in]flagsBitmask of flag bits to watch. Must not be 0 and must not have bit 31 set.
[in]optionsCombination of WAIT_ANY / WAIT_ALL and optionally NO_CLEAR. Default: WAIT_ANY (clear on success).
[in]timeoutMaximum time to wait (ticks). Use WAIT_INFINITE to block indefinitely, NO_WAIT for a non-blocking poll.
Returns
Bitmask of the flags that caused the wakeup (the matched subset), or one of the ERROR_* sentinels on failure. Always check IsError() before using the return value as a flags mask.
Note
If the predicate becomes satisfied in the same tick that the deadline expires, the wait succeeds and returns the matched flags. The timeout is only reported when the condition is not met at deadline time.
Warning
ISR-safe only with timeout = NO_WAIT, ISR-unsafe otherwise.

Definition at line 283 of file stk_sync_eventflags.h.

284{
285 // validate: flags must be non-zero and must not have the error sentinel bit set
286 if ((flags == 0U) || ((flags & ERROR_MASK) != 0U))
287 return ERROR_PARAMETER;
288
289 // ISR check: only NO_WAIT is permitted inside an ISR
290 if (hw::IsInsideISR() && (timeout != NO_WAIT))
291 return ERROR_ISR;
292
293 const bool timed_wait = (timeout != WAIT_INFINITE) && (timeout != NO_WAIT);
294
295 // capture an absolute deadline once, before entering the wait loop,
296 // this prevents the timeout from being silently restarted on each
297 // spurious wakeup (e.g. a partial Set() that does not satisfy WAIT_ALL)
298 const Timeout deadline = (timed_wait ? static_cast<Timeout>(GetTicks() + timeout) : timeout);
299
300 ScopedCriticalSection cs_;
301
302 // spin (with blocking) until the predicate is satisfied or we time out
303 while (!IsSatisfied(flags, options))
304 {
305 Timeout remaining = deadline;
306 if (timed_wait)
307 {
308 const Timeout now = static_cast<Timeout>(GetTicks());
309 remaining = (now >= deadline ? NO_WAIT : (deadline - now));
310 }
311
312 if (!m_cv.Wait(cs_, remaining))
313 {
314 // timeout: release the mutex and report failure
315 return ERROR_TIMEOUT;
316 }
317 }
318
319 // predicate satisfied: determine which flags matched
320 const uint32_t matched = (options & OPT_WAIT_ALL ? flags : (m_flags & flags));
321
322 // atomically clear the matched flags unless the caller opted out
323 if ((options & OPT_NO_CLEAR) == 0U)
324 m_flags &= ~matched;
325
326 return matched;
327}
const Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:139
Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:248
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:133
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Definition stktest.cpp:103
static const uint32_t ERROR_ISR
Return sentinel: called from an ISR with a blocking timeout.
static const uint32_t OPT_NO_CLEAR
Do not clear matched flags after a successful wait.
bool IsSatisfied(uint32_t flags, uint32_t options) const

References ERROR_ISR, ERROR_MASK, ERROR_PARAMETER, ERROR_TIMEOUT, stk::GetTicks(), stk::hw::IsInsideISR(), IsSatisfied(), m_cv, m_flags, stk::NO_WAIT, OPT_NO_CLEAR, OPT_WAIT_ALL, and stk::WAIT_INFINITE.

Referenced by stk_ef_wait(), and TryWait().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ ERROR_ISR

const uint32_t stk::sync::EventFlags::ERROR_ISR = 0x80000004U
static

Return sentinel: called from an ISR with a blocking timeout.

Definition at line 103 of file stk_sync_eventflags.h.

Referenced by Wait().

◆ ERROR_MASK

const uint32_t stk::sync::EventFlags::ERROR_MASK = 0x80000000U
static

Reserved error mask. Any return value with bit 31 set is an error.

Definition at line 106 of file stk_sync_eventflags.h.

Referenced by Clear(), EventFlags(), IsError(), Set(), and Wait().

◆ ERROR_PARAMETER

const uint32_t stk::sync::EventFlags::ERROR_PARAMETER = 0x80000001U
static

Return sentinel: invalid flags argument (bit 31 set).

Definition at line 97 of file stk_sync_eventflags.h.

Referenced by Clear(), Set(), and Wait().

◆ ERROR_TIMEOUT

const uint32_t stk::sync::EventFlags::ERROR_TIMEOUT = 0x80000002U
static

Return sentinel: wait timed out before the flags condition was met.

Definition at line 100 of file stk_sync_eventflags.h.

Referenced by stk::test::eventflags::InitialFlagsTask< _AccessMode >::Run(), stk::test::eventflags::TimeoutTask< _AccessMode >::Run(), and Wait().

◆ m_cv

ConditionVariable stk::sync::EventFlags::m_cv
private

woken by Set() to re-evaluate waiting tasks

Definition at line 228 of file stk_sync_eventflags.h.

Referenced by Set(), and Wait().

◆ m_flags

volatile uint32_t stk::sync::EventFlags::m_flags
private

32-bit flags word (bit 31 is reserved for errors)

Definition at line 227 of file stk_sync_eventflags.h.

Referenced by Clear(), EventFlags(), Get(), IsSatisfied(), Set(), and Wait().

◆ OPT_NO_CLEAR

const uint32_t stk::sync::EventFlags::OPT_NO_CLEAR = 0x00000002U
static

Do not clear matched flags after a successful wait.

Definition at line 90 of file stk_sync_eventflags.h.

Referenced by stk::test::eventflags::NoClearTask< _AccessMode >::Run(), and Wait().

◆ OPT_WAIT_ALL

const uint32_t stk::sync::EventFlags::OPT_WAIT_ALL = 0x00000001U
static

Wait for ALL of the specified flags to be set simultaneously (AND semantics).

Definition at line 87 of file stk_sync_eventflags.h.

Referenced by IsSatisfied(), stk::test::eventflags::MultiWaiterAllTask< _AccessMode >::Run(), stk::test::eventflags::SetWaitAllTask< _AccessMode >::Run(), and Wait().

◆ OPT_WAIT_ANY


The documentation for this class was generated from the following file: