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 TestUnsafeVolatileCAS
  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 = TestUnsafeVolatileCAS.class.getField("f_int");
  43             f_obj_field = TestUnsafeVolatileCAS.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 TestUnsafeVolatileCAS t = new TestUnsafeVolatileCAS();
  55         for (int i = 0; i < 100_000; i++) {
  56             t.f_int = -1;
  57             t.testInt(-1, i);
  58             if (t.f_int != i) {
  59                 throw new RuntimeException("bad result!");
  60             }
  61         }
  62         Integer minusOne = Integer.valueOf(-1);
  63         for (int i = 0; i < 100_000; i++) {
  64             t.f_obj = minusOne;
  65             t.testObj(minusOne, Integer.valueOf(i));
  66             if (t.f_obj != i) {
  67                 throw new RuntimeException("bad result!");
  68             }
  69         }
  70     }
  71     public void testInt(int x, int i)
  72     {
  73         unsafe.compareAndSetInt(this, f_int_off, x, i);
  74     }
  75 
  76     public void testObj(Object x, Object o)
  77     {
  78         unsafe.compareAndSetReference(this, f_obj_off, x, o);
  79     }
  80 }