Skip to content

Commit aa2731d

Browse files
authored
Don't do memory operations in pthreads before the runtime is initialized. NFC (#12052)
Pthreads.initMainThreadBlock is called during "preRun" (before compiled code is ready) and Pthreads.initRuntime is called during "atInit" (when compiled code is ready, just before global ctors). This moves some code from the former into the latter. Specifically it moves all allocation and memory operations. This leaves initMainThreadBlock to only do pure JS operations for preloading the workers, and everything else to initRuntime.
1 parent f377d3e commit aa2731d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/library_pthread.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ var LibraryPThread = {
2323
runningWorkers: [],
2424
// Points to a pthread_t structure in the Emscripten main heap, allocated on demand if/when first needed.
2525
// mainThreadBlock: undefined,
26-
initRuntime: function() {
27-
// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
28-
// Global constructors trying to access this value will read the wrong value, but that is UB anyway.
29-
registerPthreadPtr(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
30-
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
31-
},
3226
initMainThreadBlock: function() {
3327
#if ASSERTIONS
3428
assert(!ENVIRONMENT_IS_PTHREAD);
@@ -41,7 +35,8 @@ var LibraryPThread = {
4135
PThread.allocateUnusedWorker();
4236
}
4337
#endif
44-
38+
},
39+
initRuntime: function() {
4540
PThread.mainThreadBlock = {{{ makeStaticAlloc(C_STRUCTS.pthread.__size__) }}};
4641

4742
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}}/4; ++i) HEAPU32[PThread.mainThreadBlock/4+i] = 0;
@@ -66,6 +61,11 @@ var LibraryPThread = {
6661
PThread.setThreadName(PThread.mainThreadBlock, "Browser main thread");
6762
PThread.setThreadStatus(PThread.mainThreadBlock, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
6863
#endif
64+
65+
// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
66+
// Global constructors trying to access this value will read the wrong value, but that is UB anyway.
67+
registerPthreadPtr(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
68+
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
6969
},
7070
initWorker: function() {
7171
#if USE_CLOSURE_COMPILER

0 commit comments

Comments
 (0)