-
Notifications
You must be signed in to change notification settings - Fork 207
Opt-in busywait mode for futexes #562
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
base: main
Are you sure you want to change the base?
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? |
@@ -64,6 +64,9 @@ int __wasilibc_rename_oldat(int olddirfd, const char *oldpath, const char *newpa | |||
int __wasilibc_rename_newat(const char *oldpath, int newdirfd, const char *newpath) | |||
__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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
We are heavily using wasi-libc + wasip1-threads on browsers. Since wasi-libc uses
memory.atomic.wait32
to implement futex, and the usage ofmemory.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 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_thread
is not used by user program and LTO is enabled.