34#ifdef _STK_ARCH_ARM_CORTEX_M
36#define _STK_ARCH_DEFINED
38#ifdef _STK_ARCH_RISC_V
40#define _STK_ARCH_DEFINED
42#ifdef _STK_ARCH_X86_WIN32
44#define _STK_ARCH_DEFINED
63#define STK_KERNEL_PANIC(id) \
65 __stk_debug_break(); \
66 STK_PANIC_HANDLER(id); \
97 return reinterpret_cast<Word>(ptr);
114 return reinterpret_cast<T *
>(value);
129#if !_STK_INLINE_TLS_DEFINED
157template <
class _TyTls>
169template <
class _TyTls>
330#ifdef _STK_ARCH_X86_WIN32
331 volatile long m_lock;
362 if (
sizeof(
void *) == 8)
371 volatile const uint32_t *plo = &((
volatile const uint32_t *)addr)[
STK_ENDIAN_IDX_LO];
372 volatile const uint32_t *phi = &((
volatile const uint32_t *)addr)[
STK_ENDIAN_IDX_HI];
378 __stk_full_memfence();
381 __stk_full_memfence();
383 while (hi != (*phi));
385 return ((uint64_t)hi << 32) | lo;
416 if (
sizeof(
void *) == 8)
427 (*phi) = (uint32_t)(value >> 32);
428 __stk_full_memfence();
430 (*plo) = (uint32_t)value;
478#if (STK_ARCH_CPU_COUNT > 1)
484 while (itr !=
nullptr)
488 if (!itr->
Tick(elapsed_ticks))
499#ifndef STK_PANIC_HANDLER
509 #define STK_PANIC_HANDLER(id) STK_PANIC_HANDLER_DEFAULT(id)
Platform port for ARM Cortex-M.
Platform port for RISC-V.
Platform port for Windows Win32 (STK emulator).
void STK_PANIC_HANDLER_DEFAULT(stk::EKernelPanicId id)
Default panic handler: disable interrupts, record the id, and spin in a tight loop — a defined,...
#define STK_STATIC_ASSERT_N(NAME, X)
Compile-time assertion with a user-defined name suffix.
#define STK_ENDIAN_IDX_LO
Array index of the low 32-bit word when a 64-bit value is viewed as uint32_t[2].
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define STK_ENDIAN_IDX_HI
Array index of the high 32-bit word when a 64-bit value is viewed as uint32_t[2].
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Namespace of STK package.
uintptr_t Word
Native processor word type.
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
uint64_t Cycles
Cycles value.
EKernelPanicId
Identifies the source of a kernel panic.
Hardware Abstraction Layer (HAL) for architecture-specific operations.
__stk_forceinline T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
__stk_forceinline void WriteVolatile64(volatile T *addr, T value)
Atomically write a 64-bit volatile value.
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
__stk_forceinline void SetTlsPtr(const _TyTls *tp)
Type-safe wrapper around SetTls() that stores a typed pointer as the raw TP value.
void SetTls(Word tp)
Write raw thread-pointer (TP) register used as per-task TLS storage.
__stk_forceinline _TyTls * GetTlsPtr()
Type-safe wrapper around GetTls() that casts the raw TP value to a typed pointer.
Word GetTls()
Read raw thread-pointer (TP) register used as per-task TLS storage.
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
__stk_forceinline T ReadVolatile64(volatile const T *addr)
Atomically read a 64-bit volatile value.
STK_NONCOPYABLE_CLASS(CriticalSection)
static void Exit()
Exit a critical section.
static void Enter()
Enter a critical section.
RAII guard that enters the critical section on construction and exits it on destruction.
STK_NONCOPYABLE_CLASS(ScopedLock)
ScopedLock()
Enter the critical section.
~ScopedLock()
Exit the critical section.
bool TryLock()
Attempt to acquire SpinLock in a single non-blocking attempt.
SpinLock()
Construct a SpinLock (unlocked by default).
EState
Internal lock state values.
@ UNLOCKED
Lock is free and available for acquisition.
@ LOCKED
Lock is held by a thread or core.
STK_NONCOPYABLE_CLASS(SpinLock)
bool IsLocked() const
Sample current lock state.
volatile bool m_lock __stk_aligned(8)
Lock state (see EState). 8-byte aligned to occupy its own cache line word and avoid false sharing on ...
void Lock()
Acquire SpinLock, blocking until it is available.
void Unlock()
Release SpinLock, allowing another thread or core to acquire it.
High-resolution clock for high-precision measurements.
static uint32_t GetFrequency()
Get clock frequency.
static Ticks GetTimeUs()
Get elapsed time in microseconds.
static Cycles GetCycles()
Get number of clock cycles elapsed.
virtual void Wake(bool timeout)=0
Wake task.
virtual bool Tick(Timeout elapsed_ticks)=0
Update wait object's waiting time.
IWaitObject::ListHeadType m_wait_list
tasks blocked on this object
virtual bool Tick(Timeout elapsed_ticks)
Called by kernel on every system tick to handle timeout logic of waiting tasks.
DLEntryType * GetNext() const
Get the next entry in the list.