Skip to content

Commit 93894b0

Browse files
authored
Merge pull request RustPython#1930 from palaviv/threading-support
Make PyValue Send + Sync
2 parents 4647731 + 621c309 commit 93894b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+126
-281
lines changed

vm/src/dictdatatype.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::obj::objstr::PyString;
22
use crate::pyhash;
3-
use crate::pyobject::{IdProtocol, IntoPyObject, PyObjectRef, PyResult, ThreadSafe};
3+
use crate::pyobject::{IdProtocol, IntoPyObject, PyObjectRef, PyResult};
44
use crate::vm::VirtualMachine;
55
use num_bigint::ToBigInt;
66
/// Ordered dictionary implementation.
@@ -23,8 +23,6 @@ pub struct Dict<T = PyObjectRef> {
2323
inner: RwLock<InnerDict<T>>,
2424
}
2525

26-
impl ThreadSafe for Dict {}
27-
2826
struct InnerDict<T> {
2927
size: usize,
3028
indices: HashMap<HashIndex, EntryIndex>,

vm/src/exceptions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::obj::objtuple::{PyTuple, PyTupleRef};
66
use crate::obj::objtype::{self, PyClass, PyClassRef};
77
use crate::py_serde;
88
use crate::pyobject::{
9-
PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
10-
TryFromObject, TypeProtocol,
9+
PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
10+
TypeProtocol,
1111
};
1212
use crate::slots::PyTpFlags;
1313
use crate::types::create_type;
@@ -30,8 +30,6 @@ pub struct PyBaseException {
3030
args: RwLock<PyTupleRef>,
3131
}
3232

33-
impl ThreadSafe for PyBaseException {}
34-
3533
impl fmt::Debug for PyBaseException {
3634
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3735
// TODO: implement more detailed, non-recursive Debug formatter

vm/src/obj/objasyncgenerator.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::objtype::{self, PyClassRef};
44
use crate::exceptions::PyBaseExceptionRef;
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;
7-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
7+
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
88
use crate::vm::VirtualMachine;
99

1010
use crossbeam_utils::atomic::AtomicCell;
@@ -16,7 +16,6 @@ pub struct PyAsyncGen {
1616
running_async: AtomicCell<bool>,
1717
}
1818
pub type PyAsyncGenRef = PyRef<PyAsyncGen>;
19-
impl ThreadSafe for PyAsyncGen {}
2019

2120
impl PyValue for PyAsyncGen {
2221
fn class(vm: &VirtualMachine) -> PyClassRef {
@@ -164,8 +163,6 @@ struct PyAsyncGenASend {
164163
value: PyObjectRef,
165164
}
166165

167-
impl ThreadSafe for PyAsyncGenASend {}
168-
169166
impl PyValue for PyAsyncGenASend {
170167
fn class(vm: &VirtualMachine) -> PyClassRef {
171168
vm.ctx.types.async_generator_asend.clone()
@@ -262,8 +259,6 @@ struct PyAsyncGenAThrow {
262259
value: (PyObjectRef, PyObjectRef, PyObjectRef),
263260
}
264261

265-
impl ThreadSafe for PyAsyncGenAThrow {}
266-
267262
impl PyValue for PyAsyncGenAThrow {
268263
fn class(vm: &VirtualMachine) -> PyClassRef {
269264
vm.ctx.types.async_generator_athrow.clone()

vm/src/obj/objbuiltinfunc.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use crate::function::{OptionalArg, PyFuncArgs, PyNativeFunc};
44
use crate::obj::objtype::PyClassRef;
55
use crate::pyobject::{
6-
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, ThreadSafe, TypeProtocol,
6+
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol,
77
};
88
use crate::slots::{SlotCall, SlotDescriptor};
99
use crate::vm::VirtualMachine;
@@ -12,7 +12,6 @@ use crate::vm::VirtualMachine;
1212
pub struct PyBuiltinFunction {
1313
value: PyNativeFunc,
1414
}
15-
impl ThreadSafe for PyBuiltinFunction {}
1615

1716
impl PyValue for PyBuiltinFunction {
1817
fn class(vm: &VirtualMachine) -> PyClassRef {
@@ -49,7 +48,6 @@ impl PyBuiltinFunction {}
4948
pub struct PyBuiltinMethod {
5049
function: PyBuiltinFunction,
5150
}
52-
impl ThreadSafe for PyBuiltinMethod {}
5351

5452
impl PyValue for PyBuiltinMethod {
5553
fn class(vm: &VirtualMachine) -> PyClassRef {

vm/src/obj/objbytearray.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::function::{OptionalArg, OptionalOption};
2121
use crate::obj::objstr::do_cformat_string;
2222
use crate::pyobject::{
2323
Either, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult,
24-
PyValue, ThreadSafe, TryFromObject, TypeProtocol,
24+
PyValue, TryFromObject, TypeProtocol,
2525
};
2626
use crate::vm::VirtualMachine;
2727

@@ -42,8 +42,6 @@ pub struct PyByteArray {
4242
inner: RwLock<PyByteInner>,
4343
}
4444

45-
impl ThreadSafe for PyByteArray {}
46-
4745
pub type PyByteArrayRef = PyRef<PyByteArray>;
4846

4947
impl PyByteArray {

vm/src/obj/objbyteinner.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use super::pystr::{self, PyCommonString, PyCommonStringWrapper};
1717
use crate::function::{OptionalArg, OptionalOption};
1818
use crate::pyhash;
1919
use crate::pyobject::{
20-
Either, PyComparisonValue, PyIterable, PyObjectRef, PyResult, ThreadSafe, TryFromObject,
21-
TypeProtocol,
20+
Either, PyComparisonValue, PyIterable, PyObjectRef, PyResult, TryFromObject, TypeProtocol,
2221
};
2322
use crate::vm::VirtualMachine;
2423

@@ -33,8 +32,6 @@ impl From<Vec<u8>> for PyByteInner {
3332
}
3433
}
3534

36-
impl ThreadSafe for PyByteInner {}
37-
3835
impl TryFromObject for PyByteInner {
3936
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
4037
match_class!(match obj {

vm/src/obj/objbytes.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::pyobject::{
2222
Either, IntoPyObject,
2323
PyArithmaticValue::{self, *},
2424
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
25-
ThreadSafe, TryFromObject, TypeProtocol,
25+
TryFromObject, TypeProtocol,
2626
};
2727
use crate::vm::VirtualMachine;
2828

@@ -41,8 +41,6 @@ pub struct PyBytes {
4141
inner: PyByteInner,
4242
}
4343

44-
impl ThreadSafe for PyBytes {}
45-
4644
pub type PyBytesRef = PyRef<PyBytes>;
4745

4846
impl PyBytes {

vm/src/obj/objclassmethod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::objtype::PyClassRef;
22
use crate::function::OptionalArg;
33
use crate::pyobject::{
4-
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe, TypeProtocol,
4+
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
55
};
66
use crate::slots::SlotDescriptor;
77
use crate::vm::VirtualMachine;
@@ -32,7 +32,6 @@ pub struct PyClassMethod {
3232
callable: PyObjectRef,
3333
}
3434
pub type PyClassMethodRef = PyRef<PyClassMethod>;
35-
impl ThreadSafe for PyClassMethod {}
3635

3736
impl PyClassMethod {
3837
pub fn new(value: PyObjectRef) -> Self {

vm/src/obj/objcode.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use std::ops::Deref;
77

88
use super::objtype::PyClassRef;
99
use crate::bytecode;
10-
use crate::pyobject::{
11-
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
12-
};
10+
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1311
use crate::vm::VirtualMachine;
1412

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

22-
impl ThreadSafe for PyCode {}
23-
2420
impl Deref for PyCode {
2521
type Target = bytecode::CodeObject;
2622
fn deref(&self) -> &Self::Target {

vm/src/obj/objcomplex.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::objtype::PyClassRef;
77
use crate::function::OptionalArg;
88
use crate::pyhash;
99
use crate::pyobject::{
10-
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
10+
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
1111
};
1212
use crate::vm::VirtualMachine;
1313

@@ -20,8 +20,6 @@ pub struct PyComplex {
2020
value: Complex64,
2121
}
2222

23-
impl ThreadSafe for PyComplex {}
24-
2523
type PyComplexRef = PyRef<PyComplex>;
2624

2725
impl PyValue for PyComplex {

vm/src/obj/objcoroinner.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::objtype::{self, PyClassRef};
22
use crate::exceptions::{self, PyBaseExceptionRef};
33
use crate::frame::{ExecutionResult, FrameRef};
4-
use crate::pyobject::{PyObjectRef, PyResult, ThreadSafe};
4+
use crate::pyobject::{PyObjectRef, PyResult};
55
use crate::vm::VirtualMachine;
66

77
use crossbeam_utils::atomic::AtomicCell;
@@ -42,8 +42,6 @@ pub struct Coro {
4242
variant: Variant,
4343
}
4444

45-
impl ThreadSafe for Coro {}
46-
4745
impl Coro {
4846
pub fn new(frame: FrameRef, variant: Variant) -> Self {
4947
Coro {

vm/src/obj/objcoroutine.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::objstr::PyStringRef;
44
use super::objtype::PyClassRef;
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;
7-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
7+
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
88
use crate::vm::VirtualMachine;
99

1010
pub type PyCoroutineRef = PyRef<PyCoroutine>;
@@ -15,8 +15,6 @@ pub struct PyCoroutine {
1515
inner: Coro,
1616
}
1717

18-
impl ThreadSafe for PyCoroutine {}
19-
2018
impl PyValue for PyCoroutine {
2119
fn class(vm: &VirtualMachine) -> PyClassRef {
2220
vm.ctx.types.coroutine_type.clone()
@@ -103,8 +101,6 @@ pub struct PyCoroutineWrapper {
103101
coro: PyCoroutineRef,
104102
}
105103

106-
impl ThreadSafe for PyCoroutineWrapper {}
107-
108104
impl PyValue for PyCoroutineWrapper {
109105
fn class(vm: &VirtualMachine) -> PyClassRef {
110106
vm.ctx.types.coroutine_wrapper_type.clone()

vm/src/obj/objdict.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::cell::Cell;
21
use std::fmt;
32

3+
use crossbeam_utils::atomic::AtomicCell;
4+
45
use super::objiter;
56
use super::objstr;
67
use super::objtype::{self, PyClassRef};
@@ -9,7 +10,7 @@ use crate::exceptions::PyBaseExceptionRef;
910
use crate::function::{KwArgs, OptionalArg, PyFuncArgs};
1011
use crate::pyobject::{
1112
IdProtocol, IntoPyObject, ItemProtocol, PyAttributes, PyClassImpl, PyContext, PyIterable,
12-
PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
13+
PyObjectRef, PyRef, PyResult, PyValue,
1314
};
1415
use crate::vm::{ReprGuard, VirtualMachine};
1516

@@ -23,7 +24,6 @@ pub struct PyDict {
2324
entries: DictContentType,
2425
}
2526
pub type PyDictRef = PyRef<PyDict>;
26-
impl ThreadSafe for PyDict {}
2727

2828
impl fmt::Debug for PyDict {
2929
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -585,14 +585,14 @@ macro_rules! dict_iterator {
585585
struct $iter_name {
586586
pub dict: PyDictRef,
587587
pub size: dictdatatype::DictSize,
588-
pub position: Cell<usize>,
588+
pub position: AtomicCell<usize>,
589589
}
590590

591591
#[pyimpl]
592592
impl $iter_name {
593593
fn new(dict: PyDictRef) -> Self {
594594
$iter_name {
595-
position: Cell::new(0),
595+
position: AtomicCell::new(0),
596596
size: dict.size(),
597597
dict,
598598
}
@@ -601,15 +601,15 @@ macro_rules! dict_iterator {
601601
#[pymethod(name = "__next__")]
602602
#[allow(clippy::redundant_closure_call)]
603603
fn next(&self, vm: &VirtualMachine) -> PyResult {
604-
let mut position = self.position.get();
605604
if self.dict.entries.has_changed_size(&self.size) {
606605
return Err(
607606
vm.new_runtime_error("dictionary changed size during iteration".to_owned())
608607
);
609608
}
609+
let mut position = self.position.load();
610610
match self.dict.entries.next_entry(&mut position) {
611611
Some((key, value)) => {
612-
self.position.set(position);
612+
self.position.store(position);
613613
Ok($result_fn(vm, key, value))
614614
}
615615
None => Err(objiter::new_stop_iteration(vm)),
@@ -623,7 +623,7 @@ macro_rules! dict_iterator {
623623

624624
#[pymethod(name = "__length_hint__")]
625625
fn length_hint(&self) -> usize {
626-
self.dict.entries.len_from_entry_index(self.position.get())
626+
self.dict.entries.len_from_entry_index(self.position.load())
627627
}
628628
}
629629

vm/src/obj/objenumerate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::objint::PyIntRef;
88
use super::objiter;
99
use super::objtype::PyClassRef;
1010
use crate::function::OptionalArg;
11-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe};
11+
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1212
use crate::vm::VirtualMachine;
1313

1414
#[pyclass]
@@ -18,7 +18,6 @@ pub struct PyEnumerate {
1818
iterator: PyObjectRef,
1919
}
2020
type PyEnumerateRef = PyRef<PyEnumerate>;
21-
impl ThreadSafe for PyEnumerate {}
2221

2322
impl PyValue for PyEnumerate {
2423
fn class(vm: &VirtualMachine) -> PyClassRef {

vm/src/obj/objfilter.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use super::objbool;
22
use super::objiter;
33
use super::objtype::PyClassRef;
4-
use crate::pyobject::{
5-
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, ThreadSafe,
6-
};
4+
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
75
use crate::vm::VirtualMachine;
86

97
pub type PyFilterRef = PyRef<PyFilter>;
@@ -18,7 +16,6 @@ pub struct PyFilter {
1816
predicate: PyObjectRef,
1917
iterator: PyObjectRef,
2018
}
21-
impl ThreadSafe for PyFilter {}
2219

2320
impl PyValue for PyFilter {
2421
fn class(vm: &VirtualMachine) -> PyClassRef {

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::function::{OptionalArg, OptionalOption};
1212
use crate::pyhash;
1313
use crate::pyobject::{
1414
IntoPyObject, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
15-
PyRef, PyResult, PyValue, ThreadSafe, TryFromObject, TypeProtocol,
15+
PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1616
};
1717
use crate::vm::VirtualMachine;
1818

@@ -23,8 +23,6 @@ pub struct PyFloat {
2323
value: f64,
2424
}
2525

26-
impl ThreadSafe for PyFloat {}
27-
2826
impl PyFloat {
2927
pub fn to_f64(self) -> f64 {
3028
self.value

0 commit comments

Comments
 (0)