codekingpro/portable-devtools
114k
1#ifndef Py_INTERNAL_BRC_H2#define Py_INTERNAL_BRC_H3 4#include <stdint.h>5#include "pycore_llist.h" // struct llist_node6#include "pycore_object_stack.h" // _PyObjectStack7 8#ifdef __cplusplus9extern "C" {10#endif11 12#ifndef Py_BUILD_CORE13# error "this header requires Py_BUILD_CORE define"14#endif15 16#ifdef Py_GIL_DISABLED17 18// Prime number to avoid correlations with memory addresses.19#define _Py_BRC_NUM_BUCKETS 25720 21// Hash table bucket22struct _brc_bucket {23 // Mutex protects both the bucket and thread state queues in this bucket.24 PyMutex mutex;25 26 // Linked list of _PyThreadStateImpl objects hashed to this bucket.27 struct llist_node root;28};29 30// Per-interpreter biased reference counting state31struct _brc_state {32 // Hash table of thread states by thread-id. Thread states within a bucket33 // are chained using a doubly-linked list.34 struct _brc_bucket table[_Py_BRC_NUM_BUCKETS];35};36 37// Per-thread biased reference counting state38struct _brc_thread_state {39 // Linked-list of thread states per hash bucket40 struct llist_node bucket_node;41 42 // Thread-id as determined by _PyThread_Id()43 uintptr_t tid;44 45 // Objects with refcounts to be merged (protected by bucket mutex)46 _PyObjectStack objects_to_merge;47 48 // Local stack of objects to be merged (not accessed by other threads)49 _PyObjectStack local_objects_to_merge;50};51 52// Initialize/finalize the per-thread biased reference counting state53void _Py_brc_init_thread(PyThreadState *tstate);54void _Py_brc_remove_thread(PyThreadState *tstate);55 56// Initialize per-interpreter state57void _Py_brc_init_state(PyInterpreterState *interp);58 59void _Py_brc_after_fork(PyInterpreterState *interp);60 61// Enqueues an object to be merged by it's owning thread (tid). This62// steals a reference to the object.63void _Py_brc_queue_object(PyObject *ob);64 65// Merge the refcounts of queued objects for the current thread.66void _Py_brc_merge_refcounts(PyThreadState *tstate);67 68#endif /* Py_GIL_DISABLED */69 70#ifdef __cplusplus71}72#endif73#endif /* !Py_INTERNAL_BRC_H */74 