Skip to content

Commit 845ad34

Browse files
authored
Re-enable extern-pre-js and extern-post-js under pthreads (#22013)
In #21701 the running of extern-pre/post-js in pthreads was disabled. The reason was that we had a couple of tests that seemed to be assuming this. However, it turns out that there are important use cases that depend on this code running in threads. Instead, fix the two test cases by adding an extra condition. Fixes: #22012
1 parent cb99414 commit 845ad34

File tree

3 files changed

+16
-42
lines changed

3 files changed

+16
-42
lines changed

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9268,7 +9268,7 @@ def test_pthread_offset_converter_modularize(self):
92689268
self.set_setting('USE_OFFSET_CONVERTER')
92699269
self.set_setting('MODULARIZE')
92709270
self.set_setting('EXPORT_NAME', 'foo')
9271-
create_file('post.js', 'foo();')
9271+
create_file('post.js', 'if (!isPthread) foo();')
92729272
self.emcc_args += ['--extern-post-js', 'post.js']
92739273
if '-g' in self.emcc_args:
92749274
self.emcc_args += ['-DDEBUG']

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def test_emcc_output_mjs(self, args):
366366
})
367367
@node_pthreads
368368
def test_emcc_output_worker_mjs(self, args):
369-
create_file('extern-post.js', 'await Module();')
369+
create_file('extern-post.js', 'if (!isPthread) await Module();')
370370
os.mkdir('subdir')
371371
self.run_process([EMCC, '-o', 'subdir/hello_world.mjs',
372372
'-sEXIT_RUNTIME', '-sPROXY_TO_PTHREAD', '-pthread', '-O1',

tools/link.py

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,6 @@ def phase_linker_setup(options, state, newargs):
677677
options.extern_pre_js = read_js_files(options.extern_pre_js)
678678
options.extern_post_js = read_js_files(options.extern_post_js)
679679

680-
if settings.PTHREADS:
681-
# Don't run extern pre/post code on pthreads.
682-
if options.extern_pre_js:
683-
options.extern_pre_js = 'if (!isPthread) {' + options.extern_pre_js + '}'
684-
if options.extern_post_js:
685-
options.extern_post_js = 'if (!isPthread) {' + options.extern_post_js + '}'
686-
687680
# TODO: support source maps with js_transform
688681
if options.js_transform and settings.GENERATE_SOURCE_MAP:
689682
logger.warning('disabling source maps because a js transform is being done')
@@ -2082,7 +2075,7 @@ def phase_final_emitting(options, state, target, wasm_target):
20822075
create_worker_file('src/audio_worklet.js', target_dir, settings.AUDIO_WORKLET_FILE, options)
20832076

20842077
if settings.MODULARIZE:
2085-
modularize(options)
2078+
modularize()
20862079
elif settings.USE_CLOSURE_COMPILER:
20872080
module_export_name_substitution()
20882081

@@ -2109,8 +2102,6 @@ def phase_final_emitting(options, state, target, wasm_target):
21092102
src = read_file(final_js)
21102103
final_js += '.epp.js'
21112104
with open(final_js, 'w', encoding='utf-8') as f:
2112-
if settings.PTHREADS and (options.extern_pre_js or options.extern_post_js):
2113-
f.write(make_pthread_detection())
21142105
f.write(options.extern_pre_js)
21152106
f.write(src)
21162107
f.write(options.extern_post_js)
@@ -2352,34 +2343,7 @@ def node_pthread_detection():
23522343
return "require('worker_threads').workerData === 'em-pthread'\n"
23532344

23542345

2355-
def make_pthread_detection():
2356-
"""Create code for detecting if we are running in a pthread.
2357-
2358-
Normally this detection is done when the module is itself is run but there
2359-
are some cases were we need to do this detection outside of the normal
2360-
module code.
2361-
2362-
1. When running in MODULARIZE mode we need use this to know if we should
2363-
run the module constructor on startup (true only for pthreads)
2364-
2. When using `--extern-pre-js` and `--extern-post-js` we need to avoid
2365-
running this code on pthreads.
2366-
"""
2367-
2368-
if settings.ENVIRONMENT_MAY_BE_WEB or settings.ENVIRONMENT_MAY_BE_WORKER:
2369-
code = "var isPthread = globalThis.self?.name === 'em-pthread';\n"
2370-
# In order to support both web and node we also need to detect node here.
2371-
if settings.ENVIRONMENT_MAY_BE_NODE:
2372-
code += "var isNode = typeof globalThis.process?.versions?.node == 'string';\n"
2373-
code += f'if (isNode) isPthread = {node_pthread_detection()}\n'
2374-
elif settings.ENVIRONMENT_MAY_BE_NODE:
2375-
code = f'var isPthread = {node_pthread_detection()}\n'
2376-
else:
2377-
assert False
2378-
2379-
return code
2380-
2381-
2382-
def modularize(options):
2346+
def modularize():
23832347
global final_js
23842348
logger.debug(f'Modularizing, assigning to var {settings.EXPORT_NAME}')
23852349
src = read_file(final_js)
@@ -2461,8 +2425,18 @@ def modularize(options):
24612425
''' % {'EXPORT_NAME': settings.EXPORT_NAME}
24622426

24632427
if settings.PTHREADS:
2464-
if not options.extern_pre_js and not options.extern_post_js:
2465-
src += make_pthread_detection()
2428+
# Create code for detecting if we are running in a pthread.
2429+
# Normally this detection is done when the module is itself run but
2430+
# when running in MODULARIZE mode we need use this to know if we should
2431+
# run the module constructor on startup (true only for pthreads).
2432+
if settings.ENVIRONMENT_MAY_BE_WEB or settings.ENVIRONMENT_MAY_BE_WORKER:
2433+
src += "var isPthread = globalThis.self?.name === 'em-pthread';\n"
2434+
# In order to support both web and node we also need to detect node here.
2435+
if settings.ENVIRONMENT_MAY_BE_NODE:
2436+
src += "var isNode = typeof globalThis.process?.versions?.node == 'string';\n"
2437+
src += f'if (isNode) isPthread = {node_pthread_detection()}\n'
2438+
elif settings.ENVIRONMENT_MAY_BE_NODE:
2439+
src += f'var isPthread = {node_pthread_detection()}\n'
24662440
src += '// When running as a pthread, construct a new instance on startup\n'
24672441
src += 'isPthread && %s();\n' % settings.EXPORT_NAME
24682442

0 commit comments

Comments
 (0)