Skip to content

Commit f3840c2

Browse files
committed
unify socket fds and file fds
1 parent 8b1b77e commit f3840c2

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/PSocket.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.socket;
4242

43+
import java.io.IOException;
4344
import java.net.InetSocketAddress;
45+
import java.nio.channels.Channel;
4446
import java.nio.channels.ServerSocketChannel;
4547
import java.nio.channels.SocketChannel;
4648

4749
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4850
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
4951

50-
public class PSocket extends PythonBuiltinObject {
52+
public class PSocket extends PythonBuiltinObject implements Channel {
5153
public static final int AF_UNSPEC = 0;
5254
public static final int AF_INET = 2;
5355
public static final int AF_INET6 = 23;
@@ -76,9 +78,7 @@ public class PSocket extends PythonBuiltinObject {
7678

7779
public static final int IPPROTO_TCP = 6;
7880

79-
8081
private static final InetSocketAddress EPHEMERAL_ADDRESS = new InetSocketAddress(0);
81-
private static Integer nextFd = 0;
8282

8383
private final int family;
8484
private final int type;
@@ -174,4 +174,14 @@ public boolean isBlocking() {
174174
public void setBlocking(boolean blocking) {
175175
this.blocking = blocking;
176176
}
177+
178+
public boolean isOpen() {
179+
return getSocket() != null && getSocket().isOpen();
180+
}
181+
182+
public void close() throws IOException {
183+
if (getSocket() != null) {
184+
getSocket().close();
185+
}
186+
}
177187
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PosixResources.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public class PosixResources {
7777
/** Context-local file-descriptor mappings and PID mappings */
7878
private final SortedMap<Integer, ChannelWrapper> files;
7979
private final Map<Integer, String> filePaths;
80-
private final List<PSocket> sockets;
8180
private final List<Process> children;
8281
private final Map<String, Integer> inodes;
8382
private int inodeCnt = 0;
@@ -272,8 +271,9 @@ public String getFilePath(int fd) {
272271

273272
@TruffleBoundary
274273
public PSocket getSocket(int fd) {
275-
if (sockets.size() > fd) {
276-
return sockets.get(fd);
274+
ChannelWrapper channelWrapper = files.getOrDefault(fd, null);
275+
if (channelWrapper != null && channelWrapper.channel instanceof PSocket) {
276+
return (PSocket) channelWrapper.channel;
277277
}
278278
return null;
279279
}
@@ -286,23 +286,16 @@ public void close(int fd) {
286286
}
287287
}
288288

289-
@TruffleBoundary
290-
public void closeSocket(int fd) {
291-
if (sockets.size() > fd) {
292-
sockets.set(fd, null);
293-
}
294-
}
295-
296289
@TruffleBoundary
297290
public int openSocket(PSocket socket) {
298-
int fd = nextFreeSocketFd();
299-
sockets.set(fd, socket);
291+
int fd = nextFreeFd();
292+
addFD(fd, socket);
300293
return fd;
301294
}
302295

303296
@TruffleBoundary
304297
public void reopenSocket(PSocket socket, int fd) {
305-
sockets.set(fd, socket);
298+
addFD(fd, socket);
306299
}
307300

308301
@TruffleBoundary(allowInlining = true)
@@ -380,20 +373,6 @@ private int nextFreeFd() {
380373
}
381374
}
382375

383-
@TruffleBoundary(allowInlining = true)
384-
private int nextFreeSocketFd() {
385-
synchronized (sockets) {
386-
for (int i = 0; i < sockets.size(); i++) {
387-
PSocket socket = sockets.get(i);
388-
if (socket == null) {
389-
return i;
390-
}
391-
}
392-
sockets.add(null);
393-
return sockets.size() - 1;
394-
}
395-
}
396-
397376
@TruffleBoundary(allowInlining = true)
398377
public int registerChild(Process child) {
399378
int pid = nextFreePid();

0 commit comments

Comments
 (0)