jdk/src/share/classes/java/nio/channels/spi/AbstractSelector.java
author alanb
Thu, 24 Jul 2008 12:46:41 +0100
changeset 895 67f1dc69ad10
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
6726309: Compiler warnings in nio code Reviewed-by: sherman, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.SelectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.Selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.nio.ch.Interruptible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicBoolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Base implementation class for selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> This class encapsulates the low-level machinery required to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the interruption of selection operations.  A concrete selector class must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * invoke the {@link #begin begin} and {@link #end end} methods before and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * after, respectively, invoking an I/O operation that might block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * indefinitely.  In order to ensure that the {@link #end end} method is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * invoked, these methods should be used within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block: <a name="be">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     // Perform blocking I/O operation here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p> This class also defines methods for maintaining a selector's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * cancelled-key set and for removing a key from its channel's key set, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * declares the abstract {@link #register register} method that is invoked by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * selectable channel's {@link AbstractSelectableChannel#register register}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * method in order to perform the actual work of registering a channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public abstract class AbstractSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    extends Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private AtomicBoolean selectorOpen = new AtomicBoolean(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // The provider that created this selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final SelectorProvider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Initializes a new instance of this class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected AbstractSelector(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    85
    private final Set<SelectionKey> cancelledKeys = new HashSet<SelectionKey>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    void cancel(SelectionKey k) {                       // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        synchronized (cancelledKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            cancelledKeys.add(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Closes this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p> If the selector has already been closed then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * immediately.  Otherwise it marks the selector as closed and then invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * the {@link #implCloseSelector implCloseSelector} method in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * complete the close operation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        boolean open = selectorOpen.getAndSet(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!open)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        implCloseSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Closes this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p> This method is invoked by the {@link #close close} method in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * to perform the actual work of closing the selector.  This method is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * invoked if the selector has not yet been closed, and it is never invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <p> An implementation of this method must arrange for any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * that is blocked in a selection operation upon this selector to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * immediately as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * java.nio.channels.Selector#wakeup wakeup} method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *          If an I/O error occurs while closing the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected abstract void implCloseSelector() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public final boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return selectorOpen.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Returns the provider that created this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return  The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public final SelectorProvider provider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Retrieves this selector's cancelled-key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p> This set should only be used while synchronized upon it.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return  The cancelled-key set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected final Set<SelectionKey> cancelledKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return cancelledKeys;
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
     * Registers the given channel with this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p> This method is invoked by a channel's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * AbstractSelectableChannel#register register} method in order to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the actual work of registering the channel with this selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *         The channel to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param  ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *         The initial interest set, which must be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param  att
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         The initial attachment for the resulting key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return  A new key representing the registration of the given channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *          with this selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected abstract SelectionKey register(AbstractSelectableChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                             int ops, Object att);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Removes the given key from its channel's key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p> This method must be invoked by the selector for each channel that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * deregisters.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param  key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         The selection key to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected final void deregister(AbstractSelectionKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ((AbstractSelectableChannel)key.channel()).removeKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // -- Interruption machinery --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private Interruptible interruptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Marks the beginning of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> This method should be invoked in tandem with the {@link #end end}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * method, using a <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * shown <a href="#be">above</a>, in order to implement interruption for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p> Invoking this method arranges for the selector's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Selector#wakeup wakeup} method to be invoked if a thread's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Thread#interrupt interrupt} method is invoked while the thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * blocked in an I/O operation upon the selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected final void begin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (interruptor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            interruptor = new Interruptible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        AbstractSelector.this.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        AbstractInterruptibleChannel.blockedOn(interruptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (Thread.currentThread().isInterrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            interruptor.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Marks the end of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <p> This method should be invoked in tandem with the {@link #begin begin}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * method, using a <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * shown <a href="#be">above</a>, in order to implement interruption for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * this selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected final void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        AbstractInterruptibleChannel.blockedOn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}