test/hotspot/jtreg/compiler/c2/aarch64/TestUnsafeVolatileCAE.java
changeset 52409 87bc444ca642
equal deleted inserted replaced
52408:04cbcebf5adf 52409:87bc444ca642
       
     1 /*
       
     2  * Copyright (c) 2018, Red Hat, Inc. 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 package compiler.c2.aarch64;
       
    25 
       
    26 import java.lang.reflect.Field;
       
    27 import jdk.internal.misc.Unsafe;
       
    28 
       
    29 class TestUnsafeVolatileCAE
       
    30 {
       
    31     public volatile int f_int = 0;
       
    32     public volatile Integer f_obj = Integer.valueOf(0);
       
    33     public volatile long f_long = 0;
       
    34     public volatile byte f_byte = 0;
       
    35     public volatile short f_short = 0;
       
    36 
       
    37     public static Unsafe unsafe = Unsafe.getUnsafe();
       
    38     public static Field f_int_field;
       
    39     public static Field f_obj_field;
       
    40     public static Field f_long_field;
       
    41     public static Field f_byte_field;
       
    42     public static Field f_short_field;
       
    43     public static long f_int_off;
       
    44     public static long f_obj_off;
       
    45     public static long f_long_off;
       
    46     public static long f_byte_off;
       
    47     public static long f_short_off;
       
    48 
       
    49     static {
       
    50         try {
       
    51             f_int_field = TestUnsafeVolatileCAE.class.getField("f_int");
       
    52             f_obj_field = TestUnsafeVolatileCAE.class.getField("f_obj");
       
    53             f_long_field = TestUnsafeVolatileCAE.class.getField("f_long");
       
    54             f_byte_field = TestUnsafeVolatileCAE.class.getField("f_byte");
       
    55             f_short_field = TestUnsafeVolatileCAE.class.getField("f_short");
       
    56             f_int_off = unsafe.objectFieldOffset(f_int_field);
       
    57             f_obj_off = unsafe.objectFieldOffset(f_obj_field);
       
    58             f_long_off = unsafe.objectFieldOffset(f_long_field);
       
    59             f_byte_off = unsafe.objectFieldOffset(f_byte_field);
       
    60             f_short_off = unsafe.objectFieldOffset(f_short_field);
       
    61         } catch (Exception e) {
       
    62             System.out.println("reflection failed " + e);
       
    63             e.printStackTrace();
       
    64         }
       
    65     }
       
    66 
       
    67     public static void main(String[] args)
       
    68     {
       
    69         final TestUnsafeVolatileCAE t = new TestUnsafeVolatileCAE();
       
    70         for (int i = 0; i < 100_000; i++) {
       
    71             t.f_int = -1;
       
    72             int res = t.testInt(-1, i);
       
    73             if (res != -1 || t.f_int != i) {
       
    74                 throw new RuntimeException("bad result!");
       
    75             }
       
    76         }
       
    77         for (int i = 0; i < 100_000; i++) {
       
    78             t.f_long = -1;
       
    79             long res = t.testLong(-1, i);
       
    80             if (res != -1 || t.f_long != i) {
       
    81                 throw new RuntimeException("bad result!");
       
    82             }
       
    83         }
       
    84         for (int i = 0; i < 100_000; i++) {
       
    85             t.f_byte = -1;
       
    86             byte i_b = (byte)i;
       
    87             byte res = t.testByte((byte)-1, i_b);
       
    88             if (res != (byte)-1 || t.f_byte != i_b) {
       
    89                 throw new RuntimeException("bad result!");
       
    90             }
       
    91         }
       
    92         for (int i = 0; i < 100_000; i++) {
       
    93             t.f_short = -1;
       
    94             short i_s = (short)i;
       
    95             short res = t.testShort((byte)-1, i_s);
       
    96             if (res != (short)-1 || t.f_short != i_s) {
       
    97                 throw new RuntimeException("bad result!");
       
    98             }
       
    99         }
       
   100         Integer minusOne = Integer.valueOf(-1);
       
   101         for (int i = 0; i < 100_000; i++) {
       
   102             t.f_obj = minusOne;
       
   103             Object res = t.testObj(minusOne, Integer.valueOf(i));
       
   104             if (res != minusOne || t.f_obj != i) {
       
   105                 throw new RuntimeException("bad result!");
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110     public int testInt(int x, int i)
       
   111     {
       
   112         return unsafe.compareAndExchangeInt(this, f_int_off, x, i);
       
   113     }
       
   114 
       
   115     public Object testObj(Object x, Object o)
       
   116     {
       
   117         return unsafe.compareAndExchangeReference(this, f_obj_off, x, o);
       
   118     }
       
   119     public long testLong(long x, long i)
       
   120     {
       
   121         return unsafe.compareAndExchangeLong(this, f_long_off, x, i);
       
   122     }
       
   123 
       
   124     public byte testByte(byte x, byte i)
       
   125     {
       
   126         return unsafe.compareAndExchangeByte(this, f_byte_off, x, i);
       
   127     }
       
   128 
       
   129     public short testShort(short x, short i)
       
   130     {
       
   131         return unsafe.compareAndExchangeShort(this, f_short_off, x, i);
       
   132     }
       
   133 }