Skip to content

Commit

Permalink
mre shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Pizlo committed Jan 19, 2024
1 parent c7c3281 commit 389e313
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/env/__init_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@
#include "libc.h"
#include "atomic.h"
#include "syscall.h"
#include <stdfil.h>

volatile int __thread_list_lock;

static void* internal_thread_data_key;

static void internal_thread_data_destructor(void* data)
{
zfree(data);
}

hidden void __init_tls(void)
{
internal_thread_data_key = zthread_key_create(internal_thread_data_destructor);
ZASSERT(internal_thread_data_key);
set_new_internal_thread_data();
}

hidden internal_thread_data* get_internal_thread_data(void)
{
ZASSERT(internal_thread_data_key);
return (internal_thread_data*)zthread_getspecific(internal_thread_data_key);
}

hidden void set_new_internal_thread_data(void)
{
ZASSERT(!get_internal_thread_data());
internal_thread_data* data = zalloc(internal_thread_data, 1);
data->the_errno = 0;
data->locale = &libc.global_locale;
ZASSERT(zthread_setspecific(internal_thread_data_key, data));
}

3 changes: 2 additions & 1 deletion src/env/__libc_start_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void __init_libc(char **envp, char *pn)
{
size_t i, *auxv, aux[AUX_CNT] = { 0 };

zregister_sys_errno_handler(errno_handler);
zthread_boot_main_thread();
__init_tls();
zregister_sys_errno_handler(errno_handler);

__dl_initerror();

Expand Down
5 changes: 1 addition & 4 deletions src/errno/__errno_location.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include <errno.h>
#include "pthread_impl.h"

// FIXME: We'll have to fix this when we support threads!!!!!
static int global_errno_hack;

int *__errno_location(void)
{
return &global_errno_hack;
return &get_internal_thread_data()->the_errno;
}

weak_alias(__errno_location, ___errno_location);
2 changes: 1 addition & 1 deletion src/internal/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern hidden struct __libc __libc;
#define libc __libc

void __init_libc(char **, char *);
hidden void __init_tls(size_t *);
hidden void __init_tls(void);
hidden void __init_ssp(void *);
hidden void __libc_start_init(void);
hidden void __funcs_on_exit(void);
Expand Down
4 changes: 2 additions & 2 deletions src/internal/locale_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ hidden char *__gettextdomain(void);
#define C_LOCALE ((locale_t)&__c_locale)
#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)

#define CURRENT_LOCALE (__pthread_self()->locale)
#define CURRENT_LOCALE (get_internal_thread_data()->locale)

#define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
#define CURRENT_UTF8 (!!get_internal_thread_data()->locale->cat[LC_CTYPE])

#undef MB_CUR_MAX
#define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
Expand Down
8 changes: 8 additions & 0 deletions src/internal/pthread_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ struct pthread {
#endif
};

typedef struct {
int the_errno;
locale_t locale;
} internal_thread_data;

hidden internal_thread_data* get_internal_thread_data(void);
hidden void set_new_internal_thread_data(void);

enum {
DT_EXITED = 0,
DT_EXITING,
Expand Down

0 comments on commit 389e313

Please sign in to comment.