Skip to content

Commit 3647568

Browse files
committed
Make threading optional
1 parent 39303f0 commit 3647568

File tree

10 files changed

+105
-18
lines changed

10 files changed

+105
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ name = "bench"
1515
path = "./benchmarks/bench.rs"
1616

1717
[features]
18+
default = ["threading"]
1819
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
1920
freeze-stdlib = ["rustpython-vm/freeze-stdlib"]
21+
threading = ["rustpython-vm/threading"]
2022

2123
ssl = ["rustpython-vm/ssl"]
2224

2325
[dependencies]
2426
log = "0.4"
2527
env_logger = "0.7"
2628
clap = "2.33"
27-
rustpython-compiler = {path = "compiler", version = "0.1.1"}
28-
rustpython-parser = {path = "parser", version = "0.1.1"}
29-
rustpython-vm = {path = "vm", version = "0.1.1"}
29+
rustpython-compiler = { path = "compiler", version = "0.1.1" }
30+
rustpython-parser = { path = "parser", version = "0.1.1" }
31+
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["rustpython-parser", "rustpython-compiler"] }
3032
dirs = { package = "dirs-next", version = "1.0" }
3133
num-traits = "0.2.8"
3234
cfg-if = "0.1"

Lib/importlib/_bootstrap_external.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,10 @@ def _setup(_bootstrap_module):
15871587
setattr(self_module, '_pathseps_with_colon', {f':{s}' for s in path_separators})
15881588

15891589
# Directly load the _thread module (needed during bootstrap).
1590-
thread_module = _bootstrap._builtin_from_name('_thread')
1590+
try:
1591+
thread_module = _bootstrap._builtin_from_name('_thread')
1592+
except ImportError:
1593+
thread_module = None
15911594
setattr(self_module, '_thread', thread_module)
15921595

15931596
# Directly load the _weakref module (needed during bootstrap).

common/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ version = "0.0.0"
44
authors = ["RustPython Team"]
55
edition = "2018"
66

7+
[features]
8+
threading = ["parking_lot"]
9+
710
[dependencies]
8-
parking_lot = "0.11.0"
11+
parking_lot = { version = "0.11.0", optional = true }
912
num-traits = "0.2"
1013
num-bigint = "0.3"
1114
lexical = "4"
1215
hexf-parse = "0.1.0"
16+
cfg-if = "0.1"
1317

1418
[dev-dependencies]
1519
rand = "0.7.3"

common/src/cell.rs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
1+
#[cfg(feature = "threading")]
12
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
3+
#[cfg(not(feature = "threading"))]
4+
use std::cell::{Ref, RefCell, RefMut};
25
use std::ops::{Deref, DerefMut};
36

7+
cfg_if::cfg_if! {
8+
if #[cfg(feature = "threading")] {
9+
type MutexInner<T> = Mutex<T>;
10+
type MutexGuardInner<'a, T> = MutexGuard<'a, T>;
11+
const fn new_mutex<T>(value: T) -> MutexInner<T> {
12+
parking_lot::const_mutex(value)
13+
}
14+
fn lock_mutex<T: ?Sized>(m: &MutexInner<T>) -> MutexGuardInner<T> {
15+
m.lock()
16+
}
17+
} else {
18+
type MutexInner<T> = RefCell<T>;
19+
type MutexGuardInner<'a, T> = RefMut<'a, T>;
20+
const fn new_mutex<T>(value: T) -> MutexInner<T> {
21+
RefCell::new(value)
22+
}
23+
fn lock_mutex<T: ?Sized>(m: &MutexInner<T>) -> MutexGuardInner<T> {
24+
m.borrow_mut()
25+
}
26+
}
27+
}
28+
429
#[derive(Debug, Default)]
530
#[repr(transparent)]
6-
pub struct PyMutex<T: ?Sized>(Mutex<T>);
31+
pub struct PyMutex<T: ?Sized>(MutexInner<T>);
732

