|
65 | 65 | import static com.oracle.graal.python.nodes.StringLiterals.T_WARNINGS;
|
66 | 66 | import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.isJavaString;
|
67 | 67 | import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
|
| 68 | +import static com.oracle.graal.python.util.PythonUtils.initUnsafe; |
68 | 69 | import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
|
69 | 70 | import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
|
70 | 71 |
|
@@ -351,6 +352,12 @@ public static final class PythonThreadState {
|
351 | 352 | */
|
352 | 353 | Object nativeThreadLocalVarPointer;
|
353 | 354 |
|
| 355 | + /** |
| 356 | + * Raw pointer to {@link #nativeThreadLocalVarPointer} if available, so that we can clean up |
| 357 | + * during thread disposal. |
| 358 | + */ |
| 359 | + long nativeThreadLocalVarRawPointer; |
| 360 | + |
354 | 361 | /* The global tracing function, set by sys.settrace and returned by sys.gettrace. */
|
355 | 362 | Object traceFun;
|
356 | 363 |
|
@@ -551,9 +558,14 @@ public void dispose(PythonContext context) {
|
551 | 558 | }
|
552 | 559 | /*
|
553 | 560 | * 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. |
555 | 563 | */
|
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()) { |
557 | 569 | CStructAccess.WritePointerNode.writeUncached(nativeThreadLocalVarPointer, 0, context.getNativeNull());
|
558 | 570 | nativeThreadLocalVarPointer = null;
|
559 | 571 | }
|
@@ -624,10 +636,17 @@ public void setAsyncgenFirstIter(Object asyncgenFirstIter) {
|
624 | 636 | this.asyncgenFirstIter = asyncgenFirstIter;
|
625 | 637 | }
|
626 | 638 |
|
627 |
| - public void setNativeThreadLocalVarPointer(Object ptr) { |
| 639 | + public void setNativeThreadLocalVarPointer(InteropLibrary interop, Object ptr) { |
628 | 640 | // either unset or same
|
629 | 641 | assert nativeThreadLocalVarPointer == null || nativeThreadLocalVarPointer == ptr ||
|
630 | 642 | 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 | + } |
631 | 650 | this.nativeThreadLocalVarPointer = ptr;
|
632 | 651 | }
|
633 | 652 | }
|
|
0 commit comments