Skip to content

Commit 737946c

Browse files
committed
Fix access to PythonThreadState
1 parent fc59aa1 commit 737946c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+677
-392
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/ConversionNodeTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
5454
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
5555
import com.oracle.graal.python.runtime.PythonContext;
56+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
5657
import com.oracle.graal.python.runtime.exception.PException;
5758
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5859
import com.oracle.truffle.api.RootCallTarget;
@@ -64,8 +65,10 @@ public class ConversionNodeTests {
6465
@Rule public ExpectedException expectedException = ExpectedException.none();
6566

6667
protected static Object call(Object arg, ArgumentCastNodeWithRaise castNode) {
68+
PythonLanguage language = PythonLanguage.getCurrent();
6769
final PythonContext pythonContext = PythonLanguage.getContext();
68-
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(null) {
70+
71+
RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(language) {
6972
@Child private CalleeContext calleeContext = CalleeContext.create();
7073
@Child private ArgumentCastNodeWithRaise node = castNode;
7174

@@ -94,11 +97,12 @@ public boolean isPythonInternal() {
9497
PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict());
9598
PArguments.setException(arguments, PException.NO_EXCEPTION);
9699
PArguments.setArgument(arguments, 0, arg);
97-
PFrame.Reference frameInfo = IndirectCalleeContext.enter(pythonContext, arguments, callTarget);
100+
PythonThreadState threadState = pythonContext.getThreadState(language);
101+
PFrame.Reference frameInfo = IndirectCalleeContext.enter(threadState, arguments, callTarget);
98102
try {
99103
return CallTargetInvokeNode.invokeUncached(callTarget, arguments);
100104
} finally {
101-
IndirectCalleeContext.exit(pythonContext, frameInfo);
105+
IndirectCalleeContext.exit(threadState, frameInfo);
102106
}
103107
} catch (PException e) {
104108
// materialize PException's error message since we are leaving Python

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.parser.PythonParserImpl;
5959
import com.oracle.graal.python.runtime.GilNode;
6060
import com.oracle.graal.python.runtime.PythonContext;
61+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
6162
import com.oracle.graal.python.runtime.PythonCore;
6263
import com.oracle.graal.python.runtime.PythonOptions;
6364
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -190,7 +191,8 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
190191
private Shape cApiSymbolCache;
191192
private Shape hpySymbolCache;
192193

193-
private final ContextThreadLocal<PythonContext.PythonThreadState> threadState = createContextThreadLocal(PythonContext.PythonThreadState::new);
194+
/** For fast access to the PythonThreadState object by the owning thread. */
195+
private final ContextThreadLocal<PythonThreadState> threadState = createContextThreadLocal(PythonContext.PythonThreadState::new);
194196

195197
public final ConcurrentHashMap<String, HiddenKey> typeHiddenKeys = new ConcurrentHashMap<>(TypeBuiltins.INITIAL_HIDDEN_TYPE_KEYS);
196198

@@ -216,6 +218,14 @@ public NodeFactory getNodeFactory() {
216218
return nodeFactory;
217219
}
218220

221+
/**
222+
* <b>DO NOT DIRECTLY USE THIS METHOD !!!</b>
223+
* Instead, use {@link PythonContext#getThreadState(PythonLanguage)}}.
224+
*/
225+
public ContextThreadLocal<PythonThreadState> getThreadStateLocal() {
226+
return threadState;
227+
}
228+
219229
@Override
220230
protected void finalizeContext(PythonContext context) {
221231
context.finalizeContext();
@@ -242,7 +252,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
242252
@Override
243253
protected PythonContext createContext(Env env) {
244254
Python3Core newCore = new Python3Core(new PythonParserImpl(env), env.isNativeAccessAllowed());
245-
final PythonContext context = new PythonContext(this, env, newCore, threadState);
255+
final PythonContext context = new PythonContext(this, env, newCore);
246256
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
247257

248258
Object[] engineOptionsUnroll = this.engineOptionsStorage;
@@ -773,7 +783,7 @@ protected void initializeMultiThreading(PythonContext context) {
773783

774784
@Override
775785
protected void initializeThread(PythonContext context, Thread thread) {
776-
context.attachThread(thread);
786+
context.attachThread(thread, threadState);
777787
}
778788

779789
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import com.oracle.graal.python.nodes.object.GetClassNode;
6161
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6262
import com.oracle.graal.python.runtime.PythonContext;
63+
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
64+
import com.oracle.graal.python.runtime.PythonContextFactory.GetThreadStateNodeGen;
6365
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
6466
import com.oracle.graal.python.runtime.exception.PException;
6567
import com.oracle.truffle.api.CompilerDirectives;
@@ -85,6 +87,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
8587
abstract static class RegisterNode extends PythonVarargsBuiltinNode {
8688
private static class AtExitRootNode extends RootNode {
8789
@Child private CallNode callNode = CallNode.create();
90+
@Child private GetThreadStateNode getThreadStateNode = GetThreadStateNodeGen.create();
8891

8992
private final ContextReference<PythonContext> contextRef = lookupContextReference(PythonLanguage.class);
9093

@@ -95,8 +98,8 @@ protected AtExitRootNode(TruffleLanguage<?> language) {
9598
@Override
9699
public Object execute(VirtualFrame frame) {
97100
PythonContext context = contextRef.get();
98-
context.setTopFrameInfo(PFrame.Reference.EMPTY);
99-
context.setCaughtException(PException.NO_EXCEPTION);
101+
getThreadStateNode.setTopFrameInfo(context, PFrame.Reference.EMPTY);
102+
getThreadStateNode.setCaughtException(context, PException.NO_EXCEPTION);
100103

101104
Object callable = frame.getArguments()[0];
102105
Object[] arguments = (Object[]) frame.getArguments()[1];
@@ -110,8 +113,8 @@ public Object execute(VirtualFrame frame) {
110113
handleException(context, e);
111114
throw e;
112115
} finally {
113-
context.popTopFrameInfo();
114-
context.setCaughtException(null);
116+
getThreadStateNode.clearTopFrameInfo(context);
117+
getThreadStateNode.setCaughtException(context, null);
115118
}
116119
}
117120

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
243243
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
244244
import com.oracle.graal.python.runtime.PythonContext;
245+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
245246
import com.oracle.graal.python.runtime.PythonCore;
246247
import com.oracle.graal.python.runtime.exception.PException;
247248
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -258,7 +259,7 @@
258259
import com.oracle.truffle.api.Truffle;
259260
import com.oracle.truffle.api.dsl.Cached;
260261
import com.oracle.truffle.api.dsl.Cached.Shared;
261-
import com.oracle.truffle.api.dsl.CachedContext;
262+
import com.oracle.truffle.api.dsl.CachedLanguage;
262263
import com.oracle.truffle.api.dsl.Fallback;
263264
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
264265
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -2149,6 +2150,7 @@ Object type(Object cls, Object obj, PNone bases, PNone dict, PKeyword[] kwds,
21492150
@Megamorphic
21502151
@Specialization(guards = "isString(wName)")
21512152
Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict namespaceOrig, PKeyword[] kwds,
2153+
@CachedLanguage PythonLanguage language,
21522154
@Cached GetClassNode getClassNode,
21532155
@CachedLibrary(limit = "5") PythonObjectLibrary lib,
21542156
@CachedLibrary(limit = "3") HashingStorageLibrary hashingStoragelib,
@@ -2185,8 +2187,8 @@ Object typeNew(VirtualFrame frame, Object cls, Object wName, PTuple bases, PDict
21852187
assert SpecialMethodSlot.pushInitializedTypePlaceholder();
21862188
PDict namespace = factory().createDict();
21872189
namespace.setDictStorage(initNode.execute(frame, namespaceOrig, PKeyword.EMPTY_KEYWORDS));
2188-
PythonClass newType = typeMetaclass(frame, name, bases, namespace, metaclass, lib, hashingStoragelib, getDictAttrNode, getWeakRefAttrNode, getBestBaseNode, getItemSize, writeItemSize,
2189-
isIdentifier);
2190+
PythonClass newType = typeMetaclass(frame, language, name, bases, namespace, metaclass, lib, hashingStoragelib,
2191+
getDictAttrNode, getWeakRefAttrNode, getBestBaseNode, getItemSize, writeItemSize, isIdentifier);
21902192

21912193
for (DictEntry entry : hashingStoragelib.entries(namespace.getDictStorage())) {
21922194
Object setName = getSetNameNode.execute(entry.value);
@@ -2287,7 +2289,7 @@ private String getModuleNameFromGlobals(PythonObject globals, HashingStorageLibr
22872289
}
22882290
}
22892291

2290-
private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases, PDict namespace, Object metaclass,
2292+
private PythonClass typeMetaclass(VirtualFrame frame, PythonLanguage language, String name, PTuple bases, PDict namespace, Object metaclass,
22912293
PythonObjectLibrary lib, HashingStorageLibrary hashingStorageLib, LookupAttributeInMRONode getDictAttrNode,
22922294
LookupAttributeInMRONode getWeakRefAttrNode, GetBestBaseClassNode getBestBaseNode, GetItemsizeNode getItemSize, WriteAttributeToObjectNode writeItemSize,
22932295
IsIdentifierNode isIdentifier) {
@@ -2425,8 +2427,8 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24252427
}
24262428
// Make slots into a tuple
24272429
}
2428-
PythonContext context = getContextRef().get();
2429-
Object state = IndirectCallContext.enter(frame, context, this);
2430+
PythonThreadState threadState = getContextRef().get().getThreadState(language);
2431+
Object state = IndirectCallContext.enter(frame, threadState, this);
24302432
try {
24312433
pythonClass.setAttribute(__SLOTS__, slotsObject);
24322434
if (basesArray.length > 1) {
@@ -2442,7 +2444,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
24422444
addNativeSlots(pythonClass, newSlots);
24432445
}
24442446
} finally {
2445-
IndirectCallContext.exit(frame, context, state);
2447+
IndirectCallContext.exit(frame, threadState, state);
24462448
}
24472449
Object dict = LookupAttributeInMRONode.lookupSlowPath(pythonClass, __DICT__);
24482450
if (!addDict && dict == PNone.NO_VALUE) {
@@ -3375,29 +3377,30 @@ PMemoryView fromArray(@SuppressWarnings("unused") Object cls, PArray object) {
33753377
}
33763378

33773379
@Specialization
3378-
PMemoryView fromMemoryView(@SuppressWarnings("unused") Object cls, PMemoryView object,
3379-
@Shared("c") @CachedContext(PythonLanguage.class) PythonContext context) {
3380+
PMemoryView fromMemoryView(@SuppressWarnings("unused") Object cls, PMemoryView object) {
33803381
object.checkReleased(this);
3381-
return factory().createMemoryView(context, object.getManagedBuffer(), object.getOwner(), object.getLength(),
3382+
return factory().createMemoryView(getContext(), object.getManagedBuffer(), object.getOwner(), object.getLength(),
33823383
object.isReadOnly(), object.getItemSize(), object.getFormat(), object.getFormatString(), object.getDimensions(),
33833384
object.getBufferPointer(), object.getOffset(), object.getBufferShape(), object.getBufferStrides(),
33843385
object.getBufferSuboffsets(), object.getFlags());
33853386
}
33863387

33873388
@Specialization
33883389
PMemoryView fromNative(VirtualFrame frame, @SuppressWarnings("unused") Object cls, PythonAbstractNativeObject object,
3390+
@CachedLanguage PythonLanguage language,
33893391
@Cached CExtNodes.ToSulongNode toSulongNode,
33903392
@Cached CExtNodes.AsPythonObjectNode asPythonObjectNode,
33913393
@Cached PCallCapiFunction callCapiFunction,
33923394
@Cached PythonCextBuiltins.DefaultCheckFunctionResultNode checkFunctionResultNode) {
33933395
PythonContext context = getContext();
3394-
Object state = IndirectCallContext.enter(frame, context, this);
3396+
PythonThreadState threadState = context.getThreadState(language);
3397+
Object state = IndirectCallContext.enter(frame, threadState, this);
33953398
try {
33963399
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, toSulongNode.execute(object));
33973400
checkFunctionResultNode.execute(context, FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT.getName(), result);
33983401
return (PMemoryView) asPythonObjectNode.execute(result);
33993402
} finally {
3400-
IndirectCallContext.exit(frame, context, state);
3403+
IndirectCallContext.exit(frame, threadState, state);
34013404
}
34023405
}
34033406

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
102102
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
103103
import com.oracle.graal.python.runtime.PythonContext;
104+
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
105+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
106+
import com.oracle.graal.python.runtime.PythonContextFactory.GetThreadStateNodeGen;
104107
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
105108
import com.oracle.graal.python.util.Function;
106109
import com.oracle.graal.python.util.PythonUtils;
@@ -480,6 +483,7 @@ static final class ExternalFunctionInvokeNode extends PNodeWithContext implement
480483
@Child private ToJavaStealingNode asPythonObjectNode = ToJavaStealingNodeGen.create();
481484
@Child private InteropLibrary lib;
482485
@Child private PRaiseNode raiseNode;
486+
@Child private GetThreadStateNode getThreadStateNode = GetThreadStateNodeGen.create();
483487

484488
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
485489
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
@@ -532,10 +536,11 @@ public Object execute(VirtualFrame frame, String name, Object callable, Object[]
532536
toSulongNode.executeInto(frameArgs, argsOffset, cArguments, 0);
533537

534538
PythonContext ctx = getContext();
539+
PythonThreadState threadState = getThreadStateNode.execute(ctx);
535540

536541
// If any code requested the caught exception (i.e. used 'sys.exc_info()'), we store
537542
// it to the context since we cannot propagate it through the native frames.
538-
Object state = IndirectCallContext.enter(frame, ctx, this);
543+
Object state = IndirectCallContext.enter(frame, threadState, this);
539544

540545
try {
541546
return fromNative(asPythonObjectNode.execute(checkResultNode.execute(ctx, name, lib.execute(callable, cArguments))));
@@ -548,8 +553,8 @@ public Object execute(VirtualFrame frame, String name, Object callable, Object[]
548553
} finally {
549554
// special case after calling a C function: transfer caught exception back to frame
550555
// to simulate the global state semantics
551-
PArguments.setException(frame, ctx.getCaughtException());
552-
IndirectCallContext.exit(frame, ctx, state);
556+
PArguments.setException(frame, threadState.getCaughtException());
557+
IndirectCallContext.exit(frame, threadState, state);
553558
}
554559
}
555560

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.List;
4545
import java.util.Map;
4646

47+
import com.oracle.graal.python.PythonLanguage;
4748
import com.oracle.graal.python.annotations.ArgumentClinic;
4849
import com.oracle.graal.python.builtins.Builtin;
4950
import com.oracle.graal.python.builtins.CoreFunctions;
@@ -56,11 +57,13 @@
5657
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
5758
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
5859
import com.oracle.graal.python.runtime.PythonContext;
60+
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
5961
import com.oracle.graal.python.runtime.PythonOptions;
6062
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
6163
import com.oracle.graal.python.runtime.exception.PException;
6264
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6365
import com.oracle.truffle.api.ThreadLocalAction;
66+
import com.oracle.truffle.api.dsl.CachedLanguage;
6467
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6568
import com.oracle.truffle.api.dsl.NodeFactory;
6669
import com.oracle.truffle.api.dsl.Specialization;
@@ -93,20 +96,22 @@ private static void dumpTraceback(Object callable, Object file) {
9396
@GenerateNodeFactory
9497
abstract static class DumpTracebackNode extends PythonClinicBuiltinNode {
9598
@Specialization
96-
public PNone doit(VirtualFrame frame, Object file, boolean allThreads) {
97-
Object state = IndirectCallContext.enter(frame, getContext(), this);
99+
PNone doit(VirtualFrame frame, Object file, boolean allThreads,
100+
@CachedLanguage PythonLanguage language) {
101+
PythonContext context = getContext();
102+
PythonThreadState threadState = context.getThreadState(language);
103+
Object state = IndirectCallContext.enter(frame, threadState, this);
98104
try {
99105
// it's not important for this to be fast at all
100-
dump(getContext(), file, allThreads);
106+
dump(language, context, file, allThreads);
101107
} finally {
102-
IndirectCallContext.exit(frame, getContext(), state);
108+
IndirectCallContext.exit(frame, threadState, state);
103109
}
104110
return PNone.NONE;
105111
}
106112

107113
@TruffleBoundary
108-
@SuppressWarnings("unchecked")
109-
private static final void dump(PythonContext context, Object file, boolean allThreads) {
114+
private static void dump(PythonLanguage language, PythonContext context, Object file, boolean allThreads) {
110115
Object printStackFunc;
111116
try {
112117
Object tracebackModule = AbstractImportNode.importModule("traceback");
@@ -116,7 +121,7 @@ private static final void dump(PythonContext context, Object file, boolean allTh
116121
}
117122

118123
if (allThreads) {
119-
if (PythonOptions.isWithJavaStacktrace(context.getLanguage())) {
124+
if (PythonOptions.isWithJavaStacktrace(language)) {
120125
PrintWriter err = new PrintWriter(context.getStandardErr());
121126
Thread[] ths = context.getThreads();
122127
for (Map.Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
@@ -145,7 +150,7 @@ protected void perform(ThreadLocalAction.Access access) {
145150
}
146151
});
147152
} else {
148-
if (PythonOptions.isWithJavaStacktrace(context.getLanguage())) {
153+
if (PythonOptions.isWithJavaStacktrace(language)) {
149154
PrintWriter err = new PrintWriter(context.getStandardErr());
150155
err.println();
151156
err.println(Thread.currentThread());

0 commit comments

Comments
 (0)