Skip to content

Commit 08d4e75

Browse files
use relaxed atomics for it_index
1 parent 97d0011 commit 08d4e75

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Lib/test/test_free_threading/test_list.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ def reader_func():
7171
for reader in readers:
7272
reader.join()
7373

74+
def test_list_iterator_reduce(self):
75+
l = list(range(100))
76+
77+
for it in [iter(l), iter(reversed(l))]:
78+
79+
def reduce():
80+
for i in range(100):
81+
it.__reduce__()
82+
83+
def setstate():
84+
for i in range(100):
85+
it.__setstate__(i)
86+
87+
t1 = Thread(target=reduce)
88+
t2 = Thread(target=setstate)
89+
90+
with threading_helper.start_threads([t1, t2]):
91+
pass
7492

7593
if __name__ == "__main__":
7694
unittest.main()

Objects/listobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3988,12 +3988,12 @@ listiter_setstate(PyObject *self, PyObject *state)
39883988
Py_ssize_t index = PyLong_AsSsize_t(state);
39893989
if (index == -1 && PyErr_Occurred())
39903990
return NULL;
3991-
if (it->it_seq != NULL) {
3991+
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index) >= 0) {
39923992
if (index < -1)
39933993
index = -1;
39943994
else if (index > PyList_GET_SIZE(it->it_seq))
39953995
index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
3996-
it->it_index = index;
3996+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
39973997
}
39983998
Py_RETURN_NONE;
39993999
}
@@ -4140,12 +4140,12 @@ listreviter_setstate(PyObject *self, PyObject *state)
41404140
Py_ssize_t index = PyLong_AsSsize_t(state);
41414141
if (index == -1 && PyErr_Occurred())
41424142
return NULL;
4143-
if (it->it_seq != NULL) {
4143+
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(it->it_index) >= 0) {
41444144
if (index < -1)
41454145
index = -1;
41464146
else if (index > PyList_GET_SIZE(it->it_seq) - 1)
41474147
index = PyList_GET_SIZE(it->it_seq) - 1;
4148-
it->it_index = index;
4148+
FT_ATOMIC_STORE_SSIZE_RELAXED(it->it_index, index);
41494149
}
41504150
Py_RETURN_NONE;
41514151
}

0 commit comments

Comments
 (0)