1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 01:47:36 +00:00

LibCore+Ladybird: Make unregistering timer infallible

Let's force calling code to provide valid timer ids. No code changes are
required since, surprise, nobody used this obscure functionality.
This commit is contained in:
Dan Klishch 2024-02-18 00:10:23 -05:00 committed by Andrew Kaster
parent 21097d1c9e
commit bed4af6fef
10 changed files with 17 additions and 23 deletions

View file

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

View file

@ -82,18 +82,15 @@ int CFEventLoopManager::register_timer(Core::EventReceiver& receiver, int interv
return timer_id; return timer_id;
} }
bool CFEventLoopManager::unregister_timer(int timer_id) void CFEventLoopManager::unregister_timer(int timer_id)
{ {
auto& thread_data = ThreadData::the(); auto& thread_data = ThreadData::the();
thread_data.timer_id_allocator.deallocate(timer_id); thread_data.timer_id_allocator.deallocate(timer_id);
if (auto timer = thread_data.timers.take(timer_id); timer.has_value()) { auto timer = thread_data.timers.take(timer_id);
CFRunLoopTimerInvalidate(*timer); VERIFY(timer.has_value());
CFRelease(*timer); CFRunLoopTimerInvalidate(*timer);
return true; CFRelease(*timer);
}
return false;
} }
static void socket_notifier(CFSocketRef socket, CFSocketCallBackType notification_type, CFDataRef, void const*, void* info) static void socket_notifier(CFSocketRef socket, CFSocketCallBackType notification_type, CFDataRef, void const*, void* info)

View file

@ -109,11 +109,11 @@ int EventLoopManagerQt::register_timer(Core::EventReceiver& object, int millisec
return timer_id; return timer_id;
} }
bool EventLoopManagerQt::unregister_timer(int timer_id) void EventLoopManagerQt::unregister_timer(int timer_id)
{ {
auto& thread_data = ThreadData::the(); auto& thread_data = ThreadData::the();
thread_data.timer_id_allocator.deallocate(timer_id); thread_data.timer_id_allocator.deallocate(timer_id);
return thread_data.timers.remove(timer_id); VERIFY(thread_data.timers.remove(timer_id));
} }
static void qt_notifier_activated(Core::Notifier& notifier) static void qt_notifier_activated(Core::Notifier& notifier)

View file

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

View file

@ -130,9 +130,9 @@ int EventLoop::register_timer(EventReceiver& object, int milliseconds, bool shou
return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible); return EventLoopManager::the().register_timer(object, milliseconds, should_reload, fire_when_not_visible);
} }
bool EventLoop::unregister_timer(int timer_id) void EventLoop::unregister_timer(int timer_id)
{ {
return EventLoopManager::the().unregister_timer(timer_id); EventLoopManager::the().unregister_timer(timer_id);
} }
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier) void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)

View file

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

View file

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

View file

@ -509,11 +509,11 @@ int EventLoopManagerUnix::register_timer(EventReceiver& object, int milliseconds
return timer_id; return timer_id;
} }
bool EventLoopManagerUnix::unregister_timer(int timer_id) void EventLoopManagerUnix::unregister_timer(int timer_id)
{ {
auto& thread_data = ThreadData::the(); auto& thread_data = ThreadData::the();
thread_data.id_allocator.deallocate(timer_id); thread_data.id_allocator.deallocate(timer_id);
return thread_data.timers.remove(timer_id); VERIFY(thread_data.timers.remove(timer_id));
} }
void EventLoopManagerUnix::register_notifier(Notifier& notifier) void EventLoopManagerUnix::register_notifier(Notifier& notifier)

View file

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

View file

@ -129,10 +129,7 @@ void EventReceiver::stop_timer()
{ {
if (!m_timer_id) if (!m_timer_id)
return; return;
bool success = Core::EventLoop::unregister_timer(m_timer_id); Core::EventLoop::unregister_timer(m_timer_id);
if (!success) {
dbgln("{:p} could not unregister timer {}", this, m_timer_id);
}
m_timer_id = 0; m_timer_id = 0;
} }