43
43
import static com .oracle .graal .python .nodes .SpecialMethodNames .__ENTER__ ;
44
44
import static com .oracle .graal .python .nodes .SpecialMethodNames .__EXIT__ ;
45
45
import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
46
+ import static com .oracle .graal .python .runtime .exception .PythonErrorType .ValueError ;
46
47
47
48
import java .util .List ;
48
49
53
54
import com .oracle .graal .python .builtins .objects .PNone ;
54
55
import com .oracle .graal .python .builtins .objects .thread .RLockBuiltinsFactory .AcquireRLockNodeFactory ;
55
56
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
57
+ import com .oracle .graal .python .nodes .expression .CastToBooleanNode ;
56
58
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
57
59
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
58
60
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
59
61
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
62
+ import com .oracle .graal .python .nodes .util .CastToDoubleNode ;
60
63
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
61
64
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
62
65
import com .oracle .truffle .api .dsl .Cached ;
@@ -72,31 +75,47 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
72
75
return RLockBuiltinsFactory .getFactories ();
73
76
}
74
77
75
- @ Builtin (name = "acquire" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 3 , keywordArguments = {"waitflag " , "timeout" })
78
+ @ Builtin (name = "acquire" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 3 , keywordArguments = {"blocking " , "timeout" })
76
79
@ GenerateNodeFactory
77
80
abstract static class AcquireRLockNode extends PythonTernaryBuiltinNode {
78
81
@ Specialization
79
82
@ TruffleBoundary
80
- boolean doAcquire (PRLock self , int waitFlag , double timeout ) {
81
- return self .acquire (waitFlag , timeout );
83
+ boolean doAcquire (PRLock self , @ SuppressWarnings ( "unused" ) PNone waitFlag , @ SuppressWarnings ( "unused" ) PNone timeout ) {
84
+ return self .acquire (true );
82
85
}
83
86
84
87
@ Specialization
85
88
@ TruffleBoundary
86
- boolean doAcquire (PRLock self , @ SuppressWarnings ("unused" ) PNone waitFlag , double timeout ) {
87
- return self .acquire (1 , timeout );
89
+ boolean doAcquire (PRLock self , Object blocking , @ SuppressWarnings ("unused" ) PNone timeout ,
90
+ @ Cached ("createIfTrueNode()" ) CastToBooleanNode castToBooleanNode ) {
91
+ return self .acquire (castToBooleanNode .executeWith (blocking ));
88
92
}
89
93
90
94
@ Specialization
91
95
@ TruffleBoundary
92
- boolean doAcquire (PRLock self , int waitFlag , @ SuppressWarnings ("unused" ) PNone timeout ) {
93
- return self .acquire (waitFlag , -1.0 );
96
+ boolean doAcquire (PRLock self , @ SuppressWarnings ("unused" ) PNone waitFlag , Object timeout ,
97
+ @ Cached ("create()" ) CastToDoubleNode castToDoubleNode ) {
98
+ double timeoutSeconds = castToDoubleNode .execute (timeout );
99
+ if (timeoutSeconds < 0 ) {
100
+ throw raise (ValueError , "timeout value must be positive" );
101
+ }
102
+ return self .acquire (true , timeoutSeconds );
94
103
}
95
104
96
105
@ Specialization
97
106
@ TruffleBoundary
98
- boolean doAcquire (PRLock self , @ SuppressWarnings ("unused" ) PNone waitFlag , @ SuppressWarnings ("unused" ) PNone timeout ) {
99
- return self .acquire (1 , -1.0 );
107
+ boolean doAcquire (PRLock self , Object blocking , Object timeout ,
108
+ @ Cached ("create()" ) CastToDoubleNode castToDoubleNode ,
109
+ @ Cached ("createIfTrueNode()" ) CastToBooleanNode castToBooleanNode ) {
110
+ boolean isBlocking = castToBooleanNode .executeWith (blocking );
111
+ if (!isBlocking ) {
112
+ throw raise (ValueError , "can't specify a timeout for a non-blocking call" );
113
+ }
114
+ double timeoutSeconds = castToDoubleNode .execute (timeout );
115
+ if (timeoutSeconds < 0 ) {
116
+ throw raise (ValueError , "timeout value must be positive" );
117
+ }
118
+ return self .acquire (true , timeoutSeconds );
100
119
}
101
120
102
121
public static AcquireRLockNode create () {
@@ -181,7 +200,7 @@ abstract static class ReprRLockNode extends PythonUnaryBuiltinNode {
181
200
String repr (PRLock self ) {
182
201
return String .format ("<%s %s object owner=%d count=%d at %s>" ,
183
202
(self .locked ()) ? "locked" : "unlocked" ,
184
- self .getPythonClass (),
203
+ self .getPythonClass (). getName () ,
185
204
self .getOwnerId (),
186
205
self .getCount (),
187
206
self .hashCode ());
0 commit comments