Opt-in busywait mode for futexes#562
Conversation
84a2ff9 to
57331a7
Compare
This commit adds a browser test harness to run the tests in the browser. ## Motivation We are heavily using wasi-libc on browsers but we don't have any test for the case in wasi-libc. In theory, there should be no behavioral difference between wasmtime and browsers (as long as WASI implementations are correct) but browsers have their own limitations in their Wasm engines. For example, memory.atomic.wait32 is not permitted on the main thread. We are working on adding some mitigation for such browser-specific issues (#562) and this test harness will help to validate the fix.
what happens when the instruction is executed on the main thread? does it trap? |
| __attribute__((__warn_unused_result__)); | ||
|
|
||
| /// Enable busywait in futex on current thread. | ||
| void __wasilibc_enable_futex_busywait_on_current_thread(void); |
There was a problem hiding this comment.
while currently futex might be only the user of the blocking instruction, i guess it's better to name the api to make it clear it's about any blocking instructions, not necessarily for futex.
what do you think?
There was a problem hiding this comment.
Yeah, I'm also not a fan of the current name. Do you have any better idea?
Yes, it traps. From the JS embedder's view, it throws a JS exception. |
|
after skimming the relevant discussions, i have a question. |
Yes, that's right. A user program needs to have a main entry point for each platform (web or not) and it's fine (at least for us) to hardcode the platform knowledge in the entry point of main and threads. If we really don't want to hardcode the platform knowledge into the user program, it might be an option to export the enable function and ask embedders to call it.
Actually we are using the approach now, overriding the futex functions in the user program but we consider it as a workaround. We thought we just need to hook So to make it overriding the default mode reliably, we have some options:
1 or 3 are best for us and other on-browser p1-threads users but 2 is better than nothing for us. |
|
@sunfishcode Sorry for the ping, but it would be appreciated if you take a look at this 🙏 |
|
Is it the case that with the weak symbols, this code is not linked in at all if the user doesn't request it? |
|
@sunfishcode Correct. Unless user explicitly reference |
sunfishcode
left a comment
There was a problem hiding this comment.
Ok, I haven't closely reviewed the threading side of this, but since it's opt-in, it at least shouldn't affect people who don't need it.
|
This PR appears to be causing a failure in CI; see https://github.com/WebAssembly/wasi-libc/actions/runs/15767574941/job/44446777284. @kateinoigakukun would you be able to investigate? Thanks! |
|
Sure, let me check 🙏 |
|
@sunfishcode #589 should fix the CI issue |
This release includes various fixes and features, including: * Fix for too conservative incremental builds WebAssembly/wasi-libc@eadb436 * Opt-in busywait mode for futexes WebAssembly/wasi-libc#562 * Musl update from 1.2.3 to 1.2.5 WebAssembly/wasi-libc#557
We are heavily using wasi-libc + wasip1-threads on browsers. Since wasi-libc uses
memory.atomic.wait32to implement futex, and the usage ofmemory.atomic.wait32on 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 as a workaround. Rust has always employs busywait regardless of the current thread is main. 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.
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_threadis not used by user program and LTO is enabled.