Skip to content

Commit aef9180

Browse files
committed
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.
1 parent 6f09f69 commit aef9180

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Include/pyport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,13 @@ extern "C" {
470470
# define WITH_THREAD
471471
#endif
472472

473+
/* Some WebAssembly platforms do not provide a working pthread implementation.
474+
* Thread support is stubbed and any attempt to create a new thread fails.
475+
*/
476+
#if !defined(__wasi__) && (!defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__))
477+
# define Py_CAN_START_THREADS 1
478+
#endif
479+
473480
#ifdef WITH_THREAD
474481
# ifdef Py_BUILD_CORE
475482
# ifdef HAVE_THREAD_LOCAL

Modules/_testinternalcapi/test_critical_sections.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ thread_critical_sections(void *arg)
170170
}
171171
}
172172

173+
#ifdef Py_CAN_START_THREADS
173174
static PyObject *
174175
test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
175176
{
@@ -194,12 +195,15 @@ test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
194195
Py_DECREF(test_data.obj1);
195196
Py_RETURN_NONE;
196197
}
198+
#endif
197199

198200
static PyMethodDef test_methods[] = {
199201
{"test_critical_sections", test_critical_sections, METH_NOARGS},
200202
{"test_critical_sections_nest", test_critical_sections_nest, METH_NOARGS},
201203
{"test_critical_sections_suspend", test_critical_sections_suspend, METH_NOARGS},
204+
#ifdef Py_CAN_START_THREADS
202205
{"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS},
206+
#endif
203207
{NULL, NULL} /* sentinel */
204208
};
205209

0 commit comments

Comments
 (0)