Skip to content

Commit f970f4f

Browse files
committed
Add global import lock
1 parent aebac26 commit f970f4f

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

vm/src/stdlib/imp.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
use crate::import;
2+
use crate::obj::objbytes::PyBytesRef;
23
use crate::obj::objcode::PyCode;
34
use crate::obj::objmodule::PyModuleRef;
45
use crate::obj::objstr;
56
use crate::obj::objstr::PyStringRef;
67
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult};
8+
use crate::stdlib::thread::RawRMutex;
79
use crate::vm::VirtualMachine;
810

11+
static IMP_LOCK: RawRMutex = RawRMutex::INIT;
12+
913
fn imp_extension_suffixes(vm: &VirtualMachine) -> PyResult {
1014
Ok(vm.ctx.new_list(vec![]))
1115
}
1216

13-
fn imp_acquire_lock(_vm: &VirtualMachine) -> PyResult<()> {
14-
// TODO
15-
Ok(())
17+
fn imp_acquire_lock(_vm: &VirtualMachine) {
18+
IMP_LOCK.lock()
1619
}
1720

18-
fn imp_release_lock(_vm: &VirtualMachine) -> PyResult<()> {
19-
// TODO
20-
Ok(())
21+
fn imp_release_lock(vm: &VirtualMachine) -> PyResult<()> {
22+
if !IMP_LOCK.is_locked() {
23+
Err(vm.new_runtime_error("Global import lock not held".to_owned()))
24+
} else {
25+
IMP_LOCK.unlock();
26+
Ok(())
27+
}
2128
}
2229

23-
fn imp_lock_held(_vm: &VirtualMachine) -> PyResult<()> {
24-
// TODO
25-
Ok(())
30+
fn imp_lock_held(_vm: &VirtualMachine) -> bool {
31+
IMP_LOCK.is_locked()
2632
}
2733

2834
fn imp_is_builtin(name: PyStringRef, vm: &VirtualMachine) -> bool {
@@ -90,6 +96,11 @@ fn imp_fix_co_filename(_code: PyObjectRef, _path: PyStringRef) {
9096
// TODO:
9197
}
9298

99+
fn imp_source_hash(_key: u64, _source: PyBytesRef, vm: &VirtualMachine) -> PyResult {
100+
// TODO:
101+
Ok(vm.get_none())
102+
}
103+
93104
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
94105
let ctx = &vm.ctx;
95106
py_module!(vm, "_imp", {
@@ -105,5 +116,6 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
105116
"init_frozen" => ctx.new_function(imp_init_frozen),
106117
"is_frozen_package" => ctx.new_function(imp_is_frozen_package),
107118
"_fix_co_filename" => ctx.new_function(imp_fix_co_filename),
119+
"source_hash" => ctx.new_function(imp_source_hash)
108120
})
109121
}

vm/src/stdlib/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl PyLock {
145145
}
146146
}
147147

148-
type RawRMutex = RawReentrantMutex<RawMutex, RawThreadId>;
148+
pub type RawRMutex = RawReentrantMutex<RawMutex, RawThreadId>;
149149
#[pyclass(name = "RLock")]
150150
struct PyRLock {
151151
mu: RawRMutex,

0 commit comments

Comments
 (0)