218 |
218 |
219 static void testInstanceFinalField(VarHandleTestAccessInt recv, VarHandle vh) { |
219 static void testInstanceFinalField(VarHandleTestAccessInt recv, VarHandle vh) { |
220 // Plain |
220 // Plain |
221 { |
221 { |
222 int x = (int) vh.get(recv); |
222 int x = (int) vh.get(recv); |
223 assertEquals(x, 1, "get int value"); |
223 assertEquals(x, 0x01234567, "get int value"); |
224 } |
224 } |
225 |
225 |
226 |
226 |
227 // Volatile |
227 // Volatile |
228 { |
228 { |
229 int x = (int) vh.getVolatile(recv); |
229 int x = (int) vh.getVolatile(recv); |
230 assertEquals(x, 1, "getVolatile int value"); |
230 assertEquals(x, 0x01234567, "getVolatile int value"); |
231 } |
231 } |
232 |
232 |
233 // Lazy |
233 // Lazy |
234 { |
234 { |
235 int x = (int) vh.getAcquire(recv); |
235 int x = (int) vh.getAcquire(recv); |
236 assertEquals(x, 1, "getRelease int value"); |
236 assertEquals(x, 0x01234567, "getRelease int value"); |
237 } |
237 } |
238 |
238 |
239 // Opaque |
239 // Opaque |
240 { |
240 { |
241 int x = (int) vh.getOpaque(recv); |
241 int x = (int) vh.getOpaque(recv); |
242 assertEquals(x, 1, "getOpaque int value"); |
242 assertEquals(x, 0x01234567, "getOpaque int value"); |
243 } |
243 } |
244 } |
244 } |
245 |
245 |
246 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
246 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
247 checkUOE(() -> { |
247 checkUOE(() -> { |
248 vh.set(recv, 2); |
248 vh.set(recv, 0x89ABCDEF); |
249 }); |
249 }); |
250 |
250 |
251 checkUOE(() -> { |
251 checkUOE(() -> { |
252 vh.setVolatile(recv, 2); |
252 vh.setVolatile(recv, 0x89ABCDEF); |
253 }); |
253 }); |
254 |
254 |
255 checkUOE(() -> { |
255 checkUOE(() -> { |
256 vh.setRelease(recv, 2); |
256 vh.setRelease(recv, 0x89ABCDEF); |
257 }); |
257 }); |
258 |
258 |
259 checkUOE(() -> { |
259 checkUOE(() -> { |
260 vh.setOpaque(recv, 2); |
260 vh.setOpaque(recv, 0x89ABCDEF); |
261 }); |
261 }); |
262 |
262 |
263 |
263 |
264 } |
264 } |
265 |
265 |
266 |
266 |
267 static void testStaticFinalField(VarHandle vh) { |
267 static void testStaticFinalField(VarHandle vh) { |
268 // Plain |
268 // Plain |
269 { |
269 { |
270 int x = (int) vh.get(); |
270 int x = (int) vh.get(); |
271 assertEquals(x, 1, "get int value"); |
271 assertEquals(x, 0x01234567, "get int value"); |
272 } |
272 } |
273 |
273 |
274 |
274 |
275 // Volatile |
275 // Volatile |
276 { |
276 { |
277 int x = (int) vh.getVolatile(); |
277 int x = (int) vh.getVolatile(); |
278 assertEquals(x, 1, "getVolatile int value"); |
278 assertEquals(x, 0x01234567, "getVolatile int value"); |
279 } |
279 } |
280 |
280 |
281 // Lazy |
281 // Lazy |
282 { |
282 { |
283 int x = (int) vh.getAcquire(); |
283 int x = (int) vh.getAcquire(); |
284 assertEquals(x, 1, "getRelease int value"); |
284 assertEquals(x, 0x01234567, "getRelease int value"); |
285 } |
285 } |
286 |
286 |
287 // Opaque |
287 // Opaque |
288 { |
288 { |
289 int x = (int) vh.getOpaque(); |
289 int x = (int) vh.getOpaque(); |
290 assertEquals(x, 1, "getOpaque int value"); |
290 assertEquals(x, 0x01234567, "getOpaque int value"); |
291 } |
291 } |
292 } |
292 } |
293 |
293 |
294 static void testStaticFinalFieldUnsupported(VarHandle vh) { |
294 static void testStaticFinalFieldUnsupported(VarHandle vh) { |
295 checkUOE(() -> { |
295 checkUOE(() -> { |
296 vh.set(2); |
296 vh.set(0x89ABCDEF); |
297 }); |
297 }); |
298 |
298 |
299 checkUOE(() -> { |
299 checkUOE(() -> { |
300 vh.setVolatile(2); |
300 vh.setVolatile(0x89ABCDEF); |
301 }); |
301 }); |
302 |
302 |
303 checkUOE(() -> { |
303 checkUOE(() -> { |
304 vh.setRelease(2); |
304 vh.setRelease(0x89ABCDEF); |
305 }); |
305 }); |
306 |
306 |
307 checkUOE(() -> { |
307 checkUOE(() -> { |
308 vh.setOpaque(2); |
308 vh.setOpaque(0x89ABCDEF); |
309 }); |
309 }); |
310 |
310 |
311 |
311 |
312 } |
312 } |
313 |
313 |
314 |
314 |
315 static void testInstanceField(VarHandleTestAccessInt recv, VarHandle vh) { |
315 static void testInstanceField(VarHandleTestAccessInt recv, VarHandle vh) { |
316 // Plain |
316 // Plain |
317 { |
317 { |
318 vh.set(recv, 1); |
318 vh.set(recv, 0x01234567); |
319 int x = (int) vh.get(recv); |
319 int x = (int) vh.get(recv); |
320 assertEquals(x, 1, "set int value"); |
320 assertEquals(x, 0x01234567, "set int value"); |
321 } |
321 } |
322 |
322 |
323 |
323 |
324 // Volatile |
324 // Volatile |
325 { |
325 { |
326 vh.setVolatile(recv, 2); |
326 vh.setVolatile(recv, 0x89ABCDEF); |
327 int x = (int) vh.getVolatile(recv); |
327 int x = (int) vh.getVolatile(recv); |
328 assertEquals(x, 2, "setVolatile int value"); |
328 assertEquals(x, 0x89ABCDEF, "setVolatile int value"); |
329 } |
329 } |
330 |
330 |
331 // Lazy |
331 // Lazy |
332 { |
332 { |
333 vh.setRelease(recv, 1); |
333 vh.setRelease(recv, 0x01234567); |
334 int x = (int) vh.getAcquire(recv); |
334 int x = (int) vh.getAcquire(recv); |
335 assertEquals(x, 1, "setRelease int value"); |
335 assertEquals(x, 0x01234567, "setRelease int value"); |
336 } |
336 } |
337 |
337 |
338 // Opaque |
338 // Opaque |
339 { |
339 { |
340 vh.setOpaque(recv, 2); |
340 vh.setOpaque(recv, 0x89ABCDEF); |
341 int x = (int) vh.getOpaque(recv); |
341 int x = (int) vh.getOpaque(recv); |
342 assertEquals(x, 2, "setOpaque int value"); |
342 assertEquals(x, 0x89ABCDEF, "setOpaque int value"); |
343 } |
343 } |
344 |
344 |
345 vh.set(recv, 1); |
345 vh.set(recv, 0x01234567); |
346 |
346 |
347 // Compare |
347 // Compare |
348 { |
348 { |
349 boolean r = vh.compareAndSet(recv, 1, 2); |
349 boolean r = vh.compareAndSet(recv, 0x01234567, 0x89ABCDEF); |
350 assertEquals(r, true, "success compareAndSet int"); |
350 assertEquals(r, true, "success compareAndSet int"); |
351 int x = (int) vh.get(recv); |
351 int x = (int) vh.get(recv); |
352 assertEquals(x, 2, "success compareAndSet int value"); |
352 assertEquals(x, 0x89ABCDEF, "success compareAndSet int value"); |
353 } |
353 } |
354 |
354 |
355 { |
355 { |
356 boolean r = vh.compareAndSet(recv, 1, 3); |
356 boolean r = vh.compareAndSet(recv, 0x01234567, 0xCAFEBABE); |
357 assertEquals(r, false, "failing compareAndSet int"); |
357 assertEquals(r, false, "failing compareAndSet int"); |
358 int x = (int) vh.get(recv); |
358 int x = (int) vh.get(recv); |
359 assertEquals(x, 2, "failing compareAndSet int value"); |
359 assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value"); |
360 } |
360 } |
361 |
361 |
362 { |
362 { |
363 int r = (int) vh.compareAndExchangeVolatile(recv, 2, 1); |
363 int r = (int) vh.compareAndExchangeVolatile(recv, 0x89ABCDEF, 0x01234567); |
364 assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
364 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeVolatile int"); |
365 int x = (int) vh.get(recv); |
365 int x = (int) vh.get(recv); |
366 assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
366 assertEquals(x, 0x01234567, "success compareAndExchangeVolatile int value"); |
367 } |
367 } |
368 |
368 |
369 { |
369 { |
370 int r = (int) vh.compareAndExchangeVolatile(recv, 2, 3); |
370 int r = (int) vh.compareAndExchangeVolatile(recv, 0x89ABCDEF, 0xCAFEBABE); |
371 assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
371 assertEquals(r, 0x01234567, "failing compareAndExchangeVolatile int"); |
372 int x = (int) vh.get(recv); |
372 int x = (int) vh.get(recv); |
373 assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
373 assertEquals(x, 0x01234567, "failing compareAndExchangeVolatile int value"); |
374 } |
374 } |
375 |
375 |
376 { |
376 { |
377 int r = (int) vh.compareAndExchangeAcquire(recv, 1, 2); |
377 int r = (int) vh.compareAndExchangeAcquire(recv, 0x01234567, 0x89ABCDEF); |
378 assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
378 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int"); |
379 int x = (int) vh.get(recv); |
379 int x = (int) vh.get(recv); |
380 assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
380 assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value"); |
381 } |
381 } |
382 |
382 |
383 { |
383 { |
384 int r = (int) vh.compareAndExchangeAcquire(recv, 1, 3); |
384 int r = (int) vh.compareAndExchangeAcquire(recv, 0x01234567, 0xCAFEBABE); |
385 assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
385 assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int"); |
386 int x = (int) vh.get(recv); |
386 int x = (int) vh.get(recv); |
387 assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
387 assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value"); |
388 } |
388 } |
389 |
389 |
390 { |
390 { |
391 int r = (int) vh.compareAndExchangeRelease(recv, 2, 1); |
391 int r = (int) vh.compareAndExchangeRelease(recv, 0x89ABCDEF, 0x01234567); |
392 assertEquals(r, 2, "success compareAndExchangeRelease int"); |
392 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int"); |
393 int x = (int) vh.get(recv); |
393 int x = (int) vh.get(recv); |
394 assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
394 assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value"); |
395 } |
395 } |
396 |
396 |
397 { |
397 { |
398 int r = (int) vh.compareAndExchangeRelease(recv, 2, 3); |
398 int r = (int) vh.compareAndExchangeRelease(recv, 0x89ABCDEF, 0xCAFEBABE); |
399 assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
399 assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int"); |
400 int x = (int) vh.get(recv); |
400 int x = (int) vh.get(recv); |
401 assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
401 assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value"); |
402 } |
402 } |
403 |
403 |
404 { |
404 { |
405 boolean success = false; |
405 boolean success = false; |
406 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
406 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
407 success = vh.weakCompareAndSet(recv, 1, 2); |
407 success = vh.weakCompareAndSet(recv, 0x01234567, 0x89ABCDEF); |
408 } |
408 } |
409 assertEquals(success, true, "weakCompareAndSet int"); |
409 assertEquals(success, true, "weakCompareAndSet int"); |
410 int x = (int) vh.get(recv); |
410 int x = (int) vh.get(recv); |
411 assertEquals(x, 2, "weakCompareAndSet int value"); |
411 assertEquals(x, 0x89ABCDEF, "weakCompareAndSet int value"); |
412 } |
412 } |
413 |
413 |
414 { |
414 { |
415 boolean success = false; |
415 boolean success = false; |
416 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
416 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
417 success = vh.weakCompareAndSetAcquire(recv, 2, 1); |
417 success = vh.weakCompareAndSetAcquire(recv, 0x89ABCDEF, 0x01234567); |
418 } |
418 } |
419 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
419 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
420 int x = (int) vh.get(recv); |
420 int x = (int) vh.get(recv); |
421 assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
421 assertEquals(x, 0x01234567, "weakCompareAndSetAcquire int"); |
422 } |
422 } |
423 |
423 |
424 { |
424 { |
425 boolean success = false; |
425 boolean success = false; |
426 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
426 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
427 success = vh.weakCompareAndSetRelease(recv, 1, 2); |
427 success = vh.weakCompareAndSetRelease(recv, 0x01234567, 0x89ABCDEF); |
428 } |
428 } |
429 assertEquals(success, true, "weakCompareAndSetRelease int"); |
429 assertEquals(success, true, "weakCompareAndSetRelease int"); |
430 int x = (int) vh.get(recv); |
430 int x = (int) vh.get(recv); |
431 assertEquals(x, 2, "weakCompareAndSetRelease int"); |
431 assertEquals(x, 0x89ABCDEF, "weakCompareAndSetRelease int"); |
432 } |
432 } |
433 |
433 |
434 { |
434 { |
435 boolean success = false; |
435 boolean success = false; |
436 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
436 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
437 success = vh.weakCompareAndSetVolatile(recv, 2, 1); |
437 success = vh.weakCompareAndSetVolatile(recv, 0x89ABCDEF, 0x01234567); |
438 } |
438 } |
439 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
439 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
440 int x = (int) vh.get(recv); |
440 int x = (int) vh.get(recv); |
441 assertEquals(x, 1, "weakCompareAndSetVolatile int value"); |
441 assertEquals(x, 0x01234567, "weakCompareAndSetVolatile int value"); |
442 } |
442 } |
443 |
443 |
444 // Compare set and get |
444 // Compare set and get |
445 { |
445 { |
446 int o = (int) vh.getAndSet(recv, 2); |
446 int o = (int) vh.getAndSet(recv, 0x89ABCDEF); |
447 assertEquals(o, 1, "getAndSet int"); |
447 assertEquals(o, 0x01234567, "getAndSet int"); |
448 int x = (int) vh.get(recv); |
448 int x = (int) vh.get(recv); |
449 assertEquals(x, 2, "getAndSet int value"); |
449 assertEquals(x, 0x89ABCDEF, "getAndSet int value"); |
450 } |
450 } |
451 |
451 |
452 vh.set(recv, 1); |
452 vh.set(recv, 0x01234567); |
453 |
453 |
454 // get and add, add and get |
454 // get and add, add and get |
455 { |
455 { |
456 int o = (int) vh.getAndAdd(recv, 3); |
456 int o = (int) vh.getAndAdd(recv, 0xCAFEBABE); |
457 assertEquals(o, 1, "getAndAdd int"); |
457 assertEquals(o, 0x01234567, "getAndAdd int"); |
458 int c = (int) vh.addAndGet(recv, 3); |
458 int c = (int) vh.addAndGet(recv, 0xCAFEBABE); |
459 assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
459 assertEquals(c, (int)(0x01234567 + 0xCAFEBABE + 0xCAFEBABE), "getAndAdd int value"); |
460 } |
460 } |
461 } |
461 } |
462 |
462 |
463 static void testInstanceFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
463 static void testInstanceFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
464 |
464 |
466 |
466 |
467 |
467 |
468 static void testStaticField(VarHandle vh) { |
468 static void testStaticField(VarHandle vh) { |
469 // Plain |
469 // Plain |
470 { |
470 { |
471 vh.set(1); |
471 vh.set(0x01234567); |
472 int x = (int) vh.get(); |
472 int x = (int) vh.get(); |
473 assertEquals(x, 1, "set int value"); |
473 assertEquals(x, 0x01234567, "set int value"); |
474 } |
474 } |
475 |
475 |
476 |
476 |
477 // Volatile |
477 // Volatile |
478 { |
478 { |
479 vh.setVolatile(2); |
479 vh.setVolatile(0x89ABCDEF); |
480 int x = (int) vh.getVolatile(); |
480 int x = (int) vh.getVolatile(); |
481 assertEquals(x, 2, "setVolatile int value"); |
481 assertEquals(x, 0x89ABCDEF, "setVolatile int value"); |
482 } |
482 } |
483 |
483 |
484 // Lazy |
484 // Lazy |
485 { |
485 { |
486 vh.setRelease(1); |
486 vh.setRelease(0x01234567); |
487 int x = (int) vh.getAcquire(); |
487 int x = (int) vh.getAcquire(); |
488 assertEquals(x, 1, "setRelease int value"); |
488 assertEquals(x, 0x01234567, "setRelease int value"); |
489 } |
489 } |
490 |
490 |
491 // Opaque |
491 // Opaque |
492 { |
492 { |
493 vh.setOpaque(2); |
493 vh.setOpaque(0x89ABCDEF); |
494 int x = (int) vh.getOpaque(); |
494 int x = (int) vh.getOpaque(); |
495 assertEquals(x, 2, "setOpaque int value"); |
495 assertEquals(x, 0x89ABCDEF, "setOpaque int value"); |
496 } |
496 } |
497 |
497 |
498 vh.set(1); |
498 vh.set(0x01234567); |
499 |
499 |
500 // Compare |
500 // Compare |
501 { |
501 { |
502 boolean r = vh.compareAndSet(1, 2); |
502 boolean r = vh.compareAndSet(0x01234567, 0x89ABCDEF); |
503 assertEquals(r, true, "success compareAndSet int"); |
503 assertEquals(r, true, "success compareAndSet int"); |
504 int x = (int) vh.get(); |
504 int x = (int) vh.get(); |
505 assertEquals(x, 2, "success compareAndSet int value"); |
505 assertEquals(x, 0x89ABCDEF, "success compareAndSet int value"); |
506 } |
506 } |
507 |
507 |
508 { |
508 { |
509 boolean r = vh.compareAndSet(1, 3); |
509 boolean r = vh.compareAndSet(0x01234567, 0xCAFEBABE); |
510 assertEquals(r, false, "failing compareAndSet int"); |
510 assertEquals(r, false, "failing compareAndSet int"); |
511 int x = (int) vh.get(); |
511 int x = (int) vh.get(); |
512 assertEquals(x, 2, "failing compareAndSet int value"); |
512 assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value"); |
513 } |
513 } |
514 |
514 |
515 { |
515 { |
516 int r = (int) vh.compareAndExchangeVolatile(2, 1); |
516 int r = (int) vh.compareAndExchangeVolatile(0x89ABCDEF, 0x01234567); |
517 assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
517 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeVolatile int"); |
518 int x = (int) vh.get(); |
518 int x = (int) vh.get(); |
519 assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
519 assertEquals(x, 0x01234567, "success compareAndExchangeVolatile int value"); |
520 } |
520 } |
521 |
521 |
522 { |
522 { |
523 int r = (int) vh.compareAndExchangeVolatile(2, 3); |
523 int r = (int) vh.compareAndExchangeVolatile(0x89ABCDEF, 0xCAFEBABE); |
524 assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
524 assertEquals(r, 0x01234567, "failing compareAndExchangeVolatile int"); |
525 int x = (int) vh.get(); |
525 int x = (int) vh.get(); |
526 assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
526 assertEquals(x, 0x01234567, "failing compareAndExchangeVolatile int value"); |
527 } |
527 } |
528 |
528 |
529 { |
529 { |
530 int r = (int) vh.compareAndExchangeAcquire(1, 2); |
530 int r = (int) vh.compareAndExchangeAcquire(0x01234567, 0x89ABCDEF); |
531 assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
531 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int"); |
532 int x = (int) vh.get(); |
532 int x = (int) vh.get(); |
533 assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
533 assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value"); |
534 } |
534 } |
535 |
535 |
536 { |
536 { |
537 int r = (int) vh.compareAndExchangeAcquire(1, 3); |
537 int r = (int) vh.compareAndExchangeAcquire(0x01234567, 0xCAFEBABE); |
538 assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
538 assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int"); |
539 int x = (int) vh.get(); |
539 int x = (int) vh.get(); |
540 assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
540 assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value"); |
541 } |
541 } |
542 |
542 |
543 { |
543 { |
544 int r = (int) vh.compareAndExchangeRelease(2, 1); |
544 int r = (int) vh.compareAndExchangeRelease(0x89ABCDEF, 0x01234567); |
545 assertEquals(r, 2, "success compareAndExchangeRelease int"); |
545 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int"); |
546 int x = (int) vh.get(); |
546 int x = (int) vh.get(); |
547 assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
547 assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value"); |
548 } |
548 } |
549 |
549 |
550 { |
550 { |
551 int r = (int) vh.compareAndExchangeRelease(2, 3); |
551 int r = (int) vh.compareAndExchangeRelease(0x89ABCDEF, 0xCAFEBABE); |
552 assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
552 assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int"); |
553 int x = (int) vh.get(); |
553 int x = (int) vh.get(); |
554 assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
554 assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value"); |
555 } |
555 } |
556 |
556 |
557 { |
557 { |
558 boolean success = false; |
558 boolean success = false; |
559 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
559 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
560 success = vh.weakCompareAndSet(1, 2); |
560 success = vh.weakCompareAndSet(0x01234567, 0x89ABCDEF); |
561 } |
561 } |
562 assertEquals(success, true, "weakCompareAndSet int"); |
562 assertEquals(success, true, "weakCompareAndSet int"); |
563 int x = (int) vh.get(); |
563 int x = (int) vh.get(); |
564 assertEquals(x, 2, "weakCompareAndSet int value"); |
564 assertEquals(x, 0x89ABCDEF, "weakCompareAndSet int value"); |
565 } |
565 } |
566 |
566 |
567 { |
567 { |
568 boolean success = false; |
568 boolean success = false; |
569 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
569 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
570 success = vh.weakCompareAndSetAcquire(2, 1); |
570 success = vh.weakCompareAndSetAcquire(0x89ABCDEF, 0x01234567); |
571 } |
571 } |
572 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
572 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
573 int x = (int) vh.get(); |
573 int x = (int) vh.get(); |
574 assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
574 assertEquals(x, 0x01234567, "weakCompareAndSetAcquire int"); |
575 } |
575 } |
576 |
576 |
577 { |
577 { |
578 boolean success = false; |
578 boolean success = false; |
579 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
579 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
580 success = vh.weakCompareAndSetRelease(1, 2); |
580 success = vh.weakCompareAndSetRelease(0x01234567, 0x89ABCDEF); |
581 } |
581 } |
582 assertEquals(success, true, "weakCompareAndSetRelease int"); |
582 assertEquals(success, true, "weakCompareAndSetRelease int"); |
583 int x = (int) vh.get(); |
583 int x = (int) vh.get(); |
584 assertEquals(x, 2, "weakCompareAndSetRelease int"); |
584 assertEquals(x, 0x89ABCDEF, "weakCompareAndSetRelease int"); |
585 } |
585 } |
586 |
586 |
587 { |
587 { |
588 boolean success = false; |
588 boolean success = false; |
589 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
589 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
590 success = vh.weakCompareAndSetRelease(2, 1); |
590 success = vh.weakCompareAndSetRelease(0x89ABCDEF, 0x01234567); |
591 } |
591 } |
592 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
592 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
593 int x = (int) vh.get(); |
593 int x = (int) vh.get(); |
594 assertEquals(x, 1, "weakCompareAndSetVolatile int"); |
594 assertEquals(x, 0x01234567, "weakCompareAndSetVolatile int"); |
595 } |
595 } |
596 |
596 |
597 // Compare set and get |
597 // Compare set and get |
598 { |
598 { |
599 int o = (int) vh.getAndSet(2); |
599 int o = (int) vh.getAndSet(0x89ABCDEF); |
600 assertEquals(o, 1, "getAndSet int"); |
600 assertEquals(o, 0x01234567, "getAndSet int"); |
601 int x = (int) vh.get(); |
601 int x = (int) vh.get(); |
602 assertEquals(x, 2, "getAndSet int value"); |
602 assertEquals(x, 0x89ABCDEF, "getAndSet int value"); |
603 } |
603 } |
604 |
604 |
605 vh.set(1); |
605 vh.set(0x01234567); |
606 |
606 |
607 // get and add, add and get |
607 // get and add, add and get |
608 { |
608 { |
609 int o = (int) vh.getAndAdd( 3); |
609 int o = (int) vh.getAndAdd( 0xCAFEBABE); |
610 assertEquals(o, 1, "getAndAdd int"); |
610 assertEquals(o, 0x01234567, "getAndAdd int"); |
611 int c = (int) vh.addAndGet(3); |
611 int c = (int) vh.addAndGet(0xCAFEBABE); |
612 assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
612 assertEquals(c, (int)(0x01234567 + 0xCAFEBABE + 0xCAFEBABE), "getAndAdd int value"); |
613 } |
613 } |
614 } |
614 } |
615 |
615 |
616 static void testStaticFieldUnsupported(VarHandle vh) { |
616 static void testStaticFieldUnsupported(VarHandle vh) { |
617 |
617 |
622 int[] array = new int[10]; |
622 int[] array = new int[10]; |
623 |
623 |
624 for (int i = 0; i < array.length; i++) { |
624 for (int i = 0; i < array.length; i++) { |
625 // Plain |
625 // Plain |
626 { |
626 { |
627 vh.set(array, i, 1); |
627 vh.set(array, i, 0x01234567); |
628 int x = (int) vh.get(array, i); |
628 int x = (int) vh.get(array, i); |
629 assertEquals(x, 1, "get int value"); |
629 assertEquals(x, 0x01234567, "get int value"); |
630 } |
630 } |
631 |
631 |
632 |
632 |
633 // Volatile |
633 // Volatile |
634 { |
634 { |
635 vh.setVolatile(array, i, 2); |
635 vh.setVolatile(array, i, 0x89ABCDEF); |
636 int x = (int) vh.getVolatile(array, i); |
636 int x = (int) vh.getVolatile(array, i); |
637 assertEquals(x, 2, "setVolatile int value"); |
637 assertEquals(x, 0x89ABCDEF, "setVolatile int value"); |
638 } |
638 } |
639 |
639 |
640 // Lazy |
640 // Lazy |
641 { |
641 { |
642 vh.setRelease(array, i, 1); |
642 vh.setRelease(array, i, 0x01234567); |
643 int x = (int) vh.getAcquire(array, i); |
643 int x = (int) vh.getAcquire(array, i); |
644 assertEquals(x, 1, "setRelease int value"); |
644 assertEquals(x, 0x01234567, "setRelease int value"); |
645 } |
645 } |
646 |
646 |
647 // Opaque |
647 // Opaque |
648 { |
648 { |
649 vh.setOpaque(array, i, 2); |
649 vh.setOpaque(array, i, 0x89ABCDEF); |
650 int x = (int) vh.getOpaque(array, i); |
650 int x = (int) vh.getOpaque(array, i); |
651 assertEquals(x, 2, "setOpaque int value"); |
651 assertEquals(x, 0x89ABCDEF, "setOpaque int value"); |
652 } |
652 } |
653 |
653 |
654 vh.set(array, i, 1); |
654 vh.set(array, i, 0x01234567); |
655 |
655 |
656 // Compare |
656 // Compare |
657 { |
657 { |
658 boolean r = vh.compareAndSet(array, i, 1, 2); |
658 boolean r = vh.compareAndSet(array, i, 0x01234567, 0x89ABCDEF); |
659 assertEquals(r, true, "success compareAndSet int"); |
659 assertEquals(r, true, "success compareAndSet int"); |
660 int x = (int) vh.get(array, i); |
660 int x = (int) vh.get(array, i); |
661 assertEquals(x, 2, "success compareAndSet int value"); |
661 assertEquals(x, 0x89ABCDEF, "success compareAndSet int value"); |
662 } |
662 } |
663 |
663 |
664 { |
664 { |
665 boolean r = vh.compareAndSet(array, i, 1, 3); |
665 boolean r = vh.compareAndSet(array, i, 0x01234567, 0xCAFEBABE); |
666 assertEquals(r, false, "failing compareAndSet int"); |
666 assertEquals(r, false, "failing compareAndSet int"); |
667 int x = (int) vh.get(array, i); |
667 int x = (int) vh.get(array, i); |
668 assertEquals(x, 2, "failing compareAndSet int value"); |
668 assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value"); |
669 } |
669 } |
670 |
670 |
671 { |
671 { |
672 int r = (int) vh.compareAndExchangeVolatile(array, i, 2, 1); |
672 int r = (int) vh.compareAndExchangeVolatile(array, i, 0x89ABCDEF, 0x01234567); |
673 assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
673 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeVolatile int"); |
674 int x = (int) vh.get(array, i); |
674 int x = (int) vh.get(array, i); |
675 assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
675 assertEquals(x, 0x01234567, "success compareAndExchangeVolatile int value"); |
676 } |
676 } |
677 |
677 |
678 { |
678 { |
679 int r = (int) vh.compareAndExchangeVolatile(array, i, 2, 3); |
679 int r = (int) vh.compareAndExchangeVolatile(array, i, 0x89ABCDEF, 0xCAFEBABE); |
680 assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
680 assertEquals(r, 0x01234567, "failing compareAndExchangeVolatile int"); |
681 int x = (int) vh.get(array, i); |
681 int x = (int) vh.get(array, i); |
682 assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
682 assertEquals(x, 0x01234567, "failing compareAndExchangeVolatile int value"); |
683 } |
683 } |
684 |
684 |
685 { |
685 { |
686 int r = (int) vh.compareAndExchangeAcquire(array, i, 1, 2); |
686 int r = (int) vh.compareAndExchangeAcquire(array, i, 0x01234567, 0x89ABCDEF); |
687 assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
687 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int"); |
688 int x = (int) vh.get(array, i); |
688 int x = (int) vh.get(array, i); |
689 assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
689 assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value"); |
690 } |
690 } |
691 |
691 |
692 { |
692 { |
693 int r = (int) vh.compareAndExchangeAcquire(array, i, 1, 3); |
693 int r = (int) vh.compareAndExchangeAcquire(array, i, 0x01234567, 0xCAFEBABE); |
694 assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
694 assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int"); |
695 int x = (int) vh.get(array, i); |
695 int x = (int) vh.get(array, i); |
696 assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
696 assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value"); |
697 } |
697 } |
698 |
698 |
699 { |
699 { |
700 int r = (int) vh.compareAndExchangeRelease(array, i, 2, 1); |
700 int r = (int) vh.compareAndExchangeRelease(array, i, 0x89ABCDEF, 0x01234567); |
701 assertEquals(r, 2, "success compareAndExchangeRelease int"); |
701 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int"); |
702 int x = (int) vh.get(array, i); |
702 int x = (int) vh.get(array, i); |
703 assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
703 assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value"); |
704 } |
704 } |
705 |
705 |
706 { |
706 { |
707 int r = (int) vh.compareAndExchangeRelease(array, i, 2, 3); |
707 int r = (int) vh.compareAndExchangeRelease(array, i, 0x89ABCDEF, 0xCAFEBABE); |
708 assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
708 assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int"); |
709 int x = (int) vh.get(array, i); |
709 int x = (int) vh.get(array, i); |
710 assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
710 assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value"); |
711 } |
711 } |
712 |
712 |
713 { |
713 { |
714 boolean success = false; |
714 boolean success = false; |
715 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
715 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
716 success = vh.weakCompareAndSet(array, i, 1, 2); |
716 success = vh.weakCompareAndSet(array, i, 0x01234567, 0x89ABCDEF); |
717 } |
717 } |
718 assertEquals(success, true, "weakCompareAndSet int"); |
718 assertEquals(success, true, "weakCompareAndSet int"); |
719 int x = (int) vh.get(array, i); |
719 int x = (int) vh.get(array, i); |
720 assertEquals(x, 2, "weakCompareAndSet int value"); |
720 assertEquals(x, 0x89ABCDEF, "weakCompareAndSet int value"); |
721 } |
721 } |
722 |
722 |
723 { |
723 { |
724 boolean success = false; |
724 boolean success = false; |
725 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
725 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
726 success = vh.weakCompareAndSetAcquire(array, i, 2, 1); |
726 success = vh.weakCompareAndSetAcquire(array, i, 0x89ABCDEF, 0x01234567); |
727 } |
727 } |
728 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
728 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
729 int x = (int) vh.get(array, i); |
729 int x = (int) vh.get(array, i); |
730 assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
730 assertEquals(x, 0x01234567, "weakCompareAndSetAcquire int"); |
731 } |
731 } |
732 |
732 |
733 { |
733 { |
734 boolean success = false; |
734 boolean success = false; |
735 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
735 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
736 success = vh.weakCompareAndSetRelease(array, i, 1, 2); |
736 success = vh.weakCompareAndSetRelease(array, i, 0x01234567, 0x89ABCDEF); |
737 } |
737 } |
738 assertEquals(success, true, "weakCompareAndSetRelease int"); |
738 assertEquals(success, true, "weakCompareAndSetRelease int"); |
739 int x = (int) vh.get(array, i); |
739 int x = (int) vh.get(array, i); |
740 assertEquals(x, 2, "weakCompareAndSetRelease int"); |
740 assertEquals(x, 0x89ABCDEF, "weakCompareAndSetRelease int"); |
741 } |
741 } |
742 |
742 |
743 { |
743 { |
744 boolean success = false; |
744 boolean success = false; |
745 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
745 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
746 success = vh.weakCompareAndSetVolatile(array, i, 2, 1); |
746 success = vh.weakCompareAndSetVolatile(array, i, 0x89ABCDEF, 0x01234567); |
747 } |
747 } |
748 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
748 assertEquals(success, true, "weakCompareAndSetVolatile int"); |
749 int x = (int) vh.get(array, i); |
749 int x = (int) vh.get(array, i); |
750 assertEquals(x, 1, "weakCompareAndSetVolatile int"); |
750 assertEquals(x, 0x01234567, "weakCompareAndSetVolatile int"); |
751 } |
751 } |
752 |
752 |
753 // Compare set and get |
753 // Compare set and get |
754 { |
754 { |
755 int o = (int) vh.getAndSet(array, i, 2); |
755 int o = (int) vh.getAndSet(array, i, 0x89ABCDEF); |
756 assertEquals(o, 1, "getAndSet int"); |
756 assertEquals(o, 0x01234567, "getAndSet int"); |
757 int x = (int) vh.get(array, i); |
757 int x = (int) vh.get(array, i); |
758 assertEquals(x, 2, "getAndSet int value"); |
758 assertEquals(x, 0x89ABCDEF, "getAndSet int value"); |
759 } |
759 } |
760 |
760 |
761 vh.set(array, i, 1); |
761 vh.set(array, i, 0x01234567); |
762 |
762 |
763 // get and add, add and get |
763 // get and add, add and get |
764 { |
764 { |
765 int o = (int) vh.getAndAdd(array, i, 3); |
765 int o = (int) vh.getAndAdd(array, i, 0xCAFEBABE); |
766 assertEquals(o, 1, "getAndAdd int"); |
766 assertEquals(o, 0x01234567, "getAndAdd int"); |
767 int c = (int) vh.addAndGet(array, i, 3); |
767 int c = (int) vh.addAndGet(array, i, 0xCAFEBABE); |
768 assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
768 assertEquals(c, (int)(0x01234567 + 0xCAFEBABE + 0xCAFEBABE), "getAndAdd int value"); |
769 } |
769 } |
770 } |
770 } |
771 } |
771 } |
772 |
772 |
773 static void testArrayUnsupported(VarHandle vh) { |
773 static void testArrayUnsupported(VarHandle vh) { |
786 checkIOOBE(() -> { |
786 checkIOOBE(() -> { |
787 int x = (int) vh.get(array, ci); |
787 int x = (int) vh.get(array, ci); |
788 }); |
788 }); |
789 |
789 |
790 checkIOOBE(() -> { |
790 checkIOOBE(() -> { |
791 vh.set(array, ci, 1); |
791 vh.set(array, ci, 0x01234567); |
792 }); |
792 }); |
793 |
793 |
794 checkIOOBE(() -> { |
794 checkIOOBE(() -> { |
795 int x = (int) vh.getVolatile(array, ci); |
795 int x = (int) vh.getVolatile(array, ci); |
796 }); |
796 }); |
797 |
797 |
798 checkIOOBE(() -> { |
798 checkIOOBE(() -> { |
799 vh.setVolatile(array, ci, 1); |
799 vh.setVolatile(array, ci, 0x01234567); |
800 }); |
800 }); |
801 |
801 |
802 checkIOOBE(() -> { |
802 checkIOOBE(() -> { |
803 int x = (int) vh.getAcquire(array, ci); |
803 int x = (int) vh.getAcquire(array, ci); |
804 }); |
804 }); |
805 |
805 |
806 checkIOOBE(() -> { |
806 checkIOOBE(() -> { |
807 vh.setRelease(array, ci, 1); |
807 vh.setRelease(array, ci, 0x01234567); |
808 }); |
808 }); |
809 |
809 |
810 checkIOOBE(() -> { |
810 checkIOOBE(() -> { |
811 int x = (int) vh.getOpaque(array, ci); |
811 int x = (int) vh.getOpaque(array, ci); |
812 }); |
812 }); |
813 |
813 |
814 checkIOOBE(() -> { |
814 checkIOOBE(() -> { |
815 vh.setOpaque(array, ci, 1); |
815 vh.setOpaque(array, ci, 0x01234567); |
816 }); |
816 }); |
817 |
817 |
818 checkIOOBE(() -> { |
818 checkIOOBE(() -> { |
819 boolean r = vh.compareAndSet(array, ci, 1, 2); |
819 boolean r = vh.compareAndSet(array, ci, 0x01234567, 0x89ABCDEF); |
820 }); |
820 }); |
821 |
821 |
822 checkIOOBE(() -> { |
822 checkIOOBE(() -> { |
823 int r = (int) vh.compareAndExchangeVolatile(array, ci, 2, 1); |
823 int r = (int) vh.compareAndExchangeVolatile(array, ci, 0x89ABCDEF, 0x01234567); |
824 }); |
824 }); |
825 |
825 |
826 checkIOOBE(() -> { |
826 checkIOOBE(() -> { |
827 int r = (int) vh.compareAndExchangeAcquire(array, ci, 2, 1); |
827 int r = (int) vh.compareAndExchangeAcquire(array, ci, 0x89ABCDEF, 0x01234567); |
828 }); |
828 }); |
829 |
829 |
830 checkIOOBE(() -> { |
830 checkIOOBE(() -> { |
831 int r = (int) vh.compareAndExchangeRelease(array, ci, 2, 1); |
831 int r = (int) vh.compareAndExchangeRelease(array, ci, 0x89ABCDEF, 0x01234567); |
832 }); |
832 }); |
833 |
833 |
834 checkIOOBE(() -> { |
834 checkIOOBE(() -> { |
835 boolean r = vh.weakCompareAndSet(array, ci, 1, 2); |
835 boolean r = vh.weakCompareAndSet(array, ci, 0x01234567, 0x89ABCDEF); |
836 }); |
836 }); |
837 |
837 |
838 checkIOOBE(() -> { |
838 checkIOOBE(() -> { |
839 boolean r = vh.weakCompareAndSetVolatile(array, ci, 1, 2); |
839 boolean r = vh.weakCompareAndSetVolatile(array, ci, 0x01234567, 0x89ABCDEF); |
840 }); |
840 }); |
841 |
841 |
842 checkIOOBE(() -> { |
842 checkIOOBE(() -> { |
843 boolean r = vh.weakCompareAndSetAcquire(array, ci, 1, 2); |
843 boolean r = vh.weakCompareAndSetAcquire(array, ci, 0x01234567, 0x89ABCDEF); |
844 }); |
844 }); |
845 |
845 |
846 checkIOOBE(() -> { |
846 checkIOOBE(() -> { |
847 boolean r = vh.weakCompareAndSetRelease(array, ci, 1, 2); |
847 boolean r = vh.weakCompareAndSetRelease(array, ci, 0x01234567, 0x89ABCDEF); |
848 }); |
848 }); |
849 |
849 |
850 checkIOOBE(() -> { |
850 checkIOOBE(() -> { |
851 int o = (int) vh.getAndSet(array, ci, 1); |
851 int o = (int) vh.getAndSet(array, ci, 0x01234567); |
852 }); |
852 }); |
853 |
853 |
854 checkIOOBE(() -> { |
854 checkIOOBE(() -> { |
855 int o = (int) vh.getAndAdd(array, ci, 3); |
855 int o = (int) vh.getAndAdd(array, ci, 0xCAFEBABE); |
856 }); |
856 }); |
857 |
857 |
858 checkIOOBE(() -> { |
858 checkIOOBE(() -> { |
859 int o = (int) vh.addAndGet(array, ci, 3); |
859 int o = (int) vh.addAndGet(array, ci, 0xCAFEBABE); |
860 }); |
860 }); |
861 } |
861 } |
862 } |
862 } |
863 } |
863 } |
864 |
864 |