8000 Add _PyObject_SetMaybeWeakref · python/cpython@9bf3820 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bf3820

Browse files
committed
Add _PyObject_SetMaybeWeakref
We need to tag weakrefs and their referents
1 parent 72eea51 commit 9bf3820

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Include/internal/pycore_object.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,25 @@ _Py_XNewRefWithLock(PyObject *obj)
507507
return _Py_NewRefWithLock(obj);
508508
}
509509

510+
static inline void
511+
_PyObject_SetMaybeWeakref(PyObject *op)
512+
{
513+
if (_Py_IsImmortal(op)) {
514+
return;
515+
}
516+
for (;;) {
517+
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
518+
if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) {
519+
// Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states.
520+
return;
521+
}
522+
if (_Py_atomic_compare_exchange_ssize(
523+
&op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
524+
return;
525+
}
526+
}
527+
}
528+
510529
#endif
511530

512531
#ifdef Py_REF_DEBUG

0 commit comments

Comments
 (0)
0