8189330: Cleanup FileDescriptor implementation

Reviewed-by: bpb
This commit is contained in:
Roger Riggs 2018-02-23 14:26:29 -05:00
parent b2ed2bda60
commit cfe9ad4608
12 changed files with 245 additions and 516 deletions

View file

@ -36,20 +36,21 @@ import java.lang.ref.Cleaner;
/**
* Cleanup for a socket/datagramsocket FileDescriptor when it becomes phantom reachable.
* Cleanable for a socket/datagramsocket FileDescriptor when it becomes phantom reachable.
* Create a cleanup if the raw fd != -1. Windows closes sockets using the fd.
* Subclassed from {@code PhantomCleanable} so that {@code clear} can be
* called to disable the cleanup when the socket fd is closed by any means
* other than calling {@link FileDescriptor#close}.
* Otherwise, it would incorrectly close the handle or fd after it has been reused.
* Otherwise, it might incorrectly close the handle or fd after it has been reused.
*/
final class SocketCleanable extends PhantomCleanable<Object> {
final class SocketCleanable extends PhantomCleanable<FileDescriptor> {
// Access to FileDescriptor internals
// Access to FileDescriptor private fields
private static final JavaIOFileDescriptorAccess fdAccess =
SharedSecrets.getJavaIOFileDescriptorAccess();
// Native function to call NET_SocketClose(fd)
// Used only for last chance cleanup.
private static native void cleanupClose0(int fd) throws IOException;
// The raw fd to close
@ -62,12 +63,10 @@ final class SocketCleanable extends PhantomCleanable<Object> {
* @param fdo the FileDescriptor; may be null
*/
static void register(FileDescriptor fdo) {
if (fdo != null) {
if (fdo != null && fdo.valid()) {
int fd = fdAccess.get(fdo);
if (fd != -1) {
fdAccess.registerCleanup(fdo,
new SocketCleanable(fdo, CleanerFactory.cleaner(), fd));
}
fdAccess.registerCleanup(fdo,
new SocketCleanable(fdo, CleanerFactory.cleaner(), fd));
}
}
@ -88,7 +87,7 @@ final class SocketCleanable extends PhantomCleanable<Object> {
* @param cleaner the cleaner
* @param fd file descriptor to close
*/
private SocketCleanable(Object obj, Cleaner cleaner, int fd) {
private SocketCleanable(FileDescriptor obj, Cleaner cleaner, int fd) {
super(obj, cleaner);
this.fd = fd;
}