Skip to content

Commit

Permalink
8233002: Further enhance datagram socket support
Browse files Browse the repository at this point in the history
Reviewed-by: alanb, chegar, dfuchs
  • Loading branch information
Ivan Gerasimov committed Oct 29, 2019
1 parent c5f884c commit 34e36a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected synchronized void create() throws SocketException {
fd = new FileDescriptor();
try {
datagramSocketCreate();
SocketCleanable.register(fd);
SocketCleanable.register(fd, false);
} catch (SocketException ioe) {
ResourceManager.afterUdpClose();
fd = null;
Expand Down
17 changes: 10 additions & 7 deletions src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected synchronized void create(boolean stream) throws IOException {
fd = new FileDescriptor();
try {
socketCreate(false);
SocketCleanable.register(fd);
SocketCleanable.register(fd, false);
} catch (IOException ioe) {
ResourceManager.afterUdpClose();
fd = null;
Expand All @@ -136,7 +136,7 @@ protected synchronized void create(boolean stream) throws IOException {
} else {
fd = new FileDescriptor();
socketCreate(true);
SocketCleanable.register(fd);
SocketCleanable.register(fd, true);
}
}

Expand Down Expand Up @@ -580,7 +580,7 @@ protected void accept(SocketImpl si) throws IOException {
} finally {
releaseFD();
}
SocketCleanable.register(si.fd);
SocketCleanable.register(si.fd, true);
}

/**
Expand Down Expand Up @@ -683,9 +683,6 @@ protected synchronized int available() throws IOException {
protected void close() throws IOException {
synchronized(fdLock) {
if (fd != null) {
if (!stream) {
ResourceManager.afterUdpClose();
}
if (fdUseCount == 0) {
if (closePending) {
return;
Expand Down Expand Up @@ -840,7 +837,13 @@ private void socketPreClose() throws IOException {
*/
protected void socketClose() throws IOException {
SocketCleanable.unregister(fd);
socketClose0(false);
try {
socketClose0(false);
} finally {
if (!stream) {
ResourceManager.afterUdpClose();
}
}
}

abstract void socketCreate(boolean stream) throws IOException;
Expand Down
23 changes: 18 additions & 5 deletions src/java.base/share/classes/java/net/SocketCleanable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,6 +34,7 @@
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner;

import sun.net.ResourceManager;

/**
* Cleanable for a socket/datagramsocket FileDescriptor when it becomes phantom reachable.
Expand All @@ -56,17 +57,22 @@ final class SocketCleanable extends PhantomCleanable<FileDescriptor> {
// The raw fd to close
private final int fd;

// true for socket, false for datagram socket
private final boolean stream;

/**
* Register a socket specific Cleanable with the FileDescriptor
* if the FileDescriptor is non-null and the raw fd is != -1.
*
* @param fdo the FileDescriptor; may be null
* @param fdo the FileDescriptor; may be null
* @param stream false for datagram socket
*/
static void register(FileDescriptor fdo) {
static void register(FileDescriptor fdo, boolean stream) {
if (fdo != null && fdo.valid()) {
int fd = fdAccess.get(fdo);
fdAccess.registerCleanup(fdo,
new SocketCleanable(fdo, CleanerFactory.cleaner(), fd));
new SocketCleanable(fdo, CleanerFactory.cleaner(),
fd, stream));
}
}

Expand All @@ -86,10 +92,13 @@ static void unregister(FileDescriptor fdo) {
* @param obj the object to monitor
* @param cleaner the cleaner
* @param fd file descriptor to close
* @param stream false for datagram socket
*/
private SocketCleanable(FileDescriptor obj, Cleaner cleaner, int fd) {
private SocketCleanable(FileDescriptor obj, Cleaner cleaner,
int fd, boolean stream) {
super(obj, cleaner);
this.fd = fd;
this.stream = stream;
}

/**
Expand All @@ -101,6 +110,10 @@ protected void performCleanup() {
cleanupClose0(fd);
} catch (IOException ioe) {
throw new UncheckedIOException("close", ioe);
} finally {
if (!stream) {
ResourceManager.afterUdpClose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ protected synchronized void create() throws SocketException {
fd1 = new FileDescriptor();
try {
super.create();
SocketCleanable.register(fd1);
// make SocketCleanable treat fd1 as a stream socket
// to avoid touching the counter in ResourceManager
SocketCleanable.register(fd1, true);
} catch (SocketException e) {
fd1 = null;
throw e;
Expand All @@ -114,8 +116,10 @@ protected synchronized void bind0(int lport, InetAddress laddr)

bind0(lport, laddr, exclusiveBind);

SocketCleanable.register(fd);
SocketCleanable.register(fd1);
SocketCleanable.register(fd, false);
// make SocketCleanable treat fd1 as a stream socket
// to avoid touching the counter in ResourceManager
SocketCleanable.register(fd1, true);
}

protected synchronized void receive(DatagramPacket p)
Expand Down

0 comments on commit 34e36a8

Please sign in to comment.