40
40
*/
41
41
package com .oracle .graal .python .builtins .objects .thread ;
42
42
43
- import static com .oracle .graal .python .builtins .objects .thread .AbstractPythonLock .DEFAULT_BLOCKING ;
44
- import static com .oracle .graal .python .builtins .objects .thread .AbstractPythonLock .DEFAULT_TIMEOUT ;
45
- import static com .oracle .graal .python .builtins .objects .thread .AbstractPythonLock .TIMEOUT_MAX ;
46
- import static com .oracle .graal .python .nodes .SpecialMethodNames .__ENTER__ ;
47
- import static com .oracle .graal .python .nodes .SpecialMethodNames .__EXIT__ ;
48
- import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
49
- import static com .oracle .graal .python .runtime .exception .PythonErrorType .OverflowError ;
50
- import static com .oracle .graal .python .runtime .exception .PythonErrorType .ValueError ;
51
-
52
43
import java .util .List ;
53
44
54
45
import com .oracle .graal .python .builtins .Builtin ;
55
46
import com .oracle .graal .python .builtins .CoreFunctions ;
56
47
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
57
48
import com .oracle .graal .python .builtins .PythonBuiltins ;
58
49
import com .oracle .graal .python .builtins .objects .PNone ;
59
- import com .oracle .graal .python .builtins .objects .thread .RLockBuiltinsFactory .AcquireRLockNodeFactory ;
60
50
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
61
- import com .oracle .graal .python .nodes .expression .CastToBooleanNode ;
62
51
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
63
- import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
64
- import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
65
52
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
66
- import com .oracle .graal .python .nodes .util .CastToDoubleNode ;
67
53
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
68
- import com .oracle .truffle .api .CompilerDirectives ;
69
- import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
70
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
71
54
import com .oracle .truffle .api .dsl .Cached ;
72
55
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
73
56
import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -81,98 +64,6 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
81
64
return RLockBuiltinsFactory .getFactories ();
82
65
}
83
66
84
- @ Builtin (name = "acquire" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 3 , keywordArguments = {"blocking" , "timeout" })
85
- @ GenerateNodeFactory
86
- abstract static class AcquireRLockNode extends PythonTernaryBuiltinNode {
87
- private @ Child CastToDoubleNode castToDoubleNode ;
88
- private @ Child CastToBooleanNode castToBooleanNode ;
89
- private @ CompilationFinal ConditionProfile isBlockingProfile = ConditionProfile .createBinaryProfile ();
90
- private @ CompilationFinal ConditionProfile defaultTimeoutProfile = ConditionProfile .createBinaryProfile ();
91
-
92
- private CastToDoubleNode getCastToDoubleNode () {
93
- if (castToDoubleNode == null ) {
94
- CompilerDirectives .transferToInterpreterAndInvalidate ();
95
- castToDoubleNode = insert (CastToDoubleNode .create ());
96
- }
97
- return castToDoubleNode ;
98
- }
99
-
100
- private CastToBooleanNode getCastToBooleanNode () {
101
- if (castToBooleanNode == null ) {
102
- CompilerDirectives .transferToInterpreterAndInvalidate ();
103
- castToBooleanNode = insert (CastToBooleanNode .createIfTrueNode ());
104
- }
105
- return castToBooleanNode ;
106
- }
107
-
108
- @ Specialization
109
- boolean doAcquire (PRLock self , Object blocking , Object timeout ) {
110
- // args setup
111
- boolean isBlocking = (blocking instanceof PNone ) ? DEFAULT_BLOCKING : getCastToBooleanNode ().executeWith (blocking );
112
- double timeoutSeconds = DEFAULT_TIMEOUT ;
113
- if (!(timeout instanceof PNone )) {
114
- if (!isBlocking ) {
115
- throw raise (ValueError , "can't specify a timeout for a non-blocking call" );
116
- }
117
-
118
- timeoutSeconds = getCastToDoubleNode ().execute (timeout );
119
-
120
- if (timeoutSeconds < 0 ) {
121
- throw raise (ValueError , "timeout value must be positive" );
122
- } else if (timeoutSeconds > TIMEOUT_MAX ) {
123
- throw raise (OverflowError , "timeout value is too large" );
124
- }
125
- }
126
-
127
- // acquire lock
128
- if (isBlockingProfile .profile (!isBlocking )) {
129
- return self .acquireNonBlocking ();
130
- } else {
131
- if (defaultTimeoutProfile .profile (timeoutSeconds == DEFAULT_TIMEOUT )) {
132
- return self .acquireBlocking ();
133
- } else {
134
- return self .acquireTimeout (timeoutSeconds );
135
- }
136
- }
137
- }
138
-
139
- public static AcquireRLockNode create () {
140
- return AcquireRLockNodeFactory .create ();
141
- }
142
- }
143
-
144
- @ Builtin (name = __ENTER__ , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 3 , keywordArguments = {"blocking" , "timeout" })
145
- @ GenerateNodeFactory
146
- abstract static class EnterRLockNode extends PythonTernaryBuiltinNode {
147
- @ Specialization
148
- Object acquire (PRLock self , Object blocking , Object timeout ,
149
- @ Cached ("create()" ) AcquireRLockNode acquireLockNode ) {
150
- return acquireLockNode .execute (self , blocking , timeout );
151
- }
152
- }
153
-
154
- @ Builtin (name = "release" , fixedNumOfPositionalArgs = 1 )
155
- @ GenerateNodeFactory
156
- abstract static class ReleaseRLockNode extends PythonUnaryBuiltinNode {
157
- @ Specialization
158
- @ TruffleBoundary
159
- Object doRelease (PRLock self ) {
160
- self .release ();
161
- return PNone .NONE ;
162
- }
163
- }
164
-
165
- @ Builtin (name = __EXIT__ , fixedNumOfPositionalArgs = 4 )
166
- @ GenerateNodeFactory
167
- abstract static class ExitRLockNode extends PythonBuiltinNode {
168
- @ Specialization
169
- @ TruffleBoundary
170
- Object exit (PRLock self , @ SuppressWarnings ("unused" ) Object type , @ SuppressWarnings ("unused" ) Object value , @ SuppressWarnings ("unused" ) Object traceback ) {
171
- self .release ();
172
- return PNone .NONE ;
173
- }
174
- }
175
-
176
67
@ Builtin (name = "_is_owned" , fixedNumOfPositionalArgs = 1 )
177
68
@ GenerateNodeFactory
178
69
abstract static class IsOwnedRLockNode extends PythonUnaryBuiltinNode {
@@ -209,19 +100,4 @@ Object releaseSave(PRLock self,
209
100
return retVal ;
210
101
}
211
102
}
212
-
213
- @ Builtin (name = __REPR__ , fixedNumOfPositionalArgs = 1 )
214
- @ GenerateNodeFactory
215
- abstract static class ReprRLockNode extends PythonUnaryBuiltinNode {
216
- @ Specialization
217
- @ TruffleBoundary
218
- String repr (PRLock self ) {
219
- return String .format ("<%s %s object owner=%d count=%d at %s>" ,
220
- (self .locked ()) ? "locked" : "unlocked" ,
221
- self .getPythonClass ().getName (),
222
- self .getOwnerId (),
223
- self .getCount (),
224
- self .hashCode ());
225
- }
226
- }
227
103
}
0 commit comments