-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-110481: Implement inter-thread queue for biased reference counting #114824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
colesbury
merged 11 commits into
python:main
from
colesbury:gh-110481-inter-thread-queue
Feb 9, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
544adf2
gh-110481: Implement inter-thread queue for biased reference counting
colesbury ee01891
Update comments from review
colesbury cbfaf6b
Fix _Py_brc_remove_thread
colesbury 127c595
Include pycore_freelist.h in pycore_object_stack.h
colesbury f00252a
Move _Py_brc_after_fork up
colesbury bc68b9a
Merge branch 'main' into gh-110481-inter-thread-queue
colesbury 459638f
Merge branch 'main' into gh-110481-inter-thread-queue
colesbury 51c6625
Fix refleak tests.
colesbury c631856
Fix test_weakref.test_threaded_weak_key_dict_copy refleak
colesbury 8311e18
Merge branch 'main' into gh-110481-inter-thread-queue
colesbury 97cf964
Remove duplciate _Py_AddRefTotal from merge commit
colesbury File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#ifndef Py_INTERNAL_BRC_H | ||
#define Py_INTERNAL_BRC_H | ||
|
||
#include <stdint.h> | ||
#include "pycore_llist.h" // struct llist_node | ||
#include "pycore_lock.h" // PyMutex | ||
#include "pycore_object_stack.h" // _PyObjectStack | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
#ifdef Py_GIL_DISABLED | ||
|
||
// Prime number to avoid correlations with memory addresses. | ||
#define _Py_BRC_NUM_BUCKETS 257 | ||
ericsnowcurrently marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Hash table bucket | ||
struct _brc_bucket { | ||
// Mutex protects both the bucket and thread state queues in this bucket. | ||
PyMutex mutex; | ||
|
||
// Linked list of _PyThreadStateImpl objects hashed to this bucket. | ||
struct llist_node root; | ||
}; | ||
|
||
// Per-interpreter biased reference counting state | ||
struct _brc_state { | ||
// Hash table of thread states by thread-id. Thread states within a bucket | ||
// are chained using a doubly-linked list. | ||
struct _brc_bucket table[_Py_BRC_NUM_BUCKETS]; | ||
}; | ||
|
||
// Per-thread biased reference counting state | ||
struct _brc_thread_state { | ||
// Linked-list of thread states per hash bucket | ||
struct llist_node bucket_node; | ||
|
||
// Thread-id as determined by _PyThread_Id() | ||
uintptr_t tid; | ||
|
||
// Objects with refcounts to be merged (protected by bucket mutex) | ||
_PyObjectStack objects_to_merge; | ||
|
||
// Local stack of objects to be merged (not accessed by other threads) | ||
_PyObjectStack local_objects_to_merge; | ||
}; | ||
|
||
// Initialize/finalize the per-thread biased reference counting state | ||
void _Py_brc_init_thread(PyThreadState *tstate); | ||
void _Py_brc_remove_thread(PyThreadState *tstate); | ||
|
||
// Initialize per-interpreter state | ||
void _Py_brc_init_state(PyInterpreterState *interp); | ||
|
||
void _Py_brc_after_fork(PyInterpreterState *interp); | ||
|
||
// Enqueues an object to be merged by it's owning thread (tid). This | ||
// steals a reference to the object. | ||
void _Py_brc_queue_object(PyObject *ob); | ||
|
||
// Merge the refcounts of queued objects for the current thread. | ||
void _Py_brc_merge_refcounts(PyThreadState *tstate); | ||
|
||
#endif /* Py_GIL_DISABLED */ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif /* !Py_INTERNAL_BRC_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.