https://github.com/python/cpython/commit/454d7963e31cded1de3a90642da7259848efd232
commit: 454d7963e31cded1de3a90642da7259848efd232
branch: main
author: Dino Viehland <[email protected]>
committer: DinoV <[email protected]>
date: 2024-02-15T16:28:31-08:00
summary:
gh-113743: Use per-interpreter locks for types (#115541)
Move type-lock to per-interpreter lock to avoid heavy contention in
interpreters test
files:
M Include/internal/pycore_typeobject.h
M Objects/typeobject.c
M Python/pystate.c
diff --git a/Include/internal/pycore_typeobject.h
b/Include/internal/pycore_typeobject.h
index 664f6fb212a57d..9134ab45cd0039 100644
--- a/Include/internal/pycore_typeobject.h
+++ b/Include/internal/pycore_typeobject.h
@@ -22,7 +22,6 @@ struct _types_runtime_state {
// bpo-42745: next_version_tag remains shared by all interpreters
// because of static types.
unsigned int next_version_tag;
- PyMutex type_mutex;
};
@@ -71,6 +70,7 @@ struct types_state {
struct type_cache type_cache;
size_t num_builtins_initialized;
static_builtin_state builtins[_Py_MAX_STATIC_BUILTIN_TYPES];
+ PyMutex mutex;
};
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index e0711dfe8545b7..0118ee255ef017 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -60,17 +60,18 @@ class object "PyObject *" "&PyBaseObject_Type"
// in odd behaviors w.r.t. running with the GIL as the outer type lock could
// be released and reacquired during a subclass update if there's contention
// on the subclass lock.
+#define TYPE_LOCK &PyInterpreterState_Get()->types.mutex
#define BEGIN_TYPE_LOCK() \
{ \
_PyCriticalSection _cs; \
- _PyCriticalSection_Begin(&_cs, &_PyRuntime.types.type_mutex); \
+ _PyCriticalSection_Begin(&_cs, TYPE_LOCK); \
#define END_TYPE_LOCK() \
_PyCriticalSection_End(&_cs); \
}
#define ASSERT_TYPE_LOCK_HELD() \
- _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&_PyRuntime.types.type_mutex)
+ _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(TYPE_LOCK)
#else
diff --git a/Python/pystate.c b/Python/pystate.c
index b1d1a085d629b4..24f9b7790915ab 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -395,7 +395,7 @@ _Py_COMP_DIAG_POP
&(runtime)->atexit.mutex, \
&(runtime)->audit_hooks.mutex, \
&(runtime)->allocators.mutex, \
- &(runtime)->types.type_mutex, \
+ &(runtime)->_main_interpreter.types.mutex, \
}
static void
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]