You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When instantiating a Wasm Worker, one has to create a memory array for the LLVM
349
+
data stack for the created Worker. This data stack will generally consist only
350
+
of local variables that have been "spilled" by LLVM into memory, e.g. to contain
351
+
large arrays, structs, or other variables that are referenced by a memory
352
+
address. This stack will not contain control flow information.
353
+
354
+
Since WebAssembly does not support virtual memory, the size of the LLVM data
355
+
stack that is defined both for Wasm Workers but also the main thread will not be
356
+
possible to grow at runtime. So if the Worker (or the main thread) runs out of
357
+
stack space, the program behavior will be undefined. Use the Emscripten linker
358
+
flag -sSTACK_OVERFLOW_CHECK=2 to emit runtime stack overflow checks into the
359
+
program code to detect these situations during development.
360
+
361
+
Note that to avoid the need to perform two separate allocations, the TLS memory
362
+
for the Wasm Worker will be located at the bottom end (low memory address) of
363
+
the Wasm Worker stack space.
364
+
365
+
Wasm Workers vs the earlier Emscripten Worker API
366
+
=================================================
367
+
368
+
Emscripten provides a second Worker API as part of the emscripten.h header. This Worker API predates the advent of SharedArrayBuffer, and is quite distinct from Wasm Workers API, just the naming of these two APIs is similar due to historical reasons.
369
+
370
+
Both APIs allow one to spawn Web Workers from the main thread, though the semantics are different.
371
+
372
+
With the Worker API, the user will be able to spawn a Web Worker from a custom URL. This URL can point to a completely separate JS file that was not compiled with Emscripten, to load up Workers from arbitrary URLs. With Wasm Workers, a custom URL is not specified: Wasm Workers will always spawn a Web Worker that computes in the same WebAssembly+JavaScript context as the main program.
373
+
374
+
The Worker API does not integrate with SharedArrayBuffer, so interaction with the loaded Worker will always be asynchronous. Wasm Workers howerer is built on top of SharedArrayBuffer, and each Wasm Worker shares and computes in the same WebAssembly Memory address space of the main thread.
375
+
376
+
Both the Worker API and Wasm Workers API provide the user with ability to postMessage() function calls to the Worker. In Worker API, this message posting is restricted to need to originate/initiate from the main thread towards the Worker (using the API ``emscripten_call_worker()`` and ``emscripten_worker_respond()`` in ``<emscripten.h>``). With Wasm Workers however one can also postMessage() function calls to their parent (owning) thread.
377
+
378
+
If posting function calls with the Emscripten Worker API, it is required that the target Worker URL points to an Emscripten compiled program (so it has the ``Module`` structure to locate function names). Only functions that have been exported to the ``Module`` object are callable. With Wasm Workers, any C/C++ function may be posted, and does not need to be exported.
379
+
380
+
Use the Emscripten Worker API when:
381
+
- you want to easily spawn a Worker from a JS file that was not built using Emscripten
382
+
- you want to spawn as Worker a single separate compiled program than the main thread program represents, and the main thread and Worker programs do not share common code
383
+
- you do not want to require the use of SharedArrayBuffer, or setting up COOP+COEP headers
384
+
- you only need to communicate with the Worker asynchronously using postMessage() function calls
385
+
386
+
Use the Wasm Workers API when:
387
+
- you want to create one or more new threads that synchronously compute in the same Wasm Module context
388
+
- you want to spawn multiple Workers from the same codebase and save memory by sharing the WebAssembly Module (object code) and Memory (address space) across the Workers
389
+
- you want to synchronously coordinate communication between threads by using atomic primitives and locks
390
+
- your web server has been configured with the needed COOP+COEP headers to enable SharedArrayBuffer capabilities on the site
0 commit comments