1109N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1109N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1109N/A *
1109N/A * This code is free software; you can redistribute it and/or modify it
1109N/A * under the terms of the GNU General Public License version 2 only, as
1109N/A * published by the Free Software Foundation.
1109N/A *
1109N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1109N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1109N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1109N/A * version 2 for more details (a copy is included in the LICENSE file that
1109N/A * accompanied this code).
1109N/A *
1109N/A * You should have received a copy of the GNU General Public License version
1109N/A * 2 along with this work; if not, write to the Free Software Foundation,
1109N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1109N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1109N/A */
1109N/A
1109N/A/* @test
1580N/A * @bug 6543863 6842687
1109N/A * @summary Try to cause a deadlock between (Asynchronous)FileChannel.close
1109N/A * and FileLock.release
1109N/A */
1109N/A
1109N/Aimport java.io.*;
1109N/Aimport java.nio.file.Path;
1109N/Aimport static java.nio.file.StandardOpenOption.*;
1109N/Aimport java.nio.channels.*;
1109N/Aimport java.util.concurrent.*;
1109N/A
1109N/Apublic class ReleaseOnCloseDeadlock {
1109N/A private static final int LOCK_COUNT = 1024;
1109N/A
1109N/A public static void main(String[] args) throws IOException {
1109N/A File blah = File.createTempFile("blah", null);
1109N/A blah.deleteOnExit();
2546N/A try {
2546N/A for (int i=0; i<100; i++) {
2546N/A test(blah.toPath());
2546N/A }
2546N/A } finally {
2546N/A blah.delete();
1109N/A }
1109N/A }
1109N/A
1109N/A static void test(Path file) throws IOException {
1109N/A FileLock[] locks = new FileLock[LOCK_COUNT];
1109N/A
1109N/A FileChannel fc = FileChannel.open(file, READ, WRITE);
1109N/A for (int i=0; i<LOCK_COUNT; i++) {
1109N/A locks[i] = fc.lock(i, 1, true);
1109N/A }
1109N/A tryToDeadlock(fc, locks);
1109N/A
1109N/A AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ, WRITE);
1109N/A for (int i=0; i<LOCK_COUNT; i++) {
1109N/A try {
1580N/A locks[i] = ch.lock(i, 1, true).get();
1109N/A } catch (InterruptedException x) {
1109N/A throw new RuntimeException(x);
1109N/A } catch (ExecutionException x) {
1109N/A throw new RuntimeException(x);
1109N/A }
1109N/A }
1109N/A tryToDeadlock(ch, locks);
1109N/A }
1109N/A
1109N/A static void tryToDeadlock(final Channel channel, FileLock[] locks)
1109N/A throws IOException
1109N/A {
1109N/A // start thread to close the file (and invalidate the locks)
1109N/A Thread closer = new Thread(
1109N/A new Runnable() {
1109N/A public void run() {
1109N/A try {
1109N/A channel.close();
1109N/A } catch (IOException ignore) {
1109N/A ignore.printStackTrace();
1109N/A }
1109N/A }});
1109N/A closer.start();
1109N/A
1109N/A // release the locks explicitly
1109N/A for (int i=0; i<locks.length; i++) {
1109N/A try {
1109N/A locks[i].release();
1109N/A } catch (ClosedChannelException ignore) { }
1109N/A }
1109N/A
1109N/A // we are done when closer has terminated
1109N/A while (closer.isAlive()) {
1109N/A try {
1109N/A closer.join();
1109N/A } catch (InterruptedException ignore) { }
1109N/A }
1109N/A }
1109N/A}