test/hotspot/jtreg/compiler/c2/aarch64/TestUnsafeVolatileStore.java
changeset 50874 551c340ca01a
child 52220 9c260a6b6471
equal deleted inserted replaced
50873:ce53844224b6 50874:551c340ca01a
       
     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 TestUnsafeVolatileStore
       
    30 {
       
    31     public volatile int f_int = 0;
       
    32     public volatile Integer f_obj = Integer.valueOf(0);
       
    33 
       
    34     public static Unsafe unsafe = Unsafe.getUnsafe();
       
    35     public static Field f_int_field;
       
    36     public static Field f_obj_field;
       
    37     public static long f_int_off;
       
    38     public static long f_obj_off;
       
    39 
       
    40     static {
       
    41         try {
       
    42             f_int_field = TestUnsafeVolatileStore.class.getField("f_int");
       
    43             f_obj_field = TestUnsafeVolatileStore.class.getField("f_obj");
       
    44             f_int_off = unsafe.objectFieldOffset(f_int_field);
       
    45             f_obj_off = unsafe.objectFieldOffset(f_obj_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 TestUnsafeVolatileStore t = new TestUnsafeVolatileStore();
       
    55         for (int i = 0; i < 100_000; i++) {
       
    56             t.f_int = -1;
       
    57             t.testInt(i);
       
    58             if (t.f_int != i) {
       
    59                 throw new RuntimeException("bad result!");
       
    60             }
       
    61         }
       
    62         for (int i = 0; i < 100_000; i++) {
       
    63             t.f_obj = null;
       
    64             t.testObj(Integer.valueOf(i));
       
    65             if (t.f_obj != i) {
       
    66                 throw new RuntimeException("bad result!");
       
    67             }
       
    68         }
       
    69     }
       
    70     public void testInt(int i)
       
    71     {
       
    72         unsafe.putIntVolatile(this, f_int_off, i);
       
    73     }
       
    74 
       
    75     public void testObj(Object o)
       
    76     {
       
    77         unsafe.putObjectVolatile(this, f_obj_off, o);
       
    78     }
       
    79 }