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