Skip to content

Commit 454d796

Browse files
authored
gh-113743: Use per-interpreter locks for types (#115541)
Move type-lock to per-interpreter lock to avoid heavy contention in interpreters test
1 parent bce6931 commit 454d796

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

Include/internal/pycore_typeobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct _types_runtime_state {
2222
// bpo-42745: next_version_tag remains shared by all interpreters
2323
// because of static types.
2424
unsigned int next_version_tag;
25-
PyMutex type_mutex;
2625
};
2726

2827

@@ -71,6 +70,7 @@ struct types_state {
7170
struct type_cache type_cache;
7271
size_t num_builtins_initialized;
7372
static_builtin_state builtins[_Py_MAX_STATIC_BUILTIN_TYPES];
73+
PyMutex mutex;
7474
};
7575

7676

Objects/typeobject.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,18 @@ class object "PyObject *" "&PyBaseObject_Type"
6060
// in odd behaviors w.r.t. running with the GIL as the outer type lock could
6161
// be released and reacquired during a subclass update if there's contention
6262
// on the subclass lock.
63+
#define TYPE_LOCK &PyInterpreterState_Get()->types.mutex
6364
#define BEGIN_TYPE_LOCK() \
6465
{ \
6566
_PyCriticalSection _cs; \
66-
_PyCriticalSection_Begin(&_cs, &_PyRuntime.types.type_mutex); \
67+
_PyCriticalSection_Begin(&_cs, TYPE_LOCK); \
6768

6869
#define END_TYPE_LOCK() \
6970
_PyCriticalSection_End(&_cs); \
7071
}
7172

7273
#define ASSERT_TYPE_LOCK_HELD() \
73-
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&_PyRuntime.types.type_mutex)
74+
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(TYPE_LOCK)
7475

7576
#else
7677

Python/pystate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ _Py_COMP_DIAG_POP
395395
&(runtime)->atexit.mutex, \
396396
&(runtime)->audit_hooks.mutex, \
397397
&(runtime)->allocators.mutex, \
398-
&(runtime)->types.type_mutex, \
398+
&(runtime)->_main_interpreter.types.mutex, \
399399
}
400400

401401
static void

0 commit comments

Comments
 (0)