Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
pycore_weakref.h134 linesDownload Raw Back to internal
1#ifndef Py_INTERNAL_WEAKREF_H2#define Py_INTERNAL_WEAKREF_H3#ifdef __cplusplus4extern "C" {5#endif6 7#ifndef Py_BUILD_CORE8#  error "this header requires Py_BUILD_CORE define"9#endif10 11#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()12#include "pycore_lock.h"             // PyMutex_LockFlags()13#include "pycore_object.h"           // _Py_REF_IS_MERGED()14#include "pycore_pyatomic_ft_wrappers.h"15 16#ifdef Py_GIL_DISABLED17 18#define WEAKREF_LIST_LOCK(obj) \19    _PyInterpreterState_GET()  \20        ->weakref_locks[((uintptr_t)obj) % NUM_WEAKREF_LIST_LOCKS]21 22// Lock using the referenced object23#define LOCK_WEAKREFS(obj) \24    PyMutex_LockFlags(&WEAKREF_LIST_LOCK(obj), _Py_LOCK_DONT_DETACH)25#define UNLOCK_WEAKREFS(obj) PyMutex_Unlock(&WEAKREF_LIST_LOCK(obj))26 27// Lock using a weakref28#define LOCK_WEAKREFS_FOR_WR(wr) \29    PyMutex_LockFlags(wr->weakrefs_lock, _Py_LOCK_DONT_DETACH)30#define UNLOCK_WEAKREFS_FOR_WR(wr) PyMutex_Unlock(wr->weakrefs_lock)31 32#define FT_CLEAR_WEAKREFS(obj, weakref_list)    \33    do {                                        \34        assert(Py_REFCNT(obj) == 0);            \35        PyObject_ClearWeakRefs(obj);            \36    } while (0)37 38#else39 40#define LOCK_WEAKREFS(obj)41#define UNLOCK_WEAKREFS(obj)42 43#define LOCK_WEAKREFS_FOR_WR(wr)44#define UNLOCK_WEAKREFS_FOR_WR(wr)45 46#define FT_CLEAR_WEAKREFS(obj, weakref_list)        \47    do {                                            \48        assert(Py_REFCNT(obj) == 0);                \49        if (weakref_list != NULL) {                 \50            PyObject_ClearWeakRefs(obj);            \51        }                                           \52    } while (0)53 54#endif55 56static inline int _is_dead(PyObject *obj)57{58    // Explanation for the Py_REFCNT() check: when a weakref's target is part59    // of a long chain of deallocations which triggers the trashcan mechanism,60    // clearing the weakrefs can be delayed long after the target's refcount61    // has dropped to zero.  In the meantime, code accessing the weakref will62    // be able to "see" the target object even though it is supposed to be63    // unreachable.  See issue gh-60806.64#if defined(Py_GIL_DISABLED)65    Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared);66    return shared == _Py_REF_SHARED(0, _Py_REF_MERGED);67#else68    return (Py_REFCNT(obj) == 0);69#endif70}71 72static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj)73{74    assert(PyWeakref_Check(ref_obj));75    PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);76 77    PyObject *obj = FT_ATOMIC_LOAD_PTR(ref->wr_object);78    if (obj == Py_None) {79        // clear_weakref() was called80        return NULL;81    }82 83    LOCK_WEAKREFS(obj);84#ifdef Py_GIL_DISABLED85    if (ref->wr_object == Py_None) {86        // clear_weakref() was called87        UNLOCK_WEAKREFS(obj);88        return NULL;89    }90#endif91    if (_Py_TryIncref(obj)) {92        UNLOCK_WEAKREFS(obj);93        return obj;94    }95    UNLOCK_WEAKREFS(obj);96    return NULL;97}98 99static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)100{101    assert(PyWeakref_Check(ref_obj));102    int ret = 0;103    PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);104    PyObject *obj = FT_ATOMIC_LOAD_PTR(ref->wr_object);105    if (obj == Py_None) {106        // clear_weakref() was called107        ret = 1;108    }109    else {110        LOCK_WEAKREFS(obj);111        // See _PyWeakref_GET_REF() for the rationale of this test112#ifdef Py_GIL_DISABLED113        ret = (ref->wr_object == Py_None) || _is_dead(obj);114#else115        ret = _is_dead(obj);116#endif117        UNLOCK_WEAKREFS(obj);118    }119    return ret;120}121 122extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyObject *obj);123 124// Clear all the weak references to obj but leave their callbacks uncalled and125// intact.126extern void _PyWeakref_ClearWeakRefsNoCallbacks(PyObject *obj);127 128PyAPI_FUNC(int) _PyWeakref_IsDead(PyObject *weakref);129 130#ifdef __cplusplus131}132#endif133#endif /* !Py_INTERNAL_WEAKREF_H */134 
codekingpro/portable-devtools · Team Ai