Skip to content

Commit 9f8f146

Browse files
committed
Apply review comments
1 parent d1ba2a9 commit 9f8f146

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

src/parseTools.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,9 @@ function makeStaticString(string) {
14771477
return '(stringToUTF8("' + string + '", ' + ptr + ', ' + len + '), ' + ptr + ')';
14781478
}
14791479

1480-
// Generates access to module exports variable in pthreads worker.js
1480+
// Generates access to module exports variable in pthreads worker.js. Depending on whether main code is built with MODULARIZE
1481+
// or not, asm module exports need to either be accessed via a local exports object obtained from instantiating the module (in src/worker.js), or via
1482+
// the global Module exports object.
14811483
function makeAsmExportAccessInPthread(variable) {
14821484
if (MODULARIZE) {
14831485
return "Module['" + variable + "']" // 'Module' is defined in worker.js local scope, so not EXPORT_NAME in this case.
@@ -1486,7 +1488,8 @@ function makeAsmExportAccessInPthread(variable) {
14861488
}
14871489
}
14881490

1489-
// Generates access to a global scope variable in pthreads worker.js
1491+
// Generates access to a JS global scope variable in pthreads worker.js. In MODULARIZE mode the JS scope is not directly accessible, so all the relevant variables
1492+
// are exported via Module. In non-MODULARIZE mode, we can directly access the variables in global scope.
14901493
function makeAsmGlobalAccessInPthread(variable) {
14911494
if (MODULARIZE) {
14921495
return "Module['" + variable + "']" // 'Module' is defined in worker.js local scope, so not EXPORT_NAME in this case.
@@ -1496,8 +1499,8 @@ function makeAsmGlobalAccessInPthread(variable) {
14961499
}
14971500

14981501
// Generates access to both global scope variable and exported Module variable, e.g. "Module['foo'] = foo" or just plain "foo" depending on if we are MODULARIZEing.
1499-
// Used the be able to initialize both variables at the same time.
1500-
function makeAsmExportAndGlobalAccessInPthread(variable) {
1502+
// Used the be able to initialize both variables at the same time in scenarios where a variable exists in both global scope and in Module.
1503+
function makeAsmExportAndGlobalAssignTargetInPthread(variable) {
15011504
if (MODULARIZE) {
15021505
return "Module['" + variable + "'] = " + variable // 'Module' is defined in worker.js local scope, so not EXPORT_NAME in this case.
15031506
} else {

src/shell.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,14 @@ if (Module['ENVIRONMENT']) {
7676
}
7777
#endif
7878

79-
// Three configurations we can be running in:
80-
// 1) We could be the application main() thread running in the main JS UI thread. (ENVIRONMENT_IS_WORKER == false and ENVIRONMENT_IS_PTHREAD == false)
81-
// 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false)
82-
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
83-
#if USE_PTHREADS
84-
85-
if (typeof ENVIRONMENT_IS_PTHREAD === 'undefined') {
86-
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
87-
// N.B. this line needs to appear without 'var' keyword to avoid 'var hoisting' from occurring. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
88-
ENVIRONMENT_IS_PTHREAD = false;
89-
var PthreadWorkerInit = {}; // Collects together variables that are needed at initialization time for the web workers that host pthreads.
90-
}
91-
#if MODULARIZE
92-
else {
93-
// Grab imports from the pthread to local scope.
94-
var buffer = {{{EXPORT_NAME}}}.buffer;
95-
var tempDoublePtr = {{{EXPORT_NAME}}}.tempDoublePtr;
96-
var TOTAL_MEMORY = {{{EXPORT_NAME}}}.TOTAL_MEMORY;
97-
var STATICTOP = {{{EXPORT_NAME}}}.STATICTOP;
98-
var DYNAMIC_BASE = {{{EXPORT_NAME}}}.DYNAMIC_BASE;
99-
var DYNAMICTOP_PTR = {{{EXPORT_NAME}}}.DYNAMICTOP_PTR;
100-
var PthreadWorkerInit = {{{EXPORT_NAME}}}.PthreadWorkerInit;
101-
// Note that not all runtime fields are imported above. Values for STACK_BASE, STACKTOP and STACK_MAX are not yet known at worker.js load time.
102-
// These will be filled in at pthread startup time (the 'run' message for a pthread - pthread start establishes the stack frame)
103-
}
104-
#endif
79+
#include "shell_pthreads.js"
10580

106-
#if !MODULARIZE
81+
#if USE_PTHREADS && !MODULARIZE
10782
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
10883
// before the page load. In non-MODULARIZE modes generate it here.
10984
var _scriptDir = (typeof document !== 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
11085
#endif
11186

112-
#endif // USE_PTHREADS
113-
11487
// `/` should be present at the end if `scriptDirectory` is not empty
11588
var scriptDirectory = '';
11689
function locateFile(path) {

src/shell_pthreads.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Three configurations we can be running in:
2+
// 1) We could be the application main() thread running in the main JS UI thread. (ENVIRONMENT_IS_WORKER == false and ENVIRONMENT_IS_PTHREAD == false)
3+
// 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false)
4+
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
5+
#if USE_PTHREADS
6+
7+
if (typeof ENVIRONMENT_IS_PTHREAD === 'undefined') {
8+
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
9+
// N.B. this line needs to appear without 'var' keyword to avoid 'var hoisting' from occurring. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
10+
ENVIRONMENT_IS_PTHREAD = false;
11+
var PthreadWorkerInit = {}; // Collects together variables that are needed at initialization time for the web workers that host pthreads.
12+
}
13+
#if MODULARIZE
14+
else {
15+
// Grab imports from the pthread to local scope.
16+
var buffer = {{{EXPORT_NAME}}}.buffer;
17+
var tempDoublePtr = {{{EXPORT_NAME}}}.tempDoublePtr;
18+
var TOTAL_MEMORY = {{{EXPORT_NAME}}}.TOTAL_MEMORY;
19+
var STATICTOP = {{{EXPORT_NAME}}}.STATICTOP;
20+
var DYNAMIC_BASE = {{{EXPORT_NAME}}}.DYNAMIC_BASE;
21+
var DYNAMICTOP_PTR = {{{EXPORT_NAME}}}.DYNAMICTOP_PTR;
22+
var PthreadWorkerInit = {{{EXPORT_NAME}}}.PthreadWorkerInit;
23+
// Note that not all runtime fields are imported above. Values for STACK_BASE, STACKTOP and STACK_MAX are not yet known at worker.js load time.
24+
// These will be filled in at pthread startup time (the 'run' message for a pthread - pthread start establishes the stack frame)
25+
}
26+
#endif
27+
28+
#endif

src/worker.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ this.onmessage = function(e) {
9292
{{{ makeAsmGlobalAccessInPthread('tempDoublePtr') }}} = e.data.tempDoublePtr;
9393

9494
// Initialize the global "process"-wide fields:
95-
{{{ makeAsmExportAndGlobalAccessInPthread('TOTAL_MEMORY') }}} = e.data.TOTAL_MEMORY;
96-
{{{ makeAsmExportAndGlobalAccessInPthread('DYNAMIC_BASE') }}} = e.data.DYNAMIC_BASE;
97-
{{{ makeAsmExportAndGlobalAccessInPthread('DYNAMICTOP_PTR') }}} = e.data.DYNAMICTOP_PTR;
95+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('TOTAL_MEMORY') }}} = e.data.TOTAL_MEMORY;
96+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('DYNAMIC_BASE') }}} = e.data.DYNAMIC_BASE;
97+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('DYNAMICTOP_PTR') }}} = e.data.DYNAMICTOP_PTR;
9898

