Skip to content

Commit 04cc344

Browse files
authored
Minor TLS init refactoring. NFC (#17002)
Preparing for #16948
1 parent b647c98 commit 04cc344

File tree

12 files changed

+30
-42
lines changed

12 files changed

+30
-42
lines changed

emcc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ def setup_pthreads(target):
15141514
'__emscripten_thread_init',
15151515
'__emscripten_thread_exit',
15161516
'__emscripten_thread_crashed',
1517-
'_emscripten_tls_init',
1517+
'__emscripten_tls_init',
15181518
'_pthread_self',
15191519
'executeNotifiedProxyingQueue',
15201520
]
@@ -1858,7 +1858,7 @@ def phase_linker_setup(options, state, newargs, user_settings):
18581858

18591859
if settings.USE_PTHREADS:
18601860
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += [
1861-
'$registerTlsInit',
1861+
'$registerTLSInit',
18621862
]
18631863

18641864
if settings.RELOCATABLE:

src/library_dylink.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var LibraryDylink = {
7272
'__tls_size',
7373
'__tls_align',
7474
'__set_stack_limits',
75-
'emscripten_tls_init',
75+
'_emscripten_tls_init',
7676
'__wasm_init_tls',
7777
'__wasm_call_ctors',
7878
].includes(symName)
@@ -593,8 +593,8 @@ var LibraryDylink = {
593593
// initialize the module
594594
#if USE_PTHREADS
595595
// Only one thread (currently The main thread) should call
596-
// __wasm_call_ctors, but all threads need to call emscripten_tls_init
597-
registerTlsInit(moduleExports['emscripten_tls_init'], instance.exports, metadata)
596+
// __wasm_call_ctors, but all threads need to call _emscripten_tls_init
597+
registerTLSInit(moduleExports['_emscripten_tls_init'], instance.exports, metadata)
598598
if (!ENVIRONMENT_IS_PTHREAD) {
599599
#endif
600600
var init = moduleExports['__wasm_call_ctors'];

src/library_pthread.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var LibraryPThread = {
9494
// worker.js is not compiled together with us, and must access certain
9595
// things.
9696
PThread['receiveObjectTransfer'] = PThread.receiveObjectTransfer;
97-
PThread['threadInit'] = PThread.threadInit;
97+
PThread['threadInitTLS'] = PThread.threadInitTLS;
9898
#if !MINIMAL_RUNTIME
9999
PThread['setExitStatus'] = PThread.setExitStatus;
100100
#endif
@@ -212,11 +212,11 @@ var LibraryPThread = {
212212
#endif
213213
},
214214
// Called by worker.js each time a thread is started.
215-
threadInit: function() {
215+
threadInitTLS: function() {
216216
#if PTHREADS_DEBUG
217-
err('threadInit.');
217+
err('threadInitTLS.');
218218
#endif
219-
// Call thread init functions (these are the emscripten_tls_init for each
219+
// Call thread init functions (these are the _emscripten_tls_init for each
220220
// module loaded.
221221
for (var i in PThread.tlsInitFunctions) {
222222
if (PThread.tlsInitFunctions.hasOwnProperty(i)) PThread.tlsInitFunctions[i]();
@@ -475,12 +475,12 @@ var LibraryPThread = {
475475
},
476476

477477
#if MAIN_MODULE
478-
$registerTlsInit: function(tlsInitFunc, moduleExports, metadata) {
478+
$registerTLSInit: function(tlsInitFunc, moduleExports, metadata) {
479479
#if DYLINK_DEBUG
480-
err("registerTlsInit: " + tlsInitFunc);
480+
err("registerTLSInit: " + tlsInitFunc);
481481
#endif
482482
// In relocatable builds, we use the result of calling tlsInitFunc
483-
// (`emscripten_tls_init`) to relocate the TLS exports of the module
483+
// (`_emscripten_tls_init`) to relocate the TLS exports of the module
484484
// according to this new __tls_base.
485485
function tlsInitWrapper() {
486486
var __tls_base = tlsInitFunc();
@@ -512,7 +512,7 @@ var LibraryPThread = {
512512
}
513513
},
514514
#else
515-
$registerTlsInit: function(tlsInitFunc) {
515+
$registerTLSInit: function(tlsInitFunc) {
516516
PThread.tlsInitFunctions.push(tlsInitFunc);
517517
},
518518
#endif
@@ -599,7 +599,7 @@ var LibraryPThread = {
599599
/*start_profiling=*/true
600600
#endif
601601
);
602-
PThread.threadInit();
602+
PThread.threadInitTLS();
603603
},
604604

605605
$pthreadCreateProxied__internal: true,

src/postamble_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function initRuntime(asm) {
8585
#endif
8686

8787
#if USE_PTHREADS
88-
PThread.tlsInitFunctions.push(asm['emscripten_tls_init']);
88+
PThread.tlsInitFunctions.push(asm['_emscripten_tls_init']);
8989
#endif
9090

9191
#if hasExportedFunction('___wasm_call_ctors')

src/preamble.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,9 @@ function createWasm() {
10221022

10231023
#if USE_PTHREADS
10241024
#if MAIN_MODULE
1025-
registerTlsInit(Module['asm']['emscripten_tls_init'], instance.exports, metadata);
1025+
registerTLSInit(Module['asm']['_emscripten_tls_init'], instance.exports, metadata);
10261026
#else
1027-
registerTlsInit(Module['asm']['emscripten_tls_init']);
1027+
registerTLSInit(Module['asm']['_emscripten_tls_init']);
10281028
#endif
10291029
#endif
10301030

src/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ self.onmessage = (e) => {
211211
// Also call inside JS module to set up the stack frame for this pthread in JS module scope
212212
Module['establishStackSpace']();
213213
Module['PThread'].receiveObjectTransfer(e.data);
214-
Module['PThread'].threadInit();
214+
Module['PThread'].threadInitTLS();
215215

216216
if (!initializedJS) {
217217
#if EMBIND

system/lib/libc/musl/src/thread/pthread_join.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ static void dummy1(pthread_t t)
77
}
88
weak_alias(dummy1, __tl_sync);
99

10-
#ifdef __EMSCRIPTEN__ // XXX Emscripten add extern for __emscripten_thread_cleanup
11-
extern void __emscripten_thread_cleanup(pthread_t thread);
12-
#endif
13-
1410
static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
1511
{
1612
#ifdef __EMSCRIPTEN__

system/lib/pthread/emscripten_thread_state.S

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#define PTR i32
55
#endif
66

7-
.globaltype __tls_base, PTR
8-
97
.globaltype thread_ptr, PTR
108
thread_ptr:
119

@@ -37,14 +35,6 @@ __set_thread_state:
3735
global.set supports_wait
3836
end_function
3937

40-
# Accessor for `__tls_base` symbol which is a wasm global an not directly
41-
# accessible from C/C++.
42-
.globl _emscripten_tls_base
43-
_emscripten_tls_base:
44-
.functype _emscripten_tls_base () -> (PTR)
45-
global.get __tls_base
46-
end_function
47-
4838
# Semantically the same as testing "!ENVIRONMENT_IS_PTHREAD" in JS
4939
.globl emscripten_is_main_runtime_thread
5040
emscripten_is_main_runtime_thread:

system/lib/pthread/emscripten_tls_init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int is_non_primary(void) {
4646
return val;
4747
}
4848

49-
void emscripten_tls_free() {
49+
void _emscripten_tls_free() {
5050
void* tls_block = __builtin_wasm_tls_base();
5151
if (tls_block && is_non_primary()) {
5252
#ifdef DEBUG_TLS
@@ -56,7 +56,7 @@ void emscripten_tls_free() {
5656
}
5757
}
5858

59-
void* emscripten_tls_init(void) {
59+
void* _emscripten_tls_init(void) {
6060
size_t tls_size = __builtin_wasm_tls_size();
6161
void* tls_block = __builtin_wasm_tls_base();
6262
if (is_non_primary() || (!tls_block && tls_size)) {

system/lib/pthread/pthread_create.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
// See musl's pthread_create.c
2424

25-
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
26-
extern void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
27-
extern int _emscripten_default_pthread_stack_size();
28-
extern void __emscripten_thread_cleanup(pthread_t thread);
25+
int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
26+
int _emscripten_default_pthread_stack_size();
27+
void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
28+
2929
extern int8_t __dso_handle;
3030

3131
static void dummy_0()
@@ -196,8 +196,6 @@ void _emscripten_thread_free_data(pthread_t t) {
196196
emscripten_builtin_free(t);
197197
}
198198

199-
void emscripten_tls_free(void);
200-
201199
void _emscripten_thread_exit(void* result) {
202200
struct pthread *self = __pthread_self();
203201
assert(self);
@@ -212,7 +210,7 @@ void _emscripten_thread_exit(void* result) {
212210
// Call into the musl function that runs destructors of all thread-specific data.
213211
__pthread_tsd_run_dtors();
214212

215-
emscripten_tls_free();
213+
_emscripten_tls_free();
216214

217215
if (!--libc.threads_minus_1) libc.need_locks = 0;
218216

0 commit comments

Comments
 (0)