-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1c557c
commit 84a2ff9
Showing
12 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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; | ||
} |