oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/thread.h>
17#include <ove/types.hpp>
18
19namespace ove {
20
37template <size_t StackSize = 0>
38class Thread {
39public:
55 template <typename F>
56 Thread(F entry, void *ctx, ove_prio_t prio, const char *name)
57 requires (StackSize > 0) && ThreadEntry<F>
58 {
59 struct ove_thread_desc desc = {};
60 desc.name = name;
61 desc.entry = entry;
62 desc.arg = ctx;
63 desc.priority = prio;
64 desc.stack_size = StackSize;
65#ifdef CONFIG_OVE_ZERO_HEAP
66 static_assert(StackSize > 0,
67 "StackSize must be > 0 in zero-heap mode");
68 desc.stack = stack_;
69 int err = ove_thread_init(&handle_, &storage_, &desc);
70#else
71 int err = ove_thread_create_(&handle_, &desc);
72#endif
73 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
74 }
75
80 if (!handle_) return;
81#ifdef CONFIG_OVE_ZERO_HEAP
82 ove_thread_deinit(handle_);
83#else
84 ove_thread_destroy(handle_);
85#endif
86 }
87
88 Thread(const Thread &) = delete;
89 Thread &operator=(const Thread &) = delete;
90
91#ifdef CONFIG_OVE_ZERO_HEAP
92 Thread(Thread &&) = delete;
93 Thread &operator=(Thread &&) = delete;
94#else
99 Thread(Thread &&other) noexcept : handle_(other.handle_) {
100 other.handle_ = nullptr;
101 }
102
108 Thread &operator=(Thread &&other) noexcept {
109 if (this != &other) {
110 if (handle_) ove_thread_destroy(handle_);
111 handle_ = other.handle_;
112 other.handle_ = nullptr;
113 }
114 return *this;
115 }
116#endif
117
122 void set_priority(ove_prio_t prio) {
123 ove_thread_set_priority(handle_, prio);
124 }
125
131 void suspend() {
132 ove_thread_suspend(handle_);
133 }
134
138 void resume() {
139 ove_thread_resume(handle_);
140 }
141
146 ove_thread_state_t get_state() const {
147 return ove_thread_get_state(handle_);
148 }
149
154 size_t get_stack_usage() const {
155 return ove_thread_get_stack_usage(handle_);
156 }
157
163 int get_runtime_stats(struct ove_thread_stats *stats) const {
164 return ove_thread_get_runtime_stats(handle_, stats);
165 }
166
171 bool valid() const { return handle_ != nullptr; }
172
177 ove_thread_t handle() const { return handle_; }
178
183 static void sleep_ms(uint32_t ms) {
184 ove_thread_sleep_ms(ms);
185 }
186
190 static void yield() {
191 ove_thread_yield();
192 }
193
198 static ove_thread_t self() {
199 return ove_thread_get_self();
200 }
201
202private:
203 ove_thread_t handle_ = nullptr;
204#ifdef CONFIG_OVE_ZERO_HEAP
205 ove_thread_storage_t storage_ = {};
206 OVE_THREAD_STACK_MEMBER_(stack_,
207 StackSize > 0 ? StackSize : 1);
208#endif
209};
210
211/* ── System memory statistics ──────────────────────────────────── */
212
217struct MemStats {
218 size_t total{};
219 size_t free{};
220 size_t used{};
221 size_t peak_used{};
222};
223
229[[nodiscard]] inline int get_mem_stats(MemStats &stats) {
230 struct ove_mem_stats ms{};
231 int ret = ove_sys_get_mem_stats(&ms);
232 if (ret == OVE_OK) {
233 stats.total = ms.total;
234 stats.free = ms.free;
235 stats.used = ms.used;
236 stats.peak_used = ms.peak_used;
237 }
238 return ret;
239}
240
241/* ── Thread enumeration ───────────────────────────────────────── */
242
248 const char *name{};
249 ove_thread_state_t state{OVE_THREAD_STATE_UNKNOWN};
250 int priority{};
251 size_t stack_used{};
252};
253
261[[nodiscard]] inline int thread_list(ThreadInfo *out, size_t max,
262 size_t *count = nullptr) {
263 struct ove_thread_info buf[16];
264 size_t n = 0;
265 int ret = ove_thread_list(buf, max < 16 ? max : 16, &n);
266 if (ret == OVE_OK) {
267 for (size_t i = 0; i < n && i < max; i++) {
268 out[i].name = buf[i].name;
269 out[i].state = buf[i].state;
270 out[i].priority = buf[i].priority;
271 out[i].stack_used = buf[i].stack_used;
272 }
273 if (count) *count = n;
274 }
275 return ret;
276}
277
278} /* namespace ove */
RAII wrapper around an oveRTOS thread (task).
Definition thread.hpp:38
void set_priority(ove_prio_t prio)
Changes the priority of the thread at runtime.
Definition thread.hpp:122
Thread & operator=(Thread &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition thread.hpp:108
Thread(F entry, void *ctx, ove_prio_t prio, const char *name)
Constructs and starts the thread.
Definition thread.hpp:56
ove_thread_t handle() const
Returns the raw oveRTOS thread handle.
Definition thread.hpp:177
~Thread()
Destroys the thread wrapper, terminating and releasing the kernel thread.
Definition thread.hpp:79
void resume()
Resumes a previously suspended thread.
Definition thread.hpp:138
int get_runtime_stats(struct ove_thread_stats *stats) const
Retrieves runtime statistics for the thread.
Definition thread.hpp:163
Thread(Thread &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition thread.hpp:99
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition thread.hpp:171
ove_thread_state_t get_state() const
Returns the current execution state of the thread.
Definition thread.hpp:146
static ove_thread_t self()
Returns the oveRTOS handle of the currently executing thread.
Definition thread.hpp:198
size_t get_stack_usage() const
Returns the number of bytes used by the thread's stack so far.
Definition thread.hpp:154
void suspend()
Suspends execution of the thread.
Definition thread.hpp:131
static void yield()
Yields the calling thread's remaining time slice to the scheduler.
Definition thread.hpp:190
static void sleep_ms(uint32_t ms)
Suspends the calling thread for the specified duration.
Definition thread.hpp:183
Concept satisfied by any callable convertible to void(*)(void*).
Definition types.hpp:54
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
int get_mem_stats(MemStats &stats)
Query system heap statistics.
Definition thread.hpp:229
int thread_list(ThreadInfo *out, size_t max, size_t *count=nullptr)
List all threads in the system.
Definition thread.hpp:261
System heap statistics snapshot.
Definition thread.hpp:217
size_t used
Definition thread.hpp:220
size_t total
Definition thread.hpp:218
size_t peak_used
Definition thread.hpp:221
size_t free
Definition thread.hpp:219
Snapshot of a single thread.
Definition thread.hpp:247
const char * name
Thread name.
Definition thread.hpp:248
size_t stack_used
Peak stack usage in bytes.
Definition thread.hpp:251
ove_thread_state_t state
Current state.
Definition thread.hpp:249
int priority
Scheduling priority.
Definition thread.hpp:250
Common type definitions and concepts for the C++ wrapper layer.