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 Martin Buchholz and Doug Lea with assistance from
3387N/A * members of JCP JSR-166 Expert Group and released to the public
3387N/A * 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 Test Phaser phase integer overflow behavior
3387N/A */
3387N/A
3387N/Aimport java.util.concurrent.Phaser;
3387N/Aimport java.lang.reflect.Field;
3387N/A
3387N/Apublic class PhaseOverflow {
3387N/A Field stateField;
3387N/A
3387N/A void checkState(Phaser phaser,
3387N/A int phase, int parties, int unarrived) {
3387N/A equal(phase, phaser.getPhase());
3387N/A equal(parties, phaser.getRegisteredParties());
3387N/A equal(unarrived, phaser.getUnarrivedParties());
3387N/A }
3387N/A
3387N/A void test(String[] args) throws Throwable {
3387N/A stateField = Phaser.class.getDeclaredField("state");
3387N/A stateField.setAccessible(true);
3387N/A testLeaf();
3387N/A testTiered();
3387N/A }
3387N/A
3387N/A void testLeaf() throws Throwable {
3387N/A Phaser phaser = new Phaser();
3387N/A // this is extremely dependent on internal representation
3387N/A stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
3387N/A checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
3387N/A phaser.register();
3387N/A checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
3387N/A phaser.arrive();
3387N/A checkState(phaser, Integer.MAX_VALUE, 1, 1);
3387N/A phaser.arrive();
3387N/A checkState(phaser, 0, 1, 1);
3387N/A phaser.arrive();
3387N/A checkState(phaser, 1, 1, 1);
3387N/A }
3387N/A
3387N/A int phaseInc(int phase) { return (phase + 1) & Integer.MAX_VALUE; }
3387N/A
3387N/A void testTiered() throws Throwable {
3387N/A Phaser root = new Phaser();
3387N/A // this is extremely dependent on internal representation
3387N/A stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 0, 0);
3387N/A Phaser p1 = new Phaser(root, 1);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 1, 1);
3387N/A checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
3387N/A Phaser p2 = new Phaser(root, 2);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 2, 2);
3387N/A checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
3387N/A int ph = Integer.MAX_VALUE - 1;
3387N/A for (int k = 0; k < 5; k++) {
3387N/A checkState(root, ph, 2, 2);
3387N/A checkState(p1, ph, 1, 1);
3387N/A checkState(p2, ph, 2, 2);
3387N/A p1.arrive();
3387N/A checkState(root, ph, 2, 1);
3387N/A checkState(p1, ph, 1, 0);
3387N/A checkState(p2, ph, 2, 2);
3387N/A p2.arrive();
3387N/A checkState(root, ph, 2, 1);
3387N/A checkState(p1, ph, 1, 0);
3387N/A checkState(p2, ph, 2, 1);
3387N/A p2.arrive();
3387N/A ph = phaseInc(ph);
3387N/A checkState(root, ph, 2, 2);
3387N/A checkState(p1, ph, 1, 1);
3387N/A checkState(p2, ph, 2, 2);
3387N/A }
3387N/A equal(3, ph);
3387N/A }
3387N/A
3387N/A void xtestTiered() throws Throwable {
3387N/A Phaser root = new Phaser();
3387N/A stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 0, 0);
3387N/A Phaser p1 = new Phaser(root, 1);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 1, 1);
3387N/A checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
3387N/A Phaser p2 = new Phaser(root, 2);
3387N/A checkState(root, Integer.MAX_VALUE - 1, 2, 2);
3387N/A checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
3387N/A int ph = Integer.MAX_VALUE - 1;
3387N/A for (int k = 0; k < 5; k++) {
3387N/A checkState(root, ph, 2, 2);
3387N/A checkState(p1, ph, 1, 1);
3387N/A checkState(p2, ph, 2, 2);
3387N/A p1.arrive();
3387N/A checkState(root, ph, 2, 1);
3387N/A checkState(p1, ph, 1, 0);
3387N/A checkState(p2, ph, 2, 2);
3387N/A p2.arrive();
3387N/A checkState(root, ph, 2, 1);
3387N/A checkState(p1, ph, 1, 0);
3387N/A checkState(p2, ph, 2, 1);
3387N/A p2.arrive();
3387N/A ph = phaseInc(ph);
3387N/A checkState(root, ph, 2, 2);
3387N/A checkState(p1, ph, 1, 1);
3387N/A checkState(p2, ph, 2, 2);
3387N/A }
3387N/A equal(3, ph);
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 PhaseOverflow().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}