Skip to content

Commit fbebca2

Browse files
GH-116946: eliminate the need for the GC in the _thread.lock and _thread.RLock (#141268)
1 parent 9ce99c6 commit fbebca2

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

Modules/_threadmodule.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ typedef struct {
4141
typedef struct {
4242
PyObject_HEAD
4343
PyMutex lock;
44+
PyObject *weakreflist; /* List of weak references */
4445
} lockobject;
4546

4647
#define lockobject_CAST(op) ((lockobject *)(op))
4748

4849
typedef struct {
4950
PyObject_HEAD
5051
_PyRecursiveMutex lock;
52+
PyObject *weakreflist; /* List of weak references */
5153
} rlockobject;
5254

5355
#define rlockobject_CAST(op) ((rlockobject *)(op))
@@ -767,7 +769,6 @@ static PyType_Spec ThreadHandle_Type_spec = {
767769
static void
768770
lock_dealloc(PyObject *self)
769771
{
770-
PyObject_GC_UnTrack(self);
771772
PyObject_ClearWeakRefs(self);
772773
PyTypeObject *tp = Py_TYPE(self);
773774
tp->tp_free(self);
@@ -999,6 +1000,10 @@ lock_new_impl(PyTypeObject *type)
9991000
return (PyObject *)self;
10001001
}
10011002

1003+
static PyMemberDef lock_members[] = {
1004+
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(lockobject, weakreflist), Py_READONLY},
1005+
{NULL}
1006+
};
10021007

10031008
static PyMethodDef lock_methods[] = {
10041009
_THREAD_LOCK_ACQUIRE_LOCK_METHODDEF
@@ -1034,17 +1039,16 @@ static PyType_Slot lock_type_slots[] = {
10341039
{Py_tp_dealloc, lock_dealloc},
10351040
{Py_tp_repr, lock_repr},
10361041
{Py_tp_doc, (void *)lock_doc},
1042+
{Py_tp_members, lock_members},
10371043
{Py_tp_methods, lock_methods},
1038-
{Py_tp_traverse, _PyObject_VisitType},
10391044
{Py_tp_new, lock_new},
10401045
{0, 0}
10411046
};
10421047

10431048
static PyType_Spec lock_type_spec = {
10441049
.name = "_thread.lock",
10451050
.basicsize = sizeof(lockobject),
1046-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1047-
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_WEAKREF),
1051+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
10481052
.slots = lock_type_slots,
10491053
};
10501054

@@ -1059,7 +1063,6 @@ rlock_locked_impl(rlockobject *self)
10591063
static void
10601064
rlock_dealloc(PyObject *self)
10611065
{
1062-
PyObject_GC_UnTrack(self);
10631066
PyObject_ClearWeakRefs(self);
10641067
PyTypeObject *tp = Py_TYPE(self);
10651068
tp->tp_free(self);
@@ -1319,6 +1322,11 @@ _thread_RLock__at_fork_reinit_impl(rlockobject *self)
13191322
#endif /* HAVE_FORK */
13201323

13211324

1325+
static PyMemberDef rlock_members[] = {
1326+
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(rlockobject, weakreflist), Py_READONLY},
1327+
{NULL}
1328+
};
1329+
13221330
static PyMethodDef rlock_methods[] = {
13231331
_THREAD_RLOCK_ACQUIRE_METHODDEF
13241332
_THREAD_RLOCK_RELEASE_METHODDEF
@@ -1339,18 +1347,18 @@ static PyMethodDef rlock_methods[] = {
13391347
static PyType_Slot rlock_type_slots[] = {
13401348
{Py_tp_dealloc, rlock_dealloc},
13411349
{Py_tp_repr, rlock_repr},
1350+
{Py_tp_members, rlock_members},
13421351
{Py_tp_methods, rlock_methods},
13431352
{Py_tp_alloc, PyType_GenericAlloc},
13441353
{Py_tp_new, rlock_new},
1345-
{Py_tp_traverse, _PyObject_VisitType},
13461354
{0, 0},
13471355
};
13481356

13491357
static PyType_Spec rlock_type_spec = {
13501358
.name = "_thread.RLock",
13511359
.basicsize = sizeof(rlockobject),
13521360
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1353-
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_WEAKREF),
1361+
Py_TPFLAGS_IMMUTABLETYPE),
13541362
.slots = rlock_type_slots,
13551363
};
13561364

0 commit comments

Comments
 (0)