mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +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: https://github.com/LadybirdBrowser/ladybird/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;
|
||||
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)
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <LibCore/ThreadEventQueue.h>
|
||||
#include <LibThreading/Mutex.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
|
@ -39,15 +40,24 @@ struct ThreadEventQueue::Private {
|
|||
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()
|
||||
{
|
||||
if (!s_current_thread_event_queue) {
|
||||
// FIXME: Don't leak these.
|
||||
s_current_thread_event_queue = new ThreadEventQueue;
|
||||
pthread_once(&s_current_thread_event_queue_key_once, [] {
|
||||
pthread_key_create(&s_current_thread_event_queue_key, [](void* value) {
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue