gh-111968: Refactor _PyXXX_Fini to integrate with _PyObject_ClearFreeLists (gh-114899)

This commit is contained in:
Donghee Na 2024-02-10 09:57:04 +09:00 committed by GitHub
parent 564385612c
commit d4d5bae147
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 38 additions and 107 deletions

View file

@ -1284,17 +1284,6 @@ _PyContext_ClearFreeList(_PyFreeListState *freelist_state, int is_finalization)
}
void
_PyContext_Fini(_PyFreeListState *state)
{
// With Py_GIL_DISABLED:
// the freelists for the current thread state have already been cleared.
#ifndef Py_GIL_DISABLED
_PyContext_ClearFreeList(state, 1);
#endif
}
PyStatus
_PyContext_Init(PyInterpreterState *interp)
{

View file

@ -1721,7 +1721,7 @@ _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
HEAD_LOCK(&_PyRuntime);
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)interp->threads.head;
while (tstate != NULL) {
_Py_ClearFreeLists(&tstate->freelist_state, 0);
_PyObject_ClearFreeLists(&tstate->freelist_state, 0);
tstate = (_PyThreadStateImpl *)tstate->base.next;
}
HEAD_UNLOCK(&_PyRuntime);

View file

@ -11,7 +11,7 @@
void
_PyGC_ClearAllFreeLists(PyInterpreterState *interp)
{
_Py_ClearFreeLists(&interp->freelist_state, 0);
_PyObject_ClearFreeLists(&interp->freelist_state, 0);
}
#endif

View file

@ -1790,16 +1790,14 @@ finalize_interp_types(PyInterpreterState *interp)
// a dict internally.
_PyUnicode_ClearInterned(interp);
_PyDict_Fini(interp);
_PyUnicode_Fini(interp);
#ifndef Py_GIL_DISABLED
// With Py_GIL_DISABLED:
// the freelists for the current thread state have already been cleared.
_PyFreeListState *state = _PyFreeListState_GET();
_PyTuple_Fini(state);
_PyList_Fini(state);
_PyFloat_Fini(state);
_PySlice_Fini(state);
_PyContext_Fini(state);
_PyAsyncGen_Fini(state);
_PyObject_ClearFreeLists(state, 1);
#endif
#ifdef Py_DEBUG
_PyStaticObjects_CheckRefcnt(interp);

View file

@ -1468,20 +1468,6 @@ clear_datastack(PyThreadState *tstate)
}
}
void
_Py_ClearFreeLists(_PyFreeListState *state, int is_finalization)
{
// In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
// In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
_PyFloat_ClearFreeList(state, is_finalization);
_PyTuple_ClearFreeList(state, is_finalization);
_PyList_ClearFreeList(state, is_finalization);
_PyDict_ClearFreeList(state, is_finalization);
_PyContext_ClearFreeList(state, is_finalization);
_PyAsyncGen_ClearFreeLists(state, is_finalization);
_PyObjectStackChunk_ClearFreeList(state, is_finalization);
}
void
PyThreadState_Clear(PyThreadState *tstate)
{
@ -1566,9 +1552,8 @@ PyThreadState_Clear(PyThreadState *tstate)
}
#ifdef Py_GIL_DISABLED
// Each thread should clear own freelists in free-threading builds.
_PyFreeListState *freelist_state = &((_PyThreadStateImpl*)tstate)->freelist_state;
_Py_ClearFreeLists(freelist_state, 1);
_PySlice_ClearCache(freelist_state);
_PyFreeListState *freelist_state = _PyFreeListState_GET();
_PyObject_ClearFreeLists(freelist_state, 1);
// Remove ourself from the biased reference counting table of threads.
_Py_brc_remove_thread(tstate);