Skip to content

Commit 3320241

Browse files
committed
Use a dedicated heap allocation for interpreter guards.
1 parent 8bf1899 commit 3320241

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

Python/pystate.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,7 @@ static void
16181618
add_threadstate(PyInterpreterState *interp, PyThreadState *tstate,
16191619
PyThreadState *next)
16201620
{
1621+
assert(interp != NULL);
16211622
assert(interp->threads.head != tstate);
16221623
if (next != NULL) {
16231624
assert(next->prev == NULL || next->prev == tstate);
@@ -3355,9 +3356,17 @@ try_acquire_interp_guard(PyInterpreterState *interp)
33553356
return NULL;
33563357
}
33573358

3359+
PyInterpreterGuard *guard = PyMem_RawMalloc(sizeof(PyInterpreterGuard));
3360+
if (guard == NULL) {
3361+
_PyRWMutex_RUnlock(&interp->finalization_guards.lock);
3362+
return NULL;
3363+
}
3364+
33583365
_Py_atomic_add_ssize(&interp->finalization_guards.countdown, 1);
33593366
_PyRWMutex_RUnlock(&interp->finalization_guards.lock);
3360-
return (PyInterpreterGuard *)interp;
3367+
3368+
guard->interp = interp;
3369+
return guard;
33613370
}
33623371

33633372
PyInterpreterGuard *
@@ -3391,6 +3400,8 @@ PyInterpreterGuard_Close(PyInterpreterGuard *guard)
33913400
Py_FatalError("interpreter has negative guard count, likely due"
33923401
" to an extra PyInterpreterGuard_Close() call");
33933402
}
3403+
3404+
PyMem_RawFree(guard);
33943405
}
33953406

33963407
PyInterpreterView *
@@ -3440,6 +3451,8 @@ PyInterpreterGuard_FromView(PyInterpreterView *view)
34403451

34413452
PyInterpreterGuard *guard = try_acquire_interp_guard(interp);
34423453
HEAD_UNLOCK(runtime);
3454+
3455+
assert(guard == NULL || guard->interp != NULL);
34433456
return guard;
34443457
}
34453458

0 commit comments

Comments
 (0)