23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for long
28 * @modules java.base/jdk.internal.misc
29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestLong
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestLong
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestLong
32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestLong
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 JdkInternalMiscUnsafeAccessTestLong {
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);
234 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
235 long x = UNSAFE.getLong(base, offset);
236 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
237 }
238
239 {
240 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 2L, 1L);
241 assertEquals(r, 2L, "success compareAndExchangeRelease long");
242 long x = UNSAFE.getLong(base, offset);
243 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
244 }
245
246 {
247 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 2L, 3L);
248 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
249 long x = UNSAFE.getLong(base, offset);
250 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
251 }
252
253 {
254 boolean r = UNSAFE.weakCompareAndSwapLong(base, offset, 1L, 2L);
255 assertEquals(r, true, "weakCompareAndSwap long");
256 long x = UNSAFE.getLong(base, offset);
257 assertEquals(x, 2L, "weakCompareAndSwap long value");
258 }
259
260 {
261 boolean r = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 2L, 1L);
262 assertEquals(r, true, "weakCompareAndSwapAcquire long");
263 long x = UNSAFE.getLong(base, offset);
264 assertEquals(x, 1L, "weakCompareAndSwapAcquire long");
265 }
266
267 {
268 boolean r = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 1L, 2L);
269 assertEquals(r, true, "weakCompareAndSwapRelease long");
270 long x = UNSAFE.getLong(base, offset);
271 assertEquals(x, 2L, "weakCompareAndSwapRelease long");
272 }
273
274 // Compare set and get
275 {
276 long o = UNSAFE.getAndSetLong(base, offset, 1L);
277 assertEquals(o, 2L, "getAndSet long");
278 long x = UNSAFE.getLong(base, offset);
279 assertEquals(x, 1L, "getAndSet long value");
280 }
281
282 UNSAFE.putLong(base, offset, 1L);
283
284 // get and add, add and get
285 {
286 long o = UNSAFE.getAndAddLong(base, offset, 2L);
287 assertEquals(o, 1L, "getAndAdd long");
288 long x = UNSAFE.getLong(base, offset);
289 assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
290 }
291 }
292
293 static void testAccess(long address) {
294 // Plain
295 {
296 UNSAFE.putLong(address, 1L);
297 long x = UNSAFE.getLong(address);
298 assertEquals(x, 1L, "set long value");
299 }
300 }
301 }
302
303
|
23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for long
28 * @modules java.base/jdk.internal.misc
29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestLong
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestLong
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestLong
32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestLong
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 JdkInternalMiscUnsafeAccessTestLong {
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);
235 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
236 long x = UNSAFE.getLong(base, offset);
237 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
238 }
239
240 {
241 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 2L, 1L);
242 assertEquals(r, 2L, "success compareAndExchangeRelease long");
243 long x = UNSAFE.getLong(base, offset);
244 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
245 }
246
247 {
248 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 2L, 3L);
249 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
250 long x = UNSAFE.getLong(base, offset);
251 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
252 }
253
254 {
255 boolean success = false;
256 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
257 success = UNSAFE.weakCompareAndSwapLong(base, offset, 1L, 2L);
258 }
259 assertEquals(success, true, "weakCompareAndSwap long");
260 long x = UNSAFE.getLong(base, offset);
261 assertEquals(x, 2L, "weakCompareAndSwap long value");
262 }
263
264 {
265 boolean success = false;
266 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
267 success = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 2L, 1L);
268 }
269 assertEquals(success, true, "weakCompareAndSwapAcquire long");
270 long x = UNSAFE.getLong(base, offset);
271 assertEquals(x, 1L, "weakCompareAndSwapAcquire long");
272 }
273
274 {
275 boolean success = false;
276 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
277 success = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 1L, 2L);
278 }
279 assertEquals(success, true, "weakCompareAndSwapRelease long");
280 long x = UNSAFE.getLong(base, offset);
281 assertEquals(x, 2L, "weakCompareAndSwapRelease long");
282 }
283
284 // Compare set and get
285 {
286 long o = UNSAFE.getAndSetLong(base, offset, 1L);
287 assertEquals(o, 2L, "getAndSet long");
288 long x = UNSAFE.getLong(base, offset);
289 assertEquals(x, 1L, "getAndSet long value");
290 }
291
292 UNSAFE.putLong(base, offset, 1L);
293
294 // get and add, add and get
295 {
296 long o = UNSAFE.getAndAddLong(base, offset, 2L);
297 assertEquals(o, 1L, "getAndAdd long");
298 long x = UNSAFE.getLong(base, offset);
299 assertEquals(x, 1L + 2L, "getAndAdd long");
300 }
301 }
302
303 static void testAccess(long address) {
304 // Plain
305 {
306 UNSAFE.putLong(address, 1L);
307 long x = UNSAFE.getLong(address);
308 assertEquals(x, 1L, "set long value");
309 }
310 }
311 }
312
|