Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review
  • Loading branch information
corona10 committed Mar 2, 2024
commit 85b419c2b0107e5be77d87fd314ae3213d805753
3 changes: 0 additions & 3 deletions Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ extern PyObject *_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs);
extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp);
extern void _Py_ScheduleGC(PyThreadState *tstate);
extern void _Py_RunGC(PyThreadState *tstate);
#ifdef Py_GIL_DISABLED
extern void _PyGC_Clear_DelayedObjects(PyInterpreterState *interp);
#endif

#ifdef __cplusplus
}
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,6 @@ def gc_collect():
gc.collect()
gc.collect()
gc.collect()
if Py_GIL_DISABLED:
gc._collect_delayed_objects()


@contextlib.contextmanager
Expand Down
28 changes: 1 addition & 27 deletions Modules/clinic/gcmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,6 @@ gc_collect_impl(PyObject *module, int generation)
return _PyGC_Collect(tstate, generation, _Py_GC_REASON_MANUAL);
}

#ifdef Py_GIL_DISABLED
/*[clinic input]
gc._collect_delayed_objects

Process delayed free requests by force

[clinic start generated code]*/

static PyObject *
gc__collect_delayed_objects_impl(PyObject *module)
/*[clinic end generated code: output=a016a10f967d4229 input=1064c31903cd9fac]*/
{
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyGC_Clear_DelayedObjects(interp);
Py_RETURN_NONE;
}
#endif

/*[clinic input]
gc.set_debug

Expand Down Expand Up @@ -526,7 +508,6 @@ static PyMethodDef GcMethods[] = {
GC_FREEZE_METHODDEF
GC_UNFREEZE_METHODDEF
GC_GET_FREEZE_COUNT_METHODDEF
GC__COLLECT_DELAYED_OBJECTS_METHODDEF
{NULL, NULL} /* Sentinel */
};

Expand Down
30 changes: 18 additions & 12 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,23 @@ merge_all_queued_objects(PyInterpreterState *interp, struct collection_state *st
HEAD_UNLOCK(&_PyRuntime);
}

static void
process_delayed_frees(PyInterpreterState *interp)
{
// In STW status, we can observe the latest write sequence by
// advancing the write sequence immediately.
_Py_qsbr_advance(&interp->qsbr);
_PyThreadStateImpl *current_tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
_Py_qsbr_quiescent_state(current_tstate->qsbr);
HEAD_LOCK(&_PyRuntime);
PyThreadState *tstate = interp->threads.head;
while (tstate != NULL) {
_PyMem_ProcessDelayed(tstate);
tstate = (PyThreadState *)tstate->next;
}
HEAD_UNLOCK(&_PyRuntime);
}

// Subtract an incoming reference from the computed "gc_refs" refcount.
static int
visit_decref(PyObject *op, void *arg)
Expand Down Expand Up @@ -1006,6 +1023,7 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state)
_PyEval_StopTheWorld(interp);
// merge refcounts for all queued objects
merge_all_queued_objects(interp, state);
process_delayed_frees(interp);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit/question: _PyEval_StopTheWorld also locks _PyRuntime. Maybe is worth moving the lock up?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PyEval_StopTheWorld locks _PyRuntime temporarily to loop over threads, but does not hold the lock

We want process_delayed_frees after the stop-the-world call because it guarantees that we can free all the "delayed free" pointers. If we moved it up before the _PyEval_StopTheWorld there might be some memory blocks we don't free.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for clarifying

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we moved it up before the _PyEval_StopTheWorld there might be some memory blocks we don't free.

With "moving the lock up" i meant to have a unique locking for both functions, not to move this call before the other. But your comment still applies


// Find unreachable objects
int err = deduce_unreachable_heap(interp, state);
Expand Down Expand Up @@ -1758,16 +1776,4 @@ _PyGC_ClearAllFreeLists(PyInterpreterState *interp)
HEAD_UNLOCK(&_PyRuntime);
}

void
_PyGC_Clear_DelayedObjects(PyInterpreterState *interp)
{
HEAD_LOCK(&_PyRuntime);
PyThreadState *tstate = interp->threads.head;
while (tstate != NULL) {
_PyMem_ProcessDelayed(tstate);
tstate = (PyThreadState *)tstate->next;
}
HEAD_UNLOCK(&_PyRuntime);
}

#endif // Py_GIL_DISABLED