Skip to content

Commit 7f72096

Browse files
committed
Use C11 _Thread_local for current locale. NFC
We already do a similar thing for `__errno_location`. This means that locale usage no longer depends on pthread_self, which means it works correctly in Wasm Workers. I guess the codesize savings here come from the fact that the TLS data doesn't need to initialized programmatically anymore.
1 parent 1f839bb commit 7f72096

File tree

9 files changed

+36
-13
lines changed

9 files changed

+36
-13
lines changed

src/struct_info_generated.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,11 +1036,11 @@
10361036
"p_proto": 8
10371037
},
10381038
"pthread": {
1039-
"__size__": 132,
1040-
"profilerBlock": 112,
1041-
"stack": 52,
1042-
"stack_size": 56,
1043-
"waiting_async": 128
1039+
"__size__": 124,
1040+
"profilerBlock": 104,
1041+
"stack": 48,
1042+
"stack_size": 52,
1043+
"waiting_async": 120
10441044
},
10451045
"pthread_attr_t": {
10461046
"__size__": 44,

src/struct_info_generated_wasm64.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,11 +1036,11 @@
10361036
"p_proto": 16
10371037
},
10381038
"pthread": {
1039-
"__size__": 232,
1040-
"profilerBlock": 200,
1041-
"stack": 88,
1042-
"stack_size": 96,
1043-
"waiting_async": 228
1039+
"__size__": 216,
1040+
"profilerBlock": 184,
1041+
"stack": 80,
1042+
"stack_size": 88,
1043+
"waiting_async": 212
10441044
},
10451045
"pthread_attr_t": {
10461046
"__size__": 88,

system/lib/libc/musl/src/internal/locale_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ hidden char *__gettextdomain(void);
4545
#define C_LOCALE ((locale_t)&__c_locale)
4646
#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
4747

48+
#ifdef __EMSCRIPTEN__
49+
extern _Thread_local locale_t __tls_locale;
50+
51+
#define CURRENT_LOCALE (__tls_locale)
52+
53+
#define CURRENT_UTF8 (!!__tls_locale->cat[LC_CTYPE])
54+
#else
4855
#define CURRENT_LOCALE (__pthread_self()->locale)
4956

5057
#define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
58+
#endif
5159

5260
#undef MB_CUR_MAX
5361
#define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)

system/lib/libc/musl/src/internal/pthread_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ struct pthread {
3939

4040
/* Part 2 -- implementation details, non-ABI. */
4141
int tid;
42+
#ifndef __EMSCRIPTEN__
43+
// Emscripten uses C11 _Thread_local instead for errno
4244
int errno_val;
45+
#endif
4346
volatile int detach_state;
4447
volatile int cancel;
4548
volatile unsigned char canceldisable, cancelasync;
@@ -60,7 +63,10 @@ struct pthread {
6063
} robust_list;
6164
int h_errno_val;
6265
volatile int timer_id;
66+
#ifndef __EMSCRIPTEN__
67+
// Emscripten uses C11 _Thread_local instead for locale
6368
locale_t locale;
69+
#endif
6470
volatile int killlock[1];
6571
char *dlerror_buf;
6672
void *stdio_locks;

system/lib/libc/musl/src/locale/uselocale.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
#include "pthread_impl.h"
33
#include "libc.h"
44

5+
#ifdef __EMSCRIPTEN__
6+
_Thread_local locale_t __tls_locale = &libc.global_locale;
7+
#endif
8+
59
locale_t __uselocale(locale_t new)
610
{
11+
#ifdef __EMSCRIPTEN__
12+
locale_t old = __tls_locale;
13+
locale_t global = &libc.global_locale;
14+
15+
if (new) __tls_locale = new == LC_GLOBAL_LOCALE ? global : new;
16+
#else
717
pthread_t self = __pthread_self();
818
locale_t old = self->locale;
919
locale_t global = &libc.global_locale;
1020

1121
if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
22+
#endif
1223

1324
return old == global ? LC_GLOBAL_LOCALE : old;
1425
}

system/lib/pthread/library_pthread.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ void _emscripten_init_main_thread(void) {
147147
// Main thread ID is always 1. It can't be 0 because musl assumes
148148
// tid is always non-zero.
149149
__main_pthread.tid = getpid();
150-
__main_pthread.locale = &libc.global_locale;
151150
// pthread struct prev and next should initially point to itself (see __init_tp),
152151
// this is used by pthread_key_delete for deleting thread-specific data.
153152
__main_pthread.next = __main_pthread.prev = &__main_pthread;

system/lib/pthread/pthread_create.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ int __pthread_create(pthread_t* restrict res,
182182
// pthread struct robust_list head should point to itself.
183183
new->robust_list.head = &new->robust_list.head;
184184

185-
new->locale = &libc.global_locale;
186185
if (attr._a_detach) {
187186
new->detach_state = DT_DETACHED;
188187
} else {

system/lib/pthread/pthread_self_stub.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ extern int __stack_low;
3030

3131
__attribute__((constructor))
3232
static void init_pthread_self(void) {
33-
__main_pthread.locale = &libc.global_locale;
3433
__main_pthread.tid = getpid();
3534
__main_pthread.stack = &__stack_high;
3635
__main_pthread.stack_size = ((size_t)&__stack_high) - ((size_t)&__stack_low);

test/core/test_dlfcn_self.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ __stack_chk_guard
1414
__stack_pointer
1515
__threwValue
1616
__timezone
17+
__tls_locale
1718
__tzname
1819
_environ
1920
_ns_flagdata

0 commit comments

Comments
 (0)