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

Update documentation on Wasm Workers #21686

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

juj
Copy link
Collaborator

@juj juj commented Apr 3, 2024

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

@juj juj enabled auto-merge (squash) April 3, 2024 07:00
@msqr1
Copy link

msqr1 commented Apr 14, 2024

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

@msqr1
Copy link

msqr1 commented Apr 19, 2024

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?

@juj
Copy link
Collaborator Author

juj commented May 27, 2024

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:

// worker APIs
typedef int worker_handle;
worker_handle emscripten_create_worker(const char *url);
void emscripten_destroy_worker(worker_handle worker);
typedef void (*em_worker_callback_func)(char*, int, void*);
void emscripten_call_worker(worker_handle worker, const char *funcname, char *data, int size, em_worker_callback_func callback, void *arg);
void emscripten_worker_respond(char *data, int size);
void emscripten_worker_respond_provisionally(char *data, int size);
int emscripten_get_worker_queue_size(worker_handle worker);

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 juj force-pushed the update_wasm_worker_documentation branch from 658392e to f926a27 Compare May 27, 2024 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants