| 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_object_stack.h" // _PyObjectStack |
| 7 |
|
| 8 |
#ifdef __cplusplus |
| 9 |
extern "C" { |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef Py_BUILD_CORE |
| 13 |
# error "this header requires Py_BUILD_CORE define" |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifdef Py_GIL_DISABLED |
| 17 |
|
| 18 |
// Prime number to avoid correlations with memory addresses. |
| 19 |
#define _Py_BRC_NUM_BUCKETS 257 |
| 20 |
|
| 21 |
// Hash table bucket |
| 22 |
struct _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 state |
| 31 |
struct _brc_state { |
| 32 |
// Hash table of thread states by thread-id. Thread states within a bucket |
| 33 |
// are chained using a doubly-linked list. |
| 34 |
struct _brc_bucket table[_Py_BRC_NUM_BUCKETS]; |
| 35 |
}; |
| 36 |
|
| 37 |
// Per-thread biased reference counting state |
| 38 |
struct _brc_thread_state { |
| 39 |
// Linked-list of thread states per hash bucket |
| 40 |
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 state |
| 53 |
void _Py_brc_init_thread(PyThreadState *tstate); |
| 54 |
void _Py_brc_remove_thread(PyThreadState *tstate); |
| 55 |
|
| 56 |
// Initialize per-interpreter state |
| 57 |
void _Py_brc_init_state(PyInterpreterState *interp); |
| 58 |
|
| 59 |
void _Py_brc_after_fork(PyInterpreterState *interp); |
| 60 |
|
| 61 |
// Enqueues an object to be merged by it's owning thread (tid). This |
| 62 |
// steals a reference to the object. |
| 63 |
void _Py_brc_queue_object(PyObject *ob); |
| 64 |
|
| 65 |
// Merge the refcounts of queued objects for the current thread. |
| 66 |
void _Py_brc_merge_refcounts(PyThreadState *tstate); |
| 67 |
|
| 68 |
#endif /* Py_GIL_DISABLED */ |
| 69 |
|
| 70 |
#ifdef __cplusplus |
| 71 |
} |
| 72 |
#endif |
| 73 |
#endif /* !Py_INTERNAL_BRC_H */ |