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 * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations 27 * to hit compilation thresholds 28 * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestMethodHandleAccessLong 29 */ 30 31 import org.testng.annotations.BeforeClass; 32 import org.testng.annotations.DataProvider; 33 import org.testng.annotations.Test; 34 35 import java.lang.invoke.MethodHandle; 36 import java.lang.invoke.MethodHandles; 37 import java.lang.invoke.VarHandle; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.List; 41 42 import static org.testng.Assert.*; 43 44 public class VarHandleTestMethodHandleAccessLong extends VarHandleBaseTest { 45 static final long static_final_v = 0x0123456789ABCDEFL; 46 47 static long static_v; 48 49 final long final_v = 0x0123456789ABCDEFL; 50 51 long v; 52 53 VarHandle vhFinalField; 54 55 VarHandle vhField; 56 57 VarHandle vhStaticField; 58 59 VarHandle vhStaticFinalField; 60 61 VarHandle vhArray; 62 63 @BeforeClass 64 public void setup() throws Exception { 65 vhFinalField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessLong.class, "final_v", long.class); 67 68 vhField = MethodHandles.lookup().findVarHandle( 69 VarHandleTestMethodHandleAccessLong.class, "v", long.class); 70 71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 72 VarHandleTestMethodHandleAccessLong.class, "static_final_v", long.class); 73 74 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 75 VarHandleTestMethodHandleAccessLong.class, "static_v", long.class); 76 77 vhArray = MethodHandles.arrayElementVarHandle(long[].class); 78 } 79 80 81 @DataProvider 82 public Object[][] accessTestCaseProvider() throws Exception { 83 List<AccessTestCase<?>> cases = new ArrayList<>(); 84 85 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) { 86 cases.add(new MethodHandleAccessTestCase("Instance field", 87 vhField, f, hs -> testInstanceField(this, hs))); 88 cases.add(new MethodHandleAccessTestCase("Instance field unsupported", 89 vhField, f, hs -> testInstanceFieldUnsupported(this, hs), 90 false)); 91 92 cases.add(new MethodHandleAccessTestCase("Static field", 93 vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticField)); 94 cases.add(new MethodHandleAccessTestCase("Static field unsupported", 95 vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticFieldUnsupported, 96 false)); 97 98 cases.add(new MethodHandleAccessTestCase("Array", 99 vhArray, f, VarHandleTestMethodHandleAccessLong::testArray)); 100 cases.add(new MethodHandleAccessTestCase("Array unsupported", 101 vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayUnsupported, 102 false)); 103 cases.add(new MethodHandleAccessTestCase("Array index out of bounds", 104 vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayIndexOutOfBounds, 105 false)); 106 } 107 108 // Work around issue with jtreg summary reporting which truncates 109 // the String result of Object.toString to 30 characters, hence 110 // the first dummy argument 111 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 112 } 113 114 @Test(dataProvider = "accessTestCaseProvider") 115 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 116 T t = atc.get(); 117 int iters = atc.requiresLoop() ? ITERS : 1; 118 for (int c = 0; c < iters; c++) { 119 atc.testAccess(t); 120 } 121 } 122 123 124 static void testInstanceField(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { 125 // Plain 126 { 127 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 128 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 129 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 130 } 131 132 133 // Volatile 134 { 135 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 136 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); 137 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value"); 138 } 139 140 // Lazy 141 { 142 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 0x0123456789ABCDEFL); 143 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); 144 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value"); 145 } 146 147 // Opaque 148 { 149 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 150 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); 151 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value"); 152 } 153 154 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 155 156 // Compare 157 { 158 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 159 assertEquals(r, true, "success compareAndSet long"); 160 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 161 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value"); 162 } 163 164 { 165 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 166 assertEquals(r, false, "failing compareAndSet long"); 167 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 168 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value"); 169 } 170 171 { 172 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 173 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long"); 174 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 175 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value"); 176 } 177 178 { 179 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 180 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long"); 181 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 182 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value"); 183 } 184 185 { 186 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 187 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long"); 188 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 189 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value"); 190 } 191 192 { 193 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 194 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long"); 195 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 196 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value"); 197 } 198 199 { 200 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 201 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long"); 202 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 203 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value"); 204 } 205 206 { 207 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 208 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long"); 209 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 210 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value"); 211 } 212 213 { 214 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN); 215 boolean success = false; 216 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 217 success = (boolean) mh.invokeExact(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 218 if (!success) weakDelay(); 219 } 220 assertEquals(success, true, "success weakCompareAndSetPlain long"); 221 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 222 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetPlain long value"); 223 } 224 225 { 226 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 227 assertEquals(success, false, "failing weakCompareAndSetPlain long"); 228 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 229 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetPlain long value"); 230 } 231 232 { 233 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE); 234 boolean success = false; 235 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 236 success = (boolean) mh.invokeExact(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 237 if (!success) weakDelay(); 238 } 239 assertEquals(success, true, "success weakCompareAndSetAcquire long"); 240 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 241 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSetAcquire long"); 242 } 243 244 { 245 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 246 assertEquals(success, false, "failing weakCompareAndSetAcquire long"); 247 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 248 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSetAcquire long value"); 249 } 250 251 { 252 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE); 253 boolean success = false; 254 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 255 success = (boolean) mh.invokeExact(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 256 if (!success) weakDelay(); 257 } 258 assertEquals(success, true, "success weakCompareAndSetRelease long"); 259 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 260 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetRelease long"); 261 } 262 263 { 264 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 265 assertEquals(success, false, "failing weakCompareAndSetRelease long"); 266 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 267 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetRelease long value"); 268 } 269 270 { 271 boolean success = false; 272 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET); 273 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 274 success = (boolean) mh.invokeExact(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 275 if (!success) weakDelay(); 276 } 277 assertEquals(success, true, "success weakCompareAndSet long"); 278 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 279 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSet long"); 280 } 281 282 { 283 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 284 assertEquals(success, false, "failing weakCompareAndSet long"); 285 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 286 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSet long value"); 287 } 288 289 // Compare set and get 290 { 291 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 0xCAFEBABECAFEBABEL); 292 assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long"); 293 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 294 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value"); 295 } 296 297 // get and add, add and get 298 { 299 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 300 301 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 0xCAFEBABECAFEBABEL); 302 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long"); 303 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 304 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value"); 305 } 306 307 { 308 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 309 310 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 311 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long"); 312 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 313 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value"); 314 } 315 316 { 317 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 318 319 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 320 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddRelease long"); 321 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 322 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value"); 323 } 324 325 // get and bitwise or 326 { 327 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 328 329 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, 0xCAFEBABECAFEBABEL); 330 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long"); 331 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 332 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value"); 333 } 334 335 { 336 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 337 338 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 339 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long"); 340 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 341 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value"); 342 } 343 344 { 345 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 346 347 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 348 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long"); 349 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 350 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value"); 351 } 352 353 // get and bitwise and 354 { 355 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 356 357 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, 0xCAFEBABECAFEBABEL); 358 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long"); 359 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 360 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value"); 361 } 362 363 { 364 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 365 366 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 367 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long"); 368 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 369 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value"); 370 } 371 372 { 373 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 374 375 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 376 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long"); 377 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 378 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value"); 379 } 380 381 // get and bitwise xor 382 { 383 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 384 385 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, 0xCAFEBABECAFEBABEL); 386 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long"); 387 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 388 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value"); 389 } 390 391 { 392 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 393 394 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 395 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long"); 396 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 397 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value"); 398 } 399 400 { 401 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 402 403 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 404 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long"); 405 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 406 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value"); 407 } 408 } 409 410 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { 411 412 413 } 414 415 416 static void testStaticField(Handles hs) throws Throwable { 417 // Plain 418 { 419 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 420 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 421 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 422 } 423 424 425 // Volatile 426 { 427 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0xCAFEBABECAFEBABEL); 428 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 429 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value"); 430 } 431 432 // Lazy 433 { 434 hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x0123456789ABCDEFL); 435 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); 436 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value"); 437 } 438 439 // Opaque 440 { 441 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(0xCAFEBABECAFEBABEL); 442 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); 443 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value"); 444 } 445 446 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 447 448 // Compare 449 { 450 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 451 assertEquals(r, true, "success compareAndSet long"); 452 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 453 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value"); 454 } 455 456 { 457 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 458 assertEquals(r, false, "failing compareAndSet long"); 459 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 460 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value"); 461 } 462 463 { 464 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 465 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long"); 466 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 467 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value"); 468 } 469 470 { 471 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 472 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long"); 473 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 474 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value"); 475 } 476 477 { 478 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 479 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long"); 480 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 481 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value"); 482 } 483 484 { 485 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 486 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long"); 487 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 488 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value"); 489 } 490 491 { 492 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 493 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long"); 494 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 495 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value"); 496 } 497 498 { 499 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 500 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long"); 501 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 502 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value"); 503 } 504 505 { 506 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN); 507 boolean success = false; 508 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 509 success = (boolean) mh.invokeExact(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 510 if (!success) weakDelay(); 511 } 512 assertEquals(success, true, "success weakCompareAndSetPlain long"); 513 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 514 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetPlain long value"); 515 } 516 517 { 518 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 519 assertEquals(success, false, "failing weakCompareAndSetPlain long"); 520 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 521 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetPlain long value"); 522 } 523 524 { 525 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE); 526 boolean success = false; 527 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 528 success = (boolean) mh.invokeExact(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 529 if (!success) weakDelay(); 530 } 531 assertEquals(success, true, "success weakCompareAndSetAcquire long"); 532 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 533 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSetAcquire long"); 534 } 535 536 { 537 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE); 538 boolean success = (boolean) mh.invokeExact(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 539 assertEquals(success, false, "failing weakCompareAndSetAcquire long"); 540 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 541 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSetAcquire long value"); 542 } 543 544 { 545 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE); 546 boolean success = false; 547 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 548 success = (boolean) mh.invokeExact(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 549 if (!success) weakDelay(); 550 } 551 assertEquals(success, true, "success weakCompareAndSetRelease long"); 552 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 553 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetRelease long"); 554 } 555 556 { 557 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 558 assertEquals(success, false, "failing weakCompareAndSetRelease long"); 559 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 560 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetRelease long value"); 561 } 562 563 { 564 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET); 565 boolean success = false; 566 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 567 success = (boolean) mh.invokeExact(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 568 if (!success) weakDelay(); 569 } 570 assertEquals(success, true, "success weakCompareAndSet long"); 571 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 572 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSet long"); 573 } 574 575 { 576 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 577 assertEquals(success, false, "failing weakCompareAndSet long"); 578 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 579 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSetRe long value"); 580 } 581 582 // Compare set and get 583 { 584 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 585 586 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(0xCAFEBABECAFEBABEL); 587 assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long"); 588 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 589 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value"); 590 } 591 592 // Compare set and get 593 { 594 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 595 596 long o = (long) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(0xCAFEBABECAFEBABEL); 597 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetAcquire long"); 598 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 599 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetAcquire long value"); 600 } 601 602 // Compare set and get 603 { 604 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 605 606 long o = (long) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(0xCAFEBABECAFEBABEL); 607 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetRelease long"); 608 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 609 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetRelease long value"); 610 } 611 612 // get and add, add and get 613 { 614 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 615 616 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(0xCAFEBABECAFEBABEL); 617 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long"); 618 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 619 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value"); 620 } 621 622 { 623 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 624 625 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(0xCAFEBABECAFEBABEL); 626 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long"); 627 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 628 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value"); 629 } 630 631 { 632 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 633 634 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(0xCAFEBABECAFEBABEL); 635 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddRelease long"); 636 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 637 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value"); 638 } 639 640 // get and bitwise or 641 { 642 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 643 644 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(0xCAFEBABECAFEBABEL); 645 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long"); 646 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 647 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value"); 648 } 649 650 { 651 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 652 653 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(0xCAFEBABECAFEBABEL); 654 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long"); 655 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 656 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value"); 657 } 658 659 { 660 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 661 662 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(0xCAFEBABECAFEBABEL); 663 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long"); 664 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 665 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value"); 666 } 667 668 // get and bitwise and 669 { 670 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 671 672 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(0xCAFEBABECAFEBABEL); 673 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long"); 674 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 675 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value"); 676 } 677 678 { 679 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 680 681 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(0xCAFEBABECAFEBABEL); 682 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long"); 683 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 684 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value"); 685 } 686 687 { 688 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 689 690 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(0xCAFEBABECAFEBABEL); 691 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long"); 692 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 693 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value"); 694 } 695 696 // get and bitwise xor 697 { 698 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 699 700 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(0xCAFEBABECAFEBABEL); 701 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long"); 702 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 703 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value"); 704 } 705 706 { 707 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 708 709 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(0xCAFEBABECAFEBABEL); 710 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long"); 711 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 712 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value"); 713 } 714 715 { 716 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 717 718 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(0xCAFEBABECAFEBABEL); 719 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long"); 720 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 721 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value"); 722 } 723 } 724 725 static void testStaticFieldUnsupported(Handles hs) throws Throwable { 726 727 728 } 729 730 731 static void testArray(Handles hs) throws Throwable { 732 long[] array = new long[10]; 733 734 for (int i = 0; i < array.length; i++) { 735 // Plain 736 { 737 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 738 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 739 assertEquals(x, 0x0123456789ABCDEFL, "get long value"); 740 } 741 742 743 // Volatile 744 { 745 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 746 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); 747 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value"); 748 } 749 750 // Lazy 751 { 752 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 0x0123456789ABCDEFL); 753 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); 754 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value"); 755 } 756 757 // Opaque 758 { 759 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 760 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); 761 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value"); 762 } 763 764 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 765 766 // Compare 767 { 768 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 769 assertEquals(r, true, "success compareAndSet long"); 770 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 771 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value"); 772 } 773 774 { 775 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 776 assertEquals(r, false, "failing compareAndSet long"); 777 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 778 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value"); 779 } 780 781 { 782 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 783 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long"); 784 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 785 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value"); 786 } 787 788 { 789 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 790 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long"); 791 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 792 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value"); 793 } 794 795 { 796 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 797 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long"); 798 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 799 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value"); 800 } 801 802 { 803 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 804 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long"); 805 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 806 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value"); 807 } 808 809 { 810 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 811 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long"); 812 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 813 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value"); 814 } 815 816 { 817 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 818 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long"); 819 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 820 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value"); 821 } 822 823 { 824 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN); 825 boolean success = false; 826 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 827 success = (boolean) mh.invokeExact(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 828 if (!success) weakDelay(); 829 } 830 assertEquals(success, true, "success weakCompareAndSetPlain long"); 831 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 832 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetPlain long value"); 833 } 834 835 { 836 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 837 assertEquals(success, false, "failing weakCompareAndSetPlain long"); 838 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 839 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetPlain long value"); 840 } 841 842 { 843 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE); 844 boolean success = false; 845 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 846 success = (boolean) mh.invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 847 if (!success) weakDelay(); 848 } 849 assertEquals(success, true, "success weakCompareAndSetAcquire long"); 850 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 851 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSetAcquire long"); 852 } 853 854 { 855 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 856 assertEquals(success, false, "failing weakCompareAndSetAcquire long"); 857 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 858 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSetAcquire long value"); 859 } 860 861 { 862 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE); 863 boolean success = false; 864 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 865 success = (boolean) mh.invokeExact(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 866 if (!success) weakDelay(); 867 } 868 assertEquals(success, true, "success weakCompareAndSetRelease long"); 869 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 870 assertEquals(x, 0xCAFEBABECAFEBABEL, "success weakCompareAndSetRelease long"); 871 } 872 873 { 874 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 875 assertEquals(success, false, "failing weakCompareAndSetAcquire long"); 876 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 877 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing weakCompareAndSetAcquire long value"); 878 } 879 880 { 881 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET); 882 boolean success = false; 883 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 884 success = (boolean) mh.invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 885 if (!success) weakDelay(); 886 } 887 assertEquals(success, true, "success weakCompareAndSet long"); 888 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 889 assertEquals(x, 0x0123456789ABCDEFL, "success weakCompareAndSet long"); 890 } 891 892 { 893 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 894 assertEquals(success, false, "failing weakCompareAndSet long"); 895 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 896 assertEquals(x, 0x0123456789ABCDEFL, "failing weakCompareAndSet long value"); 897 } 898 899 // Compare set and get 900 { 901 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 902 903 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 904 assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long"); 905 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 906 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value"); 907 } 908 909 { 910 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 911 912 long o = (long) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 913 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetAcquire long"); 914 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 915 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetAcquire long value"); 916 } 917 918 { 919 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 920 921 long o = (long) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 922 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetRelease long"); 923 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 924 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetRelease long value"); 925 } 926 927 // get and add, add and get 928 { 929 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 930 931 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 932 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long"); 933 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 934 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value"); 935 } 936 937 { 938 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 939 940 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 941 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long"); 942 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 943 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value"); 944 } 945 946 { 947 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 948 949 long o = (long) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 950 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddRelease long"); 951 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 952 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value"); 953 } 954 955 // get and bitwise or 956 { 957 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 958 959 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 960 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long"); 961 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 962 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value"); 963 } 964 965 { 966 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 967 968 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 969 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long"); 970 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 971 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value"); 972 } 973 974 { 975 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 976 977 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 978 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long"); 979 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 980 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value"); 981 } 982 983 // get and bitwise and 984 { 985 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 986 987 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 988 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long"); 989 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 990 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value"); 991 } 992 993 { 994 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 995 996 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 997 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long"); 998 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 999 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value"); 1000 } 1001 1002 { 1003 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 1004 1005 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 1006 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long"); 1007 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 1008 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value"); 1009 } 1010 1011 // get and bitwise xor 1012 { 1013 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 1014 1015 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 1016 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long"); 1017 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 1018 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value"); 1019 } 1020 1021 { 1022 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 1023 1024 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 1025 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long"); 1026 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 1027 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value"); 1028 } 1029 1030 { 1031 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x0123456789ABCDEFL); 1032 1033 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, 0xCAFEBABECAFEBABEL); 1034 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long"); 1035 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); 1036 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value"); 1037 } 1038 } 1039 } 1040 1041 static void testArrayUnsupported(Handles hs) throws Throwable { 1042 long[] array = new long[10]; 1043 1044 final int i = 0; 1045 1046 1047 } 1048 1049 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { 1050 long[] array = new long[10]; 1051 1052 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 1053 final int ci = i; 1054 1055 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { 1056 checkAIOOBE(am, () -> { 1057 long x = (long) hs.get(am).invokeExact(array, ci); 1058 }); 1059 } 1060 1061 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 1062 checkAIOOBE(am, () -> { 1063 hs.get(am).invokeExact(array, ci, 0x0123456789ABCDEFL); 1064 }); 1065 } 1066 1067 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 1068 checkAIOOBE(am, () -> { 1069 boolean r = (boolean) hs.get(am).invokeExact(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 1070 }); 1071 } 1072 1073 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 1074 checkAIOOBE(am, () -> { 1075 long r = (long) hs.get(am).invokeExact(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 1076 }); 1077 } 1078 1079 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 1080 checkAIOOBE(am, () -> { 1081 long o = (long) hs.get(am).invokeExact(array, ci, 0x0123456789ABCDEFL); 1082 }); 1083 } 1084 1085 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 1086 checkAIOOBE(am, () -> { 1087 long o = (long) hs.get(am).invokeExact(array, ci, 0xDEADBEEFDEADBEEFL); 1088 }); 1089 } 1090 1091 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) { 1092 checkAIOOBE(am, () -> { 1093 long o = (long) hs.get(am).invokeExact(array, ci, 0xDEADBEEFDEADBEEFL); 1094 }); 1095 } 1096 } 1097 } 1098 } 1099