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/publicdomain/zero/1.0/
33 */
34
35/*
36 * @test
37 * @modules java.base/java.util.concurrent:open
38 * @summary Test Phaser phase integer overflow behavior
39 */
40
41import java.util.concurrent.Phaser;
42import java.lang.reflect.Field;
43
44public class PhaseOverflow {
45    Field stateField;
46
47    void checkState(Phaser phaser,
48                    int phase, int parties, int unarrived) {
49        equal(phase, phaser.getPhase());
50        equal(parties, phaser.getRegisteredParties());
51        equal(unarrived, phaser.getUnarrivedParties());
52    }
53
54    void test(String[] args) throws Throwable {
55        stateField = Phaser.class.getDeclaredField("state");
56        stateField.setAccessible(true);
57        testLeaf();
58        testTiered();
59    }
60
61    void testLeaf() throws Throwable {
62        Phaser phaser = new Phaser();
63        // this is extremely dependent on internal representation
64        stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
65        checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
66        phaser.register();
67        checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
68        phaser.arrive();
69        checkState(phaser, Integer.MAX_VALUE, 1, 1);
70        phaser.arrive();
71        checkState(phaser, 0, 1, 1);
72        phaser.arrive();
73        checkState(phaser, 1, 1, 1);
74    }
75
76    int phaseInc(int phase) { return (phase + 1) & Integer.MAX_VALUE; }
77
78    void testTiered() throws Throwable {
79        Phaser root = new Phaser();
80        // this is extremely dependent on internal representation
81        stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
82        checkState(root, Integer.MAX_VALUE - 1, 0, 0);
83        Phaser p1 = new Phaser(root, 1);
84        checkState(root, Integer.MAX_VALUE - 1, 1, 1);
85        checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
86        Phaser p2 = new Phaser(root, 2);
87        checkState(root, Integer.MAX_VALUE - 1, 2, 2);
88        checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
89        int ph = Integer.MAX_VALUE - 1;
90        for (int k = 0; k < 5; k++) {
91            checkState(root, ph, 2, 2);
92            checkState(p1, ph, 1, 1);
93            checkState(p2, ph, 2, 2);
94            p1.arrive();
95            checkState(root, ph, 2, 1);
96            checkState(p1, ph, 1, 0);
97            checkState(p2, ph, 2, 2);
98            p2.arrive();
99            checkState(root, ph, 2, 1);
100            checkState(p1, ph, 1, 0);
101            checkState(p2, ph, 2, 1);
102            p2.arrive();
103            ph = phaseInc(ph);
104            checkState(root, ph, 2, 2);
105            checkState(p1, ph, 1, 1);
106            checkState(p2, ph, 2, 2);
107        }
108        equal(3, ph);
109    }
110
111    void xtestTiered() throws Throwable {
112        Phaser root = new Phaser();
113        stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
114        checkState(root, Integer.MAX_VALUE - 1, 0, 0);
115        Phaser p1 = new Phaser(root, 1);
116        checkState(root, Integer.MAX_VALUE - 1, 1, 1);
117        checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
118        Phaser p2 = new Phaser(root, 2);
119        checkState(root, Integer.MAX_VALUE - 1, 2, 2);
120        checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
121        int ph = Integer.MAX_VALUE - 1;
122        for (int k = 0; k < 5; k++) {
123            checkState(root, ph, 2, 2);
124            checkState(p1, ph, 1, 1);
125            checkState(p2, ph, 2, 2);
126            p1.arrive();
127            checkState(root, ph, 2, 1);
128            checkState(p1, ph, 1, 0);
129            checkState(p2, ph, 2, 2);
130            p2.arrive();
131            checkState(root, ph, 2, 1);
132            checkState(p1, ph, 1, 0);
133            checkState(p2, ph, 2, 1);
134            p2.arrive();
135            ph = phaseInc(ph);
136            checkState(root, ph, 2, 2);
137            checkState(p1, ph, 1, 1);
138            checkState(p2, ph, 2, 2);
139        }
140        equal(3, ph);
141    }
142
143    //--------------------- Infrastructure ---------------------------
144    volatile int passed = 0, failed = 0;
145    void pass() {passed++;}
146    void fail() {failed++; Thread.dumpStack();}
147    void fail(String msg) {System.err.println(msg); fail();}
148    void unexpected(Throwable t) {failed++; t.printStackTrace();}
149    void check(boolean cond) {if (cond) pass(); else fail();}
150    void equal(Object x, Object y) {
151        if (x == null ? y == null : x.equals(y)) pass();
152        else fail(x + " not equal to " + y);}
153    public static void main(String[] args) throws Throwable {
154        new PhaseOverflow().instanceMain(args);}
155    public void instanceMain(String[] args) throws Throwable {
156        try {test(args);} catch (Throwable t) {unexpected(t);}
157        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
158        if (failed > 0) throw new AssertionError("Some tests failed");}
159}
160