X-VarHandleTestByteArrayView.java.template revision 15540:9cb5558f968d
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 VarHandleTestByteArrayAs$Type$ 28 * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAs$Type$ 29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAs$Type$ 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 VarHandleTestByteArrayAs$Type$ extends VarHandleBaseByteArrayTest { 47 static final int SIZE = $BoxType$.BYTES; 48 49 static final $type$ VALUE_1 = $value1$; 50 51 static final $type$ VALUE_2 = $value2$; 52 53 static final $type$ VALUE_3 = $value3$; 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($type$[].class, bo), 66 endianess, MemoryMode.READ_WRITE); 67 vhss.add(aeh); 68 69 VarHandleSource bbh = new VarHandleSource( 70 MethodHandles.byteBufferViewVarHandle($type$[].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#if[CAS] 92 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); 93 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)); 94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); 95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); 96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); 97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); 98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); 99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); 100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); 101 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)); 102 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE)); 103#else[CAS] 104 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); 105 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)); 106 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); 107 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); 108 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); 109 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); 110 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); 111 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); 112 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); 113 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)); 114 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE)); 115#end[CAS] 116 117#if[AtomicAdd] 118 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 119 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 120 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE)); 121#else[AtomicAdd] 122 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 123 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 124 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE)); 125#end[AtomicAdd] 126 127#if[Bitwise] 128 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR)); 129 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)); 130 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)); 131 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND)); 132 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)); 133 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)); 134 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR)); 135 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)); 136 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE)); 137#else[Bitwise] 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#end[Bitwise] 148 } 149 150 @Test(dataProvider = "typesProvider") 151 public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) { 152 assertEquals(vh.varType(), $type$.class); 153 154 assertEquals(vh.coordinateTypes(), pts); 155 156 testTypes(vh); 157 } 158 159 160 @DataProvider 161 public Object[][] accessTestCaseProvider() throws Exception { 162 List<AccessTestCase<?>> cases = new ArrayList<>(); 163 164 for (ByteArrayViewSource<?> bav : bavss) { 165 for (VarHandleSource vh : vhss) { 166 if (vh.matches(bav)) { 167 if (bav instanceof ByteArraySource) { 168 ByteArraySource bas = (ByteArraySource) bav; 169 170 cases.add(new VarHandleSourceAccessTestCase( 171 "read write", bav, vh, h -> testArrayReadWrite(bas, h), 172 true)); 173 cases.add(new VarHandleSourceAccessTestCase( 174 "unsupported", bav, vh, h -> testArrayUnsupported(bas, h), 175 false)); 176 cases.add(new VarHandleSourceAccessTestCase( 177 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h), 178 false)); 179 cases.add(new VarHandleSourceAccessTestCase( 180 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h), 181 false)); 182 } 183 else { 184 ByteBufferSource bbs = (ByteBufferSource) bav; 185 186 if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) { 187 cases.add(new VarHandleSourceAccessTestCase( 188 "read write", bav, vh, h -> testArrayReadWrite(bbs, h), 189 true)); 190 } 191 else { 192 cases.add(new VarHandleSourceAccessTestCase( 193 "read only", bav, vh, h -> testArrayReadOnly(bbs, h), 194 true)); 195 } 196 197 cases.add(new VarHandleSourceAccessTestCase( 198 "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h), 199 false)); 200 cases.add(new VarHandleSourceAccessTestCase( 201 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h), 202 false)); 203 cases.add(new VarHandleSourceAccessTestCase( 204 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h), 205 false)); 206 } 207 } 208 } 209 } 210 211 // Work around issue with jtreg summary reporting which truncates 212 // the String result of Object.toString to 30 characters, hence 213 // the first dummy argument 214 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 215 } 216 217 @Test(dataProvider = "accessTestCaseProvider") 218 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 219 T t = atc.get(); 220 int iters = atc.requiresLoop() ? ITERS : 1; 221 for (int c = 0; c < iters; c++) { 222 atc.testAccess(t); 223 } 224 } 225 226 227 static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) { 228 VarHandle vh = vhs.s; 229 byte[] array = bs.s; 230 int ci = 1; 231 232#if[!CAS] 233 checkUOE(() -> { 234 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 235 }); 236 237 checkUOE(() -> { 238 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 239 }); 240 241 checkUOE(() -> { 242 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 243 }); 244 245 checkUOE(() -> { 246 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 247 }); 248 249 checkUOE(() -> { 250 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 251 }); 252 253 checkUOE(() -> { 254 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 255 }); 256 257 checkUOE(() -> { 258 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 259 }); 260 261 checkUOE(() -> { 262 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 263 }); 264 265 checkUOE(() -> { 266 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 267 }); 268 269 checkUOE(() -> { 270 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 271 }); 272 273 checkUOE(() -> { 274 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 275 }); 276#end[CAS] 277 278#if[!AtomicAdd] 279 checkUOE(() -> { 280 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 281 }); 282 283 checkUOE(() -> { 284 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 285 }); 286 287 checkUOE(() -> { 288 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 289 }); 290#end[AtomicAdd] 291 292#if[!Bitwise] 293 checkUOE(() -> { 294 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 295 }); 296 297 checkUOE(() -> { 298 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 299 }); 300 301 checkUOE(() -> { 302 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 303 }); 304 305 checkUOE(() -> { 306 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 307 }); 308 309 checkUOE(() -> { 310 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 311 }); 312 313 checkUOE(() -> { 314 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 315 }); 316 317 checkUOE(() -> { 318 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 319 }); 320 321 checkUOE(() -> { 322 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 323 }); 324 325 checkUOE(() -> { 326 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 327 }); 328#end[Bitwise] 329 } 330 331 static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) { 332 VarHandle vh = vhs.s; 333 ByteBuffer array = bs.s; 334 int ci = 0; 335 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 336 337 if (readOnly) { 338 checkROBE(() -> { 339 vh.set(array, ci, VALUE_1); 340 }); 341 } 342 343 if (readOnly) { 344 checkROBE(() -> { 345 vh.setVolatile(array, ci, VALUE_1); 346 }); 347 348 checkROBE(() -> { 349 vh.setRelease(array, ci, VALUE_1); 350 }); 351 352 checkROBE(() -> { 353 vh.setOpaque(array, ci, VALUE_1); 354 }); 355#if[CAS] 356 357 checkROBE(() -> { 358 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 359 }); 360 361 checkROBE(() -> { 362 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 363 }); 364 365 checkROBE(() -> { 366 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 367 }); 368 369 checkROBE(() -> { 370 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 371 }); 372 373 checkROBE(() -> { 374 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 375 }); 376 377 checkROBE(() -> { 378 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 379 }); 380 381 checkROBE(() -> { 382 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 383 }); 384 385 checkROBE(() -> { 386 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 387 }); 388 389 checkROBE(() -> { 390 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 391 }); 392 393 checkROBE(() -> { 394 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 395 }); 396 397 checkROBE(() -> { 398 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 399 }); 400 401#else[CAS] 402 checkUOE(() -> { 403 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 404 }); 405 406 checkUOE(() -> { 407 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 408 }); 409 410 checkUOE(() -> { 411 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 412 }); 413 414 checkUOE(() -> { 415 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 416 }); 417 418 checkUOE(() -> { 419 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 420 }); 421 422 checkUOE(() -> { 423 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 424 }); 425 426 checkUOE(() -> { 427 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 428 }); 429 430 checkUOE(() -> { 431 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 432 }); 433 434 checkUOE(() -> { 435 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 436 }); 437 438 checkUOE(() -> { 439 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 440 }); 441 442 checkUOE(() -> { 443 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 444 }); 445#end[CAS] 446 447#if[AtomicAdd] 448 checkROBE(() -> { 449 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 450 }); 451 452 checkROBE(() -> { 453 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 454 }); 455 456 checkROBE(() -> { 457 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 458 }); 459#else[AtomicAdd] 460 checkUOE(() -> { 461 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 462 }); 463 464 checkUOE(() -> { 465 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 466 }); 467 468 checkUOE(() -> { 469 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 470 }); 471#end[AtomicAdd] 472 473#if[Bitwise] 474 checkROBE(() -> { 475 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 476 }); 477 478 checkROBE(() -> { 479 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 480 }); 481 482 checkROBE(() -> { 483 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 484 }); 485 486 checkROBE(() -> { 487 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 488 }); 489 490 checkROBE(() -> { 491 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 492 }); 493 494 checkROBE(() -> { 495 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 496 }); 497 498 checkROBE(() -> { 499 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 500 }); 501 502 checkROBE(() -> { 503 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 504 }); 505 506 checkROBE(() -> { 507 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 508 }); 509#else[Bitwise] 510 checkUOE(() -> { 511 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 512 }); 513 514 checkUOE(() -> { 515 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 516 }); 517 518 checkUOE(() -> { 519 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 520 }); 521 522 checkUOE(() -> { 523 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 524 }); 525 526 checkUOE(() -> { 527 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 528 }); 529 530 checkUOE(() -> { 531 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 532 }); 533 534 checkUOE(() -> { 535 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 536 }); 537 538 checkUOE(() -> { 539 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 540 }); 541 542 checkUOE(() -> { 543 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 544 }); 545#end[Bitwise] 546 } 547 else { 548#if[!CAS] 549 checkUOE(() -> { 550 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 551 }); 552 553 checkUOE(() -> { 554 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 555 }); 556 557 checkUOE(() -> { 558 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 559 }); 560 561 checkUOE(() -> { 562 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 563 }); 564 565 checkUOE(() -> { 566 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 567 }); 568 569 checkUOE(() -> { 570 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 571 }); 572 573 checkUOE(() -> { 574 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 575 }); 576 577 checkUOE(() -> { 578 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 579 }); 580 581 checkUOE(() -> { 582 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 583 }); 584 585 checkUOE(() -> { 586 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 587 }); 588 589 checkUOE(() -> { 590 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 591 }); 592#end[CAS] 593#if[!AtomicAdd] 594 checkUOE(() -> { 595 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 596 }); 597 598 checkUOE(() -> { 599 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 600 }); 601 602 checkUOE(() -> { 603 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 604 }); 605#end[AtomicAdd] 606#if[!Bitwise] 607 checkUOE(() -> { 608 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 609 }); 610 611 checkUOE(() -> { 612 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 613 }); 614 615 checkUOE(() -> { 616 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 617 }); 618 619 checkUOE(() -> { 620 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 621 }); 622 623 checkUOE(() -> { 624 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 625 }); 626 627 checkUOE(() -> { 628 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 629 }); 630 631 checkUOE(() -> { 632 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 633 }); 634 635 checkUOE(() -> { 636 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 637 }); 638 639 checkUOE(() -> { 640 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 641 }); 642#end[Bitwise] 643 } 644 } 645 646 647 static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 648 VarHandle vh = vhs.s; 649 byte[] array = bs.s; 650 651 int length = array.length - SIZE + 1; 652 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 653 final int ci = i; 654 655 checkIOOBE(() -> { 656 $type$ x = ($type$) vh.get(array, ci); 657 }); 658 659 checkIOOBE(() -> { 660 vh.set(array, ci, VALUE_1); 661 }); 662 663 checkIOOBE(() -> { 664 $type$ x = ($type$) vh.getVolatile(array, ci); 665 }); 666 667 checkIOOBE(() -> { 668 $type$ x = ($type$) vh.getAcquire(array, ci); 669 }); 670 671 checkIOOBE(() -> { 672 $type$ x = ($type$) vh.getOpaque(array, ci); 673 }); 674 675 checkIOOBE(() -> { 676 vh.setVolatile(array, ci, VALUE_1); 677 }); 678 679 checkIOOBE(() -> { 680 vh.setRelease(array, ci, VALUE_1); 681 }); 682 683 checkIOOBE(() -> { 684 vh.setOpaque(array, ci, VALUE_1); 685 }); 686#if[CAS] 687 688 checkIOOBE(() -> { 689 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 690 }); 691 692 checkIOOBE(() -> { 693 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 694 }); 695 696 checkIOOBE(() -> { 697 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 698 }); 699 700 checkIOOBE(() -> { 701 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 702 }); 703 704 checkIOOBE(() -> { 705 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 706 }); 707 708 checkIOOBE(() -> { 709 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 710 }); 711 712 checkIOOBE(() -> { 713 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 714 }); 715 716 checkIOOBE(() -> { 717 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 718 }); 719 720 checkIOOBE(() -> { 721 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 722 }); 723 724 checkIOOBE(() -> { 725 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 726 }); 727 728 checkIOOBE(() -> { 729 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 730 }); 731#end[CAS] 732 733#if[AtomicAdd] 734 checkIOOBE(() -> { 735 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 736 }); 737 738 checkIOOBE(() -> { 739 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 740 }); 741 742 checkIOOBE(() -> { 743 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 744 }); 745#end[AtomicAdd] 746 747#if[Bitwise] 748 checkIOOBE(() -> { 749 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 750 }); 751 752 checkIOOBE(() -> { 753 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 754 }); 755 756 checkIOOBE(() -> { 757 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 758 }); 759 760 checkIOOBE(() -> { 761 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 762 }); 763 764 checkIOOBE(() -> { 765 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 766 }); 767 768 checkIOOBE(() -> { 769 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 770 }); 771 772 checkIOOBE(() -> { 773 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 774 }); 775 776 checkIOOBE(() -> { 777 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 778 }); 779 780 checkIOOBE(() -> { 781 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 782 }); 783#end[Bitwise] 784 785 } 786 } 787 788 static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 789 VarHandle vh = vhs.s; 790 ByteBuffer array = bs.s; 791 792 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 793 794 int length = array.limit() - SIZE + 1; 795 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 796 final int ci = i; 797 798 checkIOOBE(() -> { 799 $type$ x = ($type$) vh.get(array, ci); 800 }); 801 802 if (!readOnly) { 803 checkIOOBE(() -> { 804 vh.set(array, ci, VALUE_1); 805 }); 806 } 807 808 checkIOOBE(() -> { 809 $type$ x = ($type$) vh.getVolatile(array, ci); 810 }); 811 812 checkIOOBE(() -> { 813 $type$ x = ($type$) vh.getAcquire(array, ci); 814 }); 815 816 checkIOOBE(() -> { 817 $type$ x = ($type$) vh.getOpaque(array, ci); 818 }); 819 820 if (!readOnly) { 821 checkIOOBE(() -> { 822 vh.setVolatile(array, ci, VALUE_1); 823 }); 824 825 checkIOOBE(() -> { 826 vh.setRelease(array, ci, VALUE_1); 827 }); 828 829 checkIOOBE(() -> { 830 vh.setOpaque(array, ci, VALUE_1); 831 }); 832 833#if[CAS] 834 checkIOOBE(() -> { 835 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 836 }); 837 838 checkIOOBE(() -> { 839 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 840 }); 841 842 checkIOOBE(() -> { 843 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 844 }); 845 846 checkIOOBE(() -> { 847 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 848 }); 849 850 checkIOOBE(() -> { 851 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 852 }); 853 854 checkIOOBE(() -> { 855 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 856 }); 857 858 checkIOOBE(() -> { 859 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 860 }); 861 862 checkIOOBE(() -> { 863 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 864 }); 865 866 checkIOOBE(() -> { 867 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 868 }); 869 870 checkIOOBE(() -> { 871 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 872 }); 873 874 checkIOOBE(() -> { 875 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 876 }); 877#end[CAS] 878 879#if[AtomicAdd] 880 checkIOOBE(() -> { 881 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 882 }); 883 884 checkIOOBE(() -> { 885 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 886 }); 887 888 checkIOOBE(() -> { 889 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 890 }); 891#end[AtomicAdd] 892 893#if[Bitwise] 894 checkIOOBE(() -> { 895 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 896 }); 897 898 checkIOOBE(() -> { 899 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 900 }); 901 902 checkIOOBE(() -> { 903 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 904 }); 905 906 checkIOOBE(() -> { 907 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 908 }); 909 910 checkIOOBE(() -> { 911 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 912 }); 913 914 checkIOOBE(() -> { 915 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 916 }); 917 918 checkIOOBE(() -> { 919 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 920 }); 921 922 checkIOOBE(() -> { 923 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 924 }); 925 926 checkIOOBE(() -> { 927 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 928 }); 929#end[Bitwise] 930 } 931 } 932 } 933 934 static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 935 VarHandle vh = vhs.s; 936 byte[] array = bs.s; 937 938 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 939 940 int length = array.length - SIZE + 1; 941 for (int i = 0; i < length; i++) { 942 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 943 final int ci = i; 944 945 if (!iAligned) { 946 checkISE(() -> { 947 $type$ x = ($type$) vh.getVolatile(array, ci); 948 }); 949 950 checkISE(() -> { 951 $type$ x = ($type$) vh.getAcquire(array, ci); 952 }); 953 954 checkISE(() -> { 955 $type$ x = ($type$) vh.getOpaque(array, ci); 956 }); 957 958 checkISE(() -> { 959 vh.setVolatile(array, ci, VALUE_1); 960 }); 961 962 checkISE(() -> { 963 vh.setRelease(array, ci, VALUE_1); 964 }); 965 966 checkISE(() -> { 967 vh.setOpaque(array, ci, VALUE_1); 968 }); 969#if[CAS] 970 971 checkISE(() -> { 972 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 973 }); 974 975 checkISE(() -> { 976 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 977 }); 978 979 checkISE(() -> { 980 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 981 }); 982 983 checkISE(() -> { 984 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 985 }); 986 987 checkISE(() -> { 988 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 989 }); 990 991 checkISE(() -> { 992 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 993 }); 994 995 checkISE(() -> { 996 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 997 }); 998 999 checkISE(() -> { 1000 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 1001 }); 1002 1003 checkISE(() -> { 1004 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 1005 }); 1006 1007 checkISE(() -> { 1008 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 1009 }); 1010 1011 checkISE(() -> { 1012 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 1013 }); 1014#end[CAS] 1015 1016#if[AtomicAdd] 1017 checkISE(() -> { 1018 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 1019 }); 1020 1021 checkISE(() -> { 1022 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 1023 }); 1024 1025 checkISE(() -> { 1026 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 1027 }); 1028#end[AtomicAdd] 1029 1030#if[Bitwise] 1031 checkISE(() -> { 1032 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 1033 }); 1034 1035 checkISE(() -> { 1036 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 1037 }); 1038 1039 checkISE(() -> { 1040 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 1041 }); 1042 1043 checkISE(() -> { 1044 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 1045 }); 1046 1047 checkISE(() -> { 1048 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 1049 }); 1050 1051 checkISE(() -> { 1052 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 1053 }); 1054 1055 checkISE(() -> { 1056 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 1057 }); 1058 1059 checkISE(() -> { 1060 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 1061 }); 1062 1063 checkISE(() -> { 1064 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 1065 }); 1066#end[Bitwise] 1067 } 1068 } 1069 } 1070 1071 static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 1072 VarHandle vh = vhs.s; 1073 ByteBuffer array = bs.s; 1074 1075 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 1076 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1077 1078 int length = array.limit() - SIZE + 1; 1079 for (int i = 0; i < length; i++) { 1080 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1081 final int ci = i; 1082 1083 if (!iAligned) { 1084 checkISE(() -> { 1085 $type$ x = ($type$) vh.getVolatile(array, ci); 1086 }); 1087 1088 checkISE(() -> { 1089 $type$ x = ($type$) vh.getAcquire(array, ci); 1090 }); 1091 1092 checkISE(() -> { 1093 $type$ x = ($type$) vh.getOpaque(array, ci); 1094 }); 1095 1096 if (!readOnly) { 1097 checkISE(() -> { 1098 vh.setVolatile(array, ci, VALUE_1); 1099 }); 1100 1101 checkISE(() -> { 1102 vh.setRelease(array, ci, VALUE_1); 1103 }); 1104 1105 checkISE(() -> { 1106 vh.setOpaque(array, ci, VALUE_1); 1107 }); 1108 1109#if[CAS] 1110 checkISE(() -> { 1111 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 1112 }); 1113 1114 checkISE(() -> { 1115 $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 1116 }); 1117 1118 checkISE(() -> { 1119 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 1120 }); 1121 1122 checkISE(() -> { 1123 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 1124 }); 1125 1126 checkISE(() -> { 1127 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 1128 }); 1129 1130 checkISE(() -> { 1131 boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); 1132 }); 1133 1134 checkISE(() -> { 1135 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 1136 }); 1137 1138 checkISE(() -> { 1139 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 1140 }); 1141 1142 checkISE(() -> { 1143 $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1); 1144 }); 1145 1146 checkISE(() -> { 1147 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1); 1148 }); 1149 1150 checkISE(() -> { 1151 $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1); 1152 }); 1153#end[CAS] 1154 1155#if[AtomicAdd] 1156 checkISE(() -> { 1157 $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1); 1158 }); 1159 1160 checkISE(() -> { 1161 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1); 1162 }); 1163 1164 checkISE(() -> { 1165 $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1); 1166 }); 1167#end[AtomicAdd] 1168 1169#if[Bitwise] 1170 checkISE(() -> { 1171 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1); 1172 }); 1173 1174 checkISE(() -> { 1175 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 1176 }); 1177 1178 checkISE(() -> { 1179 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 1180 }); 1181 1182 checkISE(() -> { 1183 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1); 1184 }); 1185 1186 checkISE(() -> { 1187 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 1188 }); 1189 1190 checkISE(() -> { 1191 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 1192 }); 1193 1194 checkISE(() -> { 1195 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1); 1196 }); 1197 1198 checkISE(() -> { 1199 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 1200 }); 1201 1202 checkISE(() -> { 1203 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 1204 }); 1205#end[Bitwise] 1206 } 1207 } 1208 } 1209 } 1210 1211 static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { 1212 VarHandle vh = vhs.s; 1213 byte[] array = bs.s; 1214 1215 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 1216 1217 bs.fill((byte) 0xff); 1218 int length = array.length - SIZE + 1; 1219 for (int i = 0; i < length; i++) { 1220 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1221 1222 // Plain 1223 { 1224 vh.set(array, i, VALUE_1); 1225 $type$ x = ($type$) vh.get(array, i); 1226 assertEquals(x, VALUE_1, "get $type$ value"); 1227 } 1228 1229 1230 if (iAligned) { 1231 // Volatile 1232 { 1233 vh.setVolatile(array, i, VALUE_2); 1234 $type$ x = ($type$) vh.getVolatile(array, i); 1235 assertEquals(x, VALUE_2, "setVolatile $type$ value"); 1236 } 1237 1238 // Lazy 1239 { 1240 vh.setRelease(array, i, VALUE_1); 1241 $type$ x = ($type$) vh.getAcquire(array, i); 1242 assertEquals(x, VALUE_1, "setRelease $type$ value"); 1243 } 1244 1245 // Opaque 1246 { 1247 vh.setOpaque(array, i, VALUE_2); 1248 $type$ x = ($type$) vh.getOpaque(array, i); 1249 assertEquals(x, VALUE_2, "setOpaque $type$ value"); 1250 } 1251#if[CAS] 1252 1253 vh.set(array, i, VALUE_1); 1254 1255 // Compare 1256 { 1257 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 1258 assertEquals(r, true, "success compareAndSet $type$"); 1259 $type$ x = ($type$) vh.get(array, i); 1260 assertEquals(x, VALUE_2, "success compareAndSet $type$ value"); 1261 } 1262 1263 { 1264 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 1265 assertEquals(r, false, "failing compareAndSet $type$"); 1266 $type$ x = ($type$) vh.get(array, i); 1267 assertEquals(x, VALUE_2, "failing compareAndSet $type$ value"); 1268 } 1269 1270 { 1271 $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 1272 assertEquals(r, VALUE_2, "success compareAndExchange $type$"); 1273 $type$ x = ($type$) vh.get(array, i); 1274 assertEquals(x, VALUE_1, "success compareAndExchange $type$ value"); 1275 } 1276 1277 { 1278 $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 1279 assertEquals(r, VALUE_1, "failing compareAndExchange $type$"); 1280 $type$ x = ($type$) vh.get(array, i); 1281 assertEquals(x, VALUE_1, "failing compareAndExchange $type$ value"); 1282 } 1283 1284 { 1285 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 1286 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire $type$"); 1287 $type$ x = ($type$) vh.get(array, i); 1288 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire $type$ value"); 1289 } 1290 1291 { 1292 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 1293 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire $type$"); 1294 $type$ x = ($type$) vh.get(array, i); 1295 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire $type$ value"); 1296 } 1297 1298 { 1299 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 1300 assertEquals(r, VALUE_2, "success compareAndExchangeRelease $type$"); 1301 $type$ x = ($type$) vh.get(array, i); 1302 assertEquals(x, VALUE_1, "success compareAndExchangeRelease $type$ value"); 1303 } 1304 1305 { 1306 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 1307 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease $type$"); 1308 $type$ x = ($type$) vh.get(array, i); 1309 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease $type$ value"); 1310 } 1311 1312 { 1313 boolean success = false; 1314 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1315 success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); 1316 } 1317 assertEquals(success, true, "weakCompareAndSet $type$"); 1318 $type$ x = ($type$) vh.get(array, i); 1319 assertEquals(x, VALUE_2, "weakCompareAndSet $type$ value"); 1320 } 1321 1322 { 1323 boolean success = false; 1324 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1325 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 1326 } 1327 assertEquals(success, true, "weakCompareAndSetAcquire $type$"); 1328 $type$ x = ($type$) vh.get(array, i); 1329 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire $type$"); 1330 } 1331 1332 { 1333 boolean success = false; 1334 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1335 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 1336 } 1337 assertEquals(success, true, "weakCompareAndSetRelease $type$"); 1338 $type$ x = ($type$) vh.get(array, i); 1339 assertEquals(x, VALUE_2, "weakCompareAndSetRelease $type$"); 1340 } 1341 1342 { 1343 boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); 1344 assertEquals(r, true, "weakCompareAndSetVolatile $type$"); 1345 $type$ x = ($type$) vh.get(array, i); 1346 assertEquals(x, VALUE_1, "weakCompareAndSetVolatile $type$ value"); 1347 } 1348 1349 // Compare set and get 1350 { 1351 vh.set(array, i, VALUE_1); 1352 1353 $type$ o = ($type$) vh.getAndSet(array, i, VALUE_2); 1354 assertEquals(o, VALUE_1, "getAndSet $type$"); 1355 $type$ x = ($type$) vh.get(array, i); 1356 assertEquals(x, VALUE_2, "getAndSet $type$ value"); 1357 } 1358 1359 { 1360 vh.set(array, i, VALUE_1); 1361 1362 $type$ o = ($type$) vh.getAndSetAcquire(array, i, VALUE_2); 1363 assertEquals(o, VALUE_1, "getAndSetAcquire $type$"); 1364 $type$ x = ($type$) vh.get(array, i); 1365 assertEquals(x, VALUE_2, "getAndSetAcquire $type$ value"); 1366 } 1367 1368 { 1369 vh.set(array, i, VALUE_1); 1370 1371 $type$ o = ($type$) vh.getAndSetRelease(array, i, VALUE_2); 1372 assertEquals(o, VALUE_1, "getAndSetRelease $type$"); 1373 $type$ x = ($type$) vh.get(array, i); 1374 assertEquals(x, VALUE_2, "getAndSetRelease $type$ value"); 1375 } 1376#end[CAS] 1377 1378#if[AtomicAdd] 1379 // get and add, add and get 1380 { 1381 vh.set(array, i, VALUE_1); 1382 1383 $type$ o = ($type$) vh.getAndAdd(array, i, VALUE_2); 1384 assertEquals(o, VALUE_1, "getAndAdd $type$"); 1385 $type$ x = ($type$) vh.get(array, i); 1386 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd $type$ value"); 1387 } 1388 1389 { 1390 vh.set(array, i, VALUE_1); 1391 1392 $type$ o = ($type$) vh.getAndAddAcquire(array, i, VALUE_2); 1393 assertEquals(o, VALUE_1, "getAndAddAcquire $type$"); 1394 $type$ x = ($type$) vh.get(array, i); 1395 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire $type$ value"); 1396 } 1397 1398 { 1399 vh.set(array, i, VALUE_1); 1400 1401 $type$ o = ($type$) vh.getAndAddRelease(array, i, VALUE_2); 1402 assertEquals(o, VALUE_1, "getAndAddRelease $type$"); 1403 $type$ x = ($type$) vh.get(array, i); 1404 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease $type$ value"); 1405 } 1406#end[AtomicAdd] 1407 1408#if[Bitwise] 1409 // get and bitwise or 1410 { 1411 vh.set(array, i, VALUE_1); 1412 1413 $type$ o = ($type$) vh.getAndBitwiseOr(array, i, VALUE_2); 1414 assertEquals(o, VALUE_1, "getAndBitwiseOr $type$"); 1415 $type$ x = ($type$) vh.get(array, i); 1416 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr $type$ value"); 1417 } 1418 1419 { 1420 vh.set(array, i, VALUE_1); 1421 1422 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1423 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire $type$"); 1424 $type$ x = ($type$) vh.get(array, i); 1425 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire $type$ value"); 1426 } 1427 1428 { 1429 vh.set(array, i, VALUE_1); 1430 1431 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1432 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease $type$"); 1433 $type$ x = ($type$) vh.get(array, i); 1434 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease $type$ value"); 1435 } 1436 1437 // get and bitwise and 1438 { 1439 vh.set(array, i, VALUE_1); 1440 1441 $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, VALUE_2); 1442 assertEquals(o, VALUE_1, "getAndBitwiseAnd $type$"); 1443 $type$ x = ($type$) vh.get(array, i); 1444 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd $type$ value"); 1445 } 1446 1447 { 1448 vh.set(array, i, VALUE_1); 1449 1450 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1451 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire $type$"); 1452 $type$ x = ($type$) vh.get(array, i); 1453 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire $type$ value"); 1454 } 1455 1456 { 1457 vh.set(array, i, VALUE_1); 1458 1459 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1460 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease $type$"); 1461 $type$ x = ($type$) vh.get(array, i); 1462 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease $type$ value"); 1463 } 1464 1465 // get and bitwise xor 1466 { 1467 vh.set(array, i, VALUE_1); 1468 1469 $type$ o = ($type$) vh.getAndBitwiseXor(array, i, VALUE_2); 1470 assertEquals(o, VALUE_1, "getAndBitwiseXor $type$"); 1471 $type$ x = ($type$) vh.get(array, i); 1472 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor $type$ value"); 1473 } 1474 1475 { 1476 vh.set(array, i, VALUE_1); 1477 1478 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1479 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire $type$"); 1480 $type$ x = ($type$) vh.get(array, i); 1481 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire $type$ value"); 1482 } 1483 1484 { 1485 vh.set(array, i, VALUE_1); 1486 1487 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1488 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease $type$"); 1489 $type$ x = ($type$) vh.get(array, i); 1490 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease $type$ value"); 1491 } 1492#end[Bitwise] 1493 } 1494 } 1495 } 1496 1497 1498 static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { 1499 VarHandle vh = vhs.s; 1500 ByteBuffer array = bs.s; 1501 1502 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1503 1504 bs.fill((byte) 0xff); 1505 int length = array.limit() - SIZE + 1; 1506 for (int i = 0; i < length; i++) { 1507 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1508 1509 // Plain 1510 { 1511 vh.set(array, i, VALUE_1); 1512 $type$ x = ($type$) vh.get(array, i); 1513 assertEquals(x, VALUE_1, "get $type$ value"); 1514 } 1515 1516 if (iAligned) { 1517 // Volatile 1518 { 1519 vh.setVolatile(array, i, VALUE_2); 1520 $type$ x = ($type$) vh.getVolatile(array, i); 1521 assertEquals(x, VALUE_2, "setVolatile $type$ value"); 1522 } 1523 1524 // Lazy 1525 { 1526 vh.setRelease(array, i, VALUE_1); 1527 $type$ x = ($type$) vh.getAcquire(array, i); 1528 assertEquals(x, VALUE_1, "setRelease $type$ value"); 1529 } 1530 1531 // Opaque 1532 { 1533 vh.setOpaque(array, i, VALUE_2); 1534 $type$ x = ($type$) vh.getOpaque(array, i); 1535 assertEquals(x, VALUE_2, "setOpaque $type$ value"); 1536 } 1537#if[CAS] 1538 1539 vh.set(array, i, VALUE_1); 1540 1541 // Compare 1542 { 1543 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 1544 assertEquals(r, true, "success compareAndSet $type$"); 1545 $type$ x = ($type$) vh.get(array, i); 1546 assertEquals(x, VALUE_2, "success compareAndSet $type$ value"); 1547 } 1548 1549 { 1550 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 1551 assertEquals(r, false, "failing compareAndSet $type$"); 1552 $type$ x = ($type$) vh.get(array, i); 1553 assertEquals(x, VALUE_2, "failing compareAndSet $type$ value"); 1554 } 1555 1556 { 1557 $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 1558 assertEquals(r, VALUE_2, "success compareAndExchange $type$"); 1559 $type$ x = ($type$) vh.get(array, i); 1560 assertEquals(x, VALUE_1, "success compareAndExchange $type$ value"); 1561 } 1562 1563 { 1564 $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 1565 assertEquals(r, VALUE_1, "failing compareAndExchange $type$"); 1566 $type$ x = ($type$) vh.get(array, i); 1567 assertEquals(x, VALUE_1, "failing compareAndExchange $type$ value"); 1568 } 1569 1570 { 1571 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 1572 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire $type$"); 1573 $type$ x = ($type$) vh.get(array, i); 1574 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire $type$ value"); 1575 } 1576 1577 { 1578 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 1579 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire $type$"); 1580 $type$ x = ($type$) vh.get(array, i); 1581 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire $type$ value"); 1582 } 1583 1584 { 1585 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 1586 assertEquals(r, VALUE_2, "success compareAndExchangeRelease $type$"); 1587 $type$ x = ($type$) vh.get(array, i); 1588 assertEquals(x, VALUE_1, "success compareAndExchangeRelease $type$ value"); 1589 } 1590 1591 { 1592 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 1593 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease $type$"); 1594 $type$ x = ($type$) vh.get(array, i); 1595 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease $type$ value"); 1596 } 1597 1598 { 1599 boolean success = false; 1600 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1601 success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); 1602 } 1603 assertEquals(success, true, "weakCompareAndSet $type$"); 1604 $type$ x = ($type$) vh.get(array, i); 1605 assertEquals(x, VALUE_2, "weakCompareAndSet $type$ value"); 1606 } 1607 1608 { 1609 boolean success = false; 1610 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1611 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 1612 } 1613 assertEquals(success, true, "weakCompareAndSetAcquire $type$"); 1614 $type$ x = ($type$) vh.get(array, i); 1615 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire $type$"); 1616 } 1617 1618 { 1619 boolean success = false; 1620 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1621 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 1622 } 1623 assertEquals(success, true, "weakCompareAndSetRelease $type$"); 1624 $type$ x = ($type$) vh.get(array, i); 1625 assertEquals(x, VALUE_2, "weakCompareAndSetRelease $type$"); 1626 } 1627 1628 { 1629 boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); 1630 assertEquals(r, true, "weakCompareAndSetVolatile $type$"); 1631 $type$ x = ($type$) vh.get(array, i); 1632 assertEquals(x, VALUE_1, "weakCompareAndSetVolatile $type$ value"); 1633 } 1634 1635 // Compare set and get 1636 { 1637 vh.set(array, i, VALUE_1); 1638 1639 $type$ o = ($type$) vh.getAndSet(array, i, VALUE_2); 1640 assertEquals(o, VALUE_1, "getAndSet $type$"); 1641 $type$ x = ($type$) vh.get(array, i); 1642 assertEquals(x, VALUE_2, "getAndSet $type$ value"); 1643 } 1644 1645 { 1646 vh.set(array, i, VALUE_1); 1647 1648 $type$ o = ($type$) vh.getAndSetAcquire(array, i, VALUE_2); 1649 assertEquals(o, VALUE_1, "getAndSetAcquire $type$"); 1650 $type$ x = ($type$) vh.get(array, i); 1651 assertEquals(x, VALUE_2, "getAndSetAcquire $type$ value"); 1652 } 1653 1654 { 1655 vh.set(array, i, VALUE_1); 1656 1657 $type$ o = ($type$) vh.getAndSetRelease(array, i, VALUE_2); 1658 assertEquals(o, VALUE_1, "getAndSetRelease $type$"); 1659 $type$ x = ($type$) vh.get(array, i); 1660 assertEquals(x, VALUE_2, "getAndSetRelease $type$ value"); 1661 } 1662#end[CAS] 1663 1664#if[AtomicAdd] 1665 // get and add, add and get 1666 { 1667 vh.set(array, i, VALUE_1); 1668 1669 $type$ o = ($type$) vh.getAndAdd(array, i, VALUE_2); 1670 assertEquals(o, VALUE_1, "getAndAdd $type$"); 1671 $type$ x = ($type$) vh.get(array, i); 1672 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd $type$ value"); 1673 } 1674 1675 { 1676 vh.set(array, i, VALUE_1); 1677 1678 $type$ o = ($type$) vh.getAndAddAcquire(array, i, VALUE_2); 1679 assertEquals(o, VALUE_1, "getAndAddAcquire $type$"); 1680 $type$ x = ($type$) vh.get(array, i); 1681 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire $type$ value"); 1682 } 1683 1684 { 1685 vh.set(array, i, VALUE_1); 1686 1687 $type$ o = ($type$) vh.getAndAddRelease(array, i, VALUE_2); 1688 assertEquals(o, VALUE_1, "getAndAddRelease $type$"); 1689 $type$ x = ($type$) vh.get(array, i); 1690 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease $type$ value"); 1691 } 1692#end[AtomicAdd] 1693 1694#if[Bitwise] 1695 // get and bitwise or 1696 { 1697 vh.set(array, i, VALUE_1); 1698 1699 $type$ o = ($type$) vh.getAndBitwiseOr(array, i, VALUE_2); 1700 assertEquals(o, VALUE_1, "getAndBitwiseOr $type$"); 1701 $type$ x = ($type$) vh.get(array, i); 1702 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr $type$ value"); 1703 } 1704 1705 { 1706 vh.set(array, i, VALUE_1); 1707 1708 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1709 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire $type$"); 1710 $type$ x = ($type$) vh.get(array, i); 1711 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire $type$ value"); 1712 } 1713 1714 { 1715 vh.set(array, i, VALUE_1); 1716 1717 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1718 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease $type$"); 1719 $type$ x = ($type$) vh.get(array, i); 1720 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease $type$ value"); 1721 } 1722 1723 // get and bitwise and 1724 { 1725 vh.set(array, i, VALUE_1); 1726 1727 $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, VALUE_2); 1728 assertEquals(o, VALUE_1, "getAndBitwiseAnd $type$"); 1729 $type$ x = ($type$) vh.get(array, i); 1730 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd $type$ value"); 1731 } 1732 1733 { 1734 vh.set(array, i, VALUE_1); 1735 1736 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1737 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire $type$"); 1738 $type$ x = ($type$) vh.get(array, i); 1739 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire $type$ value"); 1740 } 1741 1742 { 1743 vh.set(array, i, VALUE_1); 1744 1745 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1746 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease $type$"); 1747 $type$ x = ($type$) vh.get(array, i); 1748 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease $type$ value"); 1749 } 1750 1751 // get and bitwise xor 1752 { 1753 vh.set(array, i, VALUE_1); 1754 1755 $type$ o = ($type$) vh.getAndBitwiseXor(array, i, VALUE_2); 1756 assertEquals(o, VALUE_1, "getAndBitwiseXor $type$"); 1757 $type$ x = ($type$) vh.get(array, i); 1758 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor $type$ value"); 1759 } 1760 1761 { 1762 vh.set(array, i, VALUE_1); 1763 1764 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1765 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire $type$"); 1766 $type$ x = ($type$) vh.get(array, i); 1767 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire $type$ value"); 1768 } 1769 1770 { 1771 vh.set(array, i, VALUE_1); 1772 1773 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1774 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease $type$"); 1775 $type$ x = ($type$) vh.get(array, i); 1776 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease $type$ value"); 1777 } 1778#end[Bitwise] 1779 } 1780 } 1781 } 1782 1783 static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { 1784 VarHandle vh = vhs.s; 1785 ByteBuffer array = bs.s; 1786 1787 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1788 1789 ByteBuffer bb = ByteBuffer.allocate(SIZE); 1790 bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); 1791 bs.fill(bb.put$Type$(0, VALUE_2).array()); 1792 1793 int length = array.limit() - SIZE + 1; 1794 for (int i = 0; i < length; i++) { 1795 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1796 1797 $type$ v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) 1798 ? rotateLeft(VALUE_2, (i % SIZE) << 3) 1799 : rotateRight(VALUE_2, (i % SIZE) << 3); 1800 // Plain 1801 { 1802 $type$ x = ($type$) vh.get(array, i); 1803 assertEquals(x, v, "get $type$ value"); 1804 } 1805 1806 if (iAligned) { 1807 // Volatile 1808 { 1809 $type$ x = ($type$) vh.getVolatile(array, i); 1810 assertEquals(x, v, "getVolatile $type$ value"); 1811 } 1812 1813 // Lazy 1814 { 1815 $type$ x = ($type$) vh.getAcquire(array, i); 1816 assertEquals(x, v, "getRelease $type$ value"); 1817 } 1818 1819 // Opaque 1820 { 1821 $type$ x = ($type$) vh.getOpaque(array, i); 1822 assertEquals(x, v, "getOpaque $type$ value"); 1823 } 1824 } 1825 } 1826 } 1827 1828} 1829 1830