1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:47:34 +00:00

Everywhere: Use MonotonicTime instead of Duration

This is easily identifiable by anyone who uses Duration::now_monotonic,
and any downstream users of that data.
This commit is contained in:
kleines Filmröllchen 2023-03-17 19:50:39 +01:00 committed by Jelle Raaijmakers
parent b2e7b8cdff
commit fc5cab5c21
29 changed files with 79 additions and 80 deletions

View file

@ -20,13 +20,12 @@ ElapsedTimer ElapsedTimer::start_new()
void ElapsedTimer::start()
{
m_valid = true;
m_origin_time = m_precise ? Duration::now_monotonic() : Duration::now_monotonic_coarse();
m_origin_time = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
}
void ElapsedTimer::reset()
{
m_valid = false;
m_origin_time = {};
}
i64 ElapsedTimer::elapsed_milliseconds() const
@ -37,7 +36,7 @@ i64 ElapsedTimer::elapsed_milliseconds() const
Duration ElapsedTimer::elapsed_time() const
{
VERIFY(is_valid());
auto now = m_precise ? Duration::now_monotonic() : Duration::now_monotonic_coarse();
auto now = m_precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
return now - m_origin_time;
}

View file

@ -32,10 +32,10 @@ public:
return elapsed_milliseconds();
}
Duration const& origin_time() const { return m_origin_time; }
MonotonicTime const& origin_time() const { return m_origin_time; }
private:
Duration m_origin_time {};
MonotonicTime m_origin_time { MonotonicTime::now() };
bool m_precise { false };
bool m_valid { false };
};

View file

@ -30,13 +30,13 @@ thread_local ThreadData* s_thread_data;
struct EventLoopTimer {
int timer_id { 0 };
Duration interval;
Duration fire_time;
MonotonicTime fire_time { MonotonicTime::now_coarse() };
bool should_reload { false };
TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
WeakPtr<Object> owner;
void reload(Duration const& now) { fire_time = now + interval; }
bool has_expired(Duration const& now) const { return now > fire_time; }
void reload(MonotonicTime const& now) { fire_time = now + interval; }
bool has_expired(MonotonicTime const& now) const { return now > fire_time; }
};
struct ThreadData {
@ -171,13 +171,13 @@ retry:
// Figure out how long to wait at maximum.
// This mainly depends on the PumpMode and whether we have pending events, but also the next expiring timer.
Duration now;
MonotonicTime now = MonotonicTime::now_coarse();
struct timeval timeout = { 0, 0 };
bool should_wait_forever = false;
if (mode == EventLoopImplementation::PumpMode::WaitForEvents && !has_pending_events) {
auto next_timer_expiration = get_next_timer_expiration();
if (next_timer_expiration.has_value()) {
now = Duration::now_monotonic_coarse();
now = MonotonicTime::now();
auto computed_timeout = next_timer_expiration.value() - now;
if (computed_timeout.is_negative())
computed_timeout = Duration::zero();
@ -231,7 +231,7 @@ try_select_again:
}
if (!thread_data.timers.is_empty()) {
now = Duration::now_monotonic_coarse();
now = MonotonicTime::now_coarse();
}
// Handle expired timers.
@ -349,10 +349,10 @@ void EventLoopImplementationUnix::notify_forked_and_in_child()
thread_data.pid = getpid();
}
Optional<Duration> EventLoopManagerUnix::get_next_timer_expiration()
Optional<MonotonicTime> EventLoopManagerUnix::get_next_timer_expiration()
{
auto now = Duration::now_monotonic_coarse();
Optional<Duration> soonest {};
auto now = MonotonicTime::now_coarse();
Optional<MonotonicTime> soonest {};
for (auto& it : ThreadData::the().timers) {
auto& fire_time = it.value->fire_time;
auto owner = it.value->owner.strong_ref();
@ -490,7 +490,7 @@ int EventLoopManagerUnix::register_timer(Object& object, int milliseconds, bool
auto timer = make<EventLoopTimer>();
timer->owner = object;
timer->interval = Duration::from_milliseconds(milliseconds);
timer->reload(Duration::now_monotonic_coarse());
timer->reload(MonotonicTime::now_coarse());
timer->should_reload = should_reload;
timer->fire_when_not_visible = fire_when_not_visible;
int timer_id = thread_data.id_allocator.allocate();

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Time.h>
#include <LibCore/EventLoopImplementation.h>
namespace Core {
@ -28,7 +29,7 @@ public:
virtual void unregister_signal(int handler_id) override;
void wait_for_events(EventLoopImplementation::PumpMode);
static Optional<Duration> get_next_timer_expiration();
static Optional<MonotonicTime> get_next_timer_expiration();
private:
void dispatch_signal(int signal_number);