src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
author alanb
Fri, 30 Mar 2018 08:28:09 +0100
changeset 49493 814bd31f8da0
parent 49143 2d5cc05d877e
child 49702 09c01737ad27
permissions -rw-r--r--
8200257: (se) More Selector cleanup Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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.InetAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    31
import java.net.InetSocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    32
import java.net.ProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    33
import java.net.Socket;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    34
import java.net.SocketAddress;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    35
import java.net.SocketOption;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    37
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.nio.channels.AlreadyBoundException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    40
import java.nio.channels.AlreadyConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.AsynchronousCloseException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.ConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.NoConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    45
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    46
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.nio.channels.SocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
import java.util.HashSet;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    51
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    54
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
    55
import sun.net.NetHooks;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    56
import sun.net.ext.ExtendedSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class SocketChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Our file descriptor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Lock held by current reading or connecting thread
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    74
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Lock held by current writing or connecting thread
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    77
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    83
    // Input/Output closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    84
    private volatile boolean isInputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    85
    private volatile boolean isOutputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    89
    // set true when exclusive binding is on and SO_REUSEADDR is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    90
    private boolean isReuseAddress;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // State, increases monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final int ST_UNCONNECTED = 0;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    94
    private static final int ST_CONNECTIONPENDING = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int ST_CONNECTED = 2;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    96
    private static final int ST_CLOSING = 3;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    97
    private static final int ST_KILLPENDING = 4;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    98
    private static final int ST_KILLED = 5;
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
    99
    private volatile int state;  // need stateLock to change
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   100
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   101
    // IDs of native threads doing reads and writes, for signalling
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   102
    private long readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   103
    private long writerThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // Binding
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   106
    private InetSocketAddress localAddress;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   107
    private InetSocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Socket adaptor, created on demand
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   110
    private Socket socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    SocketChannelImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   123
    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   124
        throws IOException
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   125
    {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   126
        super(sp);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   127
        this.fd = fd;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   128
        this.fdVal = IOUtil.fdVal(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   129
        if (bound) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   130
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   131
                this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   132
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   133
        }
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   134
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // Constructor for sockets obtained from server sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    //
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   138
    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.fdVal = IOUtil.fdVal(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   144
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   145
            this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   146
            this.remoteAddress = isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   147
            this.state = ST_CONNECTED;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   148
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   151
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   152
     * Checks that the channel is open.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   153
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   154
     * @throws ClosedChannelException if channel is closed (or closing)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   155
     */
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   156
    private void ensureOpen() throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   157
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   158
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   159
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   160
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   161
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   162
     * Checks that the channel is open and connected.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   163
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   164
     * @apiNote This method uses the "state" field to check if the channel is
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   165
     * open. It should never be used in conjuncion with isOpen or ensureOpen
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   166
     * as these methods check AbstractInterruptibleChannel's closed field - that
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   167
     * field is set before implCloseSelectableChannel is called and so before
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   168
     * the state is changed.
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   169
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   170
     * @throws ClosedChannelException if channel is closed (or closing)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   171
     * @throws NotYetConnectedException if open and not connected
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   172
     */
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   173
    private void ensureOpenAndConnected() throws ClosedChannelException {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   174
        int state = this.state;
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   175
        if (state < ST_CONNECTED) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   176
            throw new NotYetConnectedException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   177
        } else if (state > ST_CONNECTED) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   178
            throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   179
        }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   180
    }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   181
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   182
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public Socket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                socket = SocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   191
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   192
    public SocketAddress getLocalAddress() throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   193
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   194
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   195
            return Net.getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   196
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   197
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   198
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   199
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   200
    public SocketAddress getRemoteAddress() throws IOException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   202
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   208
    public <T> SocketChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   211
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   212
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   213
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   216
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   217
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   218
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   219
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   220
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   221
                Net.setSocketOption(fd, family, name, value);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
                return this;
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   223
            }
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   224
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   225
            if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   226
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   227
                isReuseAddress = (Boolean)value;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   228
                return this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   229
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   230
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   231
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   232
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   233
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   234
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   235
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   236
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   242
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   243
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   244
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   246
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   247
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   249
            if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   250
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   251
                return (T)Boolean.valueOf(isReuseAddress);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   252
            }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   253
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
            // special handling for IP_TOS: always return 0 when IPv6
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   255
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   256
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   257
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   258
                return (T) Net.getSocketOption(fd, family, name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   259
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   260
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   261
            // no options that require special handling
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   262
            return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   263
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   264
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   265
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   266
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   267
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   268
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   269
        private static Set<SocketOption<?>> defaultOptions() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   270
            HashSet<SocketOption<?>> set = new HashSet<>();
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   271
            set.add(StandardSocketOptions.SO_SNDBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   272
            set.add(StandardSocketOptions.SO_RCVBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   273
            set.add(StandardSocketOptions.SO_KEEPALIVE);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   274
            set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   275
            if (Net.isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   276
                set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   277
            }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   278
            set.add(StandardSocketOptions.SO_LINGER);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   279
            set.add(StandardSocketOptions.TCP_NODELAY);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   280
            // additional options required by socket adaptor
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   281
            set.add(StandardSocketOptions.IP_TOS);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   282
            set.add(ExtendedSocketOption.SO_OOBINLINE);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   283
            set.addAll(ExtendedSocketOptions.getInstance().options());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   284
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   286
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   287
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   288
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   289
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   290
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   292
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   293
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   294
     * Marks the beginning of a read operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   295
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   296
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   297
     * @throws NotYetConnectedException if the channel is not yet connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   298
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   299
    private void beginRead(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   300
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   301
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   302
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   303
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   304
            synchronized (stateLock) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   305
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   306
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   307
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   308
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   309
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   310
            ensureOpenAndConnected();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   314
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   315
     * Marks the end of a read operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   316
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   317
     * @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
   318
     * thread being interrupted on a blocking read operation.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   319
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   320
    private void endRead(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   321
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   322
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   323
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   324
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   325
                readerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   326
                // notify any thread waiting in implCloseSelectableChannel
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   327
                if (state == ST_CLOSING) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   328
                    stateLock.notifyAll();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   329
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   330
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   331
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   332
            end(completed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   336
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   337
    public int read(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   338
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   339
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   340
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   341
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   342
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   343
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   344
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   345
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   346
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   347
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   348
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   349
                    return IOStatus.EOF;
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
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   352
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   353
                        n = IOUtil.read(fd, buf, -1, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   354
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   355
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   356
                    n = IOUtil.read(fd, buf, -1, nd);
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
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   359
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   360
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   361
                    return IOStatus.EOF;
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
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   364
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   365
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   369
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   370
    public long read(ByteBuffer[] dsts, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   371
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   372
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   373
        Objects.checkFromIndexSize(offset, length, dsts.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   375
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   376
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   377
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   378
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   379
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   380
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   381
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   382
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   383
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   384
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   385
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   386
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   387
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   388
                        n = IOUtil.read(fd, dsts, offset, length, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   389
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   390
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   391
                    n = IOUtil.read(fd, dsts, offset, length, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   392
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   393
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   394
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   395
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   396
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   397
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   398
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   399
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   400
            readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   401
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   402
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   403
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   404
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   405
     * Marks the beginning of a write operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   406
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   407
     * @throws ClosedChannelException if the channel is closed or output shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   408
     * @throws NotYetConnectedException if the channel is not yet connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   409
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   410
    private void beginWrite(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   411
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   412
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   413
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   414
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   415
            synchronized (stateLock) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   416
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   417
                if (isOutputClosed)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   418
                    throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   419
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   420
                writerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   421
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   422
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   423
            ensureOpenAndConnected();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   424
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   425
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   426
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   427
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   428
     * Marks the end of a write operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   429
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   430
     * @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
   431
     * thread being interrupted on a blocking write operation.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   432
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   433
    private void endWrite(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   434
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   435
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   436
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   437
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   438
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   439
                // notify any thread waiting in implCloseSelectableChannel
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   440
                if (state == ST_CLOSING) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   441
                    stateLock.notifyAll();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   442
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   443
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   444
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   445
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   446
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   447
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   448
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   449
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   450
    public int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   451
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   452
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   453
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   454
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   455
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   458
                beginWrite(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   459
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   460
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   461
                        n = IOUtil.write(fd, buf, -1, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   462
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   463
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   464
                    n = IOUtil.write(fd, buf, -1, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   465
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   466
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   467
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   468
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   469
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   470
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   471
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   472
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   473
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   474
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   475
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   477
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   478
    public long write(ByteBuffer[] srcs, int offset, int length)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   479
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   480
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   481
        Objects.checkFromIndexSize(offset, length, srcs.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   483
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   484
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   485
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   486
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   487
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   488
                beginWrite(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   489
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   490
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   491
                        n = IOUtil.write(fd, srcs, offset, length, nd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   492
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   493
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   494
                    n = IOUtil.write(fd, srcs, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   497
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   498
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   499
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   500
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   501
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   502
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   503
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   504
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   505
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   507
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   508
     * Writes a byte of out of band data.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   509
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   510
    int sendOutOfBandData(byte b) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   511
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   512
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   513
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   514
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   515
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   516
                beginWrite(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   517
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   518
                    do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   519
                        n = sendOutOfBandData(fd, b);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   520
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   521
                } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   522
                    n = sendOutOfBandData(fd, b);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   523
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   524
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   525
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   526
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   527
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   528
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   529
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   530
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   531
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   532
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   533
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   534
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   535
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   536
    protected void implConfigureBlocking(boolean block) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   537
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   538
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   539
            writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   540
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   542
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   543
                    IOUtil.configureBlocking(fd, block);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   545
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   546
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   548
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   549
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   553
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   554
     * 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
   555
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   556
    InetSocketAddress localAddress() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   562
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   563
     * Returns the remote address, or null if not connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   564
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   565
    InetSocketAddress remoteAddress() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   571
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   572
    public SocketChannel bind(SocketAddress local) throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   573
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   574
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   575
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   576
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   578
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   579
                    if (state == ST_CONNECTIONPENDING)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   580
                        throw new ConnectionPendingException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    if (localAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        throw new AlreadyBoundException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   583
                    InetSocketAddress isa = (local == null) ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   584
                        new InetSocketAddress(0) : Net.checkAddress(local);
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   585
                    SecurityManager sm = System.getSecurityManager();
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   586
                    if (sm != null) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   587
                        sm.checkListen(isa.getPort());
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   588
                    }
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   589
                    NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   593
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   594
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   596
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   597
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   599
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   602
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public boolean isConnected() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   604
        return (state == ST_CONNECTED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   607
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public boolean isConnectionPending() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   609
        return (state == ST_CONNECTIONPENDING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   612
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   613
     * Marks the beginning of a connect operation that might block.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   614
     * @param blocking true if configured blocking
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   615
     * @param isa the remote address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   616
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   617
     * @throws AlreadyConnectedException if already connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   618
     * @throws ConnectionPendingException is a connection is pending
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   619
     * @throws IOException if the pre-connect hook fails
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   620
     */
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   621
    private void beginConnect(boolean blocking, InetSocketAddress isa)
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   622
        throws IOException
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   623
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   624
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   625
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   626
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   627
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   629
            ensureOpen();
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   630
            int state = this.state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   633
            if (state == ST_CONNECTIONPENDING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                throw new ConnectionPendingException();
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   635
            assert state == ST_UNCONNECTED;
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   636
            this.state = ST_CONNECTIONPENDING;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   637
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   638
            if (localAddress == null)
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   639
                NetHooks.beforeTcpConnect(fd, isa.getAddress(), isa.getPort());
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   640
            remoteAddress = isa;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   641
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   642
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   643
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   644
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   645
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   649
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   650
     * Marks the end of a connect operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   651
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   652
     * @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
   653
     * thread being interrupted on a blocking connect operation.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   654
     * @throws IOException if completed and unable to obtain the local address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   655
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   656
    private void endConnect(boolean blocking, boolean completed)
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   657
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   658
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   659
        endRead(blocking, completed);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   660
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   661
        if (completed) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   662
            synchronized (stateLock) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   663
                if (state == ST_CONNECTIONPENDING) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   664
                    localAddress = Net.localAddress(fd);
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   665
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   666
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   667
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   668
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   669
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   670
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   671
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public boolean connect(SocketAddress sa) throws IOException {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   673
        InetSocketAddress isa = Net.checkAddress(sa);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   674
        SecurityManager sm = System.getSecurityManager();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   675
        if (sm != null)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   676
            sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   677
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   678
        InetAddress ia = isa.getAddress();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   679
        if (ia.isAnyLocalAddress())
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   680
            ia = InetAddress.getLocalHost();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   681
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   682
        try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   683
            readLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   684
            try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   685
                writeLock.lock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   686
                try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   687
                    int n = 0;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   688
                    boolean blocking = isBlocking();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   689
                    try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   690
                        beginConnect(blocking, isa);
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   691
                        do {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   692
                            n = Net.connect(fd, ia, isa.getPort());
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   693
                        } while (n == IOStatus.INTERRUPTED && isOpen());
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   694
                    } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   695
                        endConnect(blocking, (n > 0));
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   696
                    }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   697
                    assert IOStatus.check(n);
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   698
                    return n > 0;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   699
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   700
                    writeLock.unlock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   701
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   702
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   703
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   705
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   706
            // connect failed, close the channel
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   707
            close();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   708
            throw ioe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   712
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   713
     * Marks the beginning of a finishConnect operation that might block.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   714
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   715
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   716
     * @throws NoConnectionPendingException if no connection is pending
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   717
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   718
    private void beginFinishConnect(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   719
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   720
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   721
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   722
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   723
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   724
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   725
            if (state != ST_CONNECTIONPENDING)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   726
                throw new NoConnectionPendingException();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   727
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   728
                // record thread so it can be signalled if needed
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   729
                readerThread = NativeThread.current();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   730
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   731
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   732
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   733
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   734
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   735
     * Marks the end of a finishConnect operation that may have blocked.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   736
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   737
     * @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
   738
     * thread being interrupted on a blocking connect operation.
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   739
     * @throws IOException if completed and unable to obtain the local address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   740
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   741
    private void endFinishConnect(boolean blocking, boolean completed)
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   742
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   743
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   744
        endRead(blocking, completed);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   745
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   746
        if (completed) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   747
            synchronized (stateLock) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   748
                if (state == ST_CONNECTIONPENDING) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   749
                    localAddress = Net.localAddress(fd);
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   750
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   751
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   752
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   753
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   754
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   755
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   756
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public boolean finishConnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   758
        try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   759
            readLock.lock();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   760
            try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   761
                writeLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   762
                try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   763
                    // no-op if already connected
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   764
                    if (isConnected())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        return true;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   766
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   767
                    boolean blocking = isBlocking();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   768
                    boolean connected = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   770
                        beginFinishConnect(blocking);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   771
                        int n = 0;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   772
                        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   773
                            do {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   774
                                n = checkConnect(fd, true);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   775
                            } while ((n == 0 || n == IOStatus.INTERRUPTED) && isOpen());
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   776
                        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   777
                            n = checkConnect(fd, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   779
                        connected = (n > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   781
                        endFinishConnect(blocking, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   783
                    assert (blocking && connected) ^ !blocking;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   784
                    return connected;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   785
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   786
                    writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   788
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   789
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   791
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   792
            // connect failed, close the channel
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   793
            close();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   794
            throw ioe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   798
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   799
     * Invoked by implCloseChannel to close the channel.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   800
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   801
     * This method waits for outstanding I/O operations to complete. When in
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   802
     * blocking mode, the socket is pre-closed and the threads in blocking I/O
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   803
     * operations are signalled to ensure that the outstanding I/O operations
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   804
     * complete quickly.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   805
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   806
     * If the socket is connected then it is shutdown by this method. The
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   807
     * shutdown ensures that the peer reads EOF for the case that the socket is
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   808
     * not pre-closed or closed by this method.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   809
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   810
     * The socket is closed by this method when it is not registered with a
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   811
     * Selector. Note that a channel configured blocking may be registered with
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   812
     * a Selector. This arises when a key is canceled and the channel configured
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   813
     * to blocking mode before the key is flushed from the Selector.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   814
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   815
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   816
    protected void implCloseSelectableChannel() throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   817
        assert !isOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   818
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   819
        boolean blocking;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   820
        boolean connected;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   821
        boolean interrupted = false;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   822
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   823
        // set state to ST_CLOSING
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   824
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   825
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   826
            blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   827
            connected = (state == ST_CONNECTED);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   828
            state = ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   829
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   830
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   831
        // wait for any outstanding I/O operations to complete
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   832
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   833
            synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   834
                assert state == ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   835
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   836
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   837
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   838
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   839
                    connected = false; // fd is no longer connected socket
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   840
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   841
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   842
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   843
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   844
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   845
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   846
                    // wait for blocking I/O operations to end
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   847
                    while (readerThread != 0 || writerThread != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   848
                        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   849
                            stateLock.wait();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   850
                        } catch (InterruptedException e) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   851
                            interrupted = true;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   852
                        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   853
                    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   854
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   855
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   856
        } else {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   857
            // non-blocking mode: wait for read/write to complete
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   858
            readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   859
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   860
                writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   861
                writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   862
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   863
                readLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   864
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   865
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   866
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   867
        // set state to ST_KILLPENDING
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   868
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   869
            assert state == ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   870
            // if connected, and the channel is registered with a Selector, we
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   871
            // shutdown the output so that the peer reads EOF
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   872
            if (connected && isRegistered()) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   873
                try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   874
                    Net.shutdown(fd, Net.SHUT_WR);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   875
                } catch (IOException ignore) { }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   876
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   877
            state = ST_KILLPENDING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   878
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   879
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   880
        // close socket if not registered with Selector
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   881
        if (!isRegistered())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   882
            kill();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   883
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   884
        // restore interrupt status
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   885
        if (interrupted)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   886
            Thread.currentThread().interrupt();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   887
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   888
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   889
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   890
    public void kill() throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   891
        synchronized (stateLock) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   892
            if (state == ST_KILLPENDING) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   893
                state = ST_KILLED;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   894
                nd.close(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   895
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   896
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   897
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   898
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   899
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   900
    public SocketChannel shutdownInput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   902
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   903
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   904
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   905
            if (!isInputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   906
                Net.shutdown(fd, Net.SHUT_RD);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   907
                long thread = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   908
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   909
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   910
                isInputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   911
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   912
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   916
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   917
    public SocketChannel shutdownOutput() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   919
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   920
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   921
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   922
            if (!isOutputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   923
                Net.shutdown(fd, Net.SHUT_WR);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   924
                long thread = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   925
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   926
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   927
                isOutputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   928
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   929
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   933
    boolean isInputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   934
        return !isInputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   935
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   936
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   937
    boolean isOutputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   938
        return !isOutputClosed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   941
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   942
     * Poll this channel's socket for reading up to the given timeout.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   943
     * @return {@code true} if the socket is polled
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   944
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   945
    boolean pollRead(long timeout) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   946
        boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   947
        assert Thread.holdsLock(blockingLock()) && blocking;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   948
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   949
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   950
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   951
            boolean polled = false;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   952
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   953
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   954
                int n = Net.poll(fd, Net.POLLIN, timeout);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   955
                polled = (n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   956
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   957
                endRead(blocking, polled);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   958
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   959
            return polled;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   960
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   961
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   965
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   966
     * Poll this channel's socket for a connection, up to the given timeout.
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   967
     * @return {@code true} if the socket is polled
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   968
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   969
    boolean pollConnected(long timeout) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   970
        boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   971
        assert Thread.holdsLock(blockingLock()) && blocking;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   973
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   974
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   975
            writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   976
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   977
                boolean polled = false;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   978
                try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   979
                    beginFinishConnect(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   980
                    int n = Net.poll(fd, Net.POLLCONN, timeout);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   981
                    polled = (n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   982
                } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   983
                    endFinishConnect(blocking, polled);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   984
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   985
                return polled;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   986
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   987
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   989
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   990
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
   997
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
   998
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
   999
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1002
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1009
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1011
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1015
        boolean connected = isConnected();
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1016
        if (((ops & Net.POLLIN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1017
            ((intOps & SelectionKey.OP_READ) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1020
        if (((ops & Net.POLLCONN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1021
            ((intOps & SelectionKey.OP_CONNECT) != 0) && isConnectionPending())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1024
        if (((ops & Net.POLLOUT) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1025
            ((intOps & SelectionKey.OP_WRITE) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1028
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1032
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1033
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1036
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1037
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1043
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1046
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1048
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1050
            newOps |= Net.POLLCONN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1051
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1062
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23879
diff changeset
  1064
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    break;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1075
                case ST_CONNECTIONPENDING:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    sb.append("connected");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1080
                    if (isInputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        sb.append(" ishut");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1082
                    if (isOutputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                }
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1086
                InetSocketAddress addr = localAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1087
                if (addr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    sb.append(" local=");
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1089
                    sb.append(Net.getRevealedLocalAddressAsString(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
  1104
    private static native int checkConnect(FileDescriptor fd, boolean block)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
6117
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1107
    private static native int sendOutOfBandData(FileDescriptor fd, byte data)
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1108
        throws IOException;
471ae95609d5 6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents: 5506
diff changeset
  1109
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 18212
diff changeset
  1111
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        nd = new SocketDispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
}