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-115304: Add doc for initialize PyMutex as global variables #115305

Merged
merged 3 commits into from
Feb 21, 2024

Conversation

aisk
Copy link
Member

@aisk aisk commented Feb 11, 2024

@aisk aisk changed the title gh-115304: Correct the typical initialization of PyMutex in its comme… gh-115304: Correct the initialization doc of PyMutex in its comment Feb 11, 2024
@aisk aisk changed the title gh-115304: Correct the initialization doc of PyMutex in its comment gh-115304: Make the initialization doc of PyMutex in its comment portable Feb 11, 2024
@@ -26,7 +26,7 @@ extern "C" {
// 0b11: locked and has parked threads
//
// Typical initialization:
// PyMutex m = (PyMutex){0};
// PyMutex m = {0};
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should make this change. The comment is for variables that need explicit initialization, such as local variables and fields in heap allocated structures. Static variables are implicitly initialized to zero in C.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe instead we can add an additional example of statically initializing the PyMutex?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review! For local variables, should we remove the cast (PyMutex)? Seems it is needless in modern C.

Copy link
Contributor

Choose a reason for hiding this comment

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

Most commonly, we are initializing fields in a struct like:

self->mutex = (PyMutex){0};

You can't write:

self->mutex = {0};  // INVALID!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I've got the point!

@aisk aisk changed the title gh-115304: Make the initialization doc of PyMutex in its comment portable gh-115304: Add doc for initialize PyMutex as global variables Feb 17, 2024
@colesbury colesbury self-requested a review February 18, 2024 05:13
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.

Thanks!

@colesbury colesbury merged commit 87a65a5 into python:main Feb 21, 2024
36 checks passed
@aisk aisk deleted the patch-1 branch March 2, 2024 16:59
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
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.

2 participants