|
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 TestUnsafeVolatileWeakCAS |
|
30 { |
|
31 public volatile int f_int = 0; |
|
32 public volatile Integer f_obj = Integer.valueOf(0); |
|
33 public volatile long f_long = 0; |
|
34 public volatile byte f_byte = 0; |
|
35 public volatile short f_short = 0; |
|
36 |
|
37 public static Unsafe unsafe = Unsafe.getUnsafe(); |
|
38 public static Field f_int_field; |
|
39 public static Field f_obj_field; |
|
40 public static Field f_long_field; |
|
41 public static Field f_byte_field; |
|
42 public static Field f_short_field; |
|
43 public static long f_int_off; |
|
44 public static long f_obj_off; |
|
45 public static long f_long_off; |
|
46 public static long f_byte_off; |
|
47 public static long f_short_off; |
|
48 |
|
49 static { |
|
50 try { |
|
51 f_int_field = TestUnsafeVolatileWeakCAS.class.getField("f_int"); |
|
52 f_obj_field = TestUnsafeVolatileWeakCAS.class.getField("f_obj"); |
|
53 f_long_field = TestUnsafeVolatileWeakCAS.class.getField("f_long"); |
|
54 f_byte_field = TestUnsafeVolatileWeakCAS.class.getField("f_byte"); |
|
55 f_short_field = TestUnsafeVolatileWeakCAS.class.getField("f_short"); |
|
56 f_int_off = unsafe.objectFieldOffset(f_int_field); |
|
57 f_obj_off = unsafe.objectFieldOffset(f_obj_field); |
|
58 f_long_off = unsafe.objectFieldOffset(f_long_field); |
|
59 f_byte_off = unsafe.objectFieldOffset(f_byte_field); |
|
60 f_short_off = unsafe.objectFieldOffset(f_short_field); |
|
61 } catch (Exception e) { |
|
62 System.out.println("reflection failed " + e); |
|
63 e.printStackTrace(); |
|
64 } |
|
65 } |
|
66 |
|
67 public static void main(String[] args) |
|
68 { |
|
69 final TestUnsafeVolatileWeakCAS t = new TestUnsafeVolatileWeakCAS(); |
|
70 for (int i = 0; i < 100_000; i++) { |
|
71 t.f_int = -1; |
|
72 if (t.testInt(-1, i)) { |
|
73 if (t.f_int != i) { |
|
74 throw new RuntimeException("bad result!"); |
|
75 } |
|
76 } |
|
77 } |
|
78 for (int i = 0; i < 100_000; i++) { |
|
79 t.f_long = -1; |
|
80 if (t.testLong(-1, i)) { |
|
81 if (t.f_long != i) { |
|
82 throw new RuntimeException("bad result!"); |
|
83 } |
|
84 } |
|
85 } |
|
86 for (int i = 0; i < 100_000; i++) { |
|
87 t.f_byte = -1; |
|
88 byte i_b = (byte)i; |
|
89 if (t.testByte((byte)-1, i_b)) { |
|
90 if (t.f_byte != i_b) { |
|
91 throw new RuntimeException("bad result!"); |
|
92 } |
|
93 } |
|
94 } |
|
95 for (int i = 0; i < 100_000; i++) { |
|
96 t.f_short = -1; |
|
97 short i_s = (short)i; |
|
98 if (t.testShort((byte)-1, i_s)) { |
|
99 if (t.f_short != i_s) { |
|
100 throw new RuntimeException("bad result!"); |
|
101 } |
|
102 } |
|
103 } |
|
104 Integer minusOne = Integer.valueOf(-1); |
|
105 for (int i = 0; i < 100_000; i++) { |
|
106 t.f_obj = minusOne; |
|
107 if (t.testObj(minusOne, Integer.valueOf(i))) { |
|
108 if (t.f_obj != i) { |
|
109 throw new RuntimeException("bad result!"); |
|
110 } |
|
111 } |
|
112 } |
|
113 } |
|
114 |
|
115 public boolean testInt(int x, int i) |
|
116 { |
|
117 return unsafe.weakCompareAndSetInt(this, f_int_off, x, i); |
|
118 } |
|
119 |
|
120 public boolean testObj(Object x, Object o) |
|
121 { |
|
122 return unsafe.weakCompareAndSetReference(this, f_obj_off, x, o); |
|
123 } |
|
124 |
|
125 public boolean testLong(long x, long i) |
|
126 { |
|
127 return unsafe.weakCompareAndSetLong(this, f_long_off, x, i); |
|
128 } |
|
129 |
|
130 public boolean testByte(byte x, byte i) |
|
131 { |
|
132 return unsafe.weakCompareAndSetByte(this, f_byte_off, x, i); |
|
133 } |
|
134 |
|
135 public boolean testShort(short x, short i) |
|
136 { |
|
137 return unsafe.weakCompareAndSetShort(this, f_short_off, x, i); |
|
138 } |
|
139 } |