Skip to content

Commit 6d25cd1

Browse files
committed
Release/acquire GIL in PyEval_Save/RestoreThread
1 parent 5cf2cf4 commit 6d25cd1

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

graalpython/com.oracle.graal.python.cext/src/ceval.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ int PyEval_ThreadsInitialized() {
5858
return 1;
5959
}
6060

61+
typedef PyThreadState* (*save_thread_fun_t)();
62+
UPCALL_TYPED_ID(PyEval_SaveThread, save_thread_fun_t);
6163
PyThreadState* PyEval_SaveThread() {
62-
return NULL;
64+
return _jls_PyEval_SaveThread();
6365
}
6466

67+
typedef void (*restore_thread_fun_t)();
68+
UPCALL_TYPED_ID(PyEval_RestoreThread, restore_thread_fun_t);
6569
void PyEval_RestoreThread(PyThreadState *ptr) {
70+
return _jls_PyEval_RestoreThread();
6671
}
6772

6873
UPCALL_ID(PyEval_GetBuiltins);

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
128128
import com.oracle.graal.python.builtins.objects.cext.capi.PyCFunctionDecorator;
129129
import com.oracle.graal.python.builtins.objects.cext.capi.PyDateTimeCAPIWrapper;
130+
import com.oracle.graal.python.builtins.objects.cext.capi.PyEvalNodes.PyEvalRestoreThread;
131+
import com.oracle.graal.python.builtins.objects.cext.capi.PyEvalNodes.PyEvalSaveThread;
130132
import com.oracle.graal.python.builtins.objects.cext.capi.PySequenceArrayWrapper;
131133
import com.oracle.graal.python.builtins.objects.cext.capi.PyTruffleObjectAlloc;
132134
import com.oracle.graal.python.builtins.objects.cext.capi.PyTruffleObjectFree;
@@ -238,7 +240,6 @@
238240
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
239241
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
240242
import com.oracle.graal.python.runtime.PythonContext;
241-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
242243
import com.oracle.graal.python.runtime.PythonCore;
243244
import com.oracle.graal.python.runtime.PythonOptions;
244245
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
@@ -312,6 +313,8 @@ public void initialize(PythonCore core) {
312313
builtinConstants.put("CErrorHandler", errorHandlerClass);
313314
builtinConstants.put(ERROR_HANDLER, core.factory().createPythonObject(errorHandlerClass));
314315
builtinConstants.put(NATIVE_NULL, new PythonNativeNull());
316+
builtinConstants.put("PyEval_SaveThread", new PyEvalSaveThread());
317+
builtinConstants.put("PyEval_RestoreThread", new PyEvalRestoreThread());
315318
}
316319

317320
@FunctionalInterface
@@ -1711,14 +1714,7 @@ abstract static class PyThreadStateGet extends NativeBuiltin {
17111714

17121715
@Specialization
17131716
PThreadState get() {
1714-
PythonThreadState threadState = getContext().getThreadState();
1715-
PThreadState nativeWrapper = threadState.getNativeWrapper();
1716-
if (nativeWrapper == null) {
1717-
nativeWrapper = new PThreadState(threadState);
1718-
threadState.setNativeWrapper(nativeWrapper);
1719-
}
1720-
// does not require a 'to_sulong' since it is already a native wrapper type
1721-
return nativeWrapper;
1717+
return PThreadState.getThreadState(getContext());
17221718
}
17231719
}
17241720

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PThreadState.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,21 @@ public class PThreadState extends PythonNativeWrapper {
104104

105105
private final PythonThreadState threadState;
106106

107-
public PThreadState(PythonThreadState threadState) {
107+
private PThreadState(PythonThreadState threadState) {
108108
this.threadState = threadState;
109109
}
110110

111+
public static PThreadState getThreadState(PythonContext context) {
112+
PythonThreadState threadState = context.getThreadState();
113+
PThreadState nativeWrapper = threadState.getNativeWrapper();
114+
if (nativeWrapper == null) {
115+
nativeWrapper = new PThreadState(threadState);
116+
threadState.setNativeWrapper(nativeWrapper);
117+
}
118+
// does not require a 'to_sulong' since it is already a native wrapper type
119+
return nativeWrapper;
120+
}
121+
111122
// READ
112123
@ExportMessage
113124
boolean hasMembers() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package com.oracle.graal.python.builtins.objects.cext.capi;
43+
44+
import com.oracle.graal.python.PythonLanguage;
45+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject.PExecuteNode;
46+
import com.oracle.graal.python.runtime.GilNode;
47+
import com.oracle.graal.python.runtime.PythonContext;
48+
import com.oracle.truffle.api.TruffleLogger;
49+
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.CachedContext;
51+
import com.oracle.truffle.api.interop.InteropLibrary;
52+
import com.oracle.truffle.api.interop.TruffleObject;
53+
import com.oracle.truffle.api.library.ExportLibrary;
54+
import com.oracle.truffle.api.library.ExportMessage;
55+
56+
public abstract class PyEvalNodes {
57+
58+
/**
59+
* A simple executable interop object that returns the thread state structure (i.e.
60+
* {@code PyThreadState}) and releases the GIL. This cannot be implemented as a Python built-in
61+
* function in {@code PythonCextBuiltins} because if the built-in function would be called via
62+
* interop (this would go through
63+
* {@link com.oracle.graal.python.builtins.objects.PythonAbstractObject#execute(Object[], PExecuteNode, GilNode)}
64+
* ), it tries to acquire/release the GIL for exection. This would trigger an assertion error.
65+
*/
66+
@ExportLibrary(InteropLibrary.class)
67+
@SuppressWarnings("static-method")
68+
public static final class PyEvalSaveThread implements TruffleObject {
69+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(PyEvalSaveThread.class);
70+
71+
@ExportMessage
72+
boolean isExecutable() {
73+
return true;
74+
}
75+
76+
@ExportMessage
77+
PThreadState execute(@SuppressWarnings("unused") Object[] arguments,
78+
@CachedContext(PythonLanguage.class) PythonContext context,
79+
@Cached GilNode gil) {
80+
PThreadState threadState = PThreadState.getThreadState(context);
81+
LOGGER.fine("C extension releases GIL");
82+
gil.release(context, true);
83+
return threadState;
84+
}
85+
}
86+
87+
/**
88+
* A simple executable interop object that acquires the GIL. This cannot be implemented as a
89+
* Python built-in function in {@code PythonCextBuiltins} because if the built-in function would
90+
* be called via interop (this would go through
91+
* {@link com.oracle.graal.python.builtins.objects.PythonAbstractObject#execute(Object[], PExecuteNode, GilNode)}
92+
* ), it tries to acquire/release the GIL for execution. This would again release the GIL when
93+
* returning.
94+
*/
95+
@ExportLibrary(InteropLibrary.class)
96+
@SuppressWarnings("static-method")
97+
public static final class PyEvalRestoreThread implements TruffleObject {
98+
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(PyEvalRestoreThread.class);
99+
100+
@ExportMessage
101+
boolean isExecutable() {
102+
return true;
103+
}
104+
105+
@ExportMessage
106+
PThreadState execute(@SuppressWarnings("unused") Object[] arguments,
107+
@CachedContext(PythonLanguage.class) PythonContext context,
108+
@Cached GilNode gil) {
109+
PThreadState threadState = PThreadState.getThreadState(context);
110+
LOGGER.fine("C extension acquires GIL");
111+
gil.acquire(context);
112+
return threadState;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)