-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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-115432: Add critical section variant that handles a NULL object #115433
Conversation
This adds `Py_BEGIN_CRITICAL_SECTION_OPT` and `Py_END_CRITICAL_SECTION_OPT`, which accept a possibly NULL object as an argument. If the argument is NULL, then nothing is locked or unlocked. Otherwise, they behave like `Py_BEGIN/END_CRITICAL_SECTION`.
cc @tomasr8 |
The context here is in the Lines 1015 to 1016 in 514b1c9
We generally want to lock the Related PR: #113800 |
#ifdef Py_GIL_DISABLED | ||
if (op != NULL) { | ||
_PyCriticalSection_Begin(c, &_PyObject_CAST(op)->ob_mutex); | ||
} | ||
#endif |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 an ob_mutex
field.
We could have instead put the #ifdef Py_GIL_DISABLED
around the whole _PyCriticalSection_BeginOpt
function declaration. That would work too.
There was a problem hiding this comment.
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!
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. |
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 the |
…ect (python#115433) This adds `Py_XBEGIN_CRITICAL_SECTION` and `Py_XEND_CRITICAL_SECTION`, which accept a possibly NULL object as an argument. If the argument is NULL, then nothing is locked or unlocked. Otherwise, they behave like `Py_BEGIN/END_CRITICAL_SECTION`.
…NULL object (python#115433)" This reverts commit ad4f909. The API ended up not being used.
…NULL object (pythonGH-115433)" (pythonGH-118861) This reverts commit ad4f909. The API ended up not being used. (cherry picked from commit 46c8081) Co-authored-by: Sam Gross <[email protected]>
…NULL object (python#115433)" (python#118861) This reverts commit ad4f909. The API ended up not being used.
This adds
Py_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
.Py_BEGIN_CRITICAL_SECTION
that accepts aNULL
argument #115432