833
impl<T> PyMutex<T> {
934
pub const fn new(value: T) -> Self {
10-
Self(parking_lot::const_mutex(value))
35+
Self(new_mutex(value))
1136
}
1237
}
1338

1439
impl<T: ?Sized> PyMutex<T> {
1540
pub fn lock(&self) -> PyMutexGuard<T> {
16-
PyMutexGuard(self.0.lock())
41+
PyMutexGuard(lock_mutex(&self.0))
1742
}
1843
}
1944

2045
#[derive(Debug)]
2146
#[repr(transparent)]
22-
pub struct PyMutexGuard<'a, T: ?Sized>(MutexGuard<'a, T>);
47+
pub struct PyMutexGuard<'a, T: ?Sized>(MutexGuardInner<'a, T>);
2348
impl<T: ?Sized> Deref for PyMutexGuard<'_, T> {
2449
type Target = T;
2550
fn deref(&self) -> &T {
@@ -32,28 +57,58 @@ impl<T: ?Sized> DerefMut for PyMutexGuard<'_, T> {
3257
}
3358
}
3459

60+
cfg_if::cfg_if! {
61+
if #[cfg(feature = "threading")] {
62+
type RwLockInner<T> = RwLock<T>;
63+
type RwLockReadInner<'a, T> = RwLockReadGuard<'a, T>;
64+
type RwLockWriteInner<'a, T> = RwLockWriteGuard<'a, T>;
65+
const fn new_rwlock<T>(value: T) -> RwLockInner<T> {
66+
parking_lot::const_rwlock(value)
67+
}
68+
fn read_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockReadInner<T> {
69+
m.read()
70+
}
71+
fn write_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockWriteInner<T> {
72+
m.write()
73+
}
74+
} else {
75+
type RwLockInner<T> = RefCell<T>;
76+
type RwLockReadInner<'a, T> = Ref<'a, T>;
77+
type RwLockWriteInner<'a, T> = RefMut<'a, T>;
78+
const fn new_rwlock<T>(value: T) -> RwLockInner<T> {
79+
RefCell::new(value)
80+
}
81+
fn read_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockReadInner<T> {
82+
m.borrow()
83+
}
84+
fn write_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockWriteInner<T> {
85+
m.borrow_mut()
86+
}
87+
}
88+
}
89+
3590
#[derive(Debug, Default)]
3691
#[repr(transparent)]
37-
pub struct PyRwLock<T: ?Sized>(RwLock<T>);
92+
pub struct PyRwLock<T: ?Sized>(RwLockInner<T>);
3893

3994
impl<T> PyRwLock<T> {
4095
pub const fn new(value: T) -> Self {
41-
Self(parking_lot::const_rwlock(value))
96+
Self(new_rwlock(value))
4297
}
4398
}
4499

45100
impl<T: ?Sized> PyRwLock<T> {
46101
pub fn read(&self) -> PyRwLockReadGuard<T> {
47-
PyRwLockReadGuard(self.0.read())
102+
PyRwLockReadGuard(read_rwlock(&self.0))
48103
}
49104
pub fn write(&self) -> PyRwLockWriteGuard<T> {
50-
PyRwLockWriteGuard(self.0.write())
105+
PyRwLockWriteGuard(write_rwlock(&self.0))
51106
}
52107
}
53108

