Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:45

0001 #ifndef Py_INTERNAL_BRC_H
0002 #define Py_INTERNAL_BRC_H
0003 
0004 #include <stdint.h>
0005 #include "pycore_llist.h"           // struct llist_node
0006 #include "pycore_lock.h"            // PyMutex
0007 #include "pycore_object_stack.h"    // _PyObjectStack
0008 
0009 #ifdef __cplusplus
0010 extern "C" {
0011 #endif
0012 
0013 #ifndef Py_BUILD_CORE
0014 #  error "this header requires Py_BUILD_CORE define"
0015 #endif
0016 
0017 #ifdef Py_GIL_DISABLED
0018 
0019 // Prime number to avoid correlations with memory addresses.
0020 #define _Py_BRC_NUM_BUCKETS 257
0021 
0022 // Hash table bucket
0023 struct _brc_bucket {
0024     // Mutex protects both the bucket and thread state queues in this bucket.
0025     PyMutex mutex;
0026 
0027     // Linked list of _PyThreadStateImpl objects hashed to this bucket.
0028     struct llist_node root;
0029 };
0030 
0031 // Per-interpreter biased reference counting state
0032 struct _brc_state {
0033     // Hash table of thread states by thread-id. Thread states within a bucket
0034     // are chained using a doubly-linked list.
0035     struct _brc_bucket table[_Py_BRC_NUM_BUCKETS];
0036 };
0037 
0038 // Per-thread biased reference counting state
0039 struct _brc_thread_state {
0040     // Linked-list of thread states per hash bucket
0041     struct llist_node bucket_node;
0042 
0043     // Thread-id as determined by _PyThread_Id()
0044     uintptr_t tid;
0045 
0046     // Objects with refcounts to be merged (protected by bucket mutex)
0047     _PyObjectStack objects_to_merge;
0048 
0049     // Local stack of objects to be merged (not accessed by other threads)
0050     _PyObjectStack local_objects_to_merge;
0051 };
0052 
0053 // Initialize/finalize the per-thread biased reference counting state
0054 void _Py_brc_init_thread(PyThreadState *tstate);
0055 void _Py_brc_remove_thread(PyThreadState *tstate);
0056 
0057 // Initialize per-interpreter state
0058 void _Py_brc_init_state(PyInterpreterState *interp);
0059 
0060 void _Py_brc_after_fork(PyInterpreterState *interp);
0061 
0062 // Enqueues an object to be merged by it's owning thread (tid). This
0063 // steals a reference to the object.
0064 void _Py_brc_queue_object(PyObject *ob);
0065 
0066 // Merge the refcounts of queued objects for the current thread.
0067 void _Py_brc_merge_refcounts(PyThreadState *tstate);
0068 
0069 #endif /* Py_GIL_DISABLED */
0070 
0071 #ifdef __cplusplus
0072 }
0073 #endif
0074 #endif /* !Py_INTERNAL_BRC_H */