23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for Object
28 * @modules java.base/jdk.internal.misc
29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestObject
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestObject
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestObject
32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestObject
33 */
34
35 import org.testng.annotations.Test;
36
37 import java.lang.reflect.Field;
38
39 import static org.testng.Assert.*;
40
41 public class JdkInternalMiscUnsafeAccessTestObject {
42 static final int ITERS = Integer.getInteger("iters", 1);
43
44 static final jdk.internal.misc.Unsafe UNSAFE;
45
46 static final long V_OFFSET;
47
48 static final Object STATIC_V_BASE;
49
50 static final long STATIC_V_OFFSET;
51
52 static int ARRAY_OFFSET;
53
54 static int ARRAY_SHIFT;
55
56 static {
57 try {
58 Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
59 f.setAccessible(true);
60 UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
61 } catch (Exception e) {
62 throw new RuntimeException("Unable to get Unsafe instance.", e);
187 assertEquals(r, "bar", "failing compareAndExchangeAcquire Object");
188 Object x = UNSAFE.getObject(base, offset);
189 assertEquals(x, "bar", "failing compareAndExchangeAcquire Object value");
190 }
191
192 {
193 Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "foo");
194 assertEquals(r, "bar", "success compareAndExchangeRelease Object");
195 Object x = UNSAFE.getObject(base, offset);
196 assertEquals(x, "foo", "success compareAndExchangeRelease Object value");
197 }
198
199 {
200 Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "baz");
201 assertEquals(r, "foo", "failing compareAndExchangeRelease Object");
202 Object x = UNSAFE.getObject(base, offset);
203 assertEquals(x, "foo", "failing compareAndExchangeRelease Object value");
204 }
205
206 {
207 boolean r = UNSAFE.weakCompareAndSwapObject(base, offset, "foo", "bar");
208 assertEquals(r, true, "weakCompareAndSwap Object");
209 Object x = UNSAFE.getObject(base, offset);
210 assertEquals(x, "bar", "weakCompareAndSwap Object value");
211 }
212
213 {
214 boolean r = UNSAFE.weakCompareAndSwapObjectAcquire(base, offset, "bar", "foo");
215 assertEquals(r, true, "weakCompareAndSwapAcquire Object");
216 Object x = UNSAFE.getObject(base, offset);
217 assertEquals(x, "foo", "weakCompareAndSwapAcquire Object");
218 }
219
220 {
221 boolean r = UNSAFE.weakCompareAndSwapObjectRelease(base, offset, "foo", "bar");
222 assertEquals(r, true, "weakCompareAndSwapRelease Object");
223 Object x = UNSAFE.getObject(base, offset);
224 assertEquals(x, "bar", "weakCompareAndSwapRelease Object");
225 }
226
227 // Compare set and get
228 {
229 Object o = UNSAFE.getAndSetObject(base, offset, "foo");
230 assertEquals(o, "bar", "getAndSet Object");
231 Object x = UNSAFE.getObject(base, offset);
232 assertEquals(x, "foo", "getAndSet Object value");
233 }
234
235 }
236
237 }
238
239
|
23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for Object
28 * @modules java.base/jdk.internal.misc
29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestObject
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestObject
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestObject
32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestObject
33 */
34
35 import org.testng.annotations.Test;
36
37 import java.lang.reflect.Field;
38
39 import static org.testng.Assert.*;
40
41 public class JdkInternalMiscUnsafeAccessTestObject {
42 static final int ITERS = Integer.getInteger("iters", 1);
43 static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
44
45 static final jdk.internal.misc.Unsafe UNSAFE;
46
47 static final long V_OFFSET;
48
49 static final Object STATIC_V_BASE;
50
51 static final long STATIC_V_OFFSET;
52
53 static int ARRAY_OFFSET;
54
55 static int ARRAY_SHIFT;
56
57 static {
58 try {
59 Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
60 f.setAccessible(true);
61 UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
62 } catch (Exception e) {
63 throw new RuntimeException("Unable to get Unsafe instance.", e);
188 assertEquals(r, "bar", "failing compareAndExchangeAcquire Object");
189 Object x = UNSAFE.getObject(base, offset);
190 assertEquals(x, "bar", "failing compareAndExchangeAcquire Object value");
191 }
192
193 {
194 Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "foo");
195 assertEquals(r, "bar", "success compareAndExchangeRelease Object");
196 Object x = UNSAFE.getObject(base, offset);
197 assertEquals(x, "foo", "success compareAndExchangeRelease Object value");
198 }
199
200 {
201 Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "baz");
202 assertEquals(r, "foo", "failing compareAndExchangeRelease Object");
203 Object x = UNSAFE.getObject(base, offset);
204 assertEquals(x, "foo", "failing compareAndExchangeRelease Object value");
205 }
206
207 {
208 boolean success = false;
209 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
210 success = UNSAFE.weakCompareAndSwapObject(base, offset, "foo", "bar");
211 }
212 assertEquals(success, true, "weakCompareAndSwap Object");
213 Object x = UNSAFE.getObject(base, offset);
214 assertEquals(x, "bar", "weakCompareAndSwap Object value");
215 }
216
217 {
218 boolean success = false;
219 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
220 success = UNSAFE.weakCompareAndSwapObjectAcquire(base, offset, "bar", "foo");
221 }
222 assertEquals(success, true, "weakCompareAndSwapAcquire Object");
223 Object x = UNSAFE.getObject(base, offset);
224 assertEquals(x, "foo", "weakCompareAndSwapAcquire Object");
225 }
226
227 {
228 boolean success = false;
229 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
230 success = UNSAFE.weakCompareAndSwapObjectRelease(base, offset, "foo", "bar");
231 }
232 assertEquals(success, true, "weakCompareAndSwapRelease Object");
233 Object x = UNSAFE.getObject(base, offset);
234 assertEquals(x, "bar", "weakCompareAndSwapRelease Object");
235 }
236
237 // Compare set and get
238 {
239 Object o = UNSAFE.getAndSetObject(base, offset, "foo");
240 assertEquals(o, "bar", "getAndSet Object");
241 Object x = UNSAFE.getObject(base, offset);
242 assertEquals(x, "foo", "getAndSet Object value");
243 }
244
245 }
246
247 }
248
|