Kernel: Make PIT counter atomic with spinlock

This commit is contained in:
Bananymous 2024-07-21 18:56:41 +03:00
parent e00efca170
commit 8fe798de6d
2 changed files with 17 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <kernel/Interruptable.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/Timer/Timer.h>
namespace Kernel
@ -19,9 +20,11 @@ namespace Kernel
private:
void initialize();
uint64_t read_counter() const;
private:
volatile uint64_t m_system_time { 0 };
mutable SpinLock m_lock;
uint64_t m_system_time { 0 };
};
}

View File

@ -54,23 +54,32 @@ namespace Kernel
void PIT::handle_irq()
{
m_system_time = m_system_time + 1;
{
SpinLockGuard _(m_lock);
m_system_time++;
}
Kernel::Scheduler::get().timer_reschedule();
}
uint64_t PIT::read_counter() const
{
SpinLockGuard _(m_lock);
return m_system_time;
}
uint64_t PIT::ms_since_boot() const
{
return m_system_time * (MS_PER_S / TICKS_PER_SECOND);
return read_counter() * (MS_PER_S / TICKS_PER_SECOND);
}
uint64_t PIT::ns_since_boot() const
{
return m_system_time * (NS_PER_S / TICKS_PER_SECOND);
return read_counter() * (NS_PER_S / TICKS_PER_SECOND);
}
timespec PIT::time_since_boot() const
{
uint64_t ticks = m_system_time;
uint64_t ticks = read_counter();
return timespec {
.tv_sec = ticks / TICKS_PER_SECOND,
.tv_nsec = (long)((ticks % TICKS_PER_SECOND) * (NS_PER_S / TICKS_PER_SECOND))