author | vlivanov |
Mon, 09 May 2016 12:39:41 +0300 | |
changeset 38358 | cb99c6d2af1b |
parent 37719 | add11bc0e6e2 |
parent 38357 | f18eb84a3b96 |
child 38368 | c8eb5d6812c5 |
permissions | -rw-r--r-- |
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 |
||
53 |
static final int VALUE_3 = 0x21222324; |
|
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 |
}); |
|
250 |
checkUOE(() -> { |
|
251 |
boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
|
252 |
}); |
|
253 |
||
254 |
checkROBE(() -> { |
|
255 |
int o = (int) vh.getAndAdd(array, ci, VALUE_1); |
|
256 |
}); |
|
257 |
||
258 |
checkROBE(() -> { |
|
259 |
int o = (int) vh.addAndGet(array, ci, VALUE_1); |
|
260 |
}); |
|
261 |
} |
|
262 |
else { |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
||
267 |
static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
|
268 |
VarHandle vh = vhs.s; |
|
269 |
byte[] array = bs.s; |
|
270 |
||
271 |
int length = array.length - SIZE + 1; |
|
272 |
for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
|
273 |
final int ci = i; |
|
274 |
||
275 |
checkIOOBE(() -> { |
|
276 |
int x = (int) vh.get(array, ci); |
|
277 |
}); |
|
278 |
||
279 |
checkIOOBE(() -> { |
|
280 |
vh.set(array, ci, VALUE_1); |
|
281 |
}); |
|
282 |
||
283 |
checkIOOBE(() -> { |
|
284 |
int x = (int) vh.getVolatile(array, ci); |
|
285 |
}); |
|
286 |
||
287 |
checkIOOBE(() -> { |
|
288 |
int x = (int) vh.getAcquire(array, ci); |
|
289 |
}); |
|
290 |
||
291 |
checkIOOBE(() -> { |
|
292 |
int x = (int) vh.getOpaque(array, ci); |
|
293 |
}); |
|
294 |
||
295 |
checkIOOBE(() -> { |
|
296 |
vh.setVolatile(array, ci, VALUE_1); |
|
297 |
}); |
|
298 |
||
299 |
checkIOOBE(() -> { |
|
300 |
vh.setRelease(array, ci, VALUE_1); |
|
301 |
}); |
|
302 |
||
303 |
checkIOOBE(() -> { |
|
304 |
vh.setOpaque(array, ci, VALUE_1); |
|
305 |
}); |
|
306 |
||
307 |
checkIOOBE(() -> { |
|
308 |
boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
|
309 |
}); |
|
310 |
||
311 |
checkIOOBE(() -> { |
|
312 |
int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
|
313 |
}); |
|
314 |
||
315 |
checkIOOBE(() -> { |
|
316 |
int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
|
317 |
}); |
|
318 |
||
319 |
checkIOOBE(() -> { |
|
320 |
int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
|
321 |
}); |
|
322 |
||
323 |
checkIOOBE(() -> { |
|
324 |
boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
|
325 |
}); |
|
326 |
||
327 |
checkIOOBE(() -> { |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
328 |
boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
329 |
}); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
330 |
|
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
331 |
checkIOOBE(() -> { |
36934 | 332 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
333 |
}); |
|
334 |
||
335 |
checkIOOBE(() -> { |
|
336 |
boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
|
337 |
}); |
|
338 |
||
339 |
checkIOOBE(() -> { |
|
340 |
int o = (int) vh.getAndSet(array, ci, VALUE_1); |
|
341 |
}); |
|
342 |
||
343 |
checkIOOBE(() -> { |
|
344 |
int o = (int) vh.getAndAdd(array, ci, VALUE_1); |
|
345 |
}); |
|
346 |
||
347 |
checkIOOBE(() -> { |
|
348 |
int o = (int) vh.addAndGet(array, ci, VALUE_1); |
|
349 |
}); |
|
350 |
||
351 |
} |
|
352 |
} |
|
353 |
||
354 |
static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
|
355 |
VarHandle vh = vhs.s; |
|
356 |
ByteBuffer array = bs.s; |
|
357 |
||
358 |
boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
|
359 |
||
360 |
int length = array.limit() - SIZE + 1; |
|
361 |
for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
|
362 |
final int ci = i; |
|
363 |
||
364 |
checkIOOBE(() -> { |
|
365 |
int x = (int) vh.get(array, ci); |
|
366 |
}); |
|
367 |
||
368 |
if (!readOnly) { |
|
369 |
checkIOOBE(() -> { |
|
370 |
vh.set(array, ci, VALUE_1); |
|
371 |
}); |
|
372 |
} |
|
373 |
||
374 |
checkIOOBE(() -> { |
|
375 |
int x = (int) vh.getVolatile(array, ci); |
|
376 |
}); |
|
377 |
||
378 |
checkIOOBE(() -> { |
|
379 |
int x = (int) vh.getAcquire(array, ci); |
|
380 |
}); |
|
381 |
||
382 |
checkIOOBE(() -> { |
|
383 |
int x = (int) vh.getOpaque(array, ci); |
|
384 |
}); |
|
385 |
||
386 |
if (!readOnly) { |
|
387 |
checkIOOBE(() -> { |
|
388 |
vh.setVolatile(array, ci, VALUE_1); |
|
389 |
}); |
|
390 |
||
391 |
checkIOOBE(() -> { |
|
392 |
vh.setRelease(array, ci, VALUE_1); |
|
393 |
}); |
|
394 |
||
395 |
checkIOOBE(() -> { |
|
396 |
vh.setOpaque(array, ci, VALUE_1); |
|
397 |
}); |
|
398 |
||
399 |
checkIOOBE(() -> { |
|
400 |
boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
|
401 |
}); |
|
402 |
||
403 |
checkIOOBE(() -> { |
|
404 |
int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
|
405 |
}); |
|
406 |
||
407 |
checkIOOBE(() -> { |
|
408 |
int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
|
409 |
}); |
|
410 |
||
411 |
checkIOOBE(() -> { |
|
412 |
int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
|
413 |
}); |
|
414 |
||
415 |
checkIOOBE(() -> { |
|
416 |
boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
|
417 |
}); |
|
418 |
||
419 |
checkIOOBE(() -> { |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
420 |
boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
421 |
}); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
422 |
|
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
423 |
checkIOOBE(() -> { |
36934 | 424 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
425 |
}); |
|
426 |
||
427 |
checkIOOBE(() -> { |
|
428 |
boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
|
429 |
}); |
|
430 |
||
431 |
checkIOOBE(() -> { |
|
432 |
int o = (int) vh.getAndSet(array, ci, VALUE_1); |
|
433 |
}); |
|
434 |
||
435 |
checkIOOBE(() -> { |
|
436 |
int o = (int) vh.getAndAdd(array, ci, VALUE_1); |
|
437 |
}); |
|
438 |
||
439 |
checkIOOBE(() -> { |
|
440 |
int o = (int) vh.addAndGet(array, ci, VALUE_1); |
|
441 |
}); |
|
442 |
} |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
|
447 |
VarHandle vh = vhs.s; |
|
448 |
byte[] array = bs.s; |
|
449 |
||
450 |
int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
|
451 |
||
452 |
int length = array.length - SIZE + 1; |
|
453 |
for (int i = 0; i < length; i++) { |
|
454 |
boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
|
455 |
final int ci = i; |
|
456 |
||
457 |
if (!iAligned) { |
|
458 |
checkISE(() -> { |
|
459 |
int x = (int) vh.getVolatile(array, ci); |
|
460 |
}); |
|
461 |
||
462 |
checkISE(() -> { |
|
463 |
int x = (int) vh.getAcquire(array, ci); |
|
464 |
}); |
|
465 |
||
466 |
checkISE(() -> { |
|
467 |
int x = (int) vh.getOpaque(array, ci); |
|
468 |
}); |
|
469 |
||
470 |
checkISE(() -> { |
|
471 |
vh.setVolatile(array, ci, VALUE_1); |
|
472 |
}); |
|
473 |
||
474 |
checkISE(() -> { |
|
475 |
vh.setRelease(array, ci, VALUE_1); |
|
476 |
}); |
|
477 |
||
478 |
checkISE(() -> { |
|
479 |
vh.setOpaque(array, ci, VALUE_1); |
|
480 |
}); |
|
481 |
||
482 |
checkISE(() -> { |
|
483 |
boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
|
484 |
}); |
|
485 |
||
486 |
checkISE(() -> { |
|
487 |
int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
|
488 |
}); |
|
489 |
||
490 |
checkISE(() -> { |
|
491 |
int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
|
492 |
}); |
|
493 |
||
494 |
checkISE(() -> { |
|
495 |
int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
|
496 |
}); |
|
497 |
||
498 |
checkISE(() -> { |
|
499 |
boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
|
500 |
}); |
|
501 |
||
502 |
checkISE(() -> { |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
503 |
boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
504 |
}); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
505 |
|
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
506 |
checkISE(() -> { |
36934 | 507 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
508 |
}); |
|
509 |
||
510 |
checkISE(() -> { |
|
511 |
boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
|
512 |
}); |
|
513 |
||
514 |
checkISE(() -> { |
|
515 |
int o = (int) vh.getAndSet(array, ci, VALUE_1); |
|
516 |
}); |
|
517 |
||
518 |
checkISE(() -> { |
|
519 |
int o = (int) vh.getAndAdd(array, ci, VALUE_1); |
|
520 |
}); |
|
521 |
||
522 |
checkISE(() -> { |
|
523 |
int o = (int) vh.addAndGet(array, ci, VALUE_1); |
|
524 |
}); |
|
525 |
||
526 |
} |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
|
531 |
VarHandle vh = vhs.s; |
|
532 |
ByteBuffer array = bs.s; |
|
533 |
||
534 |
boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
|
535 |
int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
|
536 |
||
537 |
int length = array.limit() - SIZE + 1; |
|
538 |
for (int i = 0; i < length; i++) { |
|
539 |
boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
|
540 |
final int ci = i; |
|
541 |
||
542 |
if (!iAligned) { |
|
543 |
checkISE(() -> { |
|
544 |
int x = (int) vh.getVolatile(array, ci); |
|
545 |
}); |
|
546 |
||
547 |
checkISE(() -> { |
|
548 |
int x = (int) vh.getAcquire(array, ci); |
|
549 |
}); |
|
550 |
||
551 |
checkISE(() -> { |
|
552 |
int x = (int) vh.getOpaque(array, ci); |
|
553 |
}); |
|
554 |
||
555 |
if (!readOnly) { |
|
556 |
checkISE(() -> { |
|
557 |
vh.setVolatile(array, ci, VALUE_1); |
|
558 |
}); |
|
559 |
||
560 |
checkISE(() -> { |
|
561 |
vh.setRelease(array, ci, VALUE_1); |
|
562 |
}); |
|
563 |
||
564 |
checkISE(() -> { |
|
565 |
vh.setOpaque(array, ci, VALUE_1); |
|
566 |
}); |
|
567 |
||
568 |
checkISE(() -> { |
|
569 |
boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
|
570 |
}); |
|
571 |
||
572 |
checkISE(() -> { |
|
573 |
int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
|
574 |
}); |
|
575 |
||
576 |
checkISE(() -> { |
|
577 |
int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
|
578 |
}); |
|
579 |
||
580 |
checkISE(() -> { |
|
581 |
int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
|
582 |
}); |
|
583 |
||
584 |
checkISE(() -> { |
|
585 |
boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
|
586 |
}); |
|
587 |
||
588 |
checkISE(() -> { |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
589 |
boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
590 |
}); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
591 |
|
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
592 |
checkISE(() -> { |
36934 | 593 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
594 |
}); |
|
595 |
||
596 |
checkISE(() -> { |
|
597 |
boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
|
598 |
}); |
|
599 |
||
600 |
checkISE(() -> { |
|
601 |
int o = (int) vh.getAndSet(array, ci, VALUE_1); |
|
602 |
}); |
|
603 |
||
604 |
checkISE(() -> { |
|
605 |
int o = (int) vh.getAndAdd(array, ci, VALUE_1); |
|
606 |
}); |
|
607 |
||
608 |
checkISE(() -> { |
|
609 |
int o = (int) vh.addAndGet(array, ci, VALUE_1); |
|
610 |
}); |
|
611 |
} |
|
612 |
} |
|
613 |
} |
|
614 |
} |
|
615 |
||
616 |
static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { |
|
617 |
VarHandle vh = vhs.s; |
|
618 |
byte[] array = bs.s; |
|
619 |
||
620 |
int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
|
621 |
||
622 |
bs.fill((byte) 0xff); |
|
623 |
int length = array.length - SIZE + 1; |
|
624 |
for (int i = 0; i < length; i++) { |
|
625 |
boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
|
626 |
||
627 |
// Plain |
|
628 |
{ |
|
629 |
vh.set(array, i, VALUE_1); |
|
630 |
int x = (int) vh.get(array, i); |
|
631 |
assertEquals(x, VALUE_1, "get int value"); |
|
632 |
} |
|
633 |
||
634 |
||
635 |
if (iAligned) { |
|
636 |
// Volatile |
|
637 |
{ |
|
638 |
vh.setVolatile(array, i, VALUE_2); |
|
639 |
int x = (int) vh.getVolatile(array, i); |
|
640 |
assertEquals(x, VALUE_2, "setVolatile int value"); |
|
641 |
} |
|
642 |
||
643 |
// Lazy |
|
644 |
{ |
|
645 |
vh.setRelease(array, i, VALUE_1); |
|
646 |
int x = (int) vh.getAcquire(array, i); |
|
647 |
assertEquals(x, VALUE_1, "setRelease int value"); |
|
648 |
} |
|
649 |
||
650 |
// Opaque |
|
651 |
{ |
|
652 |
vh.setOpaque(array, i, VALUE_2); |
|
653 |
int x = (int) vh.getOpaque(array, i); |
|
654 |
assertEquals(x, VALUE_2, "setOpaque int value"); |
|
655 |
} |
|
656 |
||
657 |
vh.set(array, i, VALUE_1); |
|
658 |
||
659 |
// Compare |
|
660 |
{ |
|
661 |
boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); |
|
662 |
assertEquals(r, true, "success compareAndSet int"); |
|
663 |
int x = (int) vh.get(array, i); |
|
664 |
assertEquals(x, VALUE_2, "success compareAndSet int value"); |
|
665 |
} |
|
666 |
||
667 |
{ |
|
668 |
boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); |
|
669 |
assertEquals(r, false, "failing compareAndSet int"); |
|
670 |
int x = (int) vh.get(array, i); |
|
671 |
assertEquals(x, VALUE_2, "failing compareAndSet int value"); |
|
672 |
} |
|
673 |
||
674 |
{ |
|
675 |
int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1); |
|
676 |
assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int"); |
|
677 |
int x = (int) vh.get(array, i); |
|
678 |
assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value"); |
|
679 |
} |
|
680 |
||
681 |
{ |
|
682 |
int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3); |
|
683 |
assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int"); |
|
684 |
int x = (int) vh.get(array, i); |
|
685 |
assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value"); |
|
686 |
} |
|
687 |
||
688 |
{ |
|
689 |
int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); |
|
690 |
assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int"); |
|
691 |
int x = (int) vh.get(array, i); |
|
692 |
assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value"); |
|
693 |
} |
|
694 |
||
695 |
{ |
|
696 |
int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); |
|
697 |
assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int"); |
|
698 |
int x = (int) vh.get(array, i); |
|
699 |
assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value"); |
|
700 |
} |
|
701 |
||
702 |
{ |
|
703 |
int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); |
|
704 |
assertEquals(r, VALUE_2, "success compareAndExchangeRelease int"); |
|
705 |
int x = (int) vh.get(array, i); |
|
706 |
assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value"); |
|
707 |
} |
|
708 |
||
709 |
{ |
|
710 |
int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); |
|
711 |
assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int"); |
|
712 |
int x = (int) vh.get(array, i); |
|
713 |
assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value"); |
|
714 |
} |
|
715 |
||
716 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
717 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
718 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
719 |
success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
720 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
721 |
assertEquals(success, true, "weakCompareAndSet int"); |
36934 | 722 |
int x = (int) vh.get(array, i); |
723 |
assertEquals(x, VALUE_2, "weakCompareAndSet int value"); |
|
724 |
} |
|
725 |
||
726 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
727 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
728 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
729 |
success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
730 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
731 |
assertEquals(success, true, "weakCompareAndSetAcquire int"); |
36934 | 732 |
int x = (int) vh.get(array, i); |
733 |
assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int"); |
|
734 |
} |
|
735 |
||
736 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
737 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
738 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
739 |
success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
740 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
741 |
assertEquals(success, true, "weakCompareAndSetRelease int"); |
36934 | 742 |
int x = (int) vh.get(array, i); |
743 |
assertEquals(x, VALUE_2, "weakCompareAndSetRelease int"); |
|
744 |
} |
|
745 |
||
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
746 |
{ |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
747 |
boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
748 |
assertEquals(r, true, "weakCompareAndSetVolatile int"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
749 |
int x = (int) vh.get(array, i); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
750 |
assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
751 |
} |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
752 |
|
36934 | 753 |
// Compare set and get |
754 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
755 |
int o = (int) vh.getAndSet(array, i, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
756 |
assertEquals(o, VALUE_1, "getAndSet int"); |
36934 | 757 |
int x = (int) vh.get(array, i); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
758 |
assertEquals(x, VALUE_2, "getAndSet int value"); |
36934 | 759 |
} |
760 |
||
761 |
vh.set(array, i, VALUE_1); |
|
762 |
||
763 |
// get and add, add and get |
|
764 |
{ |
|
765 |
int o = (int) vh.getAndAdd(array, i, VALUE_3); |
|
766 |
assertEquals(o, VALUE_1, "getAndAdd int"); |
|
767 |
int c = (int) vh.addAndGet(array, i, VALUE_3); |
|
768 |
assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value"); |
|
769 |
} |
|
770 |
} |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
||
775 |
static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { |
|
776 |
VarHandle vh = vhs.s; |
|
777 |
ByteBuffer array = bs.s; |
|
778 |
||
779 |
int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
|
780 |
||
781 |
bs.fill((byte) 0xff); |
|
782 |
int length = array.limit() - SIZE + 1; |
|
783 |
for (int i = 0; i < length; i++) { |
|
784 |
boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
|
785 |
||
786 |
// Plain |
|
787 |
{ |
|
788 |
vh.set(array, i, VALUE_1); |
|
789 |
int x = (int) vh.get(array, i); |
|
790 |
assertEquals(x, VALUE_1, "get int value"); |
|
791 |
} |
|
792 |
||
793 |
if (iAligned) { |
|
794 |
// Volatile |
|
795 |
{ |
|
796 |
vh.setVolatile(array, i, VALUE_2); |
|
797 |
int x = (int) vh.getVolatile(array, i); |
|
798 |
assertEquals(x, VALUE_2, "setVolatile int value"); |
|
799 |
} |
|
800 |
||
801 |
// Lazy |
|
802 |
{ |
|
803 |
vh.setRelease(array, i, VALUE_1); |
|
804 |
int x = (int) vh.getAcquire(array, i); |
|
805 |
assertEquals(x, VALUE_1, "setRelease int value"); |
|
806 |
} |
|
807 |
||
808 |
// Opaque |
|
809 |
{ |
|
810 |
vh.setOpaque(array, i, VALUE_2); |
|
811 |
int x = (int) vh.getOpaque(array, i); |
|
812 |
assertEquals(x, VALUE_2, "setOpaque int value"); |
|
813 |
} |
|
814 |
||
815 |
vh.set(array, i, VALUE_1); |
|
816 |
||
817 |
// Compare |
|
818 |
{ |
|
819 |
boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); |
|
820 |
assertEquals(r, true, "success compareAndSet int"); |
|
821 |
int x = (int) vh.get(array, i); |
|
822 |
assertEquals(x, VALUE_2, "success compareAndSet int value"); |
|
823 |
} |
|
824 |
||
825 |
{ |
|
826 |
boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); |
|
827 |
assertEquals(r, false, "failing compareAndSet int"); |
|
828 |
int x = (int) vh.get(array, i); |
|
829 |
assertEquals(x, VALUE_2, "failing compareAndSet int value"); |
|
830 |
} |
|
831 |
||
832 |
{ |
|
833 |
int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1); |
|
834 |
assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int"); |
|
835 |
int x = (int) vh.get(array, i); |
|
836 |
assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value"); |
|
837 |
} |
|
838 |
||
839 |
{ |
|
840 |
int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3); |
|
841 |
assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int"); |
|
842 |
int x = (int) vh.get(array, i); |
|
843 |
assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value"); |
|
844 |
} |
|
845 |
||
846 |
{ |
|
847 |
int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); |
|
848 |
assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int"); |
|
849 |
int x = (int) vh.get(array, i); |
|
850 |
assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value"); |
|
851 |
} |
|
852 |
||
853 |
{ |
|
854 |
int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); |
|
855 |
assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int"); |
|
856 |
int x = (int) vh.get(array, i); |
|
857 |
assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value"); |
|
858 |
} |
|
859 |
||
860 |
{ |
|
861 |
int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); |
|
862 |
assertEquals(r, VALUE_2, "success compareAndExchangeRelease int"); |
|
863 |
int x = (int) vh.get(array, i); |
|
864 |
assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value"); |
|
865 |
} |
|
866 |
||
867 |
{ |
|
868 |
int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); |
|
869 |
assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int"); |
|
870 |
int x = (int) vh.get(array, i); |
|
871 |
assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value"); |
|
872 |
} |
|
873 |
||
874 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
875 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
876 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
877 |
success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
878 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
879 |
assertEquals(success, true, "weakCompareAndSet int"); |
36934 | 880 |
int x = (int) vh.get(array, i); |
881 |
assertEquals(x, VALUE_2, "weakCompareAndSet int value"); |
|
882 |
} |
|
883 |
||
884 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
885 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
886 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
887 |
success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
888 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
889 |
assertEquals(success, true, "weakCompareAndSetAcquire int"); |
36934 | 890 |
int x = (int) vh.get(array, i); |
891 |
assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int"); |
|
892 |
} |
|
893 |
||
894 |
{ |
|
38355
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
895 |
boolean success = false; |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
896 |
for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
897 |
success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
898 |
} |
674cfd9b90cf
8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents:
37343
diff
changeset
|
899 |
assertEquals(success, true, "weakCompareAndSetRelease int"); |
36934 | 900 |
int x = (int) vh.get(array, i); |
901 |
assertEquals(x, VALUE_2, "weakCompareAndSetRelease int"); |
|
902 |
} |
|
903 |
||
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
904 |
{ |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
905 |
boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
906 |
assertEquals(r, true, "weakCompareAndSetVolatile int"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
907 |
int x = (int) vh.get(array, i); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
908 |
assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value"); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
909 |
} |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
910 |
|
36934 | 911 |
// Compare set and get |
912 |
{ |
|
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
913 |
int o = (int) vh.getAndSet(array, i, VALUE_2); |
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
914 |
assertEquals(o, VALUE_1, "getAndSet int"); |
36934 | 915 |
int x = (int) vh.get(array, i); |
37719
add11bc0e6e2
8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents:
37668
diff
changeset
|
916 |
assertEquals(x, VALUE_2, "getAndSet int value"); |
36934 | 917 |
} |
918 |
||
919 |
vh.set(array, i, VALUE_1); |
|
920 |
||
921 |
// get and add, add and get |
|
922 |
{ |
|
923 |
int o = (int) vh.getAndAdd(array, i, VALUE_3); |
|
924 |
assertEquals(o, VALUE_1, "getAndAdd int"); |
|
925 |
int c = (int) vh.addAndGet(array, i, VALUE_3); |
|
926 |
assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value"); |
|
927 |
} |
|
928 |
} |
|
929 |
} |
|
930 |
} |
|
931 |
||
932 |
static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { |
|
933 |
VarHandle vh = vhs.s; |
|
934 |
ByteBuffer array = bs.s; |
|
935 |
||
936 |
int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
|
937 |
||
938 |
ByteBuffer bb = ByteBuffer.allocate(SIZE); |
|
939 |
bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); |
|
940 |
bs.fill(bb.putInt(0, VALUE_2).array()); |
|
941 |
||
942 |
int length = array.limit() - SIZE + 1; |
|
943 |
for (int i = 0; i < length; i++) { |
|
944 |
boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
|
945 |
||
946 |
int v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) |
|
947 |
? rotateLeft(VALUE_2, (i % SIZE) << 3) |
|
948 |
: rotateRight(VALUE_2, (i % SIZE) << 3); |
|
949 |
// Plain |
|
950 |
{ |
|
951 |
int x = (int) vh.get(array, i); |
|
952 |
assertEquals(x, v, "get int value"); |
|
953 |
} |
|
954 |
||
955 |
if (iAligned) { |
|
956 |
// Volatile |
|
957 |
{ |
|
958 |
int x = (int) vh.getVolatile(array, i); |
|
959 |
assertEquals(x, v, "getVolatile int value"); |
|
960 |
} |
|
961 |
||
962 |
// Lazy |
|
963 |
{ |
|
964 |
int x = (int) vh.getAcquire(array, i); |
|
965 |
assertEquals(x, v, "getRelease int value"); |
|
966 |
} |
|
967 |
||
968 |
// Opaque |
|
969 |
{ |
|
970 |
int x = (int) vh.getOpaque(array, i); |
|
971 |
assertEquals(x, v, "getOpaque int value"); |
|
972 |
} |
|
973 |
} |
|
974 |
} |
|
975 |
} |
|
976 |
||
977 |
} |
|
978 |