Skip to content

Commit 564fa2b

Browse files
committed
pythongh-101538: Add experimental wasi-threads build
eg. ``` autoconf export WASI_SDK_PATH=/opt/wasi-sdk-19.5g0236e959edbc ./Tools/wasm/wasm_build.py wasi-threads build ~/git/toywasm/b.thread/toywasm --wasi --wasi-dir . --wasi-dir ~/git/garbage/py/th2 -- builddir/wasi-threads/python.wasm ~/git/garbage/py/th2/thread.py ``` * I used an unreleased version of wasi-sdk * I used toywasm to test because wasmtime doesn't have necessary functionality yet.
1 parent 76efcb4 commit 564fa2b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Python/thread_pthread.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,14 @@ PyThread_exit_thread(void)
356356
{
357357
if (!initialized)
358358
exit(0);
359+
#if defined(__wasi__)
360+
/*
361+
* wasi-threads doesn't have pthread_exit right now
362+
*/
363+
abort();
364+
#else
359365
pthread_exit(0);
366+
#endif
360367
}
361368

362369
#ifdef USE_SEMAPHORES

Tools/wasm/config.site-wasm32-wasi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ ac_cv_header_netpacket_packet_h=no
4040

4141
# Disable int-conversion for wask-sdk as it triggers an error from version 17.
4242
ac_cv_disable_int_conversion=yes
43+
44+
ac_cv_pthread=yes

Tools/wasm/wasm_build.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ def configure_cmd(self) -> List[str]:
480480
cmd.append(f"--{opt}-wasm-dynamic-linking")
481481

482482
if self.pthreads is not None:
483-
assert self.host.is_emscripten
484483
opt = "enable" if self.pthreads else "disable"
485484
cmd.append(f"--{opt}-wasm-pthreads")
486485

@@ -745,6 +744,13 @@ def build_emports(self, force: bool = False):
745744
support_level=SupportLevel.supported,
746745
host=Host.wasm32_wasi,
747746
),
747+
# wasm32-wasi-threads
748+
BuildProfile(
749+
"wasi-threads",
750+
support_level=SupportLevel.experimental,
751+
host=Host.wasm32_wasi,
752+
pthreads=True,
753+
),
748754
# no SDK available yet
749755
# BuildProfile(
750756
# "wasm64-wasi",

configure.ac

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,11 @@ cat > conftest.c <<EOF
10531053
# if defined(__EMSCRIPTEN__)
10541054
wasm32-emscripten
10551055
# elif defined(__wasi__)
1056+
# if defined(_REENTRANT)
1057+
wasm32-wasi-threads
1058+
# else
10561059
wasm32-wasi
1060+
# endif
10571061
# else
10581062
# error unknown wasm32 platform
10591063
# endif
@@ -1245,7 +1249,7 @@ AC_ARG_ENABLE([wasm-pthreads],
12451249
[
12461250
AS_CASE([$ac_sys_system],
12471251
[Emscripten], [],
1248-
[WASI], [AC_MSG_ERROR([WASI threading is not implemented yet.])],
1252+
[WASI], [],
12491253
[AC_MSG_ERROR([--enable-wasm-pthreads only applies to Emscripten and WASI])]
12501254
)
12511255
], [
@@ -2199,6 +2203,15 @@ AS_CASE([$ac_sys_system],
21992203
LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks"
22002204
echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
22012205
2206+
AS_VAR_IF([enable_wasm_pthreads], [yes], [
2207+
# Note: update CFLAGS because ac_compile/ac_link needs this too
2208+
CFLAGS="$CFLAGS -target wasm32-wasi-threads -pthread"
2209+
AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
2210+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
2211+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--import-memory"])
2212+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--max-memory=10485760"])
2213+
])
2214+
22022215
dnl increase initial memory and stack size, move stack first
22032216
dnl https://github.com/WebAssembly/wasi-libc/issues/233
22042217
AS_VAR_APPEND([LDFLAGS_NODIST], [" -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760"])

0 commit comments

Comments
 (0)