|
| 1 | +#ifndef Py_INTERNAL_UNIQUEID_H |
| 2 | +#define Py_INTERNAL_UNIQUEID_H |
| 3 | +#ifdef __cplusplus |
| 4 | +extern "C" { |
| 5 | +#endif |
| 6 | + |
| 7 | +#ifndef Py_BUILD_CORE |
| 8 | +# error "this header requires Py_BUILD_CORE define" |
| 9 | +#endif |
| 10 | + |
| 11 | +#ifdef Py_GIL_DISABLED |
| 12 | + |
| 13 | +// This contains code for allocating unique ids to objects for per-thread |
| 14 | +// reference counting. |
| 15 | +// |
| 16 | +// Per-thread reference counting is used along with deferred reference |
| 17 | +// counting to avoid scaling bottlenecks due to reference count contention. |
| 18 | +// |
| 19 | +// An id of -1 is used to indicate that an object doesn't use per-thread |
| 20 | +// refcounting. This value is used when the object is finalized by the GC |
| 21 | +// and during interpreter shutdown to allow the object to be |
| 22 | +// deallocated promptly when the object's refcount reaches zero. |
| 23 | +// |
| 24 | +// Each entry implicitly represents a unique id based on its offset in the |
| 25 | +// table. Non-allocated entries form a free-list via the 'next' pointer. |
| 26 | +// Allocated entries store the corresponding PyObject. |
| 27 | +typedef union _Py_unique_id_entry { |
| 28 | + // Points to the next free type id, when part of the freelist |
| 29 | + union _Py_unique_id_entry *next; |
| 30 | + |
| 31 | + // Stores the object when the id is assigned |
| 32 | + PyObject *obj; |
| 33 | +} _Py_unique_id_entry; |
| 34 | + |
| 35 | +struct _Py_unique_id_pool { |
| 36 | + PyMutex mutex; |
| 37 | + |
| 38 | + // combined table of object with allocated unique ids and unallocated ids. |
| 39 | + _Py_unique_id_entry *table; |
| 40 | + |
| 41 | + // Next entry to allocate inside 'table' or NULL |
| 42 | + _Py_unique_id_entry *freelist; |
| 43 | + |
| 44 | + // size of 'table' |
| 45 | + Py_ssize_t size; |
| 46 | +}; |
| 47 | + |
| 48 | +// Assigns the next id from the pool of ids. |
| 49 | +extern Py_ssize_t _PyObject_AssignUniqueId(PyObject *obj); |
| 50 | + |
| 51 | +// Releases the allocated id back to the pool. |
| 52 | +extern void _PyObject_ReleaseUniqueId(Py_ssize_t unique_id); |
| 53 | + |
| 54 | +// Merges the per-thread reference counts into the corresponding objects. |
| 55 | +extern void _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate); |
| 56 | + |
| 57 | +// Like _PyObject_MergePerThreadRefcounts, but also frees the per-thread |
| 58 | +// array of refcounts. |
| 59 | +extern void _PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate); |
| 60 | + |
| 61 | +// Frees the interpreter's pool of type ids. |
| 62 | +extern void _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp); |
| 63 | + |
| 64 | +// Increfs the type, resizing the per-thread refcount array if necessary. |
| 65 | +PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type); |
| 66 | + |
| 67 | +#endif /* Py_GIL_DISABLED */ |
| 68 | + |
| 69 | +#ifdef __cplusplus |
| 70 | +} |
| 71 | +#endif |
| 72 | +#endif /* !Py_INTERNAL_UNIQUEID_H */ |
0 commit comments