Skip to content

Commit e89ed30

Browse files
authored
Combine pthread setup code into separate function. NFC (#16522)
For some reason we had two separate `USE_PTHREAD` block within the massive `phase_linker_setup`.
1 parent 80fbb10 commit e89ed30

File tree

1 file changed

+88
-85
lines changed

1 file changed

+88
-85
lines changed

emcc.py

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,91 @@ def phase_setup(options, state, newargs, user_settings):
14791479
return (newargs, input_files)
14801480

14811481

1482+
def setup_pthreads(target):
1483+
if settings.RELOCATABLE:
1484+
# phtreads + dyanmic linking has certain limitations
1485+
if settings.SIDE_MODULE:
1486+
diagnostics.warning('experimental', '-s SIDE_MODULE + pthreads is experimental')
1487+
elif settings.MAIN_MODULE:
1488+
diagnostics.warning('experimental', '-s MAIN_MODULE + pthreads is experimental')
1489+
elif settings.LINKABLE:
1490+
diagnostics.warning('experimental', '-s LINKABLE + pthreads is experimental')
1491+
if settings.ALLOW_MEMORY_GROWTH:
1492+
diagnostics.warning('pthreads-mem-growth', 'USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271')
1493+
1494+
# Functions needs to be exported from the module since they are used in worker.js
1495+
settings.REQUIRED_EXPORTS += [
1496+
'emscripten_dispatch_to_thread_',
1497+
'_emscripten_thread_free_data',
1498+
'_emscripten_allow_main_runtime_queued_calls',
1499+
'emscripten_main_browser_thread_id',
1500+
'emscripten_main_thread_process_queued_calls',
1501+
'emscripten_run_in_main_runtime_thread_js',
1502+
'emscripten_stack_set_limits',
1503+
]
1504+
1505+
if settings.MAIN_MODULE:
1506+
settings.REQUIRED_EXPORTS += ['_emscripten_thread_sync_code', '__dl_seterr']
1507+
1508+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += [
1509+
'$exitOnMainThread',
1510+
]
1511+
# Some symbols are required by worker.js.
1512+
# Because emitDCEGraph only considers the main js file, and not worker.js
1513+
# we have explicitly mark these symbols as user-exported so that they will
1514+
# kept alive through DCE.
1515+
# TODO: Find a less hacky way to do this, perhaps by also scanning worker.js
1516+
# for roots.
1517+
worker_imports = [
1518+
'__emscripten_thread_init',
1519+
'__emscripten_thread_exit',
1520+
'__emscripten_thread_crashed',
1521+
'_emscripten_tls_init',
1522+
'_emscripten_current_thread_process_queued_calls',
1523+
'_pthread_self',
1524+
]
1525+
settings.EXPORTED_FUNCTIONS += worker_imports
1526+
building.user_requested_exports.update(worker_imports)
1527+
1528+
# set location of worker.js
1529+
settings.PTHREAD_WORKER_FILE = unsuffixed_basename(target) + '.worker.js'
1530+
1531+
# memalign is used to ensure allocated thread stacks are aligned.
1532+
settings.REQUIRED_EXPORTS += ['emscripten_builtin_memalign']
1533+
1534+
if settings.MINIMAL_RUNTIME:
1535+
building.user_requested_exports.add('exit')
1536+
1537+
if settings.PROXY_TO_PTHREAD:
1538+
settings.REQUIRED_EXPORTS += ['emscripten_proxy_main']
1539+
1540+
# pthread stack setup and other necessary utilities
1541+
def include_and_export(name):
1542+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$' + name]
1543+
settings.EXPORTED_FUNCTIONS += [name]
1544+
1545+
include_and_export('establishStackSpace')
1546+
include_and_export('invokeEntryPoint')
1547+
if not settings.MINIMAL_RUNTIME:
1548+
# keepRuntimeAlive does not apply to MINIMAL_RUNTIME.
1549+
settings.EXPORTED_RUNTIME_METHODS += ['keepRuntimeAlive']
1550+
1551+
if settings.MODULARIZE:
1552+
if not settings.EXPORT_ES6 and settings.EXPORT_NAME == 'Module':
1553+
exit_with_error('pthreads + MODULARIZE currently require you to set -s EXPORT_NAME=Something (see settings.js) to Something != Module, so that the .worker.js file can work')
1554+
1555+
# MODULARIZE+USE_PTHREADS mode requires extra exports out to Module so that worker.js
1556+
# can access them:
1557+
1558+
# general threading variables:
1559+
settings.EXPORTED_RUNTIME_METHODS += ['PThread']
1560+
1561+
# To keep code size to minimum, MINIMAL_RUNTIME does not utilize the global ExitStatus
1562+
# object, only regular runtime has it.
1563+
if not settings.MINIMAL_RUNTIME:
1564+
settings.EXPORTED_RUNTIME_METHODS += ['ExitStatus']
1565+
1566+
14821567
@ToolchainProfiler.profile_block('linker_setup')
14831568
def phase_linker_setup(options, state, newargs, user_settings):
14841569
autoconf = os.environ.get('EMMAKEN_JUST_CONFIGURE') or 'conftest.c' in state.orig_args
@@ -2029,46 +2114,11 @@ def phase_linker_setup(options, state, newargs, user_settings):
20292114
default_setting(user_settings, 'ABORTING_MALLOC', 0)
20302115

20312116
if settings.USE_PTHREADS:
2032-
if settings.ALLOW_MEMORY_GROWTH:
2033-
diagnostics.warning('pthreads-mem-growth', 'USE_PTHREADS + ALLOW_MEMORY_GROWTH may run non-wasm code slowly, see https://github.com/WebAssembly/design/issues/1271')
2117+
setup_pthreads(target)
20342118
settings.JS_LIBRARIES.append((0, 'library_pthread.js'))
2035-
# Functions needs to be exported from the module since they are used in worker.js
2036-
settings.REQUIRED_EXPORTS += [
2037-
'emscripten_dispatch_to_thread_',
2038-
'_emscripten_thread_free_data',
2039-
'_emscripten_allow_main_runtime_queued_calls',
2040-
'emscripten_main_browser_thread_id',
2041-
'emscripten_main_thread_process_queued_calls',
2042-
'emscripten_run_in_main_runtime_thread_js',
2043-
'emscripten_stack_set_limits',
2044-
]
2045-
2046-
if settings.MAIN_MODULE:
2047-
settings.REQUIRED_EXPORTS += ['_emscripten_thread_sync_code', '__dl_seterr']
2048-
2049-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += [
2050-
'$exitOnMainThread',
2051-
]
2052-
# Some symbols are required by worker.js.
2053-
# Because emitDCEGraph only considers the main js file, and not worker.js
2054-
# we have explicitly mark these symbols as user-exported so that they will
2055-
# kept alive through DCE.
2056-
# TODO: Find a less hacky way to do this, perhaps by also scanning worker.js
2057-
# for roots.
2058-
worker_imports = [
2059-
'__emscripten_thread_init',
2060-
'__emscripten_thread_exit',
2061-
'__emscripten_thread_crashed',
2062-
'_emscripten_tls_init',
2063-
'_emscripten_current_thread_process_queued_calls',
2064-
'_pthread_self',
2065-
]
2066-
settings.EXPORTED_FUNCTIONS += worker_imports
2067-
building.user_requested_exports.update(worker_imports)
2068-
2069-
# set location of worker.js
2070-
settings.PTHREAD_WORKER_FILE = unsuffixed_basename(target) + '.worker.js'
20712119
else:
2120+
if settings.PROXY_TO_PTHREAD:
2121+
exit_with_error('-s PROXY_TO_PTHREAD=1 requires -s USE_PTHREADS to work!')
20722122
settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js'))
20732123

20742124
# TODO: Move this into the library JS file once it becomes possible.
@@ -2107,53 +2157,6 @@ def phase_linker_setup(options, state, newargs, user_settings):
21072157
'removeRunDependency',
21082158
]
21092159

