Skip to content

Commit 3ded661

Browse files
committed
Introduce class CApiContext.
1 parent 323ca21 commit 3ded661

File tree

6 files changed

+91
-21
lines changed

6 files changed

+91
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private Object loadDynamicModuleWithSpec(String name, String path, InteropLibrar
257257
@TruffleBoundary
258258
private void ensureCapiWasLoaded() {
259259
PythonContext context = getContext();
260-
if (!context.capiWasLoaded()) {
260+
if (!context.hasCApiContext()) {
261261
Env env = context.getEnv();
262262
CompilerDirectives.transferToInterpreterAndInvalidate();
263263

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,12 @@ Object doGeneric(String name,
197197
@CachedLibrary(limit = "1") @SuppressWarnings("unused") InteropLibrary interopLib,
198198
@Shared("context") @CachedContext(PythonLanguage.class) @SuppressWarnings("unused") PythonContext context,
199199
@Cached PRaiseNode raiseNode) {
200-
return importCAPISymbol(raiseNode, interopLib, context.getCapiLibrary(), name);
200+
return importCAPISymbol(raiseNode, interopLib, context.getCApiContext(), name);
201201
}
202202

203203
protected static Object importCAPISymbolUncached(PythonContext context, String name) {
204-
Object capiLibrary = context.getCapiLibrary();
205-
InteropLibrary uncached = InteropLibrary.getFactory().getUncached(capiLibrary);
206-
return importCAPISymbol(PRaiseNode.getUncached(), uncached, capiLibrary, name);
204+
Object capiLibrary = context.getCApiContext().getLLVMLibrary();
205+
return importCAPISymbol(PRaiseNode.getUncached(), InteropLibrary.getFactory().getUncached(capiLibrary), capiLibrary, name);
207206
}
208207

209208
private static Object importCAPISymbol(PRaiseNode raiseNode, InteropLibrary library, Object capiLibrary, String name) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.objects.cext.capi;
42+
43+
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
44+
import com.oracle.graal.python.runtime.PythonContext;
45+
46+
public final class CApiContext extends CExtContext {
47+
public CApiContext(PythonContext context, Object hpyLibrary) {
48+
super(context, hpyLibrary);
49+
}
50+
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,25 @@
4343
import com.oracle.graal.python.runtime.PythonContext;
4444

4545
public abstract class CExtContext {
46-
public abstract PythonContext getContext();
4746

48-
public abstract Object getLLVMLibrary();
47+
public static CExtContext LAZY_CONTEXT = new CExtContext(null, null) {
48+
};
49+
50+
private final PythonContext context;
51+
52+
/** The LLVM bitcode library object representing 'libpython.*.so' or similar. */
53+
private final Object llvmLibrary;
54+
55+
public CExtContext(PythonContext context, Object llvmLibrary) {
56+
this.context = context;
57+
this.llvmLibrary = llvmLibrary;
58+
}
59+
60+
public final PythonContext getContext() {
61+
return context;
62+
}
63+
64+
public final Object getLLVMLibrary() {
65+
return llvmLibrary;
66+
}
4967
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@
4141
package com.oracle.graal.python.builtins.objects.cext.common;
4242

4343
import com.oracle.graal.python.nodes.PNodeWithContext;
44+
import com.oracle.graal.python.runtime.PythonContext;
4445

4546
public abstract class CExtToNativeNode extends PNodeWithContext {
4647

4748
public final Object execute(Object object) {
48-
return execute(null, object);
49+
return execute(CExtContext.LAZY_CONTEXT, object);
4950
}
5051

5152
public abstract Object execute(CExtContext nativeContext, Object object);

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.function.Supplier;
5252
import java.util.logging.Level;
5353

54+
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
5455
import org.graalvm.nativeimage.ImageInfo;
5556
import org.graalvm.options.OptionValues;
5657

@@ -198,7 +199,7 @@ List<WeakReference<Thread>> getOwners() {
198199
private OutputStream out;
199200
private OutputStream err;
200201
private InputStream in;
201-
@CompilationFinal private Object capiLibrary = null;
202+
@CompilationFinal private CApiContext cApiContext;
202203
private final Assumption singleThreaded = Truffle.getRuntime().createAssumption("single Threaded");
203204

204205
private static final Assumption singleNativeContext = Truffle.getRuntime().createAssumption("single native context assumption");
@@ -598,18 +599,6 @@ private static void writeWarning(String warning) {
598599
PythonLanguage.getLogger().warning(warning);
599600
}
600601

601-
public boolean capiWasLoaded() {
602-
return this.capiLibrary != null;
603-
}
604-
605-
public Object getCapiLibrary() {
606-
return this.capiLibrary;
607-
}
608-
609-
public void setCapiWasLoaded(Object capiLibrary) {
610-
this.capiLibrary = capiLibrary;
611-
}
612-
613602
public HashingStorage.Equivalence getSlowPathEquivalence() {
614603
if (slowPathEquivalence == null) {
615604
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -962,4 +951,17 @@ private static void releaseSentinelLock(WeakReference<PLock> sentinelLockWeakref
962951
}
963952
}
964953
}
954+
955+
public boolean hasCApiContext() {
956+
return cApiContext != null;
957+
}
958+
959+
public CApiContext getCApiContext() {
960+
return cApiContext;
961+
}
962+
963+
public void setCapiWasLoaded(Object capiLibrary) {
964+
assert cApiContext == null : "tried to create new C API context but it was already created";
965+
cApiContext = new CApiContext(this, capiLibrary);
966+
}
965967
}

0 commit comments

Comments
 (0)