Truly independent web browser
LibCore+UI: Destroy event loop queues before the event loops themselves
Zaggy1024a0b27b2da5de
diff --git a/Libraries/LibCore/EventLoopImplementationUnix.cpp b/Libraries/LibCore/EventLoopImplementationUnix.cppindex ca9c23ea321..9be46db200e 100644--- a/Libraries/LibCore/EventLoopImplementationUnix.cpp+++ b/Libraries/LibCore/EventLoopImplementationUnix.cpp@@ -200,6 +200,7 @@ struct ThreadData {static void destroy_thread_data(void* value){+ ThreadEventQueue::destroy_current();s_this_thread_data = nullptr;delete static_cast<ThreadData*>(value);}diff --git a/Libraries/LibCore/EventLoopImplementationWindows.cpp b/Libraries/LibCore/EventLoopImplementationWindows.cppindex 185f71c0aa9..e9cb1f1254d 100644--- a/Libraries/LibCore/EventLoopImplementationWindows.cpp+++ b/Libraries/LibCore/EventLoopImplementationWindows.cpp@@ -242,13 +242,7 @@ static bool is_process_completion_key(ULONG_PTR key)struct ThreadData {AK_ALLOC_WITH_KMALLOC;- static ThreadData* the()- {- thread_local OwnPtr<ThreadData> thread_data = make<ThreadData>();- if (thread_data)- return &*thread_data;- return nullptr;- }+ static ThreadData* the();ThreadData(): master_timer(make<EventLoopMasterTimer>())@@ -308,6 +302,29 @@ struct ThreadData {NonnullOwnPtr<EventLoopWake> wake_data;};+static thread_local ThreadData* s_thread_data;+static thread_local bool s_thread_data_was_destroyed;++struct ThreadDataOwner {+ ~ThreadDataOwner()+ {+ ThreadEventQueue::destroy_current();+ s_thread_data_was_destroyed = true;+ delete exchange(s_thread_data, nullptr);+ }+};++static thread_local ThreadDataOwner s_thread_data_owner;++ThreadData* ThreadData::the()+{+ if (!s_thread_data && !s_thread_data_was_destroyed) {+ (void)s_thread_data_owner;+ s_thread_data = new ThreadData;+ }+ return s_thread_data;+}+static MutexProtected<HashMap<pid_t, NonnullOwnPtr<EventLoopProcess>>> s_processes;// Arms (or disarms) the thread's shared waitable timer for the earliest pending deadline.@@ -716,7 +733,8 @@ void EventLoopManagerWindows::register_notifier(Notifier& notifier)void EventLoopManagerWindows::unregister_notifier(Notifier& notifier){auto* thread_data = ThreadData::the();- VERIFY(thread_data);+ if (!thread_data)+ return;if (auto notifier_data = thread_data->pipe_notifiers.take(¬ifier); notifier_data.has_value())unregister_pipe_notifier(*thread_data, notifier_data.release_value());diff --git a/Libraries/LibCore/ThreadEventQueue.cpp b/Libraries/LibCore/ThreadEventQueue.cppindex 5d40e209708..9540a11968e 100644--- a/Libraries/LibCore/ThreadEventQueue.cpp+++ b/Libraries/LibCore/ThreadEventQueue.cpp@@ -78,6 +78,15 @@ ThreadEventQueue& ThreadEventQueue::current()return *ptr;}+void ThreadEventQueue::destroy_current()+{+ // An event's receiver may post another event as it is freed, which makes the thread a new queue.+ while (auto* queue = current_or_null()) {+ pthread_setspecific(s_current_thread_event_queue_key, nullptr);+ delete queue;+ }+}+ThreadEventQueue::ThreadEventQueue(): m_private(make<Private>()){diff --git a/Libraries/LibCore/ThreadEventQueue.h b/Libraries/LibCore/ThreadEventQueue.hindex 5b90192a8d9..65c48b8c972 100644--- a/Libraries/LibCore/ThreadEventQueue.h+++ b/Libraries/LibCore/ThreadEventQueue.h@@ -28,6 +28,11 @@ public:static ThreadEventQueue* current_or_null();static ThreadEventQueue& current();+ // Destroys the current thread's queue, with the events still in it. An event loop implementation calls this before+ // destroying its own per-thread state, which those events' receivers can still use as they are freed. The queue is+ // otherwise destroyed when the thread exits.+ static void destroy_current();+// Process all queued events. Returns the number of events that were processed.size_t process();diff --git a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cppindex 4e1c8655e28..1c7f6247968 100644--- a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp+++ b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.cpp@@ -22,6 +22,11 @@ EventLoopThreadData& EventLoopThreadData::the()return s_thread_data;}+EventLoopThreadData::~EventLoopThreadData()+{+ Core::ThreadEventQueue::destroy_current();+}+static ALooperEventLoopImplementation& current_impl(){return as<ALooperEventLoopImplementation>(Core::EventLoop::current().impl());diff --git a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.hindex 999ea52cc3e..9e3e7345ed6 100644--- a/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h+++ b/UI/Android/src/main/cpp/ALooperEventLoopImplementation.h@@ -54,6 +54,8 @@ struct TimerData {struct EventLoopThreadData {static EventLoopThreadData& the();+ ~EventLoopThreadData();+HashMap<long, TimerData> timers;HashTable<Core::Notifier*> notifiers;Core::ThreadEventQueue* thread_queue = nullptr;diff --git a/UI/Qt/EventLoopImplementationQt.cpp b/UI/Qt/EventLoopImplementationQt.cppindex 8ac2328c653..0005741ba02 100644--- a/UI/Qt/EventLoopImplementationQt.cpp+++ b/UI/Qt/EventLoopImplementationQt.cpp@@ -73,6 +73,7 @@ struct ThreadData {~ThreadData(){+ Core::ThreadEventQueue::destroy_current();RWLockLocker<RWLock::Mode::Write> locker(s_thread_data_lock);s_thread_data.remove(s_thread_id.value());}