gh-137514: Add a free-threading wrapper for mutexes (GH-137515)

Add `FT_MUTEX_LOCK`/`FT_MUTEX_UNLOCK`, which call `PyMutex_Lock` and `PyMutex_Unlock` on the free-threaded build, and no-op otherwise.
This commit is contained in:
Peter Bierma 2025-08-07 11:24:50 -04:00 committed by GitHub
parent dec624e0af
commit 082f370cdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 44 additions and 88 deletions

View file

@ -907,13 +907,9 @@ unsignal_pending_calls(PyThreadState *tstate, PyInterpreterState *interp)
static void
clear_pending_handling_thread(struct _pending_calls *pending)
{
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&pending->mutex);
FT_MUTEX_LOCK(&pending->mutex);
pending->handling_thread = NULL;
PyMutex_Unlock(&pending->mutex);
#else
pending->handling_thread = NULL;
#endif
FT_MUTEX_UNLOCK(&pending->mutex);
}
static int

View file

@ -16,7 +16,7 @@ Copyright (c) Corporation for National Research Initiatives.
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include "pycore_unicodeobject.h" // _PyUnicode_InternMortal()
#include "pycore_pyatomic_ft_wrappers.h"
static const char *codecs_builtin_error_handlers[] = {
"strict", "ignore", "replace",
@ -40,13 +40,10 @@ int PyCodec_Register(PyObject *search_function)
PyErr_SetString(PyExc_TypeError, "argument must be callable");
goto onError;
}
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&interp->codecs.search_path_mutex);
#endif
FT_MUTEX_LOCK(&interp->codecs.search_path_mutex);
int ret = PyList_Append(interp->codecs.search_path, search_function);
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&interp->codecs.search_path_mutex);
#endif
FT_MUTEX_UNLOCK(&interp->codecs.search_path_mutex);
return ret;
onError:
@ -66,9 +63,7 @@ PyCodec_Unregister(PyObject *search_function)
PyObject *codec_search_path = interp->codecs.search_path;
assert(PyList_CheckExact(codec_search_path));
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(codec_search_path); i++) {
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&interp->codecs.search_path_mutex);
#endif
FT_MUTEX_LOCK(&interp->codecs.search_path_mutex);
PyObject *item = PyList_GetItemRef(codec_search_path, i);
int ret = 1;
if (item == search_function) {
@ -76,9 +71,7 @@ PyCodec_Unregister(PyObject *search_function)
// while we hold search_path_mutex.
ret = PyList_SetSlice(codec_search_path, i, i+1, NULL);
}
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&interp->codecs.search_path_mutex);
#endif
FT_MUTEX_UNLOCK(&interp->codecs.search_path_mutex);
Py_DECREF(item);
if (ret != 1) {
assert(interp->codecs.search_cache != NULL);

View file

@ -20,13 +20,6 @@ typedef struct _PyLegacyEventHandler {
#define _PyLegacyEventHandler_CAST(op) ((_PyLegacyEventHandler *)(op))
#ifdef Py_GIL_DISABLED
#define LOCK_SETUP() PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex);
#define UNLOCK_SETUP() PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex);
#else
#define LOCK_SETUP()
#define UNLOCK_SETUP()
#endif
/* The Py_tracefunc function expects the following arguments:
* obj: the trace object (PyObject *)
* frame: the current frame (PyFrameObject *)
@ -509,9 +502,9 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
// needs to be decref'd outside of the lock
PyObject *old_profileobj;
LOCK_SETUP();
FT_MUTEX_LOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
Py_ssize_t profiling_threads = setup_profile(tstate, func, arg, &old_profileobj);
UNLOCK_SETUP();
FT_MUTEX_UNLOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
Py_XDECREF(old_profileobj);
uint32_t events = 0;
@ -605,10 +598,10 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
}
// needs to be decref'd outside of the lock
PyObject *old_traceobj;
LOCK_SETUP();
FT_MUTEX_LOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
assert(tstate->interp->sys_tracing_threads >= 0);
Py_ssize_t tracing_threads = setup_tracing(tstate, func, arg, &old_traceobj);
UNLOCK_SETUP();
FT_MUTEX_UNLOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
Py_XDECREF(old_traceobj);
if (tracing_threads < 0) {
return -1;

View file

@ -1689,9 +1689,7 @@ PyThreadState_Clear(PyThreadState *tstate)
"PyThreadState_Clear: warning: thread still has a generator\n");
}
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex);
#endif
FT_MUTEX_LOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
if (tstate->c_profilefunc != NULL) {
tstate->interp->sys_profiling_threads--;
@ -1702,9 +1700,7 @@ PyThreadState_Clear(PyThreadState *tstate)
tstate->c_tracefunc = NULL;
}
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex);
#endif
FT_MUTEX_UNLOCK(&_PyRuntime.ceval.sys_trace_profile_mutex);
Py_CLEAR(tstate->c_profileobj);
Py_CLEAR(tstate->c_traceobj);