SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_c.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_C_H_
11#define STK_C_H_
12
13#ifdef __cplusplus
14 #include <cstdint>
15 #include <cstddef>
16 #include <cstdbool>
17 #include <cassert>
18#else
19 #include <stdint.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <assert.h>
23#endif
24
25#include <stk_config.h>
26
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
45#define STK_C_VERSION (0x20261004)
46
47// =============================================================================
48// Configuration macros (can be overridden before including this file)
49// =============================================================================
50
56#ifndef STK_C_KERNEL_MAX_TASKS
57 #define STK_C_KERNEL_MAX_TASKS (4)
58#endif
59
64#ifndef STK_C_CPU_COUNT
65 #define STK_C_CPU_COUNT (1)
66#endif
67
71#if !defined(STK_SYNC_DEBUG_NAMES) && STK_SEGGER_SYSVIEW
72 #define STK_SYNC_DEBUG_NAMES (1)
73#elif !defined(STK_SYNC_DEBUG_NAMES)
74 #define STK_SYNC_DEBUG_NAMES (0)
75#endif
76
80#define STK_C_ASSERT(e) assert(e)
81
87#ifdef __cplusplus
88 #define STK_STATIC_CAST(type, val) static_cast<type>(val)
89#else
90 #define STK_STATIC_CAST(type, val) ((type)(val))
91#endif
92
93// =============================================================================
94// Types
95// =============================================================================
96
99typedef uintptr_t stk_word_t;
100
104
108typedef int64_t stk_tick_t;
109
113typedef int64_t stk_time_t;
114
118typedef int32_t stk_timeout_t;
119
123typedef uint64_t stk_cycle_t;
124
127typedef int32_t stk_weight_t;
128
132
135typedef struct stk_task_t stk_task_t;
136
139#define STK_PERIODICITY_DEFAULT (1000U)
140
148typedef void (*stk_task_entry_t)(void *arg);
149
159
162#define STK_WAIT_INFINITE (STK_STATIC_CAST(stk_timeout_t, INT32_MAX))
163
166#define STK_NO_WAIT (STK_STATIC_CAST(stk_timeout_t, 0))
167
170#define STK_ALIGN_SIZE sizeof(stk_word_t)
171
174#define STK_ALIGN_MASK (STK_ALIGN_SIZE - 1U)
175
179#ifndef STK_STACK_MEMORY_ALIGN
180 #if defined(__riscv)
181 #define STK_STACK_MEMORY_ALIGN (16U)
182 #elif defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
183 #define STK_STACK_MEMORY_ALIGN (8U)
184 #else // ARM, others
185 #define STK_STACK_MEMORY_ALIGN (4U)
186 #endif
187#endif
188
196#define STK_DEFINE_STACK_POOL(name, max_tasks, stack_size) \
197 static stk_word_t name[max_tasks][stack_size] __stk_c_stack
198
205#define STK_GET_STACK_FROM_POOL(name, task_id) (name[task_id])
206
207// =============================================================================
208// Attributes
209// =============================================================================
210
214#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
215 #define __stk_c_stack __attribute__((aligned(STK_STACK_MEMORY_ALIGN)))
216#else
217 #define __stk_c_stack
218#endif
219
223#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
224 #define __stk_c_aligned __attribute__((aligned(STK_ALIGN_SIZE)))
225#else
226 #define __stk_c_aligned
227#endif
228
229// =============================================================================
230// Kernel factory functions
231// =============================================================================
232
233/* Available kernel type definitions:
234
235 Kernel mode flags (may be OR-combined, subject to the constraints listed below):
236 KERNEL_STATIC - fixed task list; tasks must never return from their entry function.
237 KERNEL_DYNAMIC - tasks may be added/removed at runtime and may return when done.
238 KERNEL_HRT - Hard Real-Time mode; must be combined with KERNEL_STATIC or KERNEL_DYNAMIC.
239 KERNEL_SYNC - enables synchronization primitives (Mutex, Event, Semaphore, etc.).
240 KERNEL_TICKLESS - tickless low-power idle; suppresses the SysTick when all tasks sleep.
241 Requires STK_TICKLESS_IDLE=1 in stk_config.h.
242 INCOMPATIBLE with KERNEL_HRT (HRT requires a continuous tick).
243
244// Standard variants
245Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
246Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
247Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
248Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
249Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
250Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
251
252// HRT variants (KERNEL_TICKLESS must NOT be combined with any of these)
253Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
254Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
255Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
256Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
257Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
258Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
259Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
260Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
261
262// Standard variants with KERNEL_SYNC
263Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
264Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
265Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
266Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
267Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
268Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
269
270// HRT variants with KERNEL_SYNC
271Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
272Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
273Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
274Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
275Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
276Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
277Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
278Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
279
280// Tickless variants (low-power; KERNEL_TICKLESS requires STK_TICKLESS_IDLE=1 in stk_config.h)
281Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
282Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
283Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
284Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
285Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
286Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
287
288// Tickless variants with KERNEL_SYNC
289Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
290Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
291Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
292Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
293Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
294Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
295*/
296
320
327stk_kernel_t *stk_kernel_create(uint8_t core_nr);
328
329#ifdef __cplusplus
330namespace stk { class IKernel; }
331
341extern "C" stk::IKernel *stk_kernel_get_instance(uint8_t core_nr);
342#endif // __cplusplus
343
344// =============================================================================
345// Kernel control
346// =============================================================================
347
353void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us);
354
361
371 stk_task_t *tsk,
372 int32_t periodicity_ticks,
373 int32_t deadline_ticks,
374 int32_t start_delay_ticks);
375
382
389
400
406
413
420
430
439void stk_kernel_suspend_task(stk_kernel_t *k, stk_task_t *task, bool *suspended);
440
446
457
466void stk_kernel_resume(stk_kernel_t *k, stk_timeout_t elapsed_ticks);
467
476size_t stk_kernel_enumerate_tasks(stk_kernel_t *k, stk_task_t **tasks, size_t max_count);
477
487
496
497// =============================================================================
498// Platform event overrider
499// =============================================================================
500
520{
528 bool (*on_sleep)(stk_timeout_t sleep_ticks, void *user_data);
529
536 bool (*on_hard_fault)(void *user_data);
537
543
557
558// =============================================================================
559// Tasks
560// =============================================================================
561
570 void *arg,
571 stk_word_t *stack,
572 uint32_t stack_size);
573
582 void *arg,
583 stk_word_t *stack,
584 uint32_t stack_size);
585
593
599void stk_task_set_priority(stk_task_t *tsk, uint8_t priority);
600
605void stk_task_set_name(stk_task_t *tsk, const char *tname);
606
612const char *stk_task_get_name(const stk_task_t *tsk);
613
622
623#ifdef __cplusplus
624namespace stk { class ITask; }
625
637#endif // __cplusplus
638
639// =============================================================================
640// Services available from inside tasks
641// =============================================================================
642
646stk_tid_t stk_tid(void);
647
652
656uint32_t stk_tick_resolution(void);
657
666
681
690static inline stk_tick_t stk_ticks_from_ms_r(stk_time_t msec, uint32_t resolution)
691{
692 stk_time_t result = 0LL;
693
694 if (resolution != 0U)
695 {
696 const stk_time_t total_scaled = msec * 1000LL;
697 result = total_scaled / STK_STATIC_CAST(stk_time_t, resolution);
698 }
699
700 return STK_STATIC_CAST(stk_tick_t, result);
701}
702
707
714static inline stk_time_t stk_ms_from_ticks_r(stk_tick_t ticks, uint32_t resolution)
715{
716 const stk_tick_t total_ticks = ticks * STK_STATIC_CAST(stk_tick_t, resolution);
717 return STK_STATIC_CAST(stk_time_t, total_ticks / 1000LL);
718}
719
726{
728}
729
735
741uint32_t stk_sys_timer_frequency(void);
742
743// =============================================================================
744// High-Resolution Clock (see hw::HiResClock)
745// =============================================================================
746
752
757uint32_t stk_hires_frequency(void);
758
764
768void stk_delay(stk_timeout_t ticks);
769
774
785void stk_sleep(stk_timeout_t ticks);
786
798
811
814void stk_yield(void);
815
824
825// =============================================================================
826// Dynamic cleanup
827// =============================================================================
828
835
842
843// =============================================================================
844// Thread-Local Storage (TLS)
845// =============================================================================
846
863
867void *stk_tls_get(void);
868
872void stk_tls_set(void *ptr);
873
877#define STK_TLS_GET_T(type) ((type *)stk_tls_get())
878
882#define STK_TLS_SET_T(ptr) stk_tls_set((void *)(ptr))
883
884// =============================================================================
885// Synchronization Primitives
886// =============================================================================
887
888// ----- Critical Section ------------------------------------------------------
889
897typedef uint8_t stk_cs_session_t;
898
902#define STK_DEFAULT_CS_SESSION (0U)
903
919stk_cs_session_t stk_critical_section_enter_ex(stk_cs_session_t ses /*= STK_DEFAULT_CS_SESSION*/);
920
928void stk_critical_section_exit_ex(stk_cs_session_t ses /*= STK_DEFAULT_CS_SESSION*/);
929
943
952
953// ----- Mutex -----------------------------------------------------------------
954
957#define STK_MUTEX_IMPL_SIZE (10U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
958
964
968
974
979
983void stk_mutex_lock(stk_mutex_t *mtx);
984
990
995
1005
1021
1022#ifdef __cplusplus
1023namespace stk { namespace sync { class Mutex; } }
1024
1031#endif // __cplusplus
1032
1033// ----- FastMutex -------------------------------------------------------------
1034
1037#define STK_FASTMUTEX_IMPL_SIZE (9U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1038
1044
1052
1058
1064
1070
1077
1083
1094
1111
1112#ifdef __cplusplus
1113namespace stk { namespace sync { class FastMutex; } }
1114
1121#endif // __cplusplus
1122
1123// ----- SpinLock --------------------------------------------------------------
1124
1127#define STK_SPINLOCK_IMPL_SIZE (6)
1128
1135
1139
1145
1149
1153
1158
1162
1163#ifdef __cplusplus
1164namespace stk { namespace sync { class SpinLock; } }
1165
1172#endif // __cplusplus
1173
1174// ----- Condition Variable ----------------------------------------------------
1175
1178#define STK_CV_IMPL_SIZE (7U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1179
1185
1188typedef struct stk_cv_t stk_cv_t;
1189
1194stk_cv_t *stk_cv_create(stk_cv_mem_t *const membuf);
1195
1199void stk_cv_destroy(stk_cv_t *cv);
1200
1215bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout);
1216
1232
1250
1263
1267void stk_cv_notify_one(stk_cv_t *cv);
1268
1272void stk_cv_notify_all(stk_cv_t *cv);
1273
1274#ifdef __cplusplus
1275namespace stk { namespace sync { class ConditionVariable; } }
1276
1283#endif // __cplusplus
1284
1285// ----- Event -----------------------------------------------------------------
1286
1289#define STK_EVENT_IMPL_SIZE (8U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1290
1296
1300
1307 bool manual_reset);
1308
1313
1322bool stk_event_wait(stk_event_t *ev, stk_timeout_t timeout);
1323
1339
1345
1351bool stk_event_set(stk_event_t *ev);
1352
1359
1364
1365#ifdef __cplusplus
1366namespace stk { namespace sync { class Event; } }
1367
1374#endif // __cplusplus
1375
1376// ----- Semaphore -------------------------------------------------------------
1377
1380#define STK_SEM_IMPL_SIZE (8U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1381
1387
1390typedef struct stk_sem_t stk_sem_t;
1391
1400 uint32_t initial_count,
1401 uint32_t max_count);
1402
1406void stk_sem_destroy(stk_sem_t *sem);
1407
1416bool stk_sem_wait(stk_sem_t *sem, stk_timeout_t timeout);
1417
1432
1440bool stk_sem_trywait(stk_sem_t *sem);
1441
1450void stk_sem_signal(stk_sem_t *sem);
1451
1464bool stk_sem_trysignal(stk_sem_t *sem);
1465
1472uint16_t stk_sem_get_count(const stk_sem_t *sem);
1473
1474#ifdef __cplusplus
1475namespace stk { namespace sync { class Semaphore; } }
1476
1483#endif // __cplusplus
1484
1485// ----- EventFlags ------------------------------------------------------------
1486
1489#define STK_EF_OPT_WAIT_ANY (0x00000000U)
1490#define STK_EF_OPT_WAIT_ALL (0x00000001U)
1491#define STK_EF_OPT_NO_CLEAR (0x00000002U)
1492
1495#define STK_EF_ERROR_PARAMETER (0x80000001U)
1496#define STK_EF_ERROR_TIMEOUT (0x80000002U)
1497#define STK_EF_ERROR_ISR (0x80000004U)
1498#define STK_EF_ERROR_CANCELED (0x80000008U)
1499#define STK_EF_ERROR_MASK (0x80000000U)
1500
1504static inline bool stk_ef_is_error(uint32_t result)
1505{
1506 return ((result & STK_EF_ERROR_MASK) != 0U);
1507}
1508
1513#define STK_EF_IMPL_SIZE (STK_CV_IMPL_SIZE + 1U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1514
1520
1523typedef struct stk_ef_t stk_ef_t;
1524
1531stk_ef_t *stk_ef_create(stk_ef_mem_t *const membuf,
1532 uint32_t initial_flags);
1533
1537void stk_ef_destroy(stk_ef_t *ef);
1538
1547uint32_t stk_ef_set(stk_ef_t *ef, uint32_t flags);
1548
1556uint32_t stk_ef_clear(stk_ef_t *ef, uint32_t flags);
1557
1563uint32_t stk_ef_get(stk_ef_t *ef);
1564
1587uint32_t stk_ef_wait(stk_ef_t *ef, uint32_t flags, uint32_t options, stk_timeout_t timeout);
1588
1600uint32_t stk_ef_trywait(stk_ef_t *ef, uint32_t flags, uint32_t options);
1601
1602#ifdef __cplusplus
1603namespace stk { namespace sync { class EventFlags; } }
1604
1611#endif // __cplusplus
1612
1613// ----- Pipe (FIFO) -----------------------------------------------------------
1614
1623#define STK_PIPE_IMPL_SIZE (6U + (2U * STK_CV_IMPL_SIZE) + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1624
1630
1633typedef struct stk_pipe_t stk_pipe_t;
1634
1652#define STK_PIPE_BUF_SIZE(capacity, element_size) \
1653 ((((capacity) * (element_size)) + STK_ALIGN_MASK) & ~STK_ALIGN_MASK)
1654
1673 uint8_t *buf,
1674 uint32_t buf_size,
1675 size_t capacity,
1676 size_t element_size);
1677
1683void stk_pipe_destroy(stk_pipe_t *pipe);
1684
1699bool stk_pipe_write(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout);
1700
1714stk_wait_result_t stk_pipe_write_ex(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout);
1715
1722bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data);
1723
1738bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout);
1739
1754
1761bool stk_pipe_tryread(stk_pipe_t *pipe, void *data);
1762
1779size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const void *src, size_t count, stk_timeout_t timeout);
1780
1799stk_wait_result_t stk_pipe_write_bulk_ex(stk_pipe_t *pipe, const void *src, size_t count,
1800 size_t *written, stk_timeout_t timeout);
1801
1810size_t stk_pipe_trywrite_bulk(stk_pipe_t *pipe, const void *src, size_t count);
1811
1827size_t stk_pipe_read_bulk(stk_pipe_t *pipe, void *dst, size_t count, stk_timeout_t timeout);
1828
1847stk_wait_result_t stk_pipe_read_bulk_ex(stk_pipe_t *pipe, void *dst, size_t count,
1848 size_t *read_count, stk_timeout_t timeout);
1849
1858size_t stk_pipe_tryread_bulk(stk_pipe_t *pipe, void *dst, size_t count);
1859
1880 void *dst,
1881 size_t trigger,
1882 size_t max_count,
1883 stk_timeout_t timeout);
1884
1907 void *dst,
1908 size_t trigger,
1909 size_t max_count,
1910 size_t *read_count,
1911 stk_timeout_t timeout);
1912
1922size_t stk_pipe_tryread_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t max_count);
1923
1930void stk_pipe_reset(stk_pipe_t *pipe);
1931
1937size_t stk_pipe_get_capacity(const stk_pipe_t *pipe);
1938
1944size_t stk_pipe_get_element_size(const stk_pipe_t *pipe);
1945
1951size_t stk_pipe_get_count(const stk_pipe_t *pipe);
1952
1958size_t stk_pipe_get_space(const stk_pipe_t *pipe);
1959
1965bool stk_pipe_is_empty(const stk_pipe_t *pipe);
1966
1972bool stk_pipe_is_full(const stk_pipe_t *pipe);
1973
1979bool stk_pipe_is_storage_valid(const stk_pipe_t *pipe);
1980
1981#ifdef __cplusplus
1982namespace stk { namespace sync { class Pipe; } }
1983
1990#endif // __cplusplus
1991
1992// ----- MessageQueue ----------------------------------------------------------
1993
2002#define STK_MSGQ_IMPL_SIZE (6U + (2U * STK_CV_IMPL_SIZE) + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
2003
2009
2012typedef struct stk_msgq_t stk_msgq_t;
2013
2031#define STK_MSGQ_BUF_SIZE(capacity, msg_size) ((capacity) * (msg_size))
2032
2053 uint8_t *buf,
2054 uint32_t buf_size,
2055 size_t capacity,
2056 size_t msg_size);
2057
2064
2079bool stk_msgq_put(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
2080
2096stk_wait_result_t stk_msgq_put_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
2097
2104bool stk_msgq_tryput(stk_msgq_t *mq, const void *msg);
2105
2121bool stk_msgq_putfront(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
2122
2138stk_wait_result_t stk_msgq_putfront_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
2139
2146bool stk_msgq_tryputfront(stk_msgq_t *mq, const void *msg);
2147
2161bool stk_msgq_get(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
2162
2179
2186bool stk_msgq_tryget(stk_msgq_t *mq, void *msg);
2187
2204bool stk_msgq_peek(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
2205
2222
2229bool stk_msgq_trypeek(stk_msgq_t *mq, void *msg);
2230
2246bool stk_msgq_peekfront(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
2247
2264
2271bool stk_msgq_trypeekfront(stk_msgq_t *mq, void *msg);
2272
2280void stk_msgq_reset(stk_msgq_t *mq);
2281
2287size_t stk_msgq_get_capacity(const stk_msgq_t *mq);
2288
2294size_t stk_msgq_get_msg_size(const stk_msgq_t *mq);
2295
2301size_t stk_msgq_get_count(const stk_msgq_t *mq);
2302
2308size_t stk_msgq_get_space(const stk_msgq_t *mq);
2309
2315bool stk_msgq_is_empty(const stk_msgq_t *mq);
2316
2323uint8_t *stk_msgq_get_buffer(stk_msgq_t *mq);
2324
2330bool stk_msgq_is_full(const stk_msgq_t *mq);
2331
2342
2343#ifdef __cplusplus
2344namespace stk { namespace sync { class MessageQueue; } }
2345
2352#endif // __cplusplus
2353
2354// ----- RWMutex (Reader-Writer Lock) ------------------------------------------
2355
2358#define STK_RWMUTEX_IMPL_SIZE (17U + (STK_SYNC_DEBUG_NAMES ? 3U : 0U))
2359
2365
2369
2375
2380
2387
2393
2404
2420
2426
2433
2439
2450
2466
2473
2474#ifdef __cplusplus
2475namespace stk { namespace sync { class RWMutex; } }
2476
2483#endif // __cplusplus
2484
2485// ----- Barrier -----------------------------------------------------------------
2486
2492#define STK_BARRIER_IMPL_SIZE (STK_MUTEX_IMPL_SIZE + STK_CV_IMPL_SIZE + 4U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
2493
2499
2503
2510stk_barrier_t *stk_barrier_create(stk_barrier_mem_t *const membuf, uint32_t count);
2511
2515void stk_barrier_destroy(stk_barrier_t *barrier);
2516
2526
2537bool stk_barrier_wait(stk_barrier_t *barrier);
2538
2548
2554uint32_t stk_barrier_get_threshold(const stk_barrier_t *barrier);
2555
2556#ifdef __cplusplus
2557namespace stk { namespace sync { class Barrier; } }
2558
2565#endif // __cplusplus
2566
2567#ifdef __cplusplus
2568}
2569#endif
2570
2571#ifdef __cplusplus
2572namespace stk {
2580template <typename T, typename Mem, typename... Args>
2581static inline T *ConstructIn(Mem &mem, Args &&... args)
2582{
2583 STK_STATIC_ASSERT_DESC((sizeof(T) <= sizeof(Mem)),
2584 "stk_*_mem_t is too small for the C++ object");
2585 STK_STATIC_ASSERT_DESC((alignof(T) <= alignof(Mem)),
2586 "stk_*_mem_t is not sufficiently aligned for the C++ object");
2587
2588 STK_ASSERT((reinterpret_cast<uintptr_t>(mem.data) % alignof(T)) == 0U);
2589
2590 return new (static_cast<void *>(mem.data)) T(static_cast<Args &&>(args)...);
2591}
2592} // namespace stk
2593#endif // __cplusplus
2594
2596
2597#endif /* STK_C_H_ */
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:516
#define STK_STATIC_ASSERT_DESC(X, DESC)
Compile-time assertion with a custom error description. Produces a compilation error if X is false.
Definition stk_defs.h:536
@ Mutex
Mutex or recursive mutex (backed by stk::sync::FastMutex or stk::sync::Mutex).
stk::sync::Barrier * stk_barrier_get_instance(stk_barrier_t *barrier)
Get the underlying C++ stk::sync::Barrier object wrapped by a stk_barrier_t handle.
void stk_kernel_process_tick(stk_kernel_t *k)
Manually deliver one scheduler tick to the kernel.
Definition stk_c.cpp:515
stk::sync::EventFlags * stk_ef_get_instance(stk_ef_t *ef)
Get the underlying C++ stk::sync::EventFlags object wrapped by a stk_ef_t handle.
#define STK_FASTMUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for FastMutex instance.
Definition stk_c.h:1037
stk_task_t * stk_task_create_user(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size)
Create user-mode task.
Definition stk_c.cpp:567
stk::sync::FastMutex * stk_fastmutex_get_instance(stk_fastmutex_t *mtx)
Get the underlying C++ stk::sync::FastMutex object wrapped by a stk_fastmutex_t handle.
void stk_task_set_name(stk_task_t *tsk, const char *tname)
Assign human-readable task name (for tracing/debugging).
Definition stk_c.cpp:595
void stk_barrier_destroy(stk_barrier_t *barrier)
Destroy a Barrier.
void stk_sem_signal(stk_sem_t *sem)
Signal/Release a semaphore resource.
stk_cycle_t stk_hires_cycles(void)
Get raw CPU cycle counter.
Definition stk_c.cpp:641
size_t stk_pipe_get_capacity(const stk_pipe_t *pipe)
Get the maximum number of elements the pipe can hold.
void stk_msgq_reset(stk_msgq_t *mq)
Discard all messages and reset the queue to the empty state.
bool stk_msgq_is_full(const stk_msgq_t *mq)
Check whether the queue is currently full.
#define STK_STATIC_CAST(type, val)
Convenience wrapper for static_cast and C-style cast.
Definition stk_c.h:88
#define STK_BARRIER_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Barrier instance.
Definition stk_c.h:2492
stk_pipe_t * stk_pipe_create(stk_pipe_mem_t *const membuf, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t element_size)
Create a Pipe (using provided memory).
uint32_t stk_ef_set(stk_ef_t *ef, uint32_t flags)
Set one or more flags.
bool stk_event_set(stk_event_t *ev)
Set the event to signaled state.
stk_wait_result_t stk_rwmutex_timed_lock_ex(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the write lock with a timeout, preserving the full outcome.
uint32_t stk_ef_clear(stk_ef_t *ef, uint32_t flags)
Clear one or more flags.
void stk_mutex_destroy(stk_mutex_t *mtx)
Destroy a Mutex.
size_t stk_pipe_get_count(const stk_pipe_t *pipe)
Get the current number of elements in the pipe.
bool stk_sem_trysignal(stk_sem_t *sem)
Try to signal/release a semaphore resource without exceeding max_count.
bool stk_pipe_write(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout)
Write a single element to the pipe.
stk_tid_t stk_tid(void)
Returns current task/thread ID (the value set by stk_task_set_id).
Definition stk_c.cpp:633
size_t stk_pipe_read_bulk(stk_pipe_t *pipe, void *dst, size_t count, stk_timeout_t timeout)
Read multiple elements from the pipe.
size_t stk_pipe_trywrite_bulk(stk_pipe_t *pipe, const void *src, size_t count)
Attempt to write multiple elements to the pipe without blocking.
stk_wait_result_t stk_pipe_read_bulk_triggered_ex(stk_pipe_t *pipe, void *dst, size_t trigger, size_t max_count, size_t *read_count, stk_timeout_t timeout)
Read at least trigger elements, then drain up to max_count, preserving the full outcome.
bool stk_rwmutex_try_read_lock(stk_rwmutex_t *rw)
Try to acquire the read lock without blocking.
stk_tick_t stk_ticks_from_ms(stk_time_t msec)
Get ticks from milliseconds using current kernel tick resolution.
Definition stk_c.cpp:637
size_t stk_pipe_get_space(const stk_pipe_t *pipe)
Get the number of free slots currently available.
uint32_t stk_ef_wait(stk_ef_t *ef, uint32_t flags, uint32_t options, stk_timeout_t timeout)
Wait for one or more flags to be set.
void stk_ef_destroy(stk_ef_t *ef)
Destroy an EventFlags object.
void stk_kernel_remove_task(stk_kernel_t *k, stk_task_t *tsk)
Remove finished task from dynamic kernel.
Definition stk_c.cpp:436
void * stk_tls_get(void)
Get thread-local pointer (platform-specific slot).
bool stk_event_wait(stk_event_t *ev, stk_timeout_t timeout)
Wait for the event to become signaled.
stk_wait_result_t stk_msgq_put_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the queue, preserving the full outcome.
stk_wait_result_t
Outcome of a cancellable, timed kernel wait (see stk_cv_wait_ex(), stk_mutex_timed_lock_ex(),...
Definition stk_c.h:153
bool stk_rwmutex_trylock(stk_rwmutex_t *rw)
Try to acquire the write lock without blocking.
bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable.
uint32_t stk_ef_trywait(stk_ef_t *ef, uint32_t flags, uint32_t options)
Non-blocking flag poll.
stk_wait_result_t stk_msgq_peekfront_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the front-inserted message without removing it, preserving the full outcome.
uint8_t stk_cs_session_t
Session of Critical Section.
Definition stk_c.h:897
const char * stk_task_get_name(const stk_task_t *tsk)
Get human-readable task name previously set with stk_task_set_name().
Definition stk_c.cpp:602
#define STK_SEM_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Semaphore instance.
Definition stk_c.h:1380
void stk_spinlock_destroy(stk_spinlock_t *slock)
Destroy the SpinLock.
void stk_fastmutex_destroy(stk_fastmutex_t *mtx)
Destroy a FastMutex.
void stk_kernel_destroy(stk_kernel_t *k)
Destroy dynamic kernel instance (only when not running).
Definition stk_c.cpp:385
void stk_cv_notify_one(stk_cv_t *cv)
Wake one task waiting on the condition variable.
void stk_kernel_suspend_task(stk_kernel_t *k, stk_task_t *task, bool *suspended)
Suspend a task (prevent it from being scheduled).
Definition stk_c.cpp:460
bool stk_spinlock_trylock(stk_spinlock_t *slock)
Attempt to acquire the SpinLock immediately.
stk::IKernel * stk_kernel_get_instance(uint8_t core_nr)
Get the underlying C++ stk::IKernel object for a given core.
Definition stk_c.cpp:372
#define STK_EVENT_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Event instance.
Definition stk_c.h:1289
stk_spinlock_t * stk_spinlock_create(stk_spinlock_mem_t *const membuf)
Create a recursive SpinLock.
bool stk_msgq_tryget(stk_msgq_t *mq, void *msg)
Attempt to get a message from the queue without blocking.
bool stk_event_trywait(stk_event_t *ev)
Wait for the event to become signaled.
stk_wait_result_t stk_pipe_read_bulk_ex(stk_pipe_t *pipe, void *dst, size_t count, size_t *read_count, stk_timeout_t timeout)
Read multiple elements from the pipe, preserving the full outcome.
stk::sync::SpinLock * stk_spinlock_get_instance(stk_spinlock_t *slock)
Get the underlying C++ stk::sync::SpinLock object wrapped by a stk_spinlock_t handle.
size_t stk_msgq_get_space(const stk_msgq_t *mq)
Get the number of free slots currently available.
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:131
stk_wait_result_t stk_cv_wait_ex(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable, preserving the full outcome.
void stk_event_destroy(stk_event_t *ev)
Destroy an Event.
void stk_kernel_resume(stk_kernel_t *k, stk_timeout_t elapsed_ticks)
Resume scheduling after a prior stk_kernel_suspend() call.
Definition stk_c.cpp:508
stk_wait_result_t stk_pipe_read_ex(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe, preserving the full outcome.
stk_timeout_t stk_kernel_suspend(stk_kernel_t *k)
Suspend scheduling (tickless idle entry point).
Definition stk_c.cpp:501
bool stk_pipe_is_storage_valid(const stk_pipe_t *pipe)
Verify that the backing storage is valid and the pipe is ready for use.
uint16_t stk_sem_get_count(const stk_sem_t *sem)
Get the current counter value.
void stk_delay_ms(stk_timeout_t ms)
Busy-wait delay (other tasks continue to run).
Definition stk_c.cpp:646
void stk_yield(void)
Voluntarily give up CPU to another ready task (cooperative yield).
Definition stk_c.cpp:650
bool stk_msgq_trypeekfront(stk_msgq_t *mq, void *msg)
Attempt to peek at the front message without blocking.
uint32_t stk_hires_frequency(void)
Get CPU clock frequency in Hz.
Definition stk_c.cpp:642
stk::sync::Semaphore * stk_sem_get_instance(stk_sem_t *sem)
Get the underlying C++ stk::sync::Semaphore object wrapped by a stk_sem_t handle.
bool stk_mutex_timed_lock(stk_mutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout.
bool stk_msgq_peekfront(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the most recently front-inserted message without removing it.
void stk_kernel_resume_task(stk_kernel_t *k, stk_task_t *task)
Resume a previously suspended task.
Definition stk_c.cpp:469
void stk_mutex_lock(stk_mutex_t *mtx)
Lock the mutex. Blocks until available.
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
Definition stk_c.cpp:406
stk::sync::Pipe * stk_pipe_get_instance(stk_pipe_t *pipe)
Get the underlying C++ stk::sync::Pipe object wrapped by a stk_pipe_t handle.
stk_ef_t * stk_ef_create(stk_ef_mem_t *const membuf, uint32_t initial_flags)
Create an EventFlags object (using provided memory).
size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const void *src, size_t count, stk_timeout_t timeout)
Write multiple elements to the pipe.
void stk_rwmutex_lock(stk_rwmutex_t *rw)
Acquire the lock for exclusive writing. Blocks until available.
stk_mutex_t * stk_mutex_create(stk_mutex_mem_t *const membuf)
Create a Mutex (using provided memory).
void stk_cv_destroy(stk_cv_t *cv)
Destroy a Condition Variable.
void stk_sleep(stk_timeout_t ticks)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:645
stk_barrier_result_t
Outcome of a stk_barrier_wait_ex() call.
Definition stk_c.h:2520
bool stk_msgq_is_empty(const stk_msgq_t *mq)
Check whether the queue is currently empty.
uint64_t stk_cycle_t
CPU cycles value.
Definition stk_c.h:123
uint32_t stk_barrier_get_threshold(const stk_barrier_t *barrier)
Get the number of tasks required to trip the barrier.
void stk_task_set_weight(stk_task_t *tsk, stk_weight_t weight)
Set task weight (used only by Smooth Weighted Round Robin).
Definition stk_c.cpp:579
bool stk_mutex_trylock(stk_mutex_t *mtx)
Try locking the mutex. Does not block if already locked.
void stk_rwmutex_read_lock(stk_rwmutex_t *rw)
Acquire the lock for shared reading. Blocks until available.
void stk_kernel_set_event_overrider(stk_kernel_t *k, stk_event_overrider_t *overrider)
Install a platform event overrider on the kernel.
Definition stk_c.cpp:529
size_t stk_msgq_get_capacity(const stk_msgq_t *mq)
Get the maximum number of messages the queue can hold.
stk_msgq_t * stk_msgq_create(stk_msgq_mem_t *const membuf, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t msg_size)
Create a MessageQueue (using provided memory).
void stk_mutex_unlock(stk_mutex_t *mtx)
Unlock the mutex.
void stk_pipe_destroy(stk_pipe_t *pipe)
Destroy a Pipe.
stk_cs_session_t stk_critical_section_enter_ex(stk_cs_session_t ses)
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:671
stk_wait_result_t stk_fastmutex_timed_lock_ex(stk_fastmutex_t *mtx, stk_timeout_t timeout)
Try to lock the FastMutex with a timeout, preserving the full outcome.
stk_timeout_t stk_ticks_from_ms_clamped_to_timeout(stk_timeout_t ms)
Get ticks from milliseconds using current kernel tick resolution, clamped to the maximum value repres...
Definition stk_c.cpp:638
size_t stk_pipe_get_element_size(const stk_pipe_t *pipe)
Get the size of each element in bytes.
stk_cycle_t stk_sys_timer_count(void)
Get raw system timer counter value.
Definition stk_c.cpp:639
#define STK_MUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Mutex instance.
Definition stk_c.h:957
stk_wait_result_t stk_sem_wait_ex(stk_sem_t *sem, stk_timeout_t timeout)
Wait for a semaphore resource, preserving the full outcome.
stk_event_t * stk_event_create(stk_event_mem_t *const membuf, bool manual_reset)
Create an Event (using provided memory).
static stk_tick_t stk_ticks_from_ms_r(stk_time_t msec, uint32_t resolution)
Get ticks from milliseconds using an explicit tick resolution.
Definition stk_c.h:690
stk_tid_t stk_task_get_id(const stk_task_t *tsk)
Get the unique identifier of a task.
Definition stk_c.cpp:609
#define STK_EF_ERROR_MASK
Definition stk_c.h:1499
uint32_t stk_tick_resolution(void)
Returns how many microseconds correspond to one kernel tick.
Definition stk_c.cpp:635
bool stk_sem_wait(stk_sem_t *sem, stk_timeout_t timeout)
Wait for a semaphore resource.
bool stk_msgq_putfront(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the front of the queue (priority insert).
size_t stk_msgq_get_count(const stk_msgq_t *mq)
Get the current number of messages waiting in the queue.
bool stk_fastmutex_timed_lock(stk_fastmutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout.
uint32_t stk_sys_timer_frequency(void)
Get system timer frequency in Hz.
Definition stk_c.cpp:640
void stk_kernel_schedule_task_removal(stk_kernel_t *k, stk_task_t *task)
Schedule removal of a running task from the kernel on the next tick.
Definition stk_c.cpp:452
void stk_sleep_ms(stk_timeout_t ms)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:647
size_t stk_kernel_enumerate_tasks(stk_kernel_t *k, stk_task_t **tasks, size_t max_count)
Enumerate all currently active tasks.
Definition stk_c.cpp:477
#define STK_CV_IMPL_SIZE
A memory size (multiples of stk_word_t) required for ConditionVariable instance.
Definition stk_c.h:1178
uint8_t * stk_msgq_get_buffer(stk_msgq_t *mq)
Get a pointer to the raw message data buffer.
stk_wait_result_t stk_msgq_putfront_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the front of the queue, preserving the full outcome.
#define STK_MSGQ_IMPL_SIZE
A memory size (multiples of stk_word_t) required for a MessageQueue instance.
Definition stk_c.h:2002
void stk_kernel_add_task(stk_kernel_t *k, stk_task_t *tsk)
Add task to non-HRT kernel (static or dynamic).
Definition stk_c.cpp:428
void stk_spinlock_lock(stk_spinlock_t *slock)
Acquire the SpinLock (recursive).
bool stk_msgq_put(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the queue.
bool stk_msgq_tryput(stk_msgq_t *mq, const void *msg)
Attempt to put a message into the queue without blocking.
stk::sync::Event * stk_event_get_instance(stk_event_t *ev)
Get the underlying C++ stk::sync::Event object wrapped by a stk_event_t handle.
bool stk_cv_wait_fastmutex(stk_cv_t *cv, stk_fastmutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable, protected by a FastMutex.
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:399
void stk_critical_section_exit_ex(stk_cs_session_t ses)
Leave global critical section - re-enable context switches.
Definition stk_c.cpp:676
stk_tick_t stk_hires_time_us(void)
Get elapsed time in microseconds from the high-resolution clock.
Definition stk_c.cpp:643
#define __stk_c_aligned
Memory buffer alignment attribute.
Definition stk_c.h:226
void stk_pipe_reset(stk_pipe_t *pipe)
Discard all elements and reset the pipe to the empty state.
void(* stk_task_entry_t)(void *arg)
Task entry point function type.
Definition stk_c.h:148
stk_barrier_result_t stk_barrier_wait_ex(stk_barrier_t *barrier)
Block the calling task until count tasks have arrived, preserving the full outcome.
stk_wait_result_t stk_rwmutex_timed_read_lock_ex(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the read lock with a timeout, preserving the full outcome.
void stk_sem_destroy(stk_sem_t *sem)
Destroy a Semaphore.
void stk_spinlock_unlock(stk_spinlock_t *slock)
Release the SpinLock.
size_t stk_pipe_tryread_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t max_count)
Non-blocking variant of stk_pipe_read_bulk_triggered.
stk_cv_t * stk_cv_create(stk_cv_mem_t *const membuf)
Create a Condition Variable (using provided memory).
bool stk_sem_trywait(stk_sem_t *sem)
Poll the semaphore without blocking.
stk_kernel_state_t
Kernel state.
Definition stk_c.h:394
void stk_tls_set(void *ptr)
Set thread-local pointer.
stk::sync::RWMutex * stk_rwmutex_get_instance(stk_rwmutex_t *rw)
Get the underlying C++ stk::sync::RWMutex object wrapped by a stk_rwmutex_t handle.
void stk_rwmutex_read_unlock(stk_rwmutex_t *rw)
Release the shared reader lock.
stk_task_t * stk_task_create_privileged(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size)
Create privileged-mode (kernel-mode) task.
Definition stk_c.cpp:555
stk_tick_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
Definition stk_c.cpp:634
void stk_task_set_priority(stk_task_t *tsk, uint8_t priority)
Set task priority (used only by Fixed Priority scheduler).
Definition stk_c.cpp:586
size_t stk_msgq_get_msg_size(const stk_msgq_t *mq)
Get the size of each message in bytes.
bool stk_pipe_tryread(stk_pipe_t *pipe, void *data)
Attempt to read a single element from the pipe without blocking.
void stk_critical_section_enter()
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:681
uint32_t stk_ef_get(stk_ef_t *ef)
Read the current flags word without modifying it.
stk_word_t stk_tid_t
Task id.
Definition stk_c.h:103
stk::ITask * stk_task_get_instance(stk_task_t *tsk)
Get the underlying C++ stk::ITask object wrapped by a stk_task_t handle.
Definition stk_c.cpp:616
#define STK_PIPE_IMPL_SIZE
A memory size (multiples of stk_word_t) required for a Pipe control-block.
Definition stk_c.h:1623
stk::sync::ConditionVariable * stk_cv_get_instance(stk_cv_t *cv)
Get the underlying C++ stk::sync::ConditionVariable object wrapped by a stk_cv_t handle.
int64_t stk_tick_t
Ticks value.
Definition stk_c.h:108
stk_wait_result_t stk_msgq_peek_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the next message without removing it, preserving the full outcome.
bool stk_fastmutex_trylock(stk_fastmutex_t *mtx)
Try locking the mutex. Does not block if already locked.
size_t stk_pipe_read_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t trigger, size_t max_count, stk_timeout_t timeout)
Read at least trigger elements, then drain up to max_count without blocking.
bool stk_msgq_get(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Get a message from the queue.
#define STK_SPINLOCK_IMPL_SIZE
A memory size (multiples of stk_word_t) required for SpinLock instance.
Definition stk_c.h:1127
void stk_event_pulse(stk_event_t *ev)
Pulse the event (signal then immediately reset).
bool stk_msgq_tryputfront(stk_msgq_t *mq, const void *msg)
Attempt to put a message into the front of the queue without blocking.
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:331
bool stk_pipe_is_full(const stk_pipe_t *pipe)
Check whether the pipe is currently full.
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:99
bool stk_pipe_is_empty(const stk_pipe_t *pipe)
Check whether the pipe is currently empty.
static bool stk_ef_is_error(uint32_t result)
Returns true if a value returned by stk_ef_set(), stk_ef_clear(), stk_ef_wait(), or stk_ef_trywait() ...
Definition stk_c.h:1504
bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe.
void stk_fastmutex_lock(stk_fastmutex_t *mtx)
Lock the mutex. Blocks until available.
bool stk_kernel_is_started(const stk_kernel_t *k)
Check whether the scheduler is currently running (first task switch has occurred).
Definition stk_c.cpp:444
stk_wait_result_t stk_pipe_write_ex(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout)
Write a single element to the pipe, preserving the full outcome.
void stk_sleep_cancel(stk_tid_t tid)
Cancel the sleep of a task, waking it immediately.
Definition stk_c.cpp:649
int64_t stk_time_t
Time value.
Definition stk_c.h:113
stk_time_t stk_time_now_ms(void)
Returns current time in milliseconds since kernel start.
Definition stk_c.cpp:636
bool stk_event_reset(stk_event_t *ev)
Reset the event to non-signaled state.
int32_t stk_weight_t
Task weight value.
Definition stk_c.h:127
void stk_fastmutex_unlock(stk_fastmutex_t *mtx)
Unlock the mutex.
#define STK_RWMUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for RWMutex instance.
Definition stk_c.h:2358
void stk_kernel_add_task_hrt(stk_kernel_t *k, stk_task_t *tsk, int32_t periodicity_ticks, int32_t deadline_ticks, int32_t start_delay_ticks)
Add task with HRT timing parameters (HRT kernels only).
Definition stk_c.cpp:536
stk_wait_result_t stk_msgq_get_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Get a message from the queue, preserving the full outcome.
stk_kernel_state_t stk_kernel_get_state(const stk_kernel_t *k)
Get state of the scheduler.
Definition stk_c.cpp:413
void stk_task_destroy(stk_task_t *tsk)
Destroy dynamically created task object.
Definition stk_c.cpp:623
bool stk_sleep_until(stk_tick_t ts)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:648
bool stk_msgq_is_storage_valid(const stk_msgq_t *mq)
Verify that the backing storage is valid and the queue is ready for use.
stk_fastmutex_t * stk_fastmutex_create(stk_fastmutex_mem_t *const membuf)
Create a FastMutex (using provided memory).
bool stk_rwmutex_timed_lock(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the write lock with a timeout.
#define STK_EF_IMPL_SIZE
A memory size (multiples of stk_word_t) required for EventFlags instance.
Definition stk_c.h:1513
void stk_rwmutex_destroy(stk_rwmutex_t *rw)
Destroy an RWMutex.
void stk_msgq_destroy(stk_msgq_t *mq)
Destroy a MessageQueue.
stk_sem_t * stk_sem_create(stk_sem_mem_t *const membuf, uint32_t initial_count, uint32_t max_count)
Create a Semaphore (using provided memory).
void stk_kernel_process_hard_fault(stk_kernel_t *k)
Trigger a kernel hard fault (safe-state handler).
Definition stk_c.cpp:522
stk_wait_result_t stk_event_wait_ex(stk_event_t *ev, stk_timeout_t timeout)
Wait for the event to become signaled, preserving the full outcome.
stk_barrier_t * stk_barrier_create(stk_barrier_mem_t *const membuf, uint32_t count)
Create a Barrier (using provided memory).
static stk_time_t stk_ms_from_ticks_r(stk_tick_t ticks, uint32_t resolution)
Convert ticks to milliseconds using an explicit tick resolution.
Definition stk_c.h:714
bool stk_rwmutex_timed_read_lock(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the read lock with a timeout.
int32_t stk_timeout_t
Timeout value.
Definition stk_c.h:118
stk::sync::Mutex * stk_mutex_get_instance(stk_mutex_t *mtx)
Get the underlying C++ stk::sync::Mutex object wrapped by a stk_mutex_t handle.
bool stk_msgq_peek(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the next message to be delivered without removing it.
stk_wait_result_t stk_mutex_timed_lock_ex(stk_mutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout, preserving the full outcome.
static stk_time_t stk_ms_from_ticks(stk_tick_t ticks)
Convert ticks to milliseconds using the current kernel tick resolution.
Definition stk_c.h:725
void stk_critical_section_exit()
Leave global critical section - re-enable context switches.
Definition stk_c.cpp:688
bool stk_msgq_trypeek(stk_msgq_t *mq, void *msg)
Attempt to peek at the next message without blocking.
void stk_delay(stk_timeout_t ticks)
Busy-wait delay (other tasks continue to run).
Definition stk_c.cpp:644
bool stk_kernel_is_schedulable(const stk_kernel_t *k)
Test whether currently configured task set is schedulable.
Definition stk_c.cpp:420
void stk_cv_notify_all(stk_cv_t *cv)
Wake all tasks waiting on the condition variable.
stk_wait_result_t stk_pipe_write_bulk_ex(stk_pipe_t *pipe, const void *src, size_t count, size_t *written, stk_timeout_t timeout)
Write multiple elements to the pipe, preserving the full outcome.
stk_rwmutex_t * stk_rwmutex_create(stk_rwmutex_mem_t *const membuf)
Create an RWMutex (using provided memory).
stk_wait_result_t stk_cv_wait_ex_fastmutex(stk_cv_t *cv, stk_fastmutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable protected by a FastMutex, preserving the full outcome.
bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data)
Attempt to write a single element to the pipe without blocking.
void stk_rwmutex_unlock(stk_rwmutex_t *rw)
Release the exclusive writer lock.
stk::sync::MessageQueue * stk_msgq_get_instance(stk_msgq_t *mq)
Get the underlying C++ stk::sync::MessageQueue object wrapped by a stk_msgq_t handle.
bool stk_barrier_wait(stk_barrier_t *barrier)
Block the calling task until count tasks have called stk_barrier_wait().
size_t stk_pipe_tryread_bulk(stk_pipe_t *pipe, void *dst, size_t count)
Attempt to read multiple elements from the pipe without blocking.
@ STK_WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_c.h:156
@ STK_WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_c.h:155
@ STK_WAIT_RESULT_CANCELED
The wait was cancelled by another task, independent of signal/timeout.
Definition stk_c.h:157
@ STK_WAIT_RESULT_FAIL
The kernel returned an error without actually waiting.
Definition stk_c.h:154
@ STK_BARRIER_CANCELED
Definition stk_c.h:2523
@ STK_BARRIER_RELEASED
Released because the barrier tripped; this task was not the last arrival.
Definition stk_c.h:2521
@ STK_BARRIER_LAST_ARRIVAL
This task was the last arrival and tripped the barrier.
Definition stk_c.h:2522
@ STK_KERNEL_STATE_INACTIVE
not ready, stk_kernel_init() must be called
Definition stk_c.h:395
@ STK_KERNEL_STATE_READY
ready to start, stk_kernel_start() must be called
Definition stk_c.h:396
@ STK_KERNEL_STATE_RUNNING
initialized and running, stk_kernel_start() was called successfully
Definition stk_c.h:397
@ STK_KERNEL_STATE_SUSPENDED
scheduling suspended via stk_kernel_service_suspend() (tickless idle)
Definition stk_c.h:398
Namespace of STK package.
static T * ConstructIn(Mem &mem, Args &&... args)
Construct object T in-place inside a C memory container.
Definition stk_c.h:2581
Synchronization primitives for task coordination and resource protection.
Interface for a user task.
Definition stk_common.h:780
Interface for the implementation of the kernel of the scheduler. It supports Soft and Hard Real-Time ...
Cyclic barrier that blocks a fixed-size group of tasks until all of them have arrived.
Condition Variable primitive for signaling between tasks based on specific predicates.
Definition stk_sync_cv.h:68
Binary synchronization event (signaled / non-signaled) primitive.
32-bit event flags group for multi-flag synchronization between tasks.
Non-recursive (binary) mutex primitive with ownership tracking.
Fixed-capacity, fixed-message-size FIFO queue for inter-task communication.
Recursive mutex primitive that allows the same thread to acquire the lock multiple times.
Thread-safe FIFO communication pipe for inter-task data passing.
Reader-Writer Lock synchronization primitive for non-recursive shared and exclusive access.
Counting semaphore primitive for resource management and signaling.
Recursive spinlock.
C-level callback table that mirrors stk::IPlatform::IEventOverrider.
Definition stk_c.h:520
bool(* on_hard_fault)(void *user_data)
Called by the kernel when a hard fault occurs (e.g. HRT deadline missed).
Definition stk_c.h:536
bool(* on_sleep)(stk_timeout_t sleep_ticks, void *user_data)
Called by the kernel when it is about to enter a sleep (idle) state.
Definition stk_c.h:528
void * user_data
Opaque pointer forwarded unchanged to every callback.
Definition stk_c.h:541
Opaque memory container for a Mutex instance.
Definition stk_c.h:961
stk_word_t data[(10U+((0) ? 1U :0U))]
Definition stk_c.h:962
Opaque memory container for a FastMutex instance.
Definition stk_c.h:1041
stk_word_t data[(9U+((0) ? 1U :0U))]
Definition stk_c.h:1042
Opaque memory container for SpinLock object.
Definition stk_c.h:1132
stk_word_t data[(6)]
Definition stk_c.h:1133
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:1182
stk_word_t data[(7U+((0) ? 1U :0U))]
Definition stk_c.h:1183
Opaque memory container for an Event instance.
Definition stk_c.h:1293
stk_word_t data[(8U+((0) ? 1U :0U))]
Definition stk_c.h:1294
Opaque memory container for a Semaphore instance.
Definition stk_c.h:1384
stk_word_t data[(8U+((0) ? 1U :0U))]
Definition stk_c.h:1385
Opaque memory container for an EventFlags instance.
Definition stk_c.h:1517
stk_word_t data[((7U+((0) ? 1U :0U))+1U+((0) ? 1U :0U))]
Definition stk_c.h:1518
Opaque memory container for a Pipe control-block.
Definition stk_c.h:1627
stk_word_t data[(6U+(2U *(7U+((0) ? 1U :0U)))+((0) ? 1U :0U))]
Definition stk_c.h:1628
Opaque memory container for a MessageQueue instance.
Definition stk_c.h:2006
stk_word_t data[(6U+(2U *(7U+((0) ? 1U :0U)))+((0) ? 1U :0U))]
Definition stk_c.h:2007
Opaque memory container for an RWMutex instance.
Definition stk_c.h:2362
stk_word_t data[(17U+((0) ? 3U :0U))]
Definition stk_c.h:2363
Opaque memory container for a Barrier instance.
Definition stk_c.h:2496
stk_word_t data[((10U+((0) ? 1U :0U))+(7U+((0) ? 1U :0U))+4U+((0) ? 1U :0U))]
Definition stk_c.h:2497