Skip to content

Commit 540ccd7

Browse files
committed
Fix thread finalization with LLVM
1 parent 0d09ad7 commit 540ccd7

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextPyStateBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Object get(Object tstateCurrentPtr,
114114
@CachedLibrary("tstateCurrentPtr") InteropLibrary lib) {
115115
PythonThreadState pythonThreadState = getContext().getThreadState(getLanguage());
116116
if (!lib.isNull(tstateCurrentPtr)) {
117-
pythonThreadState.setNativeThreadLocalVarPointer(tstateCurrentPtr);
117+
pythonThreadState.setNativeThreadLocalVarPointer(lib, tstateCurrentPtr);
118118
}
119119
return PThreadState.getOrCreateNativeThreadState(pythonThreadState);
120120
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static com.oracle.graal.python.nodes.StringLiterals.T_WARNINGS;
6666
import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.isJavaString;
6767
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
68+
import static com.oracle.graal.python.util.PythonUtils.initUnsafe;
6869
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
6970
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
7071

@@ -351,6 +352,12 @@ public static final class PythonThreadState {
351352
*/
352353
Object nativeThreadLocalVarPointer;
353354

355+
/**
356+
* Raw pointer to {@link #nativeThreadLocalVarPointer} if available, so that we can clean up
357+
* during thread disposal.
358+
*/
359+
long nativeThreadLocalVarRawPointer;
360+
354361
/* The global tracing function, set by sys.settrace and returned by sys.gettrace. */
355362
Object traceFun;
356363

@@ -551,9 +558,14 @@ public void dispose(PythonContext context) {
551558
}
552559
/*
553560
* Write 'NULL' to the native thread-local variable used to store the PyThreadState
554-
* struct such that it cannot accidentally be reused.
561+
* struct such that it cannot accidentally be reused. We can invoke LLVM only if we are
562+
* not shutting down the thread.
555563
*/
556-
if (nativeThreadLocalVarPointer != null) {
564+
if (nativeThreadLocalVarRawPointer != 0 && context.env.isNativeAccessAllowed()) {
565+
context.getUnsafe().putAddress(nativeThreadLocalVarRawPointer, 0);
566+
nativeThreadLocalVarRawPointer = 0;
567+
nativeThreadLocalVarPointer = null;
568+
} else if (nativeThreadLocalVarPointer != null && !isShuttingDown()) {
557569
CStructAccess.WritePointerNode.writeUncached(nativeThreadLocalVarPointer, 0, context.getNativeNull());
558570
nativeThreadLocalVarPointer = null;
559571
}
@@ -624,10 +636,17 @@ public void setAsyncgenFirstIter(Object asyncgenFirstIter) {
624636
this.asyncgenFirstIter = asyncgenFirstIter;
625637
}
626638

627-
public void setNativeThreadLocalVarPointer(Object ptr) {
639+
public void setNativeThreadLocalVarPointer(InteropLibrary interop, Object ptr) {
628640
// either unset or same
629641
assert nativeThreadLocalVarPointer == null || nativeThreadLocalVarPointer == ptr ||
630642
InteropLibrary.getUncached().isIdentical(nativeThreadLocalVarPointer, ptr, InteropLibrary.getUncached());
643+
if (interop.isPointer(ptr)) {
644+
try {
645+
this.nativeThreadLocalVarRawPointer = interop.asPointer(ptr);
646+
} catch (UnsupportedMessageException e) {
647+
throw CompilerDirectives.shouldNotReachHere(e);
648+
}
649+
}
631650
this.nativeThreadLocalVarPointer = ptr;
632651
}
633652
}

0 commit comments

Comments
 (0)