mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-110481: Implement inter-thread queue for biased reference counting (#114824)
Biased reference counting maintains two refcount fields in each object: `ob_ref_local` and `ob_ref_shared`. The true refcount is the sum of these two fields. In some cases, when refcounting operations are split across threads, the ob_ref_shared field can be negative (although the total refcount must be at least zero). In this case, the thread that decremented the refcount requests that the owning thread give up ownership and merge the refcount fields.
This commit is contained in:
parent
a225520af9
commit
a3af3cb4f4
21 changed files with 418 additions and 11 deletions
|
|
@ -611,6 +611,9 @@ init_interpreter(PyInterpreterState *interp,
|
|||
_PyGC_InitState(&interp->gc);
|
||||
PyConfig_InitPythonConfig(&interp->config);
|
||||
_PyType_InitCache(interp);
|
||||
#ifdef Py_GIL_DISABLED
|
||||
_Py_brc_init_state(interp);
|
||||
#endif
|
||||
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
|
||||
interp->monitors.tools[i] = 0;
|
||||
}
|
||||
|
|
@ -1336,6 +1339,11 @@ init_threadstate(_PyThreadStateImpl *_tstate,
|
|||
tstate->datastack_limit = NULL;
|
||||
tstate->what_event = -1;
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
// Initialize biased reference counting inter-thread queue
|
||||
_Py_brc_init_thread(tstate);
|
||||
#endif
|
||||
|
||||
if (interp->stoptheworld.requested || _PyRuntime.stoptheworld.requested) {
|
||||
// Start in the suspended state if there is an ongoing stop-the-world.
|
||||
tstate->state = _Py_THREAD_SUSPENDED;
|
||||
|
|
@ -1561,6 +1569,9 @@ PyThreadState_Clear(PyThreadState *tstate)
|
|||
_PyFreeListState *freelist_state = &((_PyThreadStateImpl*)tstate)->freelist_state;
|
||||
_Py_ClearFreeLists(freelist_state, 1);
|
||||
_PySlice_ClearCache(freelist_state);
|
||||
|
||||
// Remove ourself from the biased reference counting table of threads.
|
||||
_Py_brc_remove_thread(tstate);
|
||||
#endif
|
||||
|
||||
_PyThreadState_ClearMimallocHeaps(tstate);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue