Skip to content

Commit 09c8871

Browse files
committed
Move most of Pthread.initRuntime into native code. NFC
This also avoids the use of malloc both for the main pthread struct and for its TLS vars. This in turn avoids the use of `withBuiltinMalloc` since we no longer use malloc here.
1 parent 8192916 commit 09c8871

File tree

7 files changed

+25
-56
lines changed

7 files changed

+25
-56
lines changed

emcc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,6 @@ def default_setting(name, new_default):
18631863
'_emscripten_get_global_libc',
18641864
'_emscripten_main_browser_thread_id',
18651865
'_emscripten_main_thread_process_queued_calls',
1866-
'_emscripten_register_main_browser_thread_id',
18671866
'_emscripten_run_in_main_runtime_thread_js',
18681867
'_emscripten_stack_set_limits',
18691868
'_emscripten_sync_run_in_main_thread_2',

src/library_pthread.js

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
var LibraryPThread = {
88
$PThread__postset: 'if (!ENVIRONMENT_IS_PTHREAD) PThread.initMainThreadBlock();',
99
$PThread__deps: ['_emscripten_thread_init',
10-
'emscripten_register_main_browser_thread_id',
1110
'emscripten_futex_wake', '$killThread',
1211
'$cancelThread', '$cleanupThread',
13-
#if USE_ASAN || USE_LSAN
14-
, '$withBuiltinMalloc'
15-
#endif
1612
],
1713
$PThread: {
1814
// Contains all Workers that are idle/unused and not currently hosting an
@@ -37,33 +33,7 @@ var LibraryPThread = {
3733
}
3834
#endif
3935
},
40-
initRuntime: function() {
41-
#if USE_ASAN || USE_LSAN
42-
// When sanitizers are enabled, malloc is normally instrumented to call
43-
// sanitizer code that checks some things about pthreads. As we are just
44-
// setting up the main thread here, and are not ready for such calls,
45-
// call malloc directly.
46-
withBuiltinMalloc(function () {
47-
#endif
48-
49-
var tb = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
50-
51-
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}}/4; ++i) HEAPU32[tb/4+i] = 0;
52-
53-
// The pthread struct has a field that points to itself - this is used as
54-
// a magic ID to detect whether the pthread_t structure is 'alive'.
55-
{{{ makeSetValue('tb', C_STRUCTS.pthread.self, 'tb', 'i32') }}};
56-
57-
// pthread struct robust_list head should point to itself.
58-
var headPtr = tb + {{{ C_STRUCTS.pthread.robust_list }}};
59-
{{{ makeSetValue('headPtr', 0, 'headPtr', 'i32') }}};
60-
61-
// Allocate memory for thread-local storage.
62-
var tlsMemory = _malloc({{{ cDefine('PTHREAD_KEYS_MAX') * 4 }}});
63-
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) HEAPU32[tlsMemory/4+i] = 0;
64-
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tsd }}} ) >> 2, tlsMemory); // Init thread-local-storage memory array.
65-
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tid }}} ) >> 2, tb); // Main thread ID.
66-
36+
initRuntime: function(tb) {
6737
#if PTHREADS_PROFILING
6838
PThread.createProfilerBlock(tb);
6939
PThread.setThreadName(tb, "Browser main thread");
@@ -74,14 +44,9 @@ var LibraryPThread = {
7444
// globals which act as a form of TLS. Global constructors trying
7545
// to access this value will read the wrong value, but that is UB anyway.
7646
__emscripten_thread_init(tb, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
77-
_emscripten_register_main_browser_thread_id(tb);
7847
#if ASSERTIONS
7948
PThread.mainRuntimeThread = true;
8049
#endif
81-
82-
#if USE_ASAN || USE_LSAN
83-
});
84-
#endif
8550
},
8651
initWorker: function() {
8752
#if USE_CLOSURE_COMPILER
@@ -568,6 +533,7 @@ var LibraryPThread = {
568533
pthread.worker.postMessage({ 'cmd': 'cancel' });
569534
},
570535

536+
$spawnThread__deps: ['$zeroMemory'],
571537
$spawnThread: function(threadParams) {
572538
if (ENVIRONMENT_IS_PTHREAD) throw 'Internal Error! spawnThread() can only ever be called from main application thread!';
573539

@@ -582,10 +548,8 @@ var LibraryPThread = {
582548
PThread.runningWorkers.push(worker);
583549

584550
// Allocate memory for thread-local storage and initialize it to zero.
585-
var tlsMemory = _malloc({{{ cDefine('PTHREAD_KEYS_MAX') }}} * 4);
586-
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) {
587-
{{{ makeSetValue('tlsMemory', 'i*4', 0, 'i32') }}};
588-
}
551+
var tlsMemory = _malloc({{{ cDefine('PTHREAD_KEYS_MAX') * 4 }}});
552+
zeroMemory(tlsMemory, {{{ cDefine('PTHREAD_KEYS_MAX') * 4 }}});
589553

590554
var stackHigh = threadParams.stackBase + threadParams.stackSize;
591555

@@ -822,7 +786,7 @@ var LibraryPThread = {
822786
// Allocate thread block (pthread_t structure).
823787
var threadInfoStruct = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
824788
// zero-initialize thread structure.
825-
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}} >> 2; ++i) HEAPU32[(threadInfoStruct>>2) + i] = 0;
789+
zeroMemory(threadInfoStruct, {{{ C_STRUCTS.pthread.__size__ }}});
826790
{{{ makeSetValue('pthread_ptr', 0, 'threadInfoStruct', 'i32') }}};
827791

828792
// The pthread struct has a field that points to itself - this is used as a

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
static void *__pthread_getspecific(pthread_key_t k)
55
{
66
struct pthread *self = __pthread_self();
7+
// XXX EMSCRIPTEN: self->tsd can be NULL in the case of the
8+
// main thread where pthread_key_create is not called.
9+
// See __pthread_key_create for where it get assigned.
10+
if (!self->tsd) return NULL;
711
return self->tsd[k];
812
}
913

system/lib/pthread/library_pthread.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,10 @@ EMSCRIPTEN_RESULT emscripten_wait_for_call_i(
405405
return res;
406406
}
407407

408-
static pthread_t main_browser_thread_id_ = 0;
409-
410-
void emscripten_register_main_browser_thread_id(
411-
pthread_t main_browser_thread_id) {
412-
main_browser_thread_id_ = main_browser_thread_id;
413-
}
408+
static struct pthread __main_pthread;
414409

415410
pthread_t emscripten_main_browser_thread_id() {
416-
return main_browser_thread_id_;
411+
return &__main_pthread;
417412
}
418413

419414
int _emscripten_do_dispatch_to_thread(pthread_t target_thread, em_queued_call* call) {
@@ -886,8 +881,8 @@ int _emscripten_call_on_thread(int forceAsync, pthread_t targetThread, EM_FUNC_S
886881
// the main thread is waiting, we wake it up before waking up any workers.
887882
EMSCRIPTEN_KEEPALIVE void* _emscripten_main_thread_futex;
888883

889-
EM_JS(void, initPthreadsJS, (void), {
890-
PThread.initRuntime();
884+
EM_JS(void, initPthreadsJS, (void* tb), {
885+
PThread.initRuntime(tb);
891886
})
892887

893888
// We must initialize the runtime at the proper time, which is after memory is
@@ -898,6 +893,15 @@ EM_JS(void, initPthreadsJS, (void), {
898893
EMSCRIPTEN_KEEPALIVE
899894
__attribute__((constructor(48)))
900895
void __emscripten_pthread_data_constructor(void) {
901-
initPthreadsJS();
902-
pthread_self()->locale = &libc.global_locale;
896+
initPthreadsJS(&__main_pthread);
897+
// The pthread struct has a field that points to itself - this is used as
898+
// a magic ID to detect whether the pthread_t structure is 'alive'.
899+
__main_pthread.self = &__main_pthread;
900+
// pthread struct robust_list head should point to itself.
901+
__main_pthread.robust_list.head = &__main_pthread.robust_list;
902+
903+
// Main thread ID.
904+
__main_pthread.tid = (long)&__main_pthread;
905+
906+
__main_pthread.locale = &libc.global_locale;
903907
}

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ M
1414
N
1515
O
1616
P
17-
Q
1817
t
1918
u
2019
v

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.funcs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ $emscripten_current_thread_process_queued_calls
3131
$emscripten_get_global_libc
3232
$emscripten_main_thread_process_queued_calls
3333
$emscripten_proxy_main
34-
$emscripten_register_main_browser_thread_id
3534
$emscripten_run_in_main_runtime_thread_js
3635
$emscripten_stack_set_limits
3736
$emscripten_sync_run_in_main_thread
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16031
1+
16084

0 commit comments

Comments
 (0)