Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ var LibraryPThread = {
unusedWorkers: [],
// Contains all Workers that are currently hosting an active pthread.
runningWorkers: [],
// Points to a pthread_t structure in the Emscripten main heap, allocated on
// demand if/when first needed.
// mainThreadBlock: undefined,
initMainThreadBlock: function() {
#if ASSERTIONS
assert(!ENVIRONMENT_IS_PTHREAD);
Expand All @@ -47,41 +44,41 @@ var LibraryPThread = {
withBuiltinMalloc(function () {
#endif

PThread.mainThreadBlock = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
var tb = _malloc({{{ C_STRUCTS.pthread.__size__ }}});

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

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

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

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

PThread.initShared();

#if PTHREADS_PROFILING
PThread.createProfilerBlock(PThread.mainThreadBlock);
PThread.setThreadName(PThread.mainThreadBlock, "Browser main thread");
PThread.setThreadStatus(PThread.mainThreadBlock, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
#endif

#if USE_ASAN || USE_LSAN
});
PThread.createProfilerBlock(tb);
PThread.setThreadName(tb, "Browser main thread");
PThread.setThreadStatus(tb, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
#endif

// Pass the thread address to the native code where they stored in wasm
// globals which act as a form of TLS. Global constructors trying
// to access this value will read the wrong value, but that is UB anyway.
__emscripten_thread_init(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
__emscripten_thread_init(tb, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
_emscripten_register_main_browser_thread_id(tb);

#if USE_ASAN || USE_LSAN
});
#endif
},
initWorker: function() {
PThread.initShared();
Expand Down Expand Up @@ -848,7 +845,7 @@ var LibraryPThread = {
#endif
},

_emscripten_do_pthread_join__deps: ['$cleanupThread', '_pthread_testcancel_js', 'emscripten_main_thread_process_queued_calls', 'emscripten_futex_wait', 'pthread_self',
_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',
#if ASSERTIONS || IN_TEST_HARNESS || !MINIMAL_RUNTIME || !ALLOW_BLOCKING_ON_MAIN_THREAD
'emscripten_check_blocking_allowed'
#endif
Expand All @@ -862,7 +859,7 @@ var LibraryPThread = {
err('PThread ' + thread + ' is attempting to join to itself!');
return ERRNO_CODES.EDEADLK;
}
else if (!ENVIRONMENT_IS_PTHREAD && PThread.mainThreadBlock == thread) {
else if (!ENVIRONMENT_IS_PTHREAD && _emscripten_main_browser_thread_id() == thread) {
err('Main thread ' + thread + ' is attempting to join to itself!');
return ERRNO_CODES.EDEADLK;
}
Expand Down
5 changes: 0 additions & 5 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ function initRuntime(asm) {
PThread.initWorker();
return;
}

// Pass the thread address inside the asm.js scope to store it for fast access
// that avoids the need for a FFI out.
__emscripten_thread_init(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
#endif

#if STACK_OVERFLOW_CHECK
Expand Down
6 changes: 3 additions & 3 deletions src/threadprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ var emscriptenThreadProfiler = {
updateUi: function updateUi() {
if (typeof PThread === 'undefined') return; // Likely running threadprofiler on a singlethreaded build, or not initialized yet, ignore updating.
var str = '';
var mainThread = PThread.mainThreadBlock;
var mainThread = _emscripten_main_browser_thread_id();

var threads = [PThread.mainThreadBlock];
var threads = [mainThread];
for(var t in PThread.pthreads) threads.push(PThread.pthreads[t].threadInfoStruct);

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