From c732297623ed21ec99c9e754a490900fe0cdd5d8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 4 Aug 2023 15:50:11 +0300 Subject: [PATCH] Kernel: Rename TimerHandler to SystemTimer I changed SystemTimer to only handle the "best" supported timer it can initialize. --- kernel/include/kernel/Timer/Timer.h | 8 ++++---- kernel/kernel/Debug.cpp | 2 +- kernel/kernel/FS/DevFS/FileSystem.cpp | 2 +- kernel/kernel/FS/Ext2.cpp | 2 +- kernel/kernel/FS/Pipe.cpp | 6 +++--- kernel/kernel/FS/RamFS/Inode.cpp | 2 +- kernel/kernel/Input/PS2Controller.cpp | 8 ++++---- kernel/kernel/Input/PS2Keyboard.cpp | 2 +- kernel/kernel/Process.cpp | 4 ++-- kernel/kernel/Scheduler.cpp | 2 +- kernel/kernel/Storage/ATABus.cpp | 8 ++++---- kernel/kernel/Timer/Timer.cpp | 20 ++++++++++---------- kernel/kernel/kernel.cpp | 2 +- 13 files changed, 34 insertions(+), 34 deletions(-) diff --git a/kernel/include/kernel/Timer/Timer.h b/kernel/include/kernel/Timer/Timer.h index 5f1ba382d8..9161373ca1 100644 --- a/kernel/include/kernel/Timer/Timer.h +++ b/kernel/include/kernel/Timer/Timer.h @@ -15,11 +15,11 @@ namespace Kernel virtual timespec time_since_boot() const = 0; }; - class TimerHandler : public Timer + class SystemTimer : public Timer { public: static void initialize(); - static TimerHandler& get(); + static SystemTimer& get(); static bool is_initialized(); virtual uint64_t ms_since_boot() const override; @@ -27,10 +27,10 @@ namespace Kernel void sleep(uint64_t) const; - uint64_t get_unix_timestamp(); + uint64_t get_unix_timestamp() const; private: - TimerHandler() = default; + SystemTimer() = default; void initialize_timers(); diff --git a/kernel/kernel/Debug.cpp b/kernel/kernel/Debug.cpp index 12295f96cc..e55830e647 100644 --- a/kernel/kernel/Debug.cpp +++ b/kernel/kernel/Debug.cpp @@ -58,7 +58,7 @@ namespace Debug void print_prefix(const char* file, int line) { - auto ms_since_boot = Kernel::TimerHandler::is_initialized() ? Kernel::TimerHandler::get().ms_since_boot() : 0; + auto ms_since_boot = Kernel::SystemTimer::is_initialized() ? Kernel::SystemTimer::get().ms_since_boot() : 0; BAN::Formatter::print(Debug::putchar, "[{5}.{3}] {}:{}: ", ms_since_boot / 1000, ms_since_boot % 1000, file, line); } diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index 8837fa7fed..95c5ce358a 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -46,7 +46,7 @@ namespace Kernel ); s_instance->m_device_lock.unlock(); - Kernel::TimerHandler::get().sleep(1); + Kernel::SystemTimer::get().sleep(1); } }, nullptr ); diff --git a/kernel/kernel/FS/Ext2.cpp b/kernel/kernel/FS/Ext2.cpp index 08f3114eac..f7a939e910 100644 --- a/kernel/kernel/FS/Ext2.cpp +++ b/kernel/kernel/FS/Ext2.cpp @@ -384,7 +384,7 @@ namespace Kernel if (error_or.error().get_error_code() != ENOENT) return error_or.error(); - uint64_t current_time = TimerHandler::get().get_unix_timestamp(); + uint64_t current_time = SystemTimer::get().get_unix_timestamp(); Ext2::Inode ext2_inode; ext2_inode.mode = mode; diff --git a/kernel/kernel/FS/Pipe.cpp b/kernel/kernel/FS/Pipe.cpp index 8f01beaf5e..67e229e81f 100644 --- a/kernel/kernel/FS/Pipe.cpp +++ b/kernel/kernel/FS/Pipe.cpp @@ -17,7 +17,7 @@ namespace Kernel : m_uid(credentials.euid()) , m_gid(credentials.egid()) { - uint64_t current_time = TimerHandler::get().get_unix_timestamp(); + uint64_t current_time = SystemTimer::get().get_unix_timestamp(); m_atime = { .tv_sec = current_time, .tv_nsec = 0 }; m_mtime = { .tv_sec = current_time, .tv_nsec = 0 }; m_ctime = { .tv_sec = current_time, .tv_nsec = 0 }; @@ -57,7 +57,7 @@ namespace Kernel memmove(m_buffer.data(), m_buffer.data() + to_copy, m_buffer.size() - to_copy); MUST(m_buffer.resize(m_buffer.size() - to_copy)); - uint64_t current_time = TimerHandler::get().get_unix_timestamp(); + uint64_t current_time = SystemTimer::get().get_unix_timestamp(); m_atime = { .tv_sec = current_time, .tv_nsec = 0 }; m_semaphore.unblock(); @@ -76,7 +76,7 @@ namespace Kernel TRY(m_buffer.resize(old_size + count)); memcpy(m_buffer.data() + old_size, buffer, count); - uint64_t current_time = TimerHandler::get().get_unix_timestamp(); + uint64_t current_time = SystemTimer::get().get_unix_timestamp(); m_mtime = { .tv_sec = current_time, .tv_nsec = 0 }; m_ctime = { .tv_sec = current_time, .tv_nsec = 0 }; diff --git a/kernel/kernel/FS/RamFS/Inode.cpp b/kernel/kernel/FS/RamFS/Inode.cpp index 81716ba480..894ae15e36 100644 --- a/kernel/kernel/FS/RamFS/Inode.cpp +++ b/kernel/kernel/FS/RamFS/Inode.cpp @@ -23,7 +23,7 @@ namespace Kernel RamInode::RamInode(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) : m_fs(fs) { - uint64_t current_unix_time = TimerHandler::get().get_unix_timestamp(); + uint64_t current_unix_time = SystemTimer::get().get_unix_timestamp(); timespec current_timespec; current_timespec.tv_sec = current_unix_time; current_timespec.tv_nsec = 0; diff --git a/kernel/kernel/Input/PS2Controller.cpp b/kernel/kernel/Input/PS2Controller.cpp index 1c25beb06d..344853119d 100644 --- a/kernel/kernel/Input/PS2Controller.cpp +++ b/kernel/kernel/Input/PS2Controller.cpp @@ -108,8 +108,8 @@ namespace Kernel::Input { if (device == 1) IO::outb(PS2::IOPort::COMMAND, PS2::Command::WRITE_TO_SECOND_PORT); - uint64_t timeout = TimerHandler::get().ms_since_boot() + s_device_timeout_ms; - while (TimerHandler::get().ms_since_boot() < timeout) + uint64_t timeout = SystemTimer::get().ms_since_boot() + s_device_timeout_ms; + while (SystemTimer::get().ms_since_boot() < timeout) { if (!(IO::inb(PS2::IOPort::STATUS) & PS2::Status::INPUT_FULL)) { @@ -122,8 +122,8 @@ namespace Kernel::Input static BAN::ErrorOr device_read_byte() { - uint64_t timeout = TimerHandler::get().ms_since_boot() + s_device_timeout_ms; - while (TimerHandler::get().ms_since_boot() < timeout) + uint64_t timeout = SystemTimer::get().ms_since_boot() + s_device_timeout_ms; + while (SystemTimer::get().ms_since_boot() < timeout) if (IO::inb(PS2::IOPort::STATUS) & PS2::Status::OUTPUT_FULL) return IO::inb(PS2::IOPort::DATA); return BAN::Error::from_error_code(ErrorCode::PS2_Timeout); diff --git a/kernel/kernel/Input/PS2Keyboard.cpp b/kernel/kernel/Input/PS2Keyboard.cpp index 716d47ffa0..1e1e208574 100644 --- a/kernel/kernel/Input/PS2Keyboard.cpp +++ b/kernel/kernel/Input/PS2Keyboard.cpp @@ -216,7 +216,7 @@ namespace Kernel::Input if (event.pressed() && event.key == Input::Key::F11) { - auto time = TimerHandler::get().time_since_boot(); + auto time = SystemTimer::get().time_since_boot(); dprintln("{}.{9} s", time.tv_sec, time.tv_nsec); } diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 0f809cea24..95714f6727 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -473,7 +473,7 @@ namespace Kernel BAN::ErrorOr Process::sys_sleep(int seconds) { - TimerHandler::get().sleep(seconds * 1000); + SystemTimer::get().sleep(seconds * 1000); return 0; } @@ -811,7 +811,7 @@ namespace Kernel { case CLOCK_MONOTONIC: { - uint64_t time_ms = TimerHandler::get().ms_since_boot(); + uint64_t time_ms = SystemTimer::get().ms_since_boot(); tp->tv_sec = time_ms / 1000; tp->tv_nsec = (time_ms % 1000) * 1000000; break; diff --git a/kernel/kernel/Scheduler.cpp b/kernel/kernel/Scheduler.cpp index 7985937db9..e9849ffc49 100644 --- a/kernel/kernel/Scheduler.cpp +++ b/kernel/kernel/Scheduler.cpp @@ -113,7 +113,7 @@ namespace Kernel { VERIFY_CLI(); - uint64_t current_time = TimerHandler::get().ms_since_boot(); + uint64_t current_time = SystemTimer::get().ms_since_boot(); while (!m_sleeping_threads.empty() && m_sleeping_threads.front().wake_time <= current_time) { Thread* thread = m_sleeping_threads.front().thread; diff --git a/kernel/kernel/Storage/ATABus.cpp b/kernel/kernel/Storage/ATABus.cpp index 094d2c0f48..dafd7ee9e4 100644 --- a/kernel/kernel/Storage/ATABus.cpp +++ b/kernel/kernel/Storage/ATABus.cpp @@ -100,7 +100,7 @@ namespace Kernel { uint8_t device_index = this->device_index(device); io_write(ATA_PORT_DRIVE_SELECT, 0xA0 | (device_index << 4)); - TimerHandler::get().sleep(1); + SystemTimer::get().sleep(1); } ATABus::DeviceType ATABus::identify(const ATADevice& device, uint16_t* buffer) @@ -111,7 +111,7 @@ namespace Kernel io_write(ATA_PORT_CONTROL, ATA_CONTROL_nIEN); io_write(ATA_PORT_COMMAND, ATA_COMMAND_IDENTIFY); - TimerHandler::get().sleep(1); + SystemTimer::get().sleep(1); // No device on port if (io_read(ATA_PORT_STATUS) == 0) @@ -135,7 +135,7 @@ namespace Kernel } io_write(ATA_PORT_COMMAND, ATA_COMMAND_IDENTIFY_PACKET); - TimerHandler::get().sleep(1); + SystemTimer::get().sleep(1); if (auto res = wait(true); res.is_error()) { @@ -304,7 +304,7 @@ namespace Kernel io_write(ATA_PORT_LBA2, (uint8_t)(lba >> 16)); io_write(ATA_PORT_COMMAND, ATA_COMMAND_WRITE_SECTORS); - TimerHandler::get().sleep(1); + SystemTimer::get().sleep(1); for (uint32_t sector = 0; sector < sector_count; sector++) { diff --git a/kernel/kernel/Timer/Timer.cpp b/kernel/kernel/Timer/Timer.cpp index e628801a75..65d6dbeee6 100644 --- a/kernel/kernel/Timer/Timer.cpp +++ b/kernel/kernel/Timer/Timer.cpp @@ -6,29 +6,29 @@ namespace Kernel { - static TimerHandler* s_instance = nullptr; + static SystemTimer* s_instance = nullptr; - void TimerHandler::initialize() + void SystemTimer::initialize() { ASSERT(s_instance == nullptr); - auto* temp = new TimerHandler; + auto* temp = new SystemTimer; ASSERT(temp); temp->initialize_timers(); s_instance = temp; } - TimerHandler& TimerHandler::get() + SystemTimer& SystemTimer::get() { ASSERT(s_instance); return *s_instance; } - bool TimerHandler::is_initialized() + bool SystemTimer::is_initialized() { return !!s_instance; } - void TimerHandler::initialize_timers() + void SystemTimer::initialize_timers() { m_rtc = MUST(BAN::UniqPtr::create()); m_boot_time = BAN::to_unix_time(m_rtc->get_current_time()); @@ -54,17 +54,17 @@ namespace Kernel Kernel::panic("Could not initialize any timer"); } - uint64_t TimerHandler::ms_since_boot() const + uint64_t SystemTimer::ms_since_boot() const { return m_timer->ms_since_boot(); } - timespec TimerHandler::time_since_boot() const + timespec SystemTimer::time_since_boot() const { return m_timer->time_since_boot(); } - void TimerHandler::sleep(uint64_t ms) const + void SystemTimer::sleep(uint64_t ms) const { if (ms == 0) return; @@ -74,7 +74,7 @@ namespace Kernel dwarnln("sleep woke {} ms too soon", wake_time - ms_since_boot()); } - uint64_t TimerHandler::get_unix_timestamp() + uint64_t SystemTimer::get_unix_timestamp() const { return m_boot_time + ms_since_boot() / 1000; } diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 020fe3e484..b3ffc5b6c0 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -144,7 +144,7 @@ extern "C" void kernel_main() InterruptController::initialize(cmdline.force_pic); dprintln("Interrupt controller initialized"); - TimerHandler::initialize(); + SystemTimer::initialize(); dprintln("Timers initialized"); DevFileSystem::initialize();