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