From d4adcff958903e168fb09f6dc98641316093ce5d Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 4 Aug 2023 15:15:00 +0300 Subject: [PATCH] Kernel: Move sleep() implementation to TimerHandler --- kernel/include/kernel/Timer/PIT.h | 1 - kernel/include/kernel/Timer/Timer.h | 3 +-- kernel/kernel/Timer/PIT.cpp | 10 ---------- kernel/kernel/Timer/Timer.cpp | 17 ++++++++++++----- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/kernel/include/kernel/Timer/PIT.h b/kernel/include/kernel/Timer/PIT.h index 09025ccf..36703636 100644 --- a/kernel/include/kernel/Timer/PIT.h +++ b/kernel/include/kernel/Timer/PIT.h @@ -14,7 +14,6 @@ namespace Kernel static BAN::ErrorOr> create(); virtual uint64_t ms_since_boot() const override; - virtual void sleep(uint64_t) const override; private: void initialize(); diff --git a/kernel/include/kernel/Timer/Timer.h b/kernel/include/kernel/Timer/Timer.h index 8117102e..56e5d519 100644 --- a/kernel/include/kernel/Timer/Timer.h +++ b/kernel/include/kernel/Timer/Timer.h @@ -12,7 +12,6 @@ namespace Kernel public: virtual ~Timer() {}; virtual uint64_t ms_since_boot() const = 0; - virtual void sleep(uint64_t) const = 0; }; class TimerHandler @@ -35,7 +34,7 @@ namespace Kernel private: uint64_t m_boot_time { 0 }; BAN::UniqPtr m_rtc; - BAN::Vector> m_timers; + BAN::UniqPtr m_timer; }; } \ No newline at end of file diff --git a/kernel/kernel/Timer/PIT.cpp b/kernel/kernel/Timer/PIT.cpp index 1450ae16..00389383 100644 --- a/kernel/kernel/Timer/PIT.cpp +++ b/kernel/kernel/Timer/PIT.cpp @@ -60,14 +60,4 @@ namespace Kernel return s_system_time; } - void PIT::sleep(uint64_t ms) const - { - if (ms == 0) - return; - uint64_t wake_time = s_system_time + ms; - Kernel::Scheduler::get().set_current_thread_sleeping(wake_time); - if (s_system_time < wake_time) - dwarnln("sleep woke {} ms too soon", wake_time - s_system_time); - } - } diff --git a/kernel/kernel/Timer/Timer.cpp b/kernel/kernel/Timer/Timer.cpp index c0a9cdc9..a9340051 100644 --- a/kernel/kernel/Timer/Timer.cpp +++ b/kernel/kernel/Timer/Timer.cpp @@ -1,5 +1,6 @@ -#include +#include #include +#include namespace Kernel { @@ -35,21 +36,27 @@ namespace Kernel dwarnln("PIT: {}", res.error()); else { - MUST(m_timers.emplace_back(BAN::move(res.release_value()))); + m_timer = res.release_value(); dprintln("PIT initialized"); + return; } - ASSERT(!m_timers.empty()); + Kernel::panic("Could not initialize any timer"); } uint64_t TimerHandler::ms_since_boot() const { - return m_timers.front()->ms_since_boot(); + return m_timer->ms_since_boot(); } void TimerHandler::sleep(uint64_t ms) const { - return m_timers.front()->sleep(ms); + if (ms == 0) + return; + uint64_t wake_time = ms_since_boot() + ms; + Scheduler::get().set_current_thread_sleeping(wake_time); + if (ms_since_boot() < wake_time) + dwarnln("sleep woke {} ms too soon", wake_time - ms_since_boot()); } uint64_t TimerHandler::get_unix_timestamp()