Skip to content

Commit 7acb656

Browse files
committed
Allow multiple native contexts.
1 parent 4911d41 commit 7acb656

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
5252
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
5353
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
54+
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.PAsPointerNodeGen;
5455
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.ReadNativeMemberNodeGen;
5556
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.ToPyObjectNodeGen;
5657
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.WriteNativeMemberNodeGen;
@@ -76,6 +77,7 @@
7677
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7778
import com.oracle.graal.python.runtime.interop.PythonMessageResolution;
7879
import com.oracle.graal.python.runtime.sequence.PSequence;
80+
import com.oracle.truffle.api.Assumption;
7981
import com.oracle.truffle.api.CompilerDirectives;
8082
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
8183
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -529,7 +531,7 @@ public Object access(Object object) {
529531

530532
@Resolve(message = "TO_NATIVE")
531533
abstract static class ToNativeNode extends Node {
532-
@Child private ToPyObjectNode toPyObjectNode = ToPyObjectNodeGen.create();
534+
@Child private ToPyObjectNode toPyObjectNode = ToPyObjectNode.create();
533535

534536
Object access(PythonNativeWrapper obj) {
535537
if (!obj.isNative()) {
@@ -543,7 +545,7 @@ Object access(PythonNativeWrapper obj) {
543545
abstract static class IsPointerNode extends Node {
544546
@Child private Node isPointerNode;
545547

546-
Object access(PythonNativeWrapper obj) {
548+
boolean access(PythonNativeWrapper obj) {
547549
return obj.isNative() && (!(obj.getNativePointer() instanceof TruffleObject) || ForeignAccess.sendIsPointer(getIsPointerNode(), (TruffleObject) obj.getNativePointer()));
548550
}
549551

@@ -558,11 +560,26 @@ private Node getIsPointerNode() {
558560

559561
@Resolve(message = "AS_POINTER")
560562
abstract static class AsPointerNode extends Node {
561-
@Child private Node asPointerNode;
563+
@Child private PAsPointerNode pAsPointerNode = PAsPointerNode.create();
562564

563565
long access(PythonNativeWrapper obj) {
566+
return pAsPointerNode.execute(obj);
567+
}
568+
}
569+
570+
abstract static class PAsPointerNode extends PBaseNode {
571+
@Child private Node asPointerNode;
572+
573+
public abstract long execute(PythonNativeWrapper o);
574+
575+
@Specialization(assumptions = "getSingleNativeContextAssumption()")
576+
long doFast(PythonNativeWrapper obj) {
564577
// the native pointer object must either be a TruffleObject or a primitive
565578
Object nativePointer = obj.getNativePointer();
579+
return ensureLong(nativePointer);
580+
}
581+
582+
private long ensureLong(Object nativePointer) {
566583
if (nativePointer instanceof TruffleObject) {
567584
if (asPointerNode == null) {
568585
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -575,8 +592,22 @@ long access(PythonNativeWrapper obj) {
575592
}
576593
}
577594
return (long) nativePointer;
595+
}
578596

597+
@Specialization(replaces = "doFast")
598+
long doSlow(PythonNativeWrapper obj,
599+
@Cached("create()") ToPyObjectNode toPyObjectNode) {
600+
return ensureLong(toPyObjectNode.execute(obj));
579601
}
602+
603+
protected Assumption getSingleNativeContextAssumption() {
604+
return getContext().getSingleNativeContextAssumption();
605+
}
606+
607+
public static PAsPointerNode create() {
608+
return PAsPointerNodeGen.create();
609+
}
610+
580611
}
581612

582613
abstract static class ToPyObjectNode extends TransformToNativeNode {
@@ -662,5 +693,9 @@ private CExtNodes.ToSulongNode getToSulongNode() {
662693
}
663694
return toSulongNode;
664695
}
696+
697+
public static ToPyObjectNode create() {
698+
return ToPyObjectNodeGen.create();
699+
}
665700
}
666701
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4242
import com.oracle.graal.python.runtime.exception.PException;
4343
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
44+
import com.oracle.truffle.api.Assumption;
4445
import com.oracle.truffle.api.CallTarget;
4546
import com.oracle.truffle.api.CompilerDirectives;
4647
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4748
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49+
import com.oracle.truffle.api.Truffle;
4850
import com.oracle.truffle.api.TruffleLanguage;
4951

5052
public class PythonContext {
@@ -67,6 +69,7 @@ public class PythonContext {
6769
private OutputStream out;
6870
private OutputStream err;
6971
@CompilationFinal private boolean capiWasLoaded = false;
72+
private final Assumption singleNativeContext = Truffle.getRuntime().createAssumption("single native context assumption");
7073

7174
@CompilationFinal private HashingStorage.Equivalence slowPathEquivalence;
7275

@@ -218,4 +221,8 @@ public void initializeMainModule(String path) {
218221
mainModule.setAttribute(__FILE__, path);
219222
}
220223
}
224+
225+
public Assumption getSingleNativeContextAssumption() {
226+
return singleNativeContext;
227+
}
221228
}

0 commit comments

Comments
 (0)