Skip to content

Commit d24f3ca

Browse files
authored
Mark USE_PTHREADS as legacy. NFC (#18923)
Create a new internal setting called just `PTHREADS` and use that everywhere internally. This allows the old setting to be marked as legacy, but still supported in case existing use JS library code uses it.
1 parent ac27217 commit d24f3ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+213
-204
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.33 (in development)
2222
-----------------------
23+
- The prefered way to enable pthread is now to just the the standard `-pthread`
24+
flag. The `-sUSE_PTHREADS` setting still works but is marked as legacy and
25+
will generate a warning in `-sSTRICT` mode.
2326
- Initial support for C++20 modules. We have added a very simple test in form
2427
of `other.test_cpp_module`. (#)
2528
- Removed `sys/sysctl.h` compatibility header. We don't implement the function

emcc.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def setup_environment_settings():
380380
settings.ENVIRONMENT_MAY_BE_WORKER = \
381381
not settings.ENVIRONMENT or \
382382
'worker' in environments or \
383-
(settings.ENVIRONMENT_MAY_BE_NODE and settings.USE_PTHREADS)
383+
(settings.ENVIRONMENT_MAY_BE_NODE and settings.PTHREADS)
384384

385385
if not settings.ENVIRONMENT_MAY_BE_WORKER and settings.PROXY_TO_WORKER:
386386
exit_with_error('If you specify --proxy-to-worker and specify a "-sENVIRONMENT=" directive, it must include "worker" as a target! (Try e.g. -sENVIRONMENT=web,worker)')
@@ -1595,11 +1595,14 @@ def phase_setup(options, state, newargs):
15951595
if settings.MAIN_MODULE or settings.SIDE_MODULE:
15961596
settings.RELOCATABLE = 1
15971597

1598+
if 'USE_PTHREADS' in user_settings:
1599+
settings.PTHREADS = settings.USE_PTHREADS
1600+
15981601
# Pthreads and Wasm Workers require targeting shared Wasm memory (SAB).
1599-
if settings.USE_PTHREADS or settings.WASM_WORKERS:
1602+
if settings.PTHREADS or settings.WASM_WORKERS:
16001603
settings.SHARED_MEMORY = 1
16011604

1602-
if settings.USE_PTHREADS and '-pthread' not in newargs:
1605+
if settings.PTHREADS and '-pthread' not in newargs:
16031606
newargs += ['-pthread']
16041607
elif settings.SHARED_MEMORY:
16051608
if '-matomics' not in newargs:
@@ -1751,7 +1754,7 @@ def include_and_export(name):
17511754
if not settings.EXPORT_ES6 and settings.EXPORT_NAME == 'Module':
17521755
exit_with_error('pthreads + MODULARIZE currently require you to set -sEXPORT_NAME=Something (see settings.js) to Something != Module, so that the .worker.js file can work')
17531756

1754-
# MODULARIZE+USE_PTHREADS mode requires extra exports out to Module so that worker.js
1757+
# MODULARIZE+PTHREADS mode requires extra exports out to Module so that worker.js
17551758
# can access them:
17561759

17571760
# general threading variables:
@@ -2099,7 +2102,7 @@ def phase_linker_setup(options, state, newargs):
20992102
'$mergeLibSymbols',
21002103
]
21012104

2102-
if settings.USE_PTHREADS:
2105+
if settings.PTHREADS:
21032106
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += [
21042107
'$registerTLSInit',
21052108
]
@@ -2210,7 +2213,7 @@ def phase_linker_setup(options, state, newargs):
22102213
# HTML output creates a singleton instance, and it does so without the
22112214
# Promise. However, in Pthreads mode the Promise is used for worker
22122215
# creation.
2213-
if settings.MINIMAL_RUNTIME and options.oformat == OFormat.HTML and not settings.USE_PTHREADS:
2216+
if settings.MINIMAL_RUNTIME and options.oformat == OFormat.HTML and not settings.PTHREADS:
22142217
settings.EXPORT_READY_PROMISE = 0
22152218

22162219
if settings.LEGACY_VM_SUPPORT:
@@ -2327,7 +2330,7 @@ def phase_linker_setup(options, state, newargs):
23272330
if settings.FETCH and final_suffix in EXECUTABLE_ENDINGS:
23282331
state.forced_stdlibs.append('libfetch')
23292332
settings.JS_LIBRARIES.append((0, 'library_fetch.js'))
2330-
if settings.USE_PTHREADS:
2333+
if settings.PTHREADS:
23312334
settings.FETCH_WORKER_FILE = unsuffixed_basename(target) + '.fetch.js'
23322335

23332336
if settings.DEMANGLE_SUPPORT:
@@ -2397,7 +2400,7 @@ def phase_linker_setup(options, state, newargs):
23972400
# overrides that.
23982401
default_setting('ABORTING_MALLOC', 0)
23992402

2400-
if settings.USE_PTHREADS:
2403+
if settings.PTHREADS:
24012404
setup_pthreads(target)
24022405
settings.JS_LIBRARIES.append((0, 'library_pthread.js'))
24032406
if settings.PROXY_TO_PTHREAD:
@@ -2629,7 +2632,7 @@ def check_memory_setting(setting):
26292632
# are based on experimentation with different tests/programs under asan and
26302633
# lsan.
26312634
settings.INITIAL_MEMORY += 50 * 1024 * 1024
2632-
if settings.USE_PTHREADS:
2635+
if settings.PTHREADS:
26332636
settings.INITIAL_MEMORY += 50 * 1024 * 1024
26342637

26352638
if settings.USE_OFFSET_CONVERTER and settings.WASM2JS:
@@ -3175,7 +3178,7 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
31753178
# write_file(final_js, src)
31763179

31773180
target_dir = os.path.dirname(os.path.abspath(target))
3178-
if settings.USE_PTHREADS:
3181+
if settings.PTHREADS:
31793182
worker_output = os.path.join(target_dir, settings.PTHREAD_WORKER_FILE)
31803183
contents = shared.read_and_preprocess(utils.path_from_root('src/worker.js'), expand_macros=True)
31813184
write_file(worker_output, contents)
@@ -3576,9 +3579,11 @@ def consume_arg_file():
35763579
options.output_eol = '\n'
35773580
else:
35783581
exit_with_error(f'Invalid value "{style}" to --output_eol!')
3579-
# Record USE_PTHREADS setting because it controls whether --shared-memory is passed to lld
3582+
# Record PTHREADS setting because it controls whether --shared-memory is passed to lld
35803583
elif arg == '-pthread':
3581-
settings_changes.append('USE_PTHREADS=1')
3584+
settings.PTHREADS = 1
3585+
# Also set the legacy setting name, in case use JS code depends on it.
3586+
settings.USE_PTHREADS = 1
35823587
elif arg == '-pthreads':
35833588
exit_with_error('unrecognized command-line option ‘-pthreads’; did you mean ‘-pthread’?')
35843589
elif arg in ('-fno-diagnostics-color', '-fdiagnostics-color=never'):
@@ -3706,7 +3711,7 @@ def phase_binaryen(target, options, wasm_target):
37063711
# adds some >>> 0 things, while growth will replace a HEAP8 with a call to
37073712
# a method to get the heap, and that call would not be recognized by the
37083713
# unsigning pass
3709-
if settings.USE_PTHREADS and settings.ALLOW_MEMORY_GROWTH:
3714+
if settings.PTHREADS and settings.ALLOW_MEMORY_GROWTH:
37103715
with ToolchainProfiler.profile_block('apply_wasm_memory_growth'):
37113716
final_js = building.apply_wasm_memory_growth(final_js)
37123717

@@ -3880,15 +3885,15 @@ def modularize():
38803885
'capture_module_function_for_audio_worklet': 'globalThis.AudioWorkletModule = Module;' if settings.AUDIO_WORKLET and settings.MODULARIZE else ''
38813886
}
38823887

3883-
if settings.MINIMAL_RUNTIME and not settings.USE_PTHREADS:
3888+
if settings.MINIMAL_RUNTIME and not settings.PTHREADS:
38843889
# Single threaded MINIMAL_RUNTIME programs do not need access to
38853890
# document.currentScript, so a simple export declaration is enough.
38863891
src = 'var %s=%s' % (settings.EXPORT_NAME, src)
38873892
else:
38883893
script_url_node = ''
38893894
# When MODULARIZE this JS may be executed later,
38903895
# after document.currentScript is gone, so we save it.
3891-
# In EXPORT_ES6 + USE_PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
3896+
# In EXPORT_ES6 + PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
38923897
# so doesn't have access to 'document'. In this case use 'import.meta' instead.
38933898
if settings.EXPORT_ES6 and settings.USE_ES6_IMPORT_META:
38943899
script_url = 'import.meta.url'

emscripten.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def update_settings_glue(wasm_file, metadata):
144144
if settings.ASYNCIFY == 2:
145145
settings.BINARYEN_FEATURES += ['--enable-reference-types']
146146

147-
if settings.USE_PTHREADS:
147+
if settings.PTHREADS:
148148
assert '--enable-threads' in settings.BINARYEN_FEATURES
149149
if settings.MEMORY64:
150150
assert '--enable-memory64' in settings.BINARYEN_FEATURES
@@ -625,7 +625,7 @@ def add_standard_wasm_imports(send_items_map):
625625

