]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101538: Add experimental wasi-threads build (#101537)
authorYAMAMOTO Takashi <yamamoto@midokura.com>
Thu, 22 Jun 2023 18:26:10 +0000 (03:26 +0900)
committerGitHub <noreply@github.com>
Thu, 22 Jun 2023 18:26:10 +0000 (11:26 -0700)
Co-authored-by: Brett Cannon <brett@python.org>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst [new file with mode: 0644]
Python/thread_pthread.h
Tools/wasm/wasm_build.py
configure
configure.ac

diff --git a/Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst b/Misc/NEWS.d/next/Build/2023-02-03-21-36-42.gh-issue-101538.sF5F6S.rst
new file mode 100644 (file)
index 0000000..4b83c30
--- /dev/null
@@ -0,0 +1 @@
+Add experimental wasi-threads support. Patch by Takashi Yamamoto.
index 76d6f3bcdf9c4073718d9e7f0ae495ceaf911a75..f96c57da64636da14671c88f98d7f3b1adedb46a 100644 (file)
@@ -356,7 +356,15 @@ PyThread_exit_thread(void)
 {
     if (!initialized)
         exit(0);
+#if defined(__wasi__)
+    /*
+     * wasi-threads doesn't have pthread_exit right now
+     * cf. https://github.com/WebAssembly/wasi-threads/issues/7
+     */
+    abort();
+#else
     pthread_exit(0);
+#endif
 }
 
 #ifdef USE_SEMAPHORES
index 241a5d4eed5ae840fcb62ef4743ac90045a214d2..c9947057a90754b9c18ed8af304aca997ed5dcec 100755 (executable)
@@ -480,7 +480,6 @@ class BuildProfile:
             cmd.append(f"--{opt}-wasm-dynamic-linking")
 
         if self.pthreads is not None:
-            assert self.host.is_emscripten
             opt = "enable" if self.pthreads else "disable"
             cmd.append(f"--{opt}-wasm-pthreads")
 
@@ -745,6 +744,13 @@ _profiles = [
         support_level=SupportLevel.supported,
         host=Host.wasm32_wasi,
     ),
+    # wasm32-wasi-threads
+    BuildProfile(
+        "wasi-threads",
+        support_level=SupportLevel.experimental,
+        host=Host.wasm32_wasi,
+        pthreads=True,
+    ),
     # no SDK available yet
     # BuildProfile(
     #    "wasm64-wasi",
index fdc7c22531785a4efbcb4d2292a3ad652fd75ce2..e33bf77779e6e2104e3cf46287953512409a4ada 100755 (executable)
--- a/configure
+++ b/configure
@@ -6874,7 +6874,11 @@ cat > conftest.c <<EOF
 #  if defined(__EMSCRIPTEN__)
        wasm32-emscripten
 #  elif defined(__wasi__)
+#    if defined(_REENTRANT)
+       wasm32-wasi-threads
+#    else
        wasm32-wasi
+#    endif
 #  else
 #    error unknown wasm32 platform
 #  endif
@@ -7160,7 +7164,7 @@ then :
   Emscripten) :
      ;; #(
   WASI) :
-    as_fn_error $? "WASI threading is not implemented yet." "$LINENO" 5 ;; #(
+     ;; #(
   *) :
     as_fn_error $? "--enable-wasm-pthreads only applies to Emscripten and WASI" "$LINENO" 5
    ;;
@@ -9394,6 +9398,21 @@ printf "%s\n" "#define _WASI_EMULATED_PROCESS_CLOCKS 1" >>confdefs.h
     LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks"
     echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
 
+    if test "x$enable_wasm_pthreads" = xyes
+then :
+
+      # Note: update CFLAGS because ac_compile/ac_link needs this too.
+      # without this, configure fails to find pthread_create, sem_init,
+      # etc because they are only available in the sysroot for
+      # wasm32-wasi-threads.
+      as_fn_append CFLAGS " -target wasm32-wasi-threads -pthread"
+      as_fn_append CFLAGS_NODIST " -target wasm32-wasi-threads -pthread"
+      as_fn_append LDFLAGS_NODIST " -target wasm32-wasi-threads -pthread"
+      as_fn_append LDFLAGS_NODIST " -Wl,--import-memory"
+      as_fn_append LDFLAGS_NODIST " -Wl,--max-memory=10485760"
+
+fi
+
             as_fn_append LDFLAGS_NODIST " -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760"
 
  ;; #(
index a24cd689730d7a277044f57a004cf097bdbe4d8b..8f097c4167759846758a5ecb44b5130c6fa2a7c9 100644 (file)
@@ -1079,7 +1079,11 @@ cat > conftest.c <<EOF
 #  if defined(__EMSCRIPTEN__)
        wasm32-emscripten
 #  elif defined(__wasi__)
+#    if defined(_REENTRANT)
+       wasm32-wasi-threads
+#    else
        wasm32-wasi
+#    endif
 #  else
 #    error unknown wasm32 platform
 #  endif
@@ -1272,7 +1276,7 @@ AC_ARG_ENABLE([wasm-pthreads],
 [
   AS_CASE([$ac_sys_system],
     [Emscripten], [],
-    [WASI], [AC_MSG_ERROR([WASI threading is not implemented yet.])],
+    [WASI], [],
     [AC_MSG_ERROR([--enable-wasm-pthreads only applies to Emscripten and WASI])]
   )
 ], [
@@ -2309,6 +2313,18 @@ AS_CASE([$ac_sys_system],
     LIBS="$LIBS -lwasi-emulated-signal -lwasi-emulated-getpid -lwasi-emulated-process-clocks"
     echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
 
+    AS_VAR_IF([enable_wasm_pthreads], [yes], [
+      # Note: update CFLAGS because ac_compile/ac_link needs this too.
+      # without this, configure fails to find pthread_create, sem_init,
+      # etc because they are only available in the sysroot for
+      # wasm32-wasi-threads.
+      AS_VAR_APPEND([CFLAGS], [" -target wasm32-wasi-threads -pthread"])
+      AS_VAR_APPEND([CFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
+      AS_VAR_APPEND([LDFLAGS_NODIST], [" -target wasm32-wasi-threads -pthread"])
+      AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--import-memory"])
+      AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--max-memory=10485760"])
+    ])
+
     dnl increase initial memory and stack size, move stack first
     dnl https://github.com/WebAssembly/wasi-libc/issues/233
     AS_VAR_APPEND([LDFLAGS_NODIST], [" -z stack-size=524288 -Wl,--stack-first -Wl,--initial-memory=10485760"])