Commit 859062b
authored
Opt-in busywait mode for futexes (WebAssembly#562)
We are heavily using wasi-libc + wasip1-threads on browsers. Since
wasi-libc uses `memory.atomic.wait32` to implement futex, and the usage
of `memory.atomic.wait32` on the main thread is prohibited on browsers,
we currently need to write code carefully not to reach the instruction.
Emscripten addresses this limitation by [employing a busy-wait on the
main
thread](https://github.com/emscripten-core/emscripten/blob/058a9fff/system/lib/pthread/emscripten_futex_wait.c#L111-L150)
as a workaround. Rust has [always employs busywait regardless of the
current thread is
main](https://github.com/rust-lang/rust/blob/a47555110cf09b3ed59811d9b02235443e76a595/library/std/src/sys/alloc/wasm.rs#L75-L144).
This approach, while effective for browsers, introduces unnecessary
overhead in environments where memory.atomic.wait32 is permitted.
This change provides a similar solution by introducing an opt-in
busy-wait mode for futexes. By making this an optional feature, we avoid
imposing the overhead of busy-waiting in non-browser environments while
ensuring compatibility with browser-based restrictions.
```c
#include <wasi/libc-busywait.h>
/// Enable busywait in futex on current thread.
void __wasilibc_enable_futex_busywait_on_current_thread(void);
```
This change slightly adds some runtime overheads in futex to check if we
should use busywait, but it can be optimized away as long as
`__wasilibc_enable_futex_busywait_on_current_thread` is not used by user
program and LTO is enabled.1 parent a3a9596 commit 859062b
File tree
16 files changed
+191
-5
lines changed- expected
- wasm32-wasip1-threads
- wasm32-wasip1
- wasm32-wasip2
- libc-bottom-half/headers/public/wasi
- libc-top-half/musl
- include/wasi
- src/thread
- wasm32
- test/src
- libc-test/functional
- misc
16 files changed
+191
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
| 370 | + | |
370 | 371 | | |
371 | 372 | | |
372 | 373 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
| 365 | + | |
365 | 366 | | |
366 | 367 | | |
367 | 368 | | |
368 | 369 | | |
369 | 370 | | |
370 | 371 | | |
371 | 372 | | |
| 373 | + | |
| 374 | + | |
372 | 375 | | |
373 | 376 | | |
374 | 377 | | |
| |||
400 | 403 | | |
401 | 404 | | |
402 | 405 | | |
| 406 | + | |
403 | 407 | | |
404 | 408 | | |
405 | 409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| 170 | + | |
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3131 | 3131 | | |
3132 | 3132 | | |
3133 | 3133 | | |
| 3134 | + | |
3134 | 3135 | | |
3135 | 3136 | | |
3136 | 3137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| 170 | + | |
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3124 | 3124 | | |
3125 | 3125 | | |
3126 | 3126 | | |
| 3127 | + | |
3127 | 3128 | | |
3128 | 3129 | | |
3129 | 3130 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| 171 | + | |
171 | 172 | | |
172 | 173 | | |
173 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3276 | 3276 | | |
3277 | 3277 | | |
3278 | 3278 | | |
| 3279 | + | |
3279 | 3280 | | |
3280 | 3281 | | |
3281 | 3282 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
0 commit comments