2110-
if settings.USE_PTHREADS:
2111-
# memalign is used to ensure allocated thread stacks are aligned.
2112-
settings.REQUIRED_EXPORTS += ['emscripten_builtin_memalign']
2113-
2114-
if settings.MINIMAL_RUNTIME:
2115-
building.user_requested_exports.add('exit')
2116-
2117-
if settings.PROXY_TO_PTHREAD:
2118-
settings.REQUIRED_EXPORTS += ['emscripten_proxy_main']
2119-
2120-
# pthread stack setup and other necessary utilities
2121-
def include_and_export(name):
2122-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$' + name]
2123-
settings.EXPORTED_FUNCTIONS += [name]
2124-
2125-
include_and_export('establishStackSpace')
2126-
include_and_export('invokeEntryPoint')
2127-
if not settings.MINIMAL_RUNTIME:
2128-
# keepRuntimeAlive does not apply to MINIMAL_RUNTIME.
2129-
settings.EXPORTED_RUNTIME_METHODS += ['keepRuntimeAlive']
2130-
2131-
if settings.MODULARIZE:
2132-
if not settings.EXPORT_ES6 and settings.EXPORT_NAME == 'Module':
2133-
exit_with_error('pthreads + MODULARIZE currently require you to set -s EXPORT_NAME=Something (see settings.js) to Something != Module, so that the .worker.js file can work')
2134-
2135-
# MODULARIZE+USE_PTHREADS mode requires extra exports out to Module so that worker.js
2136-
# can access them:
2137-
2138-
# general threading variables:
2139-
settings.EXPORTED_RUNTIME_METHODS += ['PThread']
2140-
2141-
# To keep code size to minimum, MINIMAL_RUNTIME does not utilize the global ExitStatus
2142-
# object, only regular runtime has it.
2143-
if not settings.MINIMAL_RUNTIME:
2144-
settings.EXPORTED_RUNTIME_METHODS += ['ExitStatus']
2145-
2146-
if settings.RELOCATABLE:
2147-
# phtreads + dyanmic linking has certain limitations
2148-
if settings.SIDE_MODULE:
2149-
diagnostics.warning('experimental', '-s SIDE_MODULE + pthreads is experimental')
2150-
elif settings.MAIN_MODULE:
2151-
diagnostics.warning('experimental', '-s MAIN_MODULE + pthreads is experimental')
2152-
elif settings.LINKABLE:
2153-
diagnostics.warning('experimental', '-s LINKABLE + pthreads is experimental')
2154-
elif settings.PROXY_TO_PTHREAD:
2155-
exit_with_error('-s PROXY_TO_PTHREAD=1 requires -s USE_PTHREADS to work!')
2156-
21572160
def check_memory_setting(setting):
21582161
if settings[setting] % webassembly.WASM_PAGE_SIZE != 0:
21592162
exit_with_error(f'{setting} must be a multiple of WebAssembly page size (64KiB), was {settings[setting]}')

0 commit comments

Comments
 (0)