2016-03-24 10:21:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessLong
|
|
|
|
*/
|
|
|
|
|
|
|
|
import org.testng.annotations.BeforeClass;
|
|
|
|
import org.testng.annotations.DataProvider;
|
|
|
|
import org.testng.annotations.Test;
|
|
|
|
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.VarHandle;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
|
|
|
public class VarHandleTestMethodHandleAccessLong extends VarHandleBaseTest {
|
|
|
|
static final long static_final_v = 1L;
|
|
|
|
|
|
|
|
static long static_v;
|
|
|
|
|
|
|
|
final long final_v = 1L;
|
|
|
|
|
|
|
|
long v;
|
|
|
|
|
|
|
|
VarHandle vhFinalField;
|
|
|
|
|
|
|
|
VarHandle vhField;
|
|
|
|
|
|
|
|
VarHandle vhStaticField;
|
|
|
|
|
|
|
|
VarHandle vhStaticFinalField;
|
|
|
|
|
|
|
|
VarHandle vhArray;
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public void setup() throws Exception {
|
|
|
|
vhFinalField = MethodHandles.lookup().findVarHandle(
|
|
|
|
VarHandleTestMethodHandleAccessLong.class, "final_v", long.class);
|
|
|
|
|
|
|
|
vhField = MethodHandles.lookup().findVarHandle(
|
|
|
|
VarHandleTestMethodHandleAccessLong.class, "v", long.class);
|
|
|
|
|
|
|
|
vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
|
|
|
|
VarHandleTestMethodHandleAccessLong.class, "static_final_v", long.class);
|
|
|
|
|
|
|
|
vhStaticField = MethodHandles.lookup().findStaticVarHandle(
|
|
|
|
VarHandleTestMethodHandleAccessLong.class, "static_v", long.class);
|
|
|
|
|
|
|
|
vhArray = MethodHandles.arrayElementVarHandle(long[].class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@DataProvider
|
|
|
|
public Object[][] accessTestCaseProvider() throws Exception {
|
|
|
|
List<AccessTestCase<?>> cases = new ArrayList<>();
|
|
|
|
|
|
|
|
for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Instance field",
|
|
|
|
vhField, f, hs -> testInstanceField(this, hs)));
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
|
|
|
|
vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
|
|
|
|
false));
|
|
|
|
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Static field",
|
|
|
|
vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticField));
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Static field unsupported",
|
|
|
|
vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticFieldUnsupported,
|
|
|
|
false));
|
|
|
|
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Array",
|
|
|
|
vhArray, f, VarHandleTestMethodHandleAccessLong::testArray));
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Array unsupported",
|
|
|
|
vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayUnsupported,
|
|
|
|
false));
|
|
|
|
cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
|
|
|
|
vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayIndexOutOfBounds,
|
|
|
|
false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work around issue with jtreg summary reporting which truncates
|
|
|
|
// the String result of Object.toString to 30 characters, hence
|
|
|
|
// the first dummy argument
|
|
|
|
return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "accessTestCaseProvider")
|
|
|
|
public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
|
|
|
|
T t = atc.get();
|
|
|
|
int iters = atc.requiresLoop() ? ITERS : 1;
|
|
|
|
for (int c = 0; c < iters; c++) {
|
|
|
|
atc.testAccess(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void testInstanceField(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable {
|
|
|
|
// Plain
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "set long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Volatile
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lazy
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "setRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opaque
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setOpaque long value");
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// Compare
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, true, "success compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, false, "failing compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "success compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "weakCompareAndSetAcquire long");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSetRelease long");
|
|
|
|
}
|
|
|
|
|
2016-05-17 19:28:00 +00:00
|
|
|
{
|
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(recv, 2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetVolatile long");
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
|
|
|
assertEquals(x, 1L, "weakCompareAndSetVolatile long");
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:21:21 +00:00
|
|
|
// Compare set and get
|
|
|
|
{
|
2016-05-17 19:28:00 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2L);
|
|
|
|
assertEquals(o, 1L, "getAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
|
2016-05-17 19:28:00 +00:00
|
|
|
assertEquals(x, 2L, "getAndSet long value");
|
2016-03-24 10:21:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// get and add, add and get
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(o, 1L, "getAndAdd long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void testStaticField(Handles hs) throws Throwable {
|
|
|
|
// Plain
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "set long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Volatile
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lazy
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_RELEASE).invokeExact(1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "setRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opaque
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setOpaque long value");
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// Compare
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, true, "success compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, false, "failing compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "success compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "weakCompareAndSetAcquire long");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSetRelease long");
|
|
|
|
}
|
|
|
|
|
2016-05-17 19:28:00 +00:00
|
|
|
{
|
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetVolatile long");
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
|
|
|
assertEquals(x, 1L, "weakCompareAndSetVolatile long");
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:21:21 +00:00
|
|
|
// Compare set and get
|
|
|
|
{
|
2016-05-17 19:28:00 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact( 2L);
|
|
|
|
assertEquals(o, 1L, "getAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact();
|
2016-05-17 19:28:00 +00:00
|
|
|
assertEquals(x, 2L, "getAndSet long value");
|
2016-03-24 10:21:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// get and add, add and get
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(o, 1L, "getAndAdd long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testStaticFieldUnsupported(Handles hs) throws Throwable {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void testArray(Handles hs) throws Throwable {
|
|
|
|
long[] array = new long[10];
|
|
|
|
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
|
|
// Plain
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "get long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Volatile
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lazy
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "setRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opaque
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2L);
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "setOpaque long value");
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// Compare
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, true, "success compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, false, "failing compareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 2L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "success compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 2L, "success compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "success compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(r, 1L, "failing compareAndExchangeRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSet long value");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetAcquire long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 1L, "weakCompareAndSetAcquire long");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-04 14:17:28 +00:00
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, 1L, 2L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetRelease long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(x, 2L, "weakCompareAndSetRelease long");
|
|
|
|
}
|
|
|
|
|
2016-05-17 19:28:00 +00:00
|
|
|
{
|
|
|
|
boolean success = false;
|
|
|
|
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
|
|
|
|
success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(array, i, 2L, 1L);
|
|
|
|
}
|
|
|
|
assertEquals(success, true, "weakCompareAndSetVolatile long");
|
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
|
|
|
assertEquals(x, 1L, "weakCompareAndSetVolatile long");
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:21:21 +00:00
|
|
|
// Compare set and get
|
|
|
|
{
|
2016-05-17 19:28:00 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2L);
|
|
|
|
assertEquals(o, 1L, "getAndSet long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
|
2016-05-17 19:28:00 +00:00
|
|
|
assertEquals(x, 2L, "getAndSet long value");
|
2016-03-24 10:21:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
|
2016-03-24 10:21:21 +00:00
|
|
|
|
|
|
|
// get and add, add and get
|
|
|
|
{
|
2016-04-13 13:05:48 +00:00
|
|
|
long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(o, 1L, "getAndAdd long");
|
2016-04-13 13:05:48 +00:00
|
|
|
long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, 3L);
|
2016-03-24 10:21:21 +00:00
|
|
|
assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testArrayUnsupported(Handles hs) throws Throwable {
|
|
|
|
long[] array = new long[10];
|
|
|
|
|
|
|
|
final int i = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
|
|
|
|
long[] array = new long[10];
|
|
|
|
|
|
|
|
for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
|
|
|
|
final int ci = i;
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
long x = (long) hs.get(am).invokeExact(array, ci);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
hs.get(am).invokeExact(array, ci, 1L);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1L, 2L);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
long r = (long) hs.get(am).invokeExact(array, ci, 2L, 1L);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
long o = (long) hs.get(am).invokeExact(array, ci, 1L);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:05:48 +00:00
|
|
|
for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
|
2016-03-24 10:21:21 +00:00
|
|
|
checkIOOBE(am, () -> {
|
|
|
|
long o = (long) hs.get(am).invokeExact(array, ci, 3L);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|