Skip to content

Commit 90d184c

Browse files
committed
Make objtuple use atomic types
1 parent 0e55bc9 commit 90d184c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

vm/src/obj/objtuple.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::Cell;
1+
use crossbeam_utils::atomic::AtomicCell;
22
use std::fmt;
33

44
use super::objiter;
@@ -160,7 +160,7 @@ impl PyTuple {
160160
#[pymethod(name = "__iter__")]
161161
fn iter(zelf: PyRef<Self>) -> PyTupleIterator {
162162
PyTupleIterator {
163-
position: Cell::new(0),
163+
position: AtomicCell::new(0),
164164
tuple: zelf,
165165
}
166166
}
@@ -243,7 +243,7 @@ impl PyTuple {
243243
#[pyclass]
244244
#[derive(Debug)]
245245
pub struct PyTupleIterator {
246-
position: Cell<usize>,
246+
position: AtomicCell<usize>,
247247
tuple: PyTupleRef,
248248
}
249249

@@ -257,10 +257,10 @@ impl PyValue for PyTupleIterator {
257257
impl PyTupleIterator {
258258
#[pymethod(name = "__next__")]
259259
fn next(&self, vm: &VirtualMachine) -> PyResult {
260-
if self.position.get() < self.tuple.as_slice().len() {
261-
let ret = self.tuple.as_slice()[self.position.get()].clone();
262-
self.position.set(self.position.get() + 1);
263-
Ok(ret)
260+
let pos = self.position.load();
261+
if let Some(obj) = self.tuple.as_slice().get(pos) {
262+
self.position.store(pos + 1);
263+
Ok(obj.clone())
264264
} else {
265265
Err(objiter::new_stop_iteration(vm))
266266
}

0 commit comments

Comments
 (0)