SuperTinyKernel™ RTOS 1.06.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 ISyncObject, 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
95 bool Wait(IMutex &mutex, Timeout timeout_ticks = WAIT_INFINITE);
96
100 void NotifyOne();
101
106 void NotifyOne_CS();
107
111 void NotifyAll();
112
117 void NotifyAll_CS();
118
119private:
121};
122
123// ---------------------------------------------------------------------------
124// Wait
125// ---------------------------------------------------------------------------
126
127inline bool ConditionVariable::Wait(IMutex &mutex, Timeout timeout_ticks)
128{
129 // API contract: mutex must be locked by the calling task before Wait() is called.
130 // The kernel releases it atomically during suspension and re-acquires it on wake.
131 bool success = false;
132
133 if (timeout_ticks != NO_WAIT)
134 {
135 STK_ASSERT(!hw::IsInsideISR()); // API contract: caller must not be in ISR if timeout_ticks!=NO_WAIT
136
137 success = !IKernelService::GetInstance()->Wait(this, &mutex, timeout_ticks)->IsTimeout();
138 }
139
140 return success;
141}
142
143// ---------------------------------------------------------------------------
144// NotifyOne
145// ---------------------------------------------------------------------------
146
148{
149 const ScopedCriticalSection cs_;
150 NotifyOne_CS();
151}
152
153// ---------------------------------------------------------------------------
154// NotifyOne_CS
155// ---------------------------------------------------------------------------
156
158{
159 WakeOne(); // wakes the first task in the wait list (FIFO order), if any
160}
161
162// ---------------------------------------------------------------------------
163// NotifyAll
164// ---------------------------------------------------------------------------
165
167{
168 const ScopedCriticalSection cs_;
169 NotifyAll_CS(); // wakes all tasks in the wait list simultaneously
170}
171
172// ---------------------------------------------------------------------------
173// NotifyAll_CS
174// ---------------------------------------------------------------------------
175
177{
178 WakeAll(); // wakes all tasks in the wait list simultaneously
179}
180
181} // namespace sync
182} // namespace stk
183
184#endif /* STK_SYNC_CV_H_ */
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
Definition stk_defs.h:601
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:409
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:159
Implementation of synchronization primitive: stk::sync::ScopedCriticalSection.
Namespace of STK package.
constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:189
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:125
constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:183
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Synchronization primitives for task coordination and resource protection.
virtual bool IsTimeout() const =0
Check if task woke up due to a timeout.
Traceable object.
Definition stk_common.h:393
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
Definition stk_common.h:534
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:512
ISyncObject()
Constructor.
Definition stk_common.h:498
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:526
Interface for mutex synchronization primitive.
Definition stk_common.h:542
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
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.
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.