|
43 | 43 | import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Direct; |
44 | 44 | import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Ignored; |
45 | 45 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Int; |
| 46 | +import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Pointer; |
46 | 47 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyFrameObjectTransfer; |
47 | 48 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyObject; |
48 | 49 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyObjectBorrowed; |
@@ -112,6 +113,42 @@ static Object restore( |
112 | 113 | } |
113 | 114 | } |
114 | 115 |
|
| 116 | + /** |
| 117 | + * Very unlikely fallback for threads that were already attached when another thread initialized |
| 118 | + * the C API, but were blocked at that time and therefore could not process the thread-local |
| 119 | + * action that eagerly initializes their native 'tstate_current' TLS slot. |
| 120 | + */ |
| 121 | + @CApiBuiltin(ret = PyThreadState, args = {Pointer}, acquireGil = false, call = Ignored) |
| 122 | + abstract static class GraalPyPrivate_ThreadState_Get extends CApiUnaryBuiltinNode { |
| 123 | + private static final TruffleLogger LOGGER = CApiContext.getLogger(GraalPyPrivate_ThreadState_Get.class); |
| 124 | + |
| 125 | + @Specialization |
| 126 | + @TruffleBoundary |
| 127 | + static Object get(Object tstateCurrentPtr) { |
| 128 | + PythonContext context = PythonContext.get(null); |
| 129 | + PythonThreadState threadState = context.getThreadState(context.getLanguage()); |
| 130 | + |
| 131 | + /* |
| 132 | + * The C caller may have observed 'tstate_current == NULL' before entering this upcall. |
| 133 | + * While entering this builtin, the same thread may process a queued thread-local action |
| 134 | + * from C API initialization and initialize its native thread state eagerly. So the |
| 135 | + * fallback decision made in C can be stale by the time we get here. |
| 136 | + */ |
| 137 | + if (threadState.isNativeThreadStateInitialized()) { |
| 138 | + LOGGER.fine(() -> String.format("Lazy initialization attempt of native thread state for thread %s aborted. Was initialized in the meantime.", Thread.currentThread())); |
| 139 | + Object nativeThreadState = PThreadState.getNativeThreadState(threadState); |
| 140 | + assert nativeThreadState != null; |
| 141 | + return nativeThreadState; |
| 142 | + } |
| 143 | + |
| 144 | + LOGGER.fine(() -> "Lazy (fallback) initialization of native thread state for thread " + Thread.currentThread()); |
| 145 | + assert PThreadState.getNativeThreadState(threadState) == null; |
| 146 | + Object nativeThreadState = PThreadState.getOrCreateNativeThreadState(threadState); |
| 147 | + threadState.setNativeThreadLocalVarPointer(tstateCurrentPtr); |
| 148 | + return nativeThreadState; |
| 149 | + } |
| 150 | + } |
| 151 | + |
115 | 152 | @CApiBuiltin(ret = Void, args = {}, call = Ignored) |
116 | 153 | abstract static class GraalPyPrivate_BeforeThreadDetach extends CApiNullaryBuiltinNode { |
117 | 154 | @Specialization |
|
0 commit comments