jdk/test/java/lang/invoke/VarHandles/VarHandleTestByteArrayAsInt.java
author vlivanov
Mon, 09 May 2016 12:39:41 +0300
changeset 38358 cb99c6d2af1b
parent 37719 add11bc0e6e2
parent 38357 f18eb84a3b96
child 38368 c8eb5d6812c5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     1
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     4
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     8
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    13
 * accompanied this code).
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    14
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    18
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    21
 * questions.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    22
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    23
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    24
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    25
 * @test
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    26
 * @bug 8154556
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    27
 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsInt
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    28
 * @run testng/othervm -Diters=20000                         VarHandleTestByteArrayAsInt
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    29
 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestByteArrayAsInt
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    30
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
import org.testng.annotations.DataProvider;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    33
import org.testng.annotations.Test;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    34
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    35
import java.lang.invoke.MethodHandles;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
import java.lang.invoke.VarHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
import java.nio.ByteBuffer;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
import java.nio.ByteOrder;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
import java.util.ArrayList;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
import java.util.Arrays;
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    41
import java.util.EnumSet;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    42
import java.util.List;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    43
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    44
import static org.testng.Assert.*;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    45
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    46
public class VarHandleTestByteArrayAsInt extends VarHandleBaseByteArrayTest {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    47
    static final int SIZE = Integer.BYTES;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    48
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    49
    static final int VALUE_1 = 0x01020304;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    50
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    51
    static final int VALUE_2 = 0x11121314;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    52
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    53
    static final int VALUE_3 = 0x21222324;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    54
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    55
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    56
    @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    57
    public void setupVarHandleSources() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
        // Combinations of VarHandle byte[] or ByteBuffer
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    59
        vhss = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    60
        for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) {
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    61
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    62
            ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    63
                    ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    64
            VarHandleSource aeh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    65
                    MethodHandles.byteArrayViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    66
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
            vhss.add(aeh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    68
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    69
            VarHandleSource bbh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    70
                    MethodHandles.byteBufferViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    71
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    72
            vhss.add(bbh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    73
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    74
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    75
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    76
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    77
    @Test(dataProvider = "varHandlesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    78
    public void testIsAccessModeSupported(VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    79
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    81
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    82
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    83
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    84
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    85
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    86
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    87
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    88
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    89
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    90
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    91
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    92
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    93
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    94
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    95
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    96
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE));
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    97
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    98
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    99
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   100
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   101
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   102
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   103
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   104
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   105
    @Test(dataProvider = "typesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   106
    public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   107
        assertEquals(vh.varType(), int.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   108
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   109
        assertEquals(vh.coordinateTypes(), pts);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   110
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   111
        testTypes(vh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   112
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   113
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   114
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   115
    @DataProvider
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   116
    public Object[][] accessTestCaseProvider() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   117
        List<AccessTestCase<?>> cases = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   118
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   119
        for (ByteArrayViewSource<?> bav : bavss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   120
            for (VarHandleSource vh : vhss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   121
                if (vh.matches(bav)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   122
                    if (bav instanceof ByteArraySource) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
                        ByteArraySource bas = (ByteArraySource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   124
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   125
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   126
                                "read write", bav, vh, h -> testArrayReadWrite(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   127
                                true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
                                "unsupported", bav, vh, h -> testArrayUnsupported(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   130
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   131
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   132
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   133
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   134
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   135
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   136
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
                    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
                    else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   139
                        ByteBufferSource bbs = (ByteBufferSource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   140
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   141
                        if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   142
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   143
                                    "read write", bav, vh, h -> testArrayReadWrite(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   144
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   145
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   146
                        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   147
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   148
                                    "read only", bav, vh, h -> testArrayReadOnly(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   149
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   150
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   151
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   152
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   153
                                "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   154
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   155
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   156
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   157
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   158
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   159
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   160
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   161
                    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   162
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   163
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   164
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   165
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   166
        // Work around issue with jtreg summary reporting which truncates
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   167
        // the String result of Object.toString to 30 characters, hence
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   168
        // the first dummy argument
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   169
        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   170
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   171
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   172
    @Test(dataProvider = "accessTestCaseProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   173
    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   174
        T t = atc.get();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   175
        int iters = atc.requiresLoop() ? ITERS : 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   176
        for (int c = 0; c < iters; c++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   177
            atc.testAccess(t);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   178
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   179
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   180
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   181
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   182
    static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   183
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   184
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   185
        int ci = 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   186
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   187
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   188
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   189
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   190
    static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   191
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   192
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   193
        int ci = 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   194
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   195
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   196
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   197
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   198
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   199
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   200
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   201
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   202
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   203
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   204
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   205
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   206
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   207
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   208
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   209
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   210
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   211
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   212
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   213
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   214
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   215
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   216
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   217
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   218
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   219
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   220
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   221
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   222
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   223
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   224
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   225
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   226
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   227
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   228
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   229
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   230
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   231
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   232
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   233
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   234
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   235
            checkROBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   236
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   237
            });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   238
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   239
            checkROBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   240
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   241
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   242
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   243
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   244
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   245
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   246
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   247
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   248
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   249
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   250
            checkUOE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   251
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   252
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   253
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   254
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   255
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   256
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   257
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   258
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   259
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   260
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   261
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   262
        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   263
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   264
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   265
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   266
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   267
    static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   268
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   269
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   270
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   271
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   272
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   273
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   274
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   275
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   276
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   277
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   278
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   279
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   280
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   281
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   282
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   283
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   284
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   285
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   286
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   287
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   288
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   289
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   290
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   291
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   292
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   293
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   294
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   295
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   296
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   297
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   298
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   299
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   300
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   301
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   302
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   303
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   304
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   305
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   306
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   307
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   308
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   309
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   310
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   311
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   312
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   313
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   314
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   315
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   316
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   317
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   318
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   319
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   320
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   321
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   322
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   323
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   324
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   325
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   326
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   327
            checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   328
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   329
            });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   330
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   331
            checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   332
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   333
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   334
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   335
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   336
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   337
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   338
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   339
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   340
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   341
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   342
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   343
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   344
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   345
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   346
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   347
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   348
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   349
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   350
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   351
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   352
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   353
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   354
    static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   355
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   356
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   357
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   358
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   359
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   360
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   361
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   362
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   363
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   364
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   365
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   366
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   367
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   368
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   369
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   370
                    vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   371
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   372
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   373
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   374
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   375
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   376
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   377
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   378
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   379
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   380
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   381
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   382
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   383
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   384
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   385
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   386
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   387
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   388
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   389
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   390
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   391
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   392
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   393
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   394
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   395
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   396
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   397
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   398
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   399
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   400
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   401
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   402
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   403
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   404
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   405
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   406
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   407
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   408
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   409
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   410
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   411
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   412
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   413
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   414
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   415
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   416
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   417
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   418
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   419
                checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   420
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   421
                });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   422
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   423
                checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   424
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   425
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   426
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   427
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   428
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   429
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   430
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   431
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   432
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   433
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   434
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   435
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   436
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   437
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   438
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   439
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   440
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   441
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   442
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   443
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   444
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   445
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   446
    static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   447
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   448
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   449
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   450
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   451
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   452
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   453
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   454
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   455
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   456
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   457
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   458
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   459
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   460
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   461
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   462
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   463
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   464
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   465
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   466
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   467
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   468
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   469
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   470
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   471
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   472
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   473
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   474
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   475
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   476
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   477
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   478
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   479
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   480
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   481
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   482
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   483
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   484
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   485
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   486
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   487
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   488
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   489
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   490
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   491
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   492
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   493
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   494
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   495
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   496
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   497
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   498
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   499
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   500
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   501
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   502
                checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   503
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   504
                });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   505
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   506
                checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   507
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   508
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   509
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   510
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   511
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   512
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   513
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   514
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   515
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   516
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   517
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   518
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   519
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   520
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   521
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   522
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   523
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   524
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   525
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   526
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   527
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   528
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   529
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   530
    static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   531
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   532
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   533
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   534
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   535
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   536
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   537
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   538
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   539
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   540
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   541
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   542
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   543
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   544
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   545
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   546
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   547
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   548
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   549
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   550
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   551
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   552
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   553
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   554
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   555
                if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   556
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   557
                        vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   558
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   559
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   560
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   561
                        vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   562
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   563
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   564
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   565
                        vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   566
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   567
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   568
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   569
                        boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   570
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   571
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   572
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   573
                        int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   574
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   575
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   576
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   577
                        int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   578
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   579
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   580
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   581
                        int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   582
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   583
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   584
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   585
                        boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   586
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   587
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   588
                    checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   589
                        boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   590
                    });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   591
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   592
                    checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   593
                        boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   594
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   595
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   596
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   597
                        boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   598
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   599
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   600
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   601
                        int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   602
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   603
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   604
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   605
                        int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   606
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   607
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   608
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   609
                        int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   610
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   611
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   612
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   613
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   614
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   615
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   616
    static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   617
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   618
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   619
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   620
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   621
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   622
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   623
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   624
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   625
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   626
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   627
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   628
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   629
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   630
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   631
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   632
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   633
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   634
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   635
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   636
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   637
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   638
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   639
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   640
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   641
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   642
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   643
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   644
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   645
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   646
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   647
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   648
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   649
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   650
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   651
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   652
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   653
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   654
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   655
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   656
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   657
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   658
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   659
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   660
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   661
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   662
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   663
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   664
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   665
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   666
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   667
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   668
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   669
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   670
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   671
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   672
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   673
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   674
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   675
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   676
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   677
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   678
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   679
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   680
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   681
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   682
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   683
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   684
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   685
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   686
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   687
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   688
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   689
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   690
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   691
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   692
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   693
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   694
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   695
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   696
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   697
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   698
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   699
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   700
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   701
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   702
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   703
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   704
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   705
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   706
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   707
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   708
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   709
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   710
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   711
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   712
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   713
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   714
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   715
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   716
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   717
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   718
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   719
                        success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   720
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   721
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   722
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   723
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   724
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   725
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   726
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   727
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   728
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   729
                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   730
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   731
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   732
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   733
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   734
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   735
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   736
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   737
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   738
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   739
                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   740
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   741
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   742
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   743
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   744
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   745
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   746
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   747
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   748
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   749
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   750
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   751
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   752
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   753
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   754
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   755
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   756
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   757
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   758
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   759
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   760
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   761
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   762
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   763
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   764
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   765
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   766
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   767
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   768
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   769
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   770
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   771
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   772
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   773
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   774
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   775
    static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   776
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   777
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   778
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   779
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   780
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   781
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   782
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   783
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   784
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   785
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   786
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   787
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   788
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   789
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   790
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   791
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   792
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   793
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   794
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   795
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   796
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   797
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   798
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   799
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   800
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   801
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   802
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   803
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   804
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   805
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   806
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   807
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   808
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   809
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   810
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   811
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   812
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   813
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   814
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   815
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   816
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   817
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   818
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   819
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   820
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   821
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   822
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   823
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   824
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   825
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   826
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   827
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   828
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   829
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   830
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   831
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   832
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   833
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   834
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   835
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   836
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   837
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   838
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   839
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   840
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   841
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   842
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   843
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   844
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   845
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   846
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   847
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   848
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   849
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   850
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   851
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   852
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   853
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   854
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   855
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   856
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   857
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   858
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   859
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   860
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   861
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   862
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   863
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   864
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   865
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   866
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   867
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   868
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   869
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   870
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   871
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   872
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   873
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   874
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   875
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   876
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   877
                        success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   878
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   879
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   880
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   881
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   882
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   883
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   884
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   885
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   886
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   887
                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   888
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   889
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   890
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   891
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   892
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   893
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   894
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   895
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   896
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   897
                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   898
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   899
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   900
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   901
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   902
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   903
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   904
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   905
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   906
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   907
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   908
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   909
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   910
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   911
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   912
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   913
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   914
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   915
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   916
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   917
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   918
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   919
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   920
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   921
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   922
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   923
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   924
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   925
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   926
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   927
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   928
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   929
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   930
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   931
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   932
    static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   933
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   934
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   935
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   936
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   937
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   938
        ByteBuffer bb = ByteBuffer.allocate(SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   939
        bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   940
        bs.fill(bb.putInt(0, VALUE_2).array());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   941
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   942
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   943
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   944
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   945
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   946
            int v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   947
                    ? rotateLeft(VALUE_2, (i % SIZE) << 3)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   948
                    : rotateRight(VALUE_2, (i % SIZE) << 3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   949
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   950
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   951
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   952
                assertEquals(x, v, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   953
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   954
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   955
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   956
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   957
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   958
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   959
                    assertEquals(x, v, "getVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   960
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   961
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   962
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   963
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   964
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   965
                    assertEquals(x, v, "getRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   966
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   967
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   968
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   969
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   970
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   971
                    assertEquals(x, v, "getOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   972
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   973
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   974
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   975
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   976
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   977
}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   978