1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 */ 33 34 /* 35 * @test 36 * @summary stress test for arrivals in a tiered phaser 37 * @run main TieredArriveLoops 300 38 */ 39 40 import java.util.concurrent.Phaser; 41 42 public class TieredArriveLoops { 43 final long testDurationMillisDefault = 10_000L; 44 final long testDurationMillis; 45 final long quittingTimeNanos; 46 47 TieredArriveLoops(String[] args) { 48 testDurationMillis = (args.length > 0) ? 49 Long.valueOf(args[0]) : testDurationMillisDefault; 50 quittingTimeNanos = System.nanoTime() + 51 testDurationMillis * 1000L * 1000L; 52 } 53 54 Runnable runner(final Phaser p) { 55 return new CheckedRunnable() { public void realRun() { 56 int prevPhase = p.register(); 57 while (!p.isTerminated()) { 58 int phase = p.awaitAdvance(p.arrive()); 59 if (phase < 0) 60 return; 61 equal(phase, (prevPhase + 1) & Integer.MAX_VALUE); 62 int ph = p.getPhase(); 63 check(ph < 0 || ph == phase); 64 prevPhase = phase; 65 } 66 }}; 67 } 68 69 void test(String[] args) throws Throwable { 70 final Phaser parent = new Phaser(); 71 final Phaser child1 = new Phaser(parent); 72 final Phaser child2 = new Phaser(parent); 73 74 Thread t1 = new Thread(runner(child1)); 75 Thread t2 = new Thread(runner(child2)); 76 t1.start(); 77 t2.start(); 78 79 for (int prevPhase = 0, phase; ; prevPhase = phase) { 80 phase = child2.getPhase(); 81 check(phase >= prevPhase); 82 if (System.nanoTime() - quittingTimeNanos > 0) { 83 System.err.printf("phase=%d%n", phase); 84 child1.forceTermination(); 85 break; 86 } 87 } 88 89 t1.join(); 90 t2.join(); 91 } 92 93 //--------------------- Infrastructure --------------------------- 94 volatile int passed = 0, failed = 0; 95 void pass() {passed++;} 96 void fail() {failed++; Thread.dumpStack();} 97 void fail(String msg) {System.err.println(msg); fail();} 98 void unexpected(Throwable t) {failed++; t.printStackTrace();} 99 void check(boolean cond) {if (cond) pass(); else fail();} 100 void equal(Object x, Object y) { 101 if (x == null ? y == null : x.equals(y)) pass(); 102 else fail(x + " not equal to " + y);} 103 public static void main(String[] args) throws Throwable { 104 new TieredArriveLoops(args).instanceMain(args);} 105 public void instanceMain(String[] args) throws Throwable { 106 try {test(args);} catch (Throwable t) {unexpected(t);} 107 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 108 if (failed > 0) throw new AssertionError("Some tests failed");} 109 110 abstract class CheckedRunnable implements Runnable { 111 protected abstract void realRun() throws Throwable; 112 113 public final void run() { 114 try {realRun();} catch (Throwable t) {unexpected(t);} 115 } 116 } 117 }