diff --git a/Makefile b/Makefile index bb303091..a44f110e 100644 --- a/Makefile +++ b/Makefile @@ -787,7 +787,8 @@ $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJ $(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \ -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \ -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \ - -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 + -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \ + -D_WASI_EMULATED_PTHREAD # emmalloc uses a lot of pointer type-punning, which is UB under strict aliasing, # and this was found to have real miscompilations in wasi-libc#421. diff --git a/expected/wasm32-wasip1/predefined-macros.txt b/expected/wasm32-wasip1/predefined-macros.txt index 5d56b996..dcce9410 100644 --- a/expected/wasm32-wasip1/predefined-macros.txt +++ b/expected/wasm32-wasip1/predefined-macros.txt @@ -3347,7 +3347,12 @@ #define preadv64 preadv #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) #define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); +#define pthread_create(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_detach(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") #define pthread_equal(x,y) ((x)==(y)) +#define pthread_join(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_timedjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_tryjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") #define pwrite64 pwrite #define pwritev64 pwritev #define readdir64 readdir diff --git a/expected/wasm32-wasip2/predefined-macros.txt b/expected/wasm32-wasip2/predefined-macros.txt index bb766d77..d8229353 100644 --- a/expected/wasm32-wasip2/predefined-macros.txt +++ b/expected/wasm32-wasip2/predefined-macros.txt @@ -3502,7 +3502,12 @@ #define preadv64 preadv #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0) #define pthread_cleanup_push(f,x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x); +#define pthread_create(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_detach(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") #define pthread_equal(x,y) ((x)==(y)) +#define pthread_join(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_timedjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_tryjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; to enable stub functions which always fail, compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") #define pwrite64 pwrite #define pwritev64 pwritev #define readdir64 readdir diff --git a/libc-top-half/musl/include/pthread.h b/libc-top-half/musl/include/pthread.h index 2c35d0b5..3a5566fe 100644 --- a/libc-top-half/musl/include/pthread.h +++ b/libc-top-half/musl/include/pthread.h @@ -77,12 +77,29 @@ extern "C" { #define PTHREAD_NULL ((pthread_t)0) +#ifdef __wasilibc_unmodified_upstream int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); int pthread_detach(pthread_t); -#ifdef __wasilibc_unmodified_upstream _Noreturn void pthread_exit(void *); -#endif int pthread_join(pthread_t, void **); +#else +#if defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT) +int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict); +int pthread_detach(pthread_t); +int pthread_join(pthread_t, void **); +#else +#include +#define pthread_create(...) _Static_assert(0, "This mode of WASI does not have threads enabled; \ +to enable stub functions which always fail, \ +compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_detach(...) _Static_assert(0, "This mode of WASI does not have threads enabled; \ +to enable stub functions which always fail, \ +compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_join(...) _Static_assert(0, "This mode of WASI does not have threads enabled; \ +to enable stub functions which always fail, \ +compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#endif +#endif #ifdef __GNUC__ __attribute__((const)) @@ -232,8 +249,17 @@ int pthread_setname_np(pthread_t, const char *); int pthread_getname_np(pthread_t, char *, size_t); int pthread_getattr_default_np(pthread_attr_t *); int pthread_setattr_default_np(const pthread_attr_t *); +#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_PTHREAD) || defined(_REENTRANT) int pthread_tryjoin_np(pthread_t, void **); int pthread_timedjoin_np(pthread_t, void **, const struct timespec *); +#else +#define pthread_tryjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; \ +to enable stub functions which always fail, \ +compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#define pthread_timedjoin_np(...) _Static_assert(0, "This mode of WASI does not have threads enabled; \ +to enable stub functions which always fail, \ +compile with -D_WASI_EMULATED_PTHREAD and link with -lwasi-emulated-pthread") +#endif #endif #if _REDIR_TIME64