oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
sync.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
27#ifndef OVE_SYNC_H
28#define OVE_SYNC_H
29
30#include "ove/types.h"
31#include "ove_config.h"
32#include "ove/storage.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38#ifdef CONFIG_OVE_SYNC
39
40/* =========================================================================
41 * Mutex — _init / _deinit (static storage)
42 * ========================================================================= */
43
56int ove_mutex_init(ove_mutex_t *mtx, ove_mutex_storage_t *storage);
57
70
71/* =========================================================================
72 * Semaphore — _init / _deinit (static storage)
73 * ========================================================================= */
74
89int ove_sem_init(ove_sem_t *sem, ove_sem_storage_t *storage,
90 unsigned int initial, unsigned int max);
91
102
103/* =========================================================================
104 * Binary event — _init / _deinit (static storage)
105 * ========================================================================= */
106
122int ove_event_init(ove_event_t *evt, ove_event_storage_t *storage);
123
134
135/* =========================================================================
136 * Recursive mutex — _init (static storage)
137 * ========================================================================= */
138
156 ove_mutex_storage_t *storage);
157
158/* =========================================================================
159 * Condition variable — _init / _deinit (static storage)
160 * ========================================================================= */
161
176 ove_condvar_storage_t *storage);
177
189
190/* =========================================================================
191 * Heap-gated _create / _destroy variants
192 * ========================================================================= */
193
194#ifdef OVE_HEAP_SYNC
195
207int ove_mutex_create(ove_mutex_t *mtx);
208
218void ove_mutex_destroy(ove_mutex_t mtx);
219
232int ove_sem_create(ove_sem_t *sem, unsigned int initial,
233 unsigned int max);
234
244void ove_sem_destroy(ove_sem_t sem);
245
256int ove_event_create(ove_event_t *evt);
257
267void ove_event_destroy(ove_event_t evt);
268
279int ove_recursive_mutex_create(ove_mutex_t *mtx);
280
291void ove_recursive_mutex_destroy(ove_mutex_t mtx);
292
303int ove_condvar_create(ove_condvar_t *cv);
304
315void ove_condvar_destroy(ove_condvar_t cv);
316
317#elif !defined(__ZIG_CIMPORT__) /* !OVE_HEAP_SYNC — zero-heap mode */
318
319/* Unified _create/_destroy macros — auto-generate static storage per call site.
320 * Each call site produces exactly one kernel object. Do not call in a loop
321 * to create multiple independent objects; use _init() with separate storage. */
322#define ove_mutex_create(phandle) \
323 ({ static ove_mutex_storage_t _ove_stor_; \
324 ove_mutex_init((phandle), &_ove_stor_); })
325#define ove_mutex_destroy(mtx) ove_mutex_deinit(mtx)
326
327#define ove_sem_create(psem, initial, max) \
328 ({ static ove_sem_storage_t _ove_stor_; \
329 ove_sem_init((psem), &_ove_stor_, (initial), (max)); })
330#define ove_sem_destroy(sem) ove_sem_deinit(sem)
331
332#define ove_event_create(pevt) \
333 ({ static ove_event_storage_t _ove_stor_; \
334 ove_event_init((pevt), &_ove_stor_); })
335#define ove_event_destroy(evt) ove_event_deinit(evt)
336
337#define ove_recursive_mutex_create(phandle) \
338 ({ static ove_mutex_storage_t _ove_stor_; \
339 ove_recursive_mutex_init((phandle), &_ove_stor_); })
340#define ove_recursive_mutex_destroy(mtx) ove_mutex_deinit(mtx)
341
342#define ove_condvar_create(pcv) \
343 ({ static ove_condvar_storage_t _ove_stor_; \
344 ove_condvar_init((pcv), &_ove_stor_); })
345#define ove_condvar_destroy(cv) ove_condvar_deinit(cv)
346
347#endif /* OVE_HEAP_SYNC */
348
349/* =========================================================================
350 * Operations — always available (when CONFIG_OVE_SYNC is set)
351 * ========================================================================= */
352
368int ove_mutex_lock(ove_mutex_t mtx, uint32_t timeout_ms);
369
380
396int ove_sem_take(ove_sem_t sem, uint32_t timeout_ms);
397
408
428int ove_event_wait(ove_event_t evt, uint32_t timeout_ms);
429
443
457
478int ove_recursive_mutex_lock(ove_mutex_t mtx, uint32_t timeout_ms);
479
493
516 uint32_t timeout_ms);
517
530
541
542#else /* !CONFIG_OVE_SYNC */
543
544static inline int ove_mutex_create(ove_mutex_t *m) { (void)m; return OVE_ERR_NOT_SUPPORTED; }
545static inline void ove_mutex_destroy(ove_mutex_t m) { (void)m; }
546static inline int ove_mutex_lock(ove_mutex_t m, uint32_t t) { (void)m; (void)t; return OVE_ERR_NOT_SUPPORTED; }
547static inline void ove_mutex_unlock(ove_mutex_t m) { (void)m; }
548static inline int ove_sem_create(ove_sem_t *s, unsigned int i, unsigned int x) { (void)s; (void)i; (void)x; return OVE_ERR_NOT_SUPPORTED; }
549static inline void ove_sem_destroy(ove_sem_t s) { (void)s; }
550static inline int ove_sem_take(ove_sem_t s, uint32_t t) { (void)s; (void)t; return OVE_ERR_NOT_SUPPORTED; }
551static inline void ove_sem_give(ove_sem_t s) { (void)s; }
552static inline int ove_event_create(ove_event_t *e) { (void)e; return OVE_ERR_NOT_SUPPORTED; }
553static inline void ove_event_destroy(ove_event_t e) { (void)e; }
554static inline int ove_event_wait(ove_event_t e, uint32_t t) { (void)e; (void)t; return OVE_ERR_NOT_SUPPORTED; }
555static inline void ove_event_signal(ove_event_t e) { (void)e; }
556static inline void ove_event_signal_from_isr(ove_event_t e) { (void)e; }
557static inline int ove_recursive_mutex_create(ove_mutex_t *m) { (void)m; return OVE_ERR_NOT_SUPPORTED; }
558static inline int ove_recursive_mutex_lock(ove_mutex_t m, uint32_t t) { (void)m; (void)t; return OVE_ERR_NOT_SUPPORTED; }
559static inline void ove_recursive_mutex_unlock(ove_mutex_t m) { (void)m; }
560static inline void ove_recursive_mutex_destroy(ove_mutex_t m) { (void)m; }
561static inline int ove_condvar_create(ove_condvar_t *c) { (void)c; return OVE_ERR_NOT_SUPPORTED; }
562static inline void ove_condvar_destroy(ove_condvar_t c) { (void)c; }
563static inline int ove_condvar_wait(ove_condvar_t c, ove_mutex_t m, uint32_t t) { (void)c; (void)m; (void)t; return OVE_ERR_NOT_SUPPORTED; }
564static inline void ove_condvar_signal(ove_condvar_t c) { (void)c; }
565static inline void ove_condvar_broadcast(ove_condvar_t c) { (void)c; }
566
567#endif /* CONFIG_OVE_SYNC */
568
569#ifdef __cplusplus
570}
571#endif
572
573#endif /* OVE_SYNC_H */
574
int ove_condvar_init(ove_condvar_t *cv, ove_condvar_storage_t *storage)
Initialise a condition variable using caller-supplied static storage.
int ove_condvar_wait(ove_condvar_t cv, ove_mutex_t mtx, uint32_t timeout_ms)
Atomically release a mutex and wait on a condition variable.
void ove_condvar_signal(ove_condvar_t cv)
Wake one thread waiting on a condition variable.
void ove_event_deinit(ove_event_t evt)
Release resources held by an event initialised with ove_event_init().
void ove_condvar_deinit(ove_condvar_t cv)
Release resources held by a condition variable initialised with ove_condvar_init().
void ove_sem_deinit(ove_sem_t sem)
Release resources held by a semaphore initialised with ove_sem_init().
int ove_recursive_mutex_init(ove_mutex_t *mtx, ove_mutex_storage_t *storage)
Initialise a recursive mutex using caller-supplied static storage.
void ove_mutex_deinit(ove_mutex_t mtx)
Release resources held by a mutex initialised with ove_mutex_init().
int ove_event_wait(ove_event_t evt, uint32_t timeout_ms)
Wait for a binary event to be signalled.
void ove_recursive_mutex_unlock(ove_mutex_t mtx)
Release one level of a recursive mutex lock.
int ove_recursive_mutex_lock(ove_mutex_t mtx, uint32_t timeout_ms)
Acquire a recursive mutex, blocking until it is available or the timeout expires.
int ove_event_init(ove_event_t *evt, ove_event_storage_t *storage)
Initialise a binary event object using caller-supplied static storage.
void ove_condvar_broadcast(ove_condvar_t cv)
Wake all threads waiting on a condition variable.
int ove_sem_init(ove_sem_t *sem, ove_sem_storage_t *storage, unsigned int initial, unsigned int max)
Initialise a counting semaphore using caller-supplied static storage.
int ove_mutex_lock(ove_mutex_t mtx, uint32_t timeout_ms)
Acquire a non-recursive mutex, blocking until it is available or the timeout expires.
int ove_mutex_init(ove_mutex_t *mtx, ove_mutex_storage_t *storage)
Initialise a non-recursive mutex using caller-supplied static storage.
void ove_mutex_unlock(ove_mutex_t mtx)
Release a non-recursive mutex previously acquired by ove_mutex_lock().
int ove_sem_take(ove_sem_t sem, uint32_t timeout_ms)
Decrement (take) a semaphore, blocking until a count is available or the timeout expires.
void ove_event_signal_from_isr(ove_event_t evt)
Signal a binary event from an interrupt service routine.
void ove_sem_give(ove_sem_t sem)
Increment (give) a semaphore, potentially unblocking a waiting thread.
void ove_event_signal(ove_event_t evt)
Signal a binary event, unblocking one waiting thread.
#define OVE_ERR_NOT_SUPPORTED
The requested feature is not supported by the active backend.
Definition types.h:38
struct ove_event * ove_event_t
Opaque handle for a binary event (signal/wait) object.
Definition types.h:91
struct ove_sem * ove_sem_t
Opaque handle for a counting semaphore object.
Definition types.h:88
struct ove_condvar * ove_condvar_t
Opaque handle for a condition variable object.
Definition types.h:94
struct ove_mutex * ove_mutex_t
Opaque handle for a mutex object.
Definition types.h:85