Skip to content
3 changes: 2 additions & 1 deletion vm/src/obj/objclassmethod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyobject::{
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe, TypeProtocol,
};
use crate::slots::SlotDescriptor;
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -32,6 +32,7 @@ pub struct PyClassMethod {
callable: PyObjectRef,
}
pub type PyClassMethodRef = PyRef<PyClassMethod>;
impl ThreadSafe for PyClassMethod {}

impl PyClassMethod {
pub fn new(value: PyObjectRef) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion vm/src/obj/objcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::ops::Deref;

use super::objtype::PyClassRef;
use crate::bytecode;
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
};
use crate::vm::VirtualMachine;

pub type PyCodeRef = PyRef<PyCode>;
Expand All @@ -17,6 +19,8 @@ pub struct PyCode {
pub code: bytecode::CodeObject,
}

impl ThreadSafe for PyCode {}

impl Deref for PyCode {
type Target = bytecode::CodeObject;
fn deref(&self) -> &Self::Target {
Expand Down
26 changes: 11 additions & 15 deletions vm/src/obj/objenumerate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::RefCell;
use std::ops::AddAssign;
use std::sync::RwLock;

use num_bigint::BigInt;
use num_traits::Zero;
Expand All @@ -8,16 +8,17 @@ use super::objint::PyIntRef;
use super::objiter;
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
use crate::vm::VirtualMachine;

#[pyclass]
#[derive(Debug)]
pub struct PyEnumerate {
counter: RefCell<BigInt>,
counter: RwLock<BigInt>,
iterator: PyObjectRef,
}
type PyEnumerateRef = PyRef<PyEnumerate>;
impl ThreadSafe for PyEnumerate {}

impl PyValue for PyEnumerate {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand All @@ -41,24 +42,19 @@ impl PyEnumerate {

let iterator = objiter::get_iter(vm, &iterable)?;
PyEnumerate {
counter: RefCell::new(counter),
counter: RwLock::new(counter),
iterator,
}
.into_ref_with_type(vm, cls)
}

#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let iterator = &self.iterator;
let counter = &self.counter;
let next_obj = objiter::call_next(vm, iterator)?;
let result = vm
.ctx
.new_tuple(vec![vm.ctx.new_bigint(&counter.borrow()), next_obj]);

AddAssign::add_assign(&mut counter.borrow_mut() as &mut BigInt, 1);

Ok(result)
fn next(&self, vm: &VirtualMachine) -> PyResult<(BigInt, PyObjectRef)> {
let next_obj = objiter::call_next(vm, &self.iterator)?;
let mut counter = self.counter.write().unwrap();
let position = counter.clone();
AddAssign::add_assign(&mut counter as &mut BigInt, 1);
Ok((position, next_obj))
}

#[pymethod(name = "__iter__")]
Expand Down
5 changes: 4 additions & 1 deletion vm/src/obj/objfilter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::objbool;
use super::objiter;
use super::objtype::PyClassRef;
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
};
use crate::vm::VirtualMachine;

pub type PyFilterRef = PyRef<PyFilter>;
Expand All @@ -16,6 +18,7 @@ pub struct PyFilter {
predicate: PyObjectRef,
iterator: PyObjectRef,
}
impl ThreadSafe for PyFilter {}

impl PyValue for PyFilter {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down
6 changes: 4 additions & 2 deletions vm/src/obj/objiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use super::objsequence;
use super::objtype::{self, PyClassRef};
use crate::exceptions::PyBaseExceptionRef;
use crate::pyobject::{
PyCallable, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
PyCallable, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand Down Expand Up @@ -141,6 +141,7 @@ pub struct PySequenceIterator {
pub obj: PyObjectRef,
pub reversed: bool,
}
impl ThreadSafe for PySequenceIterator {}

impl PyValue for PySequenceIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down Expand Up @@ -214,6 +215,7 @@ pub struct PyCallableIterator {
sentinel: PyObjectRef,
done: AtomicCell<bool>,
}
impl ThreadSafe for PyCallableIterator {}

impl PyValue for PyCallableIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/obj/objmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::objiter;
use super::objtype::PyClassRef;
use crate::function::Args;
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
use crate::vm::VirtualMachine;

/// map(func, *iterables) --> map object
Expand All @@ -15,6 +15,7 @@ pub struct PyMap {
iterators: Vec<PyObjectRef>,
}
type PyMapRef = PyRef<PyMap>;
impl ThreadSafe for PyMap {}

impl PyValue for PyMap {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/obj/objmemory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::objbyteinner::try_as_byte;
use super::objtype::{issubclass, PyClassRef};
use crate::pyobject::{
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe, TypeProtocol,
};
use crate::stdlib::array::PyArray;
use crate::vm::VirtualMachine;
Expand All @@ -11,6 +11,7 @@ use crate::vm::VirtualMachine;
pub struct PyMemoryView {
obj_ref: PyObjectRef,
}
impl ThreadSafe for PyMemoryView {}

pub type PyMemoryViewRef = PyRef<PyMemoryView>;

Expand Down
20 changes: 11 additions & 9 deletions vm/src/obj/objproperty.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*! Python `property` descriptor class.

*/
use std::cell::RefCell;
use std::sync::RwLock;

use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyobject::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
TypeProtocol,
};
use crate::slots::SlotDescriptor;
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -49,8 +50,9 @@ pub struct PyProperty {
getter: Option<PyObjectRef>,
setter: Option<PyObjectRef>,
deleter: Option<PyObjectRef>,
doc: RefCell<Option<PyObjectRef>>,
doc: RwLock<Option<PyObjectRef>>,
}
impl ThreadSafe for PyProperty {}

impl PyValue for PyProperty {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down Expand Up @@ -101,7 +103,7 @@ impl PyProperty {
getter: args.fget,
setter: args.fset,
deleter: args.fdel,
doc: RefCell::new(args.doc),
doc: RwLock::new(args.doc),
}
.into_ref_with_type(vm, cls)
}
Expand Down Expand Up @@ -144,11 +146,11 @@ impl PyProperty {
}

fn doc_getter(&self) -> Option<PyObjectRef> {
self.doc.borrow().clone()
self.doc.read().unwrap().clone()
}

fn doc_setter(&self, value: PyObjectRef, vm: &VirtualMachine) {
self.doc.replace(py_none_to_option(vm, &value));
*self.doc.write().unwrap() = py_none_to_option(vm, &value);
}

// Python builder functions
Expand All @@ -163,7 +165,7 @@ impl PyProperty {
getter: getter.or_else(|| zelf.getter.clone()),
setter: zelf.setter.clone(),
deleter: zelf.deleter.clone(),
doc: RefCell::new(None),
doc: RwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
Expand All @@ -178,7 +180,7 @@ impl PyProperty {
getter: zelf.getter.clone(),
setter: setter.or_else(|| zelf.setter.clone()),
deleter: zelf.deleter.clone(),
doc: RefCell::new(None),
doc: RwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
Expand All @@ -193,7 +195,7 @@ impl PyProperty {
getter: zelf.getter.clone(),
setter: zelf.setter.clone(),
deleter: deleter.or_else(|| zelf.deleter.clone()),
doc: RefCell::new(None),
doc: RwLock::new(None),
}
.into_ref_with_type(vm, TypeProtocol::class(&zelf))
}
Expand Down
17 changes: 9 additions & 8 deletions vm/src/obj/objrange.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::cell::Cell;

use crossbeam_utils::atomic::AtomicCell;
use num_bigint::{BigInt, Sign};
use num_integer::Integer;
use num_traits::{One, Signed, Zero};
Expand All @@ -13,7 +12,8 @@ use super::objtype::PyClassRef;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::pyhash;
use crate::pyobject::{
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe, TryFromObject,
TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand All @@ -32,6 +32,7 @@ pub struct PyRange {
pub stop: PyIntRef,
pub step: PyIntRef,
}
impl ThreadSafe for PyRange {}

impl PyValue for PyRange {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down Expand Up @@ -171,7 +172,7 @@ impl PyRange {
#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>) -> PyRangeIterator {
PyRangeIterator {
position: Cell::new(0),
position: AtomicCell::new(0),
range: zelf,
}
}
Expand Down Expand Up @@ -204,7 +205,7 @@ impl PyRange {
};

PyRangeIterator {
position: Cell::new(0),
position: AtomicCell::new(0),
range: reversed.into_ref(vm),
}
}
Expand Down Expand Up @@ -401,9 +402,10 @@ impl PyRange {
#[pyclass]
#[derive(Debug)]
pub struct PyRangeIterator {
position: Cell<usize>,
position: AtomicCell<usize>,
range: PyRangeRef,
}
impl ThreadSafe for PyRangeIterator {}

impl PyValue for PyRangeIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand All @@ -417,9 +419,8 @@ type PyRangeIteratorRef = PyRef<PyRangeIterator>;
impl PyRangeIterator {
#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult<BigInt> {
let position = BigInt::from(self.position.get());
let position = BigInt::from(self.position.fetch_add(1));
if let Some(int) = self.range.get(&position) {
self.position.set(self.position.get() + 1);
Ok(int)
} else {
Err(objiter::new_stop_iteration(vm))
Expand Down
4 changes: 3 additions & 1 deletion vm/src/obj/objslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::objint::PyInt;
use super::objtype::PyClassRef;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::pyobject::{
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef,
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
TryIntoRef,
};
use crate::vm::VirtualMachine;
use num_bigint::{BigInt, ToBigInt};
Expand All @@ -15,6 +16,7 @@ pub struct PySlice {
pub stop: PyObjectRef,
pub step: Option<PyObjectRef>,
}
impl ThreadSafe for PySlice {}

impl PyValue for PySlice {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down
3 changes: 2 additions & 1 deletion vm/src/obj/objstaticmethod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::objtype::PyClassRef;
use crate::function::OptionalArg;
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
use crate::slots::SlotDescriptor;
use crate::vm::VirtualMachine;

Expand All @@ -10,6 +10,7 @@ pub struct PyStaticMethod {
pub callable: PyObjectRef,
}
pub type PyStaticMethodRef = PyRef<PyStaticMethod>;
impl ThreadSafe for PyStaticMethod {}

impl PyValue for PyStaticMethod {
fn class(vm: &VirtualMachine) -> PyClassRef {
Expand Down
Loading