LibCore: Fix some thread-related memory/object leaks

This commit is contained in:
Ali Mohammad Pur
2024-06-05 10:49:31 +02:00
committed by Andreas Kling
parent 9171c35183
commit 920f470735
2 changed files with 34 additions and 12 deletions

View File

@@ -26,10 +26,12 @@ namespace {
struct ThreadData;
class TimeoutSet;
HashMap<pthread_t, OwnPtr<ThreadData>> s_thread_data;
HashMap<pthread_t, ThreadData*> s_thread_data;
pthread_key_t s_thread_key;
static pthread_rwlock_t s_thread_data_lock_impl;
static pthread_rwlock_t* s_thread_data_lock = nullptr;
thread_local pthread_t s_thread_id;
thread_local OwnPtr<ThreadData> s_this_thread_data;
short notification_type_to_poll_events(NotificationType type)
{
@@ -221,21 +223,24 @@ struct ThreadData {
if (!s_thread_data_lock) {
pthread_rwlock_init(&s_thread_data_lock_impl, nullptr);
s_thread_data_lock = &s_thread_data_lock_impl;
pthread_key_create(&s_thread_key, [](void*) {
s_this_thread_data.clear();
});
}
if (s_thread_id == 0)
s_thread_id = pthread_self();
ThreadData* data = nullptr;
pthread_rwlock_rdlock(&*s_thread_data_lock);
if (!s_thread_data.contains(s_thread_id)) {
if (!s_this_thread_data) {
data = new ThreadData;
pthread_rwlock_unlock(&*s_thread_data_lock);
s_this_thread_data = adopt_own(*data);
pthread_rwlock_wrlock(&*s_thread_data_lock);
s_thread_data.set(s_thread_id, adopt_own(*data));
s_thread_data.set(s_thread_id, s_this_thread_data.ptr());
pthread_rwlock_unlock(&*s_thread_data_lock);
} else {
data = s_thread_data.get(s_thread_id).value();
data = s_this_thread_data.ptr();
}
pthread_rwlock_unlock(&*s_thread_data_lock);
return *data;
}
@@ -253,6 +258,13 @@ struct ThreadData {
initialize_wake_pipe();
}
~ThreadData()
{
pthread_rwlock_wrlock(&*s_thread_data_lock);
s_thread_data.remove(s_thread_id);
pthread_rwlock_unlock(&*s_thread_data_lock);
}
void initialize_wake_pipe()
{
if (wake_pipe_fds[0] != -1)