• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_BRC_H
2 #define Py_INTERNAL_BRC_H
3 
4 #include <stdint.h>
5 #include "pycore_llist.h"           // struct llist_node
6 #include "pycore_lock.h"            // PyMutex
7 #include "pycore_object_stack.h"    // _PyObjectStack
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #ifndef Py_BUILD_CORE
14 #  error "this header requires Py_BUILD_CORE define"
15 #endif
16 
17 #ifdef Py_GIL_DISABLED
18 
19 // Prime number to avoid correlations with memory addresses.
20 #define _Py_BRC_NUM_BUCKETS 257
21 
22 // Hash table bucket
23 struct _brc_bucket {
24     // Mutex protects both the bucket and thread state queues in this bucket.
25     PyMutex mutex;
26 
27     // Linked list of _PyThreadStateImpl objects hashed to this bucket.
28     struct llist_node root;
29 };
30 
31 // Per-interpreter biased reference counting state
32 struct _brc_state {
33     // Hash table of thread states by thread-id. Thread states within a bucket
34     // are chained using a doubly-linked list.
35     struct _brc_bucket table[_Py_BRC_NUM_BUCKETS];
36 };
37 
38 // Per-thread biased reference counting state
39 struct _brc_thread_state {
40     // Linked-list of thread states per hash bucket
41     struct llist_node bucket_node;
42 
43     // Thread-id as determined by _PyThread_Id()
44     uintptr_t tid;
45 
46     // Objects with refcounts to be merged (protected by bucket mutex)
47     _PyObjectStack objects_to_merge;
48 
49     // Local stack of objects to be merged (not accessed by other threads)
50     _PyObjectStack local_objects_to_merge;
51 };
52 
53 // Initialize/finalize the per-thread biased reference counting state
54 void _Py_brc_init_thread(PyThreadState *tstate);
55 void _Py_brc_remove_thread(PyThreadState *tstate);
56 
57 // Initialize per-interpreter state
58 void _Py_brc_init_state(PyInterpreterState *interp);
59 
60 void _Py_brc_after_fork(PyInterpreterState *interp);
61 
62 // Enqueues an object to be merged by it's owning thread (tid). This
63 // steals a reference to the object.
64 void _Py_brc_queue_object(PyObject *ob);
65 
66 // Merge the refcounts of queued objects for the current thread.
67 void _Py_brc_merge_refcounts(PyThreadState *tstate);
68 
69 #endif /* Py_GIL_DISABLED */
70 
71 #ifdef __cplusplus
72 }
73 #endif
74 #endif /* !Py_INTERNAL_BRC_H */
75