8000 gh-135380: enhance critical section held assertions (#135381) · python/cpython@a8ec511 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8ec511

Browse files
gh-135380: enhance critical section held assertions (#135381)
1 parent 3fb6cfe commit a8ec511

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Include/internal/pycore_critical_section.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern "C" {
6464

6565
# define _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op) \
6666
if (Py_REFCNT(op) != 1) { \
67-
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&_PyObject_CAST(op)->ob_mutex); \
67+
_PyCriticalSection_AssertHeldObj(_PyObject_CAST(op)); \
6868
}
6969

7070
#else /* Py_DEBUG */
@@ -239,6 +239,28 @@ _PyCriticalSection_AssertHeld(PyMutex *mutex)
239239
#endif
240240
}
241241

242+
static inline void
243+
_PyCriticalSection_AssertHeldObj(PyObject *op)
244+
{
245+
#ifdef Py_DEBUG
246+
PyMutex *mutex = &_PyObject_CAST(op)->ob_mutex;
247+
PyThreadState *tstate = _PyThreadState_GET();
248+
uintptr_t prev = tstate->critical_section;
249+
if (prev & _Py_CRITICAL_SECTION_TWO_MUTEXES) {
250+
PyCriticalSection2 *cs = (PyCriticalSection2 *)(prev & ~_Py_CRITICAL_SECTION_MASK);
251+
_PyObject_ASSERT_WITH_MSG(op,
252+
(cs != NULL && (cs->_cs_base._cs_mutex == mutex || cs->_cs_mutex2 == mutex)),
253+
"Critical section of object is not held");
254+
}
255+
else {
256+
PyCriticalSection *cs = (PyCriticalSection *)(prev & ~_Py_CRITICAL_SECTION_MASK);
257+
_PyObject_ASSERT_WITH_MSG(op,
258+
(cs != NULL && cs->_cs_mutex == mutex),
259+
"Critical section of object is not held");
260+
}
261+
262+
#endif
263+
}
242264
#endif /* Py_GIL_DISABLED */
243265

244266
#ifdef __cplusplus

0 commit comments

Comments
 (0)
0