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