test/hotspot/jtreg/compiler/c2/aarch64/TestUnsafeVolatileGAS.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 TestUnsafeVolatileGAS
       
    30 {
       
    31     public volatile int f_int = -1;
       
    32     public volatile Integer f_obj = Integer.valueOf(-1);
       
    33     public volatile long f_long = -1;
       
    34 
       
    35     public static Unsafe unsafe = Unsafe.getUnsafe();
       
    36     public static Field f_int_field;
       
    37     public static Field f_obj_field;
       
    38     public static Field f_long_field;
       
    39     public static long f_int_off;
       
    40     public static long f_obj_off;
       
    41     public static long f_long_off;
       
    42 
       
    43     static {
       
    44         try {
       
    45             f_int_field = TestUnsafeVolatileGAS.class.getField("f_int");
       
    46             f_obj_field = TestUnsafeVolatileGAS.class.getField("f_obj");
       
    47             f_long_field = TestUnsafeVolatileGAS.class.getField("f_long");
       
    48             f_int_off = unsafe.objectFieldOffset(f_int_field);
       
    49             f_obj_off = unsafe.objectFieldOffset(f_obj_field);
       
    50             f_long_off = unsafe.objectFieldOffset(f_long_field);
       
    51         } catch (Exception e) {
       
    52             System.out.println("reflection failed " + e);
       
    53             e.printStackTrace();
       
    54         }
       
    55     }
       
    56 
       
    57     public static void main(String[] args)
       
    58     {
       
    59         final TestUnsafeVolatileGAS t = new TestUnsafeVolatileGAS();
       
    60         for (int i = 0; i < 100_000; i++) {
       
    61             if (t.testInt(i) != i-1) {
       
    62                 throw new RuntimeException("bad result!");
       
    63             }
       
    64         }
       
    65         for (int i = 0; i < 100_000; i++) {
       
    66             if (t.testLong(i) != i-1) {
       
    67                 throw new RuntimeException("bad result!");
       
    68             }
       
    69         }
       
    70         for (int i = 0; i < 100_000; i++) {
       
    71             if ((Integer)t.testObj(Integer.valueOf(i)) != i-1) {
       
    72                 throw new RuntimeException("bad result!");
       
    73             }
       
    74         }
       
    75     }
       
    76 
       
    77     public int testInt(int i)
       
    78     {
       
    79         return unsafe.getAndSetInt(this, f_int_off, i);
       
    80     }
       
    81 
       
    82     public Object testObj(Object o)
       
    83     {
       
    84         return unsafe.getAndSetReference(this, f_obj_off, o);
       
    85     }
       
    86     public long testLong(long i)
       
    87     {
       
    88         return unsafe.getAndSetLong(this, f_long_off, i);
       
    89     }
       
    90 }