jdk/src/solaris/classes/sun/nio/ch/SourceChannelImpl.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 12440 2689ca179e22
child 16921 e70261f11307
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12440
2689ca179e22 7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 2057
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: 2057
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: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
class SourceChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    extends Pipe.SourceChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // The file descriptor associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // fd value needed for dev/poll. This value will remain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // even after the value in the file descriptor object has been set to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // ID of native thread doing read, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private volatile long thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Lock held by current reading thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Channel state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int ST_INUSE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int ST_KILLED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private volatile int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.state = ST_INUSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        synchronized (stateLock) {
12440
2689ca179e22 7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents: 5506
diff changeset
    87
            if (state != ST_KILLED)
2689ca179e22 7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents: 5506
diff changeset
    88
                nd.preClose(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            long th = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (th != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ((ops & PollArrayWrapper.POLLNVAL) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new Error("POLLNVAL detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ((intOps & SelectionKey.OP_READ) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            newOps |= SelectionKey.OP_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (ops == SelectionKey.OP_READ)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            ops = PollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        sk.selector.putEventOps(sk, ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public int read(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                thread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    n = IOUtil.read(fd, dst, -1, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
           throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return read(Util.subsequence(dsts, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public long read(ByteBuffer[] dsts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (dsts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                thread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    n = IOUtil.read(fd, dsts, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                assert IOStatus.check(n);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Util.load();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   212
        nd = new FileDispatcherImpl();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}