src/java.base/share/classes/java/nio/channels/spi/AbstractSelectableChannel.java
author alanb
Wed, 20 Nov 2019 08:35:53 +0000
changeset 59146 455612b3161a
parent 49802 8ac08fa69f00
permissions -rw-r--r--
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS) Reviewed-by: dfuchs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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 java.nio.channels.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    29
import java.nio.channels.CancelledKeyException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    30
import java.nio.channels.ClosedChannelException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    31
import java.nio.channels.ClosedSelectorException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    32
import java.nio.channels.IllegalBlockingModeException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    33
import java.nio.channels.IllegalSelectorException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    34
import java.nio.channels.SelectableChannel;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    35
import java.nio.channels.SelectionKey;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    36
import java.nio.channels.Selector;
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
    37
import java.util.Arrays;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
    38
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Base implementation class for selectable channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> This class defines methods that handle the mechanics of channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * registration, deregistration, and closing.  It maintains the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * blocking mode of this channel as well as its current set of selection keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * It performs all of the synchronization required to implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * java.nio.channels.SelectableChannel} specification.  Implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * abstract protected methods defined in this class need not synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * against other threads that might be engaged in the same operations.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class AbstractSelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    extends SelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final SelectorProvider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Keys that have been created by registering this channel with selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // They are saved because if this channel is closed the keys must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // deregistered.  Protected by keyLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private SelectionKey[] keys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int keyCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Lock for key set and count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final Object keyLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Lock for registration and configureBlocking operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final Object regLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    79
    // True when non-blocking, need regLock to change;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    80
    private volatile boolean nonBlocking;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Initializes a new instance of this class.
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
    84
     *
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
    85
     * @param  provider
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
    86
     *         The provider that created this channel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected AbstractSelectableChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns the provider that created this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return  The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public final SelectorProvider provider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // -- Utility methods for the key set --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private void addKey(SelectionKey k) {
