LibC: Move pthread keys to TCB

This removes all TLS relocations from libc which may become handy ;)
This commit is contained in:
2026-05-17 00:28:03 +03:00
parent 9e6fa0a1ba
commit ff75c15ba3
7 changed files with 41 additions and 17 deletions

View File

@@ -439,6 +439,8 @@ namespace Kernel
.cancel_type = 0,
.cancel_state = 0,
.canceled = 0,
.specific_keys = {},
.specific_values = {},
.dtv = { 0, region->vaddr() }
};

View File

@@ -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 <sys/cdefs.h>
__BEGIN_DECLS
typedef unsigned pthread_key_t;
__END_DECLS
#endif

View File

@@ -8,14 +8,13 @@
__BEGIN_DECLS
#include <bits/types/pthread_attr_t.h>
#include <bits/types/pthread_key_t.h>
#include <bits/types/pthread_t.h>
#include <stdint.h>
typedef int pthread_once_t;
typedef unsigned pthread_key_t;
typedef pthread_t pthread_spinlock_t;
typedef struct

View File

@@ -8,7 +8,9 @@ __BEGIN_DECLS
#define __need_size_t
#include <sys/types.h>
#include <bits/types/pthread_key_t.h>
#include <bits/types/pthread_t.h>
#include <limits.h>
#include <stdint.h>
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];
};

View File

@@ -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<void*>(value);
if (uthread->specific_keys[i] != key)
uthread->specific_keys[i] = key;
uthread->specific_values[i] = const_cast<void*>(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);

View File

@@ -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 },
};

View File

@@ -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 = {},
};