summaryrefslogtreecommitdiffstats
path: root/Include/internal
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-01-19 23:04:14 (GMT)
committerGitHub <noreply@github.com>2023-01-19 23:04:14 (GMT)
commit6036c3e856f033bf13e929536e7bf127fdd921c9 (patch)
tree2e0f92c60f4d860826e6e224e6c073e220d76386 /Include/internal
parent8a2d4f4e8eea86352de37d2ce28117e13b3dfaed (diff)
downloadcpython-6036c3e856f033bf13e929536e7bf127fdd921c9.zip
cpython-6036c3e856f033bf13e929536e7bf127fdd921c9.tar.gz
cpython-6036c3e856f033bf13e929536e7bf127fdd921c9.tar.bz2
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
Diffstat (limited to 'Include/internal')
-rw-r--r--Include/internal/pycore_pylifecycle.h2
-rw-r--r--Include/internal/pycore_pystate.h21
-rw-r--r--Include/internal/pycore_runtime.h10
-rw-r--r--Include/internal/pycore_runtime_init.h6
4 files changed, 26 insertions, 13 deletions
diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h
index 370e4cb..2d431be 100644
--- a/Include/internal/pycore_pylifecycle.h
+++ b/Include/internal/pycore_pylifecycle.h
@@ -69,7 +69,7 @@ extern void _PyThread_FiniType(PyInterpreterState *interp);
extern void _Py_Deepfreeze_Fini(void);
extern void _PyArg_Fini(void);
-extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime);
+extern PyStatus _PyGILState_Init(PyInterpreterState *interp);
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
extern void _PyGILState_Fini(PyInterpreterState *interp);
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index 736c2b3..0e46693 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -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
diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h
index d100e83..41e1b2c 100644
--- a/Include/internal/pycore_runtime.h
+++ b/Include/internal/pycore_runtime.h
@@ -41,15 +41,11 @@ struct _gilstate_runtime_state {
/* bpo-26558: Flag to disable PyGILState_Check().
If set to non-zero, PyGILState_Check() always return 1. */
int check_enabled;
- /* Assuming the current thread holds the GIL, this is the
- PyThreadState for the current thread. */
- _Py_atomic_address tstate_current;
/* The single PyInterpreterState used by this process'
GILState implementation
*/
/* TODO: Given interp_main, it may be possible to kill this ref */
PyInterpreterState *autoInterpreterState;
- Py_tss_t autoTSSkey;
};
/* Runtime audit hook state */
@@ -124,6 +120,12 @@ typedef struct pyruntimestate {
unsigned long main_thread;
+ /* Assuming the current thread holds the GIL, this is the
+ PyThreadState for the current thread. */
+ _Py_atomic_address tstate_current;
+ /* Used for the thread state bound to the current thread. */
+ Py_tss_t autoTSSkey;
+
PyWideStringList orig_argv;
struct _parser_runtime_state parser;
diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h
index cb3fce3..f10dccc 100644
--- a/Include/internal/pycore_runtime_init.h
+++ b/Include/internal/pycore_runtime_init.h
@@ -33,6 +33,9 @@ extern "C" {
until _PyInterpreterState_Enable() is called. */ \
.next_id = -1, \
}, \
+ /* A TSS key must be initialized with Py_tss_NEEDS_INIT \
+ in accordance with the specification. */ \
+ .autoTSSkey = Py_tss_NEEDS_INIT, \
.parser = _parser_runtime_state_INIT, \
.imports = { \
.lock = { \
@@ -49,9 +52,6 @@ extern "C" {
}, \
.gilstate = { \
.check_enabled = 1, \
- /* A TSS key must be initialized with Py_tss_NEEDS_INIT \
- in accordance with the specification. */ \
- .autoTSSkey = Py_tss_NEEDS_INIT, \
}, \
.dtoa = _dtoa_runtime_state_INIT(runtime), \
.fileutils = { \