summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-06 23:24:23 (GMT)
committerGitHub <noreply@github.com>2020-03-06 23:24:23 (GMT)
commit7b3c252dc7f44d4bdc4c7c82d225ebd09c78f520 (patch)
tree39b9496171733c94644e7a2671878fc3452526b8 /Include
parent557287075c264d2458cd3e1b45e9b8ee5341e0a1 (diff)
downloadcpython-7b3c252dc7f44d4bdc4c7c82d225ebd09c78f520.zip
cpython-7b3c252dc7f44d4bdc4c7c82d225ebd09c78f520.tar.gz
cpython-7b3c252dc7f44d4bdc4c7c82d225ebd09c78f520.tar.bz2
bpo-39877: _PyRuntimeState.finalizing becomes atomic (GH-18816)
Convert _PyRuntimeState.finalizing field to an atomic variable: * Rename it to _finalizing * Change its type to _Py_atomic_address * Add _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing() functions * Remove _Py_CURRENTLY_FINALIZING() function: replace it with testing directly _PyRuntimeState_GetFinalizing() value Convert _PyRuntimeState_GetThreadState() to static inline function.
Diffstat (limited to 'Include')
-rw-r--r--Include/internal/pycore_pystate.h23
1 files changed, 17 insertions, 6 deletions
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index 405efb9..b5f5095 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -223,8 +223,11 @@ typedef struct pyruntimestate {
int initialized;
/* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize()
- is called again. */
- PyThreadState *finalizing;
+ is called again.
+
+ Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing()
+ to access it, don't access it directly. */
+ _Py_atomic_address _finalizing;
struct pyinterpreters {
PyThread_type_lock mutex;
@@ -279,8 +282,15 @@ PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void);
PyAPI_FUNC(void) _PyRuntime_Finalize(void);
-#define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
- (runtime->finalizing == tstate)
+static inline PyThreadState*
+_PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) {
+ return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->_finalizing);
+}
+
+static inline void
+_PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
+ _Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate);
+}
PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
@@ -288,8 +298,9 @@ PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
/* Variable and macro for in-line access to current thread
and interpreter state */
-#define _PyRuntimeState_GetThreadState(runtime) \
- ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current))
+static inline PyThreadState* _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime) {
+ return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current);
+}
/* Get the current Python thread state.