Uh oh!
There was an error while loading. Please reload this page.
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.
| void | ||
| _Py_ScheduleGC(PyInterpreterState *interp) | ||
| { | ||
| GCState *gcstate = &interp->gc; |
There was a problem hiding this comment.
fyi: _Py_ScheduleGC not public and only called from _PyObject_GC_Link, which already checks gcstate->collecting.
colesbury
commented
Nov 29, 2023
@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. |
pablogsal
commented
Nov 29, 2023
will review this week 👍 |
colesbury
commented
Dec 6, 2023
@pablogsal - gentle reminder, this is awaiting your review |
nascheme
commented
Dec 8, 2023
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. |
| { | ||
| GC_STAT_ADD(generation, collections, 1); | ||
| #ifdef Py_STATS | ||
| if (_Py_stats) { |
There was a problem hiding this comment.
Shouldn't object_visits be zeroed after the _Py_atomic_compare_exchange_int(&gcstate->collecting check or the check be moved up?
chris-eibl
commented
Dec 9, 2023
I think using forward declarations might help to have less moved blocks and make the review easier? |
nascheme
commented
Dec 9, 2023
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. |
pablogsal
left a comment
There was a problem hiding this comment.
I finally had time to review this. Apologies for the delay.
pablogsal
commented
Dec 11, 2023
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. |
nascheme
commented
Dec 11, 2023
The forward ref version was meant only for review purposes. I think we should merge Sam's (this) one. |
colesbury
commented
Dec 11, 2023
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. |
pablogsal
commented
Dec 11, 2023
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
collectingfield inGCStateis 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-gilbuilds.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-giland the default build (with the GIL) to avoid extra#ifdefguards and ease the maintenance burden.--disable-gilbuilds #112529