13582
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   105
        assert Thread.holdsLock(keyLock);
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   106
        int i = 0;
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   107
        if ((keys != null) && (keyCount < keys.length)) {
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   108
            // Find empty element of key array
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   109
            for (i = 0; i < keys.length; i++)
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   110
                if (keys[i] == null)
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   111
                    break;
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   112
        } else if (keys == null) {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   113
            keys = new SelectionKey[2];
13582
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   114
        } else {
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   115
            // Grow key array
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   116
            int n = keys.length * 2;
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   117
            SelectionKey[] ks =  new SelectionKey[n];
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   118
            for (i = 0; i < keys.length; i++)
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   119
                ks[i] = keys[i];
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   120
            keys = ks;
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   121
            i = keyCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
13582
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   123
        keys[i] = k;
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   124
        keyCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private SelectionKey findKey(Selector sel) {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   128
        assert Thread.holdsLock(keyLock);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   129
        if (keys == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return null;
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   131
        for (int i = 0; i < keys.length; i++)
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   132
            if ((keys[i] != null) && (keys[i].selector() == sel))
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   133
                return keys[i];
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   134
        return null;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void removeKey(SelectionKey k) {                    // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            for (int i = 0; i < keys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (keys[i] == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    keys[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    keyCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            ((AbstractSelectionKey)k).invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private boolean haveValidKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (keyCount == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if ((keys[i] != null) && keys[i].isValid())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // -- Registration --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final boolean isRegistered() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return keyCount != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public final SelectionKey keyFor(Selector sel) {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   171
        synchronized (keyLock) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   172
            return findKey(sel);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   173
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
59146
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   177
     * Invokes an action for each key.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   178
     *
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   179
     * This method is invoked by DatagramChannelImpl::disconnect.
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   180
     */
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   181
    private void forEach(Consumer<SelectionKey> action) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   182
        synchronized (keyLock) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   183
            SelectionKey[] keys = this.keys;
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   184
            if (keys != null) {
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   185
                Arrays.stream(keys).filter(k -> k != null).forEach(action::accept);
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   186
            }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   187
        }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   188
    }
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   189
455612b3161a 8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents: 49802
diff changeset
   190
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Registers this channel with the given selector, returning a selection key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>  This method first verifies that this channel is open and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * given initial interest set is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> If this channel is already registered with the given selector then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the selection key representing that registration is returned after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * setting its interest set to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p> Otherwise this channel has not yet been registered with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * selector, so the {@link AbstractSelector#register register} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * the selector is invoked while holding the appropriate locks.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * resulting key is added to this channel's key set before being returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * </p>
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   205
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   206
     * @throws  ClosedSelectorException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   207
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   208
     * @throws  IllegalBlockingModeException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   209
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   210
     * @throws  IllegalSelectorException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   211
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   212
     * @throws  CancelledKeyException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   213
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   214
     * @throws  IllegalArgumentException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   216
    public final SelectionKey register(Selector sel, int ops, Object att)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        throws ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   219
        if ((ops & ~validOps()) != 0)
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   220
            throw new IllegalArgumentException();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   221
        if (!isOpen())
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   222
            throw new ClosedChannelException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        synchronized (regLock) {
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   224
            if (isBlocking())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                throw new IllegalBlockingModeException();
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   226
            synchronized (keyLock) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   227
                // re-check if channel has been closed
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   228
                if (!isOpen())
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   229
                    throw new ClosedChannelException();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   230
                SelectionKey k = findKey(sel);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   231
                if (k != null) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   232
                    k.attach(att);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   233
                    k.interestOps(ops);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   234
                } else {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   235
                    // New registration
13582
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   236
                    k = ((AbstractSelector)sel).register(this, ops, att);
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   237
                    addKey(k);
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   238
                }
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   239
                return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // -- Closing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Closes this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p> This method, which is specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * AbstractInterruptibleChannel} class and is invoked by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * java.nio.channels.Channel#close close} method, in turn invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * {@link #implCloseSelectableChannel implCloseSelectableChannel} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * order to perform the actual work of closing this channel.  It then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * cancels all of this channel's keys.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    protected final void implCloseChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        implCloseSelectableChannel();
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   259
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   260
        // clone keys to avoid calling cancel when holding keyLock
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   261
        SelectionKey[] copyOfKeys = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        synchronized (keyLock) {
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   263
            if (keys != null) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   264
                copyOfKeys = keys.clone();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   265
            }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   266
        }
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   267
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   268
        if (copyOfKeys != null) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   269
            for (SelectionKey k : copyOfKeys) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   270
                if (k != null) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   271
                    k.cancel();   // invalidate and adds key to cancelledKey set
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 48761
diff changeset
   272
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Closes this selectable channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p> This method is invoked by the {@link java.nio.channels.Channel#close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * close} method in order to perform the actual work of closing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * channel.  This method is only invoked if the channel has not yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * closed, and it is never invoked more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p> An implementation of this method must arrange for any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * that is blocked in an I/O operation upon this channel to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * immediately, either by throwing an exception or by returning normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * </p>
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
   289
     *
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
   290
     * @throws  IOException
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
   291
     *          If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    protected abstract void implCloseSelectableChannel() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // -- Blocking --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public final boolean isBlocking() {
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   299
        return !nonBlocking;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public final Object blockingLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return regLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Adjusts this channel's blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p> If the given blocking mode is different from the current blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * mode then this method invokes the {@link #implConfigureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * implConfigureBlocking} method, while holding the appropriate locks, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * order to change the mode.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public final SelectableChannel configureBlocking(boolean block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        synchronized (regLock) {
13582
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   318
            if (!isOpen())
16f0af616ea0 7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents: 5506
diff changeset
   319
                throw new ClosedChannelException();
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   320
            boolean blocking = !nonBlocking;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   321
            if (block != blocking) {
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   322
                if (block && haveValidKeys())
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   323
                    throw new IllegalBlockingModeException();
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   324
                implConfigureBlocking(block);
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   325
                nonBlocking = !block;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   326
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Adjusts this channel's blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p> This method is invoked by the {@link #configureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * configureBlocking} method in order to perform the actual work of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * changing the blocking mode.  This method is only invoked if the new mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * is different from the current mode.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   339
     * @param  block  If {@code true} then this channel will be placed in
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   340
     *                blocking mode; if {@code false} then it will be placed
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
   341
     *                non-blocking mode
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 14342
diff changeset
   342
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected abstract void implConfigureBlocking(boolean block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}