mirror of
https://github.com/python/cpython.git
synced 2025-12-09 02:35:14 +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
|
|
@ -550,16 +550,12 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
|
|||
co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE;
|
||||
co->co_ncellvars = ncellvars;
|
||||
co->co_nfreevars = nfreevars;
|
||||
#ifdef Py_GIL_DISABLED
|
||||
PyMutex_Lock(&interp->func_state.mutex);
|
||||
#endif
|
||||
FT_MUTEX_LOCK(&interp->func_state.mutex);
|
||||
co->co_version = interp->func_state.next_version;
|
||||
if (interp->func_state.next_version != 0) {
|
||||
interp->func_state.next_version++;
|
||||
}
|
||||
#ifdef Py_GIL_DISABLED
|
||||
PyMutex_Unlock(&interp->func_state.mutex);
|
||||
#endif
|
||||
FT_MUTEX_UNLOCK(&interp->func_state.mutex);
|
||||
co->_co_monitoring = NULL;
|
||||
co->_co_instrumentation_version = 0;
|
||||
/* not set */
|
||||
|
|
@ -689,7 +685,7 @@ intern_code_constants(struct _PyCodeConstructor *con)
|
|||
#ifdef Py_GIL_DISABLED
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
struct _py_code_state *state = &interp->code_state;
|
||||
PyMutex_Lock(&state->mutex);
|
||||
FT_MUTEX_LOCK(&state->mutex);
|
||||
#endif
|
||||
if (intern_strings(con->names) < 0) {
|
||||
goto error;
|
||||
|
|
@ -700,15 +696,11 @@ intern_code_constants(struct _PyCodeConstructor *con)
|
|||
if (intern_strings(con->localsplusnames) < 0) {
|
||||
goto error;
|
||||
}
|
||||
#ifdef Py_GIL_DISABLED
|
||||
PyMutex_Unlock(&state->mutex);
|
||||
#endif
|
||||
FT_MUTEX_UNLOCK(&state->mutex);
|
||||
return 0;
|
||||
|
||||
error:
|
||||
#ifdef Py_GIL_DISABLED
|
||||
PyMutex_Unlock(&state->mutex);
|
||||
#endif
|
||||
FT_MUTEX_UNLOCK(&state->mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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