JdkInternalMiscUnsafeAccessTestBoolean.java revision 12787:5242609b8088
13071Sache/*
23071Sache * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33071Sache * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43071Sache *
53071Sache * This code is free software; you can redistribute it and/or modify it
63071Sache * under the terms of the GNU General Public License version 2 only, as
73071Sache * published by the Free Software Foundation.
83071Sache *
93071Sache * This code is distributed in the hope that it will be useful, but WITHOUT
103071Sache * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
113071Sache * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
123071Sache * version 2 for more details (a copy is included in the LICENSE file that
133071Sache * accompanied this code).
143071Sache *
153071Sache * You should have received a copy of the GNU General Public License version
163071Sache * 2 along with this work; if not, write to the Free Software Foundation,
173071Sache * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
183071Sache *
193071Sache * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
203071Sache * or visit www.oracle.com if you need additional information or have any
213071Sache * questions.
223071Sache */
233071Sache
243071Sache/*
253071Sache * @test
263071Sache * @bug 8143628
273071Sache * @summary Test unsafe access for boolean
283071Sache *
293071Sache * @modules java.base/jdk.internal.misc:+open
303071Sache * @run testng/othervm -Diters=100   -Xint                   compiler.unsafe.JdkInternalMiscUnsafeAccessTestBoolean
313071Sache * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.JdkInternalMiscUnsafeAccessTestBoolean
323071Sache * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  compiler.unsafe.JdkInternalMiscUnsafeAccessTestBoolean
333071Sache * @run testng/othervm -Diters=20000                         compiler.unsafe.JdkInternalMiscUnsafeAccessTestBoolean
343071Sache */
353071Sache
363071Sachepackage compiler.unsafe;
373071Sache
383071Sacheimport org.testng.annotations.Test;
393071Sache
403071Sacheimport java.lang.reflect.Field;
413071Sache
423071Sacheimport static org.testng.Assert.*;
433071Sache
443071Sachepublic class JdkInternalMiscUnsafeAccessTestBoolean {
453071Sache    static final int ITERS = Integer.getInteger("iters", 1);
463071Sache    static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
473071Sache
483071Sache    static final jdk.internal.misc.Unsafe UNSAFE;
493071Sache
503071Sache    static final long V_OFFSET;
513071Sache
523071Sache    static final Object STATIC_V_BASE;
533071Sache
543071Sache    static final long STATIC_V_OFFSET;
553071Sache
563071Sache    static int ARRAY_OFFSET;
573071Sache
583071Sache    static int ARRAY_SHIFT;
593071Sache
603071Sache    static {
613071Sache        try {
623071Sache            Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
633071Sache            f.setAccessible(true);
643071Sache            UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
653071Sache        } catch (Exception e) {
663071Sache            throw new RuntimeException("Unable to get Unsafe instance.", e);
673071Sache        }
683071Sache
693071Sache        try {
703071Sache            Field staticVField = JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("static_v");
713071Sache            STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
723071Sache            STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
733071Sache        } catch (Exception e) {
743071Sache            throw new RuntimeException(e);
753071Sache        }
763071Sache
773071Sache        try {
783071Sache            Field vField = JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("v");
793071Sache            V_OFFSET = UNSAFE.objectFieldOffset(vField);
803071Sache        } catch (Exception e) {
813071Sache            throw new RuntimeException(e);
823071Sache        }
833071Sache
843071Sache        ARRAY_OFFSET = UNSAFE.arrayBaseOffset(boolean[].class);
853071Sache        int ascale = UNSAFE.arrayIndexScale(boolean[].class);
863071Sache        ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
873071Sache    }
883071Sache
893071Sache    static boolean static_v;
903071Sache
913071Sache    boolean v;
923071Sache
933071Sache    @Test
943071Sache    public void testFieldInstance() {
953071Sache        JdkInternalMiscUnsafeAccessTestBoolean t = new JdkInternalMiscUnsafeAccessTestBoolean();
96        for (int c = 0; c < ITERS; c++) {
97            testAccess(t, V_OFFSET);
98        }
99    }
100
101    @Test
102    public void testFieldStatic() {
103        for (int c = 0; c < ITERS; c++) {
104            testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
105        }
106    }
107
108    @Test
109    public void testArray() {
110        boolean[] array = new boolean[10];
111        for (int c = 0; c < ITERS; c++) {
112            for (int i = 0; i < array.length; i++) {
113                testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
114            }
115        }
116    }
117
118
119    static void testAccess(Object base, long offset) {
120        // Plain
121        {
122            UNSAFE.putBoolean(base, offset, true);
123            boolean x = UNSAFE.getBoolean(base, offset);
124            assertEquals(x, true, "set boolean value");
125        }
126
127        // Volatile
128        {
129            UNSAFE.putBooleanVolatile(base, offset, false);
130            boolean x = UNSAFE.getBooleanVolatile(base, offset);
131            assertEquals(x, false, "putVolatile boolean value");
132        }
133
134
135        // Lazy
136        {
137            UNSAFE.putBooleanRelease(base, offset, true);
138            boolean x = UNSAFE.getBooleanAcquire(base, offset);
139            assertEquals(x, true, "putRelease boolean value");
140        }
141
142        // Opaque
143        {
144            UNSAFE.putBooleanOpaque(base, offset, false);
145            boolean x = UNSAFE.getBooleanOpaque(base, offset);
146            assertEquals(x, false, "putOpaque boolean value");
147        }
148
149
150        UNSAFE.putBoolean(base, offset, true);
151
152        // Compare
153        {
154            boolean r = UNSAFE.compareAndSetBoolean(base, offset, true, false);
155            assertEquals(r, true, "success compareAndSet boolean");
156            boolean x = UNSAFE.getBoolean(base, offset);
157            assertEquals(x, false, "success compareAndSet boolean value");
158        }
159
160        {
161            boolean r = UNSAFE.compareAndSetBoolean(base, offset, true, false);
162            assertEquals(r, false, "failing compareAndSet boolean");
163            boolean x = UNSAFE.getBoolean(base, offset);
164            assertEquals(x, false, "failing compareAndSet boolean value");
165        }
166
167        // Advanced compare
168        {
169            boolean r = UNSAFE.compareAndExchangeBoolean(base, offset, false, true);
170            assertEquals(r, false, "success compareAndExchange boolean");
171            boolean x = UNSAFE.getBoolean(base, offset);
172            assertEquals(x, true, "success compareAndExchange boolean value");
173        }
174
175        {
176            boolean r = UNSAFE.compareAndExchangeBoolean(base, offset, false, false);
177            assertEquals(r, true, "failing compareAndExchange boolean");
178            boolean x = UNSAFE.getBoolean(base, offset);
179            assertEquals(x, true, "failing compareAndExchange boolean value");
180        }
181
182        {
183            boolean r = UNSAFE.compareAndExchangeBooleanAcquire(base, offset, true, false);
184            assertEquals(r, true, "success compareAndExchangeAcquire boolean");
185            boolean x = UNSAFE.getBoolean(base, offset);
186            assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
187        }
188
189        {
190            boolean r = UNSAFE.compareAndExchangeBooleanAcquire(base, offset, true, false);
191            assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
192            boolean x = UNSAFE.getBoolean(base, offset);
193            assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
194        }
195
196        {
197            boolean r = UNSAFE.compareAndExchangeBooleanRelease(base, offset, false, true);
198            assertEquals(r, false, "success compareAndExchangeRelease boolean");
199            boolean x = UNSAFE.getBoolean(base, offset);
200            assertEquals(x, true, "success compareAndExchangeRelease boolean value");
201        }
202
203        {
204            boolean r = UNSAFE.compareAndExchangeBooleanRelease(base, offset, false, false);
205            assertEquals(r, true, "failing compareAndExchangeRelease boolean");
206            boolean x = UNSAFE.getBoolean(base, offset);
207            assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
208        }
209
210        {
211            boolean success = false;
212            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
213                success = UNSAFE.weakCompareAndSetBooleanPlain(base, offset, true, false);
214            }
215            assertEquals(success, true, "weakCompareAndSetPlain boolean");
216            boolean x = UNSAFE.getBoolean(base, offset);
217            assertEquals(x, false, "weakCompareAndSetPlain boolean value");
218        }
219
220        {
221            boolean success = false;
222            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
223                success = UNSAFE.weakCompareAndSetBooleanAcquire(base, offset, false, true);
224            }
225            assertEquals(success, true, "weakCompareAndSetAcquire boolean");
226            boolean x = UNSAFE.getBoolean(base, offset);
227            assertEquals(x, true, "weakCompareAndSetAcquire boolean");
228        }
229
230        {
231            boolean success = false;
232            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
233                success = UNSAFE.weakCompareAndSetBooleanRelease(base, offset, true, false);
234            }
235            assertEquals(success, true, "weakCompareAndSetRelease boolean");
236            boolean x = UNSAFE.getBoolean(base, offset);
237            assertEquals(x, false, "weakCompareAndSetRelease boolean");
238        }
239
240        {
241            boolean success = false;
242            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
243                success = UNSAFE.weakCompareAndSetBoolean(base, offset, false, true);
244            }
245            assertEquals(success, true, "weakCompareAndSet boolean");
246            boolean x = UNSAFE.getBoolean(base, offset);
247            assertEquals(x, true, "weakCompareAndSet boolean");
248        }
249
250        UNSAFE.putBoolean(base, offset, false);
251
252        // Compare set and get
253        {
254            boolean o = UNSAFE.getAndSetBoolean(base, offset, true);
255            assertEquals(o, false, "getAndSet boolean");
256            boolean x = UNSAFE.getBoolean(base, offset);
257            assertEquals(x, true, "getAndSet boolean value");
258        }
259
260    }
261
262}
263