jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
author alanb
Thu, 12 Aug 2010 19:53:25 +0100
changeset 6301 c90a67d75c9f
parent 5506 202f599c92aa
child 6319 47d9b9e70f99
permissions -rw-r--r--
6971825: (so) improve scatter/gather implementation Reviewed-by: chegar, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
diff changeset
     2
 * Copyright (c) 2000, 2009, 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: 3066
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: 3066
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: 3066
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
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.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.Cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class FileChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends FileChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // Used to make native read and write calls
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
    44
    private static final FileDispatcher nd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Memory allocation size for mapping buffers
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    47
    private static final long allocationGranularity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // File descriptor
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    50
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // File access mode (immutable)
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    53
    private final boolean writable;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    54
    private final boolean readable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Required to prevent finalization of creating stream (immutable)
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    57
    private final Object parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Thread-safe set of IDs of native threads, for signalling
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    60
    private final NativeThreadSet threads = new NativeThreadSet(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Lock for operations involving position and size
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    63
    private final Object positionLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private FileChannelImpl(FileDescriptor fd, boolean readable,
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    66
                            boolean writable, Object parent)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.readable = readable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.writable = writable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // Invoked by getChannel() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // of java.io.File{Input,Output}Stream and RandomAccessFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static FileChannel open(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                   boolean readable, boolean writable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                   Object parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    81
        return new FileChannelImpl(fd, readable, writable, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // -- Standard channel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void implCloseChannel() throws IOException {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    93
        // Release and invalidate any locks that we still hold
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (fileLockTable != null) {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    95
            for (FileLock fl: fileLockTable.removeAll()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    96
                synchronized (fl) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    97
                    if (fl.isValid()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    98
                        nd.release(fd, fl.position(), fl.size());
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
    99
                        ((FileLockImpl)fl).invalidate();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   100
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   102
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   105
        nd.preClose(fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   106
        threads.signalAndWait();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   107
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Close the fd via the parent stream's close method.  The parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // will reinvoke our close method, which is defined in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // superclass AbstractInterruptibleChannel, but the isOpen logic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // that method will prevent this method from being reinvoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            //
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
   115
            ((java.io.Closeable)parent).close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public int read(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   128
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   131
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    n = IOUtil.read(fd, dst, -1, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   146
    public long read(ByteBuffer[] dsts, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   147
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   148
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   149
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   150
            throw new IndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            long n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   156
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   159
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                do {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   163
                    n = IOUtil.read(fd, dsts, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
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
    public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   180
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   183
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    n = IOUtil.write(fd, src, -1, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   198
    public long write(ByteBuffer[] srcs, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   199
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   200
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   201
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   202
            throw new IndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            long n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   208
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   211
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                do {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   215
                    n = IOUtil.write(fd, srcs, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // -- Other operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public long position() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   232
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   235
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    p = position0(fd, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return IOStatus.normalize(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                end(p > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                assert IOStatus.check(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public FileChannel position(long newPosition) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (newPosition < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   256
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   259
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    p  = position0(fd, newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                end(p > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                assert IOStatus.check(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public long size() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            long s = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   278
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   281
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   285
                    s = nd.size(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                } while ((s == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return IOStatus.normalize(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                end(s > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                assert IOStatus.check(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public FileChannel truncate(long size) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (size > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            int rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   307
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   310
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                // get current position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    p = position0(fd, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                assert p >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // truncate file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   324
                    rv = nd.truncate(fd, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                // set position to size if greater than size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (p > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    p = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    rv = (int)position0(fd, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                end(rv > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                assert IOStatus.check(rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public void force(boolean metaData) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int rv = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   347
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   350
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   354
                rv = nd.force(fd, metaData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            end(rv > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            assert IOStatus.check(rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    // Assume at first that the underlying kernel supports sendfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private static volatile boolean transferSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    // Assume that the underlying kernel sendfile() will work if the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    // fd is a pipe; set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static volatile boolean pipeSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // Assume that the underlying kernel sendfile() will work if the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    // fd is a file; set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private static volatile boolean fileSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private long transferToDirectly(long position, int icount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                    WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (!transferSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        FileDescriptor targetFD = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (target instanceof FileChannelImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (!fileSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            targetFD = ((FileChannelImpl)target).fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } else if (target instanceof SelChImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // Direct transfer to pipe causes EINVAL on some configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if ((target instanceof SinkChannelImpl) && !pipeSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            targetFD = ((SelChImpl)target).getFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (targetFD == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        int thisFDVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int targetFDVal = IOUtil.fdVal(targetFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (thisFDVal == targetFDVal) // Not supported on some configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        long n = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   404
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   407
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                n = transferTo0(thisFDVal, position, icount, targetFDVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (n == IOStatus.UNSUPPORTED_CASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (target instanceof SinkChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    pipeSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (target instanceof FileChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    fileSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (n == IOStatus.UNSUPPORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // Don't bother trying again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                transferSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            end (n > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private long transferToTrustedChannel(long position, int icount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                          WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (  !((target instanceof FileChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                || (target instanceof SelChImpl)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // Trusted target: Use a mapped buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        MappedByteBuffer dbb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            dbb = map(MapMode.READ_ONLY, position, icount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // ## Bug: Closing this channel will not terminate the write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return target.write(dbb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (dbb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                unmap(dbb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    private long transferToArbitraryChannel(long position, int icount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                            WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // Untrusted target: Use a newly-erased buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int c = Math.min(icount, TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        long tw = 0;                    // Total bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        long pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            Util.erase(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            while (tw < icount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                bb.limit(Math.min((int)(icount - tw), TRANSFER_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                int nr = read(bb, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (nr <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // ## Bug: Will block writing target if this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                // ##      is asynchronously closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                int nw = target.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                tw += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (nw != nr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                pos += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (tw > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public long transferTo(long position, long count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                           WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (!target.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (target instanceof FileChannelImpl &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            !((FileChannelImpl)target).writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if ((position < 0) || (count < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        long sz = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (position > sz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        int icount = (int)Math.min(count, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if ((sz - position) < icount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            icount = (int)(sz - position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        long n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Attempt a direct transfer, if the kernel supports it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if ((n = transferToDirectly(position, icount, target)) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // Attempt a mapped transfer, but only to trusted channel types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if ((n = transferToTrustedChannel(position, icount, target)) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Slow path for untrusted targets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return transferToArbitraryChannel(position, icount, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private long transferFromFileChannel(FileChannelImpl src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                         long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // Note we could loop here to accumulate more at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        synchronized (src.positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            long p = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            int icount = (int)Math.min(Math.min(count, Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                       src.size() - p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            // ## Bug: Closing this channel will not terminate the write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            MappedByteBuffer bb = src.map(MapMode.READ_ONLY, p, icount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                long n = write(bb, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                src.position(p + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                unmap(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private static final int TRANSFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    private long transferFromArbitraryChannel(ReadableByteChannel src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                              long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // Untrusted target: Use a newly-erased buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        int c = (int)Math.min(count, TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        long tw = 0;                    // Total bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        long pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            Util.erase(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            while (tw < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                // ## Bug: Will block reading src if this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // ##      is asynchronously closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                int nr = src.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (nr <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                int nw = write(bb, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                tw += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                if (nw != nr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                pos += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (tw > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public long transferFrom(ReadableByteChannel src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                             long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (!src.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if ((position < 0) || (count < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (position > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (src instanceof FileChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
           return transferFromFileChannel((FileChannelImpl)src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                          position, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return transferFromArbitraryChannel(src, position, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public int read(ByteBuffer dst, long position) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (dst == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   611
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   614
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                n = IOUtil.read(fd, dst, position, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public int write(ByteBuffer src, long position) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (src == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   637
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   640
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                n = IOUtil.write(fd, src, position, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    // -- Memory-mapped buffers --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    private static class Unmapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    {
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   660
        // keep track of mapped buffer usage
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   661
        static volatile int count;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   662
        static volatile long totalSize;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   663
        static volatile long totalCapacity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        private long address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        private long size;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   667
        private int cap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   669
        private Unmapper(long address, long size, int cap) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            assert (address != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            this.size = size;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   673
            this.cap = cap;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   674
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   675
            synchronized (Unmapper.class) {
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   676
                count++;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   677
                totalSize += size;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   678
                totalCapacity += cap;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   679
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (address == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            unmap0(address, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            address = 0;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   687
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   688
            synchronized (Unmapper.class) {
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   689
                count--;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   690
                totalSize -= size;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   691
                totalCapacity -= cap;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   692
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    private static void unmap(MappedByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        Cleaner cl = ((DirectBuffer)bb).cleaner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (cl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            cl.clean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    private static final int MAP_RO = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    private static final int MAP_RW = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    private static final int MAP_PV = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public MappedByteBuffer map(MapMode mode, long position, long size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (position < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (size < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            throw new IllegalArgumentException("Negative size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (position + size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            throw new IllegalArgumentException("Position + size overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (size > Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int imode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (mode == MapMode.READ_ONLY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            imode = MAP_RO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        else if (mode == MapMode.READ_WRITE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            imode = MAP_RW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        else if (mode == MapMode.PRIVATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            imode = MAP_PV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        assert (imode >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if ((mode != MapMode.READ_ONLY) && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        long addr = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   732
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   735
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (size() < position + size) { // Extend file size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                if (!writable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    throw new IOException("Channel not open for writing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        "- cannot extend file to required size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   745
                    rv = nd.truncate(fd, position + size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                addr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                if ((!writable) || (imode == MAP_RO))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    return Util.newMappedByteBufferR(0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    return Util.newMappedByteBuffer(0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            int pagePosition = (int)(position % allocationGranularity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            long mapPosition = position - pagePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            long mapSize = size + pagePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                // If no exception was thrown from map0, the address is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                addr = map0(imode, mapPosition, mapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            } catch (OutOfMemoryError x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                // An OutOfMemoryError may indicate that we've exhausted memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                // so force gc and re-attempt map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                } catch (InterruptedException y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    addr = map0(imode, mapPosition, mapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                } catch (OutOfMemoryError y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    // After a second OOME, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    throw new IOException("Map failed", y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            assert (IOStatus.checkAll(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            assert (addr % allocationGranularity == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            int isize = (int)size;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   782
            Unmapper um = new Unmapper(addr, size + pagePosition, isize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if ((!writable) || (imode == MAP_RO))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                return Util.newMappedByteBufferR(isize, addr + pagePosition, um);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                return Util.newMappedByteBuffer(isize, addr + pagePosition, um);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            end(IOStatus.checkAll(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   793
    /**
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   794
     * Invoked by sun.management.ManagementFactoryHelper to create the management
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   795
     * interface for mapped buffers.
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   796
     */
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   797
    public static sun.misc.JavaNioAccess.BufferPool getMappedBufferPool() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   798
        return new sun.misc.JavaNioAccess.BufferPool() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   799
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   800
            public String getName() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   801
                return "mapped";
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   802
            }
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   803
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   804
            public long getCount() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   805
                return Unmapper.count;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   806
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   807
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   808
            public long getTotalCapacity() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   809
                return Unmapper.totalCapacity;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   810
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   811
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   812
            public long getMemoryUsed() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   813
                return Unmapper.totalSize;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   814
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   815
        };
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   816
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    // -- Locks --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   820
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    // keeps track of locks on this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private volatile FileLockTable fileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    // indicates if file locks are maintained system-wide (as per spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    private static boolean isSharedFileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    // indicates if the disableSystemWideOverlappingFileLockCheck property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    // has been checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    private static volatile boolean propertyChecked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    // The lock list in J2SE 1.4/5.0 was local to each FileChannel instance so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    // the overlap check wasn't system wide when there were multiple channels to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    // the same file. This property is used to get 1.4/5.0 behavior if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private static boolean isSharedFileLockTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (!propertyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            synchronized (FileChannelImpl.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                if (!propertyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    String value = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                        new GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                            "sun.nio.ch.disableSystemWideOverlappingFileLockCheck"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    isSharedFileLockTable = ((value == null) || value.equals("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    propertyChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return isSharedFileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   850
    private FileLockTable fileLockTable() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (fileLockTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if (fileLockTable == null) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   854
                    if (isSharedFileLockTable()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   855
                        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   856
                        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   857
                            ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   858
                            fileLockTable = FileLockTable.newSharedFileLockTable(this, fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   859
                        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   860
                            threads.remove(ti);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   861
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   862
                    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   863
                        fileLockTable = new SimpleFileLockTable();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   864
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        return fileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public FileLock lock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (shared && !readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if (!shared && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        FileLockImpl fli = new FileLockImpl(this, position, size, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        FileLockTable flt = fileLockTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        flt.add(fli);
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   882
        boolean completed = false;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   883
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   886
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                return null;
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   889
            int n;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   890
            do {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   891
                n = nd.lock(fd, true, position, size, shared);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   892
            } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   893
            if (isOpen()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   894
                if (n == FileDispatcher.RET_EX_LOCK) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   895
                    assert shared;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   896
                    FileLockImpl fli2 = new FileLockImpl(this, position, size,
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   897
                                                         false);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   898
                    flt.replace(fli, fli2);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   899
                    fli = fli2;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   900
                }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   901
                completed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   903
        } finally {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   904
            if (!completed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                flt.remove(fli);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            try {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   908
                end(completed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            } catch (ClosedByInterruptException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                throw new FileLockInterruptionException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return fli;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public FileLock tryLock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (shared && !readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (!shared && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        FileLockImpl fli = new FileLockImpl(this, position, size, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        FileLockTable flt = fileLockTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        flt.add(fli);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   927
        int result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   928
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   929
        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   930
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   931
            try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   932
                ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   933
                result = nd.lock(fd, false, position, size, shared);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   934
            } catch (IOException e) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   935
                flt.remove(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   936
                throw e;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   937
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   938
            if (result == FileDispatcher.NO_LOCK) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   939
                flt.remove(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   940
                return null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   941
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   942
            if (result == FileDispatcher.RET_EX_LOCK) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   943
                assert shared;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   944
                FileLockImpl fli2 = new FileLockImpl(this, position, size,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   945
                                                     false);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   946
                flt.replace(fli, fli2);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   947
                return fli2;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   948
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   949
            return fli;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   950
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   951
            threads.remove(ti);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    void release(FileLockImpl fli) throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   956
        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   957
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   958
            ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   959
            nd.release(fd, fli.position(), fli.size());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   960
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   961
            threads.remove(ti);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   962
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        assert fileLockTable != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        fileLockTable.remove(fli);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   967
    // -- File lock support --
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * A simple file lock table that maintains a list of FileLocks obtained by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * FileChannel. Use to get 1.4/5.0 behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   973
    private static class SimpleFileLockTable extends FileLockTable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // synchronize on list for access
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   975
        private final List<FileLock> lockList = new ArrayList<FileLock>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        public SimpleFileLockTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        private void checkList(long position, long size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            throws OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            assert Thread.holdsLock(lockList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            for (FileLock fl: lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                if (fl.overlaps(position, size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    throw new OverlappingFileLockException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        public void add(FileLock fl) throws OverlappingFileLockException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                checkList(fl.position(), fl.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                lockList.add(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        public void remove(FileLock fl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                lockList.remove(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1004
        public List<FileLock> removeAll() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            synchronized(lockList) {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1006
                List<FileLock> result = new ArrayList<FileLock>(lockList);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1007
                lockList.clear();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1008
                return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        public void replace(FileLock fl1, FileLock fl2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                lockList.remove(fl1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                lockList.add(fl2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    // Creates a new mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    private native long map0(int prot, long position, long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    // Removes an existing mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    private static native int unmap0(long address, long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    // Transfers from src to dst, or returns -2 if kernel can't do that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    private native long transferTo0(int src, long position, long count, int dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    // Sets or reports this file's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    // If offset is -1, the current position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    // otherwise the position is set to offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    private native long position0(FileDescriptor fd, long offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    // Caches fieldIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    private static native long initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        allocationGranularity = initIDs();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1043
        nd = new FileDispatcherImpl();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
}