summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c6
-rw-r--r--Python/gc_free_threading.c14
-rw-r--r--Python/pystate.c4
3 files changed, 11 insertions, 13 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index d192d5b..351fc84 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -870,15 +870,15 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
// gh-118527: Disable immortalization of code constants for explicit
// compile() calls to get consistent frozen outputs between the default
// and free-threaded builds.
+ // Subtract two to suppress immortalization (so that 1 -> -1)
PyInterpreterState *interp = _PyInterpreterState_GET();
- int old_value = interp->gc.immortalize.enable_on_thread_created;
- interp->gc.immortalize.enable_on_thread_created = 0;
+ _Py_atomic_add_int(&interp->gc.immortalize, -2);
#endif
result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
#ifdef Py_GIL_DISABLED
- interp->gc.immortalize.enable_on_thread_created = old_value;
+ _Py_atomic_add_int(&interp->gc.immortalize, 2);
#endif
Py_XDECREF(source_copy);
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index e6bd012..d005b79 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -703,11 +703,9 @@ _PyGC_Init(PyInterpreterState *interp)
{
GCState *gcstate = &interp->gc;
- if (_Py_IsMainInterpreter(interp)) {
- // gh-117783: immortalize objects that would use deferred refcounting
- // once the first non-main thread is created.
- gcstate->immortalize.enable_on_thread_created = 1;
- }
+ // gh-117783: immortalize objects that would use deferred refcounting
+ // once the first non-main thread is created (but not in subinterpreters).
+ gcstate->immortalize = _Py_IsMainInterpreter(interp) ? 0 : -1;
gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) {
@@ -1808,8 +1806,10 @@ _PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp)
{
struct visitor_args args;
_PyEval_StopTheWorld(interp);
- gc_visit_heaps(interp, &immortalize_visitor, &args);
- interp->gc.immortalize.enabled = 1;
+ if (interp->gc.immortalize == 0) {
+ gc_visit_heaps(interp, &immortalize_visitor, &args);
+ interp->gc.immortalize = 1;
+ }
_PyEval_StartTheWorld(interp);
}
diff --git a/Python/pystate.c b/Python/pystate.c
index 36e4206..d029391 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1583,9 +1583,7 @@ new_threadstate(PyInterpreterState *interp, int whence)
}
else {
#ifdef Py_GIL_DISABLED
- if (interp->gc.immortalize.enable_on_thread_created &&
- !interp->gc.immortalize.enabled)
- {
+ if (_Py_atomic_load_int(&interp->gc.immortalize) == 0) {
// Immortalize objects marked as using deferred reference counting
// the first time a non-main thread is created.
_PyGC_ImmortalizeDeferredObjects(interp);