-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-124397: Add threading.iter_locked #133908
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
Open
eendebakpt
wants to merge
13
commits into
python:main
Choose a base branch
from
eendebakpt:iter_locked
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6313b3d
Add iter_locked
eendebakpt 5ced0a9
cleanup
eendebakpt 110332e
cleanup
eendebakpt b675631
📜🤖 Added by blurb_it.
blurb-it[bot] 4406050
update docs
eendebakpt d30ace8
review comments
eendebakpt baec5bc
use explicit lock
eendebakpt e6ea285
Merge branch 'main' into iter_locked
eendebakpt 7315c49
fix import
eendebakpt b7c9d77
cleanup after merge to main
eendebakpt bb98c52
reduce duration of test
eendebakpt 946ee51
use recursive lock
eendebakpt 7e3653a
Merge branch 'main' into iter_locked
eendebakpt 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
review comments
- Loading branch information
commit d30ace8e7746d3f6c3ed3c8e3cf19999fab8ad03
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
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 |
|---|---|---|
|
|
@@ -747,6 +747,7 @@ static PyType_Spec ThreadHandle_Type_spec = { | |
| typedef struct { | ||
| PyObject_HEAD | ||
| PyObject *it; | ||
| PyMutex lock; | ||
| } iter_locked_object; | ||
|
|
||
| #define iter_locked_object_CAST(op) ((iter_locked_object *)(op)) | ||
|
|
@@ -774,6 +775,7 @@ _thread_iter_locked_impl(PyTypeObject *type, PyObject *iterable) | |
| return NULL; | ||
| } | ||
| il->it = it; | ||
| il->lock = (PyMutex){0}; | ||
|
|
||
| return (PyObject *)il; | ||
| } | ||
|
|
@@ -804,14 +806,17 @@ iter_locked_next(PyObject *op) | |
| iter_locked_object *lz = iter_locked_object_CAST(op); | ||
| PyObject *result = NULL; | ||
|
|
||
| Py_BEGIN_CRITICAL_SECTION(op); // lock on op or lz->it? | ||
| // we cannot use Py_BEGIN_CRITICAL_SECTION as it is not available in the normal build | ||
| PyMutex_Lock(&(lz->lock)); | ||
|
|
||
| PyObject *it = lz->it; | ||
| result = PyIter_Next(it); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
| if (result == NULL) { | ||
| /* Note: StopIteration is already cleared by PyIter_Next() */ | ||
| /* If PyErr_Occurred() we will also return NULL*/ | ||
| } | ||
| Py_END_CRITICAL_SECTION(); | ||
| PyMutex_Unlock(&(lz->lock)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
time.sleep(0)(or with small non-zero argument) orEvent.wait()with a timeout to force or significantly increase the chance of concurrent access.