summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-10 14:40:06 (GMT)
committerGitHub <noreply@github.com>2024-05-10 14:40:06 (GMT)
commit4480dd86d917dffbf020e68446579bfa48c8eb25 (patch)
tree2492a8686ac0ee5a61d831afae4f0807cd609568 /Python
parentbb5bf2422618506dca45912bd174d02b40b4113c (diff)
downloadcpython-4480dd86d917dffbf020e68446579bfa48c8eb25.zip
cpython-4480dd86d917dffbf020e68446579bfa48c8eb25.tar.gz
cpython-4480dd86d917dffbf020e68446579bfa48c8eb25.tar.bz2
[3.13] gh-117657: Fix data races reported by TSAN on `interp->threads.main` (GH-118865) (#118904)
Use relaxed loads/stores when reading/writing to this field. (cherry picked from commit 22d5185308f85efa22ec1e8251c409fe1cbd9e6b) Co-authored-by: mpage <mpage@meta.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/pystate.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index b1e085b..de6a768 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1038,6 +1038,17 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
}
#endif
+static inline void
+set_main_thread(PyInterpreterState *interp, PyThreadState *tstate)
+{
+ _Py_atomic_store_ptr_relaxed(&interp->threads.main, tstate);
+}
+
+static inline PyThreadState *
+get_main_thread(PyInterpreterState *interp)
+{
+ return _Py_atomic_load_ptr_relaxed(&interp->threads.main);
+}
int
_PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
@@ -1052,21 +1063,22 @@ _PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
"current tstate has wrong interpreter");
return -1;
}
- interp->threads.main = tstate;
+ set_main_thread(interp, tstate);
+
return 0;
}
void
_PyInterpreterState_SetNotRunningMain(PyInterpreterState *interp)
{
- assert(interp->threads.main == current_fast_get());
- interp->threads.main = NULL;
+ assert(get_main_thread(interp) == current_fast_get());
+ set_main_thread(interp, NULL);
}
int
_PyInterpreterState_IsRunningMain(PyInterpreterState *interp)
{
- if (interp->threads.main != NULL) {
+ if (get_main_thread(interp) != NULL) {
return 1;
}
// Embedders might not know to call _PyInterpreterState_SetRunningMain(),
@@ -1082,18 +1094,15 @@ int
_PyThreadState_IsRunningMain(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
- if (interp->threads.main != NULL) {
- return tstate == interp->threads.main;
- }
// See the note in _PyInterpreterState_IsRunningMain() about
// possible false negatives here for embedders.
- return 0;
+ return get_main_thread(interp) == tstate;
}
int
_PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp)
{
- if (interp->threads.main != NULL) {
+ if (get_main_thread(interp) != NULL) {
PyErr_SetString(PyExc_InterpreterError,
"interpreter already running");
return -1;
@@ -1105,8 +1114,8 @@ void
_PyInterpreterState_ReinitRunningMain(PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
- if (interp->threads.main != tstate) {
- interp->threads.main = NULL;
+ if (get_main_thread(interp) != tstate) {
+ set_main_thread(interp, NULL);
}
}