mirror of
https://github.com/python/cpython.git
synced 2025-10-23 09:53:47 +00:00
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:
parent
dec624e0af
commit
082f370cdd
9 changed files with 44 additions and 88 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue