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

LibCore: Move EventLoop to AK::Time

This commit is contained in:
Brian Gianforcaro 2021-08-14 16:50:16 -07:00 committed by Andreas Kling
parent a2a5cb0f24
commit da51b8f39d
2 changed files with 22 additions and 35 deletions

View file

@ -40,14 +40,14 @@ class InspectorServerConnection;
struct EventLoopTimer { struct EventLoopTimer {
int timer_id { 0 }; int timer_id { 0 };
int interval { 0 }; Time interval;
timeval fire_time { 0, 0 }; Time fire_time;
bool should_reload { false }; bool should_reload { false };
TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No }; TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
WeakPtr<Object> owner; WeakPtr<Object> owner;
void reload(const timeval& now); void reload(const Time& now);
bool has_expired(const timeval& now) const; bool has_expired(const Time& now) const;
}; };
struct EventLoop::Private { struct EventLoop::Private {
@ -599,21 +599,17 @@ retry:
queued_events_is_empty = m_queued_events.is_empty(); queued_events_is_empty = m_queued_events.is_empty();
} }
timeval now; Time now;
struct timeval timeout = { 0, 0 }; struct timeval timeout = { 0, 0 };
bool should_wait_forever = false; bool should_wait_forever = false;
if (mode == WaitMode::WaitForEvents && queued_events_is_empty) { if (mode == WaitMode::WaitForEvents && queued_events_is_empty) {
auto next_timer_expiration = get_next_timer_expiration(); auto next_timer_expiration = get_next_timer_expiration();
if (next_timer_expiration.has_value()) { if (next_timer_expiration.has_value()) {
timespec now_spec; now = Time::now_monotonic_coarse();
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec); auto computed_timeout = next_timer_expiration.value() - now;
now.tv_sec = now_spec.tv_sec; if (computed_timeout.is_negative())
now.tv_usec = now_spec.tv_nsec / 1000; computed_timeout = Time::zero();
timeval_sub(next_timer_expiration.value(), now, timeout); timeout = computed_timeout.to_timeval();
if (timeout.tv_sec < 0 || (timeout.tv_sec == 0 && timeout.tv_usec < 0)) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
}
} else { } else {
should_wait_forever = true; should_wait_forever = true;
} }
@ -653,10 +649,7 @@ try_select_again:
} }
if (!s_timers->is_empty()) { if (!s_timers->is_empty()) {
timespec now_spec; now = Time::now_monotonic_coarse();
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec);
now.tv_sec = now_spec.tv_sec;
now.tv_usec = now_spec.tv_nsec / 1000;
} }
for (auto& it : *s_timers) { for (auto& it : *s_timers) {
@ -696,21 +689,19 @@ try_select_again:
} }
} }
bool EventLoopTimer::has_expired(const timeval& now) const bool EventLoopTimer::has_expired(const Time& now) const
{ {
return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec); return now > fire_time;
} }
void EventLoopTimer::reload(const timeval& now) void EventLoopTimer::reload(const Time& now)
{ {
fire_time = now; fire_time = now + interval;
fire_time.tv_sec += interval / 1000;
fire_time.tv_usec += (interval % 1000) * 1000;
} }
Optional<struct timeval> EventLoop::get_next_timer_expiration() Optional<Time> EventLoop::get_next_timer_expiration()
{ {
Optional<struct timeval> soonest {}; Optional<Time> soonest {};
for (auto& it : *s_timers) { for (auto& it : *s_timers) {
auto& fire_time = it.value->fire_time; auto& fire_time = it.value->fire_time;
auto owner = it.value->owner.strong_ref(); auto owner = it.value->owner.strong_ref();
@ -718,7 +709,7 @@ Optional<struct timeval> EventLoop::get_next_timer_expiration()
&& owner && !owner->is_visible_for_timer_purposes()) { && owner && !owner->is_visible_for_timer_purposes()) {
continue; continue;
} }
if (!soonest.has_value() || fire_time.tv_sec < soonest.value().tv_sec || (fire_time.tv_sec == soonest.value().tv_sec && fire_time.tv_usec < soonest.value().tv_usec)) if (!soonest.has_value() || fire_time < soonest.value())
soonest = fire_time; soonest = fire_time;
} }
return soonest; return soonest;
@ -729,13 +720,8 @@ int EventLoop::register_timer(Object& object, int milliseconds, bool should_relo
VERIFY(milliseconds >= 0); VERIFY(milliseconds >= 0);
auto timer = make<EventLoopTimer>(); auto timer = make<EventLoopTimer>();
timer->owner = object; timer->owner = object;
timer->interval = milliseconds; timer->interval = Time::from_milliseconds(milliseconds);
timeval now; timer->reload(Time::now_monotonic_coarse());
timespec now_spec;
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec);
now.tv_sec = now_spec.tv_sec;
now.tv_usec = now_spec.tv_nsec / 1000;
timer->reload(now);
timer->should_reload = should_reload; timer->should_reload = should_reload;
timer->fire_when_not_visible = fire_when_not_visible; timer->fire_when_not_visible = fire_when_not_visible;
int timer_id = s_id_allocator->allocate(); int timer_id = s_id_allocator->allocate();

View file

@ -11,6 +11,7 @@
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/Noncopyable.h> #include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h> #include <AK/NonnullOwnPtr.h>
#include <AK/Time.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibCore/Forward.h> #include <LibCore/Forward.h>
@ -75,7 +76,7 @@ public:
private: private:
void wait_for_event(WaitMode); void wait_for_event(WaitMode);
Optional<struct timeval> get_next_timer_expiration(); Optional<Time> get_next_timer_expiration();
static void dispatch_signal(int); static void dispatch_signal(int);
static void handle_signal(int); static void handle_signal(int);