From 682de62c57c4fdce09349d9462ca0db75daead52 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 28 Feb 2024 23:06:48 +0200 Subject: [PATCH] Kernel: Replace HPET CriticalScope with SpinLock --- kernel/include/kernel/Timer/HPET.h | 3 +++ kernel/kernel/Timer/HPET.cpp | 26 ++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/kernel/include/kernel/Timer/HPET.h b/kernel/include/kernel/Timer/HPET.h index f462b9dce8..bd4cdbf5c5 100644 --- a/kernel/include/kernel/Timer/HPET.h +++ b/kernel/include/kernel/Timer/HPET.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace Kernel @@ -30,6 +31,8 @@ namespace Kernel uint64_t read_main_counter() const; private: + mutable SpinLock m_lock; + bool m_is_64bit { false }; uint64_t m_last_ticks { 0 }; diff --git a/kernel/kernel/Timer/HPET.cpp b/kernel/kernel/Timer/HPET.cpp index f1c238142d..c4fb0771c2 100644 --- a/kernel/kernel/Timer/HPET.cpp +++ b/kernel/kernel/Timer/HPET.cpp @@ -244,7 +244,7 @@ namespace Kernel if (m_is_64bit) return regs.main_counter.full; - CriticalScope _; + SpinLockGuard _(m_lock); uint32_t current_low = regs.main_counter.low; uint32_t wraps = m_32bit_wraps; if (current_low < (uint32_t)m_last_ticks) @@ -256,19 +256,21 @@ namespace Kernel { auto& regs = registers(); - uint64_t current_ticks; - - if (m_is_64bit) - current_ticks = regs.main_counter.full; - else { - uint32_t current_low = regs.main_counter.low; - if (current_low < (uint32_t)m_last_ticks) - m_32bit_wraps++; - current_ticks = ((uint64_t)m_32bit_wraps << 32) | current_low; - } + SpinLockGuard _(m_lock); - m_last_ticks = current_ticks; + uint64_t current_ticks; + if (m_is_64bit) + current_ticks = regs.main_counter.full; + else + { + uint32_t current_low = regs.main_counter.low; + if (current_low < (uint32_t)m_last_ticks) + m_32bit_wraps++; + current_ticks = ((uint64_t)m_32bit_wraps << 32) | current_low; + } + m_last_ticks = current_ticks; + } Scheduler::get().timer_reschedule(); }