SunMiscUnsafeAccessTestBoolean.java revision 10551:b64432bae527
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for boolean
28 * @modules jdk.unsupported/sun.misc
29 * @run testng/othervm -Diters=100   -Xint                   SunMiscUnsafeAccessTestBoolean
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestBoolean
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  SunMiscUnsafeAccessTestBoolean
32 * @run testng/othervm -Diters=20000                         SunMiscUnsafeAccessTestBoolean
33 */
34
35import org.testng.annotations.Test;
36
37import java.lang.reflect.Field;
38
39import static org.testng.Assert.*;
40
41public class SunMiscUnsafeAccessTestBoolean {
42    static final int ITERS = Integer.getInteger("iters", 1);
43
44    static final sun.misc.Unsafe UNSAFE;
45
46    static final long V_OFFSET;
47
48    static final Object STATIC_V_BASE;
49
50    static final long STATIC_V_OFFSET;
51
52    static int ARRAY_OFFSET;
53
54    static int ARRAY_SHIFT;
55
56    static {
57        try {
58            Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
59            f.setAccessible(true);
60            UNSAFE = (sun.misc.Unsafe) f.get(null);
61        } catch (Exception e) {
62            throw new RuntimeException("Unable to get Unsafe instance.", e);
63        }
64
65        try {
66            Field staticVField = SunMiscUnsafeAccessTestBoolean.class.getDeclaredField("static_v");
67            STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
68            STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
69        } catch (Exception e) {
70            throw new RuntimeException(e);
71        }
72
73        try {
74            Field vField = SunMiscUnsafeAccessTestBoolean.class.getDeclaredField("v");
75            V_OFFSET = UNSAFE.objectFieldOffset(vField);
76        } catch (Exception e) {
77            throw new RuntimeException(e);
78        }
79
80        ARRAY_OFFSET = UNSAFE.arrayBaseOffset(boolean[].class);
81        int ascale = UNSAFE.arrayIndexScale(boolean[].class);
82        ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
83    }
84
85    static boolean static_v;
86
87    boolean v;
88
89    @Test
90    public void testFieldInstance() {
91        SunMiscUnsafeAccessTestBoolean t = new SunMiscUnsafeAccessTestBoolean();
92        for (int c = 0; c < ITERS; c++) {
93            testAccess(t, V_OFFSET);
94        }
95    }
96
97    @Test
98    public void testFieldStatic() {
99        for (int c = 0; c < ITERS; c++) {
100            testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
101        }
102    }
103
104    @Test
105    public void testArray() {
106        boolean[] array = new boolean[10];
107        for (int c = 0; c < ITERS; c++) {
108            for (int i = 0; i < array.length; i++) {
109                testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
110            }
111        }
112    }
113
114
115    static void testAccess(Object base, long offset) {
116        // Plain
117        {
118            UNSAFE.putBoolean(base, offset, true);
119            boolean x = UNSAFE.getBoolean(base, offset);
120            assertEquals(x, true, "set boolean value");
121        }
122
123        // Volatile
124        {
125            UNSAFE.putBooleanVolatile(base, offset, false);
126            boolean x = UNSAFE.getBooleanVolatile(base, offset);
127            assertEquals(x, false, "putVolatile boolean value");
128        }
129
130
131
132
133
134
135    }
136
137}
138
139
140