626626
if settings.IMPORTED_MEMORY:
627627
memory_import = 'wasmMemory'
628-
if settings.MODULARIZE and settings.USE_PTHREADS:
628+
if settings.MODULARIZE and settings.PTHREADS:
629629
# Pthreads assign wasmMemory in their worker startup. In MODULARIZE mode, they cannot assign inside the
630630
# Module scope, so lookup via Module as well.
631631
memory_import += " || Module['wasmMemory']"

src/jsifier.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function ${name}(${args}) {
159159
throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified!`);
160160
}
161161
const sync = proxyingMode === 'sync';
162-
if (USE_PTHREADS) {
162+
if (PTHREADS) {
163163
snippet = modifyFunction(snippet, (name, args, body) => `
164164
function ${name}(${args}) {
165165
if (ENVIRONMENT_IS_PTHREAD)
@@ -489,7 +489,7 @@ function ${name}(${args}) {
489489
print(indentify(item.JS || '', 2));
490490
}
491491

492-
if (USE_PTHREADS) {
492+
if (PTHREADS) {
493493
print('\n // proxiedFunctionTable specifies the list of functions that can be called either synchronously or asynchronously from other threads in postMessage()d or internally queued events. This way a pthread in a Worker can synchronously access e.g. the DOM on the main thread.');
494494
print('\nvar proxiedFunctionTable = [' + proxiedFunctionTable.join() + '];\n');
495495
}

src/library.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mergeInto(LibraryManager.library, {
7070
checkUnflushedContent();
7171
#endif // ASSERTIONS && !EXIT_RUNTIME
7272

73-
#if USE_PTHREADS
73+
#if PTHREADS
7474
if (ENVIRONMENT_IS_PTHREAD) {
7575
// implict exit can never happen on a pthread
7676
#if ASSERTIONS
@@ -89,7 +89,7 @@ mergeInto(LibraryManager.library, {
8989
#if PTHREADS_DEBUG
9090
err('main thread called exit: keepRuntimeAlive=' + keepRuntimeAlive() + ' (counter=' + runtimeKeepaliveCounter + ')');
9191
#endif // PTHREADS_DEBUG
92-
#endif // USE_PTHREADS
92+
#endif // PTHREADS
9393

9494
#if EXIT_RUNTIME
9595
if (!keepRuntimeAlive()) {
@@ -2357,7 +2357,7 @@ mergeInto(LibraryManager.library, {
23572357
" };\n" +
23582358
"} else " +
23592359
#endif
2360-
#if USE_PTHREADS && !AUDIO_WORKLET
2360+
#if PTHREADS && !AUDIO_WORKLET
23612361
// Pthreads need their clocks synchronized to the execution of the main thread, so, when using them,
23622362
// make sure to adjust all timings to the respective time origins.
23632363
"_emscripten_get_now = () => performance.timeOrigin + performance.now();\n",
@@ -2371,7 +2371,7 @@ mergeInto(LibraryManager.library, {
23712371
// AudioWorkletGlobalScope does not have performance.now() (https://github.com/WebAudio/web-audio-api/issues/2527), so if building with
23722372
// Audio Worklets enabled, do a dynamic check for its presence.
23732373
"if (typeof performance != 'undefined' && performance.now) {\n" +
2374-
#if USE_PTHREADS
2374+
#if PTHREADS
23752375
" _emscripten_get_now = () => performance.timeOrigin + performance.now();\n" +
23762376
#else
23772377
" _emscripten_get_now = () => performance.now();\n" +
@@ -3044,7 +3044,7 @@ mergeInto(LibraryManager.library, {
30443044
$runMainThreadEmAsm__sig: 'iippi',
30453045
$runMainThreadEmAsm: function(code, sigPtr, argbuf, sync) {
30463046
var args = readEmAsmArgs(sigPtr, argbuf);
3047-
#if USE_PTHREADS
3047+
#if PTHREADS
30483048
if (ENVIRONMENT_IS_PTHREAD) {
30493049
// EM_ASM functions are variadic, receiving the actual arguments as a buffer
30503050
// in memory. the last parameter (argBuf) points to that data. We need to
@@ -3564,7 +3564,7 @@ mergeInto(LibraryManager.library, {
35643564
},
35653565

35663566
$maybeExit__deps: ['exit', '$handleException',
3567-
#if USE_PTHREADS
3567+
#if PTHREADS
35683568
'_emscripten_thread_exit',
35693569
#endif
35703570
],
@@ -3582,7 +3582,7 @@ mergeInto(LibraryManager.library, {
35823582
dbg('maybeExit: calling exit() implicitly after user callback completed: ' + EXITSTATUS);
35833583
#endif
35843584
try {
3585-
#if USE_PTHREADS
3585+
#if PTHREADS
35863586
if (ENVIRONMENT_IS_PTHREAD) __emscripten_thread_exit(EXITSTATUS);
35873587
else
35883588
#endif

src/library_browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ var LibraryBrowser = {
846846
onload = {{{ makeDynCall('v', 'onload') }}};
847847
onerror = {{{ makeDynCall('v', 'onerror') }}};
848848

849-
#if USE_PTHREADS
849+
#if PTHREADS
850850
if (ENVIRONMENT_IS_PTHREAD) {
851851
err('emscripten_async_load_script("' + UTF8ToString(url) + '") failed, emscripten_async_load_script is currently not available in pthreads!');
852852
return onerror ? onerror() : undefined;
@@ -1049,7 +1049,7 @@ var LibraryBrowser = {
10491049
GL.newRenderingFrameStarted();
10501050
#endif
10511051

1052-
#if USE_PTHREADS && OFFSCREEN_FRAMEBUFFER && GL_SUPPORT_EXPLICIT_SWAP_CONTROL
1052+
#if PTHREADS && OFFSCREEN_FRAMEBUFFER && GL_SUPPORT_EXPLICIT_SWAP_CONTROL
10531053
// If the current GL context is a proxied regular WebGL context, and was initialized with implicit swap mode on the main thread, and we are on the parent thread,
10541054
// perform the swap on behalf of the user.
10551055
if (typeof GL != 'undefined' && GL.currentContext && GL.currentContextIsProxied) {

src/library_dylink.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ var LibraryDylink = {
719719
}
720720
#if STACK_OVERFLOW_CHECK >= 2
721721
if (moduleExports['__set_stack_limits']) {
722-
#if USE_PTHREADS
722+
#if PTHREADS
723723
// When we are on an uninitialized pthread we delay calling
724724
// __set_stack_limits until $setDylinkStackLimits.
725725
if (!ENVIRONMENT_IS_PTHREAD || runtimeInitialized)
@@ -762,7 +762,7 @@ var LibraryDylink = {
762762
#endif
763763

764764
// initialize the module
765-
#if USE_PTHREADS
765+
#if PTHREADS
766766
// Only one thread should call __wasm_call_ctors, but all threads need
767767
// to call _emscripten_tls_init
768768
registerTLSInit(moduleExports['_emscripten_tls_init'], instance.exports, metadata)
@@ -788,7 +788,7 @@ var LibraryDylink = {
788788
__ATINIT__.push(init);
789789
}
790790
}
791-
#if USE_PTHREADS
791+
#if PTHREADS
792792
}
793793
#endif
794794
return moduleExports;
@@ -826,8 +826,8 @@ var LibraryDylink = {
826826
return loadModule();
827827
},
828828

829-
#if STACK_OVERFLOW_CHECK >= 2 && USE_PTHREADS
830-
// With USE_PTHREADS we load libraries before we are running a pthread and
829+
#if STACK_OVERFLOW_CHECK >= 2 && PTHREADS
830+
// With PTHREADS we load libraries before we are running a pthread and
831831
// therefore before we have a stack. Instead we delay calling
832832
// `__set_stack_limits` until we start running a thread. We also need to call
833833
// this again for each new thread that the runs on a worker (since each thread

src/library_fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include Fetch.js
88

99
var LibraryFetch = {
10-
#if USE_PTHREADS
10+
#if PTHREADS
1111
$Fetch__postset: 'if (!ENVIRONMENT_IS_PTHREAD) Fetch.staticInit();',
1212
#else
1313
$Fetch__postset: 'Fetch.staticInit();',

0 commit comments

Comments
 (0)