Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/internal/pycore_pyatomic_ft_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern "C" {
_Py_atomic_store_uintptr_release(&value, new_value)
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) \
_Py_atomic_store_ssize_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_SSIZE_RELEASE(value, new_value) \
_Py_atomic_store_ssize_release(&value, new_value)
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) \
_Py_atomic_store_uint8_relaxed(&value, new_value)
#define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) \
Expand Down Expand Up @@ -133,6 +135,7 @@ extern "C" {
#define FT_ATOMIC_STORE_PTR_RELEASE(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_SSIZE_RELEASE(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT16_RELAXED(value, new_value) value = new_value
#define FT_ATOMIC_STORE_UINT32_RELAXED(value, new_value) value = new_value
Expand Down
64 changes: 64 additions & 0 deletions Lib/test/test_free_threading/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,70 @@ def repr_set():
for set_repr in set_reprs:
self.assertIn(set_repr, ("set()", "{1, 2, 3, 4, 5, 6, 7, 8}"))

def test_contains_mutate(self):
"""Test set contains operation combined with mutation."""
barrier = Barrier(2)
s = set()
done = False

NUM_ITEMS = 2_000
NUM_LOOPS = 20

def read_set():
barrier.wait()
while not done:
for i in range(NUM_ITEMS):
item = i >> 1
result = item in s

def mutate_set():
nonlocal done
barrier.wait()
for i in range(NUM_LOOPS):
s.clear()
for j in range(NUM_ITEMS):
s.add(j)
for j in range(NUM_ITEMS):
s.discard(j)
# executes the set_swap_bodies() function
s.__iand__(set(k for k in range(10, 20)))
done = True

threads = [Thread(target=read_set), Thread(target=mutate_set)]
for t in threads:
t.start()
for t in threads:
t.join()

def test_contains_frozenset(self):
barrier = Barrier(3)
done = False

NUM_ITEMS = 2_000
NUM_LOOPS = 20

s = frozenset()
def make_set():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the make_set here in a thread? It just assigns the same frozenset to s each time.

Maybe change make_set so that it creates a different frozenset each time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. The make_set() binds s to a new frozenset object (at least I intend it to).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The s is indeed updated with a new frozenset, but it is a frozenset with the same elements each time. I was thinking about something like

def make_set():
            nonlocal s
            barrier.wait()
            num_elements = NUM_ITEMS
            while not done:
               num_elements = (num_elements  + 17 %) NUM_ITEMS
                s = frozenset(range(num_elements))

But what are we testing here. There is no concurrent mutation of the s, so this test is not specific to the free-threading build I think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, I'm not sure what I was thinking when writing that test. It doesn't test what I intended. I re-wrote it so that a set() is used as the key and it mutated.

nonlocal s
barrier.wait()
while not done:
s = frozenset(range(NUM_ITEMS))

def read_set():
nonlocal done
barrier.wait()
for _ in range(NUM_LOOPS):
for i in range(NUM_ITEMS):
item = i >> 1
result = item in s
done = True

threads = [Thread(target=read_set), Thread(target=read_set), Thread(target=make_set)]
for t in threads:
t.start()
for t in threads:
t.join()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For the free-threaded build, avoid locking the :class:`set` object for the
``__contains__`` method.
4 changes: 1 addition & 3 deletions Objects/clinic/setobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading