oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
ove::Thread< StackSize > Class Template Reference

RAII wrapper around an oveRTOS thread (task). More...

#include <thread.hpp>

Public Types

using id = thread_id
 Opaque thread identity — alias of thread_id. Matches std::thread::id naming.
 

Public Member Functions

template<typename F >
requires (StackSize > 0) && ThreadEntry<F>
 Thread (F entry, void *ctx, ove_prio_t prio, const char *name)
 Constructs and starts the thread.
 
template<typename F >
requires (StackSize > 0) && CooperativeThreadEntry<F>
 Thread (F entry, ove_prio_t prio, const char *name)
 Cooperative-cancellation constructor — std::jthread analog.
 
template<typename F >
requires (StackSize > 0) && CapturingCooperativeEntry<std::decay_t<F>>
 Thread (F &&entry, ove_prio_t prio, const char *name)
 Cooperative-cancellation constructor for capturing entries. Heap-mode only. std::jthread-shaped.
 
 ~Thread () noexcept
 Destroys the thread wrapper, terminating and releasing the kernel thread.
 
 Thread (const Thread &)=delete
 
Threadoperator= (const Thread &)=delete
 
 Thread (Thread &&other) noexcept
 Move constructor — transfers ownership of the kernel handle.
 
Threadoperator= (Thread &&other) noexcept
 Move-assignment operator — transfers ownership of the kernel handle.
 
void join () noexcept
 Block the caller until the worker thread exits, then release the kernel handle. Analog of std::thread::join.
 
void detach () noexcept
 Release ownership of the kernel thread without waiting. Analog of std::thread::detach.
 
void set_priority (ove_prio_t prio)
 Changes the priority of the thread at runtime.
 
void suspend ()
 Suspends execution of the thread.
 
void resume ()
 Resumes a previously suspended thread.
 
ove_thread_state_t get_state () const
 Returns the current execution state of the thread.
 
size_t get_stack_usage () const
 Returns the number of bytes used by the thread's stack so far.
 
Result< struct ove_thread_stats > get_runtime_stats () const noexcept
 Retrieves runtime statistics for the thread.
 
void request_stop () noexcept
 Requests the thread to stop cooperatively.
 
stop_token get_stop_token () const noexcept
 Get a token that observes this thread's cancellation flag.
 
stop_source get_stop_source () const noexcept
 Get a writable stop_source for this thread. Analog of std::jthread::get_stop_source.
 
bool stop_requested () const noexcept
 
bool valid () const
 Returns true if the underlying kernel handle is non-null.
 
bool joinable () const noexcept
 std::thread::joinable analog — true if this wrapper is associated with an active kernel thread.
 
id get_id () const noexcept
 Get the id for this thread. std::thread::get_id analog.
 
ove_thread_t handle () const
 Returns the raw oveRTOS thread handle.
 

Detailed Description

template<size_t StackSize = 0>
class ove::Thread< StackSize >

RAII wrapper around an oveRTOS thread (task).

Creates and starts a thread on construction. The thread is destroyed and the underlying kernel resource is released on destruction.

In zero-heap mode (CONFIG_OVE_ZERO_HEAP) the stack is stored as a member array, so StackSize must be greater than zero and move operations are disabled. On heap-enabled builds a non-zero StackSize is still required because it is passed to the kernel at construction time.

Template Parameters
StackSizeStack size in bytes for the thread (must be > 0).
Note
Not copyable. Move-only when heap allocation is enabled.

Constructor & Destructor Documentation

◆ Thread() [1/4]

template<size_t StackSize = 0>
template<typename F >
requires (StackSize > 0) && ThreadEntry<F>
ove::Thread< StackSize >::Thread ( entry,
void *  ctx,
ove_prio_t  prio,
const char *  name 
)
inline

Constructs and starts the thread.

Only participates in overload resolution when StackSize > 0 and the entry function satisfies ThreadEntry.

Template Parameters
FEntry function type satisfying ThreadEntry.
Parameters
[in]entryFunction pointer (or compatible callable) to use as the thread entry point, with signature void fn(void*).
[in]ctxOpaque pointer passed as the argument to entry.
[in]prioThread priority as an ove_prio_t value.
[in]nameHuman-readable name for the thread (for debugging).

Asserts at startup if thread creation fails.

◆ Thread() [2/4]

template<size_t StackSize = 0>
template<typename F >
requires (StackSize > 0) && CooperativeThreadEntry<F>
ove::Thread< StackSize >::Thread ( entry,
ove_prio_t  prio,
const char *  name 
)
inline

Cooperative-cancellation constructor — std::jthread analog.

The entry function receives an ove::stop_token and must poll stop_token::stop_requested() in any long-running loop so that ~Thread (or an explicit request_stop) can drive it to a clean exit instead of deadlocking the join wait.

[](ove::stop_token tok) {
while (!tok.stop_requested()) {
do_work();
}
},
OVE_PRIO_NORMAL, "worker");
RAII wrapper around an oveRTOS thread (task).
Definition thread.hpp:343
Lightweight read-only handle to a thread's cooperative cancellation flag.
Definition thread.hpp:57
bool stop_requested() const noexcept
Definition thread.hpp:65

The entry must be a stateless callable (function pointer or captureless lambda) — the binding has no heap to store captures in zero-heap mode. Use the void(void*) constructor above with a static context struct if you need bound data.

Only participates in overload resolution when StackSize > 0 and F satisfies CooperativeThreadEntry.

◆ Thread() [3/4]

template<size_t StackSize = 0>
template<typename F >
requires (StackSize > 0) && CapturingCooperativeEntry<std::decay_t<F>>
ove::Thread< StackSize >::Thread ( F &&  entry,
ove_prio_t  prio,
const char *  name 
)
inline

Cooperative-cancellation constructor for capturing entries. Heap-mode only. std::jthread-shaped.

Accepts any callable invocable as void(stop_token) that isn't a plain function pointer — lambdas with captures, std::function, functors with member state. The closure is heap-allocated into a box owned by the kernel thread; the trampoline reclaims (and deletes) it after the entry returns.

int channel_id = 7;
ove::Queue<Msg, 32> &q = get_queue();
[&](ove::stop_token tok) {
while (!tok.stop_requested()) {
q.send(Msg{channel_id});
}
},
OVE_PRIO_NORMAL, "w");
RAII wrapper around an oveRTOS typed message queue.
Definition queue.hpp:47

Lifetime: captured references must outlive the worker — the destructor's request_stop + join guarantees that, provided the captured data outlives the Thread itself. Same rule as std::jthread.

Cost: one heap allocation per spawn (size = sizeof(F)). One template instantiation per distinct closure type F — no type erasure or virtual dispatch.

Zero-heap: this constructor is not defined. Use the captureless cooperative constructor + a static context struct, or the legacy void(void*) constructor with a hand-rolled pointer.

◆ ~Thread()

template<size_t StackSize = 0>
ove::Thread< StackSize >::~Thread ( )
inlinenoexcept

Destroys the thread wrapper, terminating and releasing the kernel thread.

Calls ove_thread_request_stop before the join wait so cooperative workers (built with the stop_token constructor) exit cleanly without blocking the destructor. Non-cooperative workers (built with the legacy void(void*) constructor) are unaffected — the destructor still blocks until the entry function returns by its own logic.

◆ Thread() [4/4]

