From ff75c15ba3fd33918fb030ad69908fd60dd77930 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 17 May 2026 00:28:03 +0300 Subject: [PATCH] LibC: Move pthread keys to TCB This removes all TLS relocations from libc which may become handy ;) --- kernel/kernel/Process.cpp | 2 ++ .../LibC/include/bits/types/pthread_key_t.h | 14 +++++++++ .../LibC/include/bits/types/pthread_types.h | 3 +- .../LibC/include/bits/types/uthread.h | 4 +++ userspace/libraries/LibC/pthread.cpp | 31 ++++++++++--------- userspace/libraries/LibC/unistd.cpp | 2 ++ userspace/programs/DynamicLoader/main.cpp | 2 ++ 7 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 userspace/libraries/LibC/include/bits/types/pthread_key_t.h diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index f1533741..d2ff1f6e 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -439,6 +439,8 @@ namespace Kernel .cancel_type = 0, .cancel_state = 0, .canceled = 0, + .specific_keys = {}, + .specific_values = {}, .dtv = { 0, region->vaddr() } }; diff --git a/userspace/libraries/LibC/include/bits/types/pthread_key_t.h b/userspace/libraries/LibC/include/bits/types/pthread_key_t.h new file mode 100644 index 00000000..6ee016a6 --- /dev/null +++ b/userspace/libraries/LibC/include/bits/types/pthread_key_t.h @@ -0,0 +1,14 @@ +#ifndef _BITS_TYPES_PTHREAD_KEY_H +#define _BITS_TYPES_PTHREAD_KEY_H 1 + +// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html + +#include + +__BEGIN_DECLS + +typedef unsigned pthread_key_t; + +__END_DECLS + +#endif diff --git a/userspace/libraries/LibC/include/bits/types/pthread_types.h b/userspace/libraries/LibC/include/bits/types/pthread_types.h index 1a66eb75..c509dfca 100644 --- a/userspace/libraries/LibC/include/bits/types/pthread_types.h +++ b/userspace/libraries/LibC/include/bits/types/pthread_types.h @@ -8,14 +8,13 @@ __BEGIN_DECLS #include +#include #include #include typedef int pthread_once_t; -typedef unsigned pthread_key_t; - typedef pthread_t pthread_spinlock_t; typedef struct diff --git a/userspace/libraries/LibC/include/bits/types/uthread.h b/userspace/libraries/LibC/include/bits/types/uthread.h index f83b1d4e..187875d8 100644 --- a/userspace/libraries/LibC/include/bits/types/uthread.h +++ b/userspace/libraries/LibC/include/bits/types/uthread.h @@ -8,7 +8,9 @@ __BEGIN_DECLS #define __need_size_t #include +#include #include +#include #include typedef struct _pthread_cleanup_t @@ -44,6 +46,8 @@ struct uthread int cancel_type; int cancel_state; volatile int canceled; + pthread_key_t specific_keys[PTHREAD_KEYS_MAX]; + void* specific_values[PTHREAD_KEYS_MAX]; // FIXME: make this dynamic uintptr_t dtv[1 + 256]; }; diff --git a/userspace/libraries/LibC/pthread.cpp b/userspace/libraries/LibC/pthread.cpp index 3f3aeefa..18db748e 100644 --- a/userspace/libraries/LibC/pthread.cpp +++ b/userspace/libraries/LibC/pthread.cpp @@ -137,12 +137,6 @@ void pthread_cleanup_push(void (*routine)(void*), void* arg) uthread->cleanup_stack = cleanup; } -static thread_local struct -{ - void* value; - pthread_key_t key; -} s_pthread_key_values[PTHREAD_KEYS_MAX] {}; - static pthread_key_t s_pthread_key_current = 1; static pthread_key_t s_pthread_key_map[PTHREAD_KEYS_MAX] {}; static void (*s_pthread_key_destructors[PTHREAD_KEYS_MAX])(void*) {}; @@ -190,17 +184,19 @@ void* pthread_getspecific(pthread_key_t key) { void* ret = nullptr; + auto* uthread = _get_uthread(); + pthread_spin_lock(&s_pthread_key_lock); for (size_t i = 0; i < PTHREAD_KEYS_MAX; i++) { if (s_pthread_key_map[i] != key) continue; - if (s_pthread_key_values[i].key != key) + if (uthread->specific_keys[i] != key) { - s_pthread_key_values[i].key = key; - s_pthread_key_values[i].value = nullptr; + uthread->specific_keys[i] = key; + uthread->specific_values[i] = nullptr; } - ret = s_pthread_key_values[i].value; + ret = uthread->specific_values[i]; break; } pthread_spin_unlock(&s_pthread_key_lock); @@ -212,14 +208,16 @@ int pthread_setspecific(pthread_key_t key, const void* value) { int ret = EINVAL; + auto* uthread = _get_uthread(); + pthread_spin_lock(&s_pthread_key_lock); for (size_t i = 0; i < PTHREAD_KEYS_MAX; i++) { if (s_pthread_key_map[i] != key) continue; - if (s_pthread_key_values[i].key != key) - s_pthread_key_values[i].key = key; - s_pthread_key_values[i].value = const_cast(value); + if (uthread->specific_keys[i] != key) + uthread->specific_keys[i] = key; + uthread->specific_values[i] = const_cast(value); ret = 0; break; } @@ -419,6 +417,8 @@ int pthread_create(pthread_t* __restrict thread_id, const pthread_attr_t* __rest .cancel_type = PTHREAD_CANCEL_DEFERRED, .cancel_state = PTHREAD_CANCEL_ENABLE, .canceled = 0, + .specific_keys = {}, + .specific_values = {}, .dtv = { self->dtv[0] } }; @@ -468,10 +468,11 @@ void pthread_exit(void* value_ptr) void* value = nullptr; pthread_spin_lock(&s_pthread_key_lock); - if (s_pthread_key_map[i] && s_pthread_key_values[i].key == s_pthread_key_map[i]) + if (s_pthread_key_map[i] && uthread->specific_keys[i] == s_pthread_key_map[i]) { destructor = s_pthread_key_destructors[i]; - value = s_pthread_key_values[i].value; + value = uthread->specific_values[i]; + uthread->specific_values[i] = nullptr; } pthread_spin_unlock(&s_pthread_key_lock); diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index 1a62e7db..cd0a804c 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -112,6 +112,8 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t .cancel_type = PTHREAD_CANCEL_DEFERRED, .cancel_state = PTHREAD_CANCEL_ENABLE, .canceled = false, + .specific_keys = {}, + .specific_values = {}, .dtv = { 0 }, }; diff --git a/userspace/programs/DynamicLoader/main.cpp b/userspace/programs/DynamicLoader/main.cpp index 0638b584..e669d13a 100644 --- a/userspace/programs/DynamicLoader/main.cpp +++ b/userspace/programs/DynamicLoader/main.cpp @@ -1398,6 +1398,8 @@ static void initialize_tls(MasterTLS master_tls) .cancel_type = PTHREAD_CANCEL_DEFERRED, .cancel_state = PTHREAD_CANCEL_ENABLE, .canceled = false, + .specific_keys = {}, + .specific_values = {}, .dtv = {}, };