src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java
author michaelm
Tue, 29 Oct 2019 19:23:09 +0000
branchunixdomainchannels
changeset 58847 692de65ab293
parent 58801 119ac9128c1b
child 58911 2c777f25cfff
permissions -rw-r--r--
partial cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 51642
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    30
import java.net.InetSocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    31
import java.net.ServerSocket;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    32
import java.net.SocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    33
import java.net.SocketOption;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
    34
import java.net.SocketTimeoutException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    35
import java.net.StandardSocketOptions;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.nio.channels.AlreadyBoundException;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    37
import java.nio.channels.AsynchronousCloseException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    38
import java.nio.channels.ClosedChannelException;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
    39
import java.nio.channels.IllegalBlockingModeException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    40
import java.nio.channels.NotYetBoundException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.ServerSocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.SocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    45
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    46
import java.util.HashSet;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    47
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
    51
import sun.net.NetHooks;
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 49702
diff changeset
    52
import sun.net.ext.ExtendedSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * An implementation of ServerSocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
    58
abstract class ServerSocketChannelImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    extends ServerSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Used to make native close and configure calls
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    63
    static final NativeDispatcher nd = new SocketDispatcher();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Our file descriptor
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    66
    final FileDescriptor fd;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    67
    final int fdVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    69
    // Lock held by thread currently blocked on this channel
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    70
    final ReentrantLock acceptLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // DO NOT invoke a blocking I/O operation while holding this lock!
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    74
    final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Channel state, increases monotonically
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    79
    static final int ST_INUSE = 0;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    80
    static final int ST_CLOSING = 1;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    81
    static final int ST_CLOSED = 2;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    82
    int state;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    83
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    84
    // ID of native thread currently blocked in this channel, for signalling
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    85
    long thread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Binding
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    88
    SocketAddress localAddress; // null => unbound
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
    92
    ServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.fd =  fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   100
    // @throws ClosedChannelException if channel is closed
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   101
    void ensureOpen() throws ClosedChannelException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   102
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   103
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   104
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   105
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   106
    abstract SocketAddress getRevealedLocalAddress(SocketAddress addr);
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   107
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   108
    abstract String getRevealedLocalAddressAsString(SocketAddress addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   110
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   111
    public SocketAddress getLocalAddress() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   112
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   113
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   114
            return (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   115
                    ? null
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   116
                    : getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   117
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   118
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   119
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   121
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   122
     * Marks the beginning of an I/O operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   123
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   124
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   125
     * @throws NotYetBoundException if the channel's socket has not been bound yet
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   126
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   127
    private void begin(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   128
        if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   129
            begin();  // set blocker to close channel if interrupted
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   130
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   131
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   132
            if (localAddress == null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   133
                throw new NotYetBoundException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   134
            if (blocking)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   135
                thread = NativeThread.current();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   136
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   137
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   138
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   139
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   140
     * Marks the end of an I/O operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   141
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   142
     * @throws AsynchronousCloseException if the channel was closed due to this
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   143
     * thread being interrupted on a blocking I/O operation.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   144
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   145
    private void end(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   146
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   147
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   148
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   149
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   150
                thread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   151
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   152
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   153
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   154
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   155
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   156
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   157
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   158
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   159
    abstract int implAccept(FileDescriptor fd, FileDescriptor newfd, SocketAddress[] sa)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   160
        throws IOException;
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   161
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   162
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public SocketChannel accept() throws IOException {
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   164
        int n = 0;
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   165
        FileDescriptor newfd = new FileDescriptor();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   166
        SocketAddress[] isaa = new SocketAddress[1];
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   167
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   168
        acceptLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   169
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   170
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   172
                begin(blocking);
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   173
                n = implAccept(this.fd, newfd, isaa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   174
                if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   175
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   176
                        park(Net.POLLIN);
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   177
                        n = implAccept(this.fd, newfd, isaa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   178
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   179
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   181
                end(blocking, n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   184
        } finally {
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   185
            acceptLock.unlock();
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   186
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   188
        if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   189
            return finishAccept(newfd, isaa[0]);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   190
        } else {
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   191
            return null;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   192
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   193
    }
54154
1caf2daef7cf 8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents: 53419
diff changeset
   194
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   195
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   196
     * Accepts a new connection with a given timeout. This method requires the
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   197
     * channel to be configured in blocking mode.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   198
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   199
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   200
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   201
     * @param nanos the timeout, in nanoseconds
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   202
     * @throws IllegalBlockingModeException if the channel is configured non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   203
     * @throws SocketTimeoutException if the timeout expires
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   204
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   205
    SocketChannel blockingAccept(long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   206
        int n = 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   207
        FileDescriptor newfd = new FileDescriptor();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   208
        SocketAddress[] isaa = new SocketAddress[1];
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   209
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   210
        acceptLock.lock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   211
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   212
            // check that channel is configured blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   213
            if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   214
                throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   215
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   216
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   217
                begin(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   218
                // change socket to non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   219
                lockedConfigureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   220
                try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   221
                    long startNanos = System.nanoTime();
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   222
                    n = implAccept(fd, newfd, isaa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   223
                    while (n == IOStatus.UNAVAILABLE && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   224
                        long remainingNanos = nanos - (System.nanoTime() - startNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   225
                        if (remainingNanos <= 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   226
                            throw new SocketTimeoutException("Accept timed out");
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   227
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   228
                        park(Net.POLLIN, remainingNanos);
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   229
                        n = implAccept(fd, newfd, isaa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   230
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   231
                } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   232
                    // restore socket to blocking mode
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   233
                    lockedConfigureBlocking(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   234
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   235
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   236
                end(true, n > 0);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   237
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   238
        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   239
            acceptLock.unlock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   240
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   241
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   242
        assert n > 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   243
        return finishAccept(newfd, isaa[0]);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   244
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   245
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   246
    abstract SocketChannel finishAccept(FileDescriptor newfd, SocketAddress isa)
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   247
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   249
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected void implConfigureBlocking(boolean block) throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   251
        acceptLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   252
        try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   253
            lockedConfigureBlocking(block);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   254
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   255
            acceptLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   259
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   260
     * Adjust the blocking mode while holding acceptLock.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   261
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   262
    private void lockedConfigureBlocking(boolean block) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   263
        assert acceptLock.isHeldByCurrentThread();
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   264
        synchronized (stateLock) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   265
            ensureOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   266
            IOUtil.configureBlocking(fd, block);
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   267
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   268
    }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   269
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   270
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   271
     * Closes the socket if there are no accept in progress and the channel is
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   272
     * not registered with a Selector.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   273
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   274
    private boolean tryClose() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   275
        assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   276
        if ((thread == 0) && !isRegistered()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   277
            state = ST_CLOSED;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   278
            nd.close(fd);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   279
            return true;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   280
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   281
            return false;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   282
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   283
    }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   284
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   285
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   286
     * Invokes tryClose to attempt to close the socket.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   287
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   288
     * This method is used for deferred closing by I/O and Selector operations.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   289
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   290
    private void tryFinishClose() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   291
        try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   292
            tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   293
        } catch (IOException ignore) { }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   294
    }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   295
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   296
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   297
     * Closes this channel when configured in blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   298
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   299
     * If there is an accept in progress then the socket is pre-closed and the
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   300
     * accept thread is signalled, in which case the final close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   301
     * until the accept aborts.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   302
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   303
    private void implCloseBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   304
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   305
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   306
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   307
            if (!tryClose()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   308
                long th = thread;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   309
                if (th != 0) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   310
                    nd.preClose(fd);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   311
                    NativeThread.signal(th);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   312
                }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   313
            }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   314
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   315
    }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   316
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   317
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   318
     * Closes this channel when configured in non-blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   319
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   320
     * If the channel is registered with a Selector then the close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   321
     * until the channel is flushed from all Selectors.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   322
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   323
    private void implCloseNonBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   324
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   325
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   326
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   327
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   328
        // wait for any accept to complete before trying to close
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   329
        acceptLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   330
        acceptLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   331
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   332
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   333
                tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   334
            }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   335
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   336
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   337
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54154
diff changeset
   338
    /**
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   339
     * Invoked by implCloseChannel to close the channel.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   340
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   341
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   342
    protected void implCloseSelectableChannel() throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   343
        assert !isOpen();
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   344
        if (isBlocking()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   345
            implCloseBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   346
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   347
            implCloseNonBlockingMode();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   348
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   349
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   350
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   351
    @Override
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   352
    public void kill() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   353
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   354
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   355
                tryFinishClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   357
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   358
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   359
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   360
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   361
     * Returns true if channel's socket is bound
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   362
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   363
    boolean isBound() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   364
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   365
            return localAddress != null;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   366
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   367
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   368
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   369
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   370
     * Returns the local address, or null if not bound
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   371
     */
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   372
    SocketAddress localAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   373
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   374
            return localAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Translates native poll revent set into a ready operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   381
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   382
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   383
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   386
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   393
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   395
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   399
        if (((ops & Net.POLLIN) != 0) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            ((intOps & SelectionKey.OP_ACCEPT) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                newOps |= SelectionKey.OP_ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   403
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   407
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   408
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   411
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   412
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   418
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if ((ops & SelectionKey.OP_ACCEPT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   421
            newOps |= Net.POLLIN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   422
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   434
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        sb.append(this.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        sb.append('[');
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   437
        if (!isOpen()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            sb.append("closed");
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   439
        } else {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   440
            synchronized (stateLock) {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   441
                SocketAddress addr = localAddress;
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   442
                if (addr == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    sb.append("unbound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } else {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 55081
diff changeset
   445
                    sb.append(getRevealedLocalAddressAsString(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
}