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

Draft
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 futex 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.
/// FIXME: Better naming
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.

Depends on #563

@kateinoigakukun kateinoigakukun force-pushed the pr-98c3e8941de60bb9c8df94605fb1b211dc949a29 branch from 84a2ff9 to 57331a7 Compare January 21, 2025 05:03
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