Skip to content

Commit fdf159d

Browse files
committed
opt GIL: PThreadState
1 parent bcaab54 commit fdf159d

File tree

1 file changed

+20
-57
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi

1 file changed

+20
-57
lines changed

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

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41+
// skip GIL
4142
package com.oracle.graal.python.builtins.objects.cext.capi;
4243

4344
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_THREAD_STATE_TYPE_ID;
@@ -57,15 +58,13 @@
5758
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5859
import com.oracle.graal.python.nodes.PNodeWithContext;
5960
import com.oracle.graal.python.nodes.object.GetClassNode;
60-
import com.oracle.graal.python.runtime.GilNode;
6161
import com.oracle.graal.python.runtime.PythonContext;
6262
import com.oracle.graal.python.runtime.PythonOptions;
6363
import com.oracle.graal.python.runtime.exception.PException;
6464
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6565
import com.oracle.truffle.api.Assumption;
6666
import com.oracle.truffle.api.CompilerDirectives;
6767
import com.oracle.truffle.api.Truffle;
68-
import com.oracle.truffle.api.dsl.Bind;
6968
import com.oracle.truffle.api.dsl.Cached;
7069
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7170
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -141,13 +140,8 @@ protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal,
141140

142141
@ExportMessage
143142
protected Object readMember(String member,
144-
@Exclusive @Cached ThreadStateReadNode readNode, @Exclusive @Cached GilNode gil) {
145-
boolean mustRelease = gil.acquire();
146-
try {
147-
return readNode.execute(member);
148-
} finally {
149-
gil.release(mustRelease);
150-
}
143+
@Exclusive @Cached ThreadStateReadNode readNode) {
144+
return readNode.execute(member);
151145
}
152146

153147
@ImportStatic(PThreadState.class)
@@ -304,13 +298,8 @@ protected boolean isMemberInsertable(@SuppressWarnings("unused") String member)
304298
@ExportMessage
305299
protected void writeMember(String member, Object value,
306300
@Exclusive @Cached ThreadStateWriteNode writeNode,
307-
@Exclusive @Cached ToJavaNode toJavaNode, @Exclusive @Cached GilNode gil) throws UnknownIdentifierException {
308-
boolean mustRelease = gil.acquire();
309-
try {
310-
writeNode.execute(member, toJavaNode.execute(value));
311-
} finally {
312-
gil.release(mustRelease);
313-
}
301+
@Exclusive @Cached ToJavaNode toJavaNode) throws UnknownIdentifierException {
302+
writeNode.execute(member, toJavaNode.execute(value));
314303
}
315304

316305
@ExportMessage
@@ -441,44 +430,29 @@ protected static boolean isCaughtExceptionMember(Object key) {
441430
// TO POINTER / AS POINTER / TO NATIVE
442431
@ExportMessage
443432
protected boolean isPointer(
444-
@Exclusive @Cached IsPointerNode pIsPointerNode, @Exclusive @Cached GilNode gil) {
445-
boolean mustRelease = gil.acquire();
446-
try {
447-
return pIsPointerNode.execute(this);
448-
} finally {
449-
gil.release(mustRelease);
450-
}
433+
@Exclusive @Cached IsPointerNode pIsPointerNode) {
434+
return pIsPointerNode.execute(this);
451435
}
452436

453437
@ExportMessage
454438
public long asPointer(
455439
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
456-
@CachedLibrary(limit = "1") InteropLibrary interopLibrary, @Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
457-
boolean mustRelease = gil.acquire();
458-
try {
459-
Object nativePointer = lib.getNativePointer(this);
460-
if (nativePointer instanceof Long) {
461-
return (long) nativePointer;
462-
}
463-
return interopLibrary.asPointer(nativePointer);
464-
} finally {
465-
gil.release(mustRelease);
440+
@CachedLibrary(limit = "1") InteropLibrary interopLibrary) throws UnsupportedMessageException {
441+
Object nativePointer = lib.getNativePointer(this);
442+
if (nativePointer instanceof Long) {
443+
return (long) nativePointer;
466444
}
445+
return interopLibrary.asPointer(nativePointer);
467446
}
468447

469448
@ExportMessage
470449
protected void toNative(
471450
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
472451
@Exclusive @Cached ToPyObjectNode toPyObjectNode,
473-
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode, @Exclusive @Cached GilNode gil) {
474-
boolean mustRelease = gil.acquire();
475-
try {
476-
invalidateNode.execute();
477-
if (!lib.isNative(this)) {
478-
setNativePointer(toPyObjectNode.execute(this));
479-
}
480-
} finally {
481-
gil.release(mustRelease);
452+
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
453+
invalidateNode.execute();
454+
if (!lib.isNative(this)) {
455+
setNativePointer(toPyObjectNode.execute(this));
482456
}
483457
}
484458

@@ -491,26 +465,15 @@ protected boolean hasNativeType() {
491465
abstract static class GetTypeIDNode {
492466
@Specialization(assumptions = "singleNativeContextAssumption()")
493467
static Object doByteArray(@SuppressWarnings("unused") PThreadState receiver,
494-
@Exclusive @Cached GilNode gil,
495-
@Bind("gil.acquire()") boolean mustRelease,
496468
@Exclusive @Cached("callGetThreadStateTypeIDUncached()") Object nativeType) {
497-
try {
498-
// TODO(fa): use weak reference ?
499-
return nativeType;
500-
} finally {
501-
gil.release(mustRelease);
502-
}
469+
// TODO(fa): use weak reference ?
470+
return nativeType;
503471
}
504472

505473
@Specialization(replaces = "doByteArray")
506474
static Object doByteArrayMultiCtx(@SuppressWarnings("unused") PThreadState receiver,
507-
@Exclusive @Cached PCallCapiFunction callUnaryNode, @Exclusive @Cached GilNode gil) {
508-
boolean mustRelease = gil.acquire();
509-
try {
510-
return callUnaryNode.call(FUN_GET_THREAD_STATE_TYPE_ID);
511-
} finally {
512-
gil.release(mustRelease);
513-
}
475+
@Exclusive @Cached PCallCapiFunction callUnaryNode) {
476+
return callUnaryNode.call(FUN_GET_THREAD_STATE_TYPE_ID);
514477
}
515478

516479
protected static Object callGetThreadStateTypeIDUncached() {

0 commit comments

Comments
 (0)