Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-115432: Add critical section variant that handles a NULL object#115433

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
colesbury merged 4 commits intopython:mainfromcolesbury:gh-115432
Feb 15, 2024

Conversation

colesbury
Copy link
Contributor

@colesburycolesbury commentedFeb 13, 2024
edited
Loading

This addsPy_XBEGIN_CRITICAL_SECTION andPy_XEND_CRITICAL_SECTION, which accept a possiblyNULL object as an argument. If the argument isNULL, then nothing is locked or unlocked. Otherwise, they behave likePy_BEGIN/END_CRITICAL_SECTION.

This adds `Py_BEGIN_CRITICAL_SECTION_OPT` and`Py_END_CRITICAL_SECTION_OPT`, which accept a possibly NULL object as anargument. If the argument is NULL, then nothing is locked or unlocked.Otherwise, they behave like `Py_BEGIN/END_CRITICAL_SECTION`.
@colesbury
Copy link
ContributorAuthor

cc@tomasr8

tomasr8 reacted with thumbs up emoji

@colesbury
Copy link
ContributorAuthor

colesbury commentedFeb 13, 2024
edited
Loading

The context here is in theset implementation, we have a number of functions that take a possiblyNULLiterable argument:

staticPyObject*
make_new_set_basetype(PyTypeObject*type,PyObject*iterable)

We generally want to lock theiterable if it's not NULL. There's enough uses that I think it's worth handling inpycore_critical_section.h rather than insetobject.c.

Related PR:#113800

Comment on lines +204 to +208
#ifdef Py_GIL_DISABLED
if (op != NULL) {
_PyCriticalSection_Begin(c, &_PyObject_CAST(op)->ob_mutex);
}
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Is the#ifdef needed here? The whole macro should be a no-op when the GIL is enabled

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It's needed because of the&_PyObject_CAST(op)->ob_mutex will only compile on the free-threaded build. In the default build, PyObject does not have anob_mutex field.

We could have instead put the#ifdef Py_GIL_DISABLED around the whole_PyCriticalSection_BeginOpt function declaration. That would work too.

tomasr8 reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

Ah cool, thanks for clearing that up!

@DinoV
Copy link
Contributor

The context here is in theset implementation, we have a number of functions that take a possiblyNULLiterable argument:

staticPyObject*
make_new_set_basetype(PyTypeObject*type,PyObject*iterable)

I kind of wonder if we do really want to lock all of the iterables... Ultimately the dict case could proceed lock-free and for some arbitrary object that isn't a dict or a list we don't really need to lock it (e.g. for a Python used defined type it's presumably protected by some other Python lock if it needs to be thread safe).

But I could see this as being generally useful too.

@colesbury
Copy link
ContributorAuthor

I kind of wonder if we do really want to lock all of the iterables...

Yeah, I think we will want to refine the locking approach in the future, but locking all the iterables works for now. I think we want to actually lock in thedict case so that the whole operation appears atomic, even though the iteration over the dict is thread-safe without a lock. IIRC, we have code that assumes thatset(dict) is atomic.

@colesburycolesbury merged commitad4f909 intopython:mainFeb 15, 2024
@colesburycolesbury deleted the gh-115432 branchFebruary 15, 2024 13:37
diegorusso pushed a commit to diegorusso/cpython that referenced this pull requestApr 17, 2024
…ect (python#115433)This adds `Py_XBEGIN_CRITICAL_SECTION` and`Py_XEND_CRITICAL_SECTION`, which accept a possibly NULL object as anargument. If the argument is NULL, then nothing is locked or unlocked.Otherwise, they behave like `Py_BEGIN/END_CRITICAL_SECTION`.
colesbury added a commit to colesbury/cpython that referenced this pull requestMay 9, 2024
…NULL object (python#115433)"This reverts commitad4f909.The API ended up not being used.
colesbury added a commit that referenced this pull requestMay 9, 2024
…bject (#115433)" (#118861)This reverts commitad4f909.The API ended up not being used.
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestMay 9, 2024
…NULL object (pythonGH-115433)" (pythonGH-118861)This reverts commitad4f909.The API ended up not being used.(cherry picked from commit46c8081)Co-authored-by: Sam Gross <colesbury@gmail.com>
colesbury added a commit that referenced this pull requestMay 9, 2024
… NULL object (GH-115433)" (GH-118861) (#118872)This reverts commitad4f909.The API ended up not being used.(cherry picked from commit46c8081)Co-authored-by: Sam Gross <colesbury@gmail.com>
SonicField pushed a commit to SonicField/cpython that referenced this pull requestMay 10, 2024
…NULL object (python#115433)" (python#118861)This reverts commitad4f909.The API ended up not being used.
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull requestJan 22, 2025
…ect (python#115433)This adds `Py_XBEGIN_CRITICAL_SECTION` and`Py_XEND_CRITICAL_SECTION`, which accept a possibly NULL object as anargument. If the argument is NULL, then nothing is locked or unlocked.Otherwise, they behave like `Py_BEGIN/END_CRITICAL_SECTION`.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tomasr8tomasr8tomasr8 left review comments

@DinoVDinoVDinoV approved these changes

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@colesbury@DinoV@tomasr8

[8]ページ先頭

©2009-2025 Movatter.jp