Skip to content

Commit 3637f1e

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 1abc342 commit 3637f1e

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
@@ -426,15 +426,10 @@ EMSCRIPTEN_RESULT emscripten_wait_for_call_i(
426426
return res;
427427
}
428428

429-
static pthread_t main_browser_thread_id_ = 0;
430-
431-
void emscripten_register_main_browser_thread_id(
432-
pthread_t main_browser_thread_id) {
433-
main_browser_thread_id_ = main_browser_thread_id;
434-
}
429+
static struct pthread __main_pthread;
435430

436431
pthread_t emscripten_main_browser_thread_id() {
437-
return main_browser_thread_id_;
432+
return &__main_pthread;
438433
}
439434

440435
int _emscripten_do_dispatch_to_thread(pthread_t target_thread, em_queued_call* call) {
@@ -970,8 +965,8 @@ void __do_cleanup_pop(struct __ptcb *cb) {
970965
__pthread_self()->cancelbuf = cb->__next;
971966
}
972967

973-
EM_JS(void, initPthreadsJS, (void), {
974-
PThread.initRuntime();
968+
EM_JS(void, initPthreadsJS, (void* tb), {
969+
PThread.initRuntime(tb);
975970
})
976971

977972
// We must initialize the runtime at the proper time, which is after memory is
@@ -982,8 +977,17 @@ EM_JS(void, initPthreadsJS, (void), {
982977
EMSCRIPTEN_KEEPALIVE
983978
__attribute__((constructor(48)))
984979
void __emscripten_pthread_data_constructor(void) {
985-
initPthreadsJS();
986-
pthread_self()->locale = &libc.global_locale;
980+
initPthreadsJS(&__main_pthread);
981+
// The pthread struct has a field that points to itself - this is used as
982+
// a magic ID to detect whether the pthread_t structure is 'alive'.
983+
__main_pthread.self = &__main_pthread;
984+
// pthread struct robust_list head should point to itself.
985+
__main_pthread.robust_list.head = &__main_pthread.robust_list;
986+
987+
// Main thread ID.
988+
__main_pthread.tid = (long)&__main_pthread;
989+
990+
__main_pthread.locale = &libc.global_locale;
987991
}
988992

989993
extern int __pthread_create_js(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

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
@@ -30,7 +30,6 @@ $emscripten_current_thread_process_queued_calls
3030
$emscripten_get_global_libc
3131
$emscripten_main_thread_process_queued_calls
3232
$emscripten_proxy_main
33-
$emscripten_register_main_browser_thread_id
3433
$emscripten_run_in_main_runtime_thread_js
3534
$emscripten_stack_set_limits
3635
$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)