Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33
Add PyUnstable_TryIncref and PyUnstable_EnableTryIncRef.#159
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2231,6 +2231,81 @@ static inline int PyUnstable_Object_IsUniquelyReferenced(PyObject *obj) | ||
| } | ||
| #endif | ||
| // gh-128926 added PyUnstable_TryIncRef() and PyUnstable_EnableTryIncRef() to | ||
| // Python 3.14.0a5. Adapted from _Py_TryIncref() and _PyObject_SetMaybeWeakref(). | ||
| #if PY_VERSION_HEX < 0x030E00A5 | ||
| static inline int PyUnstable_TryIncRef(PyObject *op) | ||
| { | ||
| #ifndef Py_GIL_DISABLED | ||
| if (Py_REFCNT(op) > 0) { | ||
| Py_INCREF(op); | ||
| return 1; | ||
| } | ||
| return 0; | ||
| #else | ||
| // _Py_TryIncrefFast() | ||
| uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); | ||
colesbury marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| local += 1; | ||
| if (local == 0) { | ||
| // immortal | ||
| return 1; | ||
colesbury marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| if (_Py_IsOwnedByCurrentThread(op)) { | ||
| _Py_INCREF_STAT_INC(); | ||
| _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); | ||
| #ifdef Py_REF_DEBUG | ||
| _Py_INCREF_IncRefTotal(); | ||
| #endif | ||
| return 1; | ||
| } | ||
| // _Py_TryIncRefShared() | ||
| Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | ||
| for (;;) { | ||
| // If the shared refcount is zero and the object is either merged | ||
| // or may not have weak references, then we cannot incref it. | ||
| if (shared == 0 || shared == _Py_REF_MERGED) { | ||
| return 0; | ||
| } | ||
| if (_Py_atomic_compare_exchange_ssize( | ||
| &op->ob_ref_shared, | ||
| &shared, | ||
| shared + (1 << _Py_REF_SHARED_SHIFT))) { | ||
| #ifdef Py_REF_DEBUG | ||
| _Py_INCREF_IncRefTotal(); | ||
| #endif | ||
| _Py_INCREF_STAT_INC(); | ||
| return 1; | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
| static inline void PyUnstable_EnableTryIncRef(PyObject *op) | ||
| { | ||
| #ifdef Py_GIL_DISABLED | ||
| // _PyObject_SetMaybeWeakref() | ||
| if (_Py_IsImmortal(op)) { | ||
colesbury marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return; | ||
| } | ||
| for (;;) { | ||
| Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | ||
| if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) { | ||
| // Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states. | ||
| return; | ||
| } | ||
| if (_Py_atomic_compare_exchange_ssize( | ||
| &op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) { | ||
| return; | ||
| } | ||
| } | ||
| #else | ||
| (void)op; // unused argument | ||
| #endif | ||
| } | ||
| #endif | ||
| #if PY_VERSION_HEX < 0x030F0000 | ||
| static inline PyObject* | ||
Uh oh!
There was an error while loading.Please reload this page.