1/* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. 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/* 25 * @test 26 * @bug 8154556 27 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsInt 28 * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAsInt 29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAsInt 30 */ 31 32import org.testng.annotations.DataProvider; 33import org.testng.annotations.Test; 34 35import java.lang.invoke.MethodHandles; 36import java.lang.invoke.VarHandle; 37import java.nio.ByteBuffer; 38import java.nio.ByteOrder; 39import java.util.ArrayList; 40import java.util.Arrays; 41import java.util.EnumSet; 42import java.util.List; 43 44import static org.testng.Assert.*; 45 46public class VarHandleTestByteArrayAsInt extends VarHandleBaseByteArrayTest { 47 static final int SIZE = Integer.BYTES; 48 49 static final int VALUE_1 = 0x01020304; 50 51 static final int VALUE_2 = 0x11121314; 52 53 static final int VALUE_3 = 0xFFFEFDFC; 54 55 56 @Override 57 public void setupVarHandleSources() { 58 // Combinations of VarHandle byte[] or ByteBuffer 59 vhss = new ArrayList<>(); 60 for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) { 61 62 ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN 63 ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; 64 VarHandleSource aeh = new VarHandleSource( 65 MethodHandles.byteArrayViewVarHandle(int[].class, bo), 66 endianess, MemoryMode.READ_WRITE); 67 vhss.add(aeh); 68 69 VarHandleSource bbh = new VarHandleSource( 70 MethodHandles.byteBufferViewVarHandle(int[].class, bo), 71 endianess, MemoryMode.READ_WRITE); 72 vhss.add(bbh); 73 } 74 } 75 76 77 @Test(dataProvider = "varHandlesProvider") 78 public void testIsAccessModeSupported(VarHandleSource vhs) { 79 VarHandle vh = vhs.s; 80 81 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET)); 82 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET)); 83 84 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE)); 85 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE)); 86 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE)); 87 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE)); 88 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE)); 89 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE)); 90 91 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); 92 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)); 93 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); 94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); 95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN)); 96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); 97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); 98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); 99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); 100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)); 101 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE)); 102 103 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 104 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 105 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE)); 106 107 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR)); 108 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)); 109 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)); 110 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND)); 111 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)); 112 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)); 113 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR)); 114 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)); 115 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE)); 116 } 117 118 @Test(dataProvider = "typesProvider") 119 public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) { 120 assertEquals(vh.varType(), int.class); 121 122 assertEquals(vh.coordinateTypes(), pts); 123 124 testTypes(vh); 125 } 126 127 128 @DataProvider 129 public Object[][] accessTestCaseProvider() throws Exception { 130 List<AccessTestCase<?>> cases = new ArrayList<>(); 131 132 for (ByteArrayViewSource<?> bav : bavss) { 133 for (VarHandleSource vh : vhss) { 134 if (vh.matches(bav)) { 135 if (bav instanceof ByteArraySource) { 136 ByteArraySource bas = (ByteArraySource) bav; 137 138 cases.add(new VarHandleSourceAccessTestCase( 139 "read write", bav, vh, h -> testArrayReadWrite(bas, h), 140 true)); 141 cases.add(new VarHandleSourceAccessTestCase( 142 "unsupported", bav, vh, h -> testArrayUnsupported(bas, h), 143 false)); 144 cases.add(new VarHandleSourceAccessTestCase( 145 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h), 146 false)); 147 cases.add(new VarHandleSourceAccessTestCase( 148 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h), 149 false)); 150 } 151 else { 152 ByteBufferSource bbs = (ByteBufferSource) bav; 153 154 if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) { 155 cases.add(new VarHandleSourceAccessTestCase( 156 "read write", bav, vh, h -> testArrayReadWrite(bbs, h), 157 true)); 158 } 159 else { 160 cases.add(new VarHandleSourceAccessTestCase( 161 "read only", bav, vh, h -> testArrayReadOnly(bbs, h), 162 true)); 163 } 164 165 cases.add(new VarHandleSourceAccessTestCase( 166 "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h), 167 false)); 168 cases.add(new VarHandleSourceAccessTestCase( 169 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h), 170 false)); 171 cases.add(new VarHandleSourceAccessTestCase( 172 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h), 173 false)); 174 } 175 } 176 } 177 } 178 179 // Work around issue with jtreg summary reporting which truncates 180 // the String result of Object.toString to 30 characters, hence 181 // the first dummy argument 182 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 183 } 184 185 @Test(dataProvider = "accessTestCaseProvider") 186 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 187 T t = atc.get(); 188 int iters = atc.requiresLoop() ? ITERS : 1; 189 for (int c = 0; c < iters; c++) { 190 atc.testAccess(t); 191 } 192 } 193 194 195 static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) { 196 VarHandle vh = vhs.s; 197 byte[] array = bs.s; 198 int ci = 1; 199 200 201 202 } 203 204 static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) { 205 VarHandle vh = vhs.s; 206 ByteBuffer array = bs.s; 207 int ci = 0; 208 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 209 210 if (readOnly) { 211 checkROBE(() -> { 212 vh.set(array, ci, VALUE_1); 213 }); 214 } 215 216 if (readOnly) { 217 checkROBE(() -> { 218 vh.setVolatile(array, ci, VALUE_1); 219 }); 220 221 checkROBE(() -> { 222 vh.setRelease(array, ci, VALUE_1); 223 }); 224 225 checkROBE(() -> { 226 vh.setOpaque(array, ci, VALUE_1); 227 }); 228 229 checkROBE(() -> { 230 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 231 }); 232 233 checkROBE(() -> { 234 int r = (int) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 235 }); 236 237 checkROBE(() -> { 238 int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 239 }); 240 241 checkROBE(() -> { 242 int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 243 }); 244 245 checkROBE(() -> { 246 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 247 }); 248 249 checkROBE(() -> { 250 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 251 }); 252 253 checkROBE(() -> { 254 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 255 }); 256 257 checkROBE(() -> { 258 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 259 }); 260 261 checkROBE(() -> { 262 int o = (int) vh.getAndSet(array, ci, VALUE_1); 263 }); 264 265 checkROBE(() -> { 266 int o = (int) vh.getAndSetAcquire(array, ci, VALUE_1); 267 }); 268 269 checkROBE(() -> { 270 int o = (int) vh.getAndSetRelease(array, ci, VALUE_1); 271 }); 272 273 274 checkROBE(() -> { 275 int o = (int) vh.getAndAdd(array, ci, VALUE_1); 276 }); 277 278 checkROBE(() -> { 279 int o = (int) vh.getAndAddAcquire(array, ci, VALUE_1); 280 }); 281 282 checkROBE(() -> { 283 int o = (int) vh.getAndAddRelease(array, ci, VALUE_1); 284 }); 285 286 checkROBE(() -> { 287 int o = (int) vh.getAndBitwiseOr(array, ci, VALUE_1); 288 }); 289 290 checkROBE(() -> { 291 int o = (int) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 292 }); 293 294 checkROBE(() -> { 295 int o = (int) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 296 }); 297 298 checkROBE(() -> { 299 int o = (int) vh.getAndBitwiseAnd(array, ci, VALUE_1); 300 }); 301 302 checkROBE(() -> { 303 int o = (int) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 304 }); 305 306 checkROBE(() -> { 307 int o = (int) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 308 }); 309 310 checkROBE(() -> { 311 int o = (int) vh.getAndBitwiseXor(array, ci, VALUE_1); 312 }); 313 314 checkROBE(() -> { 315 int o = (int) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 316 }); 317 318 checkROBE(() -> { 319 int o = (int) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 320 }); 321 } 322 else { 323 } 324 } 325 326 327 static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 328 VarHandle vh = vhs.s; 329 byte[] array = bs.s; 330 331 int length = array.length - SIZE + 1; 332 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 333 final int ci = i; 334 335 checkIOOBE(() -> { 336 int x = (int) vh.get(array, ci); 337 }); 338 339 checkIOOBE(() -> { 340 vh.set(array, ci, VALUE_1); 341 }); 342 343 checkIOOBE(() -> { 344 int x = (int) vh.getVolatile(array, ci); 345 }); 346 347 checkIOOBE(() -> { 348 int x = (int) vh.getAcquire(array, ci); 349 }); 350 351 checkIOOBE(() -> { 352 int x = (int) vh.getOpaque(array, ci); 353 }); 354 355 checkIOOBE(() -> { 356 vh.setVolatile(array, ci, VALUE_1); 357 }); 358 359 checkIOOBE(() -> { 360 vh.setRelease(array, ci, VALUE_1); 361 }); 362 363 checkIOOBE(() -> { 364 vh.setOpaque(array, ci, VALUE_1); 365 }); 366 367 checkIOOBE(() -> { 368 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 369 }); 370 371 checkIOOBE(() -> { 372 int r = (int) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 373 }); 374 375 checkIOOBE(() -> { 376 int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 377 }); 378 379 checkIOOBE(() -> { 380 int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 381 }); 382 383 checkIOOBE(() -> { 384 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 385 }); 386 387 checkIOOBE(() -> { 388 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 389 }); 390 391 checkIOOBE(() -> { 392 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 393 }); 394 395 checkIOOBE(() -> { 396 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 397 }); 398 399 checkIOOBE(() -> { 400 int o = (int) vh.getAndSet(array, ci, VALUE_1); 401 }); 402 403 checkIOOBE(() -> { 404 int o = (int) vh.getAndSetAcquire(array, ci, VALUE_1); 405 }); 406 407 checkIOOBE(() -> { 408 int o = (int) vh.getAndSetRelease(array, ci, VALUE_1); 409 }); 410 411 checkIOOBE(() -> { 412 int o = (int) vh.getAndAdd(array, ci, VALUE_1); 413 }); 414 415 checkIOOBE(() -> { 416 int o = (int) vh.getAndAddAcquire(array, ci, VALUE_1); 417 }); 418 419 checkIOOBE(() -> { 420 int o = (int) vh.getAndAddRelease(array, ci, VALUE_1); 421 }); 422 423 checkIOOBE(() -> { 424 int o = (int) vh.getAndBitwiseOr(array, ci, VALUE_1); 425 }); 426 427 checkIOOBE(() -> { 428 int o = (int) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 429 }); 430 431 checkIOOBE(() -> { 432 int o = (int) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 433 }); 434 435 checkIOOBE(() -> { 436 int o = (int) vh.getAndBitwiseAnd(array, ci, VALUE_1); 437 }); 438 439 checkIOOBE(() -> { 440 int o = (int) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 441 }); 442 443 checkIOOBE(() -> { 444 int o = (int) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 445 }); 446 447 checkIOOBE(() -> { 448 int o = (int) vh.getAndBitwiseXor(array, ci, VALUE_1); 449 }); 450 451 checkIOOBE(() -> { 452 int o = (int) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 453 }); 454 455 checkIOOBE(() -> { 456 int o = (int) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 457 }); 458 459 } 460 } 461 462 static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 463 VarHandle vh = vhs.s; 464 ByteBuffer array = bs.s; 465 466 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 467 468 int length = array.limit() - SIZE + 1; 469 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 470 final int ci = i; 471 472 checkIOOBE(() -> { 473 int x = (int) vh.get(array, ci); 474 }); 475 476 if (!readOnly) { 477 checkIOOBE(() -> { 478 vh.set(array, ci, VALUE_1); 479 }); 480 } 481 482 checkIOOBE(() -> { 483 int x = (int) vh.getVolatile(array, ci); 484 }); 485 486 checkIOOBE(() -> { 487 int x = (int) vh.getAcquire(array, ci); 488 }); 489 490 checkIOOBE(() -> { 491 int x = (int) vh.getOpaque(array, ci); 492 }); 493 494 if (!readOnly) { 495 checkIOOBE(() -> { 496 vh.setVolatile(array, ci, VALUE_1); 497 }); 498 499 checkIOOBE(() -> { 500 vh.setRelease(array, ci, VALUE_1); 501 }); 502 503 checkIOOBE(() -> { 504 vh.setOpaque(array, ci, VALUE_1); 505 }); 506 507 checkIOOBE(() -> { 508 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 509 }); 510 511 checkIOOBE(() -> { 512 int r = (int) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 513 }); 514 515 checkIOOBE(() -> { 516 int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 517 }); 518 519 checkIOOBE(() -> { 520 int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 521 }); 522 523 checkIOOBE(() -> { 524 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 525 }); 526 527 checkIOOBE(() -> { 528 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 529 }); 530 531 checkIOOBE(() -> { 532 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 533 }); 534 535 checkIOOBE(() -> { 536 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 537 }); 538 539 checkIOOBE(() -> { 540 int o = (int) vh.getAndSet(array, ci, VALUE_1); 541 }); 542 543 checkIOOBE(() -> { 544 int o = (int) vh.getAndSetAcquire(array, ci, VALUE_1); 545 }); 546 547 checkIOOBE(() -> { 548 int o = (int) vh.getAndSetRelease(array, ci, VALUE_1); 549 }); 550 551 checkIOOBE(() -> { 552 int o = (int) vh.getAndAdd(array, ci, VALUE_1); 553 }); 554 555 checkIOOBE(() -> { 556 int o = (int) vh.getAndAddAcquire(array, ci, VALUE_1); 557 }); 558 559 checkIOOBE(() -> { 560 int o = (int) vh.getAndAddRelease(array, ci, VALUE_1); 561 }); 562 563 checkIOOBE(() -> { 564 int o = (int) vh.getAndBitwiseOr(array, ci, VALUE_1); 565 }); 566 567 checkIOOBE(() -> { 568 int o = (int) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 569 }); 570 571 checkIOOBE(() -> { 572 int o = (int) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 573 }); 574 575 checkIOOBE(() -> { 576 int o = (int) vh.getAndBitwiseAnd(array, ci, VALUE_1); 577 }); 578 579 checkIOOBE(() -> { 580 int o = (int) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 581 }); 582 583 checkIOOBE(() -> { 584 int o = (int) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 585 }); 586 587 checkIOOBE(() -> { 588 int o = (int) vh.getAndBitwiseXor(array, ci, VALUE_1); 589 }); 590 591 checkIOOBE(() -> { 592 int o = (int) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 593 }); 594 595 checkIOOBE(() -> { 596 int o = (int) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 597 }); 598 } 599 } 600 } 601 602 static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 603 VarHandle vh = vhs.s; 604 byte[] array = bs.s; 605 606 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 607 608 int length = array.length - SIZE + 1; 609 for (int i = 0; i < length; i++) { 610 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 611 final int ci = i; 612 613 if (!iAligned) { 614 checkISE(() -> { 615 int x = (int) vh.getVolatile(array, ci); 616 }); 617 618 checkISE(() -> { 619 int x = (int) vh.getAcquire(array, ci); 620 }); 621 622 checkISE(() -> { 623 int x = (int) vh.getOpaque(array, ci); 624 }); 625 626 checkISE(() -> { 627 vh.setVolatile(array, ci, VALUE_1); 628 }); 629 630 checkISE(() -> { 631 vh.setRelease(array, ci, VALUE_1); 632 }); 633 634 checkISE(() -> { 635 vh.setOpaque(array, ci, VALUE_1); 636 }); 637 638 checkISE(() -> { 639 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 640 }); 641 642 checkISE(() -> { 643 int r = (int) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 644 }); 645 646 checkISE(() -> { 647 int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 648 }); 649 650 checkISE(() -> { 651 int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 652 }); 653 654 checkISE(() -> { 655 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 656 }); 657 658 checkISE(() -> { 659 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 660 }); 661 662 checkISE(() -> { 663 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 664 }); 665 666 checkISE(() -> { 667 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 668 }); 669 670 checkISE(() -> { 671 int o = (int) vh.getAndSet(array, ci, VALUE_1); 672 }); 673 674 checkISE(() -> { 675 int o = (int) vh.getAndSetAcquire(array, ci, VALUE_1); 676 }); 677 678 checkISE(() -> { 679 int o = (int) vh.getAndSetRelease(array, ci, VALUE_1); 680 }); 681 682 checkISE(() -> { 683 int o = (int) vh.getAndAdd(array, ci, VALUE_1); 684 }); 685 686 checkISE(() -> { 687 int o = (int) vh.getAndAddAcquire(array, ci, VALUE_1); 688 }); 689 690 checkISE(() -> { 691 int o = (int) vh.getAndAddRelease(array, ci, VALUE_1); 692 }); 693 694 checkISE(() -> { 695 int o = (int) vh.getAndBitwiseOr(array, ci, VALUE_1); 696 }); 697 698 checkISE(() -> { 699 int o = (int) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 700 }); 701 702 checkISE(() -> { 703 int o = (int) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 704 }); 705 706 checkISE(() -> { 707 int o = (int) vh.getAndBitwiseAnd(array, ci, VALUE_1); 708 }); 709 710 checkISE(() -> { 711 int o = (int) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 712 }); 713 714 checkISE(() -> { 715 int o = (int) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 716 }); 717 718 checkISE(() -> { 719 int o = (int) vh.getAndBitwiseXor(array, ci, VALUE_1); 720 }); 721 722 checkISE(() -> { 723 int o = (int) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 724 }); 725 726 checkISE(() -> { 727 int o = (int) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 728 }); 729 } 730 } 731 } 732 733 static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 734 VarHandle vh = vhs.s; 735 ByteBuffer array = bs.s; 736 737 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 738 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 739 740 int length = array.limit() - SIZE + 1; 741 for (int i = 0; i < length; i++) { 742 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 743 final int ci = i; 744 745 if (!iAligned) { 746 checkISE(() -> { 747 int x = (int) vh.getVolatile(array, ci); 748 }); 749 750 checkISE(() -> { 751 int x = (int) vh.getAcquire(array, ci); 752 }); 753 754 checkISE(() -> { 755 int x = (int) vh.getOpaque(array, ci); 756 }); 757 758 if (!readOnly) { 759 checkISE(() -> { 760 vh.setVolatile(array, ci, VALUE_1); 761 }); 762 763 checkISE(() -> { 764 vh.setRelease(array, ci, VALUE_1); 765 }); 766 767 checkISE(() -> { 768 vh.setOpaque(array, ci, VALUE_1); 769 }); 770 771 checkISE(() -> { 772 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 773 }); 774 775 checkISE(() -> { 776 int r = (int) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 777 }); 778 779 checkISE(() -> { 780 int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 781 }); 782 783 checkISE(() -> { 784 int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 785 }); 786 787 checkISE(() -> { 788 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 789 }); 790 791 checkISE(() -> { 792 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 793 }); 794 795 checkISE(() -> { 796 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 797 }); 798 799 checkISE(() -> { 800 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 801 }); 802 803 checkISE(() -> { 804 int o = (int) vh.getAndSet(array, ci, VALUE_1); 805 }); 806 807 checkISE(() -> { 808 int o = (int) vh.getAndSetAcquire(array, ci, VALUE_1); 809 }); 810 811 checkISE(() -> { 812 int o = (int) vh.getAndSetRelease(array, ci, VALUE_1); 813 }); 814 815 checkISE(() -> { 816 int o = (int) vh.getAndAdd(array, ci, VALUE_1); 817 }); 818 819 checkISE(() -> { 820 int o = (int) vh.getAndAddAcquire(array, ci, VALUE_1); 821 }); 822 823 checkISE(() -> { 824 int o = (int) vh.getAndAddRelease(array, ci, VALUE_1); 825 }); 826 827 checkISE(() -> { 828 int o = (int) vh.getAndBitwiseOr(array, ci, VALUE_1); 829 }); 830 831 checkISE(() -> { 832 int o = (int) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 833 }); 834 835 checkISE(() -> { 836 int o = (int) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 837 }); 838 839 checkISE(() -> { 840 int o = (int) vh.getAndBitwiseAnd(array, ci, VALUE_1); 841 }); 842 843 checkISE(() -> { 844 int o = (int) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 845 }); 846 847 checkISE(() -> { 848 int o = (int) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 849 }); 850 851 checkISE(() -> { 852 int o = (int) vh.getAndBitwiseXor(array, ci, VALUE_1); 853 }); 854 855 checkISE(() -> { 856 int o = (int) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 857 }); 858 859 checkISE(() -> { 860 int o = (int) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 861 }); 862 } 863 } 864 } 865 } 866 867 static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { 868 VarHandle vh = vhs.s; 869 byte[] array = bs.s; 870 871 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 872 873 bs.fill((byte) 0xff); 874 int length = array.length - SIZE + 1; 875 for (int i = 0; i < length; i++) { 876 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 877 878 // Plain 879 { 880 vh.set(array, i, VALUE_1); 881 int x = (int) vh.get(array, i); 882 assertEquals(x, VALUE_1, "get int value"); 883 } 884 885 886 if (iAligned) { 887 // Volatile 888 { 889 vh.setVolatile(array, i, VALUE_2); 890 int x = (int) vh.getVolatile(array, i); 891 assertEquals(x, VALUE_2, "setVolatile int value"); 892 } 893 894 // Lazy 895 { 896 vh.setRelease(array, i, VALUE_1); 897 int x = (int) vh.getAcquire(array, i); 898 assertEquals(x, VALUE_1, "setRelease int value"); 899 } 900 901 // Opaque 902 { 903 vh.setOpaque(array, i, VALUE_2); 904 int x = (int) vh.getOpaque(array, i); 905 assertEquals(x, VALUE_2, "setOpaque int value"); 906 } 907 908 vh.set(array, i, VALUE_1); 909 910 // Compare 911 { 912 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 913 assertEquals(r, true, "success compareAndSet int"); 914 int x = (int) vh.get(array, i); 915 assertEquals(x, VALUE_2, "success compareAndSet int value"); 916 } 917 918 { 919 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 920 assertEquals(r, false, "failing compareAndSet int"); 921 int x = (int) vh.get(array, i); 922 assertEquals(x, VALUE_2, "failing compareAndSet int value"); 923 } 924 925 { 926 int r = (int) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 927 assertEquals(r, VALUE_2, "success compareAndExchange int"); 928 int x = (int) vh.get(array, i); 929 assertEquals(x, VALUE_1, "success compareAndExchange int value"); 930 } 931 932 { 933 int r = (int) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 934 assertEquals(r, VALUE_1, "failing compareAndExchange int"); 935 int x = (int) vh.get(array, i); 936 assertEquals(x, VALUE_1, "failing compareAndExchange int value"); 937 } 938 939 { 940 int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 941 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int"); 942 int x = (int) vh.get(array, i); 943 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value"); 944 } 945 946 { 947 int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 948 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int"); 949 int x = (int) vh.get(array, i); 950 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value"); 951 } 952 953 { 954 int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 955 assertEquals(r, VALUE_2, "success compareAndExchangeRelease int"); 956 int x = (int) vh.get(array, i); 957 assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value"); 958 } 959 960 { 961 int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 962 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int"); 963 int x = (int) vh.get(array, i); 964 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value"); 965 } 966 967 { 968 boolean success = false; 969 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 970 success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2); 971 } 972 assertEquals(success, true, "weakCompareAndSetPlain int"); 973 int x = (int) vh.get(array, i); 974 assertEquals(x, VALUE_2, "weakCompareAndSetPlain int value"); 975 } 976 977 { 978 boolean success = false; 979 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 980 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 981 } 982 assertEquals(success, true, "weakCompareAndSetAcquire int"); 983 int x = (int) vh.get(array, i); 984 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int"); 985 } 986 987 { 988 boolean success = false; 989 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 990 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 991 } 992 assertEquals(success, true, "weakCompareAndSetRelease int"); 993 int x = (int) vh.get(array, i); 994 assertEquals(x, VALUE_2, "weakCompareAndSetRelease int"); 995 } 996 997 { 998 boolean success = false; 999 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1000 success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1); 1001 } 1002 assertEquals(success, true, "weakCompareAndSet int"); 1003 int x = (int) vh.get(array, i); 1004 assertEquals(x, VALUE_1, "weakCompareAndSet int"); 1005 } 1006 1007 // Compare set and get 1008 { 1009 vh.set(array, i, VALUE_1); 1010 1011 int o = (int) vh.getAndSet(array, i, VALUE_2); 1012 assertEquals(o, VALUE_1, "getAndSet int"); 1013 int x = (int) vh.get(array, i); 1014 assertEquals(x, VALUE_2, "getAndSet int value"); 1015 } 1016 1017 { 1018 vh.set(array, i, VALUE_1); 1019 1020 int o = (int) vh.getAndSetAcquire(array, i, VALUE_2); 1021 assertEquals(o, VALUE_1, "getAndSetAcquire int"); 1022 int x = (int) vh.get(array, i); 1023 assertEquals(x, VALUE_2, "getAndSetAcquire int value"); 1024 } 1025 1026 { 1027 vh.set(array, i, VALUE_1); 1028 1029 int o = (int) vh.getAndSetRelease(array, i, VALUE_2); 1030 assertEquals(o, VALUE_1, "getAndSetRelease int"); 1031 int x = (int) vh.get(array, i); 1032 assertEquals(x, VALUE_2, "getAndSetRelease int value"); 1033 } 1034 1035 // get and add, add and get 1036 { 1037 vh.set(array, i, VALUE_1); 1038 1039 int o = (int) vh.getAndAdd(array, i, VALUE_2); 1040 assertEquals(o, VALUE_1, "getAndAdd int"); 1041 int x = (int) vh.get(array, i); 1042 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd int value"); 1043 } 1044 1045 { 1046 vh.set(array, i, VALUE_1); 1047 1048 int o = (int) vh.getAndAddAcquire(array, i, VALUE_2); 1049 assertEquals(o, VALUE_1, "getAndAddAcquire int"); 1050 int x = (int) vh.get(array, i); 1051 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire int value"); 1052 } 1053 1054 { 1055 vh.set(array, i, VALUE_1); 1056 1057 int o = (int) vh.getAndAddRelease(array, i, VALUE_2); 1058 assertEquals(o, VALUE_1, "getAndAddRelease int"); 1059 int x = (int) vh.get(array, i); 1060 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease int value"); 1061 } 1062 1063 // get and bitwise or 1064 { 1065 vh.set(array, i, VALUE_1); 1066 1067 int o = (int) vh.getAndBitwiseOr(array, i, VALUE_2); 1068 assertEquals(o, VALUE_1, "getAndBitwiseOr int"); 1069 int x = (int) vh.get(array, i); 1070 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr int value"); 1071 } 1072 1073 { 1074 vh.set(array, i, VALUE_1); 1075 1076 int o = (int) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1077 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire int"); 1078 int x = (int) vh.get(array, i); 1079 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire int value"); 1080 } 1081 1082 { 1083 vh.set(array, i, VALUE_1); 1084 1085 int o = (int) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1086 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease int"); 1087 int x = (int) vh.get(array, i); 1088 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease int value"); 1089 } 1090 1091 // get and bitwise and 1092 { 1093 vh.set(array, i, VALUE_1); 1094 1095 int o = (int) vh.getAndBitwiseAnd(array, i, VALUE_2); 1096 assertEquals(o, VALUE_1, "getAndBitwiseAnd int"); 1097 int x = (int) vh.get(array, i); 1098 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd int value"); 1099 } 1100 1101 { 1102 vh.set(array, i, VALUE_1); 1103 1104 int o = (int) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1105 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire int"); 1106 int x = (int) vh.get(array, i); 1107 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire int value"); 1108 } 1109 1110 { 1111 vh.set(array, i, VALUE_1); 1112 1113 int o = (int) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1114 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease int"); 1115 int x = (int) vh.get(array, i); 1116 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease int value"); 1117 } 1118 1119 // get and bitwise xor 1120 { 1121 vh.set(array, i, VALUE_1); 1122 1123 int o = (int) vh.getAndBitwiseXor(array, i, VALUE_2); 1124 assertEquals(o, VALUE_1, "getAndBitwiseXor int"); 1125 int x = (int) vh.get(array, i); 1126 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor int value"); 1127 } 1128 1129 { 1130 vh.set(array, i, VALUE_1); 1131 1132 int o = (int) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1133 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire int"); 1134 int x = (int) vh.get(array, i); 1135 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire int value"); 1136 } 1137 1138 { 1139 vh.set(array, i, VALUE_1); 1140 1141 int o = (int) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1142 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease int"); 1143 int x = (int) vh.get(array, i); 1144 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease int value"); 1145 } 1146 } 1147 } 1148 } 1149 1150 1151 static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { 1152 VarHandle vh = vhs.s; 1153 ByteBuffer array = bs.s; 1154 1155 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1156 1157 bs.fill((byte) 0xff); 1158 int length = array.limit() - SIZE + 1; 1159 for (int i = 0; i < length; i++) { 1160 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1161 1162 // Plain 1163 { 1164 vh.set(array, i, VALUE_1); 1165 int x = (int) vh.get(array, i); 1166 assertEquals(x, VALUE_1, "get int value"); 1167 } 1168 1169 if (iAligned) { 1170 // Volatile 1171 { 1172 vh.setVolatile(array, i, VALUE_2); 1173 int x = (int) vh.getVolatile(array, i); 1174 assertEquals(x, VALUE_2, "setVolatile int value"); 1175 } 1176 1177 // Lazy 1178 { 1179 vh.setRelease(array, i, VALUE_1); 1180 int x = (int) vh.getAcquire(array, i); 1181 assertEquals(x, VALUE_1, "setRelease int value"); 1182 } 1183 1184 // Opaque 1185 { 1186 vh.setOpaque(array, i, VALUE_2); 1187 int x = (int) vh.getOpaque(array, i); 1188 assertEquals(x, VALUE_2, "setOpaque int value"); 1189 } 1190 1191 vh.set(array, i, VALUE_1); 1192 1193 // Compare 1194 { 1195 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 1196 assertEquals(r, true, "success compareAndSet int"); 1197 int x = (int) vh.get(array, i); 1198 assertEquals(x, VALUE_2, "success compareAndSet int value"); 1199 } 1200 1201 { 1202 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 1203 assertEquals(r, false, "failing compareAndSet int"); 1204 int x = (int) vh.get(array, i); 1205 assertEquals(x, VALUE_2, "failing compareAndSet int value"); 1206 } 1207 1208 { 1209 int r = (int) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 1210 assertEquals(r, VALUE_2, "success compareAndExchange int"); 1211 int x = (int) vh.get(array, i); 1212 assertEquals(x, VALUE_1, "success compareAndExchange int value"); 1213 } 1214 1215 { 1216 int r = (int) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 1217 assertEquals(r, VALUE_1, "failing compareAndExchange int"); 1218 int x = (int) vh.get(array, i); 1219 assertEquals(x, VALUE_1, "failing compareAndExchange int value"); 1220 } 1221 1222 { 1223 int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 1224 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int"); 1225 int x = (int) vh.get(array, i); 1226 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value"); 1227 } 1228 1229 { 1230 int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 1231 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int"); 1232 int x = (int) vh.get(array, i); 1233 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value"); 1234 } 1235 1236 { 1237 int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 1238 assertEquals(r, VALUE_2, "success compareAndExchangeRelease int"); 1239 int x = (int) vh.get(array, i); 1240 assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value"); 1241 } 1242 1243 { 1244 int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 1245 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int"); 1246 int x = (int) vh.get(array, i); 1247 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value"); 1248 } 1249 1250 { 1251 boolean success = false; 1252 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1253 success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2); 1254 } 1255 assertEquals(success, true, "weakCompareAndSetPlain int"); 1256 int x = (int) vh.get(array, i); 1257 assertEquals(x, VALUE_2, "weakCompareAndSetPlain int value"); 1258 } 1259 1260 { 1261 boolean success = false; 1262 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1263 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 1264 } 1265 assertEquals(success, true, "weakCompareAndSetAcquire int"); 1266 int x = (int) vh.get(array, i); 1267 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int"); 1268 } 1269 1270 { 1271 boolean success = false; 1272 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1273 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 1274 } 1275 assertEquals(success, true, "weakCompareAndSetRelease int"); 1276 int x = (int) vh.get(array, i); 1277 assertEquals(x, VALUE_2, "weakCompareAndSetRelease int"); 1278 } 1279 1280 { 1281 boolean success = false; 1282 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1283 success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1); 1284 } 1285 assertEquals(success, true, "weakCompareAndSet int"); 1286 int x = (int) vh.get(array, i); 1287 assertEquals(x, VALUE_1, "weakCompareAndSet int"); 1288 } 1289 1290 // Compare set and get 1291 { 1292 vh.set(array, i, VALUE_1); 1293 1294 int o = (int) vh.getAndSet(array, i, VALUE_2); 1295 assertEquals(o, VALUE_1, "getAndSet int"); 1296 int x = (int) vh.get(array, i); 1297 assertEquals(x, VALUE_2, "getAndSet int value"); 1298 } 1299 1300 { 1301 vh.set(array, i, VALUE_1); 1302 1303 int o = (int) vh.getAndSetAcquire(array, i, VALUE_2); 1304 assertEquals(o, VALUE_1, "getAndSetAcquire int"); 1305 int x = (int) vh.get(array, i); 1306 assertEquals(x, VALUE_2, "getAndSetAcquire int value"); 1307 } 1308 1309 { 1310 vh.set(array, i, VALUE_1); 1311 1312 int o = (int) vh.getAndSetRelease(array, i, VALUE_2); 1313 assertEquals(o, VALUE_1, "getAndSetRelease int"); 1314 int x = (int) vh.get(array, i); 1315 assertEquals(x, VALUE_2, "getAndSetRelease int value"); 1316 } 1317 1318 // get and add, add and get 1319 { 1320 vh.set(array, i, VALUE_1); 1321 1322 int o = (int) vh.getAndAdd(array, i, VALUE_2); 1323 assertEquals(o, VALUE_1, "getAndAdd int"); 1324 int x = (int) vh.get(array, i); 1325 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd int value"); 1326 } 1327 1328 { 1329 vh.set(array, i, VALUE_1); 1330 1331 int o = (int) vh.getAndAddAcquire(array, i, VALUE_2); 1332 assertEquals(o, VALUE_1, "getAndAddAcquire int"); 1333 int x = (int) vh.get(array, i); 1334 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire int value"); 1335 } 1336 1337 { 1338 vh.set(array, i, VALUE_1); 1339 1340 int o = (int) vh.getAndAddRelease(array, i, VALUE_2); 1341 assertEquals(o, VALUE_1, "getAndAddRelease int"); 1342 int x = (int) vh.get(array, i); 1343 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease int value"); 1344 } 1345 1346 // get and bitwise or 1347 { 1348 vh.set(array, i, VALUE_1); 1349 1350 int o = (int) vh.getAndBitwiseOr(array, i, VALUE_2); 1351 assertEquals(o, VALUE_1, "getAndBitwiseOr int"); 1352 int x = (int) vh.get(array, i); 1353 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr int value"); 1354 } 1355 1356 { 1357 vh.set(array, i, VALUE_1); 1358 1359 int o = (int) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1360 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire int"); 1361 int x = (int) vh.get(array, i); 1362 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire int value"); 1363 } 1364 1365 { 1366 vh.set(array, i, VALUE_1); 1367 1368 int o = (int) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1369 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease int"); 1370 int x = (int) vh.get(array, i); 1371 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease int value"); 1372 } 1373 1374 // get and bitwise and 1375 { 1376 vh.set(array, i, VALUE_1); 1377 1378 int o = (int) vh.getAndBitwiseAnd(array, i, VALUE_2); 1379 assertEquals(o, VALUE_1, "getAndBitwiseAnd int"); 1380 int x = (int) vh.get(array, i); 1381 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd int value"); 1382 } 1383 1384 { 1385 vh.set(array, i, VALUE_1); 1386 1387 int o = (int) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1388 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire int"); 1389 int x = (int) vh.get(array, i); 1390 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire int value"); 1391 } 1392 1393 { 1394 vh.set(array, i, VALUE_1); 1395 1396 int o = (int) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1397 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease int"); 1398 int x = (int) vh.get(array, i); 1399 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease int value"); 1400 } 1401 1402 // get and bitwise xor 1403 { 1404 vh.set(array, i, VALUE_1); 1405 1406 int o = (int) vh.getAndBitwiseXor(array, i, VALUE_2); 1407 assertEquals(o, VALUE_1, "getAndBitwiseXor int"); 1408 int x = (int) vh.get(array, i); 1409 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor int value"); 1410 } 1411 1412 { 1413 vh.set(array, i, VALUE_1); 1414 1415 int o = (int) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1416 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire int"); 1417 int x = (int) vh.get(array, i); 1418 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire int value"); 1419 } 1420 1421 { 1422 vh.set(array, i, VALUE_1); 1423 1424 int o = (int) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1425 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease int"); 1426 int x = (int) vh.get(array, i); 1427 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease int value"); 1428 } 1429 } 1430 } 1431 } 1432 1433 static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { 1434 VarHandle vh = vhs.s; 1435 ByteBuffer array = bs.s; 1436 1437 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1438 1439 ByteBuffer bb = ByteBuffer.allocate(SIZE); 1440 bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); 1441 bs.fill(bb.putInt(0, VALUE_2).array()); 1442 1443 int length = array.limit() - SIZE + 1; 1444 for (int i = 0; i < length; i++) { 1445 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1446 1447 int v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) 1448 ? rotateLeft(VALUE_2, (i % SIZE) << 3) 1449 : rotateRight(VALUE_2, (i % SIZE) << 3); 1450 // Plain 1451 { 1452 int x = (int) vh.get(array, i); 1453 assertEquals(x, v, "get int value"); 1454 } 1455 1456 if (iAligned) { 1457 // Volatile 1458 { 1459 int x = (int) vh.getVolatile(array, i); 1460 assertEquals(x, v, "getVolatile int value"); 1461 } 1462 1463 // Lazy 1464 { 1465 int x = (int) vh.getAcquire(array, i); 1466 assertEquals(x, v, "getRelease int value"); 1467 } 1468 1469 // Opaque 1470 { 1471 int x = (int) vh.getOpaque(array, i); 1472 assertEquals(x, v, "getOpaque int value"); 1473 } 1474 } 1475 } 1476 } 1477 1478} 1479 1480