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;
175 #if STK_TICKLESS_IDLE
179 if ((m_tls = TlsAlloc()) == TLS_OUT_OF_INDEXES)
185 STK_X86_WIN32_CRITICAL_SECTION_INIT(&m_cs);
195 if (m_tls != TLS_OUT_OF_INDEXES)
201 void LoadWindowsAPI()
203 HMODULE winmm = GetModuleHandleA(
"Winmm");
204 if (winmm ==
nullptr)
205 m_winmm_dll = winmm = LoadLibraryA(
"Winmm.dll");
206 assert(winmm !=
nullptr);
208 timeBeginPeriod = (timeBeginPeriodF)GetProcAddress(winmm,
"timeBeginPeriod");
209 assert(timeBeginPeriod !=
nullptr);
214 void UnloadWindowsAPI()
216 if (m_winmm_dll !=
nullptr)
218 FreeLibrary(m_winmm_dll);
219 m_winmm_dll =
nullptr;
225 TaskContext() : m_task(nullptr), m_stack(nullptr), m_thread(nullptr), m_thread_id(0)
228 void Initialize(ITask *task, Stack *stack)
241 size_t stack_size = m_task->GetStackSize() *
sizeof(
Word);
243 m_thread = CreateThread(
nullptr, stack_size, &OnTaskRun,
this, CREATE_SUSPENDED, &m_thread_id);
246 static DWORD WINAPI OnTaskRun(LPVOID param)
248 ((TaskContext *)param)->m_task->Run();
258 bool InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task);
259 void ConfigureTime();
260 void StartActiveTask();
261 void CreateTimerThreadAndJoin();
264 void SwitchContext();
266 void Sleep(Timeout ticks);
268 IWaitObject *Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout);
270 Word GetCallerSP()
const;
275 return hw::PtrToWord(TlsGetValue(m_tls));
280 TlsSetValue(m_tls, hw::WordToPtr<void>(tp));
285 STK_X86_WIN32_CRITICAL_SECTION_START(&m_cs);
287 if (m_csu_nesting == 0)
290 if (GetCurrentThreadId() != m_timer_tid)
291 SuspendThread(m_timer_thread);
308 if (m_csu_nesting == 0)
311 if (GetCurrentThreadId() != m_timer_tid)
312 ResumeThread(m_timer_thread);
315 STK_X86_WIN32_CRITICAL_SECTION_END(&m_cs);
318 IPlatform::IEventOverrider *m_overrider;
322 HANDLE m_timer_thread;
324 std::list<TaskContext *> m_tasks;
325 std::vector<HANDLE> m_task_threads;
330 STK_X86_WIN32_CRITICAL_SECTION m_cs;
331 uint8_t m_csu_nesting;
333 volatile bool m_stop_signal;
335s_StkPlatformContext[1];
355 return (ticks *
GetContext().m_tick_resolution) / 1000U;
358static DWORD WINAPI TimerThread(LPVOID param)
362 DWORD wait_ms = TicksToMs(1U);
363 GetContext().m_timer_tid = GetCurrentThreadId();
365 while (WaitForSingleObject(
GetContext().m_timer_thread, wait_ms) == WAIT_TIMEOUT)
372 #if STK_TICKLESS_IDLE
373 wait_ms = TicksToMs(
GetContext().m_sleep_ticks);
380void Context::ConfigureTime()
383 if (m_tick_resolution < STK_X86_WIN32_MIN_RESOLUTION)
384 m_tick_resolution = STK_X86_WIN32_MIN_RESOLUTION;
390void Context::StartActiveTask()
396 ResumeThread(active_task->m_thread);
399void Context::CreateTimerThreadAndJoin()
407 m_handler->OnStart(m_stack_active);
412 m_timer_thread = CreateThread(
nullptr, 0, &TimerThread,
nullptr, 0,
nullptr);
414 SetThreadPriority(m_timer_thread, THREAD_PRIORITY_TIME_CRITICAL);
416 while (!m_task_threads.empty())
418 DWORD result = WaitForMultipleObjects((DWORD)m_task_threads.size(), m_task_threads.data(), FALSE, INFINITE);
423 Win32ScopedCriticalSection __cs(m_cs);
426 for (std::vector<HANDLE>::iterator itr = m_task_threads.begin(); itr != m_task_threads.end(); ++itr)
428 if (result == (WAIT_OBJECT_0 + i))
430 TaskContext *exiting_task =
nullptr;
431 for (std::list<TaskContext *>::iterator titr = m_tasks.begin(); titr != m_tasks.end(); ++titr)
433 if ((*titr)->m_thread == (*itr))
435 exiting_task = (*titr);
441 if (exiting_task !=
nullptr)
442 m_handler->OnTaskExit(exiting_task->m_stack);
444 m_task_threads.erase(itr);
454 if (m_timer_thread !=
nullptr)
455 WaitForSingleObject(m_timer_thread, INFINITE);
458void Context::Cleanup()
461 for (std::list<TaskContext *>::iterator itr = m_tasks.begin(); itr != m_tasks.end(); ++itr)
463 if ((*itr)->m_thread !=
nullptr)
465 CloseHandle((*itr)->m_thread);
466 (*itr)->m_thread =
nullptr;
472 if (m_timer_thread !=
nullptr)
474 CloseHandle(m_timer_thread);
475 m_timer_thread =
nullptr;
479 m_stop_signal =
false;
485void Context::ProcessTick()
487 Win32ScopedCriticalSection __cs(m_cs);
493 if (m_handler->OnTick(m_stack_idle, m_stack_active
503 m_sleep_ticks = ticks;
507void Context::SwitchContext()
510 if ((m_stack_idle != m_sleep_trap) && (m_stack_idle != m_exit_trap))
515 SuspendThread(idle_task->m_thread);
519 if (m_stack_active == m_sleep_trap)
521 if ((m_overrider ==
nullptr) || !m_overrider->OnSleep())
527 if (m_stack_active ==
GetContext().m_exit_trap)
536 ResumeThread(active_task->m_thread);
540Word Context::GetCallerSP()
const
543 DWORD calling_tid = GetCurrentThreadId();
545 Win32ScopedCriticalSection __cs(
const_cast<STK_X86_WIN32_CRITICAL_SECTION &
>(m_cs));
547 for (std::list<TaskContext *>::const_iterator itr = m_tasks.begin(), end = m_tasks.end(); itr != end; ++itr)
549 if ((*itr)->m_thread_id == calling_tid)
551 caller_sp =
hw::PtrToWord(STK_X86_WIN32_GET_SP((*itr)->m_task->GetStack()));
562TId Context::GetTid()
const
564 return m_handler->OnGetTid(GetCallerSP());
567void Context::SwitchToNext()
569 m_handler->OnTaskSwitch(GetCallerSP());
572void Context::Sleep(
Timeout ticks)
574 m_handler->OnTaskSleep(GetCallerSP(), ticks);
577void Context::SleepUntil(
Ticks timestamp)
579 m_handler->OnTaskSleepUntil(GetCallerSP(), timestamp);
584 return m_handler->OnTaskWait(GetCallerSP(), sync_obj, mutex, timeout);
589 m_stop_signal =
true;
595 InitStackMemory(stack_memory);
597 TaskContext *ctx =
reinterpret_cast<TaskContext *
>(STK_X86_WIN32_GET_SP(stack_memory->
GetStack()));
602 ctx->Initialize(user_task, stack);
604 m_tasks.push_back(ctx);
605 m_task_threads.push_back(ctx->m_thread);
625 GetContext().Initialize(event_handler, service, exit_trap, resolution_us);
653 return GetContext().InitStack(stack_type, stack, stack_memory, user_task);
663 return HiResClockQPC::GetInstance()->GetCycles();
668 return HiResClockQPC::GetInstance()->GetFrequency();
688 return GetContext().Wait(sync_obj, mutex, timeout);
747 HW_SpinLockLock(m_lock);
752 HW_SpinLockUnlock(m_lock);
757 return HW_SpinLockTryLock(m_lock);
767 return HiResClockQPC::GetInstance()->GetCycles();
772 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_TICKLESS_IDLE
Enables tickless (dynamic-tick) low-power operation during idle periods.
#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).
Namespace of STK package.
uintptr_t Word
Native processor word type.
void Sleep(Timeout ticks)
Put calling process into a sleep state.
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
@ STACK_USER_TASK
Stack of the user task.
@ STACK_EXIT_TRAP
Stack of the Exit trap.
void SetTls(Word tp)
Set thread-local storage (TLS).
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).
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.
uint32_t GetSysTimerFrequency() const
Get system timer frequency.
Cycles GetSysTimerCount() const
Get system timer count value.
Timeout Suspend()
Suspend scheduling.
void ProcessHardFault()
Cause a hard fault of the system.
void ProcessTick()
Process one tick.
void Resume(Timeout elapsed_ticks)
Resume scheduling after a prior Suspend() call.
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 Enter()
Enter a critical section.
static void Exit()
Exit 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.