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