src/java.base/share/classes/java/nio/channels/spi/AbstractSelector.java
author alanb
Wed, 02 Oct 2019 09:16:18 +0100
changeset 58439 b25362cec8ce
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231603: (se) Selector implementations do not need to use cancelledKeys Reviewed-by: chegar, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
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: 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 java.nio.channels.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    29
import java.lang.invoke.MethodHandles;
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    30
import java.lang.invoke.VarHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.SelectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.Selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.nio.ch.Interruptible;
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    36
import sun.nio.ch.SelectorImpl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Base implementation class for selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> This class encapsulates the low-level machinery required to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the interruption of selection operations.  A concrete selector class must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * invoke the {@link #begin begin} and {@link #end end} methods before and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * after, respectively, invoking an I/O operation that might block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * indefinitely.  In order to ensure that the {@link #end end} method is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * invoked, these methods should be used within a
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    48
 * {@code try}&nbsp;...&nbsp;{@code finally} block:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46054
diff changeset
    50
 * <blockquote><pre id="be">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     // Perform blocking I/O operation here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> This class also defines methods for maintaining a selector's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * cancelled-key set and for removing a key from its channel's key set, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * declares the abstract {@link #register register} method that is invoked by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * selectable channel's {@link AbstractSelectableChannel#register register}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * method in order to perform the actual work of registering a channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public abstract class AbstractSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    extends Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    74
    private static final VarHandle CLOSED;
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    75
    static {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    76
        try {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    77
            MethodHandles.Lookup l = MethodHandles.lookup();
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    78
            CLOSED = l.findVarHandle(AbstractSelector.class, "closed", boolean.class);
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    79
        } catch (Exception e) {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    80
            throw new InternalError(e);
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    81
        }
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    82
    }
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    83
    private volatile boolean closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // The provider that created this selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final SelectorProvider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    88
    // cancelled-key, not used by the JDK Selector implementations
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    89
    private final Set<SelectionKey> cancelledKeys;
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    90
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 7668
diff changeset
    92
     * Initializes a new instance of this class.
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    93
     *
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    94
     * @param  provider
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    95
     *         The provider that created this selector
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected AbstractSelector(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.provider = provider;
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
    99
        if (this instanceof SelectorImpl) {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   100
            // not used in JDK Selector implementations
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   101
            this.cancelledKeys = Set.of();
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   102
        } else {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   103
            this.cancelledKeys = new HashSet<>();
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   104
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    void cancel(SelectionKey k) {                       // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        synchronized (cancelledKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            cancelledKeys.add(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Closes this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p> If the selector has already been closed then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * immediately.  Otherwise it marks the selector as closed and then invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the {@link #implCloseSelector implCloseSelector} method in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * complete the close operation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public final void close() throws IOException {
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   125
        boolean changed = (boolean) CLOSED.compareAndSet(this, false, true);
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   126
        if (changed) {
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   127
            implCloseSelector();
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   128
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Closes this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p> This method is invoked by the {@link #close close} method in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * to perform the actual work of closing the selector.  This method is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * invoked if the selector has not yet been closed, and it is never invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p> An implementation of this method must arrange for any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * that is blocked in a selection operation upon this selector to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * immediately as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * java.nio.channels.Selector#wakeup wakeup} method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          If an I/O error occurs while closing the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected abstract void implCloseSelector() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public final boolean isOpen() {
58439
b25362cec8ce 8231603: (se) Selector implementations do not need to use cancelledKeys
alanb
parents: 47216
diff changeset
   150
        return !closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the provider that created this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return  The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public final SelectorProvider provider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Retrieves this selector's cancelled-key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p> This set should only be used while synchronized upon it.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return  The cancelled-key set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected final Set<SelectionKey> cancelledKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return cancelledKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Registers the given channel with this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p> This method is invoked by a channel's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * AbstractSelectableChannel#register register} method in order to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the actual work of registering the channel with this selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *         The channel to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param  ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         The initial interest set, which must be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param  att
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         The initial attachment for the resulting key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  A new key representing the registration of the given channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *          with this selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected abstract SelectionKey register(AbstractSelectableChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                             int ops, Object att);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Removes the given key from its channel's key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <p> This method must be invoked by the selector for each channel that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * deregisters.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param  key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         The selection key to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected final void deregister(AbstractSelectionKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        ((AbstractSelectableChannel)key.channel()).removeKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // -- Interruption machinery --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private Interruptible interruptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Marks the beginning of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p> This method should be invoked in tandem with the {@link #end end}
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   217
     * method, using a {@code try}&nbsp;...&nbsp;{@code finally} block as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * shown <a href="#be">above</a>, in order to implement interruption for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <p> Invoking this method arranges for the selector's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Selector#wakeup wakeup} method to be invoked if a thread's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Thread#interrupt interrupt} method is invoked while the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * blocked in an I/O operation upon the selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected final void begin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (interruptor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            interruptor = new Interruptible() {
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   229
                    public void interrupt(Thread ignore) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        AbstractSelector.this.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        AbstractInterruptibleChannel.blockedOn(interruptor);
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   234
        Thread me = Thread.currentThread();
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   235
        if (me.isInterrupted())
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   236
            interruptor.interrupt(me);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Marks the end of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p> This method should be invoked in tandem with the {@link #begin begin}
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   243
     * method, using a {@code try}&nbsp;...&nbsp;{@code finally} block as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * shown <a href="#be">above</a>, in order to implement interruption for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * this selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected final void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        AbstractInterruptibleChannel.blockedOn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}