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