1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:57:35 +00:00

Kernel: Make TimeManagement use AK::Time internally

I don't dare touch the multi-threading logic and locking mechanism, so it stays
timespec for now. However, this could and should be changed to AK::Time, and I
bet it will simplify the "increment_time_since_boot()" code.
This commit is contained in:
Ben Wiederhake 2021-02-27 23:56:16 +01:00 committed by Andreas Kling
parent 91c72faa3c
commit c040e64b7d
8 changed files with 39 additions and 38 deletions

View file

@ -64,7 +64,7 @@ bool TimeManagement::is_valid_clock_id(clockid_t clock_id)
};
}
KResultOr<timespec> TimeManagement::current_time(clockid_t clock_id) const
KResultOr<Time> TimeManagement::current_time(clockid_t clock_id) const
{
switch (clock_id) {
case CLOCK_MONOTONIC:
@ -87,14 +87,15 @@ bool TimeManagement::is_system_timer(const HardwareTimerBase& timer) const
return &timer == m_system_timer.ptr();
}
void TimeManagement::set_epoch_time(timespec ts)
void TimeManagement::set_epoch_time(Time ts)
{
InterruptDisabler disabler;
m_epoch_time = ts;
// FIXME: Should use AK::Time internally
m_epoch_time = ts.to_timespec();
m_remaining_epoch_time_adjustment = { 0, 0 };
}
timespec TimeManagement::monotonic_time(TimePrecision precision) const
Time TimeManagement::monotonic_time(TimePrecision precision) const
{
// This is the time when last updated by an interrupt.
u64 seconds;
@ -122,10 +123,10 @@ timespec TimeManagement::monotonic_time(TimePrecision precision) const
VERIFY(ticks < m_time_ticks_per_second);
u64 ns = ((u64)ticks * 1000000000ull) / m_time_ticks_per_second;
VERIFY(ns < 1000000000ull);
return { (long)seconds, (long)ns };
return Time::from_timespec({ (i64)seconds, (i32)ns });
}
timespec TimeManagement::epoch_time(TimePrecision) const
Time TimeManagement::epoch_time(TimePrecision) const
{
// TODO: Take into account precision
timespec ts;
@ -134,12 +135,14 @@ timespec TimeManagement::epoch_time(TimePrecision) const
update_iteration = m_update1.load(AK::MemoryOrder::memory_order_acquire);
ts = m_epoch_time;
} while (update_iteration != m_update2.load(AK::MemoryOrder::memory_order_acquire));
return ts;
return Time::from_timespec(ts);
}
u64 TimeManagement::uptime_ms() const
{
auto mtime = monotonic_time();
auto mtime = monotonic_time().to_timespec();
// This overflows after 292 million years of uptime.
// Since this is only used for performance timestamps and sys$times, that's probably enough.
u64 ms = mtime.tv_sec * 1000ull;
ms += mtime.tv_nsec / 1000000;
return ms;
@ -211,12 +214,9 @@ UNMAP_AFTER_INIT TimeManagement::TimeManagement()
}
}
timeval TimeManagement::now_as_timeval()
Time TimeManagement::now()
{
timespec ts = s_the.ptr()->epoch_time();
timeval tv;
timespec_to_timeval(ts, tv);
return tv;
return s_the.ptr()->epoch_time();
}
Vector<HardwareTimerBase*> TimeManagement::scan_and_initialize_periodic_timers()

View file

@ -28,6 +28,7 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefPtr.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <Kernel/KResult.h>
#include <Kernel/UnixTypes.h>
@ -53,15 +54,15 @@ public:
static TimeManagement& the();
static bool is_valid_clock_id(clockid_t);
KResultOr<timespec> current_time(clockid_t) const;
timespec monotonic_time(TimePrecision = TimePrecision::Coarse) const;
timespec monotonic_time_raw() const
KResultOr<Time> current_time(clockid_t) const;
Time monotonic_time(TimePrecision = TimePrecision::Coarse) const;
Time monotonic_time_raw() const
{
// TODO: implement
return monotonic_time(TimePrecision::Precise);
}
timespec epoch_time(TimePrecision = TimePrecision::Precise) const;
void set_epoch_time(timespec);
Time epoch_time(TimePrecision = TimePrecision::Precise) const;
void set_epoch_time(Time);
time_t ticks_per_second() const;
time_t boot_time() const;
@ -75,9 +76,13 @@ public:
static bool is_hpet_periodic_mode_allowed();
u64 uptime_ms() const;
static timeval now_as_timeval();
static Time now();
// FIXME: Should use AK::Time internally
// FIXME: Also, most likely broken, because it does not check m_update[12] for in-progress updates.
timespec remaining_epoch_time_adjustment() const { return m_remaining_epoch_time_adjustment; }
// FIXME: Should use AK::Time internally
// FIXME: Also, most likely broken, because it does not check m_update[12] for in-progress updates.
void set_remaining_epoch_time_adjustment(const timespec& adjustment) { m_remaining_epoch_time_adjustment = adjustment; }
bool can_query_precise_time() const { return m_can_query_precise_time; }
@ -95,6 +100,7 @@ private:
Atomic<u32> m_update1 { 0 };
u32 m_ticks_this_second { 0 };
u64 m_seconds_since_boot { 0 };
// FIXME: Should use AK::Time internally
timespec m_epoch_time { 0, 0 };
timespec m_remaining_epoch_time_adjustment { 0, 0 };
Atomic<u32> m_update2 { 0 };