src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 58900 434329f6f456
child 58911 2c777f25cfff
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50722
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    30
import java.net.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;
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    35
import java.net.SocketException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    36
import java.net.SocketOption;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    37
import java.net.SocketTimeoutException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    38
import java.net.StandardProtocolFamily;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    39
import java.net.StandardSocketOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.ByteBuffer;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.AlreadyBoundException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.AlreadyConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.AsynchronousCloseException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    44
import java.nio.channels.ClosedChannelException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    45
import java.nio.channels.ConnectionPendingException;
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
    46
import java.nio.channels.IllegalBlockingModeException;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    47
import java.nio.channels.NoConnectionPendingException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    48
import java.nio.channels.NotYetConnectedException;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    49
import java.nio.channels.SelectionKey;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    50
import java.nio.channels.SocketChannel;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    51
import java.nio.channels.spi.SelectorProvider;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    52
import java.util.Collections;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    53
import java.util.HashSet;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    54
import java.util.Objects;
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    55
import java.util.Set;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    56
import java.util.concurrent.locks.ReentrantLock;
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
    57
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    58
import sun.net.ConnectionResetException;
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
    59
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
    60
import sun.net.ext.ExtendedSocketOptions;
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 50303
diff changeset
    61
import sun.net.util.SocketExceptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * An implementation of SocketChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
class SocketChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    extends SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Used to make native read and write calls
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53419
diff changeset
    72
    private static final NativeDispatcher nd = new SocketDispatcher();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Our file descriptor object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private final FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // 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
    79
    private final ReentrantLock readLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // 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
    82
    private final ReentrantLock writeLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // DO NOT invoke a blocking I/O operation while holding this lock!
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
    86
    private final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    88
    // Input/Output closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    89
    private volatile boolean isInputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    90
    private volatile boolean isOutputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
    91
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    92
    // Connection reset protected by readLock
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    93
    private boolean connectionReset;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    97
    // set true when exclusive binding is on and SO_REUSEADDR is emulated
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    98
    private boolean isReuseAddress;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // State, increases monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    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
   102
    private static final int ST_CONNECTIONPENDING = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    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
   104
    private static final int ST_CLOSING = 3;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   105
    private static final int ST_CLOSED = 4;
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   106
    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
   107
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   108
    // 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
   109
    private long readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   110
    private long writerThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // Binding
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   113
    private InetSocketAddress localAddress;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
   114
    private InetSocketAddress remoteAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // Socket adaptor, created on demand
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   117
    private Socket socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // Constructor for normal connecting sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    SocketChannelImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.fd = Net.socket(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   130
    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   131
        throws IOException
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   132
    {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   133
        super(sp);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   134
        this.fd = fd;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   135
        this.fdVal = IOUtil.fdVal(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   136
        if (bound) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   137
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   138
                this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   139
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   140
        }
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   141
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 6301
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    // Constructor for sockets obtained from server sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    //
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   145
    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.fdVal = IOUtil.fdVal(fd);
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   151
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   152
            this.localAddress = Net.localAddress(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   153
            this.remoteAddress = isa;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   154
            this.state = ST_CONNECTED;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   158
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   159
     * 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
   160
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   161
     * @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
   162
     */
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   163
    private void ensureOpen() throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   164
        if (!isOpen())
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   165
            throw new ClosedChannelException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   166
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   167
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   168
    /**
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   169
     * 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
   170
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   171
     * @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
   172
     * 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
   173
     * 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
   174
     * 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
   175
     * 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
   176
     *
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   177
     * @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
   178
     * @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
   179
     */
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   180
    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
   181
        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
   182
        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
   183
            throw new NotYetConnectedException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   184
        } 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
   185
            throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   186
        }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   187
    }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   188
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   189
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Socket socket() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   191
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (socket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                socket = SocketAdaptor.create(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   198
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   199
    public SocketAddress getLocalAddress() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   200
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   201
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   202
            return Net.getRevealedLocalAddress(localAddress);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
        }
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
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   207
    public SocketAddress getRemoteAddress() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   208
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   209
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
            return remoteAddress;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   215
    public <T> SocketChannel setOption(SocketOption<T> name, T value)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   216
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   217
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   218
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   219
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   220
            throw new UnsupportedOperationException("'" + name + "' not supported");
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54754
diff changeset
   221
        if (!name.type().isInstance(value))
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54754
diff changeset
   222
            throw new IllegalArgumentException("Invalid value '" + value + "'");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   223
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   224
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   225
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   226
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   227
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   228
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   229
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   230
                Net.setSocketOption(fd, family, name, value);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   231
                return this;
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   232
            }
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   233
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   234
            if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   235
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   236
                isReuseAddress = (Boolean)value;
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   237
                return this;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   238
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
            // no options that require special handling
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   241
            Net.setSocketOption(fd, name, value);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   242
            return this;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
    }
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
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   247
    @SuppressWarnings("unchecked")
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
    public <T> T getOption(SocketOption<T> name)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
        throws IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   250
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   251
        Objects.requireNonNull(name);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   252
        if (!supportedOptions().contains(name))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   253
            throw new UnsupportedOperationException("'" + name + "' not supported");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   254
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   255
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   256
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   257
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   258
            if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   259
                // SO_REUSEADDR emulated when using exclusive bind
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   260
                return (T)Boolean.valueOf(isReuseAddress);
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   261
            }
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 16921
diff changeset
   262
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   263
            // special handling for IP_TOS: always return 0 when IPv6
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   264
            if (name == StandardSocketOptions.IP_TOS) {
25170
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   265
                ProtocolFamily family = Net.isIPv6Available() ?
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   266
                    StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
f58832169add 8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents: 24969
diff changeset
   267
                return (T) Net.getSocketOption(fd, family, name);
1152
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
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   270
            // no options that require special handling
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   271
            return (T) Net.getSocketOption(fd, name);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   272
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   273
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   274
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   275
    private static class DefaultOptionsHolder {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   276
        static final Set<SocketOption<?>> defaultOptions = defaultOptions();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   277
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   278
        private static Set<SocketOption<?>> defaultOptions() {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   279
            HashSet<SocketOption<?>> set = new HashSet<>();
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   280
            set.add(StandardSocketOptions.SO_SNDBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   281
            set.add(StandardSocketOptions.SO_RCVBUF);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   282
            set.add(StandardSocketOptions.SO_KEEPALIVE);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   283
            set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   284
            if (Net.isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   285
                set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34774
diff changeset
   286
            }
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   287
            set.add(StandardSocketOptions.SO_LINGER);
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   288
            set.add(StandardSocketOptions.TCP_NODELAY);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   289
            // additional options required by socket adaptor
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   290
            set.add(StandardSocketOptions.IP_TOS);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
            set.add(ExtendedSocketOption.SO_OOBINLINE);
53419
eac105e3ec13 8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents: 50722
diff changeset
   292
            set.addAll(ExtendedSocketOptions.clientSocketOptions());
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   293
            return Collections.unmodifiableSet(set);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   294
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   295
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   297
    @Override
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   298
    public final Set<SocketOption<?>> supportedOptions() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   299
        return DefaultOptionsHolder.defaultOptions;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   300
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   301
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   302
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   303
     * 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
   304
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   305
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   306
     * @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
   307
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   308
    private void beginRead(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   309
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   310
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   311
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   312
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   313
            synchronized (stateLock) {
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   314
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   315
                // 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
   316
                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
   317
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   318
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   319
            ensureOpenAndConnected();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   323
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   324
     * 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
   325
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   326
     * @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
   327
     * 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
   328
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   329
    private void endRead(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   330
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   331
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   332
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   333
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   334
                readerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   335
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   336
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   337
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   338
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   339
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   340
            end(completed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   344
    private void throwConnectionReset() throws SocketException {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   345
        throw new SocketException("Connection reset");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   346
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   347
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   348
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   349
    public int read(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   350
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   351
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   352
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   353
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   354
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   355
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   356
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   357
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   358
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   359
                // check if connection has been reset
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   360
                if (connectionReset)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   361
                    throwConnectionReset();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   362
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   363
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   364
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   365
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   366
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   367
                n = IOUtil.read(fd, buf, -1, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   368
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   369
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   370
                        park(Net.POLLIN);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   371
                        n = IOUtil.read(fd, buf, -1, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   372
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   373
                }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   374
            } catch (ConnectionResetException e) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   375
                connectionReset = true;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   376
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   377
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   378
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   379
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   380
                    return IOStatus.EOF;
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
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   383
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   384
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   388
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   389
    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
   390
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   391
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   392
        Objects.checkFromIndexSize(offset, length, dsts.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   394
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   395
        try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   396
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   397
            long n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   398
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   399
                beginRead(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   400
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   401
                // check if connection has been reset
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   402
                if (connectionReset)
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   403
                    throwConnectionReset();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   404
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   405
                // check if input is shutdown
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   406
                if (isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   407
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   408
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   409
                n = IOUtil.read(fd, dsts, offset, length, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   410
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   411
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   412
                        park(Net.POLLIN);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   413
                        n = IOUtil.read(fd, dsts, offset, length, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   414
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   415
                }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   416
            } catch (ConnectionResetException e) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   417
                connectionReset = true;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
   418
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   419
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   420
                endRead(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   421
                if (n <= 0 && isInputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   422
                    return IOStatus.EOF;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   423
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   424
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   425
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   426
            readLock.unlock();
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
    }
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
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   431
     * 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
   432
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   433
     * @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
   434
     * @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
   435
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   436
    private void beginWrite(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   437
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   438
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   439
            begin();
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   440
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   441
            synchronized (stateLock) {
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   442
                ensureOpenAndConnected();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   443
                if (isOutputClosed)
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   444
                    throw new ClosedChannelException();
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   445
                // 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
   446
                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
   447
            }
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   448
        } else {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   449
            ensureOpenAndConnected();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   450
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   451
    }
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
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   454
     * 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
   455
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   456
     * @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
   457
     * 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
   458
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   459
    private void endWrite(boolean blocking, boolean completed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   460
        throws AsynchronousCloseException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   461
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   462
        if (blocking) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   463
            synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   464
                writerThread = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   465
                if (state == ST_CLOSING) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   466
                    tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   467
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   468
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   469
            // remove hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   470
            end(completed);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   471
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   472
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   473
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   474
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   475
    public int write(ByteBuffer buf) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   476
        Objects.requireNonNull(buf);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   477
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   478
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   479
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   480
            boolean blocking = isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   483
                beginWrite(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   484
                n = IOUtil.write(fd, buf, -1, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   485
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   486
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   487
                        park(Net.POLLOUT);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   488
                        n = IOUtil.write(fd, buf, -1, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   489
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   490
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   491
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   492
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   493
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   494
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   495
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   496
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   497
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   498
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   499
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   500
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   502
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   503
    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
   504
        throws IOException
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   505
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   506
        Objects.checkFromIndexSize(offset, length, srcs.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   508
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   509
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   510
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   511
            long n = 0;
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
                beginWrite(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   514
                n = IOUtil.write(fd, srcs, offset, length, nd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   515
                if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   516
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   517
                        park(Net.POLLOUT);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   518
                        n = IOUtil.write(fd, srcs, offset, length, nd);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   519
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            } finally {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   522
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   523
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   524
                    throw new AsynchronousCloseException();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   525
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   526
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   527
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   528
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   529
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   530
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
49001
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
     * 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
   534
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   535
    int sendOutOfBandData(byte b) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   536
        writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   537
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   538
            boolean blocking = isBlocking();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   539
            int n = 0;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   540
            try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   541
                beginWrite(blocking);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   542
                if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   543
                    do {
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53419
diff changeset
   544
                        n = Net.sendOOB(fd, b);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   545
                    } while (n == IOStatus.INTERRUPTED && isOpen());
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   546
                } else {
53445
c96f9aa1f3d8 8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents: 53419
diff changeset
   547
                    n = Net.sendOOB(fd, b);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   548
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   549
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   550
                endWrite(blocking, n > 0);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   551
                if (n <= 0 && isOutputClosed)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   552
                    throw new AsynchronousCloseException();
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
            return IOStatus.normalize(n);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   555
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   556
            writeLock.unlock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   557
        }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   558
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   559
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   560
    @Override
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   561
    protected void implConfigureBlocking(boolean block) throws IOException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   562
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   563
        try {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   564
            writeLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   565
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   566
                lockedConfigureBlocking(block);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   567
            } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   568
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   570
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   571
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   575
    /**
58900
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   576
     * Adjusts the blocking mode. readLock or writeLock must already be held.
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   577
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   578
    private void lockedConfigureBlocking(boolean block) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   579
        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   580
        synchronized (stateLock) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   581
            ensureOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   582
            IOUtil.configureBlocking(fd, block);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   583
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   584
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   585
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   586
    /**
58900
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   587
     * Adjusts the blocking mode if the channel is open. readLock or writeLock
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   588
     * must already be held.
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   589
     *
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   590
     * @return {@code true} if the blocking mode was adjusted, {@code false} if
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   591
     *         the blocking mode was not adjusted because the channel is closed
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   592
     */
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   593
    private boolean tryLockedConfigureBlocking(boolean block) throws IOException {
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   594
        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   595
        synchronized (stateLock) {
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   596
            if (isOpen()) {
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   597
                IOUtil.configureBlocking(fd, block);
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   598
                return true;
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   599
            } else {
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   600
                return false;
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   601
            }
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   602
        }
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   603
    }
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   604
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
   605
    /**
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   606
     * 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
   607
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   608
    InetSocketAddress localAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   609
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            return localAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   614
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   615
     * 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
   616
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   617
    InetSocketAddress remoteAddress() {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   618
        synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return remoteAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   623
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   624
    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
   625
        readLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   626
        try {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   627
            writeLock.lock();
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   628
            try {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   629
                synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   630
                    ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   631
                    if (state == ST_CONNECTIONPENDING)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   632
                        throw new ConnectionPendingException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    if (localAddress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        throw new AlreadyBoundException();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   635
                    InetSocketAddress isa = (local == null) ?
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   636
                        new InetSocketAddress(0) : Net.checkAddress(local);
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   637
                    SecurityManager sm = System.getSecurityManager();
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   638
                    if (sm != null) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   639
                        sm.checkListen(isa.getPort());
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 19607
diff changeset
   640
                    }
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 2057
diff changeset
   641
                    NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    Net.bind(fd, isa.getAddress(), isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    localAddress = Net.localAddress(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   645
            } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   646
                writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   648
        } finally {
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   649
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   651
        return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   654
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public boolean isConnected() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   656
        return (state == ST_CONNECTED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   659
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public boolean isConnectionPending() {
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   661
        return (state == ST_CONNECTIONPENDING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   664
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   665
     * 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
   666
     * @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
   667
     * @param isa the remote address
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   668
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   669
     * @throws AlreadyConnectedException if already connected
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   670
     * @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
   671
     * @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
   672
     */
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   673
    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
   674
        throws IOException
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   675
    {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   676
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   677
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   678
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   679
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   680
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   681
            ensureOpen();
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   682
            int state = this.state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (state == ST_CONNECTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                throw new AlreadyConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   685
            if (state == ST_CONNECTIONPENDING)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                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
   687
            assert state == ST_UNCONNECTED;
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
   688
            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
   689
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   690
            if (localAddress == null)
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   691
                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
   692
            remoteAddress = isa;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   693
49143
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   694
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   695
                // 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
   696
                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
   697
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   701
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   702
     * 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
   703
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   704
     * @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
   705
     * 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
   706
     * @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
   707
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   708
    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
   709
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   710
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   711
        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
   712
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   713
        if (completed) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   714
            synchronized (stateLock) {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   715
                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
   716
                    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
   717
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   718
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   719
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   720
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   721
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   722
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   723
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   724
     * Checks the remote address to which this channel is to be connected.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   725
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   726
    private InetSocketAddress checkRemote(SocketAddress sa) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   727
        InetSocketAddress isa = Net.checkAddress(sa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   728
        SecurityManager sm = System.getSecurityManager();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   729
        if (sm != null) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   730
            sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   731
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   732
        if (isa.getAddress().isAnyLocalAddress()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   733
            return new InetSocketAddress(InetAddress.getLocalHost(), isa.getPort());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   734
        } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   735
            return isa;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   736
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   737
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   738
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   739
    @Override
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   740
    public boolean connect(SocketAddress remote) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   741
        InetSocketAddress isa = checkRemote(remote);
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   742
        try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   743
            readLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   744
            try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   745
                writeLock.lock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   746
                try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   747
                    boolean blocking = isBlocking();
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   748
                    boolean connected = false;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   749
                    try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   750
                        beginConnect(blocking, isa);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   751
                        int n = Net.connect(fd, isa.getAddress(), isa.getPort());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   752
                        if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   753
                            connected = true;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   754
                        } else if (blocking) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   755
                            assert IOStatus.okayToRetry(n);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   756
                            boolean polled = false;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   757
                            while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   758
                                park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   759
                                polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   760
                            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   761
                            connected = polled && isOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   762
                        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   763
                    } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   764
                        endConnect(blocking, connected);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   765
                    }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   766
                    return connected;
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   767
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   768
                    writeLock.unlock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   769
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   770
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   771
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   773
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   774
            // 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
   775
            close();
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 50303
diff changeset
   776
            throw SocketExceptions.of(ioe, isa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   780
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   781
     * 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
   782
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   783
     * @throws ClosedChannelException if the channel is closed
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   784
     * @throws NoConnectionPendingException if no connection is pending
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   785
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   786
    private void beginFinishConnect(boolean blocking) throws ClosedChannelException {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   787
        if (blocking) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   788
            // set hook for Thread.interrupt
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   789
            begin();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   790
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   791
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   792
            ensureOpen();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   793
            if (state != ST_CONNECTIONPENDING)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   794
                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
   795
            if (blocking) {
2d5cc05d877e 8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents: 49142
diff changeset
   796
                // 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
   797
                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
   798
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   799
        }
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
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   802
    /**
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   803
     * 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
   804
     *
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   805
     * @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
   806
     * 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
   807
     * @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
   808
     */
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   809
    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
   810
        throws IOException
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   811
    {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   812
        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
   813
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   814
        if (completed) {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   815
            synchronized (stateLock) {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   816
                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
   817
                    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
   818
                    state = ST_CONNECTED;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   819
                }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   820
            }
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   821
        }
49001
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
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   824
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public boolean finishConnect() throws IOException {
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   826
        try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   827
            readLock.lock();
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   828
            try {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   829
                writeLock.lock();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   830
                try {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   831
                    // 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
   832
                    if (isConnected())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        return true;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   834
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   835
                    boolean blocking = isBlocking();
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   836
                    boolean connected = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    try {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   838
                        beginFinishConnect(blocking);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   839
                        boolean polled = Net.pollConnectNow(fd);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   840
                        if (blocking) {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   841
                            while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   842
                                park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   843
                                polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   844
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
   846
                        connected = polled && isOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   848
                        endFinishConnect(blocking, connected);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   850
                    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
   851
                    return connected;
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   852
                } finally {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   853
                    writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
48750
ffbb784a8873 8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents: 47216
diff changeset
   855
            } finally {
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   856
                readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
49141
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   858
        } catch (IOException ioe) {
ac95c7a76132 8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents: 49001
diff changeset
   859
            // 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
   860
            close();
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 50303
diff changeset
   861
            throw SocketExceptions.of(ioe, remoteAddress);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   865
    /**
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   866
     * Closes the socket if there are no I/O operations in progress and the
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   867
     * channel is not registered with a Selector.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   868
     */
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   869
    private boolean tryClose() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   870
        assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   871
        if ((readerThread == 0) && (writerThread == 0) && !isRegistered()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   872
            state = ST_CLOSED;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   873
            nd.close(fd);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   874
            return true;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   875
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   876
            return false;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   877
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   878
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   879
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   880
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   881
     * Invokes tryClose to attempt to close the socket.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   882
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   883
     * This method is used for deferred closing by I/O and Selector operations.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   884
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   885
    private void tryFinishClose() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   886
        try {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   887
            tryClose();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   888
        } catch (IOException ignore) { }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   889
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   890
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   891
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   892
     * Closes this channel when configured in blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   893
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   894
     * If there is an I/O operation in progress then the socket is pre-closed
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   895
     * and the I/O threads signalled, in which case the final close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   896
     * until all I/O operations complete.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   897
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   898
     * Note that a channel configured blocking may be registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   899
     * This arises when a key is canceled and the channel configured to blocking
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   900
     * mode before the key is flushed from the Selector.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   901
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   902
    private void implCloseBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   903
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   904
            assert state < ST_CLOSING;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   905
            state = ST_CLOSING;
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   906
            if (!tryClose()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   907
                long reader = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   908
                long writer = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   909
                if (reader != 0 || writer != 0) {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   910
                    nd.preClose(fd);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   911
                    if (reader != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   912
                        NativeThread.signal(reader);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   913
                    if (writer != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   914
                        NativeThread.signal(writer);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   915
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   916
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   917
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   918
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   919
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   920
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   921
     * Closes this channel when configured in non-blocking mode.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   922
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   923
     * If the channel is registered with a Selector then the close is deferred
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   924
     * until the channel is flushed from all Selectors.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   925
     *
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   926
     * If the socket is connected and the channel is registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   927
     * then the socket is shutdown for writing so that the peer reads EOF. In
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   928
     * addition, if SO_LINGER is set to a non-zero value then it is disabled so
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   929
     * that the deferred close does not wait.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   930
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   931
    private void implCloseNonBlockingMode() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   932
        boolean connected;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   933
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   934
            assert state < ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   935
            connected = (state == ST_CONNECTED);
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   936
            state = ST_CLOSING;
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   937
        }
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   938
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   939
        // wait for any read/write operations to complete
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   940
        readLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   941
        readLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   942
        writeLock.lock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   943
        writeLock.unlock();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   944
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   945
        // if the socket cannot be closed because it's registered with a Selector
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   946
        // then shutdown the socket for writing.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   947
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   948
            if (state == ST_CLOSING && !tryClose() && connected && isRegistered()) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   949
                try {
50109
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   950
                    SocketOption<Integer> opt = StandardSocketOptions.SO_LINGER;
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   951
                    int interval = (int) Net.getSocketOption(fd, Net.UNSPEC, opt);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   952
                    if (interval != 0) {
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   953
                        if (interval > 0) {
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   954
                            // disable SO_LINGER
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   955
                            Net.setSocketOption(fd, Net.UNSPEC, opt, -1);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   956
                        }
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   957
                        Net.shutdown(fd, Net.SHUT_WR);
ea54197f4fe4 8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents: 49702
diff changeset
   958
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   959
                } catch (IOException ignore) { }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   960
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   961
        }
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   962
    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   963
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   964
    /**
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   965
     * Invoked by implCloseChannel to close the channel.
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   966
     */
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   967
    @Override
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   968
    protected void implCloseSelectableChannel() throws IOException {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   969
        assert !isOpen();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   970
        if (isBlocking()) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   971
            implCloseBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   972
        } else {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   973
            implCloseNonBlockingMode();
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   974
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   975
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   976
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   977
    @Override
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   978
    public void kill() {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   979
        synchronized (stateLock) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   980
            if (state == ST_CLOSING) {
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   981
                tryFinishClose();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   982
            }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   983
        }
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
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   986
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   987
    public SocketChannel shutdownInput() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
   988
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   989
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   990
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   991
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   992
            if (!isInputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   993
                Net.shutdown(fd, Net.SHUT_RD);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   994
                long thread = readerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   995
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   996
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
   997
                isInputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   998
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   999
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1003
    @Override
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1004
    public SocketChannel shutdownOutput() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1005
        synchronized (stateLock) {
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1006
            ensureOpen();
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1007
            if (!isConnected())
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1008
                throw new NotYetConnectedException();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1009
            if (!isOutputClosed) {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1010
                Net.shutdown(fd, Net.SHUT_WR);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1011
                long thread = writerThread;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1012
                if (thread != 0)
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1013
                    NativeThread.signal(thread);
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1014
                isOutputClosed = true;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1015
            }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
  1016
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1020
    boolean isInputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1021
        return !isInputClosed;
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1022
    }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1023
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1024
    boolean isOutputOpen() {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1025
        return !isOutputClosed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1028
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1029
     * Waits for a connection attempt to finish with a timeout
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1030
     * @throws SocketTimeoutException if the connect timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1031
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1032
    private boolean finishTimedConnect(long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1033
        long startNanos = System.nanoTime();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1034
        boolean polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1035
        while (!polled && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1036
            long remainingNanos = nanos - (System.nanoTime() - startNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1037
            if (remainingNanos <= 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1038
                throw new SocketTimeoutException("Connect timed out");
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1039
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1040
            park(Net.POLLOUT, remainingNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1041
            polled = Net.pollConnectNow(fd);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1042
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1043
        return polled && isOpen();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1044
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1045
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1046
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1047
     * Attempts to establish a connection to the given socket address with a
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1048
     * timeout. Closes the socket if connection cannot be established.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1049
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1050
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1051
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1052
     * @throws IllegalBlockingModeException if the channel is non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1053
     * @throws SocketTimeoutException if the read timeout elapses
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1054
     */
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1055
    void blockingConnect(SocketAddress remote, long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1056
        InetSocketAddress isa = checkRemote(remote);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1057
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1058
            readLock.lock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1059
            try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1060
                writeLock.lock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1061
                try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1062
                    if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1063
                        throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1064
                    boolean connected = false;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1065
                    try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1066
                        beginConnect(true, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1067
                        // change socket to non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1068
                        lockedConfigureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1069
                        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1070
                            int n = Net.connect(fd, isa.getAddress(), isa.getPort());
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1071
                            connected = (n > 0) ? true : finishTimedConnect(nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1072
                        } finally {
58900
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
  1073
                            // restore socket to blocking mode (if channel is open)
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
  1074
                            tryLockedConfigureBlocking(true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1075
                        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1076
                    } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1077
                        endConnect(true, connected);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1078
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1079
                } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1080
                    writeLock.unlock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1081
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1082
            } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1083
                readLock.unlock();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1084
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1085
        } catch (IOException ioe) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1086
            // connect failed, close the channel
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1087
            close();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1088
            throw SocketExceptions.of(ioe, isa);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1089
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1090
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1091
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1092
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1093
     * Attempts to read bytes from the socket into the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1094
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1095
    private int tryRead(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1096
        ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1097
        assert dst.position() == 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1098
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1099
            int n = nd.read(fd, ((DirectBuffer)dst).address(), len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1100
            if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1101
                dst.get(b, off, n);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1102
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1103
            return n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1104
        } finally{
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1105
            Util.offerFirstTemporaryDirectBuffer(dst);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1106
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1107
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1108
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1109
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1110
     * Reads bytes from the socket into the given byte array with a timeout.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1111
     * @throws SocketTimeoutException if the read timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1112
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1113
    private int timedRead(byte[] b, int off, int len, long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1114
        long startNanos = System.nanoTime();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1115
        int n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1116
        while (n == IOStatus.UNAVAILABLE && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1117
            long remainingNanos = nanos - (System.nanoTime() - startNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1118
            if (remainingNanos <= 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1119
                throw new SocketTimeoutException("Read timed out");
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1120
            }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1121
            park(Net.POLLIN, remainingNanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1122
            n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1123
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1124
        return n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1125
    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1126
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1127
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1128
     * Reads bytes from the socket into the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1129
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1130
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1131
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1132
     * @throws IllegalBlockingModeException if the channel is non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1133
     * @throws SocketTimeoutException if the read timeout elapses
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1134
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1135
    int blockingRead(byte[] b, int off, int len, long nanos) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1136
        Objects.checkFromIndexSize(off, len, b.length);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1137
        if (len == 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1138
            // nothing to do
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1139
            return 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1140
        }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1141
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1142
        readLock.lock();
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1143
        try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1144
            // check that channel is configured blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1145
            if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1146
                throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1147
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1148
            int n = 0;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1149
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1150
                beginRead(true);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1151
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1152
                // check if connection has been reset
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1153
                if (connectionReset)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1154
                    throwConnectionReset();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1155
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1156
                // check if input is shutdown
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1157
                if (isInputClosed)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1158
                    return IOStatus.EOF;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1159
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1160
                if (nanos > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1161
                    // change socket to non-blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1162
                    lockedConfigureBlocking(false);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1163
                    try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1164
                        n = timedRead(b, off, len, nanos);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1165
                    } finally {
58900
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
  1166
                        // restore socket to blocking mode (if channel is open)
434329f6f456 8233430: (sc) Socket adaptor restoring of blocking mode can override exception if socket closed
alanb
parents: 55081
diff changeset
  1167
                        tryLockedConfigureBlocking(true);
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1168
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1169
                } else {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1170
                    // read, no timeout
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1171
                    n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1172
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1173
                        park(Net.POLLIN);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1174
                        n = tryRead(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1175
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1176
                }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1177
            } catch (ConnectionResetException e) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1178
                connectionReset = true;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1179
                throwConnectionReset();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1180
            } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1181
                endRead(true, n > 0);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1182
                if (n <= 0 && isInputClosed)
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1183
                    return IOStatus.EOF;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1184
            }
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1185
            assert n > 0 || n == -1;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1186
            return n;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1187
        } finally {
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1188
            readLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1192
    /**
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1193
     * Attempts to write a sequence of bytes to the socket from the given
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1194
     * byte array.
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1195
     */
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1196
    private int tryWrite(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1197
        ByteBuffer src = Util.getTemporaryDirectBuffer(len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1198
        assert src.position() == 0;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1199
        try {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1200
            src.put(b, off, len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1201
            return nd.write(fd, ((DirectBuffer)src).address(), len);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1202
        } finally {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1203
            Util.offerFirstTemporaryDirectBuffer(src);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1204
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1205
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1207
    /**
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1208
     * Writes a sequence of bytes to the socket from the given byte array.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1209
     *
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1210
     * @apiNote This method is for use by the socket adaptor.
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1211
     */
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1212
    void blockingWriteFully(byte[] b, int off, int len) throws IOException {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1213
        Objects.checkFromIndexSize(off, len, b.length);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1214
        if (len == 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1215
            // nothing to do
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1216
            return;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1217
        }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1218
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1219
        writeLock.lock();
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1220
        try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1221
            // check that channel is configured blocking
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1222
            if (!isBlocking())
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1223
                throw new IllegalBlockingModeException();
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1224
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1225
            // loop until all bytes have been written
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1226
            int pos = off;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1227
            int end = off + len;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1228
            beginWrite(true);
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1229
            try {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1230
                while (pos < end && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1231
                    int size = end - pos;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1232
                    int n = tryWrite(b, pos, size);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1233
                    while (IOStatus.okayToRetry(n) && isOpen()) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1234
                        park(Net.POLLOUT);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1235
                        n = tryWrite(b, pos, size);
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1236
                    }
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1237
                    if (n > 0) {
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1238
                        pos += n;
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1239
                    }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1240
                }
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1241
            } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1242
                endWrite(true, pos >= end);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            }
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1244
        } finally {
54620
13b67c1420b8 8222774: (ch) Replace uses of stateLock and blockingLock with j.u.c. locks
alanb
parents: 54246
diff changeset
  1245
            writeLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    /**
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1250
     * Return the number of bytes in the socket input buffer.
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1251
     */
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1252
    int available() throws IOException {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1253
        synchronized (stateLock) {
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1254
            ensureOpenAndConnected();
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1255
            if (isInputClosed) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1256
                return 0;
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1257
            } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1258
                return Net.available(fd);
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1259
            }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1260
        }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1261
    }
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1262
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 53445
diff changeset
  1263
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * Translates native poll revent ops into a ready operation ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1266
    public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1267
        int intOps = ski.nioInterestOps();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1268
        int oldOps = ski.nioReadyOps();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1271
        if ((ops & Net.POLLNVAL) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            // This should only happen if this channel is pre-closed while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            // selection operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            // ## Throw an error if this channel has not been pre-closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1278
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            newOps = intOps;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1280
            ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1284
        boolean connected = isConnected();
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1285
        if (((ops & Net.POLLIN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1286
            ((intOps & SelectionKey.OP_READ) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1289
        if (((ops & Net.POLLCONN) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1290
            ((intOps & SelectionKey.OP_CONNECT) != 0) && isConnectionPending())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            newOps |= SelectionKey.OP_CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1293
        if (((ops & Net.POLLOUT) != 0) &&
49142
4affaea00c05 8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents: 49141
diff changeset
  1294
            ((intOps & SelectionKey.OP_WRITE) != 0) && connected)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1297
        ski.nioReadyOps(newOps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1301
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1302
        return translateReadyOps(ops, ski.nioReadyOps(), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1305
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1306
        return translateReadyOps(ops, 0, ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * Translates an interest operation set into a native poll event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     */
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1312
    public int translateInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        int newOps = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1315
            newOps |= Net.POLLIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1317
            newOps |= Net.POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        if ((ops & SelectionKey.OP_CONNECT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
  1319
            newOps |= Net.POLLCONN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49143
diff changeset
  1320
        return newOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1331
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23879
diff changeset
  1333
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        sb.append(this.getClass().getSuperclass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            sb.append("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        else {
54754
193a8f1a4f3b 8223353: (ch) Change channel close implementation to not wait for I/O threads
alanb
parents: 54620
diff changeset
  1339
            synchronized (stateLock) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    sb.append("unconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    break;
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1344
                case ST_CONNECTIONPENDING:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    sb.append("connection-pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    sb.append("connected");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1349
                    if (isInputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                        sb.append(" ishut");
49001
ce06058197a4 8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents: 48761
diff changeset
  1351
                    if (isOutputClosed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                        sb.append(" oshut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1355
                InetSocketAddress addr = localAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1356
                if (addr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    sb.append(" local=");
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 18192
diff changeset
  1358
                    sb.append(Net.getRevealedLocalAddressAsString(addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                if (remoteAddress() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    sb.append(" remote=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    sb.append(remoteAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
}