Skip to content

Commit 92c1037

Browse files
authored
gh-84461: Add --enable-wasm-pthreads and more file systems (GH-91820)
1 parent 130a8c3 commit 92c1037

File tree

5 files changed

+161
-63
lines changed

5 files changed

+161
-63
lines changed

Doc/using/configure.rst

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ WebAssemby Options
164164

165165
.. versionadded:: 3.11
166166

167+
.. cmdoption:: --enable-wasm-pthreads
168+
169+
Turn on pthreads support for WASM.
170+
171+
.. versionadded:: 3.11
172+
167173

168174
Install Options
169175
---------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :option:`--enable-wasm-pthreads` to enable pthreads support for WASM
2+
builds. ``Emscripten/node`` no longer has threading enabled by default.
3+
Include additional file systems.

Tools/wasm/README.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,27 @@ functions.
121121
- The ``select`` module is limited. ``select.select()`` crashes the runtime
122122
due to lack of exectfd support.
123123

124-
## processes, threads, signals
124+
## processes, signals
125125

126126
- Processes are not supported. System calls like fork, popen, and subprocess
127127
fail with ``ENOSYS`` or ``ENOSUP``.
128128
- Signal support is limited. ``signal.alarm``, ``itimer``, ``sigaction``
129129
are not available or do not work correctly. ``SIGTERM`` exits the runtime.
130130
- Keyboard interrupt (CTRL+C) handling is not implemented yet.
131-
- Browser builds cannot start new threads. Node's web workers consume
132-
extra file descriptors.
133131
- Resource-related functions like ``os.nice`` and most functions of the
134132
``resource`` module are not available.
135133

134+
## threading
135+
136+
- Threading is disabled by default. The ``configure`` option
137+
``--enable-wasm-pthreads`` adds compiler flag ``-pthread`` and
138+
linker flags ``-sUSE_PTHREADS -sPROXY_TO_PTHREAD``.
139+
- pthread support requires WASM threads and SharedArrayBuffer (bulk memory).
140+
The Node.JS runtime keeps a pool of web workers around. Each web worker
141+
uses several file descriptors (eventfd, epoll, pipe).
142+
- It's not advised to enable threading when building for browsers or with
143+
dynamic linking support; there are performance and stability issues.
144+
136145
## file system
137146

138147
- Most user, group, and permission related function and modules are not
@@ -173,20 +182,16 @@ functions.
173182
distutils, multiprocessing, dbm, tests and similar modules
174183
are not shipped. All other modules are bundled as pre-compiled
175184
``pyc`` files.
176-
- Threading is disabled.
177185
- In-memory file system (MEMFS) is not persistent and limited.
178186
- Test modules are disabled by default. Use ``--enable-test-modules`` build
179187
test modules like ``_testcapi``.
180188

181189
## wasm32-emscripten in node
182190

183-
Node builds use ``NODERAWFS``, ``USE_PTHREADS`` and ``PROXY_TO_PTHREAD``
184-
linker options.
191+
Node builds use ``NODERAWFS``.
185192

186-
- Node RawFS allows direct access to the host file system.
187-
- pthread support requires WASM threads and SharedArrayBuffer (bulk memory).
188-
The runtime keeps a pool of web workers around. Each web worker uses
189-
several file descriptors (eventfd, epoll, pipe).
193+
- Node RawFS allows direct access to the host file system without need to
194+
perform ``FS.mount()`` call.
190195

191196
# Hosting Python WASM builds
192197

configure

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

configure.ac

+57-27
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,21 @@ AC_ARG_ENABLE([wasm-dynamic-linking],
11261126
])
11271127
AC_MSG_RESULT([$enable_wasm_dynamic_linking])
11281128

1129+
AC_MSG_CHECKING([for --enable-wasm-pthreads])
1130+
AC_ARG_ENABLE([wasm-pthreads],
1131+
[AS_HELP_STRING([--enable-wasm-pthreads],
1132+
[Enable pthread emulation for WebAssembly (default is no)])],
1133+
[
1134+
AS_CASE([$ac_sys_system],
1135+
[Emscripten], [],
1136+
[WASI], [AC_MSG_ERROR([WASI threading is not implemented yet.])],
1137+
[AC_MSG_ERROR([--enable-wasm-pthreads only applies to Emscripten and WASI])]
1138+
)
1139+
], [
1140+
enable_wasm_pthreads=missing
1141+
])
1142+
AC_MSG_RESULT([$enable_wasm_pthreads])
1143+
11291144
AC_MSG_CHECKING([for --with-suffix])
11301145
AC_ARG_WITH([suffix],
11311146
[AS_HELP_STRING([--with-suffix=SUFFIX], [set executable suffix to SUFFIX (default is empty, yes is mapped to '.exe')])],
@@ -1909,38 +1924,53 @@ then
19091924
fi
19101925

19111926
# WASM flags
1912-
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
1913-
[Emscripten/browser*], [
1914-
LDFLAGS_NODIST="$LDFLAGS_NODIST -sALLOW_MEMORY_GROWTH"
1915-
LINKFORSHARED="--preload-file=\$(WASM_ASSETS_DIR)"
1927+
AS_CASE([$ac_sys_system],
1928+
[Emscripten], [
1929+
dnl build with WASM debug info if either Py_DEBUG is set or the target is
1930+
dnl node-debug or browser-debug.
1931+
AS_VAR_IF([Py_DEBUG], [yes], [wasm_debug=yes], [wasm_debug=no])
1932+
1933+
dnl Start with 20 MB and allow to grow
1934+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sALLOW_MEMORY_GROWTH -sTOTAL_MEMORY=20971520"])
1935+
1936+
dnl Include file system support
1937+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
1938+
19161939
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
19171940
AS_VAR_APPEND([LINKFORSHARED], [" -sMAIN_MODULE"])
19181941
])
1919-
WASM_ASSETS_DIR=".\$(prefix)"
1920-
WASM_STDLIB="\$(WASM_ASSETS_DIR)/local/lib/python\$(VERSION)/os.py"
1921-
dnl separate-dwarf does not seem to work in Chrome DevTools Support.
1922-
if test "$Py_DEBUG" = 'true' -o "$ac_sys_emscripten_target" = "browser-debug"; then
1923-
LDFLAGS_NODIST="$LDFLAGS_NODIST -sASSERTIONS"
1924-
LINKFORSHARED="$LINKFORSHARED -gsource-map --emit-symbol-map"
1925-
else
1926-
LINKFORSHARED="$LINKFORSHARED -O2 -g0"
1927-
fi
1928-
],
1929-
[Emscripten/node*], [
1930-
LDFLAGS_NODIST="$LDFLAGS_NODIST -sALLOW_MEMORY_GROWTH -sNODERAWFS -sUSE_PTHREADS"
1931-
LINKFORSHARED="-sPROXY_TO_PTHREAD -sEXIT_RUNTIME"
1932-
AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
1933-
AS_VAR_APPEND([LINKFORSHARED], [" -sMAIN_MODULE"])
1942+
1943+
AS_VAR_IF([enable_wasm_pthreads], [yes], [
1944+
AS_VAR_APPEND([CFLAGS_NODIST], [" -pthread"])
1945+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sUSE_PTHREADS"])
1946+
AS_VAR_APPEND([LINKFORSHARED], [" -sPROXY_TO_PTHREAD"])
1947+
])
1948+
1949+
AS_CASE([$ac_sys_emscripten_target],
1950+
[browser*], [
1951+
AS_VAR_IF([ac_sys_emscripten_target], [browser-debug], [wasm_debug=yes])
1952+
AS_VAR_APPEND([LINKFORSHARED], [" --preload-file=\$(WASM_ASSETS_DIR)"])
1953+
WASM_ASSETS_DIR=".\$(prefix)"
1954+
WASM_STDLIB="\$(WASM_ASSETS_DIR)/local/lib/python\$(VERSION)/os.py"
1955+
dnl separate-dwarf does not seem to work in Chrome DevTools Support.
1956+
WASM_LINKFORSHARED_DEBUG="-gsource-map --emit-symbol-map"
1957+
],
1958+
[node*], [
1959+
AS_VAR_IF([ac_sys_emscripten_target], [node-debug], [wasm_debug=yes])
1960+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sALLOW_MEMORY_GROWTH -sNODERAWFS"])
1961+
AS_VAR_APPEND([LINKFORSHARED], [" -sEXIT_RUNTIME"])
1962+
WASM_LINKFORSHARED_DEBUG="-gseparate-dwarf --emit-symbol-map"
1963+
]
1964+
)
1965+
1966+
AS_VAR_IF([wasm_debug], [yes], [
1967+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sASSERTIONS"])
1968+
AS_VAR_APPEND([LINKFORSHARED], [" $WASM_LINKFORSHARED_DEBUG"])
1969+
], [
1970+
AS_VAR_APPEND([LINKFORSHARED], [" -O2 -g0"])
19341971
])
1935-
CFLAGS_NODIST="$CFLAGS_NODIST -pthread"
1936-
if test "$Py_DEBUG" = 'true' -o "$ac_sys_emscripten_target" = "node-debug"; then
1937-
LDFLAGS_NODIST="$LDFLAGS_NODIST -sASSERTIONS"
1938-
LINKFORSHARED="$LINKFORSHARED -gseparate-dwarf --emit-symbol-map"
1939-
else
1940-
LINKFORSHARED="$LINKFORSHARED -O2 -g0"
1941-
fi
19421972
],
1943-
[WASI/*], [
1973+
[WASI], [
19441974
AC_DEFINE([_WASI_EMULATED_SIGNAL], [1], [Define to 1 if you want to emulate signals on WASI])
19451975
AC_DEFINE([_WASI_EMULATED_GETPID], [1], [Define to 1 if you want to emulate getpid() on WASI])
19461976
AC_DEFINE([_WASI_EMULATED_PROCESS_CLOCKS], [1], [Define to 1 if you want to emulate process clocks on WASI])

0 commit comments

Comments
 (0)