1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 8024833
26 * @key intermittent
27 * @summary Tests interruption of threads mapping sections of a file channel in
28 *   an attempt to deadlock due to nesting of begin calls.
29 */
30import java.io.IOException;
31import java.nio.ByteBuffer;
32import java.nio.channels.*;
33import java.nio.channels.FileChannel.MapMode;
34import java.nio.file.*;
35import java.util.concurrent.Semaphore;
36import static java.nio.file.StandardOpenOption.*;
37
38public class InterruptMapDeadlock {
39
40    static class Mapper extends Thread {
41        final FileChannel fc;
42        final Semaphore gate;
43        volatile Exception exception;
44
45        Mapper(FileChannel fc, Semaphore gate) {
46            this.fc = fc;
47            this.gate = gate;
48        }
49
50        @Override
51        public void run() {
52            try {
53                gate.acquireUninterruptibly();
54                fc.map(MapMode.READ_ONLY, 0, 1);
55                throw new Exception("Map succeeded");
56            } catch (IOException x) {
57                System.out.println(x.getClass() + " (expected)");
58            } catch (Exception unexpected) {
59                this.exception = unexpected;
60            }
61        }
62
63        Exception exception() {
64            return exception;
65        }
66
67        static Mapper startMapper(FileChannel fc, Semaphore gate) {
68            Mapper r = new Mapper(fc, gate);
69            r.setDaemon(true);
70            r.start();
71            return r;
72        }
73    }
74
75    static class Interruptor extends Thread {
76
77        final Mapper[] mappers;
78        final Semaphore gate;
79
80        Interruptor(Mapper[] mappers, Semaphore gate) {
81            this.mappers = mappers;
82            this.gate = gate;
83        }
84
85        public void run() {
86            gate.release(mappers.length);
87            for (Mapper m : mappers) {
88                m.interrupt();
89            }
90        }
91    }
92    // the number of mapper threads to start
93    private static final int MAPPER_COUNT = 4;
94
95    public static void main(String[] args) throws Exception {
96        Path file = Paths.get("data.txt");
97        FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE).close();
98
99        Mapper[] mappers = new Mapper[MAPPER_COUNT];
100
101        for (int i=1; i<=20; i++) {
102            System.out.format("Iteration: %s%n", i);
103
104            FileChannel fc = FileChannel.open(file);
105            boolean failed = false;
106
107            Semaphore gate = new Semaphore(0);
108            // start mapper threads
109            for (int j=0; j<MAPPER_COUNT; j++) {
110                mappers[j] = Mapper.startMapper(fc, gate);
111            }
112
113            // interrupt and wait for the mappers to terminate
114            Interruptor interruptor = new Interruptor(mappers, gate);
115            interruptor.start();
116            try {
117                interruptor.join(10000);
118                if (interruptor.isAlive()) {
119                    System.err.println("Interruptor thread did not terminate:");
120                    Throwable t = new Exception("Stack trace");
121                    t.setStackTrace(interruptor.getStackTrace());
122                    t.printStackTrace();
123                    failed = true;
124                }
125            } catch (InterruptedException x) {
126                System.err.println("Main thread was interrupted");
127                failed = true;
128            }
129
130            for (Mapper m: mappers) {
131                try {
132                    m.join(10000);
133                    Exception e = m.exception();
134                    if (e != null) {
135                        System.err.println("Mapper thread failed with: " + e);
136                        failed = true;
137                    } else if (m.isAlive()) {
138                        System.err.println("Mapper thread did not terminate:");
139                        Throwable t = new Exception("Stack trace");
140                        t.setStackTrace(m.getStackTrace());
141                        t.printStackTrace();
142                        failed = true;
143                    }
144                } catch (InterruptedException x) {
145                    System.err.println("Main thread was interrupted");
146                    failed = true;
147                }
148            }
149
150            if (failed)
151                throw new RuntimeException("Test failed - see log for details");
152            else
153                fc.close();
154        }
155    }
156}
157