9999
#if WASM
100100
// The Wasm module will have import fields for STACKTOP and STACK_MAX. At 'load' stage of Worker startup, we are just
@@ -109,11 +109,11 @@ this.onmessage = function(e) {
109109
{{{ makeAsmExportAccessInPthread('STACK_MAX') }}} = {{{ makeAsmExportAccessInPthread('STACKTOP') }}} = 0x7FFFFFFF;
110110

111111
// Module and memory were sent from main thread
112-
{{{ makeAsmExportAndGlobalAccessInPthread('wasmModule') }}} = e.data.wasmModule;
113-
{{{ makeAsmExportAndGlobalAccessInPthread('wasmMemory') }}} = e.data.wasmMemory;
114-
{{{ makeAsmExportAndGlobalAccessInPthread('buffer') }}} = {{{ makeAsmGlobalAccessInPthread('wasmMemory') }}}.buffer;
112+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('wasmModule') }}} = e.data.wasmModule;
113+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('wasmMemory') }}} = e.data.wasmMemory;
114+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('buffer') }}} = {{{ makeAsmGlobalAccessInPthread('wasmMemory') }}}.buffer;
115115
#else
116-
{{{ makeAsmExportAndGlobalAccessInPthread('buffer') }}} = e.data.buffer;
116+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('buffer') }}} = e.data.buffer;
117117

118118
#if SEPARATE_ASM
119119
// load the separated-out asm.js
@@ -129,7 +129,7 @@ this.onmessage = function(e) {
129129

130130
#endif
131131

132-
{{{ makeAsmExportAndGlobalAccessInPthread('PthreadWorkerInit') }}} = e.data.PthreadWorkerInit;
132+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('PthreadWorkerInit') }}} = e.data.PthreadWorkerInit;
133133

134134
if (typeof e.data.urlOrBlob === 'string') {
135135
importScripts(e.data.urlOrBlob);
@@ -158,8 +158,8 @@ this.onmessage = function(e) {
158158
selfThreadId = e.data.selfThreadId;
159159
parentThreadId = e.data.parentThreadId;
160160
// Establish the stack frame for this thread in global scope
161-
{{{ makeAsmExportAndGlobalAccessInPthread('STACK_BASE') }}} = {{{ makeAsmExportAndGlobalAccessInPthread('STACKTOP') }}} = e.data.stackBase;
162-
{{{ makeAsmExportAndGlobalAccessInPthread('STACK_MAX') }}} = STACK_BASE + e.data.stackSize;
161+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('STACK_BASE') }}} = {{{ makeAsmExportAndGlobalAssignTargetInPthread('STACKTOP') }}} = e.data.stackBase;
162+
{{{ makeAsmExportAndGlobalAssignTargetInPthread('STACK_MAX') }}} = STACK_BASE + e.data.stackSize;
163163
#if ASSERTIONS
164164
assert(threadInfoStruct);
165165
assert(selfThreadId);

0 commit comments

Comments
 (0)