Skip to content

Commit c4be74a

Browse files
committed
[GR-28394] Make test_multiprocessing_spawn pass.
PullRequest: graalpython/1921
2 parents 8968d8c + 8f04c31 commit c4be74a

File tree

15 files changed

+894
-290
lines changed

15 files changed

+894
-290
lines changed

graalpython/com.oracle.graal.python.test/src/tests/run_cpython_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def prepare_test_decimal(self, module):
6565
def loadTestsFromModule(self, module, pattern=None):
6666
if module.__name__.endswith('test_decimal'):
6767
return self.prepare_test_decimal(module)
68+
if module.__name__.endswith('test_multiprocessing_spawn'):
69+
sys.path.insert(1, os.path.dirname(module.__name__.replace(".", "/")))
70+
import _test_multiprocessing
71+
sys.modules['__main__'] = _test_multiprocessing
6872
suite = super().loadTestsFromModule(module, pattern=pattern)
6973
test_main = getattr(module, 'test_main', None)
7074
if callable(test_main):

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_multiprocessing_spawn.txt

Lines changed: 258 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 115 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,11 +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.List;
105103
import java.util.Map;
106-
import java.util.SortedMap;
107-
import java.util.TreeMap;
108-
import java.util.concurrent.LinkedBlockingQueue;
109104

110105
@TruffleLanguage.Registration(id = PythonLanguage.ID, //
111106
name = PythonLanguage.NAME, //
@@ -227,8 +222,6 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
227222

228223
private final Map<Long, ChildContextData> childContextData = new ConcurrentHashMap<>();
229224

230-
private final SharedMultiprocessingData sharedMPData = new SharedMultiprocessingData();
231-
232225
@TruffleBoundary
233226
public Thread getChildContextThread(long tid) {
234227
return childContextThreads.get(tid);
@@ -254,19 +247,10 @@ public void putChildContextData(long id, ChildContextData data) {
254247
childContextData.put(id, data);
255248
}
256249

257-
@TruffleBoundary
258-
public void removeChildContextData(long id) {
259-
childContextData.remove(id);
260-
}
261-
262250
public static PythonLanguage get(Node node) {
263251
return REFERENCE.get(node);
264252
}
265253

266-
public synchronized SharedMultiprocessingData getSharedMultiprocessingData() {
267-
return sharedMPData;
268-
}
269-
270254
public static int getNumberOfSpecialSingletons() {
271255
return CONTEXT_INSENSITIVE_SINGLETONS.length;
272256
}
@@ -895,103 +879,4 @@ public RootCallTarget getDescriptorCallTarget(BuiltinMethodDescriptor descriptor
895879
return callTarget;
896880
}
897881

898-
public static class SharedMultiprocessingData {
899-
900-
private final SortedMap<Integer, LinkedBlockingQueue<Object>> sharedContextData = new TreeMap<>();
901-
902-
/**
903-
* @return fake (negative) fd values to avoid clash with real file descriptors and to detect
904-
* potential usage by other python builtins
905-
*/
906-
@TruffleBoundary
907-
public int[] pipe() {
908-
synchronized (sharedContextData) {
909-
LinkedBlockingQueue<Object> q = new LinkedBlockingQueue<>();
910-
int readFD = nextFreeFd();
911-
sharedContextData.put(readFD, q);
912-
int writeFD = nextFreeFd();
913-
sharedContextData.put(writeFD, q);
914-
return new int[]{readFD, writeFD};
915-
}
916-
}
917-
918-
/**
919-
* Must be called in a synchronized (sharedContextData) block which also writes the new fd
920-
* into the sharedContextData map.
921-
*/
922-
@TruffleBoundary
923-
private int nextFreeFd() {
924-
if (sharedContextData.isEmpty()) {
925-
return -1;
926-
}
927-
return sharedContextData.firstKey() - 1;
928-
}
929-
930-
@TruffleBoundary
931-
public boolean hasFD(int fd) {
932-
synchronized (sharedContextData) {
933-
return sharedContextData.containsKey(fd);
934-
}
935-
}
936-
937-
@TruffleBoundary
938-
public boolean addSharedContextData(int fd, byte[] bytes, Runnable noFDHandler) {
939-
LinkedBlockingQueue<Object> q = getQueue(fd);
940-
if (q == null) {
941-
noFDHandler.run();
942-
return false;
943-
}
944-
q.add(bytes);
945-
return true;
946-
}
947-
948-
@TruffleBoundary
949-
public void makeReadable(int fd, Runnable noFDHandler) {
950-
LinkedBlockingQueue<Object> q = getQueue(fd);
951-
if (q == null) {
952-
noFDHandler.run();
953-
return;
954-
}
955-
q.add(PythonUtils.EMPTY_BYTE_ARRAY);
956-
}
957-
958-
@TruffleBoundary
959-
public Object takeSharedContextData(Node node, int fd, Runnable noFDHandler) {
960-
LinkedBlockingQueue<Object> q = getQueue(fd);
961-
if (q == null) {
962-
noFDHandler.run();
963-
return null;
964-
}
965-
Object[] o = new Object[]{PNone.NONE};
966-
TruffleSafepoint.setBlockedThreadInterruptible(node, (lbq) -> {
967-
o[0] = lbq.take();
968-
}, q);
969-
return o[0];
970-
}
971-
972-
@TruffleBoundary
973-
public boolean isEmpty(int fd, Runnable noFDHandler) {
974-
LinkedBlockingQueue<Object> q = getQueue(fd);
975-
if (q == null) {
976-
noFDHandler.run();
977-
return false;
978-
}
979-
return q.isEmpty();
980-
}
981-
982-
@TruffleBoundary
983-
public void closeFDs(List<Integer> fds) {
984-
synchronized (sharedContextData) {
985-
for (Integer fd : fds) {
986-
sharedContextData.remove(fd);
987-
}
988-
}
989-
}
990-
991-
private LinkedBlockingQueue<Object> getQueue(int fd) {
992-
synchronized (sharedContextData) {
993-
return sharedContextData.get(fd);
994-
}
995-
}
996-
}
997882
}

0 commit comments

Comments
 (0)