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

Opt-in busywait mode for futexes #562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kateinoigakukun
Copy link
Contributor

@kateinoigakukun kateinoigakukun commented Jan 21, 2025

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 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.

#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.

@kateinoigakukun kateinoigakukun force-pushed the pr-98c3e8941de60bb9c8df94605fb1b211dc949a29 branch from 84a2ff9 to 57331a7 Compare January 21, 2025 05:03
abrown pushed a commit that referenced this pull request Feb 3, 2025
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.
@kateinoigakukun kateinoigakukun marked this pull request as ready for review February 4, 2025 14:37
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.

1 participant