Skip to content

Commit 5cf2cf4

Browse files
committed
Make GilNode accept PythonContext
1 parent 10a1f69 commit 5cf2cf4

File tree

1 file changed

+39
-11
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime

1 file changed

+39
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/GilNode.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@
4646
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
49-
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
5049
import com.oracle.truffle.api.nodes.Node;
5150
import com.oracle.truffle.api.profiles.ConditionProfile;
5251

5352
public abstract class GilNode extends Node {
5453

5554
private static final class Cached extends GilNode {
56-
@CompilationFinal ContextReference<PythonContext> contextRef;
57-
@CompilationFinal LanguageReference<PythonLanguage> languageRef;
55+
@CompilationFinal private ContextReference<PythonContext> contextRef;
5856
private final ConditionProfile binaryProfile = ConditionProfile.createBinaryProfile();
5957

6058
@Override
@@ -64,6 +62,16 @@ public boolean isAdoptable() {
6462

6563
@Override
6664
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) {
6775
// n.b.: we cannot make any optimizations here based on the singleThreadedAssumption of
6876
// the language. You would think that you could use that assumption to get rid even of
6977
// the ownsGil check, but we need to actually release the GIL around blocking operations
@@ -74,21 +82,20 @@ public void release(boolean wasAcquired) {
7482
// into the next (e.g. regular) GIL release. So we need to always have this ownsGil
7583
// check.
7684
if (binaryProfile.profile(wasAcquired)) {
77-
getContext().releaseGil();
85+
context.releaseGil();
7886
}
7987
}
8088

8189
@Override
82-
public boolean acquire() {
83-
PythonContext context = getContext();
90+
public boolean acquire(PythonContext context) {
8491
if (binaryProfile.profile(!context.ownsGil())) {
8592
context.acquireGil();
8693
return true;
8794
}
8895
return false;
8996
}
9097

91-
private final PythonContext getContext() {
98+
private PythonContext getContext() {
9299
if (contextRef == null) {
93100
CompilerDirectives.transferToInterpreterAndInvalidate();
94101
contextRef = lookupContextReference(PythonLanguage.class);
@@ -106,19 +113,30 @@ public boolean isAdoptable() {
106113
@Override
107114
@TruffleBoundary
108115
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) {
110128
if (!context.ownsGil()) {
111-
PythonLanguage.getContext().acquireGil();
129+
context.acquireGil();
112130
return true;
113131
}
114132
return false;
115133
}
116134

117135
@Override
118136
@TruffleBoundary
119-
public final void release(boolean wasAcquired) {
137+
public final void release(PythonContext context, boolean wasAcquired) {
120138
if (wasAcquired) {
121-
PythonLanguage.getContext().releaseGil();
139+
context.releaseGil();
122140
}
123141
}
124142

@@ -161,13 +179,23 @@ public final void close() {
161179
*/
162180
public abstract boolean acquire();
163181

182+
/**
183+
* @see #acquire()
184+
*/
185+
public abstract boolean acquire(PythonContext context);
186+
164187
/**
165188
* Release the GIL if {@code wasAcquired} is {@code true}.
166189
*
167190
* @param wasAcquired - the return value of the preceding {@link #acquire} call.
168191
*/
169192
public abstract void release(boolean wasAcquired);
170193

194+
/**
195+
* @see #release(boolean)
196+
*/
197+
public abstract void release(PythonContext context, boolean wasAcquired);
198+
171199
public static GilNode create() {
172200
return new Cached();
173201
}

0 commit comments

Comments
 (0)