src/java.base/share/classes/java/net/ServerSocket.java
branchniosocketimpl-branch
changeset 57344 8b621b0d921c
parent 57341 733e9746d615
child 57347 16c087c9103e
equal deleted inserted replaced
57342:5b512573ccb8 57344:8b621b0d921c
    32 import java.nio.channels.ServerSocketChannel;
    32 import java.nio.channels.ServerSocketChannel;
    33 import java.security.AccessController;
    33 import java.security.AccessController;
    34 import java.security.PrivilegedExceptionAction;
    34 import java.security.PrivilegedExceptionAction;
    35 import java.util.Set;
    35 import java.util.Set;
    36 import java.util.Collections;
    36 import java.util.Collections;
    37 import java.util.concurrent.locks.ReentrantLock;
       
    38 
    37 
    39 import jdk.internal.access.JavaNetSocketAccess;
    38 import jdk.internal.access.JavaNetSocketAccess;
    40 import jdk.internal.access.SharedSecrets;
    39 import jdk.internal.access.SharedSecrets;
    41 import sun.net.PlatformSocketImpl;
    40 import sun.net.PlatformSocketImpl;
    42 
    41 
    63      * Various states of this socket.
    62      * Various states of this socket.
    64      */
    63      */
    65     private boolean created = false;
    64     private boolean created = false;
    66     private boolean bound = false;
    65     private boolean bound = false;
    67     private boolean closed = false;
    66     private boolean closed = false;
    68     private final ReentrantLock closeLock = new ReentrantLock();
    67     private Object closeLock = new Object();
    69 
    68 
    70     /**
    69     /**
    71      * The implementation of this Socket.
    70      * The implementation of this Socket.
    72      */
    71      */
    73     private SocketImpl impl;
    72     private SocketImpl impl;
   685      * @exception  IOException  if an I/O error occurs when closing the socket.
   684      * @exception  IOException  if an I/O error occurs when closing the socket.
   686      * @revised 1.4
   685      * @revised 1.4
   687      * @spec JSR-51
   686      * @spec JSR-51
   688      */
   687      */
   689     public void close() throws IOException {
   688     public void close() throws IOException {
   690         closeLock.lock();
   689         synchronized(closeLock) {
   691         try {
   690             if (isClosed())
   692             if (closed)
       
   693                 return;
   691                 return;
   694             closed = true;
       
   695             if (created)
   692             if (created)
   696                 impl.close();
   693                 impl.close();
   697         } finally {
   694             closed = true;
   698             closeLock.unlock();
       
   699         }
   695         }
   700     }
   696     }
   701 
   697 
   702     /**
   698     /**
   703      * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
   699      * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
   735      *
   731      *
   736      * @return true if the socket has been closed
   732      * @return true if the socket has been closed
   737      * @since 1.4
   733      * @since 1.4
   738      */
   734      */
   739     public boolean isClosed() {
   735     public boolean isClosed() {
   740         closeLock.lock();
   736         synchronized(closeLock) {
   741         try {
       
   742             return closed;
   737             return closed;
   743         } finally {
       
   744             closeLock.unlock();
       
   745         }
   738         }
   746     }
   739     }
   747 
   740 
   748     /**
   741     /**
   749      * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the
   742      * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the