src/java.base/share/classes/sun/nio/ch/SelectorImpl.java
author alanb
Sat, 16 Jun 2018 10:10:54 +0100
changeset 50602 ed8de3d0cd28
parent 49802 8ac08fa69f00
child 58439 b25362cec8ce
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set 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;
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    39
import java.util.Objects;
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
    40
import java.util.Set;
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
    41
import java.util.concurrent.ConcurrentHashMap;
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    42
import java.util.function.Consumer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Base Selector implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
    49
abstract class SelectorImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    extends AbstractSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    52
    // The set of keys registered with this Selector
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
    53
    private final Set<SelectionKey> keys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // The set of keys with data ready for an operation
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    56
    private final Set<SelectionKey> selectedKeys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Public views of the key sets
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    59
    private final Set<SelectionKey> publicKeys;             // Immutable
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    60
    private final Set<SelectionKey> publicSelectedKeys;     // Removal allowed, but not addition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    62
    // used to check for reentrancy
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    63
    private boolean inSelect;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected SelectorImpl(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super(sp);
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
    67
        keys = ConcurrentHashMap.newKeySet();
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    68
        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
    69
        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
    70
        publicSelectedKeys = Util.ungrowableSet(selectedKeys);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    73
    private void ensureOpen() {
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    74
        if (!isOpen())
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    75
            throw new ClosedSelectorException();
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    76
    }
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
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> keys() {
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 publicKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    84
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    85
    public final Set<SelectionKey> selectedKeys() {
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    86
        ensureOpen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return publicSelectedKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    90
    /**
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    91
     * Marks the beginning of a select operation that might block
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    92
     */
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    93
    protected final void begin(boolean blocking) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    94
        if (blocking) begin();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    95
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    96
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    97
    /**
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    98
     * Marks the end of a select operation that may have blocked
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    99
     */
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   100
    protected final void end(boolean blocking) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   101
        if (blocking) end();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   102
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   103
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   104
    /**
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   105
     * Selects the keys for channels that are ready for I/O operations.
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   106
     *
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   107
     * @param action  the action to perform, can be null
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   108
     * @param timeout timeout in milliseconds to wait, 0 to not wait, -1 to
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   109
     *                wait indefinitely
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   110
     */
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   111
    protected abstract int doSelect(Consumer<SelectionKey> action, long timeout)
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   112
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   114
    private int lockAndDoSelect(Consumer<SelectionKey> action, long timeout)
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   115
        throws IOException
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   116
    {
2
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();
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   119
            if (inSelect)
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   120
                throw new IllegalStateException("select in progress");
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   121
            inSelect = true;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   122
            try {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   123
                synchronized (publicSelectedKeys) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   124
                    return doSelect(action, timeout);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   125
                }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   126
            } finally {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   127
                inSelect = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   132
    @Override
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   133
    public final int select(long timeout) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new IllegalArgumentException("Negative timeout");
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   136
        return lockAndDoSelect(null, (timeout == 0) ? -1 : timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   139
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   140
    public final int select() throws IOException {
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   141
        return lockAndDoSelect(null, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   144
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   145
    public final int selectNow() throws IOException {
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   146
        return lockAndDoSelect(null, 0);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   147
    }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   148
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   149
    @Override
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   150
    public final int select(Consumer<SelectionKey> action, long timeout)
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   151
        throws IOException
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   152
    {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   153
        Objects.requireNonNull(action);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   154
        if (timeout < 0)
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   155
            throw new IllegalArgumentException("Negative timeout");
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   156
        return lockAndDoSelect(action, (timeout == 0) ? -1 : timeout);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   157
    }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   158
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   159
    @Override
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   160
    public final int select(Consumer<SelectionKey> action) throws IOException {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   161
        Objects.requireNonNull(action);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   162
        return lockAndDoSelect(action, -1);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   163
    }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   164
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   165
    @Override
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   166
    public final int selectNow(Consumer<SelectionKey> action) throws IOException {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   167
        Objects.requireNonNull(action);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   168
        return lockAndDoSelect(action, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   171
    /**
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   172
     * Invoked by implCloseSelector to close the selector.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   173
     */
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   174
    protected abstract void implClose() throws IOException;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   175
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   176
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   177
    public final void implCloseSelector() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        synchronized (this) {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   180
            implClose();
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   181
            synchronized (publicSelectedKeys) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   182
                // Deregister channels
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   183
                Iterator<SelectionKey> i = keys.iterator();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   184
                while (i.hasNext()) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   185
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   186
                    deregister(ski);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   187
                    SelectableChannel selch = ski.channel();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   188
                    if (!selch.isOpen() && !selch.isRegistered())
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   189
                        ((SelChImpl)selch).kill();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   190
                    selectedKeys.remove(ski);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   191
                    i.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   193
                assert selectedKeys.isEmpty() && keys.isEmpty();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   198
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected final SelectionKey register(AbstractSelectableChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                          int ops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                          Object attachment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (!(ch instanceof SelChImpl))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IllegalSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        k.attach(attachment);
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   207
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   208
        // register (if needed) before adding to key set
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   209
        implRegister(k);
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   210
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   211
        // add to the selector's key set, removing it immediately if the selector
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   212
        // is closed. The key is not in the channel's key set at this point but
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   213
        // it may be observed by a thread iterating over the selector's key set.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   214
        keys.add(k);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   215
        try {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   216
            k.interestOps(ops);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   217
        } catch (ClosedSelectorException e) {
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   218
            assert ch.keyFor(this) == null;
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   219
            keys.remove(k);
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   220
            k.cancel();
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   221
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   226
    /**
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   227
     * Register the key in the selector.
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   228
     *
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   229
     * The default implementation checks if the selector is open. It should
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   230
     * be overridden by selector implementations as needed.
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   231
     */
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   232
    protected void implRegister(SelectionKeyImpl ski) {
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   233
        ensureOpen();
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   234
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   236
    /**
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   237
     * Removes the key from the selector
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   238
     */
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   239
    protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   240
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   241
    /**
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   242
     * Invoked by selection operations to process the cancelled-key set
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   243
     */
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   244
    protected final void processDeregisterQueue() throws IOException {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   245
        assert Thread.holdsLock(this);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   246
        assert Thread.holdsLock(publicSelectedKeys);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   247
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   248
        Set<SelectionKey> cks = cancelledKeys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        synchronized (cks) {
843
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   250
            if (!cks.isEmpty()) {
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   251
                Iterator<SelectionKey> i = cks.iterator();
843
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   252
                while (i.hasNext()) {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   253
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   254
                    i.remove();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   255
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   256
                    // remove the key from the selector
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   257
                    implDereg(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   258
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   259
                    selectedKeys.remove(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   260
                    keys.remove(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   261
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   262
                    // remove from channel's key set
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   263
                    deregister(ski);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   264
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   265
                    SelectableChannel ch = ski.channel();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   266
                    if (!ch.isOpen() && !ch.isRegistered())
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   267
                        ((SelChImpl)ch).kill();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   272
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   273
    /**
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   274
     * Invoked by selection operations to handle ready events. If an action
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   275
     * is specified then it is invoked to handle the key, otherwise the key
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   276
     * is added to the selected-key set (or updated when it is already in the
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   277
     * set).
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   278
     */
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   279
    protected final int processReadyEvents(int rOps,
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   280
                                           SelectionKeyImpl ski,
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   281
                                           Consumer<SelectionKey> action) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   282
        if (action != null) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   283
            ski.translateAndSetReadyOps(rOps);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   284
            if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   285
                action.accept(ski);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   286
                ensureOpen();
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   287
                return 1;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   288
            }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   289
        } else {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   290
            assert Thread.holdsLock(publicSelectedKeys);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   291
            if (selectedKeys.contains(ski)) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   292
                if (ski.translateAndUpdateReadyOps(rOps)) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   293
                    return 1;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   294
                }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   295
            } else {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   296
                ski.translateAndSetReadyOps(rOps);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   297
                if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   298
                    selectedKeys.add(ski);
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   299
                    return 1;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   300
                }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   301
            }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   302
        }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   303
        return 0;
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   304
    }
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   305
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 49802
diff changeset
   306
    /**
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   307
     * Invoked by interestOps to ensure the interest ops are updated at the
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 49526
diff changeset
   308
     * next selection operation.
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   309
     */
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
   310
    protected abstract void setEventOps(SelectionKeyImpl ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}