Skip to content

Add Doc section in free-threaded ext howto for critical sections #132531

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

Merged
merged 3 commits into from
Apr 16, 2025

Conversation

nascheme
Copy link
Member

@nascheme nascheme commented Apr 14, 2025

Since the critical section macros are intended to be used by extensions, it would be good to have more documentation about how they work. These docs are based on the comments in Include/cpython/critical_section.h and my own reading of the source code.


📚 Documentation preview 📚: https://cpython-previews--132531.org.readthedocs.build/

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

I think an example with a type that extends PyObject might help. Critical sections are a pretty natural thing to use when you're managing internal state on such an object, which the extension should have complete control over so the caveats of critical sections can be worked around.

- For locking two objects simultaneously

These macros are no-ops in non-free-threaded builds, so they can be safely
added to code that needs to support both build types.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe worth adding you can only lock one or two objects at a time, not three or more.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is mentioned lower down, in the "Important Considerations" section.


* Critical sections operate on a per-object basis rather than globally

* Critical sections follow a stack discipline within each thread
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what "follow a stack discipline" means

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the comment: the "begin" and "end" macros enforce this since they must be paired and within the same scope. Hopefully that makes it clear enough. I think in this context "stack discipline" means you can only push and pop critical sections, in LIFO fashion.

@nascheme nascheme merged commit c6973ee into python:main Apr 16, 2025
24 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants