FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-159564: Make weakref dereference lock-free. by nascheme · Pull Request #159578 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 11 additions & 10 deletions Include/internal/pycore_weakref.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,27 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj)
assert(PyWeakref_Check(ref_obj));
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);

PyObject *obj = FT_ATOMIC_LOAD_PTR(ref->wr_object);
#ifdef Py_GIL_DISABLED
PyObject *obj = _Py_atomic_load_ptr(&ref->wr_object);
if (obj == Py_None) {
// clear_weakref() was called
return NULL;
}

LOCK_WEAKREFS(obj);
#ifdef Py_GIL_DISABLED
if (ref->wr_object == Py_None) {
if (_Py_TryIncrefCompare(&ref->wr_object, obj)) {
return obj;
}
return NULL;
#else
PyObject *obj = ref->wr_object;
if (obj == Py_None) {
// clear_weakref() was called
UNLOCK_WEAKREFS(obj);
return NULL;
}
#endif
if (_Py_TryIncref(obj)) {
UNLOCK_WEAKREFS(obj);
return obj;
}
UNLOCK_WEAKREFS(obj);
return NULL;
#endif
}

static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)
Expand All @@ -108,8 +109,8 @@ static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)
}
else {
LOCK_WEAKREFS(obj);
// See _PyWeakref_GET_REF() for the rationale of this test
#ifdef Py_GIL_DISABLED
// The weakref may have been cleared before the lock was acquired.
ret = (ref->wr_object == Py_None) || _is_dead(obj);
#else
ret = _is_dead(obj);
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_weakref.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,34 @@ def cb(wparent):
del root
gc.collect()

@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack")
def test_trashcan_deferred_dealloc(self):
# When the C stack is nearly exhausted, the trashcan defers
# deallocation of an object whose refcount has dropped to 0, leaving
# its weakrefs uncleared. Calling such a weakref must return None
# rather than resurrecting the dead object.
class Node(list):
pass

found = []
class Trigger:
def __init__(self, wr):
self.wr = wr
def __del__(self):
if self.wr() is not None:
found.append(True)

head = cur = Node()
for i in range(200_000):
child = Node()
# list_dealloc() clears items last to first: the child is
# deallocated (or deferred) before the trigger runs.
cur.extend([Trigger(weakref.ref(child)), child])
cur = child
del cur, child
del head
self.assertEqual(found, [])

def test_callback_attribute(self):
x = Object(1)
callback = lambda ref: None
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve the performance and thread scaling of dereferencing weak references
in the :term:`free-threaded build`. Dereferencing a weak reference no longer
takes a lock.
4 changes: 2 additions & 2 deletions Objects/weakrefobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ clear_weakref_lock_held(PyWeakReference *self, PyObject **callback)
/* If 'self' is the end of the list (and thus self->wr_next ==
NULL) then the weakref list itself (and thus the value of *list)
will end up being set to NULL. */
FT_ATOMIC_STORE_PTR(*list, self->wr_next);
FT_ATOMIC_STORE_PTR_RELEASE(*list, self->wr_next);
}
FT_ATOMIC_STORE_PTR(self->wr_object, Py_None);
FT_ATOMIC_STORE_PTR_RELEASE(self->wr_object, Py_None);
if (self->wr_prev != NULL) {
self->wr_prev->wr_next = self->wr_next;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL