Skip to content

Commit 4dc9060

Browse files
committed
[GR-31060] Implements C API functions PyEval_Save/RestoreThread.
PullRequest: graalpython/1775
2 parents 51a8273 + 6d25cd1 commit 4dc9060

File tree

5 files changed

+177
-22
lines changed

5 files changed

+177
-22
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;
@@ -313,6 +314,8 @@ public void initialize(PythonCore core) {
313314
builtinConstants.put("CErrorHandler", errorHandlerClass);
314315
builtinConstants.put(ERROR_HANDLER, core.factory().createPythonObject(errorHandlerClass));
315316
builtinConstants.put(NATIVE_NULL, new PythonNativeNull());
317+
builtinConstants.put("PyEval_SaveThread", new PyEvalSaveThread());
318+
builtinConstants.put("PyEval_RestoreThread", new PyEvalRestoreThread());
316319
}
317320

318321
@FunctionalInterface
@@ -1712,14 +1715,7 @@ abstract static class PyThreadStateGet extends NativeBuiltin {
17121715

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

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+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/GilNode.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@
4646
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
49-
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
5049
import com.oracle.truffle.api.nodes.Node;
5150
import com.oracle.truffle.api.profiles.ConditionProfile;
5251

5352
public abstract class GilNode extends Node {
5453

5554
private static final class Cached extends GilNode {
56-
@CompilationFinal ContextReference<PythonContext> contextRef;
57-
@CompilationFinal LanguageReference<PythonLanguage> languageRef;
55+
@CompilationFinal private ContextReference<PythonContext> contextRef;
5856
private final ConditionProfile binaryProfile = ConditionProfile.createBinaryProfile();
5957

6058
@Override
@@ -64,6 +62,16 @@ public boolean isAdoptable() {
6462

6563
@Override
6664
public void release(boolean wasAcquired) {
65+
release(getContext(), wasAcquired);
66+
}
67+
68+
@Override
69+
public boolean acquire() {
70+
return acquire(getContext());
71+
}
72+
73+
@Override
74+
public void release(PythonContext context, boolean wasAcquired) {
6775
// n.b.: we cannot make any optimizations here based on the singleThreadedAssumption of
6876
// the language. You would think that you could use that assumption to get rid even of
6977
// the ownsGil check, but we need to actually release the GIL around blocking operations
@@ -74,21 +82,20 @@ public void release(boolean wasAcquired) {
7482
// into the next (e.g. regular) GIL release. So we need to always have this ownsGil
7583
// check.
7684
if (binaryProfile.profile(wasAcquired)) {
77-
getContext().releaseGil();
85+
context.releaseGil();
7886
}
7987
}
8088

8189
@Override
82-
public boolean acquire() {
83-
PythonContext context = getContext();
90+
public boolean acquire(PythonContext context) {
8491
if (binaryProfile.profile(!context.ownsGil())) {
8592
context.acquireGil();
8693
return true;
8794
}
8895
return false;
8996
}
9097

91-
private final PythonContext getContext() {
98+
private PythonContext getContext() {
9299
if (contextRef == null) {
93100
CompilerDirectives.transferToInterpreterAndInvalidate();
94101
contextRef = lookupContextReference(PythonLanguage.class);
@@ -106,19 +113,30 @@ public boolean isAdoptable() {
106113
@Override
107114
@TruffleBoundary
108115
public final boolean acquire() {
109-
PythonContext context = PythonLanguage.getContext();
116+
return acquire(PythonLanguage.getContext());
117+
}
118+
119+
@Override
120+
@TruffleBoundary
121+
public final void release(boolean wasAcquired) {
122+
release(PythonLanguage.getContext(), wasAcquired);
123+
}
124+
125+
@Override
126+
@TruffleBoundary
127+
public final boolean acquire(PythonContext context) {
110128
if (!context.ownsGil()) {
111-
PythonLanguage.getContext().acquireGil();
129+
context.acquireGil();
112130
return true;
113131
}
114132
return false;
115133
}
116134

117135
@Override
118136
@TruffleBoundary
119-
public final void release(boolean wasAcquired) {
137+
public final void release(PythonContext context, boolean wasAcquired) {
120138
if (wasAcquired) {
121-
PythonLanguage.getContext().releaseGil();
139+
context.releaseGil();
122140
}
123141
}
124142

@@ -161,13 +179,23 @@ public final void close() {
161179
*/
162180
public abstract boolean acquire();
163181

182+
/**
183+
* @see #acquire()
184+
*/
185+
public abstract boolean acquire(PythonContext context);
186+
164187
/**
165188
* Release the GIL if {@code wasAcquired} is {@code true}.
166189
*
167190
* @param wasAcquired - the return value of the preceding {@link #acquire} call.
168191
*/
169192
public abstract void release(boolean wasAcquired);
170193

194+
/**
195+
* @see #release(boolean)
196+
*/
197+
public abstract void release(PythonContext context, boolean wasAcquired);
198+
171199
public static GilNode create() {
172200
return new Cached();
173201
}

0 commit comments

Comments
 (0)