-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-119247: Add macros to use PySequence_Fast safely in free-threaded build #119315
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b22f6e2
Initial work on critical sections for fast sequences while developing…
MojoVampire 2b4fe19
Test for efficacy of fast sequence locking macro on str.join, both te…
MojoVampire 536defc
Blurb for change
MojoVampire 0475c8e
Update Include/internal/pycore_critical_section.h
MojoVampire 1f3cec0
Tweak comments per review
MojoVampire 707852d
Use braced if per review
MojoVampire 66f0e6c
Per review, refactor extend test to extend multiple times, and reduce…
MojoVampire 515ee56
Remove listobject.h include per review (all users of pycore_critical_…
MojoVampire 254a5a5
Reduce readers in test_racing_join_replace to 10, use Event to synchr…
MojoVampire 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
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,75 @@ | ||
import sys | ||
import unittest | ||
|
||
from itertools import cycle | ||
from threading import Event, Thread | ||
from unittest import TestCase | ||
|
||
from test.support import threading_helper | ||
|
||
@threading_helper.requires_working_threading() | ||
class TestStr(TestCase): | ||
def test_racing_join_extend(self): | ||
'''Test joining a string being extended by another thread''' | ||
l = [] | ||
ITERS = 100 | ||
READERS = 10 | ||
done_event = Event() | ||
def writer_func(): | ||
for i in range(ITERS): | ||
l.extend(map(str, range(i))) | ||
l.clear() | ||
done_event.set() | ||
def reader_func(): | ||
while not done_event.is_set(): | ||
''.join(l) | ||
writer = Thread(target=writer_func) | ||
readers = [] | ||
for x in range(READERS): | ||
reader = Thread(target=reader_func) | ||
readers.append(reader) | ||
reader.start() | ||
|
||
writer.start() | ||
writer.join() | ||
for reader in readers: | ||
reader.join() | ||
|
||
def test_racing_join_replace(self): | ||
''' | ||
Test joining a string of characters being replaced with ephemeral | ||
strings by another thread. | ||
''' | ||
l = [*'abcdefg'] | ||
MAX_ORDINAL = 1_000 | ||
READERS = 10 | ||
done_event = Event() | ||
|
||
def writer_func(): | ||
for i, c in zip(cycle(range(len(l))), | ||
map(chr, range(128, MAX_ORDINAL))): | ||
l[i] = c | ||
done_event.set() | ||
|
||
def reader_func(): | ||
while not done_event.is_set(): | ||
''.join(l) | ||
''.join(l) | ||
''.join(l) | ||
''.join(l) | ||
|
||
writer = Thread(target=writer_func) | ||
readers = [] | ||
for x in range(READERS): | ||
reader = Thread(target=reader_func) | ||
readers.append(reader) | ||
reader.start() | ||
|
||
writer.start() | ||
writer.join() | ||
for reader in readers: | ||
reader.join() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/C API/2024-05-21-11-35-11.gh-issue-119247.U6n6mh.rst
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,4 @@ | ||
Added ``Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST`` and | ||
``Py_END_CRITICAL_SECTION_SEQUENCE_FAST`` macros to make it possible to use | ||
PySequence_Fast APIs safely when free-threaded, and update str.join to work | ||
without the GIL using them. |
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.