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_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
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 = WAIT_INFINITE);
96
100 void NotifyOne();
101
105 void NotifyAll();
106
107private:
109};
110
111// ---------------------------------------------------------------------------
112// Wait
113// ---------------------------------------------------------------------------
114
115inline bool ConditionVariable::Wait(IMutex &mutex, Timeout timeout)
116{
117 // API contract: mutex must be locked by the calling task before Wait() is called.
118 // The kernel releases it atomically during suspension and re-acquires it on wake.
119
120 if (timeout == NO_WAIT)
121 return false;
122
123 STK_ASSERT(!hw::IsInsideISR()); // API contract: caller must not be in ISR if timeout!=NO_WAIT
124
125 return !IKernelService::GetInstance()->Wait(this, &mutex, timeout)->IsTimeout();
126}
127
128// ---------------------------------------------------------------------------
129// NotifyOne
130// ---------------------------------------------------------------------------
131
133{
135 WakeOne(); // wakes the first task in the wait list (FIFO order), if any
136}
137
138// ---------------------------------------------------------------------------
139// NotifyAll
140// ---------------------------------------------------------------------------
141
143{
145 WakeAll(); // wakes all tasks in the wait list simultaneously
146}
147
148} // namespace sync
149} // namespace stk
150
151#endif /* STK_SYNC_CV_H_ */
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Implementation of synchronization primitive: stk::sync::ScopedCriticalSection.
Namespace of STK package.
const Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Definition stk_common.h:139
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:133
const Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:145
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Definition stktest.cpp:103
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:255
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
Definition stk_common.h:373
void WakeOne()
Wake the first task in the wait list (FIFO order).
Definition stk_common.h:357
ISyncObject()
Constructor.
Definition stk_common.h:350
void WakeAll()
Wake all tasks currently in the wait list.
Definition stk_common.h:367
Interface for mutex synchronization primitive.
Definition stk_common.h:381
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
Definition stktest.cpp:69
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
bool Wait(IMutex &mutex, Timeout timeout=WAIT_INFINITE)
Wait for a signal.
void NotifyOne()
Wake one waiting task.
STK_NONCOPYABLE_CLASS(ConditionVariable)
void NotifyAll()
Wake all waiting tasks.