|
1 /* |
|
2 * Copyright (c) 2015, 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 |
|
26 * @run testng/othervm -Diters=10 -Xint VarHandleTestAccess$Type$ |
|
27 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$ |
|
28 * @run testng/othervm -Diters=20000 VarHandleTestAccess$Type$ |
|
29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccess$Type$ |
|
30 */ |
|
31 |
|
32 import org.testng.annotations.BeforeClass; |
|
33 import org.testng.annotations.DataProvider; |
|
34 import org.testng.annotations.Test; |
|
35 |
|
36 import java.lang.invoke.MethodHandles; |
|
37 import java.lang.invoke.VarHandle; |
|
38 import java.util.ArrayList; |
|
39 import java.util.Arrays; |
|
40 import java.util.List; |
|
41 |
|
42 import static org.testng.Assert.*; |
|
43 |
|
44 public class VarHandleTestAccess$Type$ extends VarHandleBaseTest { |
|
45 static final $type$ static_final_v = $value1$; |
|
46 |
|
47 static $type$ static_v; |
|
48 |
|
49 final $type$ final_v = $value1$; |
|
50 |
|
51 $type$ v; |
|
52 |
|
53 VarHandle vhFinalField; |
|
54 |
|
55 VarHandle vhField; |
|
56 |
|
57 VarHandle vhStaticField; |
|
58 |
|
59 VarHandle vhStaticFinalField; |
|
60 |
|
61 VarHandle vhArray; |
|
62 |
|
63 @BeforeClass |
|
64 public void setup() throws Exception { |
|
65 vhFinalField = MethodHandles.lookup().findVarHandle( |
|
66 VarHandleTestAccess$Type$.class, "final_v", $type$.class); |
|
67 |
|
68 vhField = MethodHandles.lookup().findVarHandle( |
|
69 VarHandleTestAccess$Type$.class, "v", $type$.class); |
|
70 |
|
71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
|
72 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class); |
|
73 |
|
74 vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
|
75 VarHandleTestAccess$Type$.class, "static_v", $type$.class); |
|
76 |
|
77 vhArray = MethodHandles.arrayElementVarHandle($type$[].class); |
|
78 } |
|
79 |
|
80 |
|
81 @DataProvider |
|
82 public Object[][] varHandlesProvider() throws Exception { |
|
83 List<VarHandle> vhs = new ArrayList<>(); |
|
84 vhs.add(vhField); |
|
85 vhs.add(vhStaticField); |
|
86 vhs.add(vhArray); |
|
87 |
|
88 return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new); |
|
89 } |
|
90 |
|
91 @Test(dataProvider = "varHandlesProvider") |
|
92 public void testIsAccessModeSupported(VarHandle vh) { |
|
93 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.get)); |
|
94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.set)); |
|
95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.getVolatile)); |
|
96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.setVolatile)); |
|
97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.getAcquire)); |
|
98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.setRelease)); |
|
99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.getOpaque)); |
|
100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.setOpaque)); |
|
101 |
|
102 #if[CAS] |
|
103 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndSet)); |
|
104 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeVolatile)); |
|
105 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeAcquire)); |
|
106 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeRelease)); |
|
107 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSet)); |
|
108 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetAcquire)); |
|
109 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetRelease)); |
|
110 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetRelease)); |
|
111 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.getAndSet)); |
|
112 #else[CAS] |
|
113 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndSet)); |
|
114 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeVolatile)); |
|
115 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeAcquire)); |
|
116 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.compareAndExchangeRelease)); |
|
117 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSet)); |
|
118 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetAcquire)); |
|
119 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetRelease)); |
|
120 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.weakCompareAndSetRelease)); |
|
121 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.getAndSet)); |
|
122 #end[CAS] |
|
123 |
|
124 #if[AtomicAdd] |
|
125 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.getAndAdd)); |
|
126 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.addAndGet)); |
|
127 #else[AtomicAdd] |
|
128 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.getAndAdd)); |
|
129 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.addAndGet)); |
|
130 #end[AtomicAdd] |
|
131 } |
|
132 |
|
133 |
|
134 @DataProvider |
|
135 public Object[][] typesProvider() throws Exception { |
|
136 List<Object[]> types = new ArrayList<>(); |
|
137 types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccess$Type$.class)}); |
|
138 types.add(new Object[] {vhStaticField, Arrays.asList()}); |
|
139 types.add(new Object[] {vhArray, Arrays.asList($type$[].class, int.class)}); |
|
140 |
|
141 return types.stream().toArray(Object[][]::new); |
|
142 } |
|
143 |
|
144 @Test(dataProvider = "typesProvider") |
|
145 public void testTypes(VarHandle vh, List<Class<?>> pts) { |
|
146 assertEquals(vh.varType(), $type$.class); |
|
147 |
|
148 assertEquals(vh.coordinateTypes(), pts); |
|
149 |
|
150 testTypes(vh); |
|
151 } |
|
152 |
|
153 |
|
154 @Test |
|
155 public void testLookupInstanceToStatic() { |
|
156 checkIAE("Lookup of static final field to instance final field", () -> { |
|
157 MethodHandles.lookup().findStaticVarHandle( |
|
158 VarHandleTestAccess$Type$.class, "final_v", $type$.class); |
|
159 }); |
|
160 |
|
161 checkIAE("Lookup of static field to instance field", () -> { |
|
162 MethodHandles.lookup().findStaticVarHandle( |
|
163 VarHandleTestAccess$Type$.class, "v", $type$.class); |
|
164 }); |
|
165 } |
|
166 |
|
167 @Test |
|
168 public void testLookupStaticToInstance() { |
|
169 checkIAE("Lookup of instance final field to static final field", () -> { |
|
170 MethodHandles.lookup().findVarHandle( |
|
171 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class); |
|
172 }); |
|
173 |
|
174 checkIAE("Lookup of instance field to static field", () -> { |
|
175 vhStaticField = MethodHandles.lookup().findVarHandle( |
|
176 VarHandleTestAccess$Type$.class, "static_v", $type$.class); |
|
177 }); |
|
178 } |
|
179 |
|
180 |
|
181 @DataProvider |
|
182 public Object[][] accessTestCaseProvider() throws Exception { |
|
183 List<AccessTestCase<?>> cases = new ArrayList<>(); |
|
184 |
|
185 cases.add(new VarHandleAccessTestCase("Instance final field", |
|
186 vhFinalField, vh -> testInstanceFinalField(this, vh))); |
|
187 cases.add(new VarHandleAccessTestCase("Instance final field unsupported", |
|
188 vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh), |
|
189 false)); |
|
190 |
|
191 cases.add(new VarHandleAccessTestCase("Static final field", |
|
192 vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField)); |
|
193 cases.add(new VarHandleAccessTestCase("Static final field unsupported", |
|
194 vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported, |
|
195 false)); |
|
196 |
|
197 cases.add(new VarHandleAccessTestCase("Instance field", |
|
198 vhField, vh -> testInstanceField(this, vh))); |
|
199 cases.add(new VarHandleAccessTestCase("Instance field unsupported", |
|
200 vhField, vh -> testInstanceFieldUnsupported(this, vh), |
|
201 false)); |
|
202 |
|
203 cases.add(new VarHandleAccessTestCase("Static field", |
|
204 vhStaticField, VarHandleTestAccess$Type$::testStaticField)); |
|
205 cases.add(new VarHandleAccessTestCase("Static field unsupported", |
|
206 vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported, |
|
207 false)); |
|
208 |
|
209 cases.add(new VarHandleAccessTestCase("Array", |
|
210 vhArray, VarHandleTestAccess$Type$::testArray)); |
|
211 cases.add(new VarHandleAccessTestCase("Array unsupported", |
|
212 vhArray, VarHandleTestAccess$Type$::testArrayUnsupported, |
|
213 false)); |
|
214 cases.add(new VarHandleAccessTestCase("Array index out of bounds", |
|
215 vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds, |
|
216 false)); |
|
217 |
|
218 // Work around issue with jtreg summary reporting which truncates |
|
219 // the String result of Object.toString to 30 characters, hence |
|
220 // the first dummy argument |
|
221 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
|
222 } |
|
223 |
|
224 @Test(dataProvider = "accessTestCaseProvider") |
|
225 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
|
226 T t = atc.get(); |
|
227 int iters = atc.requiresLoop() ? ITERS : 1; |
|
228 for (int c = 0; c < iters; c++) { |
|
229 atc.testAccess(t); |
|
230 } |
|
231 } |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
237 // Plain |
|
238 { |
|
239 $type$ x = ($type$) vh.get(recv); |
|
240 assertEquals(x, $value1$, "get $type$ value"); |
|
241 } |
|
242 |
|
243 |
|
244 // Volatile |
|
245 { |
|
246 $type$ x = ($type$) vh.getVolatile(recv); |
|
247 assertEquals(x, $value1$, "getVolatile $type$ value"); |
|
248 } |
|
249 |
|
250 // Lazy |
|
251 { |
|
252 $type$ x = ($type$) vh.getAcquire(recv); |
|
253 assertEquals(x, $value1$, "getRelease $type$ value"); |
|
254 } |
|
255 |
|
256 // Opaque |
|
257 { |
|
258 $type$ x = ($type$) vh.getOpaque(recv); |
|
259 assertEquals(x, $value1$, "getOpaque $type$ value"); |
|
260 } |
|
261 } |
|
262 |
|
263 static void testInstanceFinalFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
264 checkUOE(() -> { |
|
265 vh.set(recv, $value2$); |
|
266 }); |
|
267 |
|
268 checkUOE(() -> { |
|
269 vh.setVolatile(recv, $value2$); |
|
270 }); |
|
271 |
|
272 checkUOE(() -> { |
|
273 vh.setRelease(recv, $value2$); |
|
274 }); |
|
275 |
|
276 checkUOE(() -> { |
|
277 vh.setOpaque(recv, $value2$); |
|
278 }); |
|
279 |
|
280 #if[!CAS] |
|
281 checkUOE(() -> { |
|
282 boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
283 }); |
|
284 |
|
285 checkUOE(() -> { |
|
286 $type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value1$, $value2$); |
|
287 }); |
|
288 |
|
289 checkUOE(() -> { |
|
290 $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
291 }); |
|
292 |
|
293 checkUOE(() -> { |
|
294 $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$); |
|
295 }); |
|
296 |
|
297 checkUOE(() -> { |
|
298 boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
299 }); |
|
300 |
|
301 checkUOE(() -> { |
|
302 boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$); |
|
303 }); |
|
304 |
|
305 checkUOE(() -> { |
|
306 boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
307 }); |
|
308 #end[CAS] |
|
309 |
|
310 #if[!AtomicAdd] |
|
311 checkUOE(() -> { |
|
312 $type$ o = ($type$) vh.getAndAdd(recv, $value1$); |
|
313 }); |
|
314 |
|
315 checkUOE(() -> { |
|
316 $type$ o = ($type$) vh.addAndGet(recv, $value1$); |
|
317 }); |
|
318 #end[AtomicAdd] |
|
319 } |
|
320 |
|
321 |
|
322 static void testStaticFinalField(VarHandle vh) { |
|
323 // Plain |
|
324 { |
|
325 $type$ x = ($type$) vh.get(); |
|
326 assertEquals(x, $value1$, "get $type$ value"); |
|
327 } |
|
328 |
|
329 |
|
330 // Volatile |
|
331 { |
|
332 $type$ x = ($type$) vh.getVolatile(); |
|
333 assertEquals(x, $value1$, "getVolatile $type$ value"); |
|
334 } |
|
335 |
|
336 // Lazy |
|
337 { |
|
338 $type$ x = ($type$) vh.getAcquire(); |
|
339 assertEquals(x, $value1$, "getRelease $type$ value"); |
|
340 } |
|
341 |
|
342 // Opaque |
|
343 { |
|
344 $type$ x = ($type$) vh.getOpaque(); |
|
345 assertEquals(x, $value1$, "getOpaque $type$ value"); |
|
346 } |
|
347 } |
|
348 |
|
349 static void testStaticFinalFieldUnsupported(VarHandle vh) { |
|
350 checkUOE(() -> { |
|
351 vh.set($value2$); |
|
352 }); |
|
353 |
|
354 checkUOE(() -> { |
|
355 vh.setVolatile($value2$); |
|
356 }); |
|
357 |
|
358 checkUOE(() -> { |
|
359 vh.setRelease($value2$); |
|
360 }); |
|
361 |
|
362 checkUOE(() -> { |
|
363 vh.setOpaque($value2$); |
|
364 }); |
|
365 |
|
366 #if[!CAS] |
|
367 checkUOE(() -> { |
|
368 boolean r = vh.compareAndSet($value1$, $value2$); |
|
369 }); |
|
370 |
|
371 checkUOE(() -> { |
|
372 $type$ r = ($type$) vh.compareAndExchangeVolatile($value1$, $value2$); |
|
373 }); |
|
374 |
|
375 checkUOE(() -> { |
|
376 $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
377 }); |
|
378 |
|
379 checkUOE(() -> { |
|
380 $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$); |
|
381 }); |
|
382 |
|
383 checkUOE(() -> { |
|
384 boolean r = vh.weakCompareAndSet($value1$, $value2$); |
|
385 }); |
|
386 |
|
387 checkUOE(() -> { |
|
388 boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$); |
|
389 }); |
|
390 |
|
391 checkUOE(() -> { |
|
392 boolean r = vh.weakCompareAndSetRelease($value1$, $value2$); |
|
393 }); |
|
394 #end[CAS] |
|
395 |
|
396 #if[!AtomicAdd] |
|
397 checkUOE(() -> { |
|
398 $type$ o = ($type$) vh.getAndAdd($value1$); |
|
399 }); |
|
400 |
|
401 checkUOE(() -> { |
|
402 $type$ o = ($type$) vh.addAndGet($value1$); |
|
403 }); |
|
404 #end[AtomicAdd] |
|
405 } |
|
406 |
|
407 |
|
408 static void testInstanceField(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
409 // Plain |
|
410 { |
|
411 vh.set(recv, $value1$); |
|
412 $type$ x = ($type$) vh.get(recv); |
|
413 assertEquals(x, $value1$, "set $type$ value"); |
|
414 } |
|
415 |
|
416 |
|
417 // Volatile |
|
418 { |
|
419 vh.setVolatile(recv, $value2$); |
|
420 $type$ x = ($type$) vh.getVolatile(recv); |
|
421 assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
422 } |
|
423 |
|
424 // Lazy |
|
425 { |
|
426 vh.setRelease(recv, $value1$); |
|
427 $type$ x = ($type$) vh.getAcquire(recv); |
|
428 assertEquals(x, $value1$, "setRelease $type$ value"); |
|
429 } |
|
430 |
|
431 // Opaque |
|
432 { |
|
433 vh.setOpaque(recv, $value2$); |
|
434 $type$ x = ($type$) vh.getOpaque(recv); |
|
435 assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
436 } |
|
437 |
|
438 #if[CAS] |
|
439 vh.set(recv, $value1$); |
|
440 |
|
441 // Compare |
|
442 { |
|
443 boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
444 assertEquals(r, true, "success compareAndSet $type$"); |
|
445 $type$ x = ($type$) vh.get(recv); |
|
446 assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
447 } |
|
448 |
|
449 { |
|
450 boolean r = vh.compareAndSet(recv, $value1$, $value3$); |
|
451 assertEquals(r, false, "failing compareAndSet $type$"); |
|
452 $type$ x = ($type$) vh.get(recv); |
|
453 assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
454 } |
|
455 |
|
456 { |
|
457 $type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value2$, $value1$); |
|
458 assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
459 $type$ x = ($type$) vh.get(recv); |
|
460 assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
461 } |
|
462 |
|
463 { |
|
464 $type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value2$, $value3$); |
|
465 assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
466 $type$ x = ($type$) vh.get(recv); |
|
467 assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
468 } |
|
469 |
|
470 { |
|
471 $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
472 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
473 $type$ x = ($type$) vh.get(recv); |
|
474 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
475 } |
|
476 |
|
477 { |
|
478 $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value3$); |
|
479 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
480 $type$ x = ($type$) vh.get(recv); |
|
481 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
482 } |
|
483 |
|
484 { |
|
485 $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value1$); |
|
486 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
487 $type$ x = ($type$) vh.get(recv); |
|
488 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
489 } |
|
490 |
|
491 { |
|
492 $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value3$); |
|
493 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
494 $type$ x = ($type$) vh.get(recv); |
|
495 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
496 } |
|
497 |
|
498 { |
|
499 boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
500 assertEquals(r, true, "weakCompareAndSet $type$"); |
|
501 $type$ x = ($type$) vh.get(recv); |
|
502 assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
503 } |
|
504 |
|
505 { |
|
506 boolean r = vh.weakCompareAndSetAcquire(recv, $value2$, $value1$); |
|
507 assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
508 $type$ x = ($type$) vh.get(recv); |
|
509 assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
510 } |
|
511 |
|
512 { |
|
513 boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
514 assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
515 $type$ x = ($type$) vh.get(recv); |
|
516 assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
517 } |
|
518 |
|
519 // Compare set and get |
|
520 { |
|
521 $type$ o = ($type$) vh.getAndSet(recv, $value1$); |
|
522 assertEquals(o, $value2$, "getAndSet $type$"); |
|
523 $type$ x = ($type$) vh.get(recv); |
|
524 assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
525 } |
|
526 #end[CAS] |
|
527 |
|
528 #if[AtomicAdd] |
|
529 vh.set(recv, $value1$); |
|
530 |
|
531 // get and add, add and get |
|
532 { |
|
533 $type$ o = ($type$) vh.getAndAdd(recv, $value3$); |
|
534 assertEquals(o, $value1$, "getAndAdd $type$"); |
|
535 $type$ c = ($type$) vh.addAndGet(recv, $value3$); |
|
536 assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
537 } |
|
538 #end[AtomicAdd] |
|
539 } |
|
540 |
|
541 static void testInstanceFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
542 #if[!CAS] |
|
543 checkUOE(() -> { |
|
544 boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
545 }); |
|
546 |
|
547 checkUOE(() -> { |
|
548 $type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value1$, $value2$); |
|
549 }); |
|
550 |
|
551 checkUOE(() -> { |
|
552 $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
553 }); |
|
554 |
|
555 checkUOE(() -> { |
|
556 $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$); |
|
557 }); |
|
558 |
|
559 checkUOE(() -> { |
|
560 boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
561 }); |
|
562 |
|
563 checkUOE(() -> { |
|
564 boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$); |
|
565 }); |
|
566 |
|
567 checkUOE(() -> { |
|
568 boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
569 }); |
|
570 #end[CAS] |
|
571 |
|
572 #if[!AtomicAdd] |
|
573 checkUOE(() -> { |
|
574 $type$ o = ($type$) vh.getAndAdd(recv, $value1$); |
|
575 }); |
|
576 |
|
577 checkUOE(() -> { |
|
578 $type$ o = ($type$) vh.addAndGet(recv, $value1$); |
|
579 }); |
|
580 #end[AtomicAdd] |
|
581 } |
|
582 |
|
583 |
|
584 static void testStaticField(VarHandle vh) { |
|
585 // Plain |
|
586 { |
|
587 vh.set($value1$); |
|
588 $type$ x = ($type$) vh.get(); |
|
589 assertEquals(x, $value1$, "set $type$ value"); |
|
590 } |
|
591 |
|
592 |
|
593 // Volatile |
|
594 { |
|
595 vh.setVolatile($value2$); |
|
596 $type$ x = ($type$) vh.getVolatile(); |
|
597 assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
598 } |
|
599 |
|
600 // Lazy |
|
601 { |
|
602 vh.setRelease($value1$); |
|
603 $type$ x = ($type$) vh.getAcquire(); |
|
604 assertEquals(x, $value1$, "setRelease $type$ value"); |
|
605 } |
|
606 |
|
607 // Opaque |
|
608 { |
|
609 vh.setOpaque($value2$); |
|
610 $type$ x = ($type$) vh.getOpaque(); |
|
611 assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
612 } |
|
613 |
|
614 #if[CAS] |
|
615 vh.set($value1$); |
|
616 |
|
617 // Compare |
|
618 { |
|
619 boolean r = vh.compareAndSet($value1$, $value2$); |
|
620 assertEquals(r, true, "success compareAndSet $type$"); |
|
621 $type$ x = ($type$) vh.get(); |
|
622 assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
623 } |
|
624 |
|
625 { |
|
626 boolean r = vh.compareAndSet($value1$, $value3$); |
|
627 assertEquals(r, false, "failing compareAndSet $type$"); |
|
628 $type$ x = ($type$) vh.get(); |
|
629 assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
630 } |
|
631 |
|
632 { |
|
633 $type$ r = ($type$) vh.compareAndExchangeVolatile($value2$, $value1$); |
|
634 assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
635 $type$ x = ($type$) vh.get(); |
|
636 assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
637 } |
|
638 |
|
639 { |
|
640 $type$ r = ($type$) vh.compareAndExchangeVolatile($value2$, $value3$); |
|
641 assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
642 $type$ x = ($type$) vh.get(); |
|
643 assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
644 } |
|
645 |
|
646 { |
|
647 $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
648 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
649 $type$ x = ($type$) vh.get(); |
|
650 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
651 } |
|
652 |
|
653 { |
|
654 $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value3$); |
|
655 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
656 $type$ x = ($type$) vh.get(); |
|
657 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
658 } |
|
659 |
|
660 { |
|
661 $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value1$); |
|
662 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
663 $type$ x = ($type$) vh.get(); |
|
664 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
665 } |
|
666 |
|
667 { |
|
668 $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value3$); |
|
669 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
670 $type$ x = ($type$) vh.get(); |
|
671 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
672 } |
|
673 |
|
674 { |
|
675 boolean r = (boolean) vh.weakCompareAndSet($value1$, $value2$); |
|
676 assertEquals(r, true, "weakCompareAndSet $type$"); |
|
677 $type$ x = ($type$) vh.get(); |
|
678 assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
679 } |
|
680 |
|
681 { |
|
682 boolean r = (boolean) vh.weakCompareAndSetAcquire($value2$, $value1$); |
|
683 assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
684 $type$ x = ($type$) vh.get(); |
|
685 assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
686 } |
|
687 |
|
688 { |
|
689 boolean r = (boolean) vh.weakCompareAndSetRelease( $value1$, $value2$); |
|
690 assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
691 $type$ x = ($type$) vh.get(); |
|
692 assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
693 } |
|
694 |
|
695 // Compare set and get |
|
696 { |
|
697 $type$ o = ($type$) vh.getAndSet( $value1$); |
|
698 assertEquals(o, $value2$, "getAndSet $type$"); |
|
699 $type$ x = ($type$) vh.get(); |
|
700 assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
701 } |
|
702 #end[CAS] |
|
703 |
|
704 #if[AtomicAdd] |
|
705 vh.set($value1$); |
|
706 |
|
707 // get and add, add and get |
|
708 { |
|
709 $type$ o = ($type$) vh.getAndAdd( $value3$); |
|
710 assertEquals(o, $value1$, "getAndAdd $type$"); |
|
711 $type$ c = ($type$) vh.addAndGet($value3$); |
|
712 assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
713 } |
|
714 #end[AtomicAdd] |
|
715 } |
|
716 |
|
717 static void testStaticFieldUnsupported(VarHandle vh) { |
|
718 #if[!CAS] |
|
719 checkUOE(() -> { |
|
720 boolean r = vh.compareAndSet($value1$, $value2$); |
|
721 }); |
|
722 |
|
723 checkUOE(() -> { |
|
724 $type$ r = ($type$) vh.compareAndExchangeVolatile($value1$, $value2$); |
|
725 }); |
|
726 |
|
727 checkUOE(() -> { |
|
728 $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
729 }); |
|
730 |
|
731 checkUOE(() -> { |
|
732 $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$); |
|
733 }); |
|
734 |
|
735 checkUOE(() -> { |
|
736 boolean r = vh.weakCompareAndSet($value1$, $value2$); |
|
737 }); |
|
738 |
|
739 checkUOE(() -> { |
|
740 boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$); |
|
741 }); |
|
742 |
|
743 checkUOE(() -> { |
|
744 boolean r = vh.weakCompareAndSetRelease($value1$, $value2$); |
|
745 }); |
|
746 #end[CAS] |
|
747 |
|
748 #if[!AtomicAdd] |
|
749 checkUOE(() -> { |
|
750 $type$ o = ($type$) vh.getAndAdd($value1$); |
|
751 }); |
|
752 |
|
753 checkUOE(() -> { |
|
754 $type$ o = ($type$) vh.addAndGet($value1$); |
|
755 }); |
|
756 #end[AtomicAdd] |
|
757 } |
|
758 |
|
759 |
|
760 static void testArray(VarHandle vh) { |
|
761 $type$[] array = new $type$[10]; |
|
762 |
|
763 for (int i = 0; i < array.length; i++) { |
|
764 // Plain |
|
765 { |
|
766 vh.set(array, i, $value1$); |
|
767 $type$ x = ($type$) vh.get(array, i); |
|
768 assertEquals(x, $value1$, "get $type$ value"); |
|
769 } |
|
770 |
|
771 |
|
772 // Volatile |
|
773 { |
|
774 vh.setVolatile(array, i, $value2$); |
|
775 $type$ x = ($type$) vh.getVolatile(array, i); |
|
776 assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
777 } |
|
778 |
|
779 // Lazy |
|
780 { |
|
781 vh.setRelease(array, i, $value1$); |
|
782 $type$ x = ($type$) vh.getAcquire(array, i); |
|
783 assertEquals(x, $value1$, "setRelease $type$ value"); |
|
784 } |
|
785 |
|
786 // Opaque |
|
787 { |
|
788 vh.setOpaque(array, i, $value2$); |
|
789 $type$ x = ($type$) vh.getOpaque(array, i); |
|
790 assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
791 } |
|
792 |
|
793 #if[CAS] |
|
794 vh.set(array, i, $value1$); |
|
795 |
|
796 // Compare |
|
797 { |
|
798 boolean r = vh.compareAndSet(array, i, $value1$, $value2$); |
|
799 assertEquals(r, true, "success compareAndSet $type$"); |
|
800 $type$ x = ($type$) vh.get(array, i); |
|
801 assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
802 } |
|
803 |
|
804 { |
|
805 boolean r = vh.compareAndSet(array, i, $value1$, $value3$); |
|
806 assertEquals(r, false, "failing compareAndSet $type$"); |
|
807 $type$ x = ($type$) vh.get(array, i); |
|
808 assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
809 } |
|
810 |
|
811 { |
|
812 $type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value2$, $value1$); |
|
813 assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
814 $type$ x = ($type$) vh.get(array, i); |
|
815 assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
816 } |
|
817 |
|
818 { |
|
819 $type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value2$, $value3$); |
|
820 assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
821 $type$ x = ($type$) vh.get(array, i); |
|
822 assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
823 } |
|
824 |
|
825 { |
|
826 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$); |
|
827 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
828 $type$ x = ($type$) vh.get(array, i); |
|
829 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
830 } |
|
831 |
|
832 { |
|
833 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value3$); |
|
834 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
835 $type$ x = ($type$) vh.get(array, i); |
|
836 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
837 } |
|
838 |
|
839 { |
|
840 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value1$); |
|
841 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
842 $type$ x = ($type$) vh.get(array, i); |
|
843 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
844 } |
|
845 |
|
846 { |
|
847 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value3$); |
|
848 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
849 $type$ x = ($type$) vh.get(array, i); |
|
850 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
851 } |
|
852 |
|
853 { |
|
854 boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$); |
|
855 assertEquals(r, true, "weakCompareAndSet $type$"); |
|
856 $type$ x = ($type$) vh.get(array, i); |
|
857 assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
858 } |
|
859 |
|
860 { |
|
861 boolean r = vh.weakCompareAndSetAcquire(array, i, $value2$, $value1$); |
|
862 assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
863 $type$ x = ($type$) vh.get(array, i); |
|
864 assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
865 } |
|
866 |
|
867 { |
|
868 boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$); |
|
869 assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
870 $type$ x = ($type$) vh.get(array, i); |
|
871 assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
872 } |
|
873 |
|
874 // Compare set and get |
|
875 { |
|
876 $type$ o = ($type$) vh.getAndSet(array, i, $value1$); |
|
877 assertEquals(o, $value2$, "getAndSet $type$"); |
|
878 $type$ x = ($type$) vh.get(array, i); |
|
879 assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
880 } |
|
881 #end[CAS] |
|
882 |
|
883 #if[AtomicAdd] |
|
884 vh.set(array, i, $value1$); |
|
885 |
|
886 // get and add, add and get |
|
887 { |
|
888 $type$ o = ($type$) vh.getAndAdd(array, i, $value3$); |
|
889 assertEquals(o, $value1$, "getAndAdd $type$"); |
|
890 $type$ c = ($type$) vh.addAndGet(array, i, $value3$); |
|
891 assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
892 } |
|
893 #end[AtomicAdd] |
|
894 } |
|
895 } |
|
896 |
|
897 static void testArrayUnsupported(VarHandle vh) { |
|
898 $type$[] array = new $type$[10]; |
|
899 |
|
900 int i = 0; |
|
901 #if[!CAS] |
|
902 checkUOE(() -> { |
|
903 boolean r = vh.compareAndSet(array, i, $value1$, $value2$); |
|
904 }); |
|
905 |
|
906 checkUOE(() -> { |
|
907 $type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value1$, $value2$); |
|
908 }); |
|
909 |
|
910 checkUOE(() -> { |
|
911 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$); |
|
912 }); |
|
913 |
|
914 checkUOE(() -> { |
|
915 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value1$, $value2$); |
|
916 }); |
|
917 |
|
918 checkUOE(() -> { |
|
919 boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$); |
|
920 }); |
|
921 |
|
922 checkUOE(() -> { |
|
923 boolean r = vh.weakCompareAndSetAcquire(array, i, $value1$, $value2$); |
|
924 }); |
|
925 |
|
926 checkUOE(() -> { |
|
927 boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$); |
|
928 }); |
|
929 #end[CAS] |
|
930 |
|
931 #if[!AtomicAdd] |
|
932 checkUOE(() -> { |
|
933 $type$ o = ($type$) vh.getAndAdd(array, i, $value1$); |
|
934 }); |
|
935 |
|
936 checkUOE(() -> { |
|
937 $type$ o = ($type$) vh.addAndGet(array, i, $value1$); |
|
938 }); |
|
939 #end[AtomicAdd] |
|
940 } |
|
941 |
|
942 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { |
|
943 $type$[] array = new $type$[10]; |
|
944 |
|
945 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
|
946 final int ci = i; |
|
947 |
|
948 checkIOOBE(() -> { |
|
949 $type$ x = ($type$) vh.get(array, ci); |
|
950 }); |
|
951 |
|
952 checkIOOBE(() -> { |
|
953 vh.set(array, ci, $value1$); |
|
954 }); |
|
955 |
|
956 checkIOOBE(() -> { |
|
957 $type$ x = ($type$) vh.getVolatile(array, ci); |
|
958 }); |
|
959 |
|
960 checkIOOBE(() -> { |
|
961 vh.setVolatile(array, ci, $value1$); |
|
962 }); |
|
963 |
|
964 checkIOOBE(() -> { |
|
965 $type$ x = ($type$) vh.getAcquire(array, ci); |
|
966 }); |
|
967 |
|
968 checkIOOBE(() -> { |
|
969 vh.setRelease(array, ci, $value1$); |
|
970 }); |
|
971 |
|
972 checkIOOBE(() -> { |
|
973 $type$ x = ($type$) vh.getOpaque(array, ci); |
|
974 }); |
|
975 |
|
976 checkIOOBE(() -> { |
|
977 vh.setOpaque(array, ci, $value1$); |
|
978 }); |
|
979 |
|
980 #if[CAS] |
|
981 checkIOOBE(() -> { |
|
982 boolean r = vh.compareAndSet(array, ci, $value1$, $value2$); |
|
983 }); |
|
984 |
|
985 checkIOOBE(() -> { |
|
986 $type$ r = ($type$) vh.compareAndExchangeVolatile(array, ci, $value2$, $value1$); |
|
987 }); |
|
988 |
|
989 checkIOOBE(() -> { |
|
990 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, $value2$, $value1$); |
|
991 }); |
|
992 |
|
993 checkIOOBE(() -> { |
|
994 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, $value2$, $value1$); |
|
995 }); |
|
996 |
|
997 checkIOOBE(() -> { |
|
998 boolean r = vh.weakCompareAndSet(array, ci, $value1$, $value2$); |
|
999 }); |
|
1000 |
|
1001 checkIOOBE(() -> { |
|
1002 boolean r = vh.weakCompareAndSetAcquire(array, ci, $value1$, $value2$); |
|
1003 }); |
|
1004 |
|
1005 checkIOOBE(() -> { |
|
1006 boolean r = vh.weakCompareAndSetRelease(array, ci, $value1$, $value2$); |
|
1007 }); |
|
1008 |
|
1009 checkIOOBE(() -> { |
|
1010 $type$ o = ($type$) vh.getAndSet(array, ci, $value1$); |
|
1011 }); |
|
1012 #end[CAS] |
|
1013 |
|
1014 #if[AtomicAdd] |
|
1015 checkIOOBE(() -> { |
|
1016 $type$ o = ($type$) vh.getAndAdd(array, ci, $value3$); |
|
1017 }); |
|
1018 |
|
1019 checkIOOBE(() -> { |
|
1020 $type$ o = ($type$) vh.addAndGet(array, ci, $value3$); |
|
1021 }); |
|
1022 #end[AtomicAdd] |
|
1023 } |
|
1024 } |
|
1025 } |
|
1026 |