8000 gh-111926: Update _PyWeakref_IS_DEAD to be thread-safe by corona10 · Pull Request #112267 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111926: Update _PyWeakref_IS_DEAD to be thread-safe #112267

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

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Changes from 1 commit
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
Next Next commit
gh-111926 Update _PyWeakref_IS_DEAD to be thread-safe
  • Loading branch information
corona10 committed Nov 19, 2023
commit 866af79af34bc46a15611a2a1b769bb09e8ee91a
15 changes: 11 additions & 4 deletions Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()

static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
assert(PyWeakref_Check(ref_obj));
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
Expand Down Expand Up @@ -35,15 +37,20 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {

static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
assert(PyWeakref_Check(ref_obj));
int ret;
Py_BEGIN_CRITICAL_SECTION(ref_obj);
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
PyObject *obj = ref->wr_object;
if (obj == Py_None) {
// clear_weakref() was called
return 1;
ret = 1;
}

// See _PyWeakref_GET_REF() for the rationale of this test
return (Py_REFCNT(obj) == 0);
else {
// See _PyWeakref_GET_REF() for the rationale of this test
ret = (Py_REFCNT(obj) == 0);
}
Py_END_CRITICAL_SECTION();
return ret;
}

extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
Expand Down
0