54109
#[derive(Debug)]
55110
#[repr(transparent)]
56-
pub struct PyRwLockReadGuard<'a, T: ?Sized>(RwLockReadGuard<'a, T>);
111+
pub struct PyRwLockReadGuard<'a, T: ?Sized>(RwLockReadInner<'a, T>);
57112
impl<T: ?Sized> Deref for PyRwLockReadGuard<'_, T> {
58113
type Target = T;
59114
fn deref(&self) -> &T {
@@ -63,7 +118,7 @@ impl<T: ?Sized> Deref for PyRwLockReadGuard<'_, T> {
63118

64119
#[derive(Debug)]
65120
#[repr(transparent)]
66-
pub struct PyRwLockWriteGuard<'a, T: ?Sized>(RwLockWriteGuard<'a, T>);
121+
pub struct PyRwLockWriteGuard<'a, T: ?Sized>(RwLockWriteInner<'a, T>);
67122
impl<T: ?Sized> Deref for PyRwLockWriteGuard<'_, T> {
68123
type Target = T;
69124
fn deref(&self) -> &T {

common/src/rc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
#[cfg(not(feature = "threading"))]
2+
use std::rc::{Rc, Weak};
3+
#[cfg(feature = "threading")]
14
use std::sync::{Arc, Weak};
25

36
// type aliases instead of newtypes because you can't do `fn method(self: PyRc<Self>)` with a
47
// newtype; requires the arbitrary_self_types unstable feature
8+
9+
#[cfg(feature = "threading")]
510
pub type PyRc<T> = Arc<T>;
11+
#[cfg(not(feature = "threading"))]
12+
pub type PyRc<T> = Rc<T>;
13+
614
pub type PyWeak<T> = Weak<T>;

vm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ edition = "2018"
99
include = ["src/**/*.rs", "Cargo.toml", "build.rs", "Lib/**/*.py"]
1010

1111
[features]
12-
default = ["rustpython-parser", "rustpython-compiler"]
12+
default = ["rustpython-parser", "rustpython-compiler", "threading"]
1313
vm-tracing-logging = []
1414
flame-it = ["flame", "flamer"]
1515
freeze-stdlib = ["rustpython-pylib"]
16+
threading = ["rustpython-common/threading"]
1617

1718
ssl = ["openssl", "openssl-sys", "openssl-probe"]
1819

vm/src/pyobject.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,17 @@ impl PyObject<dyn PyObjectPayload> {
12761276
}
12771277
}
12781278

1279-
pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
1279+
cfg_if::cfg_if! {
1280+
if #[cfg(feature = "threading")] {
1281+
pub trait PyThreadingConstraint: Send + Sync {}
1282+
impl<T: Send + Sync> PyThreadingConstraint for T {}
1283+
} else {
1284+
pub trait PyThreadingConstraint {}
1285+
impl<T> PyThreadingConstraint for T {}
1286+
}
1287+
}
1288+
1289+
pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static {
12801290
const HAVE_DICT: bool = false;
12811291

12821292
fn class(vm: &VirtualMachine) -> PyClassRef;
@@ -1306,7 +1316,7 @@ pub trait PyValue: fmt::Debug + Send + Sync + Sized + 'static {
13061316
}
13071317
}
13081318

1309-
pub trait PyObjectPayload: Any + fmt::Debug + Send + Sync + 'static {
1319+
pub trait PyObjectPayload: Any + fmt::Debug + PyThreadingConstraint + 'static {
13101320
fn as_any(&self) -> &dyn Any;
13111321
}
13121322

vm/src/stdlib/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod string;
3232
mod symtable;
3333
mod sysconfigdata;
3434
#[cfg(not(target_arch = "wasm32"))]
35+
#[cfg(feature = "threading")]
3536
mod thread;
3637
mod time_module;
3738
#[cfg(feature = "rustpython-parser")]
@@ -131,6 +132,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
131132
modules.insert("select".to_owned(), Box::new(select::make_module));
132133
#[cfg(feature = "ssl")]
133134
modules.insert("_ssl".to_owned(), Box::new(ssl::make_module));
135+
#[cfg(feature = "threading")]
134136
modules.insert("_thread".to_owned(), Box::new(thread::make_module));
135137
#[cfg(not(target_os = "redox"))]
136138
modules.insert("zlib".to_owned(), Box::new(zlib::make_module));

vm/src/vm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ impl VirtualMachine {
287287
}
288288
}
289289

290+
#[cfg(feature = "threading")]
290291
pub(crate) fn new_thread(&self) -> VirtualMachine {
291292
VirtualMachine {
292293
builtins: self.builtins.clone(),

0 commit comments

Comments
 (0)