Skip to content

Commit

Permalink
pythongh-111926 Update _weakref to be threadsafe in --disable-gil build
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Nov 17, 2023
1 parent bd89bca commit c929bc0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Python.h"
#include "pycore_dict.h" // _PyDict_DelItemIf()
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()

#include "pycore_dict.h" // _PyDict_DelItemIf()
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION

#define GET_WEAKREFS_LISTPTR(o) \
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
Expand Down Expand Up @@ -32,9 +32,11 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object)

if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
return 0;

Py_BEGIN_CRITICAL_SECTION(object);
list = GET_WEAKREFS_LISTPTR(object);
return _PyWeakref_GetWeakrefCount(*list);
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
Py_END_CRITICAL_SECTION();
return count;
}


Expand Down Expand Up @@ -94,6 +96,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
return PyList_New(0);
}

Py_BEGIN_CRITICAL_SECTION(object);
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);

Expand All @@ -107,6 +110,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
PyList_SET_ITEM(result, i, Py_NewRef(current));
current = current->wr_next;
}
Py_END_CRITICAL_SECTION();
return result;
}

Expand Down

0 comments on commit c929bc0

Please sign in to comment.