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

@ -114,14 +114,6 @@ NOTE: In the interpreter's initialization phase, some globals are currently
# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
#endif
#ifdef Py_GIL_DISABLED
# define LOCK_INTERNED(interp) PyMutex_Lock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex))
# define UNLOCK_INTERNED(interp) PyMutex_Unlock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex))
#else
# define LOCK_INTERNED(interp)
# define UNLOCK_INTERNED(interp)
#endif
static inline char* _PyUnicode_UTF8(PyObject *op)
{
return FT_ATOMIC_LOAD_PTR_ACQUIRE(_PyCompactUnicodeObject_CAST(op)->utf8);
@ -15988,14 +15980,16 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
/* Do a setdefault on the per-interpreter cache. */
PyObject *interned = get_interned_dict(interp);
assert(interned != NULL);
LOCK_INTERNED(interp);
#ifdef Py_GIL_DISABLED
# define INTERN_MUTEX &_Py_INTERP_CACHED_OBJECT(interp, interned_mutex)
#endif
FT_MUTEX_LOCK(INTERN_MUTEX);
PyObject *t;
{
int res = PyDict_SetDefaultRef(interned, s, s, &t);
if (res < 0) {
PyErr_Clear();
UNLOCK_INTERNED(interp);
FT_MUTEX_UNLOCK(INTERN_MUTEX);
return s;
}
else if (res == 1) {
@ -16005,7 +15999,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
PyUnicode_CHECK_INTERNED(t) == SSTATE_INTERNED_MORTAL) {
immortalize_interned(t);
}
UNLOCK_INTERNED(interp);
FT_MUTEX_UNLOCK(INTERN_MUTEX);
return t;
}
else {
@ -16038,7 +16032,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
immortalize_interned(s);
}
UNLOCK_INTERNED(interp);
FT_MUTEX_UNLOCK(INTERN_MUTEX);
return s;
}