FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-115432: Add critical section variant that handles a NULL object by colesbury · Pull Request #115433 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 29 additions & 0 deletions Include/internal/pycore_critical_section.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ extern "C" {
_PyCriticalSection_End(&_cs); \
}

# define Py_XBEGIN_CRITICAL_SECTION(op) \
{ \
_PyCriticalSection _cs_opt = {0}; \
_PyCriticalSection_XBegin(&_cs_opt, _PyObject_CAST(op))

# define Py_XEND_CRITICAL_SECTION() \
_PyCriticalSection_XEnd(&_cs_opt); \
}

# define Py_BEGIN_CRITICAL_SECTION2(a, b) \
{ \
_PyCriticalSection2 _cs2; \
Expand Down Expand Up @@ -131,6 +140,8 @@ extern "C" {
// The critical section APIs are no-ops with the GIL.
# define Py_BEGIN_CRITICAL_SECTION(op)
# define Py_END_CRITICAL_SECTION()
# define Py_XBEGIN_CRITICAL_SECTION(op)
# define Py_XEND_CRITICAL_SECTION()
# define Py_BEGIN_CRITICAL_SECTION2(a, b)
# define Py_END_CRITICAL_SECTION2()
# define _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(mutex)
Expand Down Expand Up @@ -187,6 +198,16 @@ _PyCriticalSection_Begin(_PyCriticalSection *c, PyMutex *m)
}
}

static inline void
_PyCriticalSection_XBegin(_PyCriticalSection *c, PyObject *op)
{
#ifdef Py_GIL_DISABLED
if (op != NULL) {
_PyCriticalSection_Begin(c, &_PyObject_CAST(op)->ob_mutex);
}
#endif
Comment on lines +204 to +208

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah cool, thanks for clearing that up!

}

// Removes the top-most critical section from the thread's stack of critical
// sections. If the new top-most critical section is inactive, then it is
// resumed.
Expand All @@ -209,6 +230,14 @@ _PyCriticalSection_End(_PyCriticalSection *c)
_PyCriticalSection_Pop(c);
}

static inline void
_PyCriticalSection_XEnd(_PyCriticalSection *c)
{
if (c->mutex) {
_PyCriticalSection_End(c);
}
}

static inline void
_PyCriticalSection2_Begin(_PyCriticalSection2 *c, PyMutex *m1, PyMutex *m2)
{
Expand Down
9 changes: 9 additions & 0 deletions Modules/_testinternalcapi/test_critical_sections.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ test_critical_sections(PyObject *self, PyObject *Py_UNUSED(args))
Py_END_CRITICAL_SECTION2();
assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));

// Optional variant behaves the same if the object is non-NULL
Py_XBEGIN_CRITICAL_SECTION(d1);
assert_nogil(PyMutex_IsLocked(&d1->ob_mutex));
Py_XEND_CRITICAL_SECTION();

// No-op
Py_XBEGIN_CRITICAL_SECTION(NULL);
Py_XEND_CRITICAL_SECTION();

Py_DECREF(d2);
Py_DECREF(d1);
Py_RETURN_NONE;
Expand Down

Back | FazBrowse Home | New Git URL