-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-112529: Use atomic operations for gcstate->collecting
#112533
Conversation
The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
@@ -2271,10 +2262,6 @@ PyObject_IS_GC(PyObject *obj) | |||
void | |||
_Py_ScheduleGC(PyInterpreterState *interp) | |||
{ | |||
GCState *gcstate = &interp->gc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi: _Py_ScheduleGC
not public and only called from _PyObject_GC_Link, which already checks gcstate->collecting
.
@pablogsal, would you be able to review this? The important bit is the atomically setting cc @nascheme in case you have time and are interested in the GC changes, I would appreciate your feedback as well. |
will review this week 👍 |
@pablogsal - gentle reminder, this is awaiting your review |
This looks okay to me. It's a bit hard to see from the diff which code has been moved vs what's been changed. However, it seems to be mostly re-organization with essentially no behaviour change, aside from using the atomics. I'd say it can be merged. |
Modules/gcmodule.c
Outdated
{ | ||
GC_STAT_ADD(generation, collections, 1); | ||
#ifdef Py_STATS | ||
if (_Py_stats) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't object_visits
be zeroed after the _Py_atomic_compare_exchange_int(&gcstate->collecting
check or the check be moved up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks!
I think using forward declarations might help to have less moved blocks and make the review easier? |
Probably it would but I'd rather the code is cleaner (without the forward decs) and have the patch be messier. |
I rewrote the commit to use forward declarations (as suggested by Chris), makes the review easier: Aside from re-ordering the code and addition the forward defs, I didn't change anything. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally had time to review this. Apologies for the delay.
We can probably go with the version with forward references although I don't think it makes a lot of improvements in the final version, is true that the review is easier to do on the diff alone. |
The forward ref version was meant only for review purposes. I think we should merge Sam's (this) one. |
I fixed the bug pointed out by @chris-eibl. Let me know if you prefer to land the forward references version. If so, I'll merge @nascheme's changes in. Otherwise, I think it's good to go now once the CI passes. |
Nah, is good, let's go with this version. Thanks for the patience! |
Exactly that was my indention :) |
…hon#112533) * pythongh-112529: Use atomic operations for `gcstate->collecting` The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
…hon#112533) * pythongh-112529: Use atomic operations for `gcstate->collecting` The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
The
collecting
field inGCState
is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in--disable-gil
builds.The GC code is refactored a bit to support this. More of the logic is pushed down to
gc_collect_main()
so that we can safely order the logic settingcollecting
, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses.The change uses atomic operations for both
--disable-gil
and the default build (with the GIL) to avoid extra#ifdef
guards and ease the maintenance burden.--disable-gil
builds #112529