Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-111926: Update _weakref to be threadsafe in --disable-gil build #112189

Merged
merged 6 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 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_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_dict.h" // _PyDict_DelItemIf()
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()

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

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

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


Expand All @@ -45,7 +48,11 @@ is_dead_weakref(PyObject *value)
PyErr_SetString(PyExc_TypeError, "not a weakref");
return -1;
}
return _PyWeakref_IS_DEAD(value);
int is_dead;
Py_BEGIN_CRITICAL_SECTION(value);
is_dead = _PyWeakref_IS_DEAD(value);
Py_END_CRITICAL_SECTION();
return is_dead;
}

/*[clinic input]
Expand Down Expand Up @@ -94,19 +101,23 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
return PyList_New(0);
}

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

PyObject *result = PyList_New(count);
result = PyList_New(count);
if (result == NULL) {
return NULL;
goto exit;
}

PyWeakReference *current = *list;
for (Py_ssize_t i = 0; i < count; ++i) {
PyList_SET_ITEM(result, i, Py_NewRef(current));
current = current->wr_next;
}
exit:
Py_END_CRITICAL_SECTION();
return result;
}

Expand Down