Skip to content

Commit fc59aa1

Browse files
committed
Use GetThreadStateNode in HPyExternalFunctionInvokeNode
1 parent 760ba50 commit fc59aa1

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/HPyExternalFunctionNodes.java

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
8484
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
8585
import com.oracle.graal.python.runtime.PythonContext;
86+
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
87+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
88+
import com.oracle.graal.python.runtime.PythonContextFactory.GetThreadStateNodeGen;
8689
import com.oracle.graal.python.runtime.PythonOptions;
8790
import com.oracle.graal.python.runtime.exception.PException;
8891
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -297,28 +300,33 @@ abstract static class HPyExternalFunctionInvokeNode extends Node implements Indi
297300
@Child private HPyConvertArgsToSulongNode toSulongNode;
298301
@Child private HPyCheckFunctionResultNode checkFunctionResultNode;
299302
@Child private HPyCloseArgHandlesNode handleCloseNode;
303+
@Child private GetThreadStateNode getThreadStateNode;
300304

301305
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
302306
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
303307

304308
HPyExternalFunctionInvokeNode() {
309+
CompilerAsserts.neverPartOfCompilation();
305310
this.toSulongNode = HPyAllAsHandleNodeGen.create();
306311
this.checkFunctionResultNode = HPyCheckHandleResultNodeGen.create();
307312
this.handleCloseNode = this.toSulongNode.createCloseHandleNode();
313+
this.getThreadStateNode = GetThreadStateNodeGen.create();
308314
}
309315

310316
HPyExternalFunctionInvokeNode(HPyConvertArgsToSulongNode convertArgsNode) {
311317
CompilerAsserts.neverPartOfCompilation();
312318
this.toSulongNode = convertArgsNode != null ? convertArgsNode : HPyAllAsHandleNodeGen.create();
313319
this.checkFunctionResultNode = HPyCheckHandleResultNodeGen.create();
314320
this.handleCloseNode = this.toSulongNode.createCloseHandleNode();
321+
this.getThreadStateNode = GetThreadStateNodeGen.create();
315322
}
316323

317324
HPyExternalFunctionInvokeNode(HPyCheckFunctionResultNode checkFunctionResultNode, HPyConvertArgsToSulongNode convertArgsNode) {
318325
CompilerAsserts.neverPartOfCompilation();
319326
this.toSulongNode = convertArgsNode != null ? convertArgsNode : HPyAllAsHandleNodeGen.create();
320327
this.checkFunctionResultNode = checkFunctionResultNode != null ? checkFunctionResultNode : HPyCheckHandleResultNodeGen.create();
321328
this.handleCloseNode = this.toSulongNode.createCloseHandleNode();
329+
this.getThreadStateNode = GetThreadStateNodeGen.create();
322330
}
323331

