Skip to content

Commit 8bfffcd

Browse files
committed
moved sharedContextData and fdsToKeep from PythonLanguage to PythonContext
1 parent d514310 commit 8bfffcd

File tree

4 files changed

+179
-188
lines changed

4 files changed

+179
-188
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import com.oracle.truffle.api.TruffleFile;
8080
import com.oracle.truffle.api.TruffleLanguage;
8181
import com.oracle.truffle.api.TruffleLogger;
82-
import com.oracle.truffle.api.TruffleSafepoint;
8382
import com.oracle.truffle.api.debug.DebuggerTags;
8483
import com.oracle.truffle.api.frame.MaterializedFrame;
8584
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -101,12 +100,7 @@
101100
import com.oracle.truffle.api.object.Shape;
102101
import com.oracle.truffle.api.source.Source;
103102
import com.oracle.truffle.api.source.Source.SourceBuilder;
104-
import java.util.HashMap;
105-
import java.util.List;
106103
import java.util.Map;
107-
import java.util.SortedMap;
108-
import java.util.TreeMap;
109-
import java.util.concurrent.LinkedBlockingQueue;
110104

111105
@TruffleLanguage.Registration(id = PythonLanguage.ID, //
112106
name = PythonLanguage.NAME, //
@@ -228,10 +222,6 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
228222

229223
private final Map<Long, ChildContextData> childContextData = new ConcurrentHashMap<>();
230224

231-
private final SharedMultiprocessingData sharedMPData = new SharedMultiprocessingData();
232-
233-
private final Map<Integer, Integer> fdsToKeep = new HashMap<>();
234-
235225
@TruffleBoundary
236226
public Thread getChildContextThread(long tid) {
237227
return childContextThreads.get(tid);
@@ -261,47 +251,6 @@ public static PythonLanguage get(Node node) {
261251
return REFERENCE.get(node);
262252
}
263253

264-
public synchronized SharedMultiprocessingData getSharedMultiprocessingData() {
265-
return sharedMPData;
266-
}
267-
268-
@TruffleBoundary
269-
public boolean isFdToKeep(int fd) {
270-
synchronized (fdsToKeep) {
271-
return fdsToKeep.containsKey(fd);
272-
}
273-
}
274-
275-
@TruffleBoundary
276-
public void addFdToKeep(int fd) {
277-
synchronized (fdsToKeep) {
278-
Integer c = fdsToKeep.get(fd);
279-
if (c == null) {
280-
c = 1;
281-
} else {
282-
c = c + 1;
283-
}
284-
fdsToKeep.put(fd, c);
285-
}
286-
}
287-
288-
@TruffleBoundary
289-
public boolean removeFdToKeep(int fd) {
290-
synchronized (fdsToKeep) {
291-
Integer c = fdsToKeep.get(fd);
292-
if (c == null) {
293-
return false;
294-
}
295-
if (c == 1) {
296-
fdsToKeep.remove(fd);
297-
return true;
298-
} else {
299-
fdsToKeep.put(fd, c - 1);
300-
return false;
301-
}
302-
}
303-
}
304-
305254
public static int getNumberOfSpecialSingletons() {
306255
return CONTEXT_INSENSITIVE_SINGLETONS.length;
307256
}
@@ -930,108 +879,4 @@ public RootCallTarget getDescriptorCallTarget(BuiltinMethodDescriptor descriptor
930879
return callTarget;
931880
}
932881

933-
public static class SharedMultiprocessingData {
934-
935-
private int counter = 0;
936-
937-
private final SortedMap<Integer, LinkedBlockingQueue<Object>> sharedContextData = new TreeMap<>();
938-
939-
/**
940-
* @return fake (negative) fd values to avoid clash with real file descriptors and to detect
941-
* potential usage by other python builtins
942-
*/
943-
@TruffleBoundary
944-
public int[] pipe() {
945-
synchronized (sharedContextData) {
946-
LinkedBlockingQueue<Object> q = new LinkedBlockingQueue<>();
947-
int readFD = --counter;
948-
sharedContextData.put(readFD, q);
949-
int writeFD = --counter;
950-
sharedContextData.put(writeFD, q);
951-
return new int[]{readFD, writeFD};
952-
}
953-
}
954-
955-
@TruffleBoundary
956-
public boolean addSharedContextData(int fd, byte[] bytes, Runnable noFDHandler, Runnable brokenPipeHandler) {
957-
LinkedBlockingQueue<Object> q = null;
958-
synchronized (sharedContextData) {
959-
q = sharedContextData.get(fd);
960-
if (q == null) {
961-
noFDHandler.run();
962-
throw CompilerDirectives.shouldNotReachHere();
963-
}
964-
Integer fd2 = getPairFd(fd);
965-
if (isClosed(fd2)) {
966-
brokenPipeHandler.run();
967-
throw CompilerDirectives.shouldNotReachHere();
968-
}
969-
}
970-
q.add(bytes);
971-
return true;
972-
}
973-
974-
@TruffleBoundary
975-
public void closeFd(int fd) {
976-
synchronized (sharedContextData) {
977-
sharedContextData.remove(fd);
978-
}
979-
}
980-
981-
@TruffleBoundary
982-
public Object takeSharedContextData(Node node, int fd, Runnable noFDHandler) {
983-
LinkedBlockingQueue<Object> q;
984-
synchronized (sharedContextData) {
985-
q = sharedContextData.get(fd);
986-
if (q == null) {
987-
noFDHandler.run();
988-
throw CompilerDirectives.shouldNotReachHere();
989-
}
990-
Integer fd2 = getPairFd(fd);
991-
if (isClosed(fd2)) {
992-
if (q.isEmpty()) {
993-
return PythonUtils.EMPTY_BYTE_ARRAY;
994-
}
995-
}
996-
}
997-
Object[] o = new Object[]{PNone.NONE};
998-
TruffleSafepoint.setBlockedThreadInterruptible(node, (lbq) -> {
999-
o[0] = lbq.take();
1000-
}, q);
1001-
return o[0];
1002-
}
1003-
1004-
@TruffleBoundary
1005-
public boolean isBlocking(int fd) {
1006-
LinkedBlockingQueue<Object> q;
1007-
synchronized (sharedContextData) {
1008-
q = sharedContextData.get(fd);
1009-
if (q == null) {
1010-
return false;
1011-
}
1012-
Integer fd2 = getPairFd(fd);
1013-
if (isClosed(fd2)) {
1014-
return false;
1015-
}
1016-
}
1017-
return q.isEmpty();
1018-
}
1019-
1020-
@TruffleBoundary
1021-
public void closeFDs(List<Integer> fds) {
1022-
synchronized (sharedContextData) {
1023-
for (Integer fd : fds) {
1024-
sharedContextData.remove(fd);
1025-
}
1026-
}
1027-
}
1028-
1029-
private static int getPairFd(int fd) {
1030-
return fd % 2 == 0 ? fd + 1 : fd - 1;
1031-
}
1032-
1033-
private boolean isClosed(int fd) {
1034-
return sharedContextData.get(fd) == null && fd >= counter;
1035-
}
1036-
}
1037882
}

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.concurrent.Semaphore;
4747

4848
import com.oracle.graal.python.PythonLanguage;
49-
import com.oracle.graal.python.PythonLanguage.SharedMultiprocessingData;
5049
import com.oracle.graal.python.builtins.Builtin;
5150
import com.oracle.graal.python.builtins.CoreFunctions;
5251
import com.oracle.graal.python.builtins.Python3Core;
@@ -78,6 +77,7 @@
7877
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
7978
import com.oracle.graal.python.runtime.GilNode;
8079
import com.oracle.graal.python.runtime.PythonContext;
80+
import com.oracle.graal.python.runtime.PythonContext.SharedContextData;
8181
import com.oracle.graal.python.runtime.sequence.PSequence;
8282
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
8383
import com.oracle.graal.python.util.ArrayBuilder;
@@ -268,17 +268,17 @@ private static long convertTid(long tid) {
268268
abstract static class PipeNode extends PythonBuiltinNode {
269269

270270
@Specialization
271-
PTuple pipe(@Cached GilNode gil,
272-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData) {
271+
PTuple pipe(@Cached GilNode gil) {
273272
int[] pipe;
274273
PythonContext ctx = getContext();
274+
SharedContextData sharedData = ctx.getSharedContextData();
275275
gil.release(true);
276276
try {
277277
pipe = sharedData.pipe();
278278
ctx.getChildContextFDs().add(pipe[0]);
279279
ctx.getChildContextFDs().add(pipe[1]);
280-
getLanguage().addFdToKeep(pipe[0]);
281-
getLanguage().addFdToKeep(pipe[1]);
280+
sharedData.addFdToKeep(pipe[0]);
281+
sharedData.addFdToKeep(pipe[1]);
282282
} finally {
283283
gil.acquire();
284284
}
@@ -291,9 +291,9 @@ PTuple pipe(@Cached GilNode gil,
291291
public abstract static class WriteNode extends PythonBinaryBuiltinNode {
292292
@Specialization(limit = "1")
293293
Object doWrite(int fd, PBytes data,
294-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData,
295294
@CachedLibrary("data") PythonBufferAccessLibrary bufferLib,
296295
@Cached GilNode gil) {
296+
SharedContextData sharedData = getContext().getSharedContextData();
297297
gil.release(true);
298298
try {
299299
byte[] bytes = bufferLib.getCopiedByteArray(data);
@@ -312,10 +312,9 @@ Object doWrite(int fd, PBytes data,
312312

313313
@Specialization(limit = "1")
314314
Object doWrite(long fd, PBytes data,
315-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData,
316315
@CachedLibrary("data") PythonBufferAccessLibrary bufferLib,
317316
@Cached GilNode gil) {
318-
return doWrite((int) fd, data, sharedData, bufferLib, gil);
317+
return doWrite((int) fd, data, bufferLib, gil);
319318
}
320319
}
321320

@@ -324,8 +323,8 @@ Object doWrite(long fd, PBytes data,
324323
public abstract static class ReadNode extends PythonBinaryBuiltinNode {
325324
@Specialization
326325
Object doRead(int fd, @SuppressWarnings("unused") Object length,
327-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData,
328326
@Cached GilNode gil) {
327+
SharedContextData sharedData = getContext().getSharedContextData();
329328
gil.release(true);
330329
try {
331330
Object data = sharedData.takeSharedContextData(this, fd, () -> {
@@ -342,22 +341,20 @@ Object doRead(int fd, @SuppressWarnings("unused") Object length,
342341

343342
@Specialization
344343
Object doRead(long fd, Object length,
345-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData,
346344
@Cached GilNode gil) {
347-
return doRead((int) fd, length, sharedData, gil);
345+
return doRead((int) fd, length, gil);
348346
}
349347
}
350348

351349
@Builtin(name = "_close", minNumOfPositionalArgs = 1, parameterNames = {"fd"})
352350
@GenerateNodeFactory
353351
public abstract static class CloseNode extends PythonUnaryBuiltinNode {
354352
@Specialization
355-
PNone close(@SuppressWarnings("unused") int fd,
356-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData) {
353+
PNone close(@SuppressWarnings("unused") int fd) {
357354
assert fd < 0;
358-
PythonLanguage lang = getLanguage();
359-
if (lang.isFdToKeep(fd)) {
360-
if (lang.removeFdToKeep(fd)) {
355+
SharedContextData sharedData = getContext().getSharedContextData();
356+
if (sharedData.isFdToKeep(fd)) {
357+
if (sharedData.removeFdToKeep(fd)) {
361358
sharedData.closeFd(fd);
362359
}
363360
} else {
@@ -367,9 +364,8 @@ PNone close(@SuppressWarnings("unused") int fd,
367364
}
368365

369366
@Specialization
370-
PNone close(@SuppressWarnings("unused") long fd,
371-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData) {
372-
return close((int) fd, sharedData);
367+
PNone close(@SuppressWarnings("unused") long fd) {
368+
return close((int) fd);
373369
}
374370
}
375371

@@ -378,13 +374,13 @@ PNone close(@SuppressWarnings("unused") long fd,
378374
abstract static class SelectNode extends PythonBuiltinNode {
379375
@Specialization
380376
Object doGeneric(VirtualFrame frame, Object rlist,
381-
@Cached("getLanguage().getSharedMultiprocessingData()") SharedMultiprocessingData sharedData,
382377
@Cached PyObjectSizeNode sizeNode,
383378
@Cached("createGetItem()") LookupAndCallBinaryNode callGetItemNode,
384379
@Cached ListNodes.FastConstructListNode constructListNode,
385380
@Cached CastToJavaIntLossyNode castToJava,
386381
@Cached GilNode gil) {
387382
ArrayBuilder<Integer> notEmpty = new ArrayBuilder<>();
383+
SharedContextData sharedData = getContext().getSharedContextData();
388384
gil.release(true);
389385
try {
390386
PSequence pSequence = constructListNode.execute(frame, rlist);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,9 @@ protected ArgumentClinicProvider getArgumentClinic() {
525525
PNone close(VirtualFrame frame, int fd,
526526
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
527527
try {
528-
if (getLanguage().isFdToKeep(fd)) {
529-
getContext().closeLater(fd);
528+
PythonContext ctx = getContext();
529+
if (ctx.getSharedContextData().isFdToKeep(fd)) {
530+
ctx.closeLater(fd);
530531
return PNone.NONE;
531532
}
532533
posixLib.close(getPosixSupport(), fd);

0 commit comments

Comments
 (0)