]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111569: Fix critical sections test on WebAssembly (GH-111897)
authorSam Gross <colesbury@gmail.com>
Thu, 9 Nov 2023 23:37:11 +0000 (18:37 -0500)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 23:37:11 +0000 (15:37 -0800)
This adds a macro `Py_CAN_START_THREADS` that corresponds to the Python
function `test.support.threading_helper.can_start_thread()`. WASI and
some Emscripten builds do not have a working pthread implementation.

This macro is used to guard the critical sections C API tests that
require a working threads implementation.

Include/pyport.h
Modules/_testinternalcapi/test_critical_sections.c

index d30fcd7f6cb7da880f9b76a7f44e118ca0c7ced7..abb526d503fddd737bf420ad279f13978951519f 100644 (file)
@@ -470,6 +470,14 @@ extern "C" {
 #  define WITH_THREAD
 #endif
 
+/* Some WebAssembly platforms do not provide a working pthread implementation.
+ * Thread support is stubbed and any attempt to create a new thread fails.
+ */
+#if (!defined(HAVE_PTHREAD_STUBS) && \
+      (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)))
+#  define Py_CAN_START_THREADS 1
+#endif
+
 #ifdef WITH_THREAD
 #  ifdef Py_BUILD_CORE
 #    ifdef HAVE_THREAD_LOCAL
index 238f29c3c62e643b4f3dc90e3c1fba4c14a3b131..9392096a16d1bf4278ae862cc1557bc23764b725 100644 (file)
@@ -170,6 +170,7 @@ thread_critical_sections(void *arg)
     }
 }
 
+#ifdef Py_CAN_START_THREADS
 static PyObject *
 test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
 {
@@ -194,12 +195,15 @@ test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
     Py_DECREF(test_data.obj1);
     Py_RETURN_NONE;
 }
+#endif
 
 static PyMethodDef test_methods[] = {
     {"test_critical_sections", test_critical_sections, METH_NOARGS},
     {"test_critical_sections_nest", test_critical_sections_nest, METH_NOARGS},
     {"test_critical_sections_suspend", test_critical_sections_suspend, METH_NOARGS},
+#ifdef Py_CAN_START_THREADS
     {"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS},
+#endif
     {NULL, NULL} /* sentinel */
 };