12#include <stk_config.h>
17#define STK_C_TASKS_MAX (STK_C_KERNEL_MAX_TASKS)
19inline void *
operator new(std::size_t,
void *ptr)
noexcept {
return ptr; }
20inline void operator delete(
void *,
void *)
noexcept { }
142#define STK_KERNEL_CASE(X) \
145 static_assert(sizeof(STK_C_KERNEL_TYPE_CPU_##X) % sizeof(stk_word_t) == 0, \
146 "Kernel memory size must be multiple of stk_word_t"); \
147 alignas(alignof(STK_C_KERNEL_TYPE_CPU_##X)) \
148 static stk_word_t kernel_##X##_mem[sizeof(STK_C_KERNEL_TYPE_CPU_##X) / sizeof(stk_word_t)]; \
149 IKernel *kernel = new (kernel_##X##_mem) STK_C_KERNEL_TYPE_CPU_##X(); \
150 return reinterpret_cast<stk_kernel_t *>(kernel); \
157#ifdef STK_C_KERNEL_TYPE_CPU_0
160#ifdef STK_C_KERNEL_TYPE_CPU_1
163#ifdef STK_C_KERNEL_TYPE_CPU_2
166#ifdef STK_C_KERNEL_TYPE_CPU_3
169#ifdef STK_C_KERNEL_TYPE_CPU_4
172#ifdef STK_C_KERNEL_TYPE_CPU_5
175#ifdef STK_C_KERNEL_TYPE_CPU_6
178#ifdef STK_C_KERNEL_TYPE_CPU_7
200 reinterpret_cast<stk::IKernel *
>(k)->Initialize(tick_period_us);
243 int32_t periodicity_ticks,
244 int32_t deadline_ticks,
245 int32_t start_delay_ticks)
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Collection of synchronization primitives (stk::sync namespace).
C language binding/interface for SuperTinyKernel (STK).
static volatile bool s_TaskPoolLock
static stk_task_t * AllocateTask(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size, EAccessMode mode)
#define STK_KERNEL_CASE(X)
static void FreeTask(const stk_task_t *task)
static TaskSlot s_Tasks[(STK_C_KERNEL_MAX_TASKS)]
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.
void stk_kernel_add_task(stk_kernel_t *k, stk_task_t *task)
Add task to non-HRT kernel (static or dynamic).
void stk_delay(uint32_t ticks)
Busy-wait delay (other tasks continue to run).
stk_tid_t stk_tid(void)
Returns current task/thread ID (the value set by stk_task_set_id).
void * stk_tls_get(void)
Get thread-local pointer (platform-specific slot).
void stk_kernel_remove_task(stk_kernel_t *k, stk_task_t *task)
Remove finished task from dynamic kernel.
void stk_kernel_destroy(stk_kernel_t *k)
Destroy dynamic kernel instance (only when not running).
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
int64_t stk_time_now_ms(void)
Returns current time in milliseconds since kernel start.
void stk_task_set_weight(stk_task_t *task, uint32_t weight)
Set task weight (used only by Smooth Weighted Round Robin).
void stk_yield(void)
Voluntarily give up CPU to another ready task (cooperative yield).
void stk_sleep(uint32_t ticks)
Put current task to sleep (non-HRT kernels only).
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
void stk_task_set_id(stk_task_t *task, uint32_t tid)
Assign application-defined task ID (for tracing/debugging).
void stk_task_destroy(stk_task_t *task)
Destroy dynamically created task object.
int64_t stk_ticks_from_ms(int64_t msec)
Get ticks from milliseconds using current kernel tick resolution.
void stk_critical_section_exit(void)
Leave critical section — re-enable context switches.
EKernelState stk_kernel_get_state(const stk_kernel_t *k)
Get state of the scheduler.
int32_t stk_tick_resolution(void)
Returns how many microseconds correspond to one kernel tick.
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
void stk_task_set_name(stk_task_t *task, const char *tname)
Assign human-readable task name (for tracing/debugging).
void(* stk_task_entry_t)(void *arg)
Task entry point function type.
int64_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
void stk_sleep_until(int64_t ts)
Put current task to sleep (non-HRT kernels only).
void stk_tls_set(void *ptr)
Set thread-local pointer.
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.
stk_word_t stk_tid_t
Task id.
enum _EKernelState EKernelState
Kernel state.
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
uintptr_t stk_word_t
CPU register type.
void stk_delay_ms(uint32_t ms)
Busy-wait delay (other tasks continue to run).
void stk_sleep_ms(uint32_t ms)
Put current task to sleep (non-HRT kernels only).
void stk_critical_section_enter(void)
Enter critical section — disable context switches on current core.
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).
void stk_task_set_priority(stk_task_t *task, uint8_t priority)
Set task priority (used only by Fixed Priority scheduler).
static int64_t stk_ticks_from_ms_r(int64_t msec, int32_t resolution)
Get ticks from milliseconds using an explicit tick resolution.
bool stk_kernel_is_schedulable(const stk_kernel_t *k)
Test whether currently configured task set is schedulable.
Namespace of STK package.
static int64_t GetTimeNowMs()
Get current time in milliseconds since kernel start.
void Sleep(uint32_t ticks)
Put calling process into a sleep state.
static void DelayMs(uint32_t ms)
Delay calling process by busy-waiting until the deadline expires.
Ticks GetTicks()
Get number of ticks elapsed since kernel start.
void Yield()
Notify scheduler to switch to the next runnable task.
void Delay(uint32_t ticks)
Delay calling process by busy-waiting until the deadline expires.
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
TId GetTid()
Get task/thread Id of the calling task.
int32_t GetTickResolution()
Get number of microseconds in one tick.
static void SleepMs(uint32_t ms)
Put calling process into a sleep state.
EAccessMode
Hardware access mode by the user task.
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details).
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
__stk_forceinline void SetTlsPtr(const _TyTls *tp)
Type-safe wrapper around SetTls() that stores a typed pointer as the raw TP value.
__stk_forceinline _TyTls * GetTlsPtr()
Type-safe wrapper around GetTls() that casts the raw TP value to a typed pointer.
static void Exit()
Exit a critical section.
static void Enter()
Enter a critical section.
Interface for a user task.
Interface for the implementation of the kernel of the scheduler. It supports Soft and Hard Real-Time ...
virtual EState GetState() const =0
Get a snapshot of the kernel state.
static SchedulabilityCheckResult< _TaskCount > IsSchedulableWCRT(const ITaskSwitchStrategy *strategy)
Perform WCRT schedulability analysis on the task set registered with strategy.
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
void SetId(stk_tid_t tid)
void SetName(const char *tname)
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
EAccessMode GetAccessMode() const
Get hardware access mode of the user task.
void Run()
Entry point of the user task.
int32_t GetWeight() const
Get static base weight of the task.
const char * GetTraceName() const
Get task trace name set by application.
stk_word_t * GetStack() const
Get pointer to the stack memory.
size_t GetStackSize() const
Get number of elements of the stack memory array.
void Initialize(stk_task_entry_t func, void *user_data, stk_word_t *stack, size_t stack_size, EAccessMode mode)
stk_tid_t GetId() const
Get task Id set by application.
void OnDeadlineMissed(uint32_t duration)
Called by the scheduler if deadline of the task is missed when Kernel is operating in Hard Real-Time ...
void SetWeight(int32_t weight)