Skip to content

Commit ce8536d

Browse files
committed
Use mutex rather than critical sections.
For TYPE_LOCK, replace critical sections with a simple mutex. A number of changes were needed to avoid deadlocking on reentrancy (trying to re-acquire the mutex). Split _PyType_LookupRefAndVersion() into a locked and unlocked version. Remove atomic operations where they are not required. Remove some cases of TYPE_LOCK being held that is not required.
1 parent ef2f07b commit ce8536d

3 files changed

Lines changed: 372 additions & 212 deletions

File tree

Include/internal/pycore_typeobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ struct types_state {
144144
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];
145145
} for_extensions;
146146
PyMutex mutex;
147+
// used to check correct usage of the above mutex
148+
unsigned long long mutex_tid;
147149

148150
// Borrowed references to type objects whose
149151
// tp_version_tag % TYPE_VERSION_CACHE_SIZE

Include/object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ given type object has a specified feature.
576576
/* Objects behave like an unbound method */
577577
#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
578578

579-
/* Unused. Legacy flag */
580-
#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
579+
/* Type structure is potentially exposed (revealed) to other threads */
580+
#define Py_TPFLAGS_EXPOSED (1UL << 19)
581581

582582
/* Type is abstract and cannot be instantiated */
583583
#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)

0 commit comments

Comments
 (0)