@@ -32,10 +32,39 @@ struct AcquireArgs {
32
32
timeout : Either < f64 , isize > ,
33
33
}
34
34
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
+
35
63
#[ pyclass( name = "lock" ) ]
36
64
struct PyLock {
37
65
mu : RawMutex ,
38
66
}
67
+ type PyLockRef = PyRef < PyLock > ;
39
68
40
69
impl PyValue for PyLock {
41
70
fn class ( vm : & VirtualMachine ) -> PyClassRef {
@@ -56,28 +85,7 @@ impl PyLock {
56
85
#[ pymethod( name = "__enter__" ) ]
57
86
#[ allow( clippy:: float_cmp, clippy:: match_bool) ]
58
87
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)
81
89
}
82
90
#[ pymethod]
83
91
#[ pymethod( name = "release_lock" ) ]
@@ -129,21 +137,7 @@ impl PyRLock {
129
137
#[ pymethod( name = "__enter__" ) ]
130
138
#[ allow( clippy:: float_cmp, clippy:: match_bool) ]
131
139
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)
147
141
}
148
142
#[ pymethod]
149
143
#[ pymethod( name = "release_lock" ) ]
0 commit comments