Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm workers #12833

Merged
merged 67 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
0f55bf4
Add Wasm Workers
juj Feb 7, 2022
fa21c46
Add TLS test, ES6ify.
juj Feb 7, 2022
f466e62
Update test
juj Feb 7, 2022
bd1e261
Add TLS support to Wasm Workers
juj Feb 15, 2022
ead8aa5
Add C++11 thread_local keyword test.
juj Feb 15, 2022
9545aeb
Add test for C11 _Thread_local.
juj Feb 15, 2022
29bed03
Add emscripten_malloc_wasm_worker and rename creation API a little.
juj Feb 15, 2022
0e3e328
Add documentation for Wasm Workers.
juj Feb 15, 2022
83fef04
Flake and lint and fix build error
juj Feb 17, 2022
57b2ff6
Remove deps_info dependency that does not work in current setup
juj Feb 17, 2022
559551f
__builtin_wasm_tls_align() can be zero
juj Feb 18, 2022
9effd63
Add more notes about __builtin_wasm_tls_align() being zero
juj Feb 18, 2022
177f29b
Add test for GCC __thread keyword.
juj Feb 18, 2022
3e0cdb3
Fix test_wasm_worker_malloc
juj Feb 18, 2022
883e539
Fix emscripten_lock_async_acquire()
juj Feb 18, 2022
a403133
Fix thread stack creation.
juj Feb 18, 2022
67d0bbb
Fix wasm64 build
juj Feb 18, 2022
ebc4319
Add slack to lock_busyspin_wait_acquire
juj Feb 18, 2022
da84c92
Fix typo in setting
juj Feb 18, 2022
ffdc68e
Remove removal of TextDecoder in threads.
juj Feb 20, 2022
2a0f5e4
Fix non-Wasm Workers build
juj Feb 20, 2022
19904b4
Fix file system case sensitivity
juj Feb 20, 2022
0300d02
Fix Wasm Workers proxying mode generation.
juj Feb 20, 2022
9f87d16
Skip TLS tests on Linux, they produce an internal compiler error.
juj Feb 20, 2022
df81c82
Fix typo
juj Feb 20, 2022
fd5ed59
Fix wasm_worker.h include from C code.
juj Feb 20, 2022
8c94a36
Add library_wasm_worker_stub.c.
juj Feb 20, 2022
1876f14
Wasm Workers working on default runtime.
juj Feb 21, 2022
3fa9bac
flake
juj Feb 21, 2022
fdb8c92
Disable most wasm workers tests to debug CI
juj Feb 21, 2022
37fba96
Fix non-minimal runtime wasm workers startup. Add test for WASM_WORKE…
juj Feb 22, 2022
a9e47d6
Simplify in MINIMAL_RUNTIME preamble assignment for wasm maximum memory.
juj Feb 22, 2022
4091a56
Fix USE_PTHREADS+WASM_WORKERS line.
juj Feb 22, 2022
90543ec
Add support for simultaneous pthreads + Wasm workers.
juj Feb 22, 2022
ce6040e
Do not pass redundant TLS size to Wasm Worker creation side.
juj Feb 22, 2022
81b0543
Update emcc.py wasm worker deps
juj Feb 22, 2022
fbc4fdc
Remove special handling of .S files in system_libs build
juj Feb 23, 2022
75d51c8
Update documentation
juj Feb 23, 2022
553dd6b
Add code size test.
juj Feb 23, 2022
7f35de8
flake
juj Feb 23, 2022
b0f04f0
Update tests and wasm worker MT build
juj Feb 23, 2022
ba81dd2
Fix mt build
juj Feb 23, 2022
59d11ad
Adjust mt build
juj Feb 23, 2022
05d0dc9
Update code size test
juj Feb 23, 2022
98cc721
Update hello worker wasm
juj Feb 24, 2022
28f37d9
flake
juj Feb 24, 2022
4dff3b7
Merge remote-tracking branch 'origin/main' into wasm_workers
juj Mar 3, 2022
76a4e82
Address review: Allow building with -sSHARED_MEMORY and add a test. M…
juj Mar 3, 2022
34a766e
Remove unnecessary dynCall statements
juj Mar 4, 2022
b45a7b7
Update mention of C11 and C++11 Atomics APIs
juj Mar 4, 2022
acdce95
Merge branch 'main' into wasm_workers
juj Mar 6, 2022
ee31eed
Remove old code.
juj Mar 6, 2022
08041f1
Utilize runOnMainThread() in MINIMAL_RUNTIME ready handler.
juj Mar 6, 2022
1f5d6b0
Simplify code
juj Mar 6, 2022
9e56c85
#error quotes
juj Mar 6, 2022
bca824e
Clean typo
juj Mar 6, 2022
07a3585
Cleanup tests
juj Mar 6, 2022
7fa8dd7
Update ChangeLog
juj Mar 6, 2022
33079fd
Fixes
juj Mar 6, 2022
4f8dc6c
Add test files.
juj Mar 6, 2022
c78609d
Fix pthreads
juj Mar 6, 2022
552e0a7
Remove moved test
juj Mar 7, 2022
7cfeb98
Address review
juj Mar 7, 2022
b7ac44d
Small code size optimization
juj Mar 7, 2022
5839916
Small code size opt
juj Mar 7, 2022
a570999
Flake
juj Mar 7, 2022
8d53ac4
Update Wasm Workers code size test
juj Mar 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ignorePatterns:
- "src/embind/"
- "src/emrun_postjs.js"
- "src/worker.js"
- "src/wasm_worker.js"
- "src/wasm2js.js"
- "src/webGLClient.js"
- "src/webGLWorker.js"
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ See docs/process.md for more on how version tagging works.
- Fix deadlock in `munmap` that was introduced in 3.1.5. The deadlock would
occur in multi-threaded programs when a partial unmap was requested (which
emscripten does not support). (#16413)
- Added new compiler+linker option -sSHARED_MEMORY=1, which enables targeting
a shared WebAssembly.Memory. (#16419)
- Added new API "Wasm Workers", which is an alternative to pthreads for building
multithreaded applications, enabled via -sWASM_WORKERS=1 (#12833)

3.1.6 - 02/24/2022
------------------
Expand Down
38 changes: 35 additions & 3 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ def get_cflags(user_args):
if settings.SHARED_MEMORY:
cflags.append('-D__EMSCRIPTEN_SHARED_MEMORY__=1')

if settings.WASM_WORKERS:
cflags.append('-D__EMSCRIPTEN_WASM_WORKERS__=1')

if not settings.STRICT:
# The preprocessor define EMSCRIPTEN is deprecated. Don't pass it to code
# in strict mode. Code should use the define __EMSCRIPTEN__ instead.
Expand Down Expand Up @@ -1424,11 +1427,17 @@ def phase_setup(options, state, newargs, user_settings):
if settings.MAIN_MODULE or settings.SIDE_MODULE:
settings.RELOCATABLE = 1

if settings.USE_PTHREADS:
# Pthreads and Wasm Workers require targeting shared Wasm memory (SAB).
if settings.USE_PTHREADS or settings.WASM_WORKERS:
settings.SHARED_MEMORY = 1

if settings.SHARED_MEMORY and '-pthread' not in newargs:
if settings.USE_PTHREADS and '-pthread' not in newargs:
newargs += ['-pthread']
elif settings.SHARED_MEMORY:
if '-matomics' not in newargs:
newargs += ['-matomics']
if '-mbulk-memory' not in newargs:
newargs += ['-mbulk-memory']

if 'DISABLE_EXCEPTION_CATCHING' in user_settings and 'EXCEPTION_CATCHING_ALLOWED' in user_settings:
# If we get here then the user specified both DISABLE_EXCEPTION_CATCHING and EXCEPTION_CATCHING_ALLOWED
Expand Down Expand Up @@ -2066,6 +2075,18 @@ def phase_linker_setup(options, state, newargs, user_settings):
if settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING:
settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free']

if settings.WASM_WORKERS:
# TODO: After #15982 is resolved, these dependencies can be declared in library_wasm_worker.js
# instead of having to record them here.
wasm_worker_imports = ['_emscripten_wasm_worker_initialize']
settings.EXPORTED_FUNCTIONS += wasm_worker_imports
juj marked this conversation as resolved.
Show resolved Hide resolved
building.user_requested_exports.update(wasm_worker_imports)
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['_wasm_worker_initializeRuntime']
# set location of Wasm Worker bootstrap JS file
if settings.WASM_WORKERS == 1:
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_wasm_worker.js')))

if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME:
# when the filesystem is forced, we export by default methods that filesystem usage
# may need, including filesystem usage from standalone file packager output (i.e.
Expand Down Expand Up @@ -2805,8 +2826,8 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
# src = re.sub(r'\n+[ \n]*\n+', '\n', src)
# write_file(final_js, src)

target_dir = os.path.dirname(os.path.abspath(target))
if settings.USE_PTHREADS:
target_dir = os.path.dirname(os.path.abspath(target))
worker_output = os.path.join(target_dir, settings.PTHREAD_WORKER_FILE)
contents = shared.read_and_preprocess(utils.path_from_root('src/worker.js'), expand_macros=True)
write_file(worker_output, contents)
Expand All @@ -2816,6 +2837,17 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
write_file(worker_output, minified_worker)

# Deploy the Wasm Worker bootstrap file as an output file (*.ww.js)
if settings.WASM_WORKERS == 1:
worker_output = os.path.join(target_dir, settings.WASM_WORKER_FILE)
with open(worker_output, 'w') as f:
f.write(shared.read_and_preprocess(shared.path_from_root('src', 'wasm_worker.js'), expand_macros=True))

# Minify the wasm_worker.js file in optimized builds
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
open(worker_output, 'w').write(minified_worker)

# track files that will need native eols
generated_text_files_with_native_eols = []

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions site/source/docs/api_reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ This section lists Emscripten's public API, organised by header file. At a very
- :ref:`Fetch-API`:
API for managing accesses to network XHR and IndexedDB.

- :ref:`wasm_workers`:
Enables writing multithreaded programs using a web-like API.

- :ref:`Module`:
Global JavaScript object that can be used to control code execution and access exported methods.

Expand Down Expand Up @@ -53,6 +56,7 @@ This section lists Emscripten's public API, organised by header file. At a very
bind.h
trace.h
fiber.h
wasm_workers
advanced-apis


Loading