jdk/src/share/classes/java/nio/channels/spi/AbstractInterruptibleChannel.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7177 0113db4feebc
child 18574 4aeaeb541678
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7177
diff changeset
     2
 * Copyright (c) 2000, 2010, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package java.nio.channels.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.nio.ch.Interruptible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Base implementation class for interruptible channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> This class encapsulates the low-level machinery required to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the asynchronous closing and interruption of channels.  A concrete channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * class must invoke the {@link #begin begin} and {@link #end end} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * before and after, respectively, invoking an I/O operation that might block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * indefinitely.  In order to ensure that the {@link #end end} method is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * invoked, these methods should be used within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block: <a name="be">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     completed = ...;    // Perform blocking I/O operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     return ...;         // Return result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     end(completed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> The <tt>completed</tt> argument to the {@link #end end} method tells
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * whether or not the I/O operation actually completed, that is, whether it had
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * any effect that would be visible to the invoker.  In the case of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * operation that reads bytes, for example, this argument should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <tt>true</tt> if, and only if, some bytes were actually transferred into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * invoker's target buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p> A concrete channel class must also implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * #implCloseChannel implCloseChannel} method in such a way that if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * invoked while another thread is blocked in a native I/O operation upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * channel then that operation will immediately return, either by throwing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * exception or by returning normally.  If a thread is interrupted or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * channel upon which it is blocked is asynchronously closed then the channel's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link #end end} method will throw the appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> This class performs the synchronization required to implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * java.nio.channels.Channel} specification.  Implementations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * #implCloseChannel implCloseChannel} method need not synchronize against
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * other threads that might be attempting to close the channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public abstract class AbstractInterruptibleChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    implements Channel, InterruptibleChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
    91
    private final Object closeLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private volatile boolean open = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected AbstractInterruptibleChannel() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Closes this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p> If the channel has already been closed then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * immediately.  Otherwise it marks the channel as closed and then invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * the {@link #implCloseChannel implCloseChannel} method in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * complete the close operation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public final void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (!open)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            implCloseChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Closes this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p> This method is invoked by the {@link #close close} method in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * to perform the actual work of closing the channel.  This method is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * invoked if the channel has not yet been closed, and it is never invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> An implementation of this method must arrange for any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * that is blocked in an I/O operation upon this channel to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * immediately, either by throwing an exception or by returning normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *          If an I/O error occurs while closing the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected abstract void implCloseChannel() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public final boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return open;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // -- Interruption machinery --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private Interruptible interruptor;
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   145
    private volatile Thread interrupted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Marks the beginning of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p> This method should be invoked in tandem with the {@link #end end}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * method, using a <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * shown <a href="#be">above</a>, in order to implement asynchronous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * closing and interruption for this channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected final void begin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (interruptor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            interruptor = new Interruptible() {
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   158
                    public void interrupt(Thread target) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            if (!open)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            open = false;
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   163
                            interrupted = target;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                AbstractInterruptibleChannel.this.implCloseChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        blockedOn(interruptor);
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   171
        Thread me = Thread.currentThread();
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   172
        if (me.isInterrupted())
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   173
            interruptor.interrupt(me);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Marks the end of an I/O operation that might block indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p> This method should be invoked in tandem with the {@link #begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * begin} method, using a <tt>try</tt>&nbsp;...&nbsp;<tt>finally</tt> block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * as shown <a href="#be">above</a>, in order to implement asynchronous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * closing and interruption for this channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param  completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         <tt>true</tt> if, and only if, the I/O operation completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         successfully, that is, had some effect that would be visible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         the operation's invoker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *          If the channel was asynchronously closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          If the thread blocked in the I/O operation was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected final void end(boolean completed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        blockedOn(null);
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   199
        Thread interrupted = this.interrupted;
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   200
        if (interrupted != null && interrupted == Thread.currentThread()) {
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   201
            interrupted = null;
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   202
            throw new ClosedByInterruptException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   204
        if (!completed && !open)
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 5506
diff changeset
   205
            throw new AsynchronousCloseException();
2
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
    // -- sun.misc.SharedSecrets --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static void blockedOn(Interruptible intr) {         // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                             intr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}