gh-116664: Make module state Py_SETREF's in _warnings thread-safe (#116959)

Mark the swap operations as critical sections.

Add an internal Py_BEGIN_CRITICAL_SECTION_MUT API that takes a PyMutex
pointer instead of a PyObject pointer.
This commit is contained in:
Erlend E. Aasland 2024-03-28 16:05:08 +01:00 committed by GitHub
parent 9a388b9a64
commit c1712ef066
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 23 deletions

View file

@ -87,10 +87,13 @@ extern "C" {
#define _Py_CRITICAL_SECTION_MASK 0x3
#ifdef Py_GIL_DISABLED
# define Py_BEGIN_CRITICAL_SECTION(op) \
# define Py_BEGIN_CRITICAL_SECTION_MUT(mutex) \
{ \
_PyCriticalSection _cs; \
_PyCriticalSection_Begin(&_cs, &_PyObject_CAST(op)->ob_mutex)
_PyCriticalSection_Begin(&_cs, mutex)
# define Py_BEGIN_CRITICAL_SECTION(op) \
Py_BEGIN_CRITICAL_SECTION_MUT(&_PyObject_CAST(op)->ob_mutex)
# define Py_END_CRITICAL_SECTION() \
_PyCriticalSection_End(&_cs); \
@ -138,6 +141,7 @@ extern "C" {
#else /* !Py_GIL_DISABLED */
// The critical section APIs are no-ops with the GIL.
# define Py_BEGIN_CRITICAL_SECTION_MUT(mut)
# define Py_BEGIN_CRITICAL_SECTION(op)
# define Py_END_CRITICAL_SECTION()
# define Py_XBEGIN_CRITICAL_SECTION(op)

View file

@ -14,6 +14,7 @@ struct _warnings_runtime_state {
PyObject *filters; /* List */
PyObject *once_registry; /* Dict */
PyObject *default_action; /* String */
struct _PyMutex mutex;
long filters_version;
};