|
50 | 50 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Py_ssize_t;
|
51 | 51 | import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Void;
|
52 | 52 |
|
| 53 | +import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBinaryBuiltinNode; |
53 | 54 | import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
|
54 | 55 | import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiNullaryBuiltinNode;
|
55 | 56 | import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
|
56 | 57 | import com.oracle.graal.python.builtins.objects.PNone;
|
| 58 | +import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext; |
57 | 59 | import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
|
| 60 | +import com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor; |
58 | 61 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
59 | 62 | import com.oracle.graal.python.builtins.objects.frame.PFrame;
|
60 | 63 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
| 64 | +import com.oracle.graal.python.builtins.objects.thread.PThread; |
| 65 | +import com.oracle.graal.python.nodes.PRaiseNode; |
| 66 | +import com.oracle.graal.python.nodes.PRootNode; |
61 | 67 | import com.oracle.graal.python.nodes.frame.GetCurrentFrameRef;
|
62 | 68 | import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
|
63 | 69 | import com.oracle.graal.python.nodes.util.CannotCastException;
|
|
67 | 73 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
68 | 74 | import com.oracle.graal.python.util.OverflowException;
|
69 | 75 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
| 76 | +import com.oracle.truffle.api.ThreadLocalAction; |
| 77 | +import com.oracle.truffle.api.TruffleLogger; |
70 | 78 | import com.oracle.truffle.api.dsl.Bind;
|
71 | 79 | import com.oracle.truffle.api.dsl.Cached;
|
72 | 80 | import com.oracle.truffle.api.dsl.Specialization;
|
73 | 81 | import com.oracle.truffle.api.interop.InteropLibrary;
|
74 | 82 | import com.oracle.truffle.api.library.CachedLibrary;
|
75 | 83 | import com.oracle.truffle.api.nodes.Node;
|
| 84 | +import com.oracle.truffle.api.nodes.RootNode; |
76 | 85 |
|
77 | 86 | public final class PythonCextPyStateBuiltins {
|
78 | 87 |
|
@@ -137,6 +146,43 @@ PDict get(@Cached PythonObjectFactory factory) {
|
137 | 146 | }
|
138 | 147 | }
|
139 | 148 |
|
| 149 | + @CApiBuiltin(ret = Int, args = {ArgDescriptor.UNSIGNED_LONG, ArgDescriptor.PyObjectTransfer}, call = Direct) |
| 150 | + abstract static class PyThreadState_SetAsyncExc extends CApiBinaryBuiltinNode { |
| 151 | + public static final TruffleLogger LOGGER = CApiContext.getLogger(PyThreadState_SetAsyncExc.class); |
| 152 | + |
| 153 | + @Specialization |
| 154 | + @TruffleBoundary |
| 155 | + int doIt(long id, Object exceptionObject) { |
| 156 | + LOGGER.warning("The application uses PyThreadState_SetAsyncExc, which is not reliably supported by GraalPy"); |
| 157 | + for (Thread thread : getContext().getThreads()) { |
| 158 | + if (PThread.getThreadId(thread) == id) { |
| 159 | + // We do not want to raise in some internal code, it could corrupt internal data |
| 160 | + // structures. |
| 161 | + ThreadLocalAction action = new ThreadLocalAction(true, false) { |
| 162 | + int missedCount = 0; |
| 163 | + |
| 164 | + @Override |
| 165 | + protected void perform(Access access) { |
| 166 | + Node location = access.getLocation(); |
| 167 | + if (location != null) { |
| 168 | + RootNode rootNode = location.getRootNode(); |
| 169 | + if (rootNode instanceof PRootNode && !rootNode.isInternal()) { |
| 170 | + throw PRaiseNode.raiseExceptionObject(null, exceptionObject); |
| 171 | + } |
| 172 | + } |
| 173 | + if (missedCount++ < 20) { |
| 174 | + getContext().getEnv().submitThreadLocal(new Thread[]{thread}, this); |
| 175 | + } |
| 176 | + } |
| 177 | + }; |
| 178 | + getContext().getEnv().submitThreadLocal(new Thread[]{thread}, action); |
| 179 | + return 1; |
| 180 | + } |
| 181 | + } |
| 182 | + return 0; |
| 183 | + } |
| 184 | + } |
| 185 | + |
140 | 186 | @CApiBuiltin(ret = PyFrameObjectTransfer, args = {PyThreadState}, call = Direct)
|
141 | 187 | abstract static class PyThreadState_GetFrame extends CApiUnaryBuiltinNode {
|
142 | 188 | @Specialization
|
|
0 commit comments