1 /*
  2  * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 
 25 /*
 26  * @test
 27  * @summary test VarHandle on value class array
 28  * @enablePreview
 29  * @run junit/othervm -XX:FlatArrayElementMaxSize=-1 ArrayElementVarHandleTest
 30  * @run junit/othervm -XX:FlatArrayElementMaxSize=0  ArrayElementVarHandleTest
 31  */
 32 
 33 import java.lang.invoke.*;
 34 import java.util.stream.Stream;
 35 
 36 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 37 import jdk.internal.vm.annotation.NullRestricted;
 38 import org.junit.jupiter.params.ParameterizedTest;
 39 import org.junit.jupiter.params.provider.Arguments;
 40 import org.junit.jupiter.params.provider.MethodSource;
 41 import static org.junit.jupiter.api.Assertions.*;
 42 
 43 public class ArrayElementVarHandleTest {
 44     @ImplicitlyConstructible
 45     static value class Point {
 46         public int x;
 47         public int y;
 48         Point(int x, int y) {
 49             this.x = x;
 50             this.y = y;
 51         }
 52     }
 53 
 54     @ImplicitlyConstructible
 55     static value class Line {
 56         @NullRestricted
 57         Point p1;
 58         @NullRestricted
 59         Point p2;
 60 
 61         Line(Point p1, Point p2) {
 62             this.p1 = p1;
 63             this.p2 = p2;
 64         }
 65         Line(int x1, int y1, int x2, int y2) {
 66             this(new Point(x1, y1), new Point(x2, y2));
 67         }
 68     }
 69 
 70     private static final Point[] POINTS = new Point[]{
 71             new Point(1, 2),
 72             new Point(10, 20),
 73             new Point(100, 200),
 74             null
 75     };
 76 
 77     private static final Line[] LINES = new Line[]{
 78             new Line(1, 2, 3, 4),
 79             new Line(10, 20, 30, 40),
 80             null
 81     };
 82 
 83     static Stream<Arguments> testCases() throws Throwable {
 84         int plen = POINTS.length;
 85         int llen = LINES.length;
 86         return Stream.of(
 87                 Arguments.of(newArray(Object[].class, plen),    POINTS),
 88                 Arguments.of(newArray(Object[].class, plen),    new Object[] { "abc", new Point(1, 2) }),
 89                 Arguments.of(newArray(Point[].class, plen),     POINTS),
 90                 Arguments.of(new Point[plen],                   POINTS),
 91 
 92                 Arguments.of(newArray(Object[].class, llen),    LINES),
 93                 Arguments.of(newArray(Line[].class, llen),      LINES),
 94                 Arguments.of(new Line[llen],                    LINES)
 95         );
 96     }
 97 
 98     /*
 99      * Constructs a new array of the specified type and size using
100      * MethodHandle.
101      */
102     private static Object[] newArray(Class<?> arrayType, int size) throws Throwable {
103         MethodHandle ctor = MethodHandles.arrayConstructor(arrayType);
104         return (Object[]) ctor.invoke(size);
105     }
106 
107     /*
108      * Test VarHandle to set elements of the given array with
109      * various access mode.
110      */
111     @ParameterizedTest
112     @MethodSource("testCases")
113     public void testSetArrayElements(Object[] array, Object[] elements) {
114         Class<?> arrayType = array.getClass();
115         assertTrue(array.length >= elements.length);
116 
117         VarHandle vh = MethodHandles.arrayElementVarHandle(arrayType);
118         set(vh, array.clone(), elements);
119         setVolatile(vh, array.clone(), elements);
120         setOpaque(vh, array.clone(), elements);
121         setRelease(vh, array.clone(), elements);
122         getAndSet(vh, array.clone(), elements);
123         compareAndSet(vh, array.clone(), elements);
124         compareAndExchange(vh, array.clone(), elements);
125     }
126 
127     // VarHandle::set
128     void set(VarHandle vh, Object[] array, Object[] elements) {
129         for (int i = 0; i < elements.length; i++) {
130             vh.set(array, i, elements[i]);
131         }
132         for (int i = 0; i < elements.length; i++) {
133             Object v = (Object) vh.get(array, i);
134             assertEquals(elements[i], v);
135         }
136     }
137 
138     // VarHandle::setVolatile
139     void setVolatile(VarHandle vh, Object[] array, Object[] elements) {
140         for (int i = 0; i < elements.length; i++) {
141             vh.setVolatile(array, i, elements[i]);
142         }
143         for (int i = 0; i < elements.length; i++) {
144             Object v = (Object) vh.getVolatile(array, i);
145             assertEquals(elements[i], v);
146         }
147     }
148 
149     // VarHandle::setOpaque
150     void setOpaque(VarHandle vh, Object[] array, Object[] elements) {
151         for (int i = 0; i < elements.length; i++) {
152             vh.setOpaque(array, i, elements[i]);
153         }
154         for (int i = 0; i < elements.length; i++) {
155             Object v = (Object) vh.getOpaque(array, i);
156             assertEquals(elements[i], v);
157         }
158     }
159 
160     // VarHandle::setRelease
161     void setRelease(VarHandle vh, Object[] array, Object[] elements) {
162         for (int i = 0; i < elements.length; i++) {
163             vh.setRelease(array, i, elements[i]);
164         }
165         for (int i = 0; i < elements.length; i++) {
166             Object v = (Object) vh.getAcquire(array, i);
167             assertEquals(elements[i], v);
168         }
169     }
170 
171     void getAndSet(VarHandle vh, Object[] array, Object[] elements) {
172         for (int i = 0; i < elements.length; i++) {
173             Object o = vh.getAndSet(array, i, elements[i]);
174         }
175         for (int i = 0; i < elements.length; i++) {
176             Object v = (Object) vh.get(array, i);
177             assertEquals(elements[i], v);
178         }
179     }
180 
181     // sanity CAS test
182     // see test/jdk/java/lang/invoke/VarHandles tests
183     void compareAndSet(VarHandle vh, Object[] array, Object[] elements) {
184         // initialize to some values
185         for (int i = 0; i < elements.length; i++) {
186             vh.set(array, i, elements[i]);
187         }
188         // shift to the right element
189         for (int i = 0; i < elements.length; i++) {
190             Object v = elements[i + 1 < elements.length ? i + 1 : 0];
191             boolean cas = vh.compareAndSet(array, i, elements[i], v);
192             if (!cas)
193                 System.out.format("cas = %s array[%d] = %s vs old = %s new = %s%n", cas, i, array[i], elements[i], v);
194             assertTrue(cas);
195         }
196     }
197 
198     void compareAndExchange(VarHandle vh, Object[] array, Object[] elements) {
199         // initialize to some values
200         for (int i = 0; i < elements.length; i++) {
201             vh.set(array, i, elements[i]);
202         }
203         // shift to the right element
204         for (int i = 0; i < elements.length; i++) {
205             Object v = elements[i + 1 < elements.length ? i + 1 : 0];
206             assertEquals(elements[i], vh.compareAndExchange(array, i, elements[i], v));
207         }
208     }
209 }