Skip to content
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-129533: Update PyGC_Enable/Disable/IsEnabled to use atomic operation #129563

Merged
merged 7 commits into from
Feb 6, 2025

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Feb 2, 2025

@corona10
Copy link
Member Author

corona10 commented Feb 2, 2025

TSAN is passed at my local machine:

import concurrent.futures
import functools
import gc
import threading

num_threads = 10

def closure(b):
  b.wait()
  for _ in range(100):
      gc.disable()
      gc.enable()

with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
  for i in range(100):
    b = threading.Barrier(num_threads)
    for _ in range(num_threads):
      executor.submit(functools.partial(closure, b))

int old_state = gcstate->enabled;
gcstate->enabled = 0;
int old_state = _Py_atomic_load_int(&(gcstate->enabled));
while (!_Py_atomic_compare_exchange_int(&gcstate->enabled, &old_state, 0));
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need to update old_state explicitly since __atomic_compare_exchange_n automatically handle.
https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gcc/_005f_005fatomic-Builtins.html

@corona10
Copy link
Member Author

corona10 commented Feb 2, 2025

cc @hawkinsp

@corona10 corona10 requested a review from pablogsal February 2, 2025 04:48
Python/gc_free_threading.c Outdated Show resolved Hide resolved
Python/gc_free_threading.c Outdated Show resolved Hide resolved
@corona10 corona10 requested a review from mpage February 2, 2025 07:13
@corona10 corona10 changed the title gh-129533: Update PyGC_Enable/Disable/IsEnabled to use CAS operation gh-129533: Update PyGC_Enable/Disable/IsEnabled to use atomic operation Feb 2, 2025
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -1952,25 +1953,23 @@ int
PyGC_Enable(void)
{
GCState *gcstate = get_gc_state();
int old_state = gcstate->enabled;
gcstate->enabled = 1;
int old_state = _Py_atomic_exchange_int(&gcstate->enabled, 1);
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the old_state variable. Same remark in PyGC_Disable() and PyGC_IsEnabled().

@corona10
Copy link
Member Author

corona10 commented Feb 2, 2025

Waiting @pablogsal before merging this PR :)

@corona10 corona10 merged commit b184abf into python:main Feb 6, 2025
42 checks passed
@miss-islington-app
Copy link

Thanks @corona10 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @corona10, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker b184abf074c0e1f379a238f07da5616460f36b93 3.13

corona10 added a commit to corona10/cpython that referenced this pull request Feb 6, 2025
@bedevere-app
Copy link

bedevere-app bot commented Feb 6, 2025

GH-129756 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Feb 6, 2025
corona10 added a commit that referenced this pull request Feb 6, 2025
… operat… (gh-129756)

gh-129533: Update PyGC_Enable/Disable/IsEnabled to use atomic operation  (gh-129563)

(cherry picked from commit b184abf)
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 2025
cmaloney pushed a commit to cmaloney/cpython that referenced this pull request Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants