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

LibCore+Ladybird: Don't force timer ids to be integer just to remap them

If we don't force event loop to fit timer id into integer, we can
eliminate awkward IDAllocator inside EventLoopImplementations.
This commit is contained in:
Dan Klishch 2024-02-18 00:11:38 -05:00 committed by Andrew Kaster
parent bed4af6fef
commit b77996884e
10 changed files with 36 additions and 46 deletions

View file

@ -125,12 +125,12 @@ void EventLoop::notify_forked(ForkEvent)
current().m_impl->notify_forked_and_in_child();
}
int EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
intptr_t EventLoop::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
{
return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible);
}
void EventLoop::unregister_timer(int timer_id)
void EventLoop::unregister_timer(intptr_t timer_id)
{
EventLoopManager::the().unregister_timer(timer_id);
}

View file

@ -76,8 +76,8 @@ public:
bool was_exit_requested() const;
// The registration functions act upon the current loop of the current thread.
static int register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
static void unregister_timer(int timer_id);
static intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
static void unregister_timer(intptr_t timer_id);
static void register_notifier(Badge<Notifier>, Notifier&);
static void unregister_notifier(Badge<Notifier>, Notifier&);

View file

@ -23,8 +23,8 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() = 0;
virtual int register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible) = 0;
virtual void unregister_timer(int timer_id) = 0;
virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible) = 0;
virtual void unregister_timer(intptr_t timer_id) = 0;
virtual void register_notifier(Notifier&) = 0;
virtual void unregister_notifier(Notifier&) = 0;

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/IDAllocator.h>
#include <AK/Singleton.h>
#include <AK/TemporaryChange.h>
#include <AK/Time.h>
@ -94,7 +93,7 @@ struct ThreadData {
}
// Each thread has its own timers, notifiers and a wake pipe.
HashMap<int, NonnullOwnPtr<EventLoopTimer>> timers;
HashTable<EventLoopTimer*> timers;
Vector<pollfd> poll_fds;
HashMap<Notifier*, size_t> notifier_by_ptr;
@ -105,8 +104,6 @@ struct ThreadData {
int wake_pipe_fds[2] { -1, -1 };
pid_t pid { 0 };
IDAllocator id_allocator;
};
EventLoopImplementationUnix::EventLoopImplementationUnix()
@ -234,7 +231,7 @@ try_select_again:
auto now = MonotonicTime::now_coarse();
for (auto& it : thread_data.timers) {
auto& timer = *it.value;
auto& timer = *it;
if (!timer.has_expired(now))
continue;
auto owner = timer.owner.strong_ref();
@ -364,9 +361,9 @@ Optional<MonotonicTime> EventLoopManagerUnix::get_next_timer_expiration()
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();
if (it.value->fire_when_not_visible == TimerShouldFireWhenNotVisible::No
auto& fire_time = it->fire_time;
auto owner = it->owner.strong_ref();
if (it->fire_when_not_visible == TimerShouldFireWhenNotVisible::No
&& owner && !owner->is_visible_for_timer_purposes()) {
continue;
}
@ -493,27 +490,26 @@ void EventLoopManagerUnix::unregister_signal(int handler_id)
info.signal_handlers.remove(remove_signal_number);
}
int EventLoopManagerUnix::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
intptr_t EventLoopManagerUnix::register_timer(EventReceiver& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
{
VERIFY(milliseconds >= 0);
auto& thread_data = ThreadData::the();
auto timer = make<EventLoopTimer>();
auto timer = new EventLoopTimer;
timer->owner = object;
timer->interval = Duration::from_milliseconds(milliseconds);
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();
timer->timer_id = timer_id;
thread_data.timers.set(timer_id, move(timer));
return timer_id;
thread_data.timers.set(timer);
return bit_cast<intptr_t>(timer);
}
void EventLoopManagerUnix::unregister_timer(int timer_id)
void EventLoopManagerUnix::unregister_timer(intptr_t timer_id)
{
auto& thread_data = ThreadData::the();
thread_data.id_allocator.deallocate(timer_id);
VERIFY(thread_data.timers.remove(timer_id));
auto* timer = bit_cast<EventLoopTimer*>(timer_id);
VERIFY(thread_data.timers.remove(timer));
delete timer;
}
void EventLoopManagerUnix::register_notifier(Notifier& notifier)

View file

@ -17,8 +17,8 @@ public:
virtual NonnullOwnPtr<EventLoopImplementation> make_implementation() override;
virtual int register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible) override;
virtual void unregister_timer(int timer_id) override;
virtual intptr_t register_timer(EventReceiver&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible) override;
virtual void unregister_timer(intptr_t timer_id) override;
virtual void register_notifier(Notifier&) override;
virtual void unregister_notifier(Notifier&) override;

View file

@ -169,7 +169,7 @@ protected:
private:
EventReceiver* m_parent { nullptr };
ByteString m_name;
int m_timer_id { 0 };
intptr_t m_timer_id { 0 };
Vector<NonnullRefPtr<EventReceiver>> m_children;
Function<bool(Core::Event&)> m_event_filter;
};