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_cv.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_CV_H_
11#define STK_SYNC_CV_H_
12
13#include "stk_sync_cs.h"
14
18
19namespace stk {
20namespace sync {
21
67class ConditionVariable final : private SyncObjectBase, public ITraceable
68{
69public:
71 {}
72
79 {
80 STK_ASSERT(m_wait_list.IsEmpty()); // API contract: must not be destroyed with waiting tasks
81 }
82
98 bool Wait(IMutex &mutex, Timeout timeout_ticks = WAIT_INFINITE);
99
111 EWaitResult WaitEx(IMutex &mutex, Timeout timeout_ticks = WAIT_INFINITE);
112
116 void NotifyOne();
117
122 void NotifyOne_CS();
123
127 void NotifyAll();
128
133 void NotifyAll_CS();
134
135private:
137};
138
139// ---------------------------------------------------------------------------
140// Wait
141// ---------------------------------------------------------------------------
142
143inline bool ConditionVariable::Wait(IMutex &mutex, Timeout timeout_ticks)
144{
145 return (WaitEx(mutex, timeout_ticks) == WAIT_RESULT_SIGNAL);
146}
147
148// ---------------------------------------------------------------------------
149// WaitEx
150// ---------------------------------------------------------------------------
151
153{
154 // API contract: mutex must be locked by the calling task before Wait() is called.
155 // The kernel releases it atomically during suspension and re-acquires it on wake.
156
157 // NO_WAIT never touches the kernel wait path: treat it the same as an
158 // immediately-expired timeout (consistent with the pre-existing bool Wait() semantics).
160
161 if (timeout_ticks != NO_WAIT)
162 {
163 STK_ASSERT(!hw::IsInsideISR()); // API contract: caller must not be in ISR if timeout_ticks!=NO_WAIT
164
165 result = IKernelService::GetInstance()->Wait(this, &mutex, timeout_ticks);
166 }
167
168 return result;
169}
170
171// ---------------------------------------------------------------------------
172// NotifyOne
173// ---------------------------------------------------------------------------
174
176{
177 const ScopedCriticalSection cs_;
178 NotifyOne_CS();
179}
180
181// ---------------------------------------------------------------------------
182// NotifyOne_CS
183// ---------------------------------------------------------------------------
184
186{
187 WakeOne(); // wakes the first task in the wait list (FIFO order), if any
188}
189
190// ---------------------------------------------------------------------------
191// NotifyAll
192// ---------------------------------------------------------------------------
193
195{
196 const ScopedCriticalSection cs_;
197 NotifyAll_CS(); // wakes all tasks in the wait list simultaneously
198}
199
200// ---------------------------------------------------------------------------
201// NotifyAll_CS
202// ---------------------------------------------------------------------------
203
205{
206 WakeAll(); // wakes all tasks in the wait list simultaneously
207}
208
209} // namespace sync
210} // namespace stk
211
212#endif /* STK_SYNC_CV_H_ */
#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::ScopedCriticalSection.
Namespace of STK package.
static constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:220
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:123
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_common.h:126
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_common.h:125
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:156
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:214
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:533
Interface for mutex synchronization primitive.
Definition stk_common.h:722
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
void WakeAll() override
Wake all tasks currently in the wait list.
Definition stk_helper.h:288
SyncObjectBase()
Constructor.
Definition stk_helper.h:275
IWaitObject::ListHeadType m_wait_list
Tasks blocked on this object.
Definition stk_helper.h:298
void WakeOne() override
Wake the first task in the wait list (FIFO order).
Definition stk_helper.h:283
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Definition stk_sync_cs.h:54
void NotifyOne_CS()
Wake one waiting task.
EWaitResult WaitEx(IMutex &mutex, Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal, preserving the full wait outcome.
void NotifyOne()
Wake one waiting task.
bool Wait(IMutex &mutex, Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal.
void NotifyAll_CS()
Wake all waiting tasks.
void NotifyAll()
Wake all waiting tasks.