From aef9180b2a4b591512b9c85c06f388788fe19d7c Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 9 Nov 2023 10:39:14 -0500 Subject: [PATCH 1/3] gh-111569: Fix critical sections test on WebAssembly 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 | 7 +++++++ Modules/_testinternalcapi/test_critical_sections.c | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/Include/pyport.h b/Include/pyport.h index d30fcd7f6cb7da..d6823711752ea3 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -470,6 +470,13 @@ 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(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)) +# define Py_CAN_START_THREADS 1 +#endif + #ifdef WITH_THREAD # ifdef Py_BUILD_CORE # ifdef HAVE_THREAD_LOCAL diff --git a/Modules/_testinternalcapi/test_critical_sections.c b/Modules/_testinternalcapi/test_critical_sections.c index 238f29c3c62e64..9392096a16d1bf 100644 --- a/Modules/_testinternalcapi/test_critical_sections.c +++ b/Modules/_testinternalcapi/test_critical_sections.c @@ -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 */ }; From fe38ff959c7875f724cc26354739264afae73c4e Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 9 Nov 2023 13:54:18 -0500 Subject: [PATCH 2/3] Use HAVE_PTHREAD_STUBS instead --- Include/pyport.h | 7 ------- Modules/_testinternalcapi/test_critical_sections.c | 4 ++-- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Include/pyport.h b/Include/pyport.h index d6823711752ea3..d30fcd7f6cb7da 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -470,13 +470,6 @@ 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(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)) -# define Py_CAN_START_THREADS 1 -#endif - #ifdef WITH_THREAD # ifdef Py_BUILD_CORE # ifdef HAVE_THREAD_LOCAL diff --git a/Modules/_testinternalcapi/test_critical_sections.c b/Modules/_testinternalcapi/test_critical_sections.c index 9392096a16d1bf..b6d493d2b1b82c 100644 --- a/Modules/_testinternalcapi/test_critical_sections.c +++ b/Modules/_testinternalcapi/test_critical_sections.c @@ -170,7 +170,7 @@ thread_critical_sections(void *arg) } } -#ifdef Py_CAN_START_THREADS +#ifndef HAVE_PTHREAD_STUBS static PyObject * test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args)) { @@ -201,7 +201,7 @@ 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 +#ifndef HAVE_PTHREAD_STUBS {"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS}, #endif {NULL, NULL} /* sentinel */ From 2ac2321dda2f51f7bc00ffa3a51e6dda51a5e368 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 9 Nov 2023 15:09:21 -0500 Subject: [PATCH 3/3] Go back to Py_CAN_START_THREADS --- Include/pyport.h | 8 ++++++++ Modules/_testinternalcapi/test_critical_sections.c | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Include/pyport.h b/Include/pyport.h index d30fcd7f6cb7da..abb526d503fddd 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -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 diff --git a/Modules/_testinternalcapi/test_critical_sections.c b/Modules/_testinternalcapi/test_critical_sections.c index b6d493d2b1b82c..9392096a16d1bf 100644 --- a/Modules/_testinternalcapi/test_critical_sections.c +++ b/Modules/_testinternalcapi/test_critical_sections.c @@ -170,7 +170,7 @@ thread_critical_sections(void *arg) } } -#ifndef HAVE_PTHREAD_STUBS +#ifdef Py_CAN_START_THREADS static PyObject * test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args)) { @@ -201,7 +201,7 @@ 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}, -#ifndef HAVE_PTHREAD_STUBS +#ifdef Py_CAN_START_THREADS {"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS}, #endif {NULL, NULL} /* sentinel */