Skip to content

Commit d8f87cd

Browse files
yamtbrettcannonerlend-aasland
authored
gh-101538: Add experimental wasi-threads build (#101537)
Co-authored-by: Brett Cannon <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 13237a2 commit d8f87cd

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental wasi-threads support. Patch by Takashi Yamamoto.

Python/thread_pthread.h

+8
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,15 @@ 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+
* cf. https://github.com/WebAssembly/wasi-threads/issues/7
363+
*/
364+
abort();
365+
#else
359366
pthread_exit(0);
367+
#endif
360368
}
361369

362370
#ifdef USE_SEMAPHORES

Tools/wasm/wasm_build.py

+7-1
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

+20-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+17-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,11 @@ cat > conftest.c <<EOF
10791079
# if defined(__EMSCRIPTEN__)
10801080
wasm32-emscripten
10811081
# elif defined(__wasi__)
1082+
# if defined(_REENTRANT)
1083+
wasm32-wasi-threads
1084+
# else
10821085
wasm32-wasi
1086+
# endif
10831087
# else
10841088
# error unknown wasm32 platform
10851089
# endif
@@ -1272,7 +1276,7 @@ AC_ARG_ENABLE([wasm-pthreads],
12721276
[
12731277
AS_CASE([$ac_sys_system],
12741278
[Emscripten], [],
1275-
[WASI], [AC_MSG_ERROR([WASI threading is not implemented yet.])],
1279+
[WASI], [],
12761280
[AC_MSG_ERROR([--enable-wasm-pthreads only applies to Emscripten and WASI])]
12771281
)
12781282
], [
@@ -2309,6 +2313,18 @@ AS_CASE([$ac_sys_system],
23092313
LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks"
23102314
echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
23112315
2316+
AS_VAR_IF([enable_wasm_pthreads], [yes], [
2317+
# Note: update CFLAGS because ac_compile/ac_link needs this too.
2318+
# without this, configure fails to find pthread_create, sem_init,
2319+
# etc because they are only available in the sysroot for
2320+
# wasm32-wasi-threads.
2321+
AS_VAR_APPEND([CFLAGS], [" -target wasm32-wasi-threads -pthread"])
2322+
AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
2323+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
2324+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--import-memory"])
2325+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--max-memory=10485760"])
2326+
])
2327+
23122328
dnl increase initial memory and stack size, move stack first
23132329
dnl https://github.com/WebAssembly/wasi-libc/issues/233
23142330
AS_VAR_APPEND([LDFLAGS_NODIST], [" -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760"])

0 commit comments

Comments
 (0)