46
46
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
47
47
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
48
48
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
49
- import com .oracle .truffle .api .TruffleLanguage .LanguageReference ;
50
49
import com .oracle .truffle .api .nodes .Node ;
51
50
import com .oracle .truffle .api .profiles .ConditionProfile ;
52
51
53
52
public abstract class GilNode extends Node {
54
53
55
54
private static final class Cached extends GilNode {
56
- @ CompilationFinal ContextReference <PythonContext > contextRef ;
57
- @ CompilationFinal LanguageReference <PythonLanguage > languageRef ;
55
+ @ CompilationFinal private ContextReference <PythonContext > contextRef ;
58
56
private final ConditionProfile binaryProfile = ConditionProfile .createBinaryProfile ();
59
57
60
58
@ Override
@@ -64,6 +62,16 @@ public boolean isAdoptable() {
64
62
65
63
@ Override
66
64
public void release (boolean wasAcquired ) {
65
+ release (getContext (), wasAcquired );
66
+ }
67
+
68
+ @ Override
69
+ public boolean acquire () {
70
+ return acquire (getContext ());
71
+ }
72
+
73
+ @ Override
74
+ public void release (PythonContext context , boolean wasAcquired ) {
67
75
// n.b.: we cannot make any optimizations here based on the singleThreadedAssumption of
68
76
// the language. You would think that you could use that assumption to get rid even of
69
77
// the ownsGil check, but we need to actually release the GIL around blocking operations
@@ -74,21 +82,20 @@ public void release(boolean wasAcquired) {
74
82
// into the next (e.g. regular) GIL release. So we need to always have this ownsGil
75
83
// check.
76
84
if (binaryProfile .profile (wasAcquired )) {
77
- getContext () .releaseGil ();
85
+ context .releaseGil ();
78
86
}
79
87
}
80
88
81
89
@ Override
82
- public boolean acquire () {
83
- PythonContext context = getContext ();
90
+ public boolean acquire (PythonContext context ) {
84
91
if (binaryProfile .profile (!context .ownsGil ())) {
85
92
context .acquireGil ();
86
93
return true ;
87
94
}
88
95
return false ;
89
96
}
90
97
91
- private final PythonContext getContext () {
98
+ private PythonContext getContext () {
92
99
if (contextRef == null ) {
93
100
CompilerDirectives .transferToInterpreterAndInvalidate ();
94
101
contextRef = lookupContextReference (PythonLanguage .class );
@@ -106,19 +113,30 @@ public boolean isAdoptable() {
106
113
@ Override
107
114
@ TruffleBoundary
108
115
public final boolean acquire () {
109
- PythonContext context = PythonLanguage .getContext ();
116
+ return acquire (PythonLanguage .getContext ());
117
+ }
118
+
119
+ @ Override
120
+ @ TruffleBoundary
121
+ public final void release (boolean wasAcquired ) {
122
+ release (PythonLanguage .getContext (), wasAcquired );
123
+ }
124
+
125
+ @ Override
126
+ @ TruffleBoundary
127
+ public final boolean acquire (PythonContext context ) {
110
128
if (!context .ownsGil ()) {
111
- PythonLanguage . getContext () .acquireGil ();
129
+ context .acquireGil ();
112
130
return true ;
113
131
}
114
132
return false ;
115
133
}
116
134
117
135
@ Override
118
136
@ TruffleBoundary
119
- public final void release (boolean wasAcquired ) {
137
+ public final void release (PythonContext context , boolean wasAcquired ) {
120
138
if (wasAcquired ) {
121
- PythonLanguage . getContext () .releaseGil ();
139
+ context .releaseGil ();
122
140
}
123
141
}
124
142
@@ -161,13 +179,23 @@ public final void close() {
161
179
*/
162
180
public abstract boolean acquire ();
163
181
182
+ /**
183
+ * @see #acquire()
184
+ */
185
+ public abstract boolean acquire (PythonContext context );
186
+
164
187
/**
165
188
* Release the GIL if {@code wasAcquired} is {@code true}.
166
189
*
167
190
* @param wasAcquired - the return value of the preceding {@link #acquire} call.
168
191
*/
169
192
public abstract void release (boolean wasAcquired );
170
193
194
+ /**
195
+ * @see #release(boolean)
196
+ */
197
+ public abstract void release (PythonContext context , boolean wasAcquired );
198
+
171
199
public static GilNode create () {
172
200
return new Cached ();
173
201
}
0 commit comments