Skip to content

Commit 45aa70c

Browse files
authored
Remove PThread.mainThreadBlock. NFC. (#12917)
The main thread pointer is already available via the `_emscripten_main_browser_thread_id` native function. There is no point in also storing it in JS. While making this change I noticed that postamble_minimal.js was also redundantly calling `__emscripten_thread_init` and `_emscripten_register_main_browser_thread_id`.
1 parent 8ac2436 commit 45aa70c

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/library_pthread.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ var LibraryPThread = {
2222
unusedWorkers: [],
2323
// Contains all Workers that are currently hosting an active pthread.
2424
runningWorkers: [],
25-
// Points to a pthread_t structure in the Emscripten main heap, allocated on
26-
// demand if/when first needed.
27-
// mainThreadBlock: undefined,
2825
initMainThreadBlock: function() {
2926
#if ASSERTIONS
3027
assert(!ENVIRONMENT_IS_PTHREAD);
@@ -47,41 +44,41 @@ var LibraryPThread = {
4744
withBuiltinMalloc(function () {
4845
#endif
4946

50-
PThread.mainThreadBlock = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
47+
var tb = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
5148

52-
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}}/4; ++i) HEAPU32[PThread.mainThreadBlock/4+i] = 0;
49+
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}}/4; ++i) HEAPU32[tb/4+i] = 0;
5350

5451
// The pthread struct has a field that points to itself - this is used as
5552
// a magic ID to detect whether the pthread_t structure is 'alive'.
56-
{{{ makeSetValue('PThread.mainThreadBlock', C_STRUCTS.pthread.self, 'PThread.mainThreadBlock', 'i32') }}};
53+
{{{ makeSetValue('tb', C_STRUCTS.pthread.self, 'tb', 'i32') }}};
5754

5855
// pthread struct robust_list head should point to itself.
59-
var headPtr = PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.robust_list }}};
56+
var headPtr = tb + {{{ C_STRUCTS.pthread.robust_list }}};
6057
{{{ makeSetValue('headPtr', 0, 'headPtr', 'i32') }}};
6158

6259
// Allocate memory for thread-local storage.
6360
var tlsMemory = _malloc({{{ cDefine('PTHREAD_KEYS_MAX') * 4 }}});
6461
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) HEAPU32[tlsMemory/4+i] = 0;
65-
Atomics.store(HEAPU32, (PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.tsd }}} ) >> 2, tlsMemory); // Init thread-local-storage memory array.
66-
Atomics.store(HEAPU32, (PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.tid }}} ) >> 2, PThread.mainThreadBlock); // Main thread ID.
62+
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tsd }}} ) >> 2, tlsMemory); // Init thread-local-storage memory array.
63+
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tid }}} ) >> 2, tb); // Main thread ID.
6764

6865
PThread.initShared();
6966

7067
#if PTHREADS_PROFILING
71-
PThread.createProfilerBlock(PThread.mainThreadBlock);
72-
PThread.setThreadName(PThread.mainThreadBlock, "Browser main thread");
73-
PThread.setThreadStatus(PThread.mainThreadBlock, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
74-
#endif
75-
76-
#if USE_ASAN || USE_LSAN
77-
});
68+
PThread.createProfilerBlock(tb);
69+
PThread.setThreadName(tb, "Browser main thread");
70+
PThread.setThreadStatus(tb, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
7871
#endif
7972

8073
// Pass the thread address to the native code where they stored in wasm
8174
// globals which act as a form of TLS. Global constructors trying
8275
// to access this value will read the wrong value, but that is UB anyway.
83-
__emscripten_thread_init(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
84-
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
76+
__emscripten_thread_init(tb, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
77+
_emscripten_register_main_browser_thread_id(tb);
78+
79+
#if USE_ASAN || USE_LSAN
80+
});
81+
#endif
8582
},
8683
initWorker: function() {
8784
PThread.initShared();
@@ -848,7 +845,7 @@ var LibraryPThread = {
848845
#endif
849846
},
850847

851-
_emscripten_do_pthread_join__deps: ['$cleanupThread', '_pthread_testcancel_js', 'emscripten_main_thread_process_queued_calls', 'emscripten_futex_wait', 'pthread_self',
848+
_emscripten_do_pthread_join__deps: ['$cleanupThread', '_pthread_testcancel_js', 'emscripten_main_thread_process_queued_calls', 'emscripten_futex_wait', 'pthread_self', 'emscripten_main_browser_thread_id',
852849
#if ASSERTIONS || IN_TEST_HARNESS || !MINIMAL_RUNTIME || !ALLOW_BLOCKING_ON_MAIN_THREAD
853850
'emscripten_check_blocking_allowed'
854851
#endif
@@ -862,7 +859,7 @@ var LibraryPThread = {
862859
err('PThread ' + thread + ' is attempting to join to itself!');
863860
return ERRNO_CODES.EDEADLK;
864861
}
865-
else if (!ENVIRONMENT_IS_PTHREAD && PThread.mainThreadBlock == thread) {
862+
else if (!ENVIRONMENT_IS_PTHREAD && _emscripten_main_browser_thread_id() == thread) {
866863
err('Main thread ' + thread + ' is attempting to join to itself!');
867864
return ERRNO_CODES.EDEADLK;
868865
}

src/postamble_minimal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ function initRuntime(asm) {
6969
PThread.initWorker();
7070
return;
7171
}
72-
73-
// Pass the thread address inside the asm.js scope to store it for fast access
74-
// that avoids the need for a FFI out.
75-
__emscripten_thread_init(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
76-
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
7772
#endif
7873

7974
#if STACK_OVERFLOW_CHECK

src/threadprofiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ var emscriptenThreadProfiler = {
2626
updateUi: function updateUi() {
2727
if (typeof PThread === 'undefined') return; // Likely running threadprofiler on a singlethreaded build, or not initialized yet, ignore updating.
2828
var str = '';
29-
var mainThread = PThread.mainThreadBlock;
29+
var mainThread = _emscripten_main_browser_thread_id();
3030

31-
var threads = [PThread.mainThreadBlock];
31+
var threads = [mainThread];
3232
for(var t in PThread.pthreads) threads.push(PThread.pthreads[t].threadInfoStruct);
3333

3434
for(var i = 0; i < threads.length; ++i) {
35-
var threadPtr = threads[i];//(t == PThread.mainThreadBlock ? PThread.mainThreadBlock : maiPThread.pthreads[t].threadInfoStruct;
35+
var threadPtr = threads[i];//(t == mainThread ? mainThread : maiPThread.pthreads[t].threadInfoStruct;
3636
var profilerBlock = Atomics.load(HEAPU32, (threadPtr + 20 /*C_STRUCTS.pthread.profilerBlock*/ ) >> 2);
3737
var threadName = PThread.getThreadName(threadPtr);
3838
if (threadName) threadName = '"' + threadName + '" (0x' + threadPtr.toString(16) + ')';

0 commit comments

Comments
 (0)