Skip to content

Commit 9a29762

Browse files
committed
GR-30323: remove gil for PythonObjectLibrary messages
1 parent 8b69289 commit 9a29762

26 files changed

+438
-883
lines changed

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/objects/PythonAbstractObject.java

Lines changed: 202 additions & 307 deletions
Large diffs are not rendered by default.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/PArray.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@
3333
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3434
import com.oracle.graal.python.nodes.ErrorMessages;
3535
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
36-
import com.oracle.graal.python.runtime.GilNode;
3736
import com.oracle.graal.python.util.BufferFormat;
3837
import com.oracle.graal.python.util.OverflowException;
3938
import com.oracle.graal.python.util.PythonUtils;
40-
import com.oracle.truffle.api.dsl.Cached;
41-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4239
import com.oracle.truffle.api.library.ExportLibrary;
4340
import com.oracle.truffle.api.library.ExportMessage;
4441
import com.oracle.truffle.api.nodes.ExplodeLoop;
@@ -174,13 +171,8 @@ static boolean isBuffer(@SuppressWarnings("unused") PArray self) {
174171
}
175172

176173
@ExportMessage
177-
byte[] getBufferBytes(@Exclusive @Cached GilNode gil) {
178-
boolean mustRelease = gil.acquire();
179-
try {
180-
return PythonUtils.arrayCopyOf(buffer, getBufferLength());
181-
} finally {
182-
gil.release(mustRelease);
183-
}
174+
byte[] getBufferBytes() {
175+
return PythonUtils.arrayCopyOf(buffer, getBufferLength());
184176
}
185177

186178
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PBytes.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
3131
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
3232
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
33-
import com.oracle.graal.python.runtime.GilNode;
3433
import com.oracle.graal.python.runtime.sequence.PSequence;
3534
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
3635
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3736
import com.oracle.truffle.api.CompilerAsserts;
3837
import com.oracle.truffle.api.dsl.Cached;
39-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4038
import com.oracle.truffle.api.interop.InteropLibrary;
4139
import com.oracle.truffle.api.library.ExportLibrary;
4240
import com.oracle.truffle.api.library.ExportMessage;
@@ -100,13 +98,8 @@ public final int hashCode() {
10098

10199
@ExportMessage
102100
public String asPathWithState(@SuppressWarnings("unused") ThreadState state,
103-
@Cached SequenceStorageNodes.ToByteArrayNode toBytes, @Exclusive @Cached GilNode gil) {
104-
boolean mustRelease = gil.acquire();
105-
try {
106-
return BytesUtils.createASCIIString(toBytes.execute(getSequenceStorage()));
107-
} finally {
108-
gil.release(mustRelease);
109-
}
101+
@Cached SequenceStorageNodes.ToByteArrayNode toBytes) {
102+
return BytesUtils.createASCIIString(toBytes.execute(getSequenceStorage()));
110103
}
111104

112105
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PBytesLike.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@
4242

4343
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4444
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
45-
import com.oracle.graal.python.runtime.GilNode;
4645
import com.oracle.graal.python.runtime.sequence.PSequence;
4746
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
4847
import com.oracle.graal.python.runtime.sequence.storage.NativeSequenceStorage;
4948
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
5049
import com.oracle.truffle.api.dsl.Cached;
51-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5250
import com.oracle.truffle.api.library.ExportLibrary;
5351
import com.oracle.truffle.api.library.ExportMessage;
5452
import com.oracle.truffle.api.object.Shape;
@@ -90,23 +88,13 @@ protected boolean isBuffer() {
9088

9189
@ExportMessage
9290
int getBufferLength(
93-
@Cached SequenceStorageNodes.LenNode lenNode, @Exclusive @Cached GilNode gil) {
94-
boolean mustRelease = gil.acquire();
95-
try {
96-
return lenNode.execute(store);
97-
} finally {
98-
gil.release(mustRelease);
99-
}
91+
@Cached SequenceStorageNodes.LenNode lenNode) {
92+
return lenNode.execute(store);
10093
}
10194

10295
@ExportMessage
10396
byte[] getBufferBytes(
104-
@Cached SequenceStorageNodes.ToByteArrayNode toByteArrayNode, @Exclusive @Cached GilNode gil) {
105-
boolean mustRelease = gil.acquire();
106-
try {
107-
return toByteArrayNode.execute(store);
108-
} finally {
109-
gil.release(mustRelease);
110-
}
97+
@Cached SequenceStorageNodes.ToByteArrayNode toByteArrayNode) {
98+
return toByteArrayNode.execute(store);
11199
}
112100
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/PCell.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@
4646
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4747
import com.oracle.graal.python.nodes.ErrorMessages;
4848
import com.oracle.graal.python.nodes.PRaiseNode;
49-
import com.oracle.graal.python.runtime.GilNode;
5049
import com.oracle.truffle.api.Assumption;
5150
import com.oracle.truffle.api.CompilerAsserts;
5251
import com.oracle.truffle.api.CompilerDirectives;
5352
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5453
import com.oracle.truffle.api.dsl.Cached;
55-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5654
import com.oracle.truffle.api.library.ExportLibrary;
5755
import com.oracle.truffle.api.library.ExportMessage;
5856

@@ -130,12 +128,7 @@ public Object getLazyPythonClass() {
130128

131129
@ExportMessage
132130
Object getIteratorWithState(@SuppressWarnings("unused") ThreadState state,
133-
@Cached PRaiseNode raiseNode, @Exclusive @Cached GilNode gil) {
134-
boolean mustRelease = gil.acquire();
135-
try {
136-
throw raiseNode.raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, this);
137-
} finally {
138-
gil.release(mustRelease);
139-
}
131+
@Cached PRaiseNode raiseNode) {
132+
throw raiseNode.raise(PythonBuiltinClassType.TypeError, ErrorMessages.OBJ_NOT_ITERABLE, this);
140133
}
141134
}

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

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -189,25 +189,20 @@ public Object asIndexWithState(ThreadState threadState,
189189
@Exclusive @Cached ConditionProfile resultProfile,
190190
@Exclusive @Cached ConditionProfile gotState,
191191
@Cached IsBuiltinClassProfile isInt,
192-
@Cached WarnNode warnNode, @Exclusive @Cached GilNode gil) {
193-
boolean mustRelease = gil.acquire();
194-
try {
195-
if (isSubtypeNode.execute(plib.getLazyPythonClass(this), PythonBuiltinClassType.PInt)) {
196-
if (!isInt.profileObject(this, PythonBuiltinClassType.PInt)) {
197-
VirtualFrame frame = null;
198-
if (gotState.profile(threadState != null)) {
199-
frame = PArguments.frameForCall(threadState);
200-
}
201-
warnNode.warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
202-
ErrorMessages.P_RETURNED_NON_P,
203-
this, "__index__", "int", this, "int");
192+
@Cached WarnNode warnNode) {
193+
if (isSubtypeNode.execute(plib.getLazyPythonClass(this), PythonBuiltinClassType.PInt)) {
194+
if (!isInt.profileObject(this, PythonBuiltinClassType.PInt)) {
195+
VirtualFrame frame = null;
196+
if (gotState.profile(threadState != null)) {
197+
frame = PArguments.frameForCall(threadState);
204198
}
205-
return this; // subclasses of 'int' should do early return
206-
} else {
207-
return asIndexWithState(threadState, plib, methodLib, resultLib, raise, isSubtypeNode, noIndex, resultProfile, gotState, isInt, warnNode, gil);
199+
warnNode.warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
200+
ErrorMessages.P_RETURNED_NON_P,
201+
this, "__index__", "int", this, "int");
208202
}
209-
} finally {
210-
gil.release(mustRelease);
203+
return this; // subclasses of 'int' should do early return
204+
} else {
205+
return asIndexWithState(threadState, plib, methodLib, resultLib, raise, isSubtypeNode, noIndex, resultProfile, gotState, isInt, warnNode);
211206
}
212207
}
213208

@@ -216,19 +211,14 @@ public PDict getDict(
216211
@Exclusive @Cached PRaiseNode raiseNode,
217212
@Exclusive @Cached ToSulongNode toSulong,
218213
@Exclusive @Cached ToJavaNode toJava,
219-
@Exclusive @Cached PCallCapiFunction callGetDictNode, @Exclusive @Cached GilNode gil) {
220-
boolean mustRelease = gil.acquire();
221-
try {
222-
Object javaDict = toJava.execute(callGetDictNode.call(FUN_PY_OBJECT_GENERIC_GET_DICT, toSulong.execute(this)));
223-
if (javaDict instanceof PDict) {
224-
return (PDict) javaDict;
225-
} else if (javaDict == PNone.NO_VALUE) {
226-
return null;
227-
} else {
228-
throw raiseNode.raise(PythonBuiltinClassType.TypeError, ErrorMessages.DICT_MUST_BE_SET_TO_DICT, javaDict);
229-
}
230-
} finally {
231-
gil.release(mustRelease);
214+
@Exclusive @Cached PCallCapiFunction callGetDictNode) {
215+
Object javaDict = toJava.execute(callGetDictNode.call(FUN_PY_OBJECT_GENERIC_GET_DICT, toSulong.execute(this)));
216+
if (javaDict instanceof PDict) {
217+
return (PDict) javaDict;
218+
} else if (javaDict == PNone.NO_VALUE) {
219+
return null;
220+
} else {
221+
throw raiseNode.raise(PythonBuiltinClassType.TypeError, ErrorMessages.DICT_MUST_BE_SET_TO_DICT, javaDict);
232222
}
233223
}
234224

@@ -243,8 +233,7 @@ static Assumption getSingleContextAssumption() {
243233
@SuppressWarnings("unused")
244234
static Object getNativeClassCachedIdentity(PythonAbstractNativeObject object,
245235
@Exclusive @Cached(value = "object", weak = true) PythonAbstractNativeObject cachedObject,
246-
@Exclusive @Cached GilNode gil,
247-
@Exclusive @Cached("getNativeClassUncached(object, gil)") Object cachedClass) {
236+
@Exclusive @Cached("getNativeClassUncached(object)") Object cachedClass) {
248237
// TODO: (tfel) is this really something we can do? It's so rare for this class to
249238
// change that it shouldn't be worth the effort, but in native code, anything can
250239
// happen. OTOH, CPython also has caches that can become invalid when someone just
@@ -256,8 +245,7 @@ static Object getNativeClassCachedIdentity(PythonAbstractNativeObject object,
256245
@SuppressWarnings("unused")
257246
static Object getNativeClassCached(PythonAbstractNativeObject object,
258247
@Exclusive @Cached(value = "object", weak = true) PythonAbstractNativeObject cachedObject,
259-
@Exclusive @Cached GilNode gil,
260-
@Exclusive @Cached("getNativeClassUncached(object, gil)") Object cachedClass,
248+
@Exclusive @Cached("getNativeClassUncached(object)") Object cachedClass,
261249
@CachedLibrary(limit = "3") @SuppressWarnings("unused") InteropLibrary lib) {
262250
// TODO same as for 'getNativeClassCachedIdentity'
263251
return cachedClass;
@@ -270,15 +258,9 @@ static Object getNativeClassCached(PythonAbstractNativeObject object,
270258
static Object getNativeClassByMember(PythonAbstractNativeObject object,
271259
@CachedLibrary("object.getPtr()") InteropLibrary lib,
272260
@Exclusive @Cached ToJavaNode toJavaNode,
273-
@Exclusive @Cached ProfileClassNode classProfile, @Exclusive @Cached GilNode gil) throws UnknownIdentifierException, UnsupportedMessageException {
274-
boolean mustRelease = gil.acquire();
275-
try {
276-
// do not convert wrap 'object.object' since that is really the native pointer
277-
// object
278-
return classProfile.profile(toJavaNode.execute(lib.readMember(object.getPtr(), NativeMember.OB_TYPE.getMemberName())));
279-
} finally {
280-
gil.release(mustRelease);
281-
}
261+
@Exclusive @Cached ProfileClassNode classProfile) throws UnknownIdentifierException, UnsupportedMessageException {
262+
// do not convert wrap 'object.object' since that is really the native pointer object
263+
return classProfile.profile(toJavaNode.execute(lib.readMember(object.getPtr(), NativeMember.OB_TYPE.getMemberName())));
282264
}
283265

284266
@Specialization(guards = {"!lib.hasMembers(object.getPtr())"}, //
@@ -290,38 +272,27 @@ static Object getNativeClassByMemberAttachType(PythonAbstractNativeObject object
290272
@Exclusive @Cached PCallCapiFunction callGetObTypeNode,
291273
@Exclusive @Cached CExtNodes.GetLLVMType getLLVMType,
292274
@Exclusive @Cached ToJavaNode toJavaNode,
293-
@Exclusive @Cached ProfileClassNode classProfile, @Exclusive @Cached GilNode gil) throws UnknownIdentifierException, UnsupportedMessageException {
294-
boolean mustRelease = gil.acquire();
295-
try {
296-
Object typedPtr = callGetObTypeNode.call(NativeCAPISymbol.FUN_POLYGLOT_FROM_TYPED, object.getPtr(), getLLVMType.execute(CApiContext.LLVMType.PyObject));
297-
return classProfile.profile(toJavaNode.execute(lib.readMember(typedPtr, NativeMember.OB_TYPE.getMemberName())));
298-
} finally {
299-
gil.release(mustRelease);
300-
}
275+
@Exclusive @Cached ProfileClassNode classProfile) throws UnknownIdentifierException, UnsupportedMessageException {
276+
Object typedPtr = callGetObTypeNode.call(NativeCAPISymbol.FUN_POLYGLOT_FROM_TYPED, object.getPtr(), getLLVMType.execute(CApiContext.LLVMType.PyObject));
277+
return classProfile.profile(toJavaNode.execute(lib.readMember(typedPtr, NativeMember.OB_TYPE.getMemberName())));
301278
}
302279

303280
@Specialization(replaces = {"getNativeClassCached", "getNativeClassCachedIdentity", "getNativeClassByMember", "getNativeClassByMemberAttachType"})
304281
static Object getNativeClass(PythonAbstractNativeObject object,
305282
@Exclusive @Cached PCallCapiFunction callGetObTypeNode,
306283
@Exclusive @Cached AsPythonObjectNode toJavaNode,
307-
@Exclusive @Cached ProfileClassNode classProfile, @Exclusive @Cached GilNode gil) {
308-
boolean mustRelease = gil.acquire();
309-
try {
310-
// do not convert wrap 'object.object' since that is really the native pointer
311-
// object
312-
return classProfile.profile(toJavaNode.execute(callGetObTypeNode.call(FUN_GET_OB_TYPE, object.getPtr())));
313-
} finally {
314-
gil.release(mustRelease);
315-
}
284+
@Exclusive @Cached ProfileClassNode classProfile) {
285+
// do not convert wrap 'object.object' since that is really the native pointer object
286+
return classProfile.profile(toJavaNode.execute(callGetObTypeNode.call(FUN_GET_OB_TYPE, object.getPtr())));
316287
}
317288

318289
static boolean isSame(InteropLibrary lib, PythonAbstractNativeObject cachedObject, PythonAbstractNativeObject object) {
319290
return lib.isIdentical(cachedObject.object, object.object, lib);
320291
}
321292

322-
public static Object getNativeClassUncached(PythonAbstractNativeObject object, GilNode gil) {
293+
public static Object getNativeClassUncached(PythonAbstractNativeObject object) {
323294
// do not wrap 'object.object' since that is really the native pointer object
324-
return getNativeClass(object, PCallCapiFunction.getUncached(), AsPythonObjectNodeGen.getUncached(), ProfileClassNodeGen.getUncached(), gil);
295+
return getNativeClass(object, PCallCapiFunction.getUncached(), AsPythonObjectNodeGen.getUncached(), ProfileClassNodeGen.getUncached());
325296
}
326297

327298
}
@@ -336,7 +307,8 @@ boolean isIdentical(Object other, InteropLibrary otherInterop,
336307
@Cached("createClassProfile()") ValueProfile otherProfile,
337308
@CachedLibrary(limit = "1") InteropLibrary thisLib,
338309
@CachedLibrary("this.object") InteropLibrary objLib,
339-
@CachedLibrary(limit = "1") InteropLibrary otherObjLib, @Exclusive @Cached GilNode gil) {
310+
@CachedLibrary(limit = "1") InteropLibrary otherObjLib,
311+
@Exclusive @Cached GilNode gil) {
340312
boolean mustRelease = gil.acquire();
341313
try {
342314
Object profiled = otherProfile.profile(other);
@@ -368,7 +340,8 @@ static TriState doOther(PythonAbstractNativeObject receiver, Object other) {
368340
@ExportMessage(library = PythonObjectLibrary.class, name = "isLazyPythonClass")
369341
@ExportMessage(library = InteropLibrary.class)
370342
boolean isMetaObject(
371-
@Exclusive @Cached TypeNodes.IsTypeNode isType, @Exclusive @Cached GilNode gil) {
343+
@Exclusive @Cached TypeNodes.IsTypeNode isType,
344+
@Exclusive @Cached GilNode gil) {
372345
boolean mustRelease = gil.acquire();
373346
try {
374347
return isType.execute(this);
@@ -382,7 +355,8 @@ boolean isMetaInstance(Object instance,
382355
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
383356
@CachedLibrary(limit = "3") PythonObjectLibrary plib,
384357
@Cached PForeignToPTypeNode convert,
385-
@Cached IsSubtypeNode isSubtype, @Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
358+
@Cached IsSubtypeNode isSubtype,
359+
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
386360
boolean mustRelease = gil.acquire();
387361
try {
388362
if (!isType.execute(this)) {
@@ -398,7 +372,8 @@ boolean isMetaInstance(Object instance,
398372
String getMetaSimpleName(
399373
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
400374
@Shared("getTypeMember") @Cached GetTypeMemberNode getTpNameNode,
401-
@Shared("castToJavaStringNode") @Cached CastToJavaStringNode castToJavaStringNode, @Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
375+
@Shared("castToJavaStringNode") @Cached CastToJavaStringNode castToJavaStringNode,
376+
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
402377
return getSimpleName(getMetaQualifiedName(isType, getTpNameNode, castToJavaStringNode, gil));
403378
}
404379

@@ -415,7 +390,8 @@ private static String getSimpleName(String fqname) {
415390
String getMetaQualifiedName(
416391
@Shared("isType") @Cached TypeNodes.IsTypeNode isType,
417392
@Shared("getTypeMember") @Cached GetTypeMemberNode getTpNameNode,
418-
@Shared("castToJavaStringNode") @Cached CastToJavaStringNode castToJavaStringNode, @Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
393+
@Shared("castToJavaStringNode") @Cached CastToJavaStringNode castToJavaStringNode,
394+
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
419395
boolean mustRelease = gil.acquire();
420396
try {
421397
if (!isType.execute(this)) {

0 commit comments

Comments
 (0)