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