-
Notifications
You must be signed in to change notification settings - Fork 208
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
Open
kateinoigakukun
wants to merge
1
commit into
WebAssembly:main
Choose a base branch
from
kateinoigakukun:pr-98c3e8941de60bb9c8df94605fb1b211dc949a29
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef __wasi_libc_busywait_h | ||
#define __wasi_libc_busywait_h | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/// Enable busywait in futex on current thread. | ||
void __wasilibc_enable_futex_busywait_on_current_thread(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
libc-top-half/musl/src/thread/wasm32/__wasilibc_busywait.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <stdint.h> | ||
#include <time.h> | ||
#include <errno.h> | ||
|
||
#define DEFINE_GLOBAL_GETTER(name, core_type, c_type) \ | ||
static inline c_type name##_get(void) { \ | ||
c_type val; \ | ||
__asm__( \ | ||
".globaltype " #name ", " #core_type "\n" \ | ||
"global.get " #name "\n" \ | ||
"local.set %0\n" \ | ||
: "=r"(val)); \ | ||
return val; \ | ||
} | ||
#define DEFINE_GLOBAL_SETTER(name, core_type, c_type) \ | ||
static inline void name##_set(c_type val) { \ | ||
__asm__( \ | ||
".globaltype " #name ", " #core_type "\n" \ | ||
"local.get %0\n" \ | ||
"global.set " #name "\n" \ | ||
: : "r"(val)); \ | ||
} | ||
|
||
#define DEFINE_RW_GLOBAL(name, core_type, c_type) \ | ||
__asm__( \ | ||
".globaltype " #name ", " #core_type "\n" \ | ||
".global " #name "\n" \ | ||
#name ":\n" \ | ||
); \ | ||
DEFINE_GLOBAL_GETTER(name, core_type, c_type) \ | ||
DEFINE_GLOBAL_SETTER(name, core_type, c_type) | ||
|
||
DEFINE_RW_GLOBAL(__wasilibc_use_busy_futex, i32, int32_t) | ||
|
||
void __wasilibc_enable_futex_busywait_on_current_thread(void) | ||
{ | ||
__wasilibc_use_busy_futex_set(1); | ||
} | ||
|
||
int __wasilibc_futex_wait_atomic_wait(volatile void *addr, int op, int val, int64_t max_wait_ns); | ||
|
||
int __wasilibc_futex_wait_maybe_busy(volatile void *addr, int op, int val, int64_t max_wait_ns) | ||
{ | ||
// PLEASE NOTE THAT WE CANNOT CALL LIBC FUNCTIONS THAT USE FUTEXES HERE | ||
|
||
if (!__wasilibc_use_busy_futex_get()) { | ||
return __wasilibc_futex_wait_atomic_wait(addr, op, val, max_wait_ns); | ||
} | ||
|
||
struct timespec start; | ||
int r = clock_gettime(CLOCK_REALTIME, &start); | ||
|
||
// If we can't get the current time, we can't wait with a timeout. | ||
if (r) return r; | ||
|
||
while (1) { | ||
// Check timeout if it's a positive value | ||
if (max_wait_ns >= 0) { | ||
struct timespec now; | ||
r = clock_gettime(CLOCK_REALTIME, &now); | ||
if (r) return r; | ||
|
||
int64_t elapsed_ns = (now.tv_sec - start.tv_sec) * 1000000000 + now.tv_nsec - start.tv_nsec; | ||
if (elapsed_ns >= max_wait_ns) { | ||
return -ETIMEDOUT; | ||
} | ||
} | ||
|
||
if (__c11_atomic_load((_Atomic int *)addr, __ATOMIC_SEQ_CST) != val) { | ||
break; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! filter.py(TARGET_TRIPLE): wasm32-wasip1-threads | ||
//! add-flags.py(CFLAGS): -I. | ||
//! add-flags.py(LDFLAGS): -Wl,--import-memory,--export-memory,--shared-memory,--max-memory=1073741824 | ||
//! add-flags.py(RUN): --wasi threads | ||
#include "build/download/libc-test/src/functional/pthread_cond.c" | ||
|
||
#include <wasi/libc-busywait.h> | ||
|
||
__attribute__((constructor)) | ||
void __wasilibc_enable_busywait(void) | ||
{ | ||
__wasilibc_enable_futex_busywait_on_current_thread(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! filter.py(TARGET_TRIPLE): wasm32-wasip1-threads | ||
//! add-flags.py(CFLAGS): -I. | ||
//! add-flags.py(LDFLAGS): -Wl,--import-memory,--export-memory,--shared-memory,--max-memory=1073741824 | ||
//! add-flags.py(RUN): --wasi threads | ||
#include "build/download/libc-test/src/functional/pthread_mutex.c" | ||
|
||
#include <wasi/libc-busywait.h> | ||
|
||
__attribute__((constructor)) | ||
void __wasilibc_enable_busywait(void) | ||
{ | ||
__wasilibc_enable_futex_busywait_on_current_thread(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//! filter.py(TARGET_TRIPLE): wasm32-wasip1-threads | ||
//! add-flags.py(CFLAGS): -I. | ||
//! add-flags.py(LDFLAGS): -Wl,--import-memory,--export-memory,--shared-memory,--max-memory=1073741824 | ||
//! add-flags.py(RUN): --wasi threads | ||
#include "build/download/libc-test/src/functional/pthread_tsd.c" | ||
|
||
#include <wasi/libc-busywait.h> | ||
|
||
__attribute__((constructor)) | ||
void __wasilibc_enable_busywait(void) | ||
{ | ||
__wasilibc_enable_futex_busywait_on_current_thread(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//! filter.py(TARGET_TRIPLE): wasm32-wasip1-threads | ||
//! add-flags.py(CFLAGS): -pthread | ||
//! add-flags.py(LDFLAGS): -pthread | ||
//! add-flags.py(RUN): --wasi threads | ||
|
||
#include <assert.h> | ||
#include <pthread.h> | ||
#include <time.h> | ||
#include <errno.h> | ||
#include <wasi/libc-busywait.h> | ||
|
||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | ||
|
||
int main() { | ||
struct timespec ts; | ||
int ret; | ||
|
||
clock_gettime(CLOCK_REALTIME, &ts); | ||
ts.tv_sec += 1; | ||
|
||
__wasilibc_enable_futex_busywait_on_current_thread(); | ||
|
||
pthread_mutex_lock(&mutex); | ||
|
||
ret = pthread_cond_timedwait(&cond, &mutex, &ts); | ||
|
||
assert(ret == ETIMEDOUT); | ||
|
||
pthread_mutex_unlock(&mutex); | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?