oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
thread.h
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
24#ifndef OVE_THREAD_H
25#define OVE_THREAD_H
26
27#include "ove/types.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
39typedef void (*ove_thread_fn)(void *arg);
40
52
57 uint64_t runtime_us;
59};
60
77
90
91#include "ove/storage.h"
92
109 ove_thread_storage_t *storage,
110 const struct ove_thread_desc *desc);
111
124
125/* _create / _destroy — unified across heap and zero-heap modes */
126#ifdef OVE_HEAP_THREAD
127
142 const struct ove_thread_desc *desc);
143
153
167#define ove_thread_create(phandle, stack_sz, pdesc) \
168 ({ struct ove_thread_desc _ove_d_ = *(pdesc); \
169 _ove_d_.stack_size = (stack_sz); \
170 ove_thread_create_((phandle), &_ove_d_); })
171
172#elif !defined(__ZIG_CIMPORT__) /* !OVE_HEAP_THREAD — zero-heap mode */
173
186#define ove_thread_create(phandle, stack_sz, pdesc) \
187 ({ static ove_thread_storage_t _ove_stor_; \
188 OVE_THREAD_STACK_BLOCK_STATIC_(_ove_stk_, stack_sz); \
189 struct ove_thread_desc _ove_d_ = *(pdesc); \
190 _ove_d_.stack_size = (stack_sz); \
191 _ove_d_.stack = _ove_stk_; \
192 ove_thread_init((phandle), &_ove_stor_, &_ove_d_); })
193
194#define ove_thread_destroy(handle) ove_thread_deinit(handle)
195
196#endif /* OVE_HEAP_THREAD */
197
204
212
219void ove_thread_sleep_ms(uint32_t ms);
220
227
238
249
259
268
276
290 struct ove_thread_stats *stats);
291
292/* ── System memory statistics ──────────────────────────────── */
293
298 size_t total;
299 size_t free;
300 size_t used;
301 size_t peak_used;
302};
303
311
312/* ── Thread enumeration ────────────────────────────────────── */
313
323
332int ove_thread_list(struct ove_thread_info *out, size_t max_count,
333 size_t *actual_count);
334
335#ifdef __cplusplus
336}
337#endif
338
339#endif /* OVE_THREAD_H */
340
void ove_thread_sleep_ms(uint32_t ms)
Block the calling thread for at least ms milliseconds.
void ove_thread_resume(ove_thread_t handle)
Resume a previously suspended thread.
void ove_thread_set_priority(ove_thread_t handle, ove_prio_t prio)
Change the scheduling priority of a thread.
void ove_thread_yield(void)
Voluntarily yield the CPU to another ready thread of equal or higher priority.
int ove_sys_get_mem_stats(struct ove_mem_stats *stats)
Query system heap statistics.
int ove_thread_deinit(ove_thread_t handle)
Terminate and release a thread created with ove_thread_init().
void ove_thread_suspend(ove_thread_t handle)
Suspend a thread, preventing it from being scheduled.
size_t ove_thread_get_stack_usage(ove_thread_t handle)
Query how many bytes of stack the thread has used at its high-water mark.
int ove_thread_create_(ove_thread_t *handle, const struct ove_thread_desc *desc)
Internal heap-backed thread creation function.
int ove_thread_init(ove_thread_t *handle, ove_thread_storage_t *storage, const struct ove_thread_desc *desc)
Initialise a thread using caller-supplied static storage.
ove_thread_state_t ove_thread_get_state(ove_thread_t handle)
Query the current execution state of a thread.
ove_thread_t ove_thread_get_self(void)
Return the handle of the currently executing thread.
ove_thread_state_t
Thread execution state as reported by the active backend.
Definition thread.h:44
int ove_thread_destroy(ove_thread_t handle)
Stop and free a thread created with ove_thread_create().
void ove_thread_start_scheduler(void)
Start the RTOS scheduler.
ove_prio_t
Portable thread-priority levels.
Definition thread.h:67
int ove_thread_get_runtime_stats(ove_thread_t handle, struct ove_thread_stats *stats)
Retrieve runtime statistics for a thread.
void(* ove_thread_fn)(void *arg)
Thread entry-point function prototype.
Definition thread.h:39
int ove_thread_list(struct ove_thread_info *out, size_t max_count, size_t *actual_count)
List all threads in the system.
@ OVE_THREAD_STATE_UNKNOWN
State could not be determined.
Definition thread.h:50
@ OVE_THREAD_STATE_BLOCKED
Blocked on a synchronisation object or delay.
Definition thread.h:47
@ OVE_THREAD_STATE_RUNNING
Currently executing on the CPU.
Definition thread.h:45
@ OVE_THREAD_STATE_TERMINATED
Entry function has returned; not yet destroyed.
Definition thread.h:49
@ OVE_THREAD_STATE_READY
Ready to run, waiting for the CPU.
Definition thread.h:46
@ OVE_THREAD_STATE_SUSPENDED
Explicitly suspended via ove_thread_suspend().
Definition thread.h:48
@ OVE_PRIO_HIGH
High priority; prefer for time-sensitive tasks.
Definition thread.h:73
@ OVE_PRIO_NORMAL
Default application priority.
Definition thread.h:71
@ OVE_PRIO_BELOW_NORMAL
Below-normal priority.
Definition thread.h:70
@ OVE_PRIO_REALTIME
Real-time priority; use with care.
Definition thread.h:74
@ OVE_PRIO_ABOVE_NORMAL
Above-normal priority.
Definition thread.h:72
@ OVE_PRIO_LOW
Low priority background work.
Definition thread.h:69
@ OVE_PRIO_IDLE
Lowest priority; runs only when no other thread is ready.
Definition thread.h:68
@ OVE_PRIO_CRITICAL
Highest priority; reserved for critical system tasks.
Definition thread.h:75
struct ove_thread * ove_thread_t
Opaque handle for a thread object.
Definition types.h:82
System heap statistics.
Definition thread.h:297
size_t total
Definition thread.h:298
size_t used
Definition thread.h:300
size_t free
Definition thread.h:299
size_t peak_used
Definition thread.h:301
Thread creation descriptor passed to ove_thread_init() / ove_thread_create().
Definition thread.h:81
void * arg
Opaque argument forwarded to entry. May be NULL.
Definition thread.h:84
ove_thread_fn entry
Thread entry-point function. Must not be NULL.
Definition thread.h:83
void * stack
Pointer to caller-allocated stack buffer (static mode only; set to NULL for heap mode).
Definition thread.h:87
size_t stack_size
Stack size in bytes. Must be > 0.
Definition thread.h:86
const char * name
Human-readable thread name (may be truncated by backend).
Definition thread.h:82
ove_prio_t priority
Scheduling priority.
Definition thread.h:85
Snapshot of a single thread's info.
Definition thread.h:317
const char * name
Definition thread.h:318
int priority
Definition thread.h:320
size_t stack_used
Definition thread.h:321
ove_thread_state_t state
Definition thread.h:319
Per-thread runtime statistics snapshot.
Definition thread.h:56
uint32_t cpu_percent_x100
CPU utilisation in hundredths of a percent (e.g. 1250 = 12.50 %).
Definition thread.h:58
uint64_t runtime_us
Total CPU time consumed by this thread in microseconds.
Definition thread.h:57