324332
public abstract Object execute(VirtualFrame frame, String name, Object callable, Object[] frameArgs);
@@ -335,21 +343,23 @@ Object doIt(VirtualFrame frame, String name, Object callable, Object[] arguments
335343
// first arg is always the HPyContext
336344
convertedArguments[0] = hPyContext;
337345

346+
PythonThreadState pythonThreadState = getThreadStateNode.execute(ctx);
347+
338348
// If any code requested the caught exception (i.e. used 'sys.exc_info()'), we store
339349
// it to the context since we cannot propagate it through the native frames.
340-
Object state = IndirectCallContext.enter(frame, ctx, this);
350+
Object state = IndirectCallContext.enter(frame, pythonThreadState, this);
341351

342352
try {
343-
return checkFunctionResultNode.execute(ctx, name, lib.execute(callable, convertedArguments));
353+
return checkFunctionResultNode.execute(pythonThreadState, hPyContext, name, lib.execute(callable, convertedArguments));
344354
} catch (UnsupportedTypeException | UnsupportedMessageException e) {
345355
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "Calling native function %s failed: %m", name, e);
346356
} catch (ArityException e) {
347357
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "Calling native function %s expected %d arguments but got %d.", name, e.getExpectedMinArity(), e.getActualArity());
348358
} finally {
349359
// special case after calling a C function: transfer caught exception back to frame
350360
// to simulate the global state semantics
351-
PArguments.setException(frame, ctx.getCaughtException());
352-
IndirectCallContext.exit(frame, ctx, state);
361+
PArguments.setException(frame, pythonThreadState.getCaughtException());
362+
IndirectCallContext.exit(frame, pythonThreadState, state);
353363

354364
// close all handles (if necessary)
355365
if (handleCloseNode != null) {
@@ -928,29 +938,29 @@ abstract static class HPyCheckFunctionResultNode extends CheckFunctionResultNode
928938
*/
929939
@Override
930940
public final Object execute(PythonContext context, String name, Object result) {
931-
return execute(context, context.getHPyContext(), name, result);
941+
return execute(context.getThreadState(), context.getHPyContext(), name, result);
932942
}
933943

934944
/**
935945
* This is the preferred way for executing the node since it avoids unnecessary field reads
936946
* in the interpreter or multi-context mode.
937947
*/
938-
public abstract Object execute(PythonContext context, GraalHPyContext nativeContext, String name, Object value);
948+
public abstract Object execute(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, Object value);
939949

940-
protected final void checkFunctionResult(String name, boolean indicatesError, PythonContext context, PRaiseNode raise, PythonObjectFactory factory, PythonLanguage language) {
941-
PException currentException = context.getCurrentException();
950+
protected final void checkFunctionResult(String name, boolean indicatesError, PythonThreadState pythonThreadState, PRaiseNode raise, PythonObjectFactory factory, PythonLanguage language) {
951+
PException currentException = pythonThreadState.getCurrentException();
942952
boolean errOccurred = currentException != null;
943953
if (indicatesError) {
944954
// consume exception
945-
context.setCurrentException(null);
955+
pythonThreadState.setCurrentException(null);
946956
if (!errOccurred) {
947957
throw raise.raise(PythonErrorType.SystemError, ErrorMessages.RETURNED_NULL_WO_SETTING_ERROR, name);
948958
} else {
949959
throw currentException.getExceptionForReraise();
950960
}
951961
} else if (errOccurred) {
952962
// consume exception
953-
context.setCurrentException(null);
963+
pythonThreadState.setCurrentException(null);
954964
PBaseException sysExc = factory.createBaseException(PythonErrorType.SystemError, ErrorMessages.RETURNED_RESULT_WITH_ERROR_SET, new Object[]{name});
955965
sysExc.setCause(currentException.getEscapedException());
956966
throw PException.fromObject(sysExc, this, PythonOptions.isPExceptionWithJavaStacktrace(language));
@@ -963,17 +973,17 @@ protected final void checkFunctionResult(String name, boolean indicatesError, Py
963973
public abstract static class HPyCheckHandleResultNode extends HPyCheckFunctionResultNode {
964974

965975
@Specialization(guards = "value == 0")
966-
Object doIntegerNull(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") int value,
976+
Object doIntegerNull(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") int value,
967977
@Shared("language") @CachedLanguage PythonLanguage language,
968978
@Shared("fact") @Cached PythonObjectFactory factory,
969979
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
970980
// NULL handle must not be closed
971-
checkFunctionResult(name, true, context, raiseNode, factory, language);
981+
checkFunctionResult(name, true, pythonThreadState, raiseNode, factory, language);
972982
throw CompilerDirectives.shouldNotReachHere("an exception should have been thrown");
973983
}
974984

975985
@Specialization(replaces = "doIntegerNull")
976-
Object doInteger(PythonContext context, GraalHPyContext nativeContext, String name, int value,
986+
Object doInteger(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, int value,
977987
@Exclusive @Cached HPyAsPythonObjectNode asPythonObjectNode,
978988
@Shared("language") @CachedLanguage PythonLanguage language,
979989
@Shared("fact") @Cached PythonObjectFactory factory,
@@ -984,22 +994,22 @@ Object doInteger(PythonContext context, GraalHPyContext nativeContext, String na
984994
// handle and we don't need it any longer. So, close it in every case.
985995
nativeContext.releaseHPyHandleForObject(value);
986996
}
987-
checkFunctionResult(name, isNullHandle, context, raiseNode, factory, language);
997+
checkFunctionResult(name, isNullHandle, pythonThreadState, raiseNode, factory, language);
988998
return asPythonObjectNode.execute(nativeContext, value);
989999
}
9901000

9911001
@Specialization(guards = "value == 0")
992-
Object doLongNull(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") long value,
1002+
Object doLongNull(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") long value,
9931003
@Shared("language") @CachedLanguage PythonLanguage language,
9941004
@Shared("fact") @Cached PythonObjectFactory factory,
9951005
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
9961006
// NULL handle must not be closed
997-
checkFunctionResult(name, true, context, raiseNode, factory, language);
1007+
checkFunctionResult(name, true, pythonThreadState, raiseNode, factory, language);
9981008
throw CompilerDirectives.shouldNotReachHere("an exception should have been thrown");
9991009
}
10001010

10011011
@Specialization(replaces = "doLongNull")
1002-
Object doLong(PythonContext context, GraalHPyContext nativeContext, String name, long value,
1012+
Object doLong(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, long value,
10031013
@Exclusive @Cached HPyAsPythonObjectNode asPythonObjectNode,
10041014
@Shared("language") @CachedLanguage PythonLanguage language,
10051015
@Shared("fact") @Cached PythonObjectFactory factory,
@@ -1010,22 +1020,22 @@ Object doLong(PythonContext context, GraalHPyContext nativeContext, String name,
10101020
// handle and we don't need it any longer. So, close it in every case.
10111021
nativeContext.releaseHPyHandleForObject(value);
10121022
}
1013-
checkFunctionResult(name, isNullHandle, context, raiseNode, factory, language);
1023+
checkFunctionResult(name, isNullHandle, pythonThreadState, raiseNode, factory, language);
10141024
return asPythonObjectNode.execute(nativeContext, value);
10151025
}
10161026

10171027
@Specialization(guards = "isNullHandle(nativeContext, handle)")
1018-
Object doNullHandle(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") GraalHPyHandle handle,
1028+
Object doNullHandle(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, @SuppressWarnings("unused") GraalHPyHandle handle,
10191029
@Shared("language") @CachedLanguage PythonLanguage language,
10201030
@Shared("fact") @Cached PythonObjectFactory factory,
10211031
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
10221032
// NULL handle must not be closed
1023-
checkFunctionResult(name, true, context, raiseNode, factory, language);
1033+
checkFunctionResult(name, true, pythonThreadState, raiseNode, factory, language);
10241034
throw CompilerDirectives.shouldNotReachHere("an exception should have been thrown");
10251035
}
10261036

10271037
@Specialization(guards = "!isNullHandle(nativeContext, handle)", replaces = "doNullHandle")
1028-
Object doNonNullHandle(PythonContext context, GraalHPyContext nativeContext, String name, GraalHPyHandle handle,
1038+
Object doNonNullHandle(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, GraalHPyHandle handle,
10291039
@Cached ConditionProfile isAllocatedProfile,
10301040
@Exclusive @Cached HPyAsPythonObjectNode asPythonObjectNode,
10311041
@Shared("language") @CachedLanguage PythonLanguage language,
@@ -1034,12 +1044,12 @@ Object doNonNullHandle(PythonContext context, GraalHPyContext nativeContext, Str
10341044
// Python land is receiving a handle from an HPy extension, so we are now owning the
10351045
// handle and we don't need it any longer. So, close it in every case.
10361046
handle.close(nativeContext, isAllocatedProfile);
1037-
checkFunctionResult(name, false, context, raiseNode, factory, language);
1047+
checkFunctionResult(name, false, pythonThreadState, raiseNode, factory, language);
10381048
return asPythonObjectNode.execute(nativeContext, handle);
10391049
}
10401050

10411051
@Specialization(replaces = {"doIntegerNull", "doNonNullHandle"})
1042-
Object doHandle(PythonContext context, GraalHPyContext nativeContext, String name, GraalHPyHandle handle,
1052+
Object doHandle(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, GraalHPyHandle handle,
10431053
@Cached ConditionProfile isAllocatedProfile,
10441054
@Exclusive @Cached HPyAsPythonObjectNode asPythonObjectNode,
10451055
@Shared("language") @CachedLanguage PythonLanguage language,
@@ -1051,12 +1061,12 @@ Object doHandle(PythonContext context, GraalHPyContext nativeContext, String nam
10511061
// handle and we don't need it any longer. So, close it in every case.
10521062
handle.close(nativeContext, isAllocatedProfile);
10531063
}
1054-
checkFunctionResult(name, isNullHandle, context, raiseNode, factory, language);
1064+
checkFunctionResult(name, isNullHandle, pythonThreadState, raiseNode, factory, language);
10551065
return asPythonObjectNode.execute(nativeContext, handle);
10561066
}
10571067

10581068
@Specialization(replaces = {"doIntegerNull", "doInteger", "doLongNull", "doLong", "doNullHandle", "doNonNullHandle", "doHandle"})
1059-
Object doGeneric(PythonContext context, GraalHPyContext nativeContext, String name, Object value,
1069+
Object doGeneric(PythonThreadState pythonThreadState, GraalHPyContext nativeContext, String name, Object value,
10601070
@Cached HPyEnsureHandleNode ensureHandleNode,
10611071
@Cached ConditionProfile isAllocatedProfile,
10621072
@Cached HPyAsPythonObjectNode asPythonObjectNode,
@@ -1070,7 +1080,7 @@ Object doGeneric(PythonContext context, GraalHPyContext nativeContext, String na
10701080
// handle and we don't need it any longer. So, close it in every case.
10711081
handle.close(nativeContext, isAllocatedProfile);
10721082
}
1073-
checkFunctionResult(name, isNullHandle(nativeContext, handle), context, raiseNode, factory, language);
1083+
checkFunctionResult(name, isNullHandle(nativeContext, handle), pythonThreadState, raiseNode, factory, language);
10741084
return asPythonObjectNode.execute(nativeContext, handle);
10751085
}
10761086

@@ -1085,38 +1095,38 @@ protected static boolean isNullHandle(GraalHPyContext nativeContext, GraalHPyHan
10851095
*/
10861096
@ImportStatic(PGuards.class)
10871097
abstract static class HPyCheckPrimitiveResultNode extends HPyCheckFunctionResultNode {
1088-
public abstract int executeInt(PythonContext context, GraalHPyContext nativeContext, String name, int value);
1098+
public abstract int executeInt(PythonThreadState context, GraalHPyContext nativeContext, String name, int value);
10891099

1090-
public abstract long executeLong(PythonContext context, GraalHPyContext nativeContext, String name, long value);
1100+
public abstract long executeLong(PythonThreadState context, GraalHPyContext nativeContext, String name, long value);
10911101

10921102
@Specialization
1093-
int doInteger(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, int value,
1103+
int doInteger(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, int value,
10941104
@Shared("language") @CachedLanguage PythonLanguage language,
10951105
@Shared("fact") @Cached PythonObjectFactory factory,
10961106
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
1097-
checkFunctionResult(name, value == -1, context, raiseNode, factory, language);
1107+
checkFunctionResult(name, value == -1, pythonThreadState, raiseNode, factory, language);
10981108
return value;
10991109
}
11001110

11011111
@Specialization(replaces = "doInteger")
1102-
long doLong(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, long value,
1112+
long doLong(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, long value,
11031113
@Shared("language") @CachedLanguage PythonLanguage language,
11041114
@Shared("fact") @Cached PythonObjectFactory factory,
11051115
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
1106-
checkFunctionResult(name, value == -1, context, raiseNode, factory, language);
1116+
checkFunctionResult(name, value == -1, pythonThreadState, raiseNode, factory, language);
11071117
return value;
11081118
}
11091119

11101120
@Specialization(limit = "1")
1111-
Object doObject(PythonContext context, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, Object value,
1121+
Object doObject(PythonThreadState pythonThreadState, @SuppressWarnings("unused") GraalHPyContext nativeContext, String name, Object value,
11121122
@Shared("language") @CachedLanguage PythonLanguage language,
11131123
@Shared("fact") @Cached PythonObjectFactory factory,
11141124
@CachedLibrary("value") InteropLibrary lib,
11151125
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
11161126
if (lib.fitsInLong(value)) {
11171127
try {
11181128
long lvalue = lib.asLong(value);
1119-
checkFunctionResult(name, lvalue == -1, context, raiseNode, factory, language);
1129+
checkFunctionResult(name, lvalue == -1, pythonThreadState, raiseNode, factory, language);
11201130
return lvalue;
11211131
} catch (UnsupportedMessageException e) {
11221132
throw CompilerDirectives.shouldNotReachHere();

0 commit comments

Comments
 (0)