Skip to content

Commit 94dccb3

Browse files
committed
Fix mutex with new git version
1 parent e1bafce commit 94dccb3

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

vm/src/stdlib/thread.rs

+31-37
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,39 @@ struct AcquireArgs {
3232
timeout: Either<f64, isize>,
3333
}
3434

35+
macro_rules! acquire_lock_impl {
36+
($mu:expr, $args:expr, $vm:expr) => {{
37+
let (mu, args, vm) = ($mu, $args, $vm);
38+
let timeout = match args.timeout {
39+
Either::A(f) => f,
40+
Either::B(i) => i as f64,
41+
};
42+
match args.waitflag {
43+
true if timeout == -1.0 => {
44+
mu.lock();
45+
Ok(true)
46+
}
47+
true if timeout < 0.0 => {
48+
Err(vm.new_value_error("timeout value must be positive".to_owned()))
49+
}
50+
true => {
51+
// TODO: respect TIMEOUT_MAX here
52+
Ok(mu.try_lock_for(Duration::from_secs_f64(timeout)))
53+
}
54+
false if timeout != -1.0 => {
55+
Err(vm
56+
.new_value_error("can't specify a timeout for a non-blocking call".to_owned()))
57+
}
58+
false => Ok(mu.try_lock()),
59+
}
60+
}};
61+
}
62+
3563
#[pyclass(name = "lock")]
3664
struct PyLock {
3765
mu: RawMutex,
3866
}
67+
type PyLockRef = PyRef<PyLock>;
3968

4069
impl PyValue for PyLock {
4170
fn class(vm: &VirtualMachine) -> PyClassRef {
@@ -56,28 +85,7 @@ impl PyLock {
5685
#[pymethod(name = "__enter__")]
5786
#[allow(clippy::float_cmp, clippy::match_bool)]
5887
fn acquire(&self, args: AcquireArgs, vm: &VirtualMachine) -> PyResult<bool> {
59-
let timeout = match args.timeout {
60-
Either::A(f) => f,
61-
Either::B(i) => i as f64,
62-
};
63-
match args.waitflag {
64-
true if args.timeout == -1.0 => {
65-
self.mu.lock();
66-
Ok(true)
67-
}
68-
true if timeout < 0.0 => {
69-
Err(vm.new_value_error("timeout value must be positive".to_owned()))
70-
}
71-
true => {
72-
// TODO: respect TIMEOUT_MAX here
73-
Ok(mu.try_lock_for(Duration::from_secs_f64(timeout)))
74-
}
75-
false if timeout != -1.0 => {
76-
Err(vm
77-
.new_value_error("can't specify a timeout for a non-blocking call".to_owned()))
78-
}
79-
false => Ok(self.mu.try_lock()),
80-
}
88+
acquire_lock_impl!(&self.mu, args, vm)
8189
}
8290
#[pymethod]
8391
#[pymethod(name = "release_lock")]
@@ -129,21 +137,7 @@ impl PyRLock {
129137
#[pymethod(name = "__enter__")]
130138
#[allow(clippy::float_cmp, clippy::match_bool)]
131139
fn acquire(&self, args: AcquireArgs, vm: &VirtualMachine) -> PyResult<bool> {
132-
match args.waitflag {
133-
true if args.timeout == -1.0 => {
134-
self.mu.lock();
135-
Ok(true)
136-
}
137-
true if args.timeout < 0.0 => {
138-
Err(vm.new_value_error("timeout value must be positive".to_owned()))
139-
}
140-
true => Ok(self.mu.try_lock_for(Duration::from_secs_f64(args.timeout))),
141-
false if args.timeout != -1.0 => {
142-
Err(vm
143-
.new_value_error("can't specify a timeout for a non-blocking call".to_owned()))
144-
}
145-
false => Ok(self.mu.try_lock()),
146-
}
140+
acquire_lock_impl!(&self.mu, args, vm)
147141
}
148142
#[pymethod]
149143
#[pymethod(name = "release_lock")]

0 commit comments

Comments
 (0)