Skip to content

Commit 0da50c3

Browse files
committed
Release GIL for isatty, lseek, fstat, openat, ftruncate
1 parent 6bf5b0e commit 0da50c3

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,17 @@ protected ArgumentClinicProvider getArgumentClinic() {
822822
PNone ftruncate(VirtualFrame frame, int fd, long length,
823823
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
824824
@Cached SysModuleBuiltins.AuditNode auditNode,
825+
@Cached GilNode gil,
825826
@Cached BranchProfile errorProfile) {
826827
auditNode.audit("os.truncate", fd, length);
827828
while (true) {
828829
try {
829-
posixLib.ftruncate(getPosixSupport(), fd, length);
830+
gil.release(true);
831+
try {
832+
posixLib.ftruncate(getPosixSupport(), fd, length);
833+
} finally {
834+
gil.acquire();
835+
}
830836
return PNone.NONE;
831837
} catch (PosixException e) {
832838
errorProfile.enter();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedWriterBuiltins.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.graal.python.builtins.modules.io.IOBaseBuiltins.CheckWritableNode;
5252
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5353
import com.oracle.graal.python.nodes.object.GetClassNode;
54+
import com.oracle.graal.python.runtime.GilNode;
5455
import com.oracle.graal.python.runtime.PosixSupportLibrary;
5556
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5657
import com.oracle.truffle.api.dsl.Cached;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,20 @@ private static void errorCleanup(VirtualFrame frame, PFileIO self, boolean fdIsO
222222
private int open(VirtualFrame frame, String name, int flags, int mode,
223223
PythonContext ctxt,
224224
PosixSupportLibrary posixLib,
225+
GilNode gil,
225226
BranchProfile errorProfile) {
226227
Object path = posixLib.createPathFromString(ctxt.getPosixSupport(), name);
227228
if (path == null) {
228229
throw raise(ValueError, EMBEDDED_NULL_BYTE);
229230
}
230231
while (true) {
231232
try {
232-
return posixLib.openat(ctxt.getPosixSupport(), AT_FDCWD.value, path, flags, mode);
233+
gil.release(true);
234+
try {
235+
return posixLib.openat(ctxt.getPosixSupport(), AT_FDCWD.value, path, flags, mode);
236+
} finally {
237+
gil.acquire();
238+
}
233239
} catch (PosixException e) {
234240
errorProfile.enter();
235241
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
@@ -303,7 +309,8 @@ void doInit(VirtualFrame frame, PFileIO self, Object nameobj, IONodes.IOMode mod
303309
@Cached SetAttributeNode.Dynamic setAttr,
304310
@Cached SysModuleBuiltins.AuditNode auditNode,
305311
@Cached BranchProfile exceptionProfile,
306-
@Cached ConditionProfile errorProfile) {
312+
@Cached ConditionProfile errorProfile,
313+
@Cached GilNode gil) {
307314
if (self.getFD() >= 0) {
308315
if (self.isCloseFD()) {
309316
/* Have to close the existing file first. */
@@ -337,7 +344,7 @@ void doInit(VirtualFrame frame, PFileIO self, Object nameobj, IONodes.IOMode mod
337344
}
338345

339346
if (opener instanceof PNone) {
340-
self.setFD(open(frame, name, flags, 0666, ctxt, posixLib, exceptionProfile), ctxt);
347+
self.setFD(open(frame, name, flags, 0666, ctxt, posixLib, gil, exceptionProfile), ctxt);
341348
} else {
342349
Object fdobj = callOpener.execute(frame, opener, nameobj, flags);
343350
if (!indexCheckNode.execute(fdobj)) {
@@ -363,7 +370,13 @@ void doInit(VirtualFrame frame, PFileIO self, Object nameobj, IONodes.IOMode mod
363370
}
364371
self.setBlksize(DEFAULT_BUFFER_SIZE);
365372
try {
366-
long[] fstatResult = posixLib.fstat(ctxt.getPosixSupport(), self.getFD());
373+
long[] fstatResult;
374+
gil.release(true);
375+
try {
376+
fstatResult = posixLib.fstat(ctxt.getPosixSupport(), self.getFD());
377+
} finally {
378+
gil.acquire();
379+
}
367380
/*
368381
* On Unix, open will succeed for directories. In Python, there should be no
369382
* file objects referring to directories, so we need a check.
@@ -396,8 +409,13 @@ void doInit(VirtualFrame frame, PFileIO self, Object nameobj, IONodes.IOMode mod
396409
* it might be done only on the first write()).
397410
*/
398411
try {
399-
long res = posixLib.lseek(ctxt.getPosixSupport(), self.getFD(), 0, mapPythonSeekWhenceToPosix(SEEK_END));
400-
self.setSeekable(res >= 0 ? 1 : 0);
412+
gil.release(true);
413+
try {
414+
long res = posixLib.lseek(ctxt.getPosixSupport(), self.getFD(), 0, mapPythonSeekWhenceToPosix(SEEK_END));
415+
self.setSeekable(res >= 0 ? 1 : 0);
416+
} finally {
417+
gil.acquire();
418+
}
401419
} catch (PosixException e) {
402420
exceptionProfile.enter();
403421
if (self.getSeekable() < 0) {
@@ -744,9 +762,15 @@ protected ArgumentClinicProvider getArgumentClinic() {
744762
@Specialization(guards = "!self.isClosed()")
745763
Object seek(VirtualFrame frame, PFileIO self, long pos, int whence,
746764
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
765+
@Cached GilNode gil,
747766
@Cached BranchProfile exceptionProfile) {
748767
try {
749-
return internalSeek(self, pos, whence, getPosixSupport(), posixLib);
768+
gil.release(true);
769+
try {
770+
return internalSeek(self, pos, whence, getPosixSupport(), posixLib);
771+
} finally {
772+
gil.acquire();
773+
}
750774
} catch (PosixException e) {
751775
exceptionProfile.enter();
752776
throw raiseOSErrorFromPosixException(frame, e);
@@ -985,8 +1009,14 @@ Object closedError(@SuppressWarnings("unused") PFileIO self) {
9851009
abstract static class IsattyNode extends PythonUnaryBuiltinNode {
9861010
@Specialization(guards = "!self.isClosed()")
9871011
boolean isatty(@SuppressWarnings("unused") PFileIO self,
988-
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
989-
return posixLib.isatty(getPosixSupport(), self.getFD());
1012+
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
1013+
@Cached GilNode gil) {
1014+
gil.release(true);
1015+
try {
1016+
return posixLib.isatty(getPosixSupport(), self.getFD());
1017+
} finally {
1018+
gil.acquire();
1019+
}
9901020
}
9911021

9921022
@Specialization(guards = "self.isClosed()")

0 commit comments

Comments
 (0)