gh-59956: Clarify GILState-related Code (gh-101161)

The objective of this change is to help make the GILState-related code easier to understand.  This mostly involves moving code around and some semantically equivalent refactors.  However, there are a also a small number of slight changes in structure and behavior:

* tstate_current is moved out of _PyRuntimeState.gilstate
* autoTSSkey is moved out of _PyRuntimeState.gilstate
* autoTSSkey is initialized earlier
* autoTSSkey is re-initialized (after fork) earlier

https://github.com/python/cpython/issues/59956
This commit is contained in:
Eric Snow 2023-01-19 16:04:14 -07:00 committed by GitHub
parent 8a2d4f4e8e
commit 6036c3e856
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 373 additions and 250 deletions

View file

@ -67,12 +67,12 @@ _Py_ThreadCanHandlePendingCalls(void)
static inline PyThreadState*
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
{
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current);
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->tstate_current);
}
/* Get the current Python thread state.
Efficient macro reading directly the 'gilstate.tstate_current' atomic
Efficient macro reading directly the 'tstate_current' atomic
variable. The macro is unsafe: it does not check for error and it can
return NULL.
@ -120,7 +120,7 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
// PyThreadState functions
PyAPI_FUNC(void) _PyThreadState_SetCurrent(PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_Bind(PyThreadState *tstate);
// We keep this around exclusively for stable ABI compatibility.
PyAPI_FUNC(void) _PyThreadState_Init(
PyThreadState *tstate);
@ -139,17 +139,28 @@ _PyThreadState_UpdateTracingState(PyThreadState *tstate)
}
/* PyThreadState status */
#define PyThreadState_UNINITIALIZED 0
/* Has been initialized to a safe state.
In order to be effective, this must be set to 0 during or right
after allocation. */
#define PyThreadState_INITIALIZED 1
#define PyThreadState_BOUND 2
#define PyThreadState_UNBOUND 3
/* Other */
PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
struct _gilstate_runtime_state *gilstate,
_PyRuntimeState *runtime,
PyThreadState *newts);
PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
#ifdef HAVE_FORK
extern PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
extern PyStatus _PyGILState_Reinit(_PyRuntimeState *runtime);
extern void _PySignal_AfterFork(void);
#endif