You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pthread-main.js was touching globals directly, which aren't
exposed as globals when the Module object is encapsulated
into a function constructor with MODULARIZE and EXPORT_NAME
options.
Modified pthread-main.js and library_pthread.js (and a little
modification to shell.js and jsifier.js) to use accessors for
a couple things, and to inject the initialized stuff for pthread
workers via options on the Module object instead of directly.
emcc.py is changed to support checking document.currentScript.src
at load time, outside the module wrapper constructor, and passes
the value in to the module via Module.currentScriptUrl; this is
overriding the document.currentScript.src check in shell.js.
Shouldn't break existing non-modular code, but does require that
pthread-main.js be updated when recompiling (as should happen
normally).
Used in ogv.js 1.4.x for experimental multithread builds.
Fixes#5009
Copy file name to clipboardExpand all lines: src/preamble.js
+11-2Lines changed: 11 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -986,7 +986,12 @@ var STACK_BASE, STACKTOP, STACK_MAX; // stack area
986
986
varDYNAMIC_BASE,DYNAMICTOP_PTR;// dynamic area handled by sbrk
987
987
988
988
#if USE_PTHREADS
989
-
if(!ENVIRONMENT_IS_PTHREAD){// Pthreads have already initialized these variables in src/pthread-main.js, where they were passed to the thread worker at startup time
989
+
if(ENVIRONMENT_IS_PTHREAD){// Pthreads have passed these variables in from src/pthread-main.js, where they were passed to the thread worker at startup time
Copy file name to clipboardExpand all lines: src/pthread-main.js
+37-50Lines changed: 37 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -2,31 +2,18 @@
2
2
// This is the entry point file that is loaded first by each Web Worker
3
3
// that executes pthreads on the Emscripten application.
4
4
5
-
// Thread-local:
5
+
// Thread-local, communicated via globals:
6
6
varthreadInfoStruct=0;// Info area for this thread in Emscripten HEAP (shared). If zero, this worker is not currently hosting an executing pthread.
7
7
varselfThreadId=0;// The ID of this thread. 0 if not hosting a pthread.
8
8
varparentThreadId=0;// The ID of the parent pthread that launched this thread.
9
-
vartempDoublePtr=0;// A temporary memory area for global float and double marshalling operations.
10
9
11
-
// Thread-local: Each thread has its own allocated stack space.
12
-
varSTACK_BASE=0;
13
-
varSTACKTOP=0;
14
-
varSTACK_MAX=0;
15
-
16
-
// These are system-wide memory area parameters that are set at main runtime startup in main thread, and stay constant throughout the application.
17
-
varbuffer;// All pthreads share the same Emscripten HEAP as SharedArrayBuffer with the main execution thread.
18
-
varDYNAMICTOP_PTR=0;
19
-
varTOTAL_MEMORY=0;
20
-
varSTATICTOP=0;
21
-
varstaticSealed=true;// When threads are being initialized, the static memory area has been already sealed a long time ago.
22
-
varDYNAMIC_BASE=0;
23
-
24
-
varENVIRONMENT_IS_PTHREAD=true;
10
+
// Send the pthreads mode and other params in through Module object settings
11
+
varModule={
12
+
ENVIRONMENT: 'PTHREAD'
13
+
};
25
14
26
15
// Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091
27
16
// Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print.
}elseif(e.data.cmd==='run'){// This worker was idle, and now should start executing its pthread entry point.
65
64
threadInfoStruct=e.data.threadInfoStruct;
66
-
__register_pthread_ptr(threadInfoStruct,/*isMainBrowserThread=*/0,/*isMainRuntimeThread=*/0);// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
65
+
Module.PThread.registerPthreadPtr(threadInfoStruct,/*isMainBrowserThread=*/0,/*isMainRuntimeThread=*/0);// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
67
66
assert(threadInfoStruct);
68
67
selfThreadId=e.data.selfThreadId;
69
68
parentThreadId=e.data.parentThreadId;
70
69
assert(selfThreadId);
71
70
assert(parentThreadId);
72
-
// TODO: Emscripten runtime has these variables twice(!), once outside the asm.js module, and a second time inside the asm.js module.
Atomics.store(HEAPU32,(threadInfoStruct+4/*{{{ C_STRUCTS.pthread.threadExitCode }}}*/)>>2,-2/*A custom entry specific to Emscripten denoting that the thread crashed.*/);
99
-
Atomics.store(HEAPU32,(threadInfoStruct+0/*{{{ C_STRUCTS.pthread.threadStatus }}}*/)>>2,1);// Mark the thread as no longer running.
100
-
_emscripten_futex_wake(threadInfoStruct+0/*{{{ C_STRUCTS.pthread.threadStatus }}}*/,0x7FFFFFFF/*INT_MAX*/);// wake all threads
85
+
Atomics.store(Module.HEAPU32,(threadInfoStruct+4/*{{{ C_STRUCTS.pthread.threadExitCode }}}*/)>>2,-2/*A custom entry specific to Emscripten denoting that the thread crashed.*/);
86
+
Atomics.store(Module.HEAPU32,(threadInfoStruct+0/*{{{ C_STRUCTS.pthread.threadStatus }}}*/)>>2,1);// Mark the thread as no longer running.
87
+
Module.PThread.wakeAllThreads();
101
88
throwe;
102
89
}
103
90
}
104
91
// The thread might have finished without calling pthread_exit(). If so, then perform the exit operation ourselves.
105
92
// (This is a no-op if explicit pthread_exit() had been called prior.)
Copy file name to clipboardExpand all lines: src/shell.js
+5-2Lines changed: 5 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,9 @@ if (Module['ENVIRONMENT']) {
53
53
ENVIRONMENT_IS_NODE=true;
54
54
}elseif(Module['ENVIRONMENT']==='SHELL'){
55
55
ENVIRONMENT_IS_SHELL=true;
56
+
}elseif(Module['ENVIRONMENT']==='PTHREAD'){
57
+
ENVIRONMENT_IS_WORKER=true;
58
+
ENVIRONMENT_IS_PTHREAD=true;
56
59
}else{
57
60
thrownewError('The provided Module[\'ENVIRONMENT\'] value is not valid. It must be one of: WEB|WORKER|NODE|SHELL.');
58
61
}
@@ -66,9 +69,9 @@ if (Module['ENVIRONMENT']) {
66
69
#if USE_PTHREADS
67
70
varENVIRONMENT_IS_PTHREAD;
68
71
if(!ENVIRONMENT_IS_PTHREAD)ENVIRONMENT_IS_PTHREAD=false;// ENVIRONMENT_IS_PTHREAD=true will have been preset in pthread-main.js. Make it false in the main runtime thread.
69
-
varPthreadWorkerInit;// Collects together variables that are needed at initialization time for the web workers that host pthreads.
72
+
varPthreadWorkerInit=Module['pthreadWorkerInit']||undefined;// Collects together variables that are needed at initialization time for the web workers that host pthreads.
0 commit comments