Skip to content

Commit f79c04d

Browse files
[GR-30323][GR-30494] Remove GIL from python internal libs.
PullRequest: graalpython/1737
2 parents cb98afe + f5e89bd commit f79c04d

38 files changed

+1225
-2092
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ graalpython/lib-python/3/test/data
6868
!graalpython/lib-python/3/test/data/README
6969
/venv
7070
*.code-workspace
71+
graalpython/com.oracle.graal.python.test/src/tests/cpyext/*.c
72+
graalpython/com.oracle.graal.python.test/src/tests/cpyext/*.sha256
73+
graalpython/com.oracle.graal.python.test/src/tests/cpyext/As_FileDescriptor_Testfile
74+
graalpython/lib-python/3/lib2to3/Grammar3.8.5.alpha.0.pickle
75+
graalpython/lib-python/3/lib2to3/PatternGrammar3.8.5.alpha.0.pickle

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ private RootNode parseWithArguments(ParsingRequest request) {
325325
CompilerDirectives.transferToInterpreter();
326326
final PythonLanguage lang = this;
327327
final RootNode executableNode = new RootNode(lang) {
328-
@Node.Child private RootNode rootNode;
328+
@Child private RootNode rootNode;
329+
@Child private GilNode gilNode;
329330

330331
protected Object[] preparePArguments(VirtualFrame frame) {
331332
int argumentsLength = frame.getArguments().length;
@@ -347,9 +348,18 @@ public Object execute(VirtualFrame frame) {
347348
CompilerDirectives.transferToInterpreterAndInvalidate();
348349
parse(context, frame);
349350
}
350-
Object[] args = preparePArguments(frame);
351-
Object result = InvokeNode.invokeUncached(rootNode.getCallTarget(), args);
352-
return result;
351+
if (gilNode == null) {
352+
CompilerDirectives.transferToInterpreterAndInvalidate();
353+
gilNode = insert(GilNode.create());
354+
}
355+
boolean wasAcquired = gilNode.acquire();
356+
try {
357+
Object[] args = preparePArguments(frame);
358+
Object result = InvokeNode.invokeUncached(rootNode.getCallTarget(), args);
359+
return result;
360+
} finally {
361+
gilNode.release(wasAcquired);
362+
}
353363
}
354364

355365
private void parse(PythonContext context, VirtualFrame frame) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -685,17 +685,12 @@ static long asJavaLongWithState(PythonBuiltinClassType type, ThreadState state,
685685
@ExportMessage
686686
public Object lookupAttributeInternal(ThreadState state, String attribName, boolean strict,
687687
@Cached ConditionProfile gotState,
688-
@Cached.Exclusive @Cached PythonAbstractObject.LookupAttributeNode lookup, @Cached.Exclusive @Cached GilNode gil) {
689-
boolean mustRelease = gil.acquire();
690-
try {
691-
VirtualFrame frame = null;
692-
if (gotState.profile(state != null)) {
693-
frame = PArguments.frameForCall(state);
694-
}
695-
return lookup.execute(frame, this, attribName, strict);
696-
} finally {
697-
gil.release(mustRelease);
688+
@Cached.Exclusive @Cached PythonAbstractObject.LookupAttributeNode lookup) {
689+
VirtualFrame frame = null;
690+
if (gotState.profile(state != null)) {
691+
frame = PArguments.frameForCall(state);
698692
}
693+
return lookup.execute(frame, this, attribName, strict);
699694
}
700695

701696
@ExportMessage

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import static com.oracle.graal.python.builtins.objects.thread.AbstractPythonLock.TIMEOUT_MAX;
4444

45+
import java.io.PrintWriter;
4546
import java.lang.ref.WeakReference;
4647
import java.util.List;
4748

@@ -51,13 +52,13 @@
5152
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5253
import com.oracle.graal.python.builtins.PythonBuiltins;
5354
import com.oracle.graal.python.builtins.objects.PNone;
55+
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5456
import com.oracle.graal.python.builtins.objects.function.PKeyword;
5557
import com.oracle.graal.python.builtins.objects.thread.PLock;
5658
import com.oracle.graal.python.builtins.objects.thread.PRLock;
5759
import com.oracle.graal.python.builtins.objects.thread.PThread;
5860
import com.oracle.graal.python.builtins.objects.thread.PThreadLocal;
5961
import com.oracle.graal.python.nodes.ErrorMessages;
60-
import com.oracle.graal.python.nodes.WriteUnraisableNode;
6162
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
6263
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
6364
import com.oracle.graal.python.nodes.call.CallNode;
@@ -204,14 +205,24 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
204205
} catch (PythonThreadKillException e) {
205206
return;
206207
} catch (PException e) {
207-
WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), "in thread started by", callable);
208+
dumpError(context, e.getUnreifiedException(), callable);
209+
// TODO (cbasca): when GR-30386 is completed use the intrinsified
210+
// sys.unraisablehook
211+
// WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), "in
212+
// thread started by", callable);
208213
}
209214
}, env.getContext(), context.getThreadGroup());
210215

211216
PThread pThread = factory().createPythonThread(cls, thread);
212217
pThread.start();
213218
return pThread.getId();
214219
}
220+
221+
@TruffleBoundary
222+
static void dumpError(PythonContext context, PBaseException exception, Object object) {
223+
PrintWriter err = new PrintWriter(context.getStandardErr());
224+
err.println(String.format("%s in thread started by %s", exception.toString(), object));
225+
}
215226
}
216227

217228
@Builtin(name = "_set_sentinel", minNumOfPositionalArgs = 0)

0 commit comments

Comments
 (0)