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