SuperTinyKernel™ RTOS 1.05.3
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™ (STK): 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#include <stdint.h>
14#include <stddef.h>
15#include <stdbool.h>
16#include <assert.h>
17
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33// ─────────────────────────────────────────────────────────────────────────────
34// Configuration macros (can be overridden before including this file)
35// ─────────────────────────────────────────────────────────────────────────────
36
42#ifndef STK_C_KERNEL_MAX_TASKS
43 #define STK_C_KERNEL_MAX_TASKS (4)
44#endif
45
50#ifndef STK_C_CPU_COUNT
51 #define STK_C_CPU_COUNT (1)
52#endif
53
57#if !defined(STK_SYNC_DEBUG_NAMES) && STK_SEGGER_SYSVIEW
58 #define STK_SYNC_DEBUG_NAMES (1)
59#elif !defined(STK_SYNC_DEBUG_NAMES)
60 #define STK_SYNC_DEBUG_NAMES (0)
61#endif
62
66#define STK_C_ASSERT(e) assert(e)
67
71#if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
72 #define __stk_c_stack_attr __attribute__((aligned(16)))
73#else
74 #define __stk_c_stack_attr
75#endif
76
77// ─────────────────────────────────────────────────────────────────────────────
78// Types
79// ─────────────────────────────────────────────────────────────────────────────
80
83typedef uintptr_t stk_word_t;
84
88
92
95typedef struct stk_task_t stk_task_t;
96
99#define STK_PERIODICITY_DEFAULT (1000U)
100
108typedef void (*stk_task_entry_t)(void *arg);
109
112#define STK_WAIT_INFINITE (INT32_MAX)
113
116#define STK_NO_WAIT (0)
117
118// ─────────────────────────────────────────────────────────────────────────────
119// Kernel factory functions
120// ─────────────────────────────────────────────────────────────────────────────
121
122/* Available kernel type definitions:
123
124 Kernel mode flags (may be OR-combined, subject to the constraints listed below):
125 KERNEL_STATIC — fixed task list; tasks must never return from their entry function.
126 KERNEL_DYNAMIC — tasks may be added/removed at runtime and may return when done.
127 KERNEL_HRT — Hard Real-Time mode; must be combined with KERNEL_STATIC or KERNEL_DYNAMIC.
128 KERNEL_SYNC — enables synchronization primitives (Mutex, Event, Semaphore, etc.).
129 KERNEL_TICKLESS — tickless low-power idle; suppresses the SysTick when all tasks sleep.
130 Requires STK_TICKLESS_IDLE=1 in stk_config.h.
131 INCOMPATIBLE with KERNEL_HRT (HRT requires a continuous tick).
132
133// Standard variants
134Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
135Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
136Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
137Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
138Kernel<KERNEL_STATIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
139Kernel<KERNEL_DYNAMIC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
140
141// HRT variants (KERNEL_TICKLESS must NOT be combined with any of these)
142Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
143Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
144Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
145Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
146Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
147Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
148Kernel<KERNEL_STATIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
149Kernel<KERNEL_DYNAMIC | KERNEL_HRT, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
150
151// Standard variants with KERNEL_SYNC
152Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
153Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
154Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
155Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
156Kernel<KERNEL_STATIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
157Kernel<KERNEL_DYNAMIC | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
158
159// HRT variants with KERNEL_SYNC
160Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
161Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
162Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
163Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRM, PlatformDefault>
164Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
165Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyDM, PlatformDefault>
166Kernel<KERNEL_STATIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
167Kernel<KERNEL_DYNAMIC | KERNEL_HRT | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyEDF, PlatformDefault>
168
169// Tickless variants (low-power; KERNEL_TICKLESS requires STK_TICKLESS_IDLE=1 in stk_config.h)
170Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
171Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
172Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
173Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
174Kernel<KERNEL_STATIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
175Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
176
177// Tickless variants with KERNEL_SYNC
178Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
179Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyRR, PlatformDefault>
180Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
181Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategySWRR, PlatformDefault>
182Kernel<KERNEL_STATIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
183Kernel<KERNEL_DYNAMIC | KERNEL_TICKLESS | KERNEL_SYNC, STK_C_KERNEL_MAX_TASKS, SwitchStrategyFP32, PlatformDefault>
184*/
185
209
216stk_kernel_t *stk_kernel_create(uint8_t core_nr);
217
218// ─────────────────────────────────────────────────────────────────────────────
219// Kernel control
220// ─────────────────────────────────────────────────────────────────────────────
221
227void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us);
228
235
245 stk_task_t *task,
246 int32_t periodicity_ticks,
247 int32_t deadline_ticks,
248 int32_t start_delay_ticks);
249
256
263
273
279
286
287// ─────────────────────────────────────────────────────────────────────────────
288// Task creation
289// ─────────────────────────────────────────────────────────────────────────────
290
299 void *arg,
300 stk_word_t *stack,
301 uint32_t stack_size);
302
311 void *arg,
312 stk_word_t *stack,
313 uint32_t stack_size);
314
321void stk_task_set_weight(stk_task_t *task, uint32_t weight);
322
328void stk_task_set_priority(stk_task_t *task, uint8_t priority);
329
334void stk_task_set_id(stk_task_t *task, uint32_t tid);
335
340void stk_task_set_name(stk_task_t *task, const char *tname);
341
342// ─────────────────────────────────────────────────────────────────────────────
343// Services available from inside tasks
344// ─────────────────────────────────────────────────────────────────────────────
345
349stk_tid_t stk_tid(void);
350
354int64_t stk_ticks(void);
355
359int32_t stk_tick_resolution(void);
360
368int64_t stk_ticks_from_ms(int64_t msec);
369
378static inline int64_t stk_ticks_from_ms_r(int64_t msec, int32_t resolution)
379{
380 return msec * 1000 / resolution;
381}
382
386int64_t stk_time_now_ms(void);
387
391void stk_delay(uint32_t ticks);
392
396void stk_delay_ms(uint32_t ms);
397
408void stk_sleep(uint32_t ticks);
409
420void stk_sleep_ms(uint32_t ms);
421
432void stk_sleep_until(int64_t ts);
433
436void stk_yield(void);
437
438// ─────────────────────────────────────────────────────────────────────────────
439// Dynamic cleanup
440// ─────────────────────────────────────────────────────────────────────────────
441
448
454void stk_task_destroy(stk_task_t *task);
455
456// ─────────────────────────────────────────────────────────────────────────────
457// Thread-Local Storage (very simple / one pointer per task)
458// ─────────────────────────────────────────────────────────────────────────────
459
463void *stk_tls_get(void);
464
468void stk_tls_set(void *ptr);
469
473#define STK_TLS_GET(type) ((type *)stk_tls_get())
474
478#define STK_TLS_SET(ptr) stk_tls_set((void *)(ptr))
479
496
497// ─────────────────────────────────────────────────────────────────────────────
498// Synchronization Primitives
499// ─────────────────────────────────────────────────────────────────────────────
500
501// ───── Critical Section ──────────────────────────────────────────────────────
502
507
512
513// ───── Mutex ─────────────────────────────────────────────────────────────────
514
517#define STK_MUTEX_IMPL_SIZE (10 + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
518
524
528
534stk_mutex_t *stk_mutex_create(stk_mutex_mem_t *memory, uint32_t memory_size);
535
540
544void stk_mutex_lock(stk_mutex_t *mtx);
545
551
556
562bool stk_mutex_timed_lock(stk_mutex_t *mtx, int32_t timeout);
563
564// ───── SpinLock ──────────────────────────────────────────────────────────────
565
568#define STK_SPINLOCK_IMPL_SIZE (1)
569
576
580
586stk_spinlock_t *stk_spinlock_create(stk_spinlock_mem_t *memory, uint32_t memory_size);
587
591
595
600
604
605// ───── Condition Variable ────────────────────────────────────────────────────
606
609#define STK_CV_IMPL_SIZE (7 + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
610
616
619typedef struct stk_cv_t stk_cv_t;
620
626stk_cv_t *stk_cv_create(stk_cv_mem_t *memory, uint32_t memory_size);
627
631void stk_cv_destroy(stk_cv_t *cv);
632
641bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, int32_t timeout);
642
647
652
653// ───── Event ─────────────────────────────────────────────────────────────────
654
657#define STK_EVENT_IMPL_SIZE (8 + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
658
664
668
675stk_event_t *stk_event_create(stk_event_mem_t *memory, uint32_t memory_size, bool manual_reset);
676
681
687bool stk_event_wait(stk_event_t *ev, int32_t timeout);
688
694
698void stk_event_set(stk_event_t *ev);
699
704
709
710// ───── Semaphore ─────────────────────────────────────────────────────────────
711
714#define STK_SEM_IMPL_SIZE (8 + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
715
721
724typedef struct stk_sem_t stk_sem_t;
725
732stk_sem_t *stk_sem_create(stk_sem_mem_t *memory, uint32_t memory_size, uint32_t initial_count);
733
737void stk_sem_destroy(stk_sem_t *sem);
738
744bool stk_sem_wait(stk_sem_t *sem, int32_t timeout);
745
749void stk_sem_signal(stk_sem_t *sem);
750
751// ───── EventFlags ────────────────────────────────────────────────────────────
752
755#define STK_EF_OPT_WAIT_ANY (0x00000000U)
756#define STK_EF_OPT_WAIT_ALL (0x00000001U)
757#define STK_EF_OPT_NO_CLEAR (0x00000002U)
758
761#define STK_EF_ERROR_PARAMETER (0x80000001U)
762#define STK_EF_ERROR_TIMEOUT (0x80000002U)
763#define STK_EF_ERROR_ISR (0x80000004U)
764#define STK_EF_ERROR_MASK (0x80000000U)
765
769static inline bool stk_ef_is_error(uint32_t result) { return ((result & STK_EF_ERROR_MASK) != 0U); }
770
775#define STK_EF_IMPL_SIZE (STK_CV_IMPL_SIZE + 1 + (STK_SYNC_DEBUG_NAMES ? 1 : 0))
776
782
785typedef struct stk_ef_t stk_ef_t;
786
794stk_ef_t *stk_ef_create(stk_ef_mem_t *memory, uint32_t memory_size, uint32_t initial_flags);
795
799void stk_ef_destroy(stk_ef_t *ef);
800
809uint32_t stk_ef_set(stk_ef_t *ef, uint32_t flags);
810
818uint32_t stk_ef_clear(stk_ef_t *ef, uint32_t flags);
819
825uint32_t stk_ef_get(stk_ef_t *ef);
826
844uint32_t stk_ef_wait(stk_ef_t *ef, uint32_t flags, uint32_t options, int32_t timeout);
845
857uint32_t stk_ef_trywait(stk_ef_t *ef, uint32_t flags, uint32_t options);
858
859// ───── Pipe (FIFO) ───────────────────────────────────────────────────────────
860
864#define STK_PIPE_SIZE 16
865
869#define STK_PIPE_IMPL_SIZE ((27 + (STK_SYNC_DEBUG_NAMES ? 3 : 0)) + STK_PIPE_SIZE)
870
876
879typedef struct stk_pipe_t stk_pipe_t;
880
886stk_pipe_t *stk_pipe_create(stk_pipe_mem_t *memory, uint32_t memory_size);
887
891void stk_pipe_destroy(stk_pipe_t *pipe);
892
899bool stk_pipe_write(stk_pipe_t *pipe, stk_word_t data, int32_t timeout);
900
907bool stk_pipe_read(stk_pipe_t *pipe, stk_word_t *data, int32_t timeout);
908
916size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const stk_word_t *src, size_t count, int32_t timeout);
917
925size_t stk_pipe_read_bulk(stk_pipe_t *pipe, stk_word_t *dst, size_t count, int32_t timeout);
926
931size_t stk_pipe_get_size(stk_pipe_t *pipe);
932
933// ───── RWMutex (Reader-Writer Lock) ──────────────────────────────────────────
934
937#define STK_RWMUTEX_IMPL_SIZE (17 + (STK_SYNC_DEBUG_NAMES ? 3 : 0))
938
944
948
954stk_rwmutex_t *stk_rwmutex_create(stk_rwmutex_mem_t *memory, uint32_t memory_size);
955
960
967
973
979bool stk_rwmutex_timed_read_lock(stk_rwmutex_t *rw, int32_t timeout);
980
986
993
999
1005bool stk_rwmutex_timed_lock(stk_rwmutex_t *rw, int32_t timeout);
1006
1013
1014#ifdef __cplusplus
1015}
1016#endif
1017
1019
1020#endif /* STK_C_H_ */
void stk_event_set(stk_event_t *ev)
Set the event to signaled state.
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:272
void stk_kernel_add_task(stk_kernel_t *k, stk_task_t *task)
Add task to non-HRT kernel (static or dynamic).
Definition stk_c.cpp:225
_EKernelState
Kernel state.
Definition stk_c.h:268
void stk_sem_signal(stk_sem_t *sem)
Signal/Release a semaphore resource.
void stk_delay(uint32_t ticks)
Busy-wait delay (other tasks continue to run).
Definition stk_c.cpp:328
uint32_t stk_ef_set(stk_ef_t *ef, uint32_t flags)
Set one or more flags.
stk_spinlock_t * stk_spinlock_create(stk_spinlock_mem_t *memory, uint32_t memory_size)
Create a recursive SpinLock.
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.
stk_tid_t stk_tid(void)
Returns current task/thread ID (the value set by stk_task_set_id).
Definition stk_c.cpp:323
bool stk_rwmutex_try_read_lock(stk_rwmutex_t *rw)
Try to acquire the read lock without blocking.
stk_ef_t * stk_ef_create(stk_ef_mem_t *memory, uint32_t memory_size, uint32_t initial_flags)
Create an EventFlags object (using provided memory).
bool stk_spinlock_trylock(stk_spinlock_t *lock)
Attempt to acquire the SpinLock immediately.
void stk_ef_destroy(stk_ef_t *ef)
Destroy an EventFlags object.
void * stk_tls_get(void)
Get thread-local pointer (platform-specific slot).
Definition stk_c.cpp:338
void stk_event_reset(stk_event_t *ev)
Reset the event to non-signaled state.
bool stk_rwmutex_trylock(stk_rwmutex_t *rw)
Try to acquire the write lock without blocking.
stk_pipe_t * stk_pipe_create(stk_pipe_mem_t *memory, uint32_t memory_size)
Create a Pipe (using provided memory).
bool stk_pipe_read(stk_pipe_t *pipe, stk_word_t *data, int32_t timeout)
Read data from the pipe.
uint32_t stk_ef_trywait(stk_ef_t *ef, uint32_t flags, uint32_t options)
Non-blocking flag poll.
#define STK_SEM_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Semaphore instance.
Definition stk_c.h:714
void stk_kernel_remove_task(stk_kernel_t *k, stk_task_t *task)
Remove finished task from dynamic kernel.
Definition stk_c.cpp:233
void stk_kernel_destroy(stk_kernel_t *k)
Destroy dynamic kernel instance (only when not running).
Definition stk_c.cpp:186
void stk_cv_notify_one(stk_cv_t *cv)
Wake one task waiting on the condition variable.
bool stk_rwmutex_timed_lock(stk_rwmutex_t *rw, int32_t timeout)
Try to acquire the write lock with a timeout.
bool stk_rwmutex_timed_read_lock(stk_rwmutex_t *rw, int32_t timeout)
Try to acquire the read lock with a timeout.
#define STK_EVENT_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Event instance.
Definition stk_c.h:657
bool stk_event_trywait(stk_event_t *ev)
Wait for the event to become signaled.
uint32_t stk_ef_wait(stk_ef_t *ef, uint32_t flags, uint32_t options, int32_t timeout)
Wait for one or more flags to be set.
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:91
int64_t stk_time_now_ms(void)
Returns current time in milliseconds since kernel start.
Definition stk_c.cpp:326
void stk_event_destroy(stk_event_t *ev)
Destroy an Event.
void stk_task_set_weight(stk_task_t *task, uint32_t weight)
Set task weight (used only by Smooth Weighted Round Robin).
Definition stk_c.cpp:284
stk_rwmutex_t * stk_rwmutex_create(stk_rwmutex_mem_t *memory, uint32_t memory_size)
Create an RWMutex (using provided memory).
void stk_yield(void)
Voluntarily give up CPU to another ready task (cooperative yield).
Definition stk_c.cpp:333
void stk_sleep(uint32_t ticks)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:329
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:203
void stk_rwmutex_lock(stk_rwmutex_t *rw)
Acquire the lock for exclusive writing. Blocks until available.
stk_sem_t * stk_sem_create(stk_sem_mem_t *memory, uint32_t memory_size, uint32_t initial_count)
Create a Semaphore (using provided memory).
void stk_cv_destroy(stk_cv_t *cv)
Destroy a Condition Variable.
void stk_task_set_id(stk_task_t *task, uint32_t tid)
Assign application-defined task ID (for tracing/debugging).
Definition stk_c.cpp:299
size_t stk_pipe_get_size(stk_pipe_t *pipe)
Get current number of elements in the pipe.
void stk_task_destroy(stk_task_t *task)
Destroy dynamically created task object.
Definition stk_c.cpp:313
void stk_spinlock_unlock(stk_spinlock_t *lock)
Release the SpinLock.
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.
int64_t stk_ticks_from_ms(int64_t msec)
Get ticks from milliseconds using current kernel tick resolution.
Definition stk_c.cpp:327
void stk_critical_section_exit(void)
Leave critical section — re-enable context switches.
Definition stk_c.cpp:356
stk_mutex_t * stk_mutex_create(stk_mutex_mem_t *memory, uint32_t memory_size)
Create a Mutex (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.
void stk_spinlock_destroy(stk_spinlock_t *lock)
Destroy the SpinLock.
#define STK_MUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Mutex instance.
Definition stk_c.h:517
EKernelState stk_kernel_get_state(const stk_kernel_t *k)
Get state of the scheduler.
Definition stk_c.cpp:210
stk_cv_t * stk_cv_create(stk_cv_mem_t *memory, uint32_t memory_size)
Create a Condition Variable (using provided memory).
#define STK_EF_ERROR_MASK
Definition stk_c.h:764
bool stk_sem_wait(stk_sem_t *sem, int32_t timeout)
Wait for a semaphore resource.
size_t stk_pipe_read_bulk(stk_pipe_t *pipe, stk_word_t *dst, size_t count, int32_t timeout)
Read multiple elements from the pipe.
int32_t stk_tick_resolution(void)
Returns how many microseconds correspond to one kernel tick.
Definition stk_c.cpp:325
#define STK_CV_IMPL_SIZE
A memory size (multiples of stk_word_t) required for ConditionVariable instance.
Definition stk_c.h:609
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:196
void stk_task_set_name(stk_task_t *task, const char *tname)
Assign human-readable task name (for tracing/debugging).
Definition stk_c.cpp:306
void stk_spinlock_lock(stk_spinlock_t *lock)
Acquire the SpinLock (recursive).
void(* stk_task_entry_t)(void *arg)
Task entry point function type.
Definition stk_c.h:108
int64_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
Definition stk_c.cpp:324
void stk_sem_destroy(stk_sem_t *sem)
Destroy a Semaphore.
void stk_sleep_until(int64_t ts)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:332
stk_event_t * stk_event_create(stk_event_mem_t *memory, uint32_t memory_size, bool manual_reset)
Create an Event (using provided memory).
void stk_tls_set(void *ptr)
Set thread-local pointer.
Definition stk_c.cpp:343
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:260
#define __stk_c_stack_attr
Stack attribute (applies required alignment).
Definition stk_c.h:74
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:87
bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, int32_t timeout)
Wait for a signal on the condition variable.
enum _EKernelState EKernelState
Kernel state.
#define STK_PIPE_IMPL_SIZE
A memory size (multiples of stk_word_t) required for Pipe instance.
Definition stk_c.h:869
#define STK_SPINLOCK_IMPL_SIZE
A memory size (multiples of stk_word_t) required for SpinLock instance.
Definition stk_c.h:568
void stk_event_pulse(stk_event_t *ev)
Pulse the event (signal then immediately reset).
size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const stk_word_t *src, size_t count, int32_t timeout)
Write multiple elements to the pipe.
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:153
uintptr_t stk_word_t
CPU register type.
Definition stk_c.h:83
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:769
void stk_delay_ms(uint32_t ms)
Busy-wait delay (other tasks continue to run).
Definition stk_c.cpp:330
#define STK_RWMUTEX_IMPL_SIZE
A memory size (multiples of stk_word_t) required for RWMutex instance.
Definition stk_c.h:937
#define STK_EF_IMPL_SIZE
A memory size (multiples of stk_word_t) required for EventFlags instance.
Definition stk_c.h:775
void stk_rwmutex_destroy(stk_rwmutex_t *rw)
Destroy an RWMutex.
void stk_sleep_ms(uint32_t ms)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:331
void stk_critical_section_enter(void)
Enter critical section — disable context switches on current core.
Definition stk_c.cpp:351
void stk_kernel_add_task_hrt(stk_kernel_t *k, stk_task_t *task, 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:241
void stk_task_set_priority(stk_task_t *task, uint8_t priority)
Set task priority (used only by Fixed Priority scheduler).
Definition stk_c.cpp:292
bool stk_pipe_write(stk_pipe_t *pipe, stk_word_t data, int32_t timeout)
Write data to the pipe.
bool stk_event_wait(stk_event_t *ev, int32_t timeout)
Wait for the event to become signaled.
static int64_t stk_ticks_from_ms_r(int64_t msec, int32_t resolution)
Get ticks from milliseconds using an explicit tick resolution.
Definition stk_c.h:378
bool stk_kernel_is_schedulable(const stk_kernel_t *k)
Test whether currently configured task set is schedulable.
Definition stk_c.cpp:217
void stk_cv_notify_all(stk_cv_t *cv)
Wake all tasks waiting on the condition variable.
bool stk_mutex_timed_lock(stk_mutex_t *mtx, int32_t timeout)
Try to lock the mutex with a timeout.
void stk_rwmutex_unlock(stk_rwmutex_t *rw)
Release the exclusive writer lock.
@ STK_KERNEL_STATE_INACTIVE
not ready, stk_kernel_init() must be called
Definition stk_c.h:269
@ STK_KERNEL_STATE_READY
ready to start, stk_kernel_start() must be called
Definition stk_c.h:270
@ STK_KERNEL_STATE_RUNNING
initialized and running, stk_kernel_start() was called successfully
Definition stk_c.h:271
Opaque memory container for a Mutex instance.
Definition stk_c.h:521
stk_word_t data[(10+((0) ? 1 :0))]
Definition stk_c.h:522
Opaque memory container for SpinLock object.
Definition stk_c.h:573
stk_word_t data[(1)]
Definition stk_c.h:574
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:613
stk_word_t data[(7+((0) ? 1 :0))]
Definition stk_c.h:614
Opaque memory container for an Event instance.
Definition stk_c.h:661
stk_word_t data[(8+((0) ? 1 :0))]
Definition stk_c.h:662
Opaque memory container for a Semaphore instance.
Definition stk_c.h:718
stk_word_t data[(8+((0) ? 1 :0))]
Definition stk_c.h:719
Opaque memory container for an EventFlags instance.
Definition stk_c.h:779
stk_word_t data[((7+((0) ? 1 :0))+1+((0) ? 1 :0))]
Definition stk_c.h:780
Opaque memory container for a Pipe instance.
Definition stk_c.h:873
stk_word_t data[((27+((0) ? 3 :0))+16)]
Definition stk_c.h:874
Opaque memory container for an RWMutex instance.
Definition stk_c.h:941
stk_word_t data[(17+((0) ? 3 :0))]
Definition stk_c.h:942