emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Update documentation on Wasm Workers

Open juj opened this issue 1 year ago • 2 comments

Update documentation on Wasm Workers stack size, and Wasm Workers vs Emscripten Workers API. Addresses #21610.

juj avatar Apr 03 '24 07:04 juj

So the pre-allocated stack is for thread-local storage? @juj

msqr1 avatar Apr 14 '24 16:04 msqr1

If the normal worker api can be spawned using a custom URL, how do we postmessage wasm function calls over? How does the worker api function?

msqr1 avatar Apr 19 '24 03:04 msqr1

Sorry for the delay, I was on a leave for a while there. Updated to address the review.

So the pre-allocated stack is for thread-local storage? @juj

With Wasm Workers, the stack memory area that is passed to Wasm Worker creation will contain the stack, but additionally, the TLS memory for the Wasm Worker will be located at the bottom end of that memory area. This simplifies the Wasm Worker memory initialization, as it allows one to allocate only one memory block for all dynamic memory needs for the Wasm Worker.

If the normal worker api can be spawned using a custom URL, how do we postmessage wasm function calls over? How does the worker api function?

The earlier Worker API uses these functions to postMessage() there and back: https://github.com/emscripten-core/emscripten/blob/58d39a5978d78f7348ad7e9b658aaa2399ab4326/system/include/emscripten/emscripten.h#L126-L138

emscripten_call_worker() performs a postMessage(), and emscripten_worker_respond() replies back. In general that API is not that fully crafted, and quite old as it predates SharedArrayBuffer. So most Emscripten applications should avoid using it (to the point of probably considering it as deprecated-ish).

juj avatar May 27 '24 09:05 juj

Hey, updated the PR to latest main. Any more reviews, or good to land?

juj avatar Aug 27 '24 12:08 juj