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