template<size_t StackSize = 0>
ove::Thread< StackSize >::Thread ( Thread< StackSize > &&  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=()

template<size_t StackSize = 0>
Thread & ove::Thread< StackSize >::operator= ( Thread< StackSize > &&  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.

◆ join()

template<size_t StackSize = 0>
void ove::Thread< StackSize >::join ( )
inlinenoexcept

Block the caller until the worker thread exits, then release the kernel handle. Analog of std::thread::join.

After join the wrapper is empty: valid returns false and ~Thread is a no-op. Further method calls on the wrapper (other than valid / handle) are undefined behaviour — matches std::thread::join post-conditions.

For cooperative workers, call request_stop before join if the worker's loop terminates on the stop flag. Workers without cooperative logic must terminate on their own; otherwise join blocks indefinitely. Calling join from the worker thread itself deadlocks.

Safe to call after detach (no-op, since the handle is already null).

Works in both heap and zero-heap modes — internally calls ove_thread_destroy (heap) or ove_thread_deinit (zero-heap), both of which wait for the worker to exit.

◆ detach()

template<size_t StackSize = 0>
void ove::Thread< StackSize >::detach ( )
inlinenoexcept

Release ownership of the kernel thread without waiting. Analog of std::thread::detach.

After detach the wrapper is empty: valid returns false and ~Thread is a no-op. The kernel thread keeps running with its own resources; the RTOS reaps them when the entry function returns.

Unlike ~Thread, detach does NOT call request_stop — the worker is genuinely fire-and-forget. Hand it out a stop_token via get_stop_token before detaching if you still want a way to cooperatively shut it down later.

ove::Thread<4096>(worker, OVE_PRIO_NORMAL, "bg").detach();
void detach() noexcept
Release ownership of the kernel thread without waiting. Analog of std::thread::detach.
Definition thread.hpp:597

Heap-mode only. See the zero-heap detach stub for why.

◆ set_priority()

template<size_t StackSize = 0>
void ove::Thread< StackSize >::set_priority ( ove_prio_t  prio)
inline

Changes the priority of the thread at runtime.

Parameters
[in]prioNew priority value.

◆ suspend()

template<size_t StackSize = 0>
void ove::Thread< StackSize >::suspend ( )
inline

Suspends execution of the thread.

The thread will not be scheduled until resume() is called.

◆ get_state()

template<size_t StackSize = 0>
ove_thread_state_t ove::Thread< StackSize >::get_state ( ) const
inline

Returns the current execution state of the thread.

Returns
An ove_thread_state_t value representing the thread state.

◆ get_stack_usage()

template<size_t StackSize = 0>
size_t ove::Thread< StackSize >::get_stack_usage ( ) const
inline

Returns the number of bytes used by the thread's stack so far.

Returns
Peak stack usage in bytes.

◆ get_runtime_stats()

template<size_t StackSize = 0>
Result< struct ove_thread_stats > ove::Thread< StackSize >::get_runtime_stats ( ) const
inlinenoexcept

Retrieves runtime statistics for the thread.

Returns
On success, the populated ove_thread_stats. On failure, an unexpected Error (Error::NotSupported if the substrate doesn't track stats).

◆ request_stop()

template<size_t StackSize = 0>
void ove::Thread< StackSize >::request_stop ( )
inlinenoexcept

Requests the thread to stop cooperatively.

Sets the thread's cancellation flag. The worker must poll stop_token::stop_requested via the stop_token it received (cooperative-cancellation constructor) for this to have any effect — the substrate does NOT force-terminate.

Safe from any context (ISR, other thread, the thread itself). Idempotent; the flag is sticky.

See also
~Thread, stop_token

◆ get_stop_token()

template<size_t StackSize = 0>
stop_token ove::Thread< StackSize >::get_stop_token ( ) const
inlinenoexcept

Get a token that observes this thread's cancellation flag.

Useful for passing the token to helper functions that need to bail out cooperatively without being given control of the Thread object itself.

◆ get_stop_source()

template<size_t StackSize = 0>
stop_source ove::Thread< StackSize >::get_stop_source ( ) const
inlinenoexcept

Get a writable stop_source for this thread. Analog of std::jthread::get_stop_source.

The returned source can issue stop requests without giving away the owning Thread. Combine with get_stop_token to hand out read-only observers separately from writable signal points.

void register_shutdown_hook(ove::stop_source ss);
register_shutdown_hook(worker.get_stop_source());
Writable counterpart to stop_token. std::stop_source analog.
Definition thread.hpp:128

◆ stop_requested()

template<size_t StackSize = 0>
bool ove::Thread< StackSize >::stop_requested ( ) const
inlinenoexcept
Returns
true if request_stop has been called on this thread.

◆ valid()

template<size_t StackSize = 0>
bool ove::Thread< StackSize >::valid ( ) const
inline

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

Returns
true when the thread was successfully created.

◆ joinable()

template<size_t StackSize = 0>
bool ove::Thread< StackSize >::joinable ( ) const
inlinenoexcept

std::thread::joinable analog — true if this wrapper is associated with an active kernel thread.

Returns false after join or detach (or default- construction failure, though our constructors abort instead). Synonym of valid; both stay supported.

◆ get_id()

template<size_t StackSize = 0>
id ove::Thread< StackSize >::get_id ( ) const
inlinenoexcept

Get the id for this thread. std::thread::get_id analog.

Returns a default-constructed id (compare-equal to id{}) if the wrapper has been joined / detached / never spawned.

◆ handle()

template<size_t StackSize = 0>
ove_thread_t ove::Thread< StackSize >::handle ( ) const
inline

Returns the raw oveRTOS thread handle.

Returns
The opaque ove_thread_t handle.

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