src/java.base/share/classes/sun/nio/ch/AsynchronousFileChannelImpl.java
author rriggs
Fri, 01 Dec 2017 16:40:08 -0500
changeset 48224 be0df5ab3093
parent 47216 71c04702a3d5
child 48748 4d716bc7ed54
permissions -rw-r--r--
8080225: FileInput/OutputStream/FileChannel cleanup should be improved Reviewed-by: mchung, plevart, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
     2
 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
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: 3632
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    23
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    25
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
package sun.nio.ch;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
    28
import java.nio.ByteBuffer;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
import java.nio.channels.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
import java.util.concurrent.ExecutorService;
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
    31
import java.util.concurrent.Future;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
import java.util.concurrent.locks.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.io.FileDescriptor;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
import java.io.IOException;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
/**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
 * Base implementation of AsynchronousFileChannel.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
abstract class AsynchronousFileChannelImpl
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
    extends AsynchronousFileChannel
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    42
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    43
    // close support
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
    protected final ReadWriteLock closeLock = new ReentrantReadWriteLock();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    45
    protected volatile boolean closed;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    46
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
    // file descriptor
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
    protected final FileDescriptor fdObj;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
    // indicates if open for reading/writing
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    51
    protected final boolean reading;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
    protected final boolean writing;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    53
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    54
    // associated Executor
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    55
    protected final ExecutorService executor;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    56
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    57
    protected AsynchronousFileChannelImpl(FileDescriptor fdObj,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    58
                                          boolean reading,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
                                          boolean writing,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
                                          ExecutorService executor)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    61
    {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    62
        this.fdObj = fdObj;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
        this.reading = reading;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
        this.writing = writing;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    65
        this.executor = executor;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    66
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    67
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    68
    final ExecutorService executor() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    69
        return executor;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    70
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    71
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    72
    @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    73
    public final boolean isOpen() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
        return !closed;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    77
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
     * Marks the beginning of an I/O operation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    80
     * @throws  ClosedChannelException  If channel is closed
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    81
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    82
    protected final void begin() throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    83
        closeLock.readLock().lock();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    84
        if (closed)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    85
            throw new ClosedChannelException();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    86
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    87
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    88
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    89
     * Marks the end of an I/O operation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
    protected final void end() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    92
        closeLock.readLock().unlock();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    93
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    94
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    95
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    96
     * Marks end of I/O operation
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    97
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    98
    protected final void end(boolean completed) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    99
        end();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   100
        if (!completed && !isOpen())
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   101
            throw new AsynchronousCloseException();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   102
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   103
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   104
    // -- file locking --
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   105
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   106
    abstract <A> Future<FileLock> implLock(long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   107
                                           long size,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   108
                                           boolean shared,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   109
                                           A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   110
                                           CompletionHandler<FileLock,? super A> handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   111
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   112
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   113
    public final Future<FileLock> lock(long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   114
                                       long size,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   115
                                       boolean shared)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   116
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   117
    {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   118
        return implLock(position, size, shared, null, null);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   119
    }
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   120
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   121
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   122
    public final <A> void lock(long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   123
                               long size,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   124
                               boolean shared,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   125
                               A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   126
                               CompletionHandler<FileLock,? super A> handler)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   127
    {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   128
        if (handler == null)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   129
            throw new NullPointerException("'handler' is null");
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   130
        implLock(position, size, shared, attachment, handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   131
    }
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   132
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   133
    private volatile FileLockTable fileLockTable;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   134
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   135
    final void ensureFileLockTableInitialized() throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   136
        if (fileLockTable == null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   137
            synchronized (this) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   138
                if (fileLockTable == null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   139
                    fileLockTable = FileLockTable.newSharedFileLockTable(this, fdObj);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   140
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   141
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   142
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   143
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   144
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   145
    final void invalidateAllLocks() throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   146
        if (fileLockTable != null) {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   147
            for (FileLock fl: fileLockTable.removeAll()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   148
                synchronized (fl) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   149
                    if (fl.isValid()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   150
                        FileLockImpl fli = (FileLockImpl)fl;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   151
                        implRelease(fli);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   152
                        fli.invalidate();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   153
                    }
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   154
                }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   155
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   156
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   157
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   158
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   159
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
     * Adds region to lock table
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   161
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   162
    protected final FileLockImpl addToFileLockTable(long position, long size, boolean shared) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   163
        final FileLockImpl fli;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   164
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   165
            // like begin() but returns null instead of exception
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   166
            closeLock.readLock().lock();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   167
            if (closed)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   168
                return null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   169
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   170
            try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   171
                ensureFileLockTableInitialized();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   172
            } catch (IOException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   173
                // should not happen
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   174
                throw new AssertionError(x);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   175
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   176
            fli = new FileLockImpl(this, position, size, shared);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   177
            // may throw OverlappedFileLockException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   178
            fileLockTable.add(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   179
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   180
            end();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   181
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   182
        return fli;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   183
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   184
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   185
    protected final void removeFromFileLockTable(FileLockImpl fli) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   186
        fileLockTable.remove(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   187
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   188
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   189
    /**
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   190
     * Releases the given file lock.
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   191
     */
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   192
    protected abstract void implRelease(FileLockImpl fli) throws IOException;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   193
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   194
    /**
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   195
     * Invoked by FileLockImpl to release the given file lock and remove it
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   196
     * from the lock table.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   197
     */
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   198
    final void release(FileLockImpl fli) throws IOException {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   199
        try {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   200
            begin();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   201
            implRelease(fli);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   202
            removeFromFileLockTable(fli);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   203
        } finally {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   204
            end();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   205
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   206
    }
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   207
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   208
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   209
    // -- reading and writing --
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   210
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   211
    abstract <A> Future<Integer> implRead(ByteBuffer dst,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   212
                                         long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   213
                                         A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   214
                                         CompletionHandler<Integer,? super A> handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   215
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   216
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   217
    public final Future<Integer> read(ByteBuffer dst, long position) {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   218
        return implRead(dst, position, null, null);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   219
    }
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   220
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   221
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   222
    public final <A> void read(ByteBuffer dst,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   223
                               long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   224
                               A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   225
                               CompletionHandler<Integer,? super A> handler)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   226
    {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   227
        if (handler == null)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   228
            throw new NullPointerException("'handler' is null");
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   229
        implRead(dst, position, attachment, handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   230
    }
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   231
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   232
    abstract <A> Future<Integer> implWrite(ByteBuffer src,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   233
                                           long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   234
                                           A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   235
                                           CompletionHandler<Integer,? super A> handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   236
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   237
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   238
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   239
    public final Future<Integer> write(ByteBuffer src, long position) {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   240
        return implWrite(src, position, null, null);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   241
    }
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   242
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   243
    @Override
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   244
    public final <A> void write(ByteBuffer src,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   245
                                long position,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   246
                                A attachment,
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   247
                                CompletionHandler<Integer,? super A> handler)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   248
    {
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   249
        if (handler == null)
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   250
            throw new NullPointerException("'handler' is null");
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   251
        implWrite(src, position, attachment, handler);
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
   252
    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   253
}