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 Martin Buchholz and Doug Lea with assistance from
  30  * members of JCP JSR-166 Expert Group and released to the public
  31  * domain, as explained at
  32  * http://creativecommons.org/licenses/publicdomain
  33  */
  34 
  35 /*
  36  * @test
  37  * @summary Test Phaser phase integer overflow behavior
  38  */
  39 
  40 import java.util.concurrent.Phaser;
  41 import java.lang.reflect.Field;
  42 
  43 public class PhaseOverflow {
  44     Field stateField;
  45 
  46     void checkState(Phaser phaser,
  47                     int phase, int parties, int unarrived) {
  48         equal(phase, phaser.getPhase());
  49         equal(parties, phaser.getRegisteredParties());
  50         equal(unarrived, phaser.getUnarrivedParties());
  51     }
  52 
  53     void test(String[] args) throws Throwable {
  54         stateField = Phaser.class.getDeclaredField("state");
  55         stateField.setAccessible(true);
  56         testLeaf();
  57         testTiered();
  58     }
  59 
  60     void testLeaf() throws Throwable {
  61         Phaser phaser = new Phaser();
  62         stateField.setLong(phaser, (Integer.MAX_VALUE - 1L) << 32 );
  63         checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
  64         phaser.register();
  65         checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
  66         phaser.arrive();
  67         checkState(phaser, Integer.MAX_VALUE, 1, 1);
  68         phaser.arrive();
  69         checkState(phaser, 0, 1, 1);
  70         phaser.arrive();
  71         checkState(phaser, 1, 1, 1);
  72     }
  73 
  74     int phaseInc(int phase) { return (phase + 1) & Integer.MAX_VALUE; }
  75 
  76     void testTiered() throws Throwable {
  77         Phaser root = new Phaser();
  78         stateField.setLong(root, (Integer.MAX_VALUE - 1L) << 32 );
  79         checkState(root, Integer.MAX_VALUE - 1, 0, 0);
  80         Phaser p1 = new Phaser(root, 1);
  81         checkState(root, Integer.MAX_VALUE - 1, 1, 1);
  82         checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
  83         Phaser p2 = new Phaser(root, 2);
  84         checkState(root, Integer.MAX_VALUE - 1, 2, 2);
  85         checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
  86         int ph = Integer.MAX_VALUE - 1;
  87         for (int k = 0; k < 5; k++) {
  88             checkState(root, ph, 2, 2);
  89             checkState(p1, ph, 1, 1);
  90             checkState(p2, ph, 2, 2);
  91             p1.arrive();
  92             checkState(root, ph, 2, 1);
  93             checkState(p1, ph, 1, 0);
  94             checkState(p2, ph, 2, 2);
  95             p2.arrive();
  96             checkState(root, ph, 2, 1);
  97             checkState(p1, ph, 1, 0);
  98             checkState(p2, ph, 2, 1);
  99             p2.arrive();
 100             ph = phaseInc(ph);
 101             checkState(root, ph, 2, 2);
 102             checkState(p1, ph, 1, 1);
 103             checkState(p2, ph, 2, 2);
 104         }
 105         equal(3, ph);
 106     }
 107 
 108     //--------------------- Infrastructure ---------------------------
 109     volatile int passed = 0, failed = 0;
 110     void pass() {passed++;}
 111     void fail() {failed++; Thread.dumpStack();}
 112     void fail(String msg) {System.err.println(msg); fail();}
 113     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 114     void check(boolean cond) {if (cond) pass(); else fail();}
 115     void equal(Object x, Object y) {
 116         if (x == null ? y == null : x.equals(y)) pass();
 117         else fail(x + " not equal to " + y);}
 118     public static void main(String[] args) throws Throwable {
 119         new PhaseOverflow().instanceMain(args);}
 120     public void instanceMain(String[] args) throws Throwable {
 121         try {test(args);} catch (Throwable t) {unexpected(t);}
 122         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 123         if (failed > 0) throw new AssertionError("Some tests failed");}
 124 }