From 44d58b85ef16c7702f138144b2da465e5a2eef89 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 16 Apr 2020 18:04:31 +0200 Subject: [PATCH] Kernel: Simplify the way we pass HardwareTimers around a bit Instead of passing around indices into the m_hardware_timers vector, just pass around a HardwareTimer* instead. --- Kernel/Time/TimeManagement.cpp | 89 ++++++++++++++-------------------- Kernel/Time/TimeManagement.h | 13 ++--- 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/Kernel/Time/TimeManagement.cpp b/Kernel/Time/TimeManagement.cpp index 3ac0e064949..8e6b8042d9a 100644 --- a/Kernel/Time/TimeManagement.cpp +++ b/Kernel/Time/TimeManagement.cpp @@ -41,9 +41,10 @@ namespace Kernel { static TimeManagement* s_time_management; -bool TimeManagement::initialized() +TimeManagement& TimeManagement::the() { - return s_time_management != nullptr; + ASSERT(s_time_management); + return *s_time_management; } bool TimeManagement::is_system_timer(const HardwareTimer& timer) const @@ -64,7 +65,7 @@ time_t TimeManagement::epoch_time() const void TimeManagement::initialize() { - ASSERT(!TimeManagement::initialized()); + ASSERT(!s_time_management); if (kernel_command_line().lookup("time").value_or("modern") == "legacy") s_time_management = new TimeManagement(false); else @@ -118,35 +119,30 @@ TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers) ASSERT_NOT_REACHED(); } -Vector TimeManagement::scan_and_initialize_periodic_timers() +Vector TimeManagement::scan_and_initialize_periodic_timers() { - bool enable_periodic_mode = is_hpet_periodic_mode_allowed(); - dbg() << "Scanning for Periodic timers"; - Vector periodic_timers_indexes; - periodic_timers_indexes.ensure_capacity(m_hardware_timers.size()); - for (size_t index = 0; index < m_hardware_timers.size(); index++) { - if (!m_hardware_timers[index].is_null()) { - if (m_hardware_timers[index]->is_periodic_capable()) { - periodic_timers_indexes.append(index); - if (enable_periodic_mode) - m_hardware_timers[index]->set_periodic(); - } + bool should_enable = is_hpet_periodic_mode_allowed(); + dbg() << "Time: Scanning for periodic timers"; + Vector timers; + for (auto& hardware_timer : m_hardware_timers) { + if (hardware_timer && hardware_timer->is_periodic_capable()) { + timers.append(hardware_timer); + if (should_enable) + hardware_timer->set_periodic(); } } - return periodic_timers_indexes; + return timers; } -Vector TimeManagement::scan_for_non_periodic_timers() +Vector TimeManagement::scan_for_non_periodic_timers() { - dbg() << "Scanning for Non-Periodic timers"; - Vector non_periodic_timers_indexes; - non_periodic_timers_indexes.ensure_capacity(m_hardware_timers.size()); - for (size_t index = 0; index < m_hardware_timers.size(); index++) { - if (!m_hardware_timers[index].is_null()) - if (!m_hardware_timers[index]->is_periodic_capable()) - non_periodic_timers_indexes.append(index); + dbg() << "Time: Scanning for non-periodic timers"; + Vector timers; + for (auto& hardware_timer : m_hardware_timers) { + if (hardware_timer && !hardware_timer->is_periodic_capable()) + timers.append(hardware_timer); } - return non_periodic_timers_indexes; + return timers; } bool TimeManagement::is_hpet_periodic_mode_allowed() @@ -174,32 +170,27 @@ bool TimeManagement::probe_and_set_non_legacy_hardware_timers() } dbg() << "HPET: Setting appropriate functions to timers."; - m_hardware_timers.resize(HPET::the().comparators().size()); - for (size_t index = 0; index < m_hardware_timers.size(); index++) { - m_hardware_timers[index] = HPET::the().comparators()[index]; -#ifdef TIME_DEBUG - dbg() << m_hardware_timers[index].ptr() << " <- " << HPET::the().comparators()[index].ptr(); -#endif - } + for (auto& hpet_comparator : HPET::the().comparators()) + m_hardware_timers.append(hpet_comparator); - auto periodic_timer_indexes = scan_and_initialize_periodic_timers(); - auto non_periodic_timer_indexes = scan_for_non_periodic_timers(); + auto periodic_timers = scan_and_initialize_periodic_timers(); + auto non_periodic_timers = scan_for_non_periodic_timers(); if (is_hpet_periodic_mode_allowed()) - ASSERT(!periodic_timer_indexes.is_empty()); + ASSERT(!periodic_timers.is_empty()); - ASSERT(periodic_timer_indexes.size() + non_periodic_timer_indexes.size() >= 2); + ASSERT(periodic_timers.size() + non_periodic_timers.size() >= 2); - if (periodic_timer_indexes.size() >= 2) { - m_time_keeper_timer = m_hardware_timers[periodic_timer_indexes[1]]; - m_system_timer = m_hardware_timers[periodic_timer_indexes[0]]; + if (periodic_timers.size() >= 2) { + m_time_keeper_timer = periodic_timers[1]; + m_system_timer = periodic_timers[0]; } else { - if (periodic_timer_indexes.size() == 1) { - m_time_keeper_timer = m_hardware_timers[periodic_timer_indexes[0]]; - m_system_timer = m_hardware_timers[non_periodic_timer_indexes[0]]; + if (periodic_timers.size() == 1) { + m_time_keeper_timer = periodic_timers[0]; + m_system_timer = non_periodic_timers[0]; } else { - m_time_keeper_timer = m_hardware_timers[non_periodic_timer_indexes[1]]; - m_system_timer = m_hardware_timers[non_periodic_timer_indexes[0]]; + m_time_keeper_timer = non_periodic_timers[1]; + m_system_timer = non_periodic_timers[0]; } } @@ -223,19 +214,13 @@ bool TimeManagement::probe_and_set_legacy_hardware_timers() } } - m_hardware_timers[0] = PIT::initialize([](const RegisterState& regs) { update_time(regs); }); - m_hardware_timers[1] = RealTimeClock::create([](const RegisterState& regs) { update_scheduler_ticks(regs); }); + m_hardware_timers.append(PIT::initialize([](const RegisterState& regs) { update_time(regs); })); + m_hardware_timers.append(RealTimeClock::create([](const RegisterState& regs) { update_scheduler_ticks(regs); })); m_time_keeper_timer = m_hardware_timers[0]; m_system_timer = m_hardware_timers[1]; return true; } -TimeManagement& TimeManagement::the() -{ - ASSERT(TimeManagement::initialized()); - return *s_time_management; -} - void TimeManagement::update_time(const RegisterState& regs) { TimeManagement::the().increment_time_since_boot(regs); diff --git a/Kernel/Time/TimeManagement.h b/Kernel/Time/TimeManagement.h index 00fc027738a..9daf046fa81 100644 --- a/Kernel/Time/TimeManagement.h +++ b/Kernel/Time/TimeManagement.h @@ -27,10 +27,8 @@ #pragma once #include -#include -#include +#include #include -#include #include namespace Kernel { @@ -38,7 +36,10 @@ namespace Kernel { #define OPTIMAL_TICKS_PER_SECOND_RATE 1000 class HardwareTimer; + class TimeManagement { + AK_MAKE_ETERNAL; + public: static bool initialized(); static void initialize(); @@ -66,9 +67,9 @@ private: explicit TimeManagement(bool probe_non_legacy_hardware_timers); bool probe_and_set_legacy_hardware_timers(); bool probe_and_set_non_legacy_hardware_timers(); - Vector scan_and_initialize_periodic_timers(); - Vector scan_for_non_periodic_timers(); - FixedArray> m_hardware_timers { 2 }; + Vector scan_and_initialize_periodic_timers(); + Vector scan_for_non_periodic_timers(); + Vector> m_hardware_timers; u32 m_ticks_this_second { 0 }; u32 m_seconds_since_boot { 0 };