SuperTinyKernel™ RTOS 1.06.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
42// =============================================================================
43// Configuration macros (can be overridden before including this file)
44// =============================================================================
45
51#ifndef STK_C_KERNEL_MAX_TASKS
52 #define STK_C_KERNEL_MAX_TASKS (4)
53#endif
54
59#ifndef STK_C_CPU_COUNT
60 #define STK_C_CPU_COUNT (1)
61#endif
62
66#if !defined(STK_SYNC_DEBUG_NAMES) && STK_SEGGER_SYSVIEW
67 #define STK_SYNC_DEBUG_NAMES (1)
68#elif !defined(STK_SYNC_DEBUG_NAMES)
69 #define STK_SYNC_DEBUG_NAMES (0)
70#endif
71
75#define STK_C_ASSERT(e) assert(e)
76
82#ifdef __cplusplus
83 #define STK_STATIC_CAST(type, val) static_cast<type>(val)
84#else
85 #define STK_STATIC_CAST(type, val) ((type)(val))
86#endif
87
88// =============================================================================
89// Types
90// =============================================================================
91
94typedef uintptr_t stk_word_t;
95
99
103typedef int64_t stk_tick_t;
104
108typedef int64_t stk_time_t;
109
113typedef int32_t stk_timeout_t;
114
118typedef uint64_t stk_cycle_t;
119
122typedef int32_t stk_weight_t;
123
127
130typedef struct stk_task_t stk_task_t;
131
134#define STK_PERIODICITY_DEFAULT (1000U)
135
143typedef void (*stk_task_entry_t)(void *arg);
144
147#define STK_WAIT_INFINITE (STK_STATIC_CAST(stk_timeout_t, INT32_MAX))
148
151#define STK_NO_WAIT (STK_STATIC_CAST(stk_timeout_t, 0))
152
155#define STK_ALIGN_SIZE sizeof(stk_word_t)
156
159#define STK_ALIGN_MASK (STK_ALIGN_SIZE - 1U)
160
164#ifndef STK_STACK_MEMORY_ALIGN
165 #if defined(__riscv)
166 #define STK_STACK_MEMORY_ALIGN (16U)
167 #elif defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
168 #define STK_STACK_MEMORY_ALIGN (8U)
169 #else // ARM, others
170 #define STK_STACK_MEMORY_ALIGN (4U)
171 #endif
172#endif
173
181#define STK_DEFINE_STACK_POOL(name, max_tasks, stack_size) \
182 static stk_word_t name[max_tasks][stack_size] __stk_c_stack
183
190#define STK_GET_STACK_FROM_POOL(name, task_id) (name[task_id])
191
192// =============================================================================
193// Attributes
194// =============================================================================
195
199#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
200 #define __stk_c_stack __attribute__((aligned(STK_STACK_MEMORY_ALIGN)))
201#else
202 #define __stk_c_stack
203#endif
204
208#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
209 #define __stk_c_aligned __attribute__((aligned(STK_ALIGN_SIZE)))
210#else
211 #define __stk_c_aligned
212#endif
213
214// =============================================================================
215// Kernel factory functions
216// =============================================================================
217
218/* Available kernel type definitions:
219
220 Kernel mode flags (may be OR-combined, subject to the constraints listed below):
221 KERNEL_STATIC - fixed task list; tasks must never return from their entry function.
222 KERNEL_DYNAMIC - tasks may be added/removed at runtime and may return when done.
223 KERNEL_HRT - Hard Real-Time mode; must be combined with KERNEL_STATIC or KERNEL_DYNAMIC.
224 KERNEL_SYNC - enables synchronization primitives (Mutex, Event, Semaphore, etc.).
225 KERNEL_TICKLESS - tickless low-power idle; suppresses the SysTick when all tasks sleep.
226 Requires STK_TICKLESS_IDLE=1 in stk_config.h.
227 INCOMPATIBLE with KERNEL_HRT (HRT requires a continuous tick).
228
229// Standard variants
230Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
231Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
232Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
233Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
234Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
235Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
236
237// HRT variants (KERNEL_TICKLESS must NOT be combined with any of these)
238Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
239Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
240Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
241Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
242Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
243Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
244Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
245Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
246
247// Standard variants with KERNEL_SYNC
248Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
249Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
250Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
251Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
252Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
253Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
254
255// HRT variants with KERNEL_SYNC
256Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
257Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
258Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
259Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
260Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
261Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
262Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
263Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
264
265// Tickless variants (low-power; KERNEL_TICKLESS requires STK_TICKLESS_IDLE=1 in stk_config.h)
266Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
267Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
268Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
269Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
270Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
271Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
272
273// Tickless variants with KERNEL_SYNC
274Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
275Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
276Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
277Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
278Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
279Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
280*/
281
305
312stk_kernel_t *stk_kernel_create(uint8_t core_nr);
313
314// =============================================================================
315// Kernel control
316// =============================================================================
317
323void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us);
324
331
341 stk_task_t *tsk,
342 int32_t periodicity_ticks,
343 int32_t deadline_ticks,
344 int32_t start_delay_ticks);
345
352
359
370
376
383
390
400
409void stk_kernel_suspend_task(stk_kernel_t *k, stk_task_t *task, bool *suspended);
410
416
427
436void stk_kernel_resume(stk_kernel_t *k, stk_timeout_t elapsed_ticks);
437
446size_t stk_kernel_enumerate_tasks(stk_kernel_t *k, stk_task_t **tasks, size_t max_count);
447
457
466
467// =============================================================================
468// Platform event overrider
469// =============================================================================
470
490{
498 bool (*on_sleep)(stk_timeout_t sleep_ticks, void *user_data);
499
506 bool (*on_hard_fault)(void *user_data);
507
513
527
528// =============================================================================
529// Tasks
530// =============================================================================
531
540 void *arg,
541 stk_word_t *stack,
542 uint32_t stack_size);
543
552 void *arg,
553 stk_word_t *stack,
554 uint32_t stack_size);
555
563
569void stk_task_set_priority(stk_task_t *tsk, uint8_t priority);
570
575void stk_task_set_name(stk_task_t *tsk, const char *tname);
576
582const char *stk_task_get_name(const stk_task_t *tsk);
583
592
593// =============================================================================
594// Services available from inside tasks
595// =============================================================================
596
600stk_tid_t stk_tid(void);
601
606
610uint32_t stk_tick_resolution(void);
611
620
629static inline stk_tick_t stk_ticks_from_ms_r(stk_time_t msec, uint32_t resolution)
630{
631 stk_time_t result = 0LL;
632
633 if (resolution != 0U)
634 {
635 const stk_time_t total_scaled = msec * 1000LL;
636 result = total_scaled / STK_STATIC_CAST(stk_time_t, resolution);
637 }
638
639 return STK_STATIC_CAST(stk_tick_t, result);
640}
641
646
653static inline stk_time_t stk_ms_from_ticks_r(stk_tick_t ticks, uint32_t resolution)
654{
655 const stk_tick_t total_ticks = ticks * STK_STATIC_CAST(stk_tick_t, resolution);
656 return STK_STATIC_CAST(stk_time_t, total_ticks / 1000LL);
657}
658
665{
667}
668
674
680uint32_t stk_sys_timer_frequency(void);
681
682// =============================================================================
683// High-Resolution Clock (see hw::HiResClock)
684// =============================================================================
685
691
696uint32_t stk_hires_frequency(void);
697
703
707void stk_delay(stk_timeout_t ticks);
708
713
724void stk_sleep(stk_timeout_t ticks);
725
737
750
753void stk_yield(void);
754
763
764// =============================================================================
765// Dynamic cleanup
766// =============================================================================
767
774
781
782// =============================================================================
783// Thread-Local Storage (TLS)
784// =============================================================================
785
802
806void *stk_tls_get(void);
807
811void stk_tls_set(void *ptr);
812
816#define STK_TLS_GET_T(type) ((type *)stk_tls_get())
817
821#define STK_TLS_SET_T(ptr) stk_tls_set((void *)(ptr))
822
823// =============================================================================
824// Synchronization Primitives
825// =============================================================================
826
827// ----- Critical Section ------------------------------------------------------
828
833
838
839// ----- Mutex -----------------------------------------------------------------
840
843#define STK_MUTEX_IMPL_SIZE (10U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
844
850
854
860stk_mutex_t *stk_mutex_create(stk_mutex_mem_t *const membuf, uint32_t membuf_size);
861
866
870void stk_mutex_lock(stk_mutex_t *mtx);
871
877
882
889
890// ----- SpinLock --------------------------------------------------------------
891
894#define STK_SPINLOCK_IMPL_SIZE (1)
895
902
906
912stk_spinlock_t *stk_spinlock_create(stk_spinlock_mem_t *const membuf, uint32_t membuf_size);
913
917
921
926
930
931// ----- Condition Variable ----------------------------------------------------
932
935#define STK_CV_IMPL_SIZE (7U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
936
942
945typedef struct stk_cv_t stk_cv_t;
946
952stk_cv_t *stk_cv_create(stk_cv_mem_t *const membuf, uint32_t membuf_size);
953
957void stk_cv_destroy(stk_cv_t *cv);
958
967bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout);
968
973
978
979// ----- Event -----------------------------------------------------------------
980
983#define STK_EVENT_IMPL_SIZE (8U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
984
990
994
1002 uint32_t membuf_size,
1003 bool manual_reset);
1004
1009
1015bool stk_event_wait(stk_event_t *ev, stk_timeout_t timeout);
1016
1022
1028bool stk_event_set(stk_event_t *ev);
1029
1036
1041
1042// ----- Semaphore -------------------------------------------------------------
1043
1046#define STK_SEM_IMPL_SIZE (8U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1047
1053
1056typedef struct stk_sem_t stk_sem_t;
1057
1066stk_sem_t *stk_sem_create(stk_sem_mem_t *const membuf,
1067 uint32_t membuf_size,
1068 uint32_t initial_count,
1069 uint32_t max_count);
1070
1074void stk_sem_destroy(stk_sem_t *sem);
1075
1081bool stk_sem_wait(stk_sem_t *sem, stk_timeout_t timeout);
1082
1090bool stk_sem_trywait(stk_sem_t *sem);
1091
1095void stk_sem_signal(stk_sem_t *sem);
1096
1103uint16_t stk_sem_get_count(const stk_sem_t *sem);
1104
1105// ----- EventFlags ------------------------------------------------------------
1106
1109#define STK_EF_OPT_WAIT_ANY (0x00000000U)
1110#define STK_EF_OPT_WAIT_ALL (0x00000001U)
1111#define STK_EF_OPT_NO_CLEAR (0x00000002U)
1112
1115#define STK_EF_ERROR_PARAMETER (0x80000001U)
1116#define STK_EF_ERROR_TIMEOUT (0x80000002U)
1117#define STK_EF_ERROR_ISR (0x80000004U)
1118#define STK_EF_ERROR_MASK (0x80000000U)
1119
1123static inline bool stk_ef_is_error(uint32_t result)
1124{
1125 return ((result & STK_EF_ERROR_MASK) != 0U);
1126}
1127
1132#define STK_EF_IMPL_SIZE (STK_CV_IMPL_SIZE + 1U + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1133
1139
1142typedef struct stk_ef_t stk_ef_t;
1143
1151stk_ef_t *stk_ef_create(stk_ef_mem_t *const membuf,
1152 uint32_t membuf_size,
1153 uint32_t initial_flags);
1154
1158void stk_ef_destroy(stk_ef_t *ef);
1159
1168uint32_t stk_ef_set(stk_ef_t *ef, uint32_t flags);
1169
1177uint32_t stk_ef_clear(stk_ef_t *ef, uint32_t flags);
1178
1184uint32_t stk_ef_get(stk_ef_t *ef);
1185
1203uint32_t stk_ef_wait(stk_ef_t *ef, uint32_t flags, uint32_t options, stk_timeout_t timeout);
1204
1216uint32_t stk_ef_trywait(stk_ef_t *ef, uint32_t flags, uint32_t options);
1217
1218// ----- Pipe (FIFO) -----------------------------------------------------------
1219
1228#define STK_PIPE_IMPL_SIZE (6U + (2U * STK_CV_IMPL_SIZE) + (STK_SYNC_DEBUG_NAMES ? 1U : 0U))
1229
1235
1238typedef struct stk_pipe_t stk_pipe_t;
1239
1257#define STK_PIPE_BUF_SIZE(capacity, element_size) \
1258 ((((capacity) * (element_size)) + STK_ALIGN_MASK) & ~STK_ALIGN_MASK)
1259
1280 uint32_t membuf_size,
1281 uint8_t *buf,
1282 uint32_t buf_size,
1283 size_t capacity,
1284 size_t element_size);
1285
1291void stk_pipe_destroy(stk_pipe_t *pipe);
1292
1304bool stk_pipe_write(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout);
1305
1312bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data);
1313
1325bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout);
1326
1333bool stk_pipe_tryread(stk_pipe_t *pipe, void *data);
1334
1348size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const void *src, size_t count, stk_timeout_t timeout);
1349
1358size_t stk_pipe_trywrite_bulk(stk_pipe_t *pipe, const void *src, size_t count);
1359
1372size_t stk_pipe_read_bulk(stk_pipe_t *pipe, void *dst, size_t count, stk_timeout_t timeout);
1373
1382size_t stk_pipe_tryread_bulk(stk_pipe_t *pipe, void *dst, size_t count);
1383
1401 void *dst,
1402 size_t trigger,
1403 size_t max_count,
1404 stk_timeout_t timeout);
1405
1415size_t stk_pipe_tryread_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t max_count);
1416
1423void stk_pipe_reset(stk_pipe_t *pipe);
1424
1430size_t stk_pipe_get_capacity(const stk_pipe_t *pipe);
1431
1437size_t stk_pipe_get_element_size(const stk_pipe_t *pipe);
1438
1444size_t stk_pipe_get_count(const stk_pipe_t *pipe);
1445
1451size_t stk_pipe_get_space(const stk_pipe_t *pipe);
1452
1458bool stk_pipe_is_empty(const stk_pipe_t *pipe);
1459
1465bool stk_pipe_is_full(const stk_pipe_t *pipe);
1466
1472bool stk_pipe_is_storage_valid(const stk_pipe_t *pipe);
1473
1474// ----- MessageQueue ----------------------------------------------------------
1475
1484#define STK_MSGQ_IMPL_SIZE (6 + (2 * STK_CV_IMPL_SIZE) + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
1485
1491
1494typedef struct stk_msgq_t stk_msgq_t;
1495
1513#define STK_MSGQ_BUF_SIZE(capacity, msg_size) ((capacity) * (msg_size))
1514
1537 uint32_t membuf_size,
1538 uint8_t *buf,
1539 uint32_t buf_size,
1540 size_t capacity,
1541 size_t msg_size);
1542
1549
1561bool stk_msgq_put(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
1562
1569bool stk_msgq_tryput(stk_msgq_t *mq, const void *msg);
1570
1583bool stk_msgq_putfront(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout);
1584
1591bool stk_msgq_tryputfront(stk_msgq_t *mq, const void *msg);
1592
1603bool stk_msgq_get(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
1604
1611bool stk_msgq_tryget(stk_msgq_t *mq, void *msg);
1612
1626bool stk_msgq_peek(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
1627
1634bool stk_msgq_trypeek(stk_msgq_t *mq, void *msg);
1635
1648bool stk_msgq_peekfront(stk_msgq_t *mq, void *msg, stk_timeout_t timeout);
1649
1656bool stk_msgq_trypeekfront(stk_msgq_t *mq, void *msg);
1657
1665void stk_msgq_reset(stk_msgq_t *mq);
1666
1672size_t stk_msgq_get_capacity(const stk_msgq_t *mq);
1673
1679size_t stk_msgq_get_msg_size(const stk_msgq_t *mq);
1680
1686size_t stk_msgq_get_count(const stk_msgq_t *mq);
1687
1693size_t stk_msgq_get_space(const stk_msgq_t *mq);
1694
1700bool stk_msgq_is_empty(const stk_msgq_t *mq);
1701
1708uint8_t *stk_msgq_get_buffer(stk_msgq_t *mq);
1709
1715bool stk_msgq_is_full(const stk_msgq_t *mq);
1716
1727
1728// ----- RWMutex (Reader-Writer Lock) ------------------------------------------
1729
1732#define STK_RWMUTEX_IMPL_SIZE (17U + (STK_SYNC_DEBUG_NAMES ? 3U : 0U))
1733
1739
1743
1749stk_rwmutex_t *stk_rwmutex_create(stk_rwmutex_mem_t *const membuf, uint32_t membuf_size);
1750
1755
1762
1768
1775
1781
1788
1794
1801
1808
1809#ifdef __cplusplus
1810}
1811#endif
1812
1814
1815#endif /* STK_C_H_ */
stk_rwmutex_t * stk_rwmutex_create(stk_rwmutex_mem_t *const membuf, uint32_t membuf_size)
Create an RWMutex (using provided memory).
void stk_kernel_process_tick(stk_kernel_t *k)
Manually deliver one scheduler tick to the kernel.
Definition stk_c.cpp:504
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:556
void stk_task_set_name(stk_task_t *tsk, const char *tname)
Assign human-readable task name (for tracing/debugging).
Definition stk_c.cpp:584
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:622
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:83
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.
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_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:615
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.
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:619
size_t stk_pipe_get_space(const stk_pipe_t *pipe)
Get the number of free slots currently available.
stk_sem_t * stk_sem_create(stk_sem_mem_t *const membuf, uint32_t membuf_size, uint32_t initial_count, uint32_t max_count)
Create a Semaphore (using provided memory).
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:425
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.
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.
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:591
#define STK_SEM_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Semaphore instance.
Definition stk_c.h:1046
void stk_spinlock_destroy(stk_spinlock_t *slock)
Destroy the SpinLock.
void stk_kernel_destroy(stk_kernel_t *k)
Destroy dynamic kernel instance (only when not running).
Definition stk_c.cpp:374
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:449
bool stk_spinlock_trylock(stk_spinlock_t *slock)
Attempt to acquire the SpinLock immediately.
stk_msgq_t * stk_msgq_create(stk_msgq_mem_t *const membuf, uint32_t membuf_size, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t msg_size)
Create a MessageQueue (using provided memory).
#define STK_EVENT_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Event instance.
Definition stk_c.h:983
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.
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:126
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:497
stk_timeout_t stk_kernel_suspend(stk_kernel_t *k)
Suspend scheduling (tickless idle entry point).
Definition stk_c.cpp:490
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:627
stk_event_t * stk_event_create(stk_event_mem_t *const membuf, uint32_t membuf_size, bool manual_reset)
Create an Event (using provided memory).
void stk_yield(void)
Voluntarily give up CPU to another ready task (cooperative yield).
Definition stk_c.cpp:631
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:623
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:458
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:395
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_ef_t * stk_ef_create(stk_ef_mem_t *const membuf, uint32_t membuf_size, uint32_t initial_flags)
Create an EventFlags object (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:626
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:118
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:568
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_critical_section_exit(void)
Leave critical section - re-enable context switches.
Definition stk_c.cpp:656
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:518
size_t stk_msgq_get_capacity(const stk_msgq_t *mq)
Get the maximum number of messages the queue can hold.
void stk_mutex_unlock(stk_mutex_t *mtx)
Unlock the mutex.
void stk_pipe_destroy(stk_pipe_t *pipe)
Destroy a Pipe.
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:620
#define STK_MUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Mutex instance.
Definition stk_c.h:843
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:629
stk_tid_t stk_task_get_id(const stk_task_t *tsk)
Get the unique identifier of a task.
Definition stk_c.cpp:598
#define STK_EF_ERROR_MASK
Definition stk_c.h:1118
uint32_t stk_tick_resolution(void)
Returns how many microseconds correspond to one kernel tick.
Definition stk_c.cpp:617
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.
uint32_t stk_sys_timer_frequency(void)
Get system timer frequency in Hz.
Definition stk_c.cpp:621
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:441
void stk_sleep_ms(stk_timeout_t ms)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:628
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:466
#define STK_CV_IMPL_SIZE
A memory size (multiples of stk_word_t) required for ConditionVariable instance.
Definition stk_c.h:935
uint8_t * stk_msgq_get_buffer(stk_msgq_t *mq)
Get a pointer to the raw message data buffer.
#define STK_MSGQ_IMPL_SIZE
A memory size (multiples of stk_word_t) required for a MessageQueue instance.
Definition stk_c.h:1484
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:417
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.
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:388
stk_tick_t stk_hires_time_us(void)
Get elapsed time in microseconds from the high-resolution clock.
Definition stk_c.cpp:624
#define __stk_c_aligned
Memory buffer alignment attribute.
Definition stk_c.h:211
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:143
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.
bool stk_sem_trywait(stk_sem_t *sem)
Poll the semaphore without blocking.
stk_kernel_state_t
Kernel state.
Definition stk_c.h:364
stk_pipe_t * stk_pipe_create(stk_pipe_mem_t *const membuf, uint32_t membuf_size, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t element_size)
Create a Pipe (using provided memory).
void stk_tls_set(void *ptr)
Set thread-local pointer.
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:544
stk_tick_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
Definition stk_c.cpp:616
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:575
size_t stk_msgq_get_msg_size(const stk_msgq_t *mq)
Get the size of each message in bytes.
stk_spinlock_t * stk_spinlock_create(stk_spinlock_mem_t *const membuf, uint32_t membuf_size)
Create a recursive SpinLock.
bool stk_pipe_tryread(stk_pipe_t *pipe, void *data)
Attempt to read a single element from the pipe without blocking.
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:98
#define STK_PIPE_IMPL_SIZE
A memory size (multiples of stk_word_t) required for a Pipe control-block.
Definition stk_c.h:1228
int64_t stk_tick_t
Ticks value.
Definition stk_c.h:103
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:894
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:333
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:94
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:1123
bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe.
stk_cv_t * stk_cv_create(stk_cv_mem_t *const membuf, uint32_t membuf_size)
Create a Condition Variable (using provided memory).
stk_mutex_t * stk_mutex_create(stk_mutex_mem_t *const membuf, uint32_t membuf_size)
Create a Mutex (using provided memory).
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:433
void stk_sleep_cancel(stk_tid_t tid)
Cancel the sleep of a task, waking it immediately.
Definition stk_c.cpp:630
int64_t stk_time_t
Time value.
Definition stk_c.h:108
stk_time_t stk_time_now_ms(void)
Returns current time in milliseconds since kernel start.
Definition stk_c.cpp:618
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:122
#define STK_RWMUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for RWMutex instance.
Definition stk_c.h:1732
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:525
stk_kernel_state_t stk_kernel_get_state(const stk_kernel_t *k)
Get state of the scheduler.
Definition stk_c.cpp:402
void stk_task_destroy(stk_task_t *tsk)
Destroy dynamically created task object.
Definition stk_c.cpp:605
bool stk_sleep_until(stk_tick_t ts)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:629
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.
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:1132
void stk_rwmutex_destroy(stk_rwmutex_t *rw)
Destroy an RWMutex.
void stk_msgq_destroy(stk_msgq_t *mq)
Destroy a MessageQueue.
void stk_critical_section_enter(void)
Enter critical section - disable context switches on current core.
Definition stk_c.cpp:651
void stk_kernel_process_hard_fault(stk_kernel_t *k)
Trigger a kernel hard fault (safe-state handler).
Definition stk_c.cpp:511
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:653
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:113
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.
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:664
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:625
bool stk_kernel_is_schedulable(const stk_kernel_t *k)
Test whether currently configured task set is schedulable.
Definition stk_c.cpp:409
void stk_cv_notify_all(stk_cv_t *cv)
Wake all tasks waiting on the condition variable.
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.
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_KERNEL_STATE_INACTIVE
not ready, stk_kernel_init() must be called
Definition stk_c.h:365
@ STK_KERNEL_STATE_READY
ready to start, stk_kernel_start() must be called
Definition stk_c.h:366
@ STK_KERNEL_STATE_RUNNING
initialized and running, stk_kernel_start() was called successfully
Definition stk_c.h:367
@ STK_KERNEL_STATE_SUSPENDED
scheduling suspended via stk_kernel_service_suspend() (tickless idle)
Definition stk_c.h:368
C-level callback table that mirrors stk::IPlatform::IEventOverrider.
Definition stk_c.h:490
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:506
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:498
void * user_data
Opaque pointer forwarded unchanged to every callback.
Definition stk_c.h:511
Opaque memory container for a Mutex instance.
Definition stk_c.h:847
stk_word_t data[(10U+((0) ? 1U :0U))]
Definition stk_c.h:848
Opaque memory container for SpinLock object.
Definition stk_c.h:899
stk_word_t data[(1)]
Definition stk_c.h:900
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:939
stk_word_t data[(7U+((0) ? 1U :0U))]
Definition stk_c.h:940
Opaque memory container for an Event instance.
Definition stk_c.h:987
stk_word_t data[(8U+((0) ? 1U :0U))]
Definition stk_c.h:988
Opaque memory container for a Semaphore instance.
Definition stk_c.h:1050
stk_word_t data[(8U+((0) ? 1U :0U))]
Definition stk_c.h:1051
Opaque memory container for an EventFlags instance.
Definition stk_c.h:1136
stk_word_t data[((7U+((0) ? 1U :0U))+1U+((0) ? 1U :0U))]
Definition stk_c.h:1137
Opaque memory container for a Pipe control-block.
Definition stk_c.h:1232
stk_word_t data[(6U+(2U *(7U+((0) ? 1U :0U)))+((0) ? 1U :0U))]
Definition stk_c.h:1233
Opaque memory container for a MessageQueue instance.
Definition stk_c.h:1488
stk_word_t data[(6+(2 *(7U+((0) ? 1U :0U)))+((0) ? 1 :0))]
Definition stk_c.h:1489
Opaque memory container for an RWMutex instance.
Definition stk_c.h:1736
stk_word_t data[(17U+((0) ? 3U :0U))]
Definition stk_c.h:1737