3387N/A/*
3387N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3387N/A *
3387N/A * This code is free software; you can redistribute it and/or modify it
3387N/A * under the terms of the GNU General Public License version 2 only, as
3387N/A * published by the Free Software Foundation.
3387N/A *
3387N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3387N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3387N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3387N/A * version 2 for more details (a copy is included in the LICENSE file that
3387N/A * accompanied this code).
3387N/A *
3387N/A * You should have received a copy of the GNU General Public License version
3387N/A * 2 along with this work; if not, write to the Free Software Foundation,
3387N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3387N/A *
3387N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3387N/A * or visit www.oracle.com if you need additional information or have any
3387N/A * questions.
3387N/A */
3387N/A
3387N/A/*
3387N/A * This file is available under and governed by the GNU General Public
3387N/A * License version 2 only, as published by the Free Software Foundation.
3387N/A * However, the following notice accompanied the original version of this
3387N/A * file:
3387N/A *
3387N/A * Written by Doug Lea with assistance from members of JCP JSR-166
3387N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
3387N/A */
3387N/A
3387N/A/*
3387N/A * @test
3387N/A * @summary stress test for arrivals in a tiered phaser
3387N/A * @run main TieredArriveLoops 300
3387N/A */
3387N/Aimport java.util.*;
3387N/Aimport java.util.concurrent.*;
3387N/A
3387N/Apublic class TieredArriveLoops {
3387N/A final long testDurationMillisDefault = 10L * 1000L;
3387N/A final long testDurationMillis;
3387N/A final long quittingTimeNanos;
3387N/A
3387N/A TieredArriveLoops(String[] args) {
3387N/A testDurationMillis = (args.length > 0) ?
3387N/A Long.valueOf(args[0]) : testDurationMillisDefault;
3387N/A quittingTimeNanos = System.nanoTime() +
3387N/A testDurationMillis * 1000L * 1000L;
3387N/A }
3387N/A
3387N/A Runnable runner(final Phaser p) {
3387N/A return new CheckedRunnable() { public void realRun() {
3387N/A int prevPhase = p.register();
3387N/A while (!p.isTerminated()) {
3387N/A int phase = p.awaitAdvance(p.arrive());
3387N/A if (phase < 0)
3387N/A return;
3387N/A equal(phase, (prevPhase + 1) & Integer.MAX_VALUE);
3387N/A int ph = p.getPhase();
3387N/A check(ph < 0 || ph == phase);
3387N/A prevPhase = phase;
3387N/A }
3387N/A }};
3387N/A }
3387N/A
3387N/A void test(String[] args) throws Throwable {
3387N/A final Phaser parent = new Phaser();
3387N/A final Phaser child1 = new Phaser(parent);
3387N/A final Phaser child2 = new Phaser(parent);
3387N/A
3387N/A Thread t1 = new Thread(runner(child1));
3387N/A Thread t2 = new Thread(runner(child2));
3387N/A t1.start();
3387N/A t2.start();
3387N/A
3387N/A for (int prevPhase = 0, phase; ; prevPhase = phase) {
3387N/A phase = child2.getPhase();
3387N/A check(phase >= prevPhase);
3387N/A if (System.nanoTime() - quittingTimeNanos > 0) {
3387N/A System.err.printf("phase=%d%n", phase);
3387N/A child1.forceTermination();
3387N/A break;
3387N/A }
3387N/A }
3387N/A
3387N/A t1.join();
3387N/A t2.join();
3387N/A }
3387N/A
3387N/A //--------------------- Infrastructure ---------------------------
3387N/A volatile int passed = 0, failed = 0;
3387N/A void pass() {passed++;}
3387N/A void fail() {failed++; Thread.dumpStack();}
3387N/A void fail(String msg) {System.err.println(msg); fail();}
3387N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
3387N/A void check(boolean cond) {if (cond) pass(); else fail();}
3387N/A void equal(Object x, Object y) {
3387N/A if (x == null ? y == null : x.equals(y)) pass();
3387N/A else fail(x + " not equal to " + y);}
3387N/A public static void main(String[] args) throws Throwable {
3387N/A new TieredArriveLoops(args).instanceMain(args);}
3387N/A public void instanceMain(String[] args) throws Throwable {
3387N/A try {test(args);} catch (Throwable t) {unexpected(t);}
3387N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3387N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3387N/A
3387N/A abstract class CheckedRunnable implements Runnable {
3387N/A protected abstract void realRun() throws Throwable;
3387N/A
3387N/A public final void run() {
3387N/A try {realRun();} catch (Throwable t) {unexpected(t);}
3387N/A }
3387N/A }
3387N/A}