12#include "stk_config.h"
14#ifdef _STK_ARCH_X86_WIN32
21#define WIN32_LEAN_AND_MEAN
32#define WINAPI __stdcall
36typedef MMRESULT (WINAPI * timeBeginPeriodF)(UINT uPeriod);
37static timeBeginPeriodF timeBeginPeriod =
nullptr;
39#define STK_X86_WIN32_CRITICAL_SECTION CRITICAL_SECTION
40#define STK_X86_WIN32_CRITICAL_SECTION_INIT(SES) ::InitializeCriticalSection(SES)
41#define STK_X86_WIN32_CRITICAL_SECTION_START(SES) ::EnterCriticalSection(SES)
42#define STK_X86_WIN32_CRITICAL_SECTION_END(SES) ::LeaveCriticalSection(SES)
43#define STK_X86_WIN32_MIN_RESOLUTION (1000)
44#define STK_X86_WIN32_GET_SP(STACK) (STACK + 2)
45#define SLK_UNLOCKED hw::SpinLock::UNLOCKED
46#define SLK_LOCKED hw::SpinLock::LOCKED
52 return (InterlockedCompareExchange(
53 reinterpret_cast<volatile LONG *
>(&lock), SLK_LOCKED, SLK_UNLOCKED) == SLK_UNLOCKED);
60 uint8_t sleep_time = 0;
61 uint32_t timeout = 0xFFFFFF;
64 while (!HW_SpinLockTryLock(lock))
72 for (
volatile int32_t spin = 100; (spin != 0); spin--)
77 if (lock == SLK_UNLOCKED)
91 InterlockedExchange(
reinterpret_cast<volatile LONG *
>(&lock), SLK_UNLOCKED);
94struct Win32ScopedCriticalSection
96 STK_X86_WIN32_CRITICAL_SECTION &m_sec;
98 explicit Win32ScopedCriticalSection(STK_X86_WIN32_CRITICAL_SECTION &sec) : m_sec(sec)
100 STK_X86_WIN32_CRITICAL_SECTION_START(&sec);
102 ~Win32ScopedCriticalSection()
104 STK_X86_WIN32_CRITICAL_SECTION_END(&m_sec);
110 LARGE_INTEGER m_freq;
111 LARGE_INTEGER m_start;
114 explicit HiResClockQPC()
116 QueryPerformanceFrequency(&m_freq);
117 QueryPerformanceCounter(&m_start);
120 static HiResClockQPC *GetInstance()
124 static HiResClockQPC clock;
130 LARGE_INTEGER current;
131 QueryPerformanceCounter(¤t);
134 return static_cast<Cycles>(current.QuadPart - m_start.QuadPart);
137 uint32_t GetFrequency()
139 return static_cast<uint32_t
>(m_freq.QuadPart);
147 : m_overrider(nullptr),
148 m_sleep_trap(nullptr),
149 m_exit_trap(nullptr),
150 m_winmm_dll(nullptr),
151 m_timer_thread(nullptr),
152 m_tls(TLS_OUT_OF_INDEXES),
162 void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap, int32_t resolution_us)
164 PlatformContext::Initialize(handler, service, exit_trap, resolution_us);
166 m_overrider =
nullptr;
167 m_sleep_trap =
nullptr;
168 m_exit_trap =
nullptr;
169 m_winmm_dll =
nullptr;
170 m_timer_thread =
nullptr;
172 m_stop_signal =
false;
176 if ((m_tls = TlsAlloc()) == TLS_OUT_OF_INDEXES)
182 STK_X86_WIN32_CRITICAL_SECTION_INIT(&m_cs);
192 if (m_tls != TLS_OUT_OF_INDEXES)
198 void LoadWindowsAPI()
200 HMODULE winmm = GetModuleHandleA(
"Winmm");
201 if (winmm ==
nullptr)
202 m_winmm_dll = winmm = LoadLibraryA(
"Winmm.dll");
203 assert(winmm !=
nullptr);
205 timeBeginPeriod = (timeBeginPeriodF)GetProcAddress(winmm,
"timeBeginPeriod");
206 assert(timeBeginPeriod !=
nullptr);
211 void UnloadWindowsAPI()
213 if (m_winmm_dll !=
nullptr)
215 FreeLibrary(m_winmm_dll);
216 m_winmm_dll =
nullptr;
222 TaskContext() : m_task(nullptr), m_stack(nullptr), m_thread(nullptr), m_thread_id(0)
225 void Initialize(ITask *task, Stack *stack)
238 size_t stack_size = m_task->GetStackSize() *
sizeof(
Word);
240 m_thread = CreateThread(
nullptr, stack_size, &OnTaskRun,
this, CREATE_SUSPENDED, &m_thread_id);
243 static DWORD WINAPI OnTaskRun(LPVOID param)
245 ((TaskContext *)param)->m_task->Run();
255 bool InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task);
256 void ConfigureTime();
257 void StartActiveTask();
258 void CreateTimerThreadAndJoin();
261 void SwitchContext();
263 void Sleep(Timeout ticks);
265 IWaitObject *Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout);
267 Word GetCallerSP()
const;
272 return hw::PtrToWord(TlsGetValue(m_tls));
277 TlsSetValue(m_tls, hw::WordToPtr<void>(tp));
282 STK_X86_WIN32_CRITICAL_SECTION_START(&m_cs);
284 if (m_csu_nesting == 0)
287 if (GetCurrentThreadId() != m_timer_tid)
288 SuspendThread(m_timer_thread);
305 if (m_csu_nesting == 0)
308 if (GetCurrentThreadId() != m_timer_tid)
309 ResumeThread(m_timer_thread);
312 STK_X86_WIN32_CRITICAL_SECTION_END(&m_cs);
315 IPlatform::IEventOverrider *m_overrider;
319 HANDLE m_timer_thread;
321 std::list<TaskContext *> m_tasks;
322 std::vector<HANDLE> m_task_threads;
324 STK_X86_WIN32_CRITICAL_SECTION m_cs;
325 uint8_t m_csu_nesting;
327 volatile bool m_stop_signal;
329s_StkPlatformContext[1];
347static DWORD WINAPI TimerThread(LPVOID param)
351 DWORD wait_ms = (1U *
GetContext().m_tick_resolution) / 1000U;
352 GetContext().m_timer_tid = GetCurrentThreadId();
354 while (WaitForSingleObject(
GetContext().m_timer_thread, wait_ms) == WAIT_TIMEOUT)
365void Context::ConfigureTime()
368 if (m_tick_resolution < STK_X86_WIN32_MIN_RESOLUTION)
369 m_tick_resolution = STK_X86_WIN32_MIN_RESOLUTION;
375void Context::StartActiveTask()
381 ResumeThread(active_task->m_thread);
384void Context::CreateTimerThreadAndJoin()
388 m_handler->OnStart(m_stack_active);
393 m_timer_thread = CreateThread(
nullptr, 0, &TimerThread,
nullptr, 0,
nullptr);
395 SetThreadPriority(m_timer_thread, THREAD_PRIORITY_TIME_CRITICAL);
397 while (!m_task_threads.empty())
399 DWORD result = WaitForMultipleObjects((DWORD)m_task_threads.size(), m_task_threads.data(), FALSE, INFINITE);
404 Win32ScopedCriticalSection __cs(m_cs);
407 for (std::vector<HANDLE>::iterator itr = m_task_threads.begin(); itr != m_task_threads.end(); ++itr)
409 if (result == (WAIT_OBJECT_0 + i))
411 TaskContext *exiting_task =
nullptr;
412 for (std::list<TaskContext *>::iterator titr = m_tasks.begin(); titr != m_tasks.end(); ++titr)
414 if ((*titr)->m_thread == (*itr))
416 exiting_task = (*titr);
422 if (exiting_task !=
nullptr)
423 m_handler->OnTaskExit(exiting_task->m_stack);
425 m_task_threads.erase(itr);
435 if (m_timer_thread !=
nullptr)
436 WaitForSingleObject(m_timer_thread, INFINITE);
439void Context::Cleanup()
442 for (std::list<TaskContext *>::iterator itr = m_tasks.begin(); itr != m_tasks.end(); ++itr)
444 if ((*itr)->m_thread !=
nullptr)
446 CloseHandle((*itr)->m_thread);
447 (*itr)->m_thread =
nullptr;
453 if (m_timer_thread !=
nullptr)
455 CloseHandle(m_timer_thread);
456 m_timer_thread =
nullptr;
460 m_stop_signal =
false;
466void Context::ProcessTick()
468 Win32ScopedCriticalSection __cs(m_cs);
470 if (m_handler->OnTick(m_stack_idle, m_stack_active))
474void Context::SwitchContext()
477 if ((m_stack_idle != m_sleep_trap) && (m_stack_idle != m_exit_trap))
482 SuspendThread(idle_task->m_thread);
486 if (m_stack_active == m_sleep_trap)
488 if ((m_overrider ==
nullptr) || !m_overrider->OnSleep())
494 if (m_stack_active ==
GetContext().m_exit_trap)
503 ResumeThread(active_task->m_thread);
507Word Context::GetCallerSP()
const
510 DWORD calling_tid = GetCurrentThreadId();
512 Win32ScopedCriticalSection __cs(
const_cast<STK_X86_WIN32_CRITICAL_SECTION &
>(m_cs));
514 for (std::list<TaskContext *>::const_iterator itr = m_tasks.begin(), end = m_tasks.end(); itr != end; ++itr)
516 if ((*itr)->m_thread_id == calling_tid)
518 caller_sp =
hw::PtrToWord(STK_X86_WIN32_GET_SP((*itr)->m_task->GetStack()));
529TId Context::GetTid()
const
531 return m_handler->OnGetTid(GetCallerSP());
534void Context::SwitchToNext()
536 m_handler->OnTaskSwitch(GetCallerSP());
539void Context::Sleep(
Timeout ticks)
541 m_handler->OnTaskSleep(GetCallerSP(), ticks);
544void Context::SleepUntil(
Ticks timestamp)
546 m_handler->OnTaskSleepUntil(GetCallerSP(), timestamp);
551 return m_handler->OnTaskWait(GetCallerSP(), sync_obj, mutex, timeout);
556 m_stop_signal =
true;
562 InitStackMemory(stack_memory);
564 TaskContext *ctx =
reinterpret_cast<TaskContext *
>(STK_X86_WIN32_GET_SP(stack_memory->
GetStack()));
569 ctx->Initialize(user_task, stack);
571 m_tasks.push_back(ctx);
572 m_task_threads.push_back(ctx->m_thread);
592 GetContext().Initialize(event_handler, service, exit_trap, resolution_us);
609 return GetContext().InitStack(stack_type, stack, stack_memory, user_task);
634 return GetContext().Wait(sync_obj, mutex, timeout);
693 HW_SpinLockLock(m_lock);
698 HW_SpinLockUnlock(m_lock);
703 return HW_SpinLockTryLock(m_lock);
713 return HiResClockQPC::GetInstance()->GetCycles();
718 return HiResClockQPC::GetInstance()->GetFrequency();
Contains common inventory for platform implementation.
#define GetContext()
Get platform's context.
Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace.
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_KERNEL_PANIC(id)
Called when the kernel detects an unrecoverable internal fault.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define __stk_attr_noinline
Prevents compiler from inlining the decorated function (function prefix).
#define STK_CRITICAL_SECTION_NESTINGS_MAX
Maximum allowable recursion depth for critical section entry (default: 16).
#define __stk_attr_noreturn
Declares that function never returns to its caller (function prefix).
#define __stk_relax_cpu
Emits a CPU pipeline-relaxation hint for use inside hot busy-wait (spin) loops (in-code statement).
Namespace of STK package.
uintptr_t Word
Native processor word type.
void Sleep(uint32_t ticks)
Put calling process into a sleep state.
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
void SetTls(Word tp)
Set thread-local storage (TLS).
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
@ STACK_USER_TASK
Stack of the user task.
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Word GetTls()
Get thread-local storage (TLS).
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.
uint64_t Cycles
Cycles value.
EKernelPanicId
Identifies the source of a kernel panic.
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
__stk_forceinline T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
void SetTls(Word tp)
Write raw thread-pointer (TP) register used as per-task TLS storage.
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).
Namespace of the test inventory.
Base platform context for all platform implementations.
bool InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
Initialize stack memory of the user task.
TId GetTid() const
Get thread Id.
uint32_t GetTickResolution() const
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
void SetEventOverrider(IEventOverrider *overrider)
Set platform event overrider.
void Start()
Start scheduling.
void Stop()
Stop scheduling.
void SwitchToNext()
Switch to a next task.
void ProcessHardFault()
Cause a hard fault of the system.
void ProcessTick()
Process one tick.
IWaitObject * Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout)
void Sleep(Timeout ticks)
Put calling process into a sleep state.
void SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap)
Initialize scheduler's context.
Word GetCallerSP() const
Get caller's Stack Pointer (SP).
static void Exit()
Exit a critical section.
static void Enter()
Enter a critical section.
bool TryLock()
Attempt to acquire SpinLock in a single non-blocking attempt.
void Lock()
Acquire SpinLock, blocking until it is available.
void Unlock()
Release SpinLock, allowing another thread or core to acquire it.
static uint32_t GetFrequency()
Get clock frequency.
static Cycles GetCycles()
Get number of clock cycles elapsed.
Word SP
Stack Pointer (SP) register (note: must be the first entry in this struct).
Interface for a stack memory region.
virtual Word * GetStack() const =0
Get pointer to the stack memory.
Interface for mutex synchronization primitive.
Interface for a user task.
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
RISC-V specific event handler.