Refactor rwlock -> mutex because multiple read is rare

This commit is contained in:
Kangzhi Shi
2021-09-28 07:31:35 +02:00
parent 1ade56fa84
commit 8b9c33c12a
10 changed files with 85 additions and 87 deletions

View File

@@ -5,7 +5,7 @@ use super::{
use crate::common::{
borrow::{BorrowedValue, BorrowedValueMut},
lock::{
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard,
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock, PyRwLockReadGuard,
PyRwLockWriteGuard,
},
};
@@ -719,7 +719,7 @@ impl Unhashable for PyByteArray {}
impl Iterable for PyByteArray {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyByteArrayIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
}
@@ -732,7 +732,7 @@ impl Iterable for PyByteArray {
#[pyclass(module = false, name = "bytearray_iterator")]
#[derive(Debug)]
pub struct PyByteArrayIterator {
internal: PyRwLock<PositionIterInternal<PyByteArrayRef>>,
internal: PyMutex<PositionIterInternal<PyByteArrayRef>>,
}
impl PyValue for PyByteArrayIterator {
@@ -745,24 +745,24 @@ impl PyValue for PyByteArrayIterator {
impl PyByteArrayIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_iter_reduce(|x| x.clone().into_object(), vm)
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
}
impl IteratorIterable for PyByteArrayIterator {}
impl SlotIterator for PyByteArrayIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|bytearray, pos| {
let buf = bytearray.borrow_buf();
buf.get(pos)

View File

@@ -17,8 +17,10 @@ use crate::{
PyRef, PyResult, PyValue, TryFromBorrowedObject, TypeProtocol, VirtualMachine,
};
use bstr::ByteSlice;
use rustpython_common::borrow::{BorrowedValue, BorrowedValueMut};
use rustpython_common::lock::PyRwLock;
use rustpython_common::{
borrow::{BorrowedValue, BorrowedValueMut},
lock::PyMutex,
};
use std::mem::size_of;
use std::ops::Deref;
@@ -574,7 +576,7 @@ impl Comparable for PyBytes {
impl Iterable for PyBytes {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyBytesIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
}
@@ -583,7 +585,7 @@ impl Iterable for PyBytes {
#[pyclass(module = false, name = "bytes_iterator")]
#[derive(Debug)]
pub struct PyBytesIterator {
internal: PyRwLock<PositionIterInternal<PyBytesRef>>,
internal: PyMutex<PositionIterInternal<PyBytesRef>>,
}
impl PyValue for PyBytesIterator {
@@ -596,25 +598,25 @@ impl PyValue for PyBytesIterator {
impl PyBytesIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_iter_reduce(|x| x.clone().into_object(), vm)
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
}
impl IteratorIterable for PyBytesIterator {}
impl SlotIterator for PyBytesIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|bytes, pos| {
bytes
.as_bytes()

View File

@@ -14,7 +14,7 @@ use crate::{
PyAttributes, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
PyResult, PyValue, TryFromObject, TypeProtocol,
};
use rustpython_common::lock::PyRwLock;
use rustpython_common::lock::PyMutex;
use std::fmt;
use std::mem::size_of;
@@ -706,7 +706,7 @@ macro_rules! dict_iterator {
#[derive(Debug)]
pub(crate) struct $iter_name {
pub size: dictdatatype::DictSize,
pub internal: PyRwLock<PositionIterInternal<PyDictRef>>,
pub internal: PyMutex<PositionIterInternal<PyDictRef>>,
}
impl PyValue for $iter_name {
@@ -720,13 +720,13 @@ macro_rules! dict_iterator {
fn new(dict: PyDictRef) -> Self {
$iter_name {
size: dict.size(),
internal: PyRwLock::new(PositionIterInternal::new(dict, 0)),
internal: PyMutex::new(PositionIterInternal::new(dict, 0)),
}
}
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|_| self.size.entries_size)
self.internal.lock().length_hint(|_| self.size.entries_size)
}
}
@@ -734,7 +734,7 @@ macro_rules! dict_iterator {
impl SlotIterator for $iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let mut internal = zelf.internal.write();
let mut internal = zelf.internal.lock();
if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
@@ -762,7 +762,7 @@ macro_rules! dict_iterator {
#[derive(Debug)]
pub(crate) struct $reverse_iter_name {
pub size: dictdatatype::DictSize,
internal: PyRwLock<PositionIterInternal<PyDictRef>>,
internal: PyMutex<PositionIterInternal<PyDictRef>>,
}
impl PyValue for $reverse_iter_name {
@@ -778,14 +778,14 @@ macro_rules! dict_iterator {
let position = size.entries_size.saturating_sub(1);
$reverse_iter_name {
size,
internal: PyRwLock::new(PositionIterInternal::new(dict, position)),
internal: PyMutex::new(PositionIterInternal::new(dict, position)),
}
}
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal
.read()
.lock()
.rev_length_hint(|_| self.size.entries_size)
}
}
@@ -794,7 +794,7 @@ macro_rules! dict_iterator {
impl SlotIterator for $reverse_iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let mut internal = zelf.internal.write();
let mut internal = zelf.internal.lock();
if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;

View File

@@ -1,5 +1,5 @@
use super::{IterStatus, PositionIterInternal, PyIntRef, PyTypeRef};
use crate::common::lock::PyRwLock;
use crate::common::lock::{PyMutex, PyRwLock};
use crate::{
function::OptionalArg,
protocol::{PyIter, PyIterReturn},
@@ -67,7 +67,7 @@ impl SlotIterator for PyEnumerate {
#[pyclass(module = false, name = "reversed")]
#[derive(Debug)]
pub struct PyReverseSequenceIterator {
internal: PyRwLock<PositionIterInternal<PyObjectRef>>,
internal: PyMutex<PositionIterInternal<PyObjectRef>>,
}
impl PyValue for PyReverseSequenceIterator {
@@ -81,13 +81,13 @@ impl PyReverseSequenceIterator {
pub fn new(obj: PyObjectRef, len: usize) -> Self {
let position = len.saturating_sub(1);
Self {
internal: PyRwLock::new(PositionIterInternal::new(obj, position)),
internal: PyMutex::new(PositionIterInternal::new(obj, position)),
}
}
#[pymethod(magic)]
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<usize> {
let internal = self.internal.read();
let internal = self.internal.lock();
if let IterStatus::Active(obj) = &internal.status {
if internal.position <= vm.obj_len(obj)? {
return Ok(internal.position + 1);
@@ -98,13 +98,13 @@ impl PyReverseSequenceIterator {
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_reversed_reduce(|x| x.clone(), vm)
}
}
@@ -113,7 +113,7 @@ impl IteratorIterable for PyReverseSequenceIterator {}
impl SlotIterator for PyReverseSequenceIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal
.write()
.lock()
.rev_next(|obj, pos| obj.get_item(pos, vm), vm)
}
}

View File

@@ -10,7 +10,7 @@ use crate::{
ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use rustpython_common::lock::{PyRwLock, PyRwLockUpgradableReadGuard};
use rustpython_common::lock::{PyMutex, PyRwLock, PyRwLockUpgradableReadGuard};
/// Marks status of iterator.
#[derive(Debug, Clone)]
@@ -170,7 +170,7 @@ impl<T> PositionIterInternal<T> {
#[pyclass(module = false, name = "iterator")]
#[derive(Debug)]
pub struct PySequenceIterator {
internal: PyRwLock<PositionIterInternal<PyObjectRef>>,
internal: PyMutex<PositionIterInternal<PyObjectRef>>,
}
impl PyValue for PySequenceIterator {
@@ -183,13 +183,13 @@ impl PyValue for PySequenceIterator {
impl PySequenceIterator {
pub fn new(obj: PyObjectRef) -> Self {
Self {
internal: PyRwLock::new(PositionIterInternal::new(obj, 0)),
internal: PyMutex::new(PositionIterInternal::new(obj, 0)),
}
}
#[pymethod(magic)]
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef {
let internal = self.internal.read();
let internal = self.internal.lock();
if let IterStatus::Active(obj) = &internal.status {
vm.obj_len(obj)
.map(|x| PyInt::from(x).into_object(vm))
@@ -201,12 +201,12 @@ impl PySequenceIterator {
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal.read().builtin_iter_reduce(|x| x.clone(), vm)
self.internal.lock().builtin_iter_reduce(|x| x.clone(), vm)
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
}
@@ -214,7 +214,7 @@ impl IteratorIterable for PySequenceIterator {}
impl SlotIterator for PySequenceIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal
.write()
.lock()
.next(|obj, pos| obj.get_item(pos, vm), vm)
}
}

View File

@@ -1,6 +1,6 @@
use super::{PositionIterInternal, PyGenericAlias, PySliceRef, PyTypeRef};
use crate::common::lock::{
PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard,
PyMappedRwLockReadGuard, PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use crate::{
function::{ArgIterable, FuncArgs, OptionalArg},
@@ -158,7 +158,7 @@ impl PyList {
fn reversed(zelf: PyRef<Self>) -> PyListReverseIterator {
let position = zelf.len().saturating_sub(1);
PyListReverseIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, position)),
internal: PyMutex::new(PositionIterInternal::new(zelf, position)),
}
}
@@ -407,7 +407,7 @@ impl PyList {
impl Iterable for PyList {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyListIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
}
@@ -462,7 +462,7 @@ fn do_sort(
#[pyclass(module = false, name = "list_iterator")]
#[derive(Debug)]
pub struct PyListIterator {
internal: PyRwLock<PositionIterInternal<PyListRef>>,
internal: PyMutex<PositionIterInternal<PyListRef>>,
}
impl PyValue for PyListIterator {
@@ -475,18 +475,18 @@ impl PyValue for PyListIterator {
impl PyListIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_iter_reduce(|x| x.clone().into_object(), vm)
}
}
@@ -494,7 +494,7 @@ impl PyListIterator {
impl IteratorIterable for PyListIterator {}
impl SlotIterator for PyListIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|list, pos| {
let vec = list.borrow_vec();
vec.get(pos)
@@ -509,7 +509,7 @@ impl SlotIterator for PyListIterator {
#[pyclass(module = false, name = "list_reverseiterator")]
#[derive(Debug)]
pub struct PyListReverseIterator {
internal: PyRwLock<PositionIterInternal<PyListRef>>,
internal: PyMutex<PositionIterInternal<PyListRef>>,
}
impl PyValue for PyListReverseIterator {
@@ -522,18 +522,18 @@ impl PyValue for PyListReverseIterator {
impl PyListReverseIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().rev_length_hint(|obj| obj.len())
self.internal.lock().rev_length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_reversed_reduce(|x| x.clone().into_object(), vm)
}
}
@@ -541,7 +541,7 @@ impl PyListReverseIterator {
impl IteratorIterable for PyListReverseIterator {}
impl SlotIterator for PyListReverseIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().rev_next(
zelf.internal.lock().rev_next(
|list, pos| {
let vec = list.borrow_vec();
vec.get(pos)

View File

@@ -169,7 +169,7 @@ impl TryIntoRef<PyStr> for &str {
#[pyclass(module = false, name = "str_iterator")]
#[derive(Debug)]
pub struct PyStrIterator {
internal: PyRwLock<PositionIterInternal<PyStrRef>>,
internal: PyMutex<PositionIterInternal<PyStrRef>>,
}
impl PyValue for PyStrIterator {
@@ -182,18 +182,18 @@ impl PyValue for PyStrIterator {
impl PyStrIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_iter_reduce(|x| x.clone().into_object(), vm)
}
}
@@ -201,7 +201,7 @@ impl PyStrIterator {
impl IteratorIterable for PyStrIterator {}
impl SlotIterator for PyStrIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let mut internal = zelf.internal.write();
let mut internal = zelf.internal.lock();
if let IterStatus::Active(s) = &internal.status {
let value = s.as_str();
if internal.position >= value.len() {
@@ -1253,7 +1253,7 @@ impl Comparable for PyStr {
impl Iterable for PyStr {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyStrIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
}

View File

@@ -16,7 +16,7 @@ use crate::{
IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol,
};
use rustpython_common::lock::PyRwLock;
use rustpython_common::lock::PyMutex;
use std::fmt;
pub type SetContentType = dictdatatype::Dict<()>;
@@ -196,11 +196,7 @@ impl PySetInner {
fn iter(&self) -> PySetIterator {
PySetIterator {
size: self.content.size(),
internal: PyRwLock::new(PositionIterInternal::new(self.content.clone(), 0))
// dict: PyRc::clone(&self.content),
// size: self.content.size(),
// position: AtomicCell::new(0),
// status: AtomicCell::new(IterStatus::Active),
internal: PyMutex::new(PositionIterInternal::new(self.content.clone(), 0)),
}
}
@@ -819,7 +815,7 @@ impl TryFromObject for SetIterable {
#[pyclass(module = false, name = "set_iterator")]
pub(crate) struct PySetIterator {
size: DictSize,
internal: PyRwLock<PositionIterInternal<PyRc<SetContentType>>>,
internal: PyMutex<PositionIterInternal<PyRc<SetContentType>>>,
}
impl fmt::Debug for PySetIterator {
@@ -839,12 +835,12 @@ impl PyValue for PySetIterator {
impl PySetIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|_| self.size.entries_size)
self.internal.lock().length_hint(|_| self.size.entries_size)
}
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<(PyObjectRef, (PyObjectRef,))> {
let internal = zelf.internal.read();
let internal = zelf.internal.lock();
Ok((
vm.get_attribute(vm.builtins.clone(), "iter")?,
(vm.ctx.new_list(match &internal.status {
@@ -860,7 +856,7 @@ impl PySetIterator {
impl IteratorIterable for PySetIterator {}
impl SlotIterator for PySetIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let mut internal = zelf.internal.write();
let mut internal = zelf.internal.lock();
if let IterStatus::Active(dict) = &internal.status {
if dict.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;

View File

@@ -15,7 +15,7 @@ use crate::{
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject,
TypeProtocol,
};
use rustpython_common::lock::PyRwLock;
use rustpython_common::lock::PyMutex;
use std::fmt;
use std::marker::PhantomData;
@@ -306,7 +306,7 @@ impl Comparable for PyTuple {
impl Iterable for PyTuple {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyTupleIterator {
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
}
@@ -315,7 +315,7 @@ impl Iterable for PyTuple {
#[pyclass(module = false, name = "tuple_iterator")]
#[derive(Debug)]
pub(crate) struct PyTupleIterator {
internal: PyRwLock<PositionIterInternal<PyTupleRef>>,
internal: PyMutex<PositionIterInternal<PyTupleRef>>,
}
impl PyValue for PyTupleIterator {
@@ -328,18 +328,18 @@ impl PyValue for PyTupleIterator {
impl PyTupleIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
self.internal.write().set_state(state, vm)
self.internal.lock().set_state(state, vm)
}
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
self.internal
.read()
.lock()
.builtin_iter_reduce(|x| x.clone().into_object(), vm)
}
}
@@ -347,7 +347,7 @@ impl PyTupleIterator {
impl IteratorIterable for PyTupleIterator {}
impl SlotIterator for PyTupleIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|tuple, pos| {
tuple
.as_slice()

View File

@@ -3,7 +3,7 @@ pub(crate) use _collections::make_module;
#[pymodule]
mod _collections {
use crate::builtins::PositionIterInternal;
use crate::common::lock::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::common::lock::{PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
use crate::{
builtins::{
IterStatus::{Active, Exhausted},
@@ -360,7 +360,7 @@ mod _collections {
fn reversed(zelf: PyRef<Self>) -> PyResult<PyReverseDequeIterator> {
Ok(PyReverseDequeIterator {
state: zelf.state.load(),
internal: PyRwLock::new(PositionIterInternal::new(zelf, 0)),
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
})
}
@@ -572,7 +572,7 @@ mod _collections {
#[derive(Debug, PyValue)]
struct PyDequeIterator {
state: usize,
internal: PyRwLock<PositionIterInternal<PyDequeRef>>,
internal: PyMutex<PositionIterInternal<PyDequeRef>>,
}
#[derive(FromArgs)]
@@ -595,7 +595,7 @@ mod _collections {
let iter = PyDequeIterator::new(deque);
if let OptionalArg::Present(index) = index {
let index = max(index, 0) as usize;
iter.internal.write().position = index;
iter.internal.lock().position = index;
}
iter.into_pyresult_with_type(vm, cls)
}
@@ -606,13 +606,13 @@ mod _collections {
pub(crate) fn new(deque: PyDequeRef) -> Self {
PyDequeIterator {
state: deque.state.load(),
internal: PyRwLock::new(PositionIterInternal::new(deque, 0)),
internal: PyMutex::new(PositionIterInternal::new(deque, 0)),
}
}
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
@@ -620,7 +620,7 @@ mod _collections {
zelf: PyRef<Self>,
vm: &VirtualMachine,
) -> (PyTypeRef, (PyDequeRef, PyObjectRef)) {
let internal = zelf.internal.read();
let internal = zelf.internal.lock();
let deque = match &internal.status {
Active(obj) => obj.clone(),
Exhausted => PyDeque::default().into_ref(vm),
@@ -635,7 +635,7 @@ mod _collections {
impl IteratorIterable for PyDequeIterator {}
impl SlotIterator for PyDequeIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|deque, pos| {
if zelf.state != deque.state.load() {
return Err(
@@ -659,7 +659,7 @@ mod _collections {
struct PyReverseDequeIterator {
state: usize,
// position is counting from the tail
internal: PyRwLock<PositionIterInternal<PyDequeRef>>,
internal: PyMutex<PositionIterInternal<PyDequeRef>>,
}
impl SlotConstructor for PyReverseDequeIterator {
@@ -674,7 +674,7 @@ mod _collections {
let iter = PyDeque::reversed(deque)?;
if let OptionalArg::Present(index) = index {
let index = max(index, 0) as usize;
iter.internal.write().position = index;
iter.internal.lock().position = index;
}
iter.into_pyresult_with_type(vm, cls)
}
@@ -684,7 +684,7 @@ mod _collections {
impl PyReverseDequeIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.internal.read().length_hint(|obj| obj.len())
self.internal.lock().length_hint(|obj| obj.len())
}
#[pymethod(magic)]
@@ -692,7 +692,7 @@ mod _collections {
zelf: PyRef<Self>,
vm: &VirtualMachine,
) -> PyResult<(PyTypeRef, (PyDequeRef, PyObjectRef))> {
let internal = zelf.internal.read();
let internal = zelf.internal.lock();
let deque = match &internal.status {
Active(obj) => obj.clone(),
Exhausted => PyDeque::default().into_ref(vm),
@@ -707,7 +707,7 @@ mod _collections {
impl IteratorIterable for PyReverseDequeIterator {}
impl SlotIterator for PyReverseDequeIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.internal.write().next(
zelf.internal.lock().next(
|deque, pos| {
if deque.state.load() != zelf.state {
return Err(