src/java.base/share/classes/java/net/SocketCleanable.java
author rriggs
Fri, 02 Feb 2018 14:17:07 -0500
changeset 48737 7c12219870fd
child 48941 4f11514fe783
permissions -rw-r--r--
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner Reviewed-by: chegar, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     1
/*
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     4
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    10
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    15
 * accompanied this code).
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    16
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    20
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    23
 * questions.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    24
 */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    25
package java.net;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    26
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    27
import jdk.internal.misc.JavaIOFileDescriptorAccess;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    28
import jdk.internal.misc.SharedSecrets;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    29
import jdk.internal.ref.CleanerFactory;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    30
import jdk.internal.ref.PhantomCleanable;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    31
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    32
import java.io.FileDescriptor;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    33
import java.io.IOException;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    34
import java.io.UncheckedIOException;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    35
import java.lang.ref.Cleaner;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    36
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    37
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    38
/**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    39
 * Cleanup for a socket/datagramsocket FileDescriptor when it becomes phantom reachable.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    40
 * Create a cleanup if the raw fd != -1. Windows closes sockets using the fd.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    41
 * Subclassed from {@code PhantomCleanable} so that {@code clear} can be
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    42
 * called to disable the cleanup when the socket fd is closed by any means
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    43
 * other than calling {@link FileDescriptor#close}.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    44
 * Otherwise, it would incorrectly close the handle or fd after it has been reused.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    45
 */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    46
final class SocketCleanable extends PhantomCleanable<Object> {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    47
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    48
    // Access to FileDescriptor internals
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    49
    private static final JavaIOFileDescriptorAccess fdAccess =
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    50
            SharedSecrets.getJavaIOFileDescriptorAccess();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    51
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    52
    // Native function to call NET_SocketClose(fd)
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    53
    private static native void cleanupClose0(int fd) throws IOException;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    54
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    55
    // The raw fd to close
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    56
    private final int fd;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    57
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    58
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    59
     * Register a socket specific Cleanable with the FileDescriptor
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    60
     * if the FileDescriptor is non-null and the raw fd is != -1.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    61
     *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    62
     * @param fdo the FileDescriptor; may be null
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    63
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    64
    static void register(FileDescriptor fdo) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    65
        if (fdo != null) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    66
            int fd = fdAccess.get(fdo);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    67
            if (fd != -1) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    68
                fdAccess.registerCleanup(fdo,
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    69
                        new SocketCleanable(fdo, CleanerFactory.cleaner(), fd));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    70
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    71
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    72
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    73
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    74
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    75
     * Unregister a Cleanable from the FileDescriptor.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    76
     * @param fdo the FileDescriptor; may be null
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    77
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    78
    static void unregister(FileDescriptor fdo) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    79
        if (fdo != null) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    80
            fdAccess.unregisterCleanup(fdo);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    81
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    82
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    83
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    84
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    85
     * Constructor for a phantom cleanable reference.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    86
     *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    87
     * @param obj     the object to monitor
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    88
     * @param cleaner the cleaner
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    89
     * @param fd      file descriptor to close
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    90
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    91
    private SocketCleanable(Object obj, Cleaner cleaner, int fd) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    92
        super(obj, cleaner);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    93
        this.fd = fd;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    94
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    95
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    96
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    97
     * Close the native handle or fd.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    98
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    99
    @Override
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   100
    protected void performCleanup() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   101
        try {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   102
            cleanupClose0(fd);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   103
        } catch (IOException ioe) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   104
            throw new UncheckedIOException("close", ioe);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   105
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   106
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   107
}