SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_sync_barrier.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_SYNC_BARRIER_H_
11#define STK_SYNC_BARRIER_H_
12
13#include "stk_sync_mutex.h"
14#include "stk_sync_cv.h"
15
19
20namespace stk {
21namespace sync {
22
54class Barrier final : public ITraceable
55{
56public:
61 explicit Barrier(uint32_t count) : m_threshold(count), m_count(count), m_generation(0U)
62 {
63 STK_ASSERT(count != 0U); // API contract: number of parties cannot be 0
64 }
65
70
78 bool Wait();
79
83 uint32_t GetThreshold() const { return m_threshold; }
84
85private:
87
90 const uint32_t m_threshold;
91 uint32_t m_count;
92 uint32_t m_generation;
93};
94
95// ---------------------------------------------------------------------------
96// Wait
97// ---------------------------------------------------------------------------
98
99inline bool Barrier::Wait()
100{
101 bool is_last = false;
102
103 STK_ASSERT(!hw::IsInsideISR()); // API contract: caller must not be in ISR
104
105 m_mutex.Lock();
106
107 const uint32_t gen = m_generation;
108
109 if (--m_count == 0U)
110 {
111 // last task to arrive: start a new generation and release everyone else
112 ++m_generation;
114
115 m_cond.NotifyAll();
116
117 is_last = true;
118 }
119 else
120 {
121 // wait in a loop to guard against spurious wakeups: keep waiting while
122 // we are still in the same generation we entered with
123 while (gen == m_generation)
124 {
126 }
127 }
128
129 m_mutex.Unlock();
130
131 return is_last;
132}
133
134} // namespace sync
135} // namespace stk
136
137#endif /* STK_SYNC_BARRIER_H_ */
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:708
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:516
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:261
Implementation of synchronization primitive: stk::sync::ConditionVariable.
Implementation of synchronization primitive: stk::sync::Mutex.
Namespace of STK package.
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:211
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Synchronization primitives for task coordination and resource protection.
Traceable object.
Definition stk_common.h:511
uint32_t m_count
number of tasks still to arrive in the current generation
Barrier(uint32_t count)
Constructor.
uint32_t GetThreshold() const
Get the number of tasks required to trip the barrier.
ConditionVariable m_cond
signals waiting tasks when the current generation completes
uint32_t m_generation
generation counter, incremented every time the barrier trips
bool Wait()
Block the calling task until count tasks have called Wait().
const uint32_t m_threshold
number of tasks required to trip the barrier
Mutex m_mutex
protects m_count / m_generation and serializes calls to m_cond
Condition Variable primitive for signaling between tasks based on specific predicates.
Definition stk_sync_cv.h:68
Recursive mutex primitive that allows the same thread to acquire the lock multiple times.