mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-10 11:36:22 +00:00
LibCore: Fix some thread-related memory/object leaks
This commit is contained in:
parent
9171c35183
commit
920f470735
Notes:
sideshowbarker
2024-07-17 06:51:48 +09:00
Author: https://github.com/alimpfard
Commit: 920f470735
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/213
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/awesomekling
2 changed files with 34 additions and 12 deletions
|
@ -26,10 +26,12 @@ namespace {
|
||||||
struct ThreadData;
|
struct ThreadData;
|
||||||
class TimeoutSet;
|
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_impl;
|
||||||
static pthread_rwlock_t* s_thread_data_lock = nullptr;
|
static pthread_rwlock_t* s_thread_data_lock = nullptr;
|
||||||
thread_local pthread_t s_thread_id;
|
thread_local pthread_t s_thread_id;
|
||||||
|
thread_local OwnPtr<ThreadData> s_this_thread_data;
|
||||||
|
|
||||||
short notification_type_to_poll_events(NotificationType type)
|
short notification_type_to_poll_events(NotificationType type)
|
||||||
{
|
{
|
||||||
|
@ -221,21 +223,24 @@ struct ThreadData {
|
||||||
if (!s_thread_data_lock) {
|
if (!s_thread_data_lock) {
|
||||||
pthread_rwlock_init(&s_thread_data_lock_impl, nullptr);
|
pthread_rwlock_init(&s_thread_data_lock_impl, nullptr);
|
||||||
s_thread_data_lock = &s_thread_data_lock_impl;
|
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)
|
if (s_thread_id == 0)
|
||||||
s_thread_id = pthread_self();
|
s_thread_id = pthread_self();
|
||||||
ThreadData* data = nullptr;
|
ThreadData* data = nullptr;
|
||||||
pthread_rwlock_rdlock(&*s_thread_data_lock);
|
if (!s_this_thread_data) {
|
||||||
if (!s_thread_data.contains(s_thread_id)) {
|
|
||||||
data = new ThreadData;
|
data = new ThreadData;
|
||||||
pthread_rwlock_unlock(&*s_thread_data_lock);
|
s_this_thread_data = adopt_own(*data);
|
||||||
|
|
||||||
pthread_rwlock_wrlock(&*s_thread_data_lock);
|
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());
|
||||||
} else {
|
|
||||||
data = s_thread_data.get(s_thread_id).value();
|
|
||||||
}
|
|
||||||
pthread_rwlock_unlock(&*s_thread_data_lock);
|
pthread_rwlock_unlock(&*s_thread_data_lock);
|
||||||
|
} else {
|
||||||
|
data = s_this_thread_data.ptr();
|
||||||
|
}
|
||||||
return *data;
|
return *data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,6 +258,13 @@ struct ThreadData {
|
||||||
initialize_wake_pipe();
|
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()
|
void initialize_wake_pipe()
|
||||||
{
|
{
|
||||||
if (wake_pipe_fds[0] != -1)
|
if (wake_pipe_fds[0] != -1)
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <LibCore/ThreadEventQueue.h>
|
#include <LibCore/ThreadEventQueue.h>
|
||||||
#include <LibThreading/Mutex.h>
|
#include <LibThreading/Mutex.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -39,15 +40,24 @@ struct ThreadEventQueue::Private {
|
||||||
bool warned_promise_count { false };
|
bool warned_promise_count { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
static thread_local ThreadEventQueue* s_current_thread_event_queue;
|
static pthread_key_t s_current_thread_event_queue_key;
|
||||||
|
static pthread_once_t s_current_thread_event_queue_key_once = PTHREAD_ONCE_INIT;
|
||||||
|
|
||||||
ThreadEventQueue& ThreadEventQueue::current()
|
ThreadEventQueue& ThreadEventQueue::current()
|
||||||
{
|
{
|
||||||
if (!s_current_thread_event_queue) {
|
pthread_once(&s_current_thread_event_queue_key_once, [] {
|
||||||
// FIXME: Don't leak these.
|
pthread_key_create(&s_current_thread_event_queue_key, [](void* value) {
|
||||||
s_current_thread_event_queue = new ThreadEventQueue;
|
if (value)
|
||||||
|
delete static_cast<ThreadEventQueue*>(value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
auto* ptr = static_cast<ThreadEventQueue*>(pthread_getspecific(s_current_thread_event_queue_key));
|
||||||
|
if (!ptr) {
|
||||||
|
ptr = new ThreadEventQueue;
|
||||||
|
pthread_setspecific(s_current_thread_event_queue_key, ptr);
|
||||||
}
|
}
|
||||||
return *s_current_thread_event_queue;
|
return *ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadEventQueue::ThreadEventQueue()
|
ThreadEventQueue::ThreadEventQueue()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue