Skip to content

Commit 7cf8352

Browse files
committed
Add design doc: Wasm Worker Pthread Compatibility
See #26631
1 parent d5ebf26 commit 7cf8352

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Design Doc: Wasm Worker Pthread Compatibility
2+
3+
- **Status**: Draft
4+
- **Bug**: https://github.com/emscripten-core/emscripten/issues/26631
5+
6+
## Context
7+
8+
Wasm Workers in Emscripten are a lightweight alternative to pthreads. They use
9+
the same memory and can use the same synchronization primitives, but they do not
10+
have a full `struct pthread` and thus many pthread-based APIs (like
11+
`pthread_self()`) currently do not work when called from a Wasm Worker.
12+
13+
This is not an issue in pure Wasm Workers programs but we also support hybrid
14+
programs that run both pthreads and Wasm Workers. In this cases the pthread
15+
API is available, but will fail in undefined ways if called from Wasm Workers.
16+
17+
This document proposes a plan to improve the hybrid mode by adding the pthread
18+
metadata (`struct pthread`) to each Wasm Worker, allowing the pthread API (or at
19+
least some subset of it) APIs to used from Wasm Workers.
20+
21+
## Proposed Changes
22+
23+
### 1. Memory Layout
24+
25+
Currently, Wasm Workers allocate space for TLS and stack: `[TLS data] [Stack]`.
26+
We propose to change this to: `[struct pthread] [TLS data] [Stack]`.
27+
28+
The `struct pthread` will be located at the very beginning of the allocated
29+
memory block for each Wasm Worker.
30+
31+
### 2. `struct pthread` Initialization
32+
33+
The `struct pthread` will be initialized by the creator thread in `emscripten_create_wasm_worker` (or `emscripten_malloc_wasm_worker`).
34+
This includes:
35+
- Zero-initializing the structure.
36+
- Setting the `self` pointer to the start of the `struct pthread`.
37+
- Initializing essential fields like `tid`.
38+
39+
On the worker thread side, `_emscripten_wasm_worker_initialize` will need to set
40+
the thread-local pointer (returned by `__get_tp()`) to the `struct pthread`
41+
location.
42+
43+
### 3. `__get_tp` Support
44+
45+
We will modify `system/lib/pthread/emscripten_thread_state.S` to provide a
46+
`__get_tp` implementation for Wasm workers that returns the address of the
47+
`struct pthread`. This will allow `__pthread_self()` and other related functions
48+
to work correctly.
49+
50+
### 4. Supported Pthread API Subset
51+
52+
We intend to support a subset of the pthread API within Wasm workers:
53+
- `pthread_self()`: Returns the worker's `struct pthread` pointer.
54+
- `pthread_equal()`: Works normally.
55+
- `pthread_getspecific()` / `pthread_setspecific()`: TSD (Thread Specific Data) should work if `tsd` field in `struct pthread` is initialized.
56+
- `pthread_mutex_*`: Mutexes will work as they rely on `struct pthread` for owner tracking.
57+
- `pthread_cond_*`: Condition variables will work as they rely on `struct pthread` for waiter tracking.
58+
- Low-level synchronization primitives that use `struct pthread` (e.g., some internal locks).
59+
60+
APIs that will NOT be supported (or will have limited support):
61+
- `pthread_create()` / `pthread_join()` / `pthread_detach()`: Wasm workers have their own creation and lifecycle management.
62+
- `pthread_cancel()`: Not supported in Wasm workers.
63+
- `pthread_kill()`: Not supported in Wasm workers.
64+
65+
## Implementation Plan
66+
67+
1. Modify `emscripten_create_wasm_worker` in `system/lib/wasm_worker/library_wasm_worker.c` to account for `sizeof(struct pthread)` in memory allocation and initialize the structure.
68+
2. Update `_emscripten_wasm_worker_initialize` in `system/lib/wasm_worker/wasm_worker_initialize.S` to set the thread pointer.
69+
3. Modify `system/lib/pthread/emscripten_thread_state.S` to enable `__get_tp` for Wasm workers.
70+
4. Review and test essential pthread functions (like TSD) in Wasm workers.
71+
5. Document the supported and unsupported pthread APIs for Wasm workers.
72+
73+
## Verification
74+
- Add a new test that makes use of `pthread_self()` and low level synchronization APIs from a Wasm Worker.
75+
- Verify that existing Wasm worker tests still pass.

0 commit comments

Comments
 (0)