Skip to content

Commit

Permalink
LibCore: Handle destroyed owner when unregistering timers and notifiers
Browse files Browse the repository at this point in the history
Cherry-picked from SerenityOS/serenity@9f4f319
  • Loading branch information
alimpfard authored and ADKaster committed Jun 27, 2024
1 parent 944dbfd commit 3214f2c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Userland/Libraries/LibCore/EventLoopImplementationUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ struct ThreadData {
return *data;
}

static ThreadData& for_thread(pthread_t thread_id)
static ThreadData* for_thread(pthread_t thread_id)
{
pthread_rwlock_rdlock(&*s_thread_data_lock);
auto& result = *s_thread_data.get(thread_id).value();
auto result = s_thread_data.get(thread_id).value_or(nullptr);
pthread_rwlock_unlock(&*s_thread_data_lock);
return result;
}
Expand Down Expand Up @@ -656,7 +656,10 @@ intptr_t EventLoopManagerUnix::register_timer(EventReceiver& object, int millise
void EventLoopManagerUnix::unregister_timer(intptr_t timer_id)
{
auto* timer = bit_cast<EventLoopTimer*>(timer_id);
auto& thread_data = ThreadData::for_thread(timer->owner_thread);
auto thread_data_ptr = ThreadData::for_thread(timer->owner_thread);
if (!thread_data_ptr)
return;
auto& thread_data = *thread_data_ptr;
auto expected = false;
if (timer->is_being_deleted.compare_exchange_strong(expected, true, AK::MemoryOrder::memory_order_acq_rel)) {
if (timer->is_scheduled())
Expand All @@ -682,8 +685,11 @@ void EventLoopManagerUnix::register_notifier(Notifier& notifier)

void EventLoopManagerUnix::unregister_notifier(Notifier& notifier)
{
auto& thread_data = ThreadData::for_thread(notifier.owner_thread());
auto thread_data_ptr = ThreadData::for_thread(notifier.owner_thread());
if (!thread_data_ptr)
return;

auto& thread_data = *thread_data_ptr;
auto it = thread_data.notifier_by_ptr.find(&notifier);
VERIFY(it != thread_data.notifier_by_ptr.end());

Expand Down

0 comments on commit 3214f2c

Please sign in to comment.