@@ -1966,7 +1966,7 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19661966 PyObject * item ;
19671967
19681968 if (it -> deque -> state != it -> state ) {
1969- it -> counter = 0 ;
1969+ FT_ATOMIC_STORE_SSIZE_RELAXED ( it -> counter , 0 ) ;
19701970 PyErr_SetString (PyExc_RuntimeError ,
19711971 "deque mutated during iteration" );
19721972 return NULL ;
@@ -1978,7 +1978,7 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19781978
19791979 item = it -> b -> data [it -> index ];
19801980 it -> index ++ ;
1981- it -> counter -- ;
1981+ FT_ATOMIC_STORE_SSIZE_RELAXED ( it -> counter , it -> counter - 1 ) ;
19821982 if (it -> index == BLOCKLEN && it -> counter > 0 ) {
19831983 CHECK_NOT_END (it -> b -> rightlink );
19841984 it -> b = it -> b -> rightlink ;
@@ -1987,6 +1987,14 @@ dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque)
19871987 return Py_NewRef (item );
19881988}
19891989
1990+ /* The mutable fields of a deque iterator (b, index and counter) are only
1991+ * written while holding the critical section of the deque it iterates over,
1992+ * never the iterator's own lock. Concurrent next() calls on a shared
1993+ * iterator are therefore serialized by the deque's lock, and locking the
1994+ * iterator as well is unnecessary. This halves the number of atomic
1995+ * operations per item in the free-threaded build. __length_hint__ reads
1996+ * counter without locking, so writes to it use relaxed atomic stores.
1997+ */
19901998static PyObject *
19911999dequeiter_next (PyObject * op )
19922000{
@@ -1995,9 +2003,9 @@ dequeiter_next(PyObject *op)
19952003 // It's safe to access it->deque without holding the per-object lock for it
19962004 // here; it->deque is only assigned during construction of it.
19972005 dequeobject * deque = it -> deque ;
1998- Py_BEGIN_CRITICAL_SECTION2 ( it , deque );
2006+ Py_BEGIN_CRITICAL_SECTION ( deque );
19992007 result = dequeiter_next_lock_held (it , deque );
2000- Py_END_CRITICAL_SECTION2 ();
2008+ Py_END_CRITICAL_SECTION ();
20012009
20022010 return result ;
20032011}
@@ -2056,10 +2064,10 @@ dequeiter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
20562064 // here; it->deque is only assigned during construction of it.
20572065 dequeobject * deque = it -> deque ;
20582066 Py_ssize_t size , counter ;
2059- Py_BEGIN_CRITICAL_SECTION2 ( it , deque );
2067+ Py_BEGIN_CRITICAL_SECTION ( deque );
20602068 size = Py_SIZE (deque );
20612069 counter = it -> counter ;
2062- Py_END_CRITICAL_SECTION2 ();
2070+ Py_END_CRITICAL_SECTION ();
20632071 return Py_BuildValue ("O(On)" , ty , deque , size - counter );
20642072}
20652073
@@ -2119,7 +2127,7 @@ dequereviter_next_lock_held(dequeiterobject *it, dequeobject *deque)
21192127 return NULL ;
21202128
21212129 if (it -> deque -> state != it -> state ) {
2122- it -> counter = 0 ;
2130+ FT_ATOMIC_STORE_SSIZE_RELAXED ( it -> counter , 0 ) ;
21232131 PyErr_SetString (PyExc_RuntimeError ,
21242132 "deque mutated during iteration" );
21252133 return NULL ;
@@ -2129,7 +2137,7 @@ dequereviter_next_lock_held(dequeiterobject *it, dequeobject *deque)
21292137
21302138 item = it -> b -> data [it -> index ];
21312139 it -> index -- ;
2132- it -> counter -- ;
2140+ FT_ATOMIC_STORE_SSIZE_RELAXED ( it -> counter , it -> counter - 1 ) ;
21332141 if (it -> index < 0 && it -> counter > 0 ) {
21342142 CHECK_NOT_END (it -> b -> leftlink );
21352143 it -> b = it -> b -> leftlink ;
@@ -2146,9 +2154,10 @@ dequereviter_next(PyObject *self)
21462154 // It's safe to access it->deque without holding the per-object lock for it
21472155 // here; it->deque is only assigned during construction of it.
21482156 dequeobject * deque = it -> deque ;
2149- Py_BEGIN_CRITICAL_SECTION2 (it , deque );
2157+ // See the comment above dequeiter_next().
2158+ Py_BEGIN_CRITICAL_SECTION (deque );
21502159 item = dequereviter_next_lock_held (it , deque );
2151- Py_END_CRITICAL_SECTION2 ();
2160+ Py_END_CRITICAL_SECTION ();
21522161 return item ;
21532162}
21542163
0 commit comments