Skip to content

Truly independent web browser

LibCore: Take AK::Duration when registering event loop timers

Shannon Boothc814e6192307
diff --git a/Libraries/LibCore/EventLoop.cpp b/Libraries/LibCore/EventLoop.cpp
index a2e34171d18..fc31a58f6ea 100644
--- a/Libraries/LibCore/EventLoop.cpp
+++ b/Libraries/LibCore/EventLoop.cpp
@@ -9,6 +9,7 @@
#include <AK/Assertions.h>
#include <AK/Badge.h>
#include <AK/Platform.h>
+#include <AK/Time.h>
#include <LibCore/EventLoop.h>
#include <LibCore/EventLoopImplementation.h>
#include <LibCore/EventReceiver.h>
@@ -105,9 +106,9 @@ void EventLoop::unregister_signal(int handler_id)
EventLoopManager::the().unregister_signal(handler_id);
}
-intptr_t EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload)
+intptr_t EventLoop::register_timer(EventReceiver& object, AK::Duration interval, bool should_reload)
{
- return EventLoopManager::the().register_timer(object, milliseconds, should_reload);
+ return EventLoopManager::the().register_timer(object, interval, should_reload);
}
void EventLoop::unregister_timer(intptr_t timer_id)
diff --git a/Libraries/LibCore/EventLoop.h b/Libraries/LibCore/EventLoop.h
index 6c145da5fbf..52a9b2beed0 100644
--- a/Libraries/LibCore/EventLoop.h
+++ b/Libraries/LibCore/EventLoop.h
@@ -78,7 +78,7 @@ public:
bool was_exit_requested();
// The registration functions act upon the current loop of the current thread.
- static intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload);
+ static intptr_t register_timer(EventReceiver&, AK::Duration interval, bool should_reload);
static void unregister_timer(intptr_t timer_id);
static void register_notifier(Badge<Notifier>, Notifier&);
diff --git a/Libraries/LibCore/EventLoopImplementation.h b/Libraries/LibCore/EventLoopImplementation.h
index e516bc34c5b..e00976ed55d 100644
--- a/Libraries/LibCore/EventLoopImplementation.h
+++ b/Libraries/LibCore/EventLoopImplementation.h
@@ -26,7 +26,7 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() = 0;
- virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload) = 0;
+ virtual intptr_t register_timer(EventReceiver&, AK::Duration interval, bool should_reload) = 0;
virtual void unregister_timer(intptr_t timer_id) = 0;
virtual void register_notifier(Notifier&) = 0;
diff --git a/Libraries/LibCore/EventLoopImplementationUnix.cpp b/Libraries/LibCore/EventLoopImplementationUnix.cpp
index 9be46db200e..3182844bc41 100644
--- a/Libraries/LibCore/EventLoopImplementationUnix.cpp
+++ b/Libraries/LibCore/EventLoopImplementationUnix.cpp
@@ -555,15 +555,15 @@ void EventLoopManagerUnix::unregister_signal(int handler_id)
info.signal_handlers.remove(remove_signal_number);
}
-intptr_t EventLoopManagerUnix::register_timer(EventReceiver& object, int milliseconds, bool should_reload)
+intptr_t EventLoopManagerUnix::register_timer(EventReceiver& object, AK::Duration interval, bool should_reload)
{
- VERIFY(milliseconds >= 0);
+ VERIFY(!interval.is_negative());
auto& thread_data = ThreadData::the();
MutexLocker locker(thread_data.mutex);
auto timer = new EventLoopTimer;
timer->owner_thread = thread_data.thread_id;
timer->owner = object;
- timer->interval = AK::Duration::from_milliseconds(milliseconds);
+ timer->interval = interval;
// NB: The timer is armed and expired on the precise clock. CLOCK_MONOTONIC_COARSE lags it by up to a scheduler
// tick on Linux, so a timer armed against the coarse clock could fire that much before its deadline on the
// clock everything else measures with, performance.now() included.
diff --git a/Libraries/LibCore/EventLoopImplementationUnix.h b/Libraries/LibCore/EventLoopImplementationUnix.h
index 7c29f1090bb..9c787dc75b0 100644
--- a/Libraries/LibCore/EventLoopImplementationUnix.h
+++ b/Libraries/LibCore/EventLoopImplementationUnix.h
@@ -21,7 +21,7 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() override;
- virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload) override;
+ virtual intptr_t register_timer(EventReceiver&, AK::Duration interval, bool should_reload) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Notifier&) override;
diff --git a/Libraries/LibCore/EventLoopImplementationWindows.cpp b/Libraries/LibCore/EventLoopImplementationWindows.cpp
index e9cb1f1254d..edd08f8ca8c 100644
--- a/Libraries/LibCore/EventLoopImplementationWindows.cpp
+++ b/Libraries/LibCore/EventLoopImplementationWindows.cpp
@@ -742,15 +742,15 @@ void EventLoopManagerWindows::unregister_notifier(Notifier& notifier)
unregister_socket_notifier(*thread_data, notifier, **socket);
}
-intptr_t EventLoopManagerWindows::register_timer(EventReceiver& object, int milliseconds, bool should_reload)
+intptr_t EventLoopManagerWindows::register_timer(EventReceiver& object, AK::Duration interval, bool should_reload)
{
- VERIFY(milliseconds >= 0);
+ VERIFY(!interval.is_negative());
auto* thread_data = ThreadData::the();
VERIFY(thread_data);
auto timer = make<EventLoopTimer>();
timer->owner = object.make_weak_ptr();
- timer->interval = AK::Duration::from_milliseconds(milliseconds);
+ timer->interval = interval;
timer->should_reload = should_reload;
timer->reload(MonotonicTime::now());
thread_data->timeouts.schedule_absolute(timer.ptr());
diff --git a/Libraries/LibCore/EventLoopImplementationWindows.h b/Libraries/LibCore/EventLoopImplementationWindows.h
index db698f2afba..3386dfdf836 100644
--- a/Libraries/LibCore/EventLoopImplementationWindows.h
+++ b/Libraries/LibCore/EventLoopImplementationWindows.h
@@ -20,7 +20,7 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() override;
- virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload) override;
+ virtual intptr_t register_timer(EventReceiver&, AK::Duration interval, bool should_reload) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Notifier&) override;
diff --git a/Libraries/LibCore/EventReceiver.cpp b/Libraries/LibCore/EventReceiver.cpp
index 4cf6e74f947..f0170fedf37 100644
--- a/Libraries/LibCore/EventReceiver.cpp
+++ b/Libraries/LibCore/EventReceiver.cpp
@@ -6,6 +6,7 @@
*/
#include <AK/Assertions.h>
+#include <AK/Time.h>
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/EventReceiver.h>
@@ -38,14 +39,14 @@ void EventReceiver::timer_event(Core::TimerEvent&)
{
}
-void EventReceiver::start_timer(int ms)
+void EventReceiver::start_timer(AK::Duration interval)
{
if (m_timer_id) {
dbgln("{} {:p} already has a timer!", class_name(), this);
VERIFY_NOT_REACHED();
}
- m_timer_id = Core::EventLoop::register_timer(*this, ms, true);
+ m_timer_id = Core::EventLoop::register_timer(*this, interval, true);
}
void EventReceiver::stop_timer()
diff --git a/Libraries/LibCore/EventReceiver.h b/Libraries/LibCore/EventReceiver.h
index bd8efe10dd8..115096395f9 100644
--- a/Libraries/LibCore/EventReceiver.h
+++ b/Libraries/LibCore/EventReceiver.h
@@ -52,7 +52,7 @@ public:
template<typename T>
bool fast_is() const = delete;
- void start_timer(int ms);
+ void start_timer(AK::Duration interval);
void stop_timer();
bool has_timer() const { return m_timer_id; }
diff --git a/Libraries/LibCore/Timer.cpp b/Libraries/LibCore/Timer.cpp
index 87d91b0ff3b..107203650cb 100644
--- a/Libraries/LibCore/Timer.cpp
+++ b/Libraries/LibCore/Timer.cpp
@@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Time.h>
#include <LibCore/Timer.h>
namespace Core {
@@ -46,7 +47,7 @@ void Timer::start(int interval_ms)
if (m_active)
return;
m_interval_ms = interval_ms;
- start_timer(interval_ms);
+ start_timer(AK::Duration::from_milliseconds(interval_ms));
m_active = true;
}
diff --git a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp
index 1c7f6247968..56ba5a65a9e 100644
--- a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp
+++ b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp
@@ -6,6 +6,7 @@
#include "ALooperEventLoopImplementation.h"
#include "JNIHelpers.h"
+#include <AK/Time.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Notifier.h>
#include <LibCore/ThreadEventQueue.h>
@@ -90,14 +91,14 @@ NonnullOwnPtr<Core::EventLoopImplementation> ALooperEventLoopManager::make_imple
return ALooperEventLoopImplementation::create();
}
-intptr_t ALooperEventLoopManager::register_timer(Core::EventReceiver& receiver, int milliseconds, bool should_reload)
+intptr_t ALooperEventLoopManager::register_timer(Core::EventReceiver& receiver, AK::Duration interval, bool should_reload)
{
JavaEnvironment env(global_vm);
auto& thread_data = EventLoopThreadData::the();
auto timer = env.get()->NewObject(m_timer_class, m_timer_constructor, reinterpret_cast<long>(&current_impl()));
- long millis = milliseconds;
+ long millis = interval.to_milliseconds();
long timer_id = env.get()->CallLongMethod(m_timer_service, m_register_timer, timer, !should_reload, millis);
// FIXME: Is there a race condition here? Maybe we should take a lock on the timers...
diff --git a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h
index 9e3e7345ed6..fc868c38fde 100644
--- a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h
+++ b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h
@@ -23,7 +23,7 @@ public:
virtual ~ALooperEventLoopManager() override;
virtual NonnullOwnPtr<Core::EventLoopImplementation> make_implementation() override;
- virtual intptr_t register_timer(Core::EventReceiver&, int milliseconds, bool should_reload) override;
+ virtual intptr_t register_timer(Core::EventReceiver&, AK::Duration interval, bool should_reload) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Core::Notifier&) override;
diff --git a/UI/Qt/EventLoopImplementationQt.cpp b/UI/Qt/EventLoopImplementationQt.cpp
index c96d0c59c2d..fe23e644275 100644
--- a/UI/Qt/EventLoopImplementationQt.cpp
+++ b/UI/Qt/EventLoopImplementationQt.cpp
@@ -6,11 +6,13 @@
#include <AK/HashMap.h>
#include <AK/IDAllocator.h>
+#include <AK/Math.h>
#include <AK/Mutex.h>
#include <AK/MutexProtected.h>
#include <AK/RWLock.h>
#include <AK/Singleton.h>
#include <AK/TemporaryChange.h>
+#include <AK/Time.h>
#include <AK/kmalloc.h>
#include <LibCore/Event.h>
#include <LibCore/EventReceiver.h>
@@ -387,11 +389,11 @@ static void qt_timer_fired(Core::EventReceiver& object)
object.dispatch_event(event);
}
-intptr_t EventLoopManagerQt::register_timer(Core::EventReceiver& object, int milliseconds, bool should_reload)
+intptr_t EventLoopManagerQt::register_timer(Core::EventReceiver& object, AK::Duration interval, bool should_reload)
{
auto timer = new QTimer;
timer->setTimerType(Qt::PreciseTimer);
- timer->setInterval(milliseconds);
+ timer->setInterval(AK::clamp_to<int>(interval.to_milliseconds()));
timer->setSingleShot(!should_reload);
auto weak_object = object.make_weak_ptr();
QObject::connect(timer, &QTimer::timeout, [weak_object = move(weak_object)] {
diff --git a/UI/Qt/EventLoopImplementationQt.h b/UI/Qt/EventLoopImplementationQt.h
index f50254b2bc2..de456e90f04 100644
--- a/UI/Qt/EventLoopImplementationQt.h
+++ b/UI/Qt/EventLoopImplementationQt.h
@@ -29,7 +29,7 @@ public:
virtual ~EventLoopManagerQt() override;
virtual NonnullOwnPtr<Core::EventLoopImplementation> make_implementation() override;
- virtual intptr_t register_timer(Core::EventReceiver&, int milliseconds, bool should_reload) override;
+ virtual intptr_t register_timer(Core::EventReceiver&, AK::Duration interval, bool should_reload) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Core::Notifier&) override;