16#include <ove/thread.h>
19#ifndef CONFIG_OVE_ZERO_HEAP
60 constexpr explicit stop_token(ove_thread_t h) noexcept : handle_(h)
67 return handle_ !=
nullptr && ove_thread_should_stop(handle_);
73 return handle_ !=
nullptr;
82 explicit operator bool() const noexcept
88 [[nodiscard]] ove_thread_t
handle() const noexcept
94 ove_thread_t handle_ =
nullptr;
131 constexpr explicit stop_source(ove_thread_t h) noexcept : handle_(h)
146 if (handle_ ==
nullptr)
148 const bool was_stopped = ove_thread_should_stop(handle_);
150 ove_thread_request_stop(handle_);
157 return handle_ !=
nullptr && ove_thread_should_stop(handle_);
163 return handle_ !=
nullptr;
170 explicit operator bool() const noexcept
182 [[nodiscard]] ove_thread_t
handle() const noexcept
187 friend constexpr bool operator==(
const stop_source &,
191 ove_thread_t handle_ =
nullptr;
206#ifndef CONFIG_OVE_ZERO_HEAP
223 std::invocable<F &, stop_token> && !std::convertible_to<F, void (*)(stop_token)>;
253 constexpr thread_id()
noexcept =
default;
254 constexpr explicit thread_id(ove_thread_t h) noexcept : handle_(h)
258 friend constexpr bool operator==(
const thread_id &,
const thread_id &)
noexcept =
default;
259 friend constexpr auto operator<=>(
const thread_id &,
const thread_id &)
noexcept =
default;
268 ove_thread_t handle_ =
nullptr;
287inline void stop_token_trampoline(
void *ctx)
289 static_assert(
sizeof(
void *) ==
sizeof(
void (*)(stop_token)),
290 "ove::stop_token trampoline requires function and data pointers "
291 "to share width — fix by adding a per-target trampoline shim");
292 auto user_fn =
reinterpret_cast<void (*)(stop_token)
>(ctx);
294 while ((self = ove_thread_get_self()) ==
nullptr) {
297 user_fn(stop_token{
self});
300#ifndef CONFIG_OVE_ZERO_HEAP
313template <
typename F>
inline void capturing_trampoline(
void *ctx)
315 std::unique_ptr<F> f{
static_cast<F *
>(ctx)};
317 while ((self = ove_thread_get_self()) ==
nullptr) {
320 (*f)(stop_token{
self});
342template <
size_t StackSize = 0>
class Thread
364 template <
typename F>
365 Thread(F entry,
void *ctx, ove_prio_t prio,
const char *name)
368#ifdef CONFIG_OVE_ZERO_HEAP
369 static_assert(StackSize > 0,
"StackSize must be > 0 in zero-heap mode");
370 int err = ove_thread_init(&handle_, &storage_, name, entry, ctx, prio, StackSize,
373 int err = ove_thread_create(&handle_, name, entry, ctx, prio, StackSize);
375 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
404 template <
typename F>
405 Thread(F entry, ove_prio_t prio,
const char *name)
409 void *ctx =
reinterpret_cast<void *
>(fn);
410#ifdef CONFIG_OVE_ZERO_HEAP
411 static_assert(StackSize > 0,
"StackSize must be > 0 in zero-heap mode");
412 int err = ove_thread_init(&handle_, &storage_, name, &detail::stop_token_trampoline,
413 ctx, prio, StackSize, stack_);
415 int err = ove_thread_create(&handle_, name, &detail::stop_token_trampoline, ctx,
418 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
421#ifndef CONFIG_OVE_ZERO_HEAP
459 template <
typename F>
460 Thread(F &&entry, ove_prio_t prio,
const char *name)
463 using FT = std::decay_t<F>;
464 FT *boxed =
new FT(std::forward<F>(entry));
465 int err = ove_thread_create(&handle_, name, &detail::capturing_trampoline<FT>,
466 boxed, prio, StackSize);
469 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
488 ove_thread_request_stop(handle_);
489#ifdef CONFIG_OVE_ZERO_HEAP
490 ove_thread_deinit(handle_);
492 ove_thread_destroy(handle_);
499#ifdef CONFIG_OVE_ZERO_HEAP
509 other.handle_ =
nullptr;
519 if (
this != &other) {
521 ove_thread_destroy(handle_);
522 handle_ = other.handle_;
523 other.handle_ =
nullptr;
555#ifdef CONFIG_OVE_ZERO_HEAP
556 ove_thread_deinit(handle_);
558 ove_thread_destroy(handle_);
563#ifdef CONFIG_OVE_ZERO_HEAP
609 ove_thread_set_priority(handle_, prio);
619 ove_thread_suspend(handle_);
627 ove_thread_resume(handle_);
636 return ove_thread_get_state(handle_);
645 return ove_thread_get_stack_usage(handle_);
657 struct ove_thread_stats stats {
659 const int rc = ove_thread_get_runtime_stats(handle_, &stats);
679 ove_thread_request_stop(handle_);
715 return handle_ !=
nullptr && ove_thread_should_stop(handle_);
724 return handle_ !=
nullptr;
737 return handle_ !=
nullptr;
761 ove_thread_t handle_ =
nullptr;
762#ifdef CONFIG_OVE_ZERO_HEAP
768 OVE_THREAD_STACK_MEMBER_(stack_, StackSize > 0 ? StackSize : 1);
769 ove_thread_storage_t storage_ = {};
794 struct ove_mem_stats ms {
796 const int rc = ove_sys_get_mem_stats(&ms);
798 stats.total = ms.total;
799 stats.free = ms.free;
800 stats.used = ms.used;
801 stats.peak_used = ms.peak_used;
844 const int rc = ove_thread_list(out, max, &n);
869 ove_thread_sleep_ms(ms);
880template <
typename Rep,
typename Period>
881inline void sleep_for(std::chrono::duration<Rep, Period> d)
noexcept
883 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
884 ove_thread_sleep_ms(ms > 0 ?
static_cast<uint32_t
>(ms) : 0u);
897inline ove_thread_t
self() noexcept
899 return ove_thread_get_self();
RAII wrapper around an oveRTOS thread (task).
Definition thread.hpp:343
Result< struct ove_thread_stats > get_runtime_stats() const noexcept
Retrieves runtime statistics for the thread.
Definition thread.hpp:655
void set_priority(ove_prio_t prio)
Changes the priority of the thread at runtime.
Definition thread.hpp:607
id get_id() const noexcept
Get the id for this thread. std::thread::get_id analog.
Definition thread.hpp:746
Thread & operator=(Thread &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition thread.hpp:517
Thread(F entry, void *ctx, ove_prio_t prio, const char *name)
Constructs and starts the thread.
Definition thread.hpp:365
bool joinable() const noexcept
std::thread::joinable analog — true if this wrapper is associated with an active kernel thread.
Definition thread.hpp:735
ove_thread_t handle() const
Returns the raw oveRTOS thread handle.
Definition thread.hpp:755
~Thread() noexcept
Destroys the thread wrapper, terminating and releasing the kernel thread.
Definition thread.hpp:484
void resume()
Resumes a previously suspended thread.
Definition thread.hpp:625
Thread(Thread &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition thread.hpp:507
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition thread.hpp:722
ove_thread_state_t get_state() const
Returns the current execution state of the thread.
Definition thread.hpp:634
void request_stop() noexcept
Requests the thread to stop cooperatively.
Definition thread.hpp:676
stop_source get_stop_source() const noexcept
Get a writable stop_source for this thread. Analog of std::jthread::get_stop_source.
Definition thread.hpp:707
size_t get_stack_usage() const
Returns the number of bytes used by the thread's stack so far.
Definition thread.hpp:643
stop_token get_stop_token() const noexcept
Get a token that observes this thread's cancellation flag.
Definition thread.hpp:689
void suspend()
Suspends execution of the thread.
Definition thread.hpp:617
bool stop_requested() const noexcept
Definition thread.hpp:713
void detach() noexcept
Release ownership of the kernel thread without waiting. Analog of std::thread::detach.
Definition thread.hpp:597
Thread(F entry, ove_prio_t prio, const char *name)
Cooperative-cancellation constructor — std::jthread analog.
Definition thread.hpp:405
void join() noexcept
Block the caller until the worker thread exits, then release the kernel handle. Analog of std::thread...
Definition thread.hpp:551
Thread(F &&entry, ove_prio_t prio, const char *name)
Cooperative-cancellation constructor for capturing entries. Heap-mode only. std::jthread-shaped.
Definition thread.hpp:460
Writable counterpart to stop_token. std::stop_source analog.
Definition thread.hpp:128
stop_token get_token() const noexcept
Definition thread.hpp:176
bool request_stop() noexcept
Set the stop flag on the associated thread.
Definition thread.hpp:144
bool stop_requested() const noexcept
Definition thread.hpp:155
ove_thread_t handle() const noexcept
Definition thread.hpp:182
bool stop_possible() const noexcept
Definition thread.hpp:161
Lightweight read-only handle to a thread's cooperative cancellation flag.
Definition thread.hpp:57
bool stop_requested() const noexcept
Definition thread.hpp:65
ove_thread_t handle() const noexcept
Definition thread.hpp:88
bool stop_possible() const noexcept
Definition thread.hpp:71
Opaque identity for an oveRTOS thread. std::thread::id analog.
Definition thread.hpp:251
ove_thread_t native_handle() const noexcept
Definition thread.hpp:262
Capturing callable invocable as void(stop_token).
Definition thread.hpp:222
Stateless callable invocable as void(stop_token).
Definition thread.hpp:204
Concept satisfied by any callable convertible to void(*)(void*).
Definition types.hpp:56
Free functions that operate on the currently running thread.
ove_thread_t self() noexcept
Definition thread.hpp:897
void yield() noexcept
Voluntarily relinquish the CPU to another ready thread of equal or higher priority....
Definition thread.hpp:891
thread_id get_id() noexcept
Definition thread.hpp:903
void sleep_ms(uint32_t ms) noexcept
Suspend the calling thread for at least ms milliseconds. Plain-millisecond analog of std::this_thread...
Definition thread.hpp:867
void sleep_for(std::chrono::duration< Rep, Period > d) noexcept
Suspend the calling thread for the given chrono duration. std::this_thread::sleep_for analog.
Definition thread.hpp:881
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
Result< MemStats > get_mem_stats() noexcept
Query system heap statistics.
Definition thread.hpp:791
Result< size_t > thread_list(ThreadInfo *out, size_t max) noexcept
List all threads in the system.
Definition thread.hpp:841
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
ove_thread_info ThreadInfo
Snapshot of a single thread.
Definition thread.hpp:823
System heap statistics snapshot.
Definition thread.hpp:779
size_t used
Definition thread.hpp:782
size_t total
Definition thread.hpp:780
size_t peak_used
Definition thread.hpp:783
size_t free
Definition thread.hpp:781
Common type definitions and concepts for the C++ wrapper layer.