1 package compiler.c2.aarch64; 2 3 import java.lang.reflect.Field; 4 import jdk.internal.misc.Unsafe; 5 6 class TestUnsafeVolatileLoad 7 { 8 public volatile int f_int = 0; 9 public volatile Integer f_obj = Integer.valueOf(0); 10 11 public static Unsafe unsafe = Unsafe.getUnsafe(); 12 public static Field f_int_field; 13 public static Field f_obj_field; 14 public static long f_int_off; 15 public static long f_obj_off; 16 17 static { 18 try { 19 f_int_field = TestUnsafeVolatileLoad.class.getField("f_int"); 20 f_obj_field = TestUnsafeVolatileLoad.class.getField("f_obj"); 21 f_int_off = unsafe.objectFieldOffset(f_int_field); 22 f_obj_off = unsafe.objectFieldOffset(f_obj_field); 23 } catch (Exception e) { 24 System.out.println("reflection failed " + e); 25 e.printStackTrace(); 26 } 27 } 28 29 public static void main(String[] args) 30 { 31 final TestUnsafeVolatileLoad t = new TestUnsafeVolatileLoad(); 32 for (int i = 0; i < 100_000; i++) { 33 t.f_int = i; 34 int r = t.testInt(); 35 if (r != i) { 36 throw new RuntimeException("bad result!"); 37 } 38 } 39 for (int i = 0; i < 100_000; i++) { 40 t.f_obj = Integer.valueOf(i); 41 int r = t.testObj(); 42 if (r != i) { 43 throw new RuntimeException("bad result!"); 44 } 45 } 46 } 47 public int testInt() 48 { 49 return unsafe.getIntVolatile(this, f_int_off); 50 } 51 52 public int testObj() 53 { 54 return ((Integer)unsafe.getObjectVolatile(this, f_obj_off)); 55 } 56 }