oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ove::CondVar Class Reference

RAII wrapper around an oveRTOS condition variable. More...

#include <sync.hpp>

Public Member Functions

 CondVar ()
 Constructs and initialises the condition variable.
 
 ~CondVar () noexcept
 Destroys the condition variable, releasing the underlying kernel resource.
 
 CondVar (const CondVar &)=delete
 
CondVaroperator= (const CondVar &)=delete
 
 CondVar (CondVar &&other) noexcept
 Move constructor — transfers ownership of the kernel handle.
 
CondVaroperator= (CondVar &&other) noexcept
 Move-assignment operator — transfers ownership of the kernel handle.
 
void wait (Mutex &mtx)
 Atomically releases the mutex and waits for a notification.
 
template<class Rep , class Period >
Result< void > try_wait_for (Mutex &mtx, const std::chrono::duration< Rep, Period > &rel) noexcept
 Bounded-wait.
 
template<class Clock , class Duration >
Result< void > try_wait_until (Mutex &mtx, const std::chrono::time_point< Clock, Duration > &deadline) noexcept
 Deadline-based wait templated over the clock.
 
template<typename Predicate >
void wait (Mutex &mtx, Predicate pred)
 Predicate-loop wait. std::condition_variable::wait predicate-overload analog.
 
template<typename Rep , typename Period , typename Predicate >
bool try_wait_for (Mutex &mtx, std::chrono::duration< Rep, Period > rel, Predicate pred)
 Bounded-wait with predicate. std::condition_variable::wait_for predicate-overload analog.
 
template<typename Clock , typename Duration , typename Predicate >
bool try_wait_until (Mutex &mtx, const std::chrono::time_point< Clock, Duration > &deadline, Predicate pred)
 Deadline-based wait with predicate. std::condition_variable::wait_until predicate-overload analog.
 
void notify_one ()
 Wakes one task waiting on this condition variable. std::condition_variable::notify_one analog.
 
void notify_all ()
 Wakes all tasks waiting on this condition variable. std::condition_variable::notify_all analog.
 
bool valid () const
 Returns true if the underlying kernel handle is non-null.
 
ove_condvar_t handle () const
 Returns the raw oveRTOS condition variable handle.
 

Detailed Description

RAII wrapper around an oveRTOS condition variable.

A condition variable allows tasks to efficiently wait for a predicate to become true. It must always be used together with a Mutex — the mutex is atomically released while waiting and re-acquired before wait() returns.

Note
Not copyable. Move-only when heap allocation is enabled.
Always check the predicate in a loop after wait() to guard against spurious wake-ups.

Constructor & Destructor Documentation

◆ CondVar() [1/2]

ove::CondVar::CondVar ( )
inline

Constructs and initialises the condition variable.

Asserts at startup if initialisation fails.

◆ CondVar() [2/2]

ove::CondVar::CondVar ( CondVar &&  other)
inlinenoexcept

Move constructor — transfers ownership of the kernel handle.

Parameters
otherThe source; its handle is set to null after the move.

Member Function Documentation

◆ operator=()

CondVar & ove::CondVar::operator= ( CondVar &&  other)
inlinenoexcept

Move-assignment operator — transfers ownership of the kernel handle.

Parameters
otherThe source; its handle is set to null after the move.
Returns
Reference to this object.

◆ wait() [1/2]

void ove::CondVar::wait ( Mutex mtx)
inline

Atomically releases the mutex and waits for a notification.

The calling task must hold mtx before calling this method. The mutex is released atomically while the task blocks, and re-acquired before the call returns.

Forever wait; failure means the handle is unusable. Aborts via OVE_STATIC_INIT_ASSERT. Always re-check the predicate after wait returns to guard against spurious wake-ups.

std::condition_variable::wait analog.

Parameters
[in]mtxThe mutex associated with the predicate being waited on. Must be locked by the calling thread.

◆ try_wait_for() [1/2]

template<class Rep , class Period >
Result< void > ove::CondVar::try_wait_for ( Mutex mtx,
const std::chrono::duration< Rep, Period > &  rel 
)
inlinenoexcept

Bounded-wait.

Loose analog of std::condition_variable::wait_for — but returns Result<void> rather than std::cv_status, matching the Result-shape convention used by the rest of the ove::Mutex/Semaphore/Event timeout-bearing methods.

(std::cv_status is unavailable on the bare-metal backends — arm-gnu-toolchain's libstdc++ ships with --enable-threads=single, so neither std::condition_variable nor std::cv_status are declared. Using Result<void> keeps the API uniform across POSIX, FreeRTOS, NuttX and Zephyr.)

Parameters
[in]mtxThe mutex (locked by the calling thread). The kernel releases it while blocking and re-acquires it before returning.
[in]relRelative timeout (any std::chrono::duration unit).
Returns
Empty Result<void> on wake; unexpected Error::Timeout if the deadline elapsed without a notification; unexpected with another Error value on backend failure.
Note
No try_wait() immediate form — a condvar wait is inherently blocking; std::condition_variable matches this (no try_wait).
Always re-check the predicate in a loop after this returns to guard against spurious wake-ups. The predicate-overload below does this internally.

◆ try_wait_until() [1/2]

template<class Clock , class Duration >
Result< void > ove::CondVar::try_wait_until ( Mutex mtx,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inlinenoexcept

Deadline-based wait templated over the clock.

Same clock-templating rationale as Mutex::try_lock_until.

Returns
As try_wait_forResult<void> with Error::Timeout on timeout.

◆ wait() [2/2]

template<typename Predicate >
void ove::CondVar::wait ( Mutex mtx,
Predicate  pred 
)
inline

Predicate-loop wait. std::condition_variable::wait predicate-overload analog.

Equivalent to:

while (!pred()) wait(mtx);
void wait(Mutex &mtx)
Atomically releases the mutex and waits for a notification.
Definition sync.hpp:1023

Handles spurious wake-ups internally; caller cannot accidentally write the if (cv.wait(...) == OVE_OK && ready) { ... } race-prone form.

Parameters
[in]mtxThe mutex (locked by the calling thread).
[in]predCallable returning bool. Evaluated under mtx.

◆ try_wait_for() [2/2]

template<typename Rep , typename Period , typename Predicate >
bool ove::CondVar::try_wait_for ( Mutex mtx,
std::chrono::duration< Rep, Period >  rel,
Predicate  pred 
)
inline

Bounded-wait with predicate. std::condition_variable::wait_for predicate-overload analog.

Loops on pred, returning when either pred() becomes true or rel elapses.

Returns
true if pred() was true on return; false if the timeout elapsed with pred() still false.

◆ try_wait_until() [2/2]

template<typename Clock , typename Duration , typename Predicate >
bool ove::CondVar::try_wait_until ( Mutex mtx,
const std::chrono::time_point< Clock, Duration > &  deadline,
Predicate  pred 
)
inline

Deadline-based wait with predicate. std::condition_variable::wait_until predicate-overload analog.

Loops on pred, recomputing the remaining timeout on each iteration so spurious wake-ups don't shorten the effective deadline.

Returns
true if pred() was true on return; false if the deadline elapsed with pred() still false.

◆ valid()

bool ove::CondVar::valid ( ) const
inline

Returns true if the underlying kernel handle is non-null.

Returns
true when the condition variable was successfully initialised.

◆ handle()

ove_condvar_t ove::CondVar::handle ( ) const
inline

Returns the raw oveRTOS condition variable handle.

Returns
The opaque ove_condvar_t handle.

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