src/java.base/share/classes/sun/nio/ch/SelectorImpl.java
author alanb
Sat, 14 Apr 2018 08:41:42 +0100
changeset 49702 09c01737ad27
parent 49526 cad4c844902a
child 49802 8ac08fa69f00
permissions -rw-r--r--
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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.IOException;
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    29
import java.nio.channels.ClosedSelectorException;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    30
import java.nio.channels.IllegalSelectorException;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    31
import java.nio.channels.SelectableChannel;
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    32
import java.nio.channels.SelectionKey;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    33
import java.nio.channels.spi.AbstractSelectableChannel;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    34
import java.nio.channels.spi.AbstractSelector;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    35
import java.nio.channels.spi.SelectorProvider;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    36
import java.util.Collections;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    37
import java.util.HashSet;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    38
import java.util.Iterator;
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    39
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Base Selector implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    46
public abstract class SelectorImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    extends AbstractSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    49
    // The set of keys registered with this Selector
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    50
    protected final Set<SelectionKey> keys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // The set of keys with data ready for an operation
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    53
    protected final Set<SelectionKey> selectedKeys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Public views of the key sets
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    56
    private final Set<SelectionKey> publicKeys;             // Immutable
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    57
    private final Set<SelectionKey> publicSelectedKeys;     // Removal allowed, but not addition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected SelectorImpl(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        super(sp);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    61
        keys = new HashSet<>();
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    62
        selectedKeys = new HashSet<>();
46094
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    63
        publicKeys = Collections.unmodifiableSet(keys);
0c23b05caf7d 8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents: 32649
diff changeset
    64
        publicSelectedKeys = Util.ungrowableSet(selectedKeys);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    67
    private void ensureOpen() {
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    68
        if (!isOpen())
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    69
            throw new ClosedSelectorException();
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    70
    }
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    71
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    72
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    73
    public final Set<SelectionKey> keys() {
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    74
        ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return publicKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    78
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    79
    public final Set<SelectionKey> selectedKeys() {
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    80
        ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return publicSelectedKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    84
    /**
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    85
     * Returns the public view of the key sets
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    86
     */
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    87
    protected final Set<SelectionKey> nioKeys() {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    88
        return publicKeys;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    89
    }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    90
    protected final Set<SelectionKey> nioSelectedKeys() {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    91
        return publicSelectedKeys;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    92
    }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    93
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    94
    /**
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    95
     * Marks the beginning of a select operation that might block
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    96
     */
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    97
    protected final void begin(boolean blocking) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    98
        if (blocking) begin();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    99
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   100
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   101
    /**
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   102
     * Marks the end of a select operation that may have blocked
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   103
     */
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   104
    protected final void end(boolean blocking) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   105
        if (blocking) end();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   106
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   107
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   108
    /**
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   109
     * Selects the keys for channels that are ready for I/O operations.
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   110
     *
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   111
     * @param timeout timeout in milliseconds to wait, 0 to not wait, -1 to
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   112
     *                wait indefinitely
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   113
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected abstract int doSelect(long timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int lockAndDoSelect(long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        synchronized (this) {
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   118
            ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            synchronized (publicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                synchronized (publicSelectedKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    return doSelect(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   127
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   128
    public final int select(long timeout)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IllegalArgumentException("Negative timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return lockAndDoSelect((timeout == 0) ? -1 : timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   136
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   137
    public final int select() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return select(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   141
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   142
    public final int selectNow() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return lockAndDoSelect(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   146
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   147
    public final void implCloseSelector() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        synchronized (this) {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   150
            implClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            synchronized (publicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                synchronized (publicSelectedKeys) {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   153
                    // Deregister channels
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   154
                    Iterator<SelectionKey> i = keys.iterator();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   155
                    while (i.hasNext()) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   156
                        SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   157
                        deregister(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   158
                        SelectableChannel selch = ski.channel();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   159
                        if (!selch.isOpen() && !selch.isRegistered())
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   160
                            ((SelChImpl)selch).kill();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   161
                        selectedKeys.remove(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   162
                        i.remove();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   163
                    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   164
                    assert selectedKeys.isEmpty() && keys.isEmpty();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
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
    protected abstract void implClose() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   172
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected final SelectionKey register(AbstractSelectableChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                          int ops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                          Object attachment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (!(ch instanceof SelChImpl))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new IllegalSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        k.attach(attachment);
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   181
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   182
        // register with selector (if needed) before adding to key set
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   183
        implRegister(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        synchronized (publicKeys) {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   185
            keys.add(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        k.interestOps(ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   191
    /**
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   192
     * Register the key in the selector.
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   193
     *
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   194
     * The default implementation checks if the selector is open. It should
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   195
     * be overridden by selector implementations as needed.
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   196
     */
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   197
    protected void implRegister(SelectionKeyImpl ski) {
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   198
        ensureOpen();
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   199
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   201
    protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   202
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   203
    protected final void processDeregisterQueue() throws IOException {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   204
        assert Thread.holdsLock(this);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   205
        assert Thread.holdsLock(publicKeys);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   206
        assert Thread.holdsLock(publicSelectedKeys);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   207
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   208
        Set<SelectionKey> cks = cancelledKeys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        synchronized (cks) {
843
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   210
            if (!cks.isEmpty()) {
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   211
                Iterator<SelectionKey> i = cks.iterator();
843
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   212
                while (i.hasNext()) {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   213
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   214
                    i.remove();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   215
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   216
                    // remove the key from the selector
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   217
                    implDereg(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   218
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   219
                    selectedKeys.remove(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   220
                    keys.remove(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   221
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   222
                    // remove from channel's key set
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   223
                    deregister(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   224
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   225
                    SelectableChannel ch = ski.channel();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   226
                    if (!ch.isOpen() && !ch.isRegistered())
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   227
                        ((SelChImpl)ch).kill();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   232
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   233
    /**
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   234
     * Change the event set in the selector
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   235
     */
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   236
    protected abstract void setEventOps(SelectionKeyImpl ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
}