Skip to content

Commit 5edae14

Browse files
authored
Don't run --extern-pre-js or --extern-post-js code on pthread workers (#21750)
This means, for example, that folks can call the module constructor as part of `--extern-post-js` without accidentally creating an extra module instance on each thread. For an example of this pattern see test_pthread_offset_converter_modularize. Although this is minor code size regression I'm about to followup with a major refactor that saves a lot more code: #21701
1 parent 4ecfddd commit 5edae14

File tree

8 files changed

+26
-6
lines changed

8 files changed

+26
-6
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ See docs/process.md for more on how version tagging works.
2121
3.1.58 (in development)
2222
-----------------------
2323
- Enable use of `::` to escape port option separator (#21710)
24+
- In multi-threaded builds `--extern-pre-js` and `--extern-post-js` code is
25+
now only run on the main thread, and not on each of the workers. (#21750)
2426

2527
3.1.57 - 04/10/24
2628
-----------------

src/runtime_init_memory.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ assert(INITIAL_MEMORY >= {{{STACK_SIZE}}}, 'INITIAL_MEMORY should be larger than
2020
#if PTHREADS
2121
if (ENVIRONMENT_IS_PTHREAD) {
2222
wasmMemory = Module['wasmMemory'];
23+
#if ASSERTIONS
24+
assert(wasmMemory, 'wasmMemory not set on incomming module object');
25+
#endif
2326
} else {
2427
#endif // PTHREADS
2528

src/shell.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ if (Module['ENVIRONMENT']) {
119119
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
120120

121121
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
122-
var ENVIRONMENT_IS_PTHREAD = Module['ENVIRONMENT_IS_PTHREAD'] || false;
122+
var ENVIRONMENT_IS_PTHREAD = globalThis['ENVIRONMENT_IS_PTHREAD'] || false;
123+
124+
#if MODULARIZE && ASSERTIONS
125+
if (ENVIRONMENT_IS_PTHREAD) {
126+
assert(!globalThis.moduleLoaded, 'module should only be loaded once on each pthread worker');
127+
globalThis.moduleLoaded = true;
128+
}
129+
#endif
123130
#endif
124131

125132
#if WASM_WORKERS

src/worker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ function handleMessage(e) {
192192
Module['workerID'] = e.data.workerID;
193193
#endif
194194

195-
#if !MINIMAL_RUNTIME || MODULARIZE
196-
{{{ makeAsmImportsAccessInPthread('ENVIRONMENT_IS_PTHREAD') }}} = true;
195+
#if !MINIMAL_RUNTIME
196+
// Set ENVIRONMENT_IS_PTHREAD before importing the main script
197+
globalThis['ENVIRONMENT_IS_PTHREAD'] = true;
197198
#endif
198199

199200
#if MODULARIZE && EXPORT_ES6
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6098
1+
6107
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13448
1+
13461

tools/js_manipulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def add_files_pre_js(pre_js_list, files_pre_js):
4444
utils.write_file(pre, '''
4545
// All the pre-js content up to here must remain later on, we need to run
4646
// it.
47-
if (Module['ENVIRONMENT_IS_PTHREAD'] || Module['$ww']) Module['preRun'] = [];
47+
if (globalThis['ENVIRONMENT_IS_PTHREAD'] || Module['$ww']) Module['preRun'] = [];
4848
var necessaryPreJSTasks = Module['preRun'].slice();
4949
''')
5050
utils.write_file(post, '''

tools/link.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ def phase_linker_setup(options, state, newargs):
721721
options.extern_pre_js = read_js_files(options.extern_pre_js)
722722
options.extern_post_js = read_js_files(options.extern_post_js)
723723

724+
if settings.PTHREADS:
725+
# Don't run extern pre/post code on pthreads.
726+
if options.extern_pre_js:
727+
options.extern_pre_js = 'if (typeof ENVIRONMENT_IS_PTHREAD == "undefined") {' + options.extern_pre_js + '}'
728+
if options.extern_post_js:
729+
options.extern_post_js = 'if (typeof ENVIRONMENT_IS_PTHREAD == "undefined") {' + options.extern_post_js + '}'
730+
724731
# TODO: support source maps with js_transform
725732
if options.js_transform and settings.GENERATE_SOURCE_MAP:
726733
logger.warning('disabling source maps because a js transform is being done')

0 commit comments

Comments
 (0)