author | psandoz |
Wed, 13 Apr 2016 15:05:48 +0200 | |
changeset 37343 | 35a2231828a7 |
parent 36973 | 951bb58383a4 |
child 37344 | 52d3d8517efc |
permissions | -rw-r--r-- |
36934 | 1 |
/* |
2 |
* Copyright (c) 2014, 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.lang.invoke; |
|
27 |
||
28 |
import jdk.internal.HotSpotIntrinsicCandidate; |
|
29 |
import jdk.internal.vm.annotation.ForceInline; |
|
30 |
||
31 |
import java.lang.reflect.Method; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Arrays; |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
34 |
import java.util.HashMap; |
36934 | 35 |
import java.util.List; |
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
36 |
import java.util.Map; |
36934 | 37 |
import java.util.function.BiFunction; |
38 |
||
39 |
import static java.lang.invoke.MethodHandleStatics.UNSAFE; |
|
40 |
import static java.lang.invoke.MethodHandleStatics.newInternalError; |
|
41 |
||
42 |
/** |
|
43 |
* A VarHandle is a dynamically typed reference to a variable, or to a |
|
44 |
* parametrically-defined family of variables, including static fields, |
|
45 |
* non-static fields, array elements, or components of an off-heap data |
|
46 |
* structure. Access to such variables is supported under various |
|
47 |
* <em>access modes</em>, including plain read/write access, volatile |
|
48 |
* read/write access, and compare-and-swap. |
|
49 |
* |
|
50 |
* <p>VarHandles are immutable and have no visible state. VarHandles cannot be |
|
51 |
* subclassed by the user. |
|
52 |
* |
|
53 |
* <p>A VarHandle has: |
|
54 |
* <ul> |
|
55 |
* <li>a {@link #varType variable type}, referred to as {@code T}, which is the |
|
56 |
* type of variable(s) referenced by this VarHandle; |
|
57 |
* <li>a list of {@link #coordinateTypes coordinate types}, referred to as |
|
58 |
* {@code CT}, where the types (primitive and reference) are represented by |
|
59 |
* {@link Class} objects). A list of arguments corresponding to instances of |
|
60 |
* the coordinate types uniquely locates a variable referenced by this |
|
61 |
* VarHandle; and |
|
62 |
* <li>a <em>shape</em>, that combines the variable type and coordinate types, |
|
63 |
* and is declared with the notation {@code (CT : T)}. An empty list of |
|
64 |
* coordinate types is declared as {@code (empty)}. |
|
65 |
* </ul> |
|
66 |
* |
|
67 |
* <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup |
|
68 |
* lookup} VarHandle instances document the supported variable type, coordinate |
|
69 |
* types, and shape. |
|
70 |
* |
|
71 |
* For example, a VarHandle referencing a non-static field will declare a shape |
|
72 |
* of {@code (R : T)}, where {@code R} is the receiver type and |
|
73 |
* {@code T} is the field type, and where the VarHandle and an instance of the |
|
74 |
* receiver type can be utilized to access the field variable. |
|
75 |
* A VarHandle referencing array elements will declare a shape of |
|
76 |
* {@code (T[], int : T)}, where {@code T[]} is the array type and {@code T} |
|
77 |
* its component type, and where the VarHandle, an instance of the array type, |
|
78 |
* and an {@code int} index can be utilized to access an array element variable. |
|
79 |
* |
|
80 |
* <p>Each access mode is associated with a |
|
81 |
* <a href="MethodHandle.html#sigpoly">signature polymorphic</a> method of the |
|
82 |
* same name, where the VarHandle shape and access mode uniquely determine the |
|
83 |
* canonical {@link #accessModeType(AccessMode) access mode type}, |
|
84 |
* which in turn determines the matching constraints on a valid symbolic |
|
85 |
* type descriptor at the call site of an access mode's method |
|
86 |
* <a href="VarHandle.html#invoke">invocation</a>. |
|
87 |
* |
|
88 |
* As such, VarHandles are dynamically and strongly typed. Their arity, |
|
89 |
* argument types, and return type of an access mode method invocation are not |
|
90 |
* statically checked. If they, and associated values, do not match the arity |
|
91 |
* and types of the access mode's type, an exception will be thrown. |
|
92 |
* |
|
93 |
* The parameter types of an access mode method type will consist of those that |
|
94 |
* are the VarHandles's coordinate types (in order), followed by access mode |
|
95 |
* parameter types specific to the access mode. |
|
96 |
* |
|
97 |
* <p>An access mode's method documents the form of its method signature, which |
|
98 |
* is derived from the access mode parameter types. The form is declared with |
|
99 |
* the notation {@code (CT, P1 p1, P2 p2, ..., PN pn)R}, where {@code CT} is the |
|
100 |
* coordinate types (as documented by a VarHandle factory method), {@code P1}, |
|
101 |
* {@code P2} and {@code PN} are the first, second and the n'th access mode |
|
102 |
* parameters named {@code p1}, {@code p2} and {@code pn} respectively, and |
|
103 |
* {@code R} is the return type. |
|
104 |
* |
|
105 |
* For example, for the generic shape of {@code (CT : T)} the |
|
106 |
* {@link #compareAndSet} access mode method documents that its method |
|
107 |
* signature is of the form {@code (CT, T expectedValue, T newValue)boolean}, |
|
108 |
* where the parameter types named {@code extendedValue} and {@code newValue} |
|
109 |
* are the access mode parameter types. If the VarHandle accesses array |
|
110 |
* elements with a shape of say {@code (T[], int : T)} then the access mode |
|
111 |
* method type is {@code (T[], int, T, T)boolean}. |
|
112 |
* |
|
113 |
* <p>Access modes are grouped into the following categories: |
|
114 |
* <ul> |
|
115 |
* <li>read access modes that get the value of a variable under specified |
|
116 |
* memory ordering effects. |
|
117 |
* The set of corresponding access mode methods belonging to this group |
|
118 |
* consists of the methods |
|
119 |
* {@link #get get}, |
|
120 |
* {@link #getVolatile getVolatile}, |
|
121 |
* {@link #getAcquire getAcquire}, |
|
122 |
* {@link #getOpaque getOpaque}. |
|
123 |
* <li>write access modes that set the value of a variable under specified |
|
124 |
* memory ordering effects. |
|
125 |
* The set of corresponding access mode methods belonging to this group |
|
126 |
* consists of the methods |
|
127 |
* {@link #set set}, |
|
128 |
* {@link #setVolatile setVolatile}, |
|
129 |
* {@link #setRelease setRelease}, |
|
130 |
* {@link #setOpaque setOpaque}. |
|
131 |
* <li>atomic update access modes that, for example, atomically compare and set |
|
132 |
* the value of a variable under specified memory ordering effects. |
|
133 |
* The set of corresponding access mode methods belonging to this group |
|
134 |
* consists of the methods |
|
135 |
* {@link #compareAndSet compareAndSet}, |
|
136 |
* {@link #weakCompareAndSet weakCompareAndSet}, |
|
137 |
* {@link #weakCompareAndSetAcquire weakCompareAndSetAcquire}, |
|
138 |
* {@link #weakCompareAndSetRelease weakCompareAndSetRelease}, |
|
139 |
* {@link #compareAndExchangeAcquire compareAndExchangeAcquire}, |
|
140 |
* {@link #compareAndExchangeVolatile compareAndExchangeVolatile}, |
|
141 |
* {@link #compareAndExchangeRelease compareAndExchangeRelease}, |
|
142 |
* {@link #getAndSet getAndSet}. |
|
143 |
* <li>numeric atomic update access modes that, for example, atomically get and |
|
144 |
* set with addition the value of a variable under specified memory ordering |
|
145 |
* effects. |
|
146 |
* The set of corresponding access mode methods belonging to this group |
|
147 |
* consists of the methods |
|
148 |
* {@link #getAndAdd getAndAdd}, |
|
149 |
* {@link #addAndGet addAndGet}. |
|
150 |
* </ul> |
|
151 |
* |
|
152 |
* <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup |
|
153 |
* lookup} VarHandle instances document the set of access modes that are |
|
154 |
* supported, which may also include documenting restrictions based on the |
|
155 |
* variable type and whether a variable is read-only. If an access mode is not |
|
156 |
* supported then the corresponding signature-polymorphic method will on |
|
157 |
* invocation throw an {@code UnsupportedOperationException}. |
|
158 |
* The {@link #get get} access mode is supported for all |
|
159 |
* VarHandle instances and the corresponding method never throws |
|
160 |
* {@code UnsupportedOperationException}. |
|
161 |
* If a VarHandle references a read-only variable (for example a {@code final} |
|
162 |
* field) then write, atomic update and numeric atomic update access modes are |
|
163 |
* not supported and corresponding methods throw |
|
164 |
* {@code UnsupportedOperationException}. |
|
165 |
* Read/write access modes (if supported), with the exception of |
|
166 |
* {@code get} and {@code set}, provide atomic access for |
|
167 |
* reference types and all primitive types. |
|
168 |
* Unless stated otherwise in the documentation of a factory method, the access |
|
169 |
* modes {@code get} and {@code set} (if supported) provide atomic access for |
|
170 |
* reference types and all primitives types, with the exception of {@code long} |
|
171 |
* and {@code double} on 32-bit platforms |
|
172 |
* |
|
173 |
* <p>Access modes will override any memory ordering effects specified at |
|
174 |
* the declaration site of a variable. For example, a VarHandle accessing a |
|
175 |
* a field using the {@code get} access mode will access the field as |
|
176 |
* specified <em>by its access mode</em> even if that field is declared |
|
177 |
* {@code volatile}. When mixed access is performed extreme care should be |
|
178 |
* taken since the Java Memory Model may permit surprising results. |
|
179 |
* |
|
180 |
* <p>In addition to supporting access to variables under various access modes, |
|
181 |
* a set of static methods, referred to as memory fence methods, is also |
|
182 |
* provided for fine-grained control of memory ordering. |
|
183 |
* |
|
184 |
* The Java Language Specification permits other threads to observe operations |
|
185 |
* as if they were executed in orders different than are apparent in program |
|
186 |
* source code, subject to constraints arising, for example, from the use of |
|
187 |
* locks, {@code volatile} fields or VarHandles. The static methods, |
|
188 |
* {@link #fullFence fullFence}, {@link #acquireFence acquireFence}, |
|
189 |
* {@link #releaseFence releaseFence}, {@link #loadLoadFence loadLoadFence} and |
|
190 |
* {@link #storeStoreFence storeStoreFence}, can also be used to impose |
|
191 |
* constraints. Their specifications, as is the case for certain access modes, |
|
192 |
* are phrased in terms of the lack of "reorderings" -- observable ordering |
|
193 |
* effects that might otherwise occur if the fence was not present. More |
|
194 |
* precise phrasing of the specification of access mode methods and memory fence |
|
195 |
* methods may accompany future updates of the Java Language Specification. |
|
196 |
* |
|
197 |
* <h1>Compilation of an access mode's method</h1> |
|
198 |
* A Java method call expression naming an access mode method can invoke a |
|
199 |
* VarHandle from Java source code. From the viewpoint of source code, these |
|
200 |
* methods can take any arguments and their polymorphic result (if expressed) |
|
201 |
* can be cast to any return type. Formally this is accomplished by giving the |
|
202 |
* access mode methods variable arity {@code Object} arguments and |
|
203 |
* {@code Object} return types (if the return type is polymorphic), but they |
|
204 |
* have an additional quality called <em>signature polymorphism</em> which |
|
205 |
* connects this freedom of invocation directly to the JVM execution stack. |
|
206 |
* <p> |
|
207 |
* As is usual with virtual methods, source-level calls to access mode methods |
|
208 |
* compile to an {@code invokevirtual} instruction. More unusually, the |
|
209 |
* compiler must record the actual argument types, and may not perform method |
|
210 |
* invocation conversions on the arguments. Instead, it must generate |
|
211 |
* instructions to push them on the stack according to their own unconverted |
|
212 |
* types. The VarHandle object itself will be pushed on the stack before the |
|
213 |
* arguments. The compiler then generates an {@code invokevirtual} instruction |
|
214 |
* that invokes the access mode method with a symbolic type descriptor which |
|
215 |
* describes the argument and return types. |
|
216 |
* <p> |
|
217 |
* To issue a complete symbolic type descriptor, the compiler must also |
|
218 |
* determine the return type (if polymorphic). This is based on a cast on the |
|
219 |
* method invocation expression, if there is one, or else {@code Object} if the |
|
220 |
* invocation is an expression, or else {@code void} if the invocation is a |
|
221 |
* statement. The cast may be to a primitive type (but not {@code void}). |
|
222 |
* <p> |
|
223 |
* As a corner case, an uncasted {@code null} argument is given a symbolic type |
|
224 |
* descriptor of {@code java.lang.Void}. The ambiguity with the type |
|
225 |
* {@code Void} is harmless, since there are no references of type {@code Void} |
|
226 |
* except the null reference. |
|
227 |
* |
|
228 |
* |
|
229 |
* <h1><a name="invoke">Invocation of an access mode's method</a></h1> |
|
230 |
* The first time an {@code invokevirtual} instruction is executed it is linked |
|
231 |
* by symbolically resolving the names in the instruction and verifying that |
|
232 |
* the method call is statically legal. This also holds for calls to access mode |
|
233 |
* methods. In this case, the symbolic type descriptor emitted by the compiler |
|
234 |
* is checked for correct syntax, and names it contains are resolved. Thus, an |
|
235 |
* {@code invokevirtual} instruction which invokes an access mode method will |
|
236 |
* always link, as long as the symbolic type descriptor is syntactically |
|
237 |
* well-formed and the types exist. |
|
238 |
* <p> |
|
239 |
* When the {@code invokevirtual} is executed after linking, the receiving |
|
240 |
* VarHandle's access mode type is first checked by the JVM to ensure that it |
|
241 |
* matches the symbolic type descriptor. If the type |
|
242 |
* match fails, it means that the access mode method which the caller is |
|
243 |
* invoking is not present on the individual VarHandle being invoked. |
|
244 |
* |
|
245 |
* <p> |
|
246 |
* Invocation of an access mode's signature-polymorphic method behaves as if an |
|
247 |
* invocation of {@link MethodHandle#invoke}, where the receiving method handle |
|
248 |
* is bound to a VarHandle instance and the access mode. More specifically, the |
|
249 |
* following: |
|
250 |
* <pre> {@code |
|
251 |
* VarHandle vh = .. |
|
252 |
* R r = (R) vh.{access-mode}(p1, p2, ..., pN); |
|
253 |
* }</pre> |
|
254 |
* behaves as if (modulo the access mode methods do not declare throwing of |
|
255 |
* {@code Throwable}): |
|
256 |
* <pre> {@code |
|
257 |
* VarHandle vh = .. |
|
258 |
* MethodHandle mh = MethodHandles.varHandleExactInvoker( |
|
259 |
* VarHandle.AccessMode.{access-mode}, |
|
260 |
* vh.accessModeType(VarHandle.AccessMode.{access-mode})); |
|
261 |
* |
|
262 |
* mh = mh.bindTo(vh); |
|
263 |
* R r = (R) mh.invoke(p1, p2, ..., pN) |
|
264 |
* }</pre> |
|
265 |
* or, more concisely, behaves as if: |
|
266 |
* <pre> {@code |
|
267 |
* VarHandle vh = .. |
|
268 |
* MethodHandle mh = vh.toMethodHandle(VarHandle.AccessMode.{access-mode}); |
|
269 |
* |
|
270 |
* R r = (R) mh.invoke(p1, p2, ..., pN) |
|
271 |
* }</pre> |
|
272 |
* In terms of equivalent {@code invokevirtual} bytecode behaviour an access |
|
273 |
* mode method invocation is equivalent to: |
|
274 |
* <pre> {@code |
|
275 |
* MethodHandle mh = MethodHandles.lookup().findVirtual( |
|
276 |
* VarHandle.class, |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
277 |
* VarHandle.AccessMode.{access-mode}.methodName(), |
36934 | 278 |
* MethodType.methodType(R, p1, p2, ..., pN)); |
279 |
* |
|
280 |
* R r = (R) mh.invokeExact(vh, p1, p2, ..., pN) |
|
281 |
* }</pre> |
|
282 |
* where the desired method type is the symbolic type descriptor and a |
|
283 |
* {@link MethodHandle#invokeExact} is performed, since before invocation of the |
|
284 |
* target, the handle will apply reference casts as necessary and box, unbox, or |
|
285 |
* widen primitive values, as if by {@link MethodHandle#asType asType} (see also |
|
286 |
* {@link MethodHandles#varHandleInvoker}). |
|
287 |
* |
|
288 |
* <h1>Invocation checking</h1> |
|
289 |
* In typical programs, VarHandle access mode type matching will usually |
|
290 |
* succeed. But if a match fails, the JVM will throw a |
|
291 |
* {@link WrongMethodTypeException}. |
|
292 |
* <p> |
|
293 |
* Thus, an access mode type mismatch which might show up as a linkage error |
|
294 |
* in a statically typed program can show up as a dynamic |
|
295 |
* {@code WrongMethodTypeException} in a program which uses VarHandles. |
|
296 |
* <p> |
|
297 |
* Because access mode types contain "live" {@code Class} objects, method type |
|
298 |
* matching takes into account both type names and class loaders. |
|
299 |
* Thus, even if a VarHandle {@code VH} is created in one class loader |
|
300 |
* {@code L1} and used in another {@code L2}, VarHandle access mode method |
|
301 |
* calls are type-safe, because the caller's symbolic type descriptor, as |
|
302 |
* resolved in {@code L2}, is matched against the original callee method's |
|
303 |
* symbolic type descriptor, as resolved in {@code L1}. The resolution in |
|
304 |
* {@code L1} happens when {@code VH} is created and its access mode types are |
|
305 |
* assigned, while the resolution in {@code L2} happens when the |
|
306 |
* {@code invokevirtual} instruction is linked. |
|
307 |
* <p> |
|
308 |
* Apart from type descriptor checks, a VarHandles's capability to |
|
309 |
* access it's variables is unrestricted. |
|
310 |
* If a VarHandle is formed on a non-public variable by a class that has access |
|
311 |
* to that variable, the resulting VarHandle can be used in any place by any |
|
312 |
* caller who receives a reference to it. |
|
313 |
* <p> |
|
314 |
* Unlike with the Core Reflection API, where access is checked every time a |
|
315 |
* reflective method is invoked, VarHandle access checking is performed |
|
316 |
* <a href="MethodHandles.Lookup.html#access">when the VarHandle is |
|
317 |
* created</a>. |
|
318 |
* Thus, VarHandles to non-public variables, or to variables in non-public |
|
319 |
* classes, should generally be kept secret. They should not be passed to |
|
320 |
* untrusted code unless their use from the untrusted code would be harmless. |
|
321 |
* |
|
322 |
* |
|
323 |
* <h1>VarHandle creation</h1> |
|
324 |
* Java code can create a VarHandle that directly accesses any field that is |
|
325 |
* accessible to that code. This is done via a reflective, capability-based |
|
326 |
* API called {@link java.lang.invoke.MethodHandles.Lookup |
|
327 |
* MethodHandles.Lookup}. |
|
328 |
* For example, a VarHandle for a non-static field can be obtained |
|
329 |
* from {@link java.lang.invoke.MethodHandles.Lookup#findVarHandle |
|
330 |
* Lookup.findVarHandle}. |
|
331 |
* There is also a conversion method from Core Reflection API objects, |
|
332 |
* {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle |
|
333 |
* Lookup.unreflectVarHandle}. |
|
334 |
* <p> |
|
335 |
* Access to protected field members is restricted to receivers only of the |
|
336 |
* accessing class, or one of its subclasses, and the accessing class must in |
|
337 |
* turn be a subclass (or package sibling) of the protected member's defining |
|
338 |
* class. If a VarHandle refers to a protected non-static field of a declaring |
|
339 |
* class outside the current package, the receiver argument will be narrowed to |
|
340 |
* the type of the accessing class. |
|
341 |
* |
|
342 |
* <h1>Interoperation between VarHandles and the Core Reflection API</h1> |
|
343 |
* Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup |
|
344 |
* Lookup} API, any field represented by a Core Reflection API object |
|
345 |
* can be converted to a behaviorally equivalent VarHandle. |
|
346 |
* For example, a reflective {@link java.lang.reflect.Field Field} can |
|
347 |
* be converted to a VarHandle using |
|
348 |
* {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle |
|
349 |
* Lookup.unreflectVarHandle}. |
|
350 |
* The resulting VarHandles generally provide more direct and efficient |
|
351 |
* access to the underlying fields. |
|
352 |
* <p> |
|
353 |
* As a special case, when the Core Reflection API is used to view the |
|
354 |
* signature polymorphic access mode methods in this class, they appear as |
|
355 |
* ordinary non-polymorphic methods. Their reflective appearance, as viewed by |
|
356 |
* {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod}, |
|
357 |
* is unaffected by their special status in this API. |
|
358 |
* For example, {@link java.lang.reflect.Method#getModifiers |
|
359 |
* Method.getModifiers} |
|
360 |
* will report exactly those modifier bits required for any similarly |
|
361 |
* declared method, including in this case {@code native} and {@code varargs} |
|
362 |
* bits. |
|
363 |
* <p> |
|
364 |
* As with any reflected method, these methods (when reflected) may be invoked |
|
365 |
* directly via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, |
|
366 |
* via JNI, or indirectly via |
|
367 |
* {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}. |
|
368 |
* However, such reflective calls do not result in access mode method |
|
369 |
* invocations. Such a call, if passed the required argument (a single one, of |
|
370 |
* type {@code Object[]}), will ignore the argument and will throw an |
|
371 |
* {@code UnsupportedOperationException}. |
|
372 |
* <p> |
|
373 |
* Since {@code invokevirtual} instructions can natively invoke VarHandle |
|
374 |
* access mode methods under any symbolic type descriptor, this reflective view |
|
375 |
* conflicts with the normal presentation of these methods via bytecodes. |
|
376 |
* Thus, these native methods, when reflectively viewed by |
|
377 |
* {@code Class.getDeclaredMethod}, may be regarded as placeholders only. |
|
378 |
* <p> |
|
379 |
* In order to obtain an invoker method for a particular access mode type, |
|
380 |
* use {@link java.lang.invoke.MethodHandles#varHandleExactInvoker} or |
|
381 |
* {@link java.lang.invoke.MethodHandles#varHandleInvoker}. The |
|
382 |
* {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual} |
|
383 |
* API is also able to return a method handle to call an access mode method for |
|
384 |
* any specified access mode type and is equivalent in behaviour to |
|
385 |
* {@link java.lang.invoke.MethodHandles#varHandleInvoker}. |
|
386 |
* |
|
387 |
* <h1>Interoperation between VarHandles and Java generics</h1> |
|
388 |
* A VarHandle can be obtained for a variable, such as a a field, which is |
|
389 |
* declared with Java generic types. As with the Core Reflection API, the |
|
390 |
* VarHandle's variable type will be constructed from the erasure of the |
|
391 |
* source-level type. When a VarHandle access mode method is invoked, the |
|
392 |
* types |
|
393 |
* of its arguments or the return value cast type may be generic types or type |
|
394 |
* instances. If this occurs, the compiler will replace those types by their |
|
395 |
* erasures when it constructs the symbolic type descriptor for the |
|
396 |
* {@code invokevirtual} instruction. |
|
397 |
* |
|
398 |
* @see MethodHandle |
|
399 |
* @see MethodHandles |
|
400 |
* @see MethodType |
|
401 |
* @since 9 |
|
402 |
*/ |
|
403 |
public abstract class VarHandle { |
|
404 |
// Use explicit final fields rather than an @Stable array as |
|
405 |
// this can reduce the memory per handle |
|
406 |
// e.g. by 24 bytes on 64 bit architectures |
|
407 |
final MethodType typeGet; |
|
408 |
final MethodType typeSet; |
|
409 |
final MethodType typeCompareSwap; |
|
410 |
final MethodType typeCompareExchange; |
|
411 |
final MethodType typeGetAndUpdate; |
|
412 |
||
413 |
final VarForm vform; |
|
414 |
||
415 |
VarHandle(VarForm vform, Class<?> receiver, Class<?> value, Class<?>... intermediate) { |
|
416 |
this.vform = vform; |
|
417 |
||
418 |
// (Receiver, <Intermediates>) |
|
419 |
List<Class<?>> l = new ArrayList<>(); |
|
420 |
if (receiver != null) |
|
421 |
l.add(receiver); |
|
422 |
l.addAll(Arrays.asList(intermediate)); |
|
423 |
||
424 |
// (Receiver, <Intermediates>)Value |
|
425 |
this.typeGet = MethodType.methodType(value, l); |
|
426 |
||
427 |
// (Receiver, <Intermediates>, Value)void |
|
428 |
l.add(value); |
|
429 |
this.typeSet = MethodType.methodType(void.class, l); |
|
430 |
||
431 |
// (Receiver, <Intermediates>, Value)Value |
|
432 |
this.typeGetAndUpdate = MethodType.methodType(value, l); |
|
433 |
||
434 |
// (Receiver, <Intermediates>, Value, Value)boolean |
|
435 |
l.add(value); |
|
436 |
this.typeCompareSwap = MethodType.methodType(boolean.class, l); |
|
437 |
||
438 |
// (Receiver, <Intermediates>, Value, Value)Value |
|
439 |
this.typeCompareExchange = MethodType.methodType(value, l); |
|
440 |
} |
|
441 |
||
442 |
RuntimeException unsupported() { |
|
443 |
return new UnsupportedOperationException(); |
|
444 |
} |
|
445 |
||
446 |
// Plain accessors |
|
447 |
||
448 |
/** |
|
449 |
* Returns the value of a variable, with memory semantics of reading as |
|
450 |
* if the variable was declared non-{@code volatile}. Commonly referred to |
|
451 |
* as plain read access. |
|
452 |
* |
|
453 |
* <p>The method signature is of the form {@code (CT)T}. |
|
454 |
* |
|
455 |
* <p>The symbolic type descriptor at the call site of {@code get} |
|
456 |
* must match the access mode type that is the result of calling |
|
457 |
* {@code accessModeType(VarHandle.AccessMode.get)} on this VarHandle. |
|
458 |
* |
|
459 |
* <p>This access mode is supported by all VarHandle instances and never |
|
460 |
* throws {@code UnsupportedOperationException}. |
|
461 |
* |
|
462 |
* @param args the signature-polymorphic parameter list of the form |
|
463 |
* {@code (CT)} |
|
464 |
* , statically represented using varargs. |
|
465 |
* @return the signature-polymorphic result that is the value of the |
|
466 |
* variable |
|
467 |
* , statically represented using {@code Object}. |
|
468 |
* @throws WrongMethodTypeException if the access mode type is not |
|
469 |
* compatible with the caller's symbolic type descriptor. |
|
470 |
*/ |
|
471 |
public final native |
|
472 |
@MethodHandle.PolymorphicSignature |
|
473 |
@HotSpotIntrinsicCandidate |
|
474 |
Object get(Object... args); |
|
475 |
||
476 |
/** |
|
477 |
* Sets the value of a variable to the {@code newValue}, with memory |
|
478 |
* semantics of setting as if the variable was declared non-{@code volatile} |
|
479 |
* and non-{@code final}. Commonly referred to as plain write access. |
|
480 |
* |
|
481 |
* <p>The method signature is of the form {@code (CT, T newValue)void} |
|
482 |
* |
|
483 |
* <p>The symbolic type descriptor at the call site of {@code set} |
|
484 |
* must match the access mode type that is the result of calling |
|
485 |
* {@code accessModeType(VarHandle.AccessMode.set)} on this VarHandle. |
|
486 |
* |
|
487 |
* @param args the signature-polymorphic parameter list of the form |
|
488 |
* {@code (CT, T newValue)} |
|
489 |
* , statically represented using varargs. |
|
490 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
491 |
* for this VarHandle. |
|
492 |
* @throws WrongMethodTypeException if the access mode type is not |
|
493 |
* compatible with the caller's symbolic type descriptor. |
|
494 |
*/ |
|
495 |
public final native |
|
496 |
@MethodHandle.PolymorphicSignature |
|
497 |
@HotSpotIntrinsicCandidate |
|
498 |
void set(Object... args); |
|
499 |
||
500 |
||
501 |
// Volatile accessors |
|
502 |
||
503 |
/** |
|
504 |
* Returns the value of a variable, with memory semantics of reading as if |
|
505 |
* the variable was declared {@code volatile}. |
|
506 |
* |
|
507 |
* <p>The method signature is of the form {@code (CT)T}. |
|
508 |
* |
|
509 |
* <p>The symbolic type descriptor at the call site of {@code getVolatile} |
|
510 |
* must match the access mode type that is the result of calling |
|
511 |
* {@code accessModeType(VarHandle.AccessMode.getVolatile)} on this VarHandle. |
|
512 |
* |
|
513 |
* @param args the signature-polymorphic parameter list of the form |
|
514 |
* {@code (CT)} |
|
515 |
* , statically represented using varargs. |
|
516 |
* @return the signature-polymorphic result that is the value of the |
|
517 |
* variable |
|
518 |
* , statically represented using {@code Object}. |
|
519 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
520 |
* for this VarHandle. |
|
521 |
* @throws WrongMethodTypeException if the access mode type is not |
|
522 |
* compatible with the caller's symbolic type descriptor. |
|
523 |
*/ |
|
524 |
public final native |
|
525 |
@MethodHandle.PolymorphicSignature |
|
526 |
@HotSpotIntrinsicCandidate |
|
527 |
Object getVolatile(Object... args); |
|
528 |
||
529 |
/** |
|
530 |
* Sets the value of a variable to the {@code newValue}, with memory |
|
531 |
* semantics of setting as if the variable was declared {@code volatile}. |
|
532 |
* |
|
533 |
* <p>The method signature is of the form {@code (CT, T newValue)void}. |
|
534 |
* |
|
535 |
* <p>The symbolic type descriptor at the call site of {@code setVolatile} |
|
536 |
* must match the access mode type that is the result of calling |
|
537 |
* {@code accessModeType(VarHandle.AccessMode.setVolatile)} on this VarHandle. |
|
538 |
* |
|
539 |
* @param args the signature-polymorphic parameter list of the form |
|
540 |
* {@code (CT, T newValue)} |
|
541 |
* , statically represented using varargs. |
|
542 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
543 |
* for this VarHandle. |
|
544 |
* @throws WrongMethodTypeException if the access mode type is not |
|
545 |
* compatible with the caller's symbolic type descriptor. |
|
546 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
547 |
* method has memory ordering effects compatible with |
|
548 |
* {@code memory_order_seq_cst}. |
|
549 |
*/ |
|
550 |
public final native |
|
551 |
@MethodHandle.PolymorphicSignature |
|
552 |
@HotSpotIntrinsicCandidate |
|
553 |
void setVolatile(Object... args); |
|
554 |
||
555 |
||
556 |
/** |
|
557 |
* Returns the value of a variable, accessed in program order, but with no |
|
558 |
* assurance of memory ordering effects with respect to other threads. |
|
559 |
* |
|
560 |
* <p>The method signature is of the form {@code (CT)T}. |
|
561 |
* |
|
562 |
* <p>The symbolic type descriptor at the call site of {@code getOpaque} |
|
563 |
* must match the access mode type that is the result of calling |
|
564 |
* {@code accessModeType(VarHandle.AccessMode.getOpaque)} on this VarHandle. |
|
565 |
* |
|
566 |
* @param args the signature-polymorphic parameter list of the form |
|
567 |
* {@code (CT)} |
|
568 |
* , statically represented using varargs. |
|
569 |
* @return the signature-polymorphic result that is the value of the |
|
570 |
* variable |
|
571 |
* , statically represented using {@code Object}. |
|
572 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
573 |
* for this VarHandle. |
|
574 |
* @throws WrongMethodTypeException if the access mode type is not |
|
575 |
* compatible with the caller's symbolic type descriptor. |
|
576 |
*/ |
|
577 |
public final native |
|
578 |
@MethodHandle.PolymorphicSignature |
|
579 |
@HotSpotIntrinsicCandidate |
|
580 |
Object getOpaque(Object... args); |
|
581 |
||
582 |
/** |
|
583 |
* Sets the value of a variable to the {@code newValue}, in program order, |
|
584 |
* but with no assurance of memory ordering effects with respect to other |
|
585 |
* threads. |
|
586 |
* |
|
587 |
* <p>The method signature is of the form {@code (CT, T newValue)void}. |
|
588 |
* |
|
589 |
* <p>The symbolic type descriptor at the call site of {@code setOpaque} |
|
590 |
* must match the access mode type that is the result of calling |
|
591 |
* {@code accessModeType(VarHandle.AccessMode.setOpaque)} on this VarHandle. |
|
592 |
* |
|
593 |
* @param args the signature-polymorphic parameter list of the form |
|
594 |
* {@code (CT, T newValue)} |
|
595 |
* , statically represented using varargs. |
|
596 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
597 |
* for this VarHandle. |
|
598 |
* @throws WrongMethodTypeException if the access mode type is not |
|
599 |
* compatible with the caller's symbolic type descriptor. |
|
600 |
*/ |
|
601 |
public final native |
|
602 |
@MethodHandle.PolymorphicSignature |
|
603 |
@HotSpotIntrinsicCandidate |
|
604 |
void setOpaque(Object... args); |
|
605 |
||
606 |
||
607 |
// Lazy accessors |
|
608 |
||
609 |
/** |
|
610 |
* Returns the value of a variable, and ensures that subsequent loads and |
|
611 |
* stores are not reordered before this access. |
|
612 |
* |
|
613 |
* <p>The method signature is of the form {@code (CT)T}. |
|
614 |
* |
|
615 |
* <p>The symbolic type descriptor at the call site of {@code getAcquire} |
|
616 |
* must match the access mode type that is the result of calling |
|
617 |
* {@code accessModeType(VarHandle.AccessMode.getAcquire)} on this VarHandle. |
|
618 |
* |
|
619 |
* @param args the signature-polymorphic parameter list of the form |
|
620 |
* {@code (CT)} |
|
621 |
* , statically represented using varargs. |
|
622 |
* @return the signature-polymorphic result that is the value of the |
|
623 |
* variable |
|
624 |
* , statically represented using {@code Object}. |
|
625 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
626 |
* for this VarHandle. |
|
627 |
* @throws WrongMethodTypeException if the access mode type is not |
|
628 |
* compatible with the caller's symbolic type descriptor. |
|
629 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
630 |
* method has memory ordering effects compatible with |
|
631 |
* {@code memory_order_acquire} ordering. |
|
632 |
*/ |
|
633 |
public final native |
|
634 |
@MethodHandle.PolymorphicSignature |
|
635 |
@HotSpotIntrinsicCandidate |
|
636 |
Object getAcquire(Object... args); |
|
637 |
||
638 |
/** |
|
639 |
* Sets the value of a variable to the {@code newValue}, and ensures that |
|
640 |
* prior loads and stores are not reordered after this access. |
|
641 |
* |
|
642 |
* <p>The method signature is of the form {@code (CT, T newValue)void}. |
|
643 |
* |
|
644 |
* <p>The symbolic type descriptor at the call site of {@code setRelease} |
|
645 |
* must match the access mode type that is the result of calling |
|
646 |
* {@code accessModeType(VarHandle.AccessMode.setRelease)} on this VarHandle. |
|
647 |
* |
|
648 |
* @param args the signature-polymorphic parameter list of the form |
|
649 |
* {@code (CT, T newValue)} |
|
650 |
* , statically represented using varargs. |
|
651 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
652 |
* for this VarHandle. |
|
653 |
* @throws WrongMethodTypeException if the access mode type is not |
|
654 |
* compatible with the caller's symbolic type descriptor. |
|
655 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
656 |
* method has memory ordering effects compatible with |
|
657 |
* {@code memory_order_release} ordering. |
|
658 |
*/ |
|
659 |
public final native |
|
660 |
@MethodHandle.PolymorphicSignature |
|
661 |
@HotSpotIntrinsicCandidate |
|
662 |
void setRelease(Object... args); |
|
663 |
||
664 |
||
665 |
// Compare and set accessors |
|
666 |
||
667 |
/** |
|
668 |
* Atomically sets the value of a variable to the {@code newValue} with the |
|
669 |
* memory semantics of {@link #setVolatile} if the variable's current value, |
|
670 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
671 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
672 |
* {@link #getVolatile}. |
|
673 |
* |
|
674 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)boolean}. |
|
675 |
* |
|
676 |
* <p>The symbolic type descriptor at the call site of {@code |
|
677 |
* compareAndSet} must match the access mode type that is the result of |
|
678 |
* calling {@code accessModeType(VarHandle.AccessMode.compareAndSet)} on |
|
679 |
* this VarHandle. |
|
680 |
* |
|
681 |
* @param args the signature-polymorphic parameter list of the form |
|
682 |
* {@code (CT, T expectedValue, T newValue)} |
|
683 |
* , statically represented using varargs. |
|
684 |
* @return {@code true} if successful, otherwise {@code false} if the |
|
685 |
* witness value was not the same as the {@code expectedValue}. |
|
686 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
687 |
* for this VarHandle. |
|
688 |
* @throws WrongMethodTypeException if the access mode type is not |
|
689 |
* compatible with the caller's symbolic type descriptor. |
|
690 |
* @see #setVolatile(Object...) |
|
691 |
* @see #getVolatile(Object...) |
|
692 |
*/ |
|
693 |
public final native |
|
694 |
@MethodHandle.PolymorphicSignature |
|
695 |
@HotSpotIntrinsicCandidate |
|
696 |
boolean compareAndSet(Object... args); |
|
697 |
||
698 |
/** |
|
699 |
* Atomically sets the value of a variable to the {@code newValue} with the |
|
700 |
* memory semantics of {@link #setVolatile} if the variable's current value, |
|
701 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
702 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
703 |
* {@link #getVolatile}. |
|
704 |
* |
|
705 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)T}. |
|
706 |
* |
|
707 |
* <p>The symbolic type descriptor at the call site of {@code |
|
708 |
* compareAndExchangeVolatile} |
|
709 |
* must match the access mode type that is the result of calling |
|
710 |
* {@code accessModeType(VarHandle.AccessMode.compareAndExchangeVolatile)} |
|
711 |
* on this VarHandle. |
|
712 |
* |
|
713 |
* @param args the signature-polymorphic parameter list of the form |
|
714 |
* {@code (CT, T expectedValue, T newValue)} |
|
715 |
* , statically represented using varargs. |
|
716 |
* @return the signature-polymorphic result that is the witness value, which |
|
717 |
* will be the same as the {@code expectedValue} if successful |
|
718 |
* , statically represented using {@code Object}. |
|
719 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
720 |
* for this VarHandle. |
|
721 |
* @throws WrongMethodTypeException if the access mode type is not |
|
722 |
* compatible with the caller's symbolic type descriptor. |
|
723 |
* @see #setVolatile(Object...) |
|
724 |
* @see #getVolatile(Object...) |
|
725 |
*/ |
|
726 |
public final native |
|
727 |
@MethodHandle.PolymorphicSignature |
|
728 |
@HotSpotIntrinsicCandidate |
|
729 |
Object compareAndExchangeVolatile(Object... args); |
|
730 |
||
731 |
/** |
|
732 |
* Atomically sets the value of a variable to the {@code newValue} with the |
|
733 |
* memory semantics of {@link #set} if the variable's current value, |
|
734 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
735 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
736 |
* {@link #getAcquire}. |
|
737 |
* |
|
738 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)T}. |
|
739 |
* |
|
740 |
* <p>The symbolic type descriptor at the call site of {@code |
|
741 |
* compareAndExchangeAcquire} |
|
742 |
* must match the access mode type that is the result of calling |
|
743 |
* {@code accessModeType(VarHandle.AccessMode.compareAndExchangeAcquire)} on |
|
744 |
* this VarHandle. |
|
745 |
* |
|
746 |
* @param args the signature-polymorphic parameter list of the form |
|
747 |
* {@code (CT, T expectedValue, T newValue)} |
|
748 |
* , statically represented using varargs. |
|
749 |
* @return the signature-polymorphic result that is the witness value, which |
|
750 |
* will be the same as the {@code expectedValue} if successful |
|
751 |
* , statically represented using {@code Object}. |
|
752 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
753 |
* for this VarHandle. |
|
754 |
* @throws WrongMethodTypeException if the access mode type is not |
|
755 |
* compatible with the caller's symbolic type descriptor. |
|
756 |
* @see #set(Object...) |
|
757 |
* @see #getAcquire(Object...) |
|
758 |
*/ |
|
759 |
public final native |
|
760 |
@MethodHandle.PolymorphicSignature |
|
761 |
@HotSpotIntrinsicCandidate |
|
762 |
Object compareAndExchangeAcquire(Object... args); |
|
763 |
||
764 |
/** |
|
765 |
* Atomically sets the value of a variable to the {@code newValue} with the |
|
766 |
* memory semantics of {@link #setRelease} if the variable's current value, |
|
767 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
768 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
769 |
* {@link #get}. |
|
770 |
* |
|
771 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)T}. |
|
772 |
* |
|
773 |
* <p>The symbolic type descriptor at the call site of {@code |
|
774 |
* compareAndExchangeRelease} |
|
775 |
* must match the access mode type that is the result of calling |
|
776 |
* {@code accessModeType(VarHandle.AccessMode.compareAndExchangeRelease)} on this VarHandle. |
|
777 |
* |
|
778 |
* @param args the signature-polymorphic parameter list of the form |
|
779 |
* {@code (CT, T expectedValue, T newValue)} |
|
780 |
* , statically represented using varargs. |
|
781 |
* @return the signature-polymorphic result that is the witness value, which |
|
782 |
* will be the same as the {@code expectedValue} if successful |
|
783 |
* , statically represented using {@code Object}. |
|
784 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
785 |
* for this VarHandle. |
|
786 |
* @throws WrongMethodTypeException if the access mode type is not |
|
787 |
* compatible with the caller's symbolic type descriptor. |
|
788 |
* @see #setRelease(Object...) |
|
789 |
* @see #get(Object...) |
|
790 |
*/ |
|
791 |
public final native |
|
792 |
@MethodHandle.PolymorphicSignature |
|
793 |
@HotSpotIntrinsicCandidate |
|
794 |
Object compareAndExchangeRelease(Object... args); |
|
795 |
||
796 |
// Weak (spurious failures allowed) |
|
797 |
||
798 |
/** |
|
799 |
* Possibly atomically sets the value of a variable to the {@code newValue} |
|
800 |
* with the semantics of {@link #set} if the variable's current value, |
|
801 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
802 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
803 |
* {@link #get}. |
|
804 |
* |
|
805 |
* <p>This operation may fail spuriously (typically, due to memory |
|
806 |
* contention) even if the current value does match the expected value. |
|
807 |
* |
|
808 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)boolean}. |
|
809 |
* |
|
810 |
* <p>The symbolic type descriptor at the call site of {@code |
|
811 |
* weakCompareAndSet} must match the access mode type that is the result of |
|
812 |
* calling {@code accessModeType(VarHandle.AccessMode.weakCompareAndSet)} on |
|
813 |
* this VarHandle. |
|
814 |
* |
|
815 |
* @param args the signature-polymorphic parameter list of the form |
|
816 |
* {@code (CT, T expectedValue, T newValue)} |
|
817 |
* , statically represented using varargs. |
|
818 |
* @return {@code true} if successful, otherwise {@code false} if the |
|
819 |
* witness value was not the same as the {@code expectedValue} or if this |
|
820 |
* operation spuriously failed. |
|
821 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
822 |
* for this VarHandle. |
|
823 |
* @throws WrongMethodTypeException if the access mode type is not |
|
824 |
* compatible with the caller's symbolic type descriptor. |
|
825 |
* @see #set(Object...) |
|
826 |
* @see #get(Object...) |
|
827 |
*/ |
|
828 |
public final native |
|
829 |
@MethodHandle.PolymorphicSignature |
|
830 |
@HotSpotIntrinsicCandidate |
|
831 |
boolean weakCompareAndSet(Object... args); |
|
832 |
||
833 |
/** |
|
834 |
* Possibly atomically sets the value of a variable to the {@code newValue} |
|
835 |
* with the semantics of {@link #set} if the variable's current value, |
|
836 |
* referred to as the <em>witness value</em>, {@code ==} the |
|
837 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
838 |
* {@link #getAcquire}. |
|
839 |
* |
|
840 |
* <p>This operation may fail spuriously (typically, due to memory |
|
841 |
* contention) even if the current value does match the expected value. |
|
842 |
* |
|
843 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)boolean}. |
|
844 |
* |
|
845 |
* <p>The symbolic type descriptor at the call site of {@code |
|
846 |
* weakCompareAndSetAcquire} |
|
847 |
* must match the access mode type that is the result of calling |
|
848 |
* {@code accessModeType(VarHandle.AccessMode.weakCompareAndSetAcquire)} on this VarHandle. |
|
849 |
* |
|
850 |
* @param args the signature-polymorphic parameter list of the form |
|
851 |
* {@code (CT, T expectedValue, T newValue)} |
|
852 |
* , statically represented using varargs. |
|
853 |
* @return {@code true} if successful, otherwise {@code false} if the |
|
854 |
* witness value was not the same as the {@code expectedValue} or if this |
|
855 |
* operation spuriously failed. |
|
856 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
857 |
* for this VarHandle. |
|
858 |
* @throws WrongMethodTypeException if the access mode type is not |
|
859 |
* compatible with the caller's symbolic type descriptor. |
|
860 |
* @see #set(Object...) |
|
861 |
* @see #getAcquire(Object...) |
|
862 |
*/ |
|
863 |
public final native |
|
864 |
@MethodHandle.PolymorphicSignature |
|
865 |
@HotSpotIntrinsicCandidate |
|
866 |
boolean weakCompareAndSetAcquire(Object... args); |
|
867 |
||
868 |
/** |
|
869 |
* Possibly atomically sets the value of a variable to the {@code newValue} |
|
870 |
* with the semantics of {@link #setRelease} if the variable's current |
|
871 |
* value, referred to as the <em>witness value</em>, {@code ==} the |
|
872 |
* {@code expectedValue}, as accessed with the memory semantics of |
|
873 |
* {@link #get}. |
|
874 |
* |
|
875 |
* <p>This operation may fail spuriously (typically, due to memory |
|
876 |
* contention) even if the current value does match the expected value. |
|
877 |
* |
|
878 |
* <p>The method signature is of the form {@code (CT, T expectedValue, T newValue)boolean}. |
|
879 |
* |
|
880 |
* <p>The symbolic type descriptor at the call site of {@code |
|
881 |
* weakCompareAndSetRelease} |
|
882 |
* must match the access mode type that is the result of calling |
|
883 |
* {@code accessModeType(VarHandle.AccessMode.weakCompareAndSetRelease)} on this VarHandle. |
|
884 |
* |
|
885 |
* @param args the signature-polymorphic parameter list of the form |
|
886 |
* {@code (CT, T expectedValue, T newValue)} |
|
887 |
* , statically represented using varargs. |
|
888 |
* @return {@code true} if successful, otherwise {@code false} if the |
|
889 |
* witness value was not the same as the {@code expectedValue} or if this |
|
890 |
* operation spuriously failed. |
|
891 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
892 |
* for this VarHandle. |
|
893 |
* @throws WrongMethodTypeException if the access mode type is not |
|
894 |
* compatible with the caller's symbolic type descriptor. |
|
895 |
* @see #setRelease(Object...) |
|
896 |
* @see #get(Object...) |
|
897 |
*/ |
|
898 |
public final native |
|
899 |
@MethodHandle.PolymorphicSignature |
|
900 |
@HotSpotIntrinsicCandidate |
|
901 |
boolean weakCompareAndSetRelease(Object... args); |
|
902 |
||
903 |
/** |
|
904 |
* Atomically sets the value of a variable to the {@code newValue} with the |
|
905 |
* memory semantics of {@link #setVolatile} and returns the variable's |
|
906 |
* previous value, as accessed with the memory semantics of |
|
907 |
* {@link #getVolatile}. |
|
908 |
* |
|
909 |
* <p>The method signature is of the form {@code (CT, T newValue)T}. |
|
910 |
* |
|
911 |
* <p>The symbolic type descriptor at the call site of {@code getAndSet} |
|
912 |
* must match the access mode type that is the result of calling |
|
913 |
* {@code accessModeType(VarHandle.AccessMode.getAndSet)} on this VarHandle. |
|
914 |
* |
|
915 |
* @param args the signature-polymorphic parameter list of the form |
|
916 |
* {@code (CT, T newValue)} |
|
917 |
* , statically represented using varargs. |
|
918 |
* @return the signature-polymorphic result that is the previous value of |
|
919 |
* the variable |
|
920 |
* , statically represented using {@code Object}. |
|
921 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
922 |
* for this VarHandle. |
|
923 |
* @throws WrongMethodTypeException if the access mode type is not |
|
924 |
* compatible with the caller's symbolic type descriptor. |
|
925 |
* @see #setVolatile(Object...) |
|
926 |
* @see #getVolatile(Object...) |
|
927 |
*/ |
|
928 |
public final native |
|
929 |
@MethodHandle.PolymorphicSignature |
|
930 |
@HotSpotIntrinsicCandidate |
|
931 |
Object getAndSet(Object... args); |
|
932 |
||
933 |
||
934 |
// Primitive adders |
|
935 |
// Throw UnsupportedOperationException for refs |
|
936 |
||
937 |
/** |
|
938 |
* Atomically adds the {@code value} to the current value of a variable with |
|
939 |
* the memory semantics of {@link #setVolatile}, and returns the variable's |
|
940 |
* previous value, as accessed with the memory semantics of |
|
941 |
* {@link #getVolatile}. |
|
942 |
* |
|
943 |
* <p>The method signature is of the form {@code (CT, T value)T}. |
|
944 |
* |
|
945 |
* <p>The symbolic type descriptor at the call site of {@code getAndAdd} |
|
946 |
* must match the access mode type that is the result of calling |
|
947 |
* {@code accessModeType(VarHandle.AccessMode.getAndAdd)} on this VarHandle. |
|
948 |
* |
|
949 |
* @param args the signature-polymorphic parameter list of the form |
|
950 |
* {@code (CT, T value)} |
|
951 |
* , statically represented using varargs. |
|
952 |
* @return the signature-polymorphic result that is the previous value of |
|
953 |
* the variable |
|
954 |
* , statically represented using {@code Object}. |
|
955 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
956 |
* for this VarHandle. |
|
957 |
* @throws WrongMethodTypeException if the access mode type is not |
|
958 |
* compatible with the caller's symbolic type descriptor. |
|
959 |
* @see #setVolatile(Object...) |
|
960 |
* @see #getVolatile(Object...) |
|
961 |
*/ |
|
962 |
public final native |
|
963 |
@MethodHandle.PolymorphicSignature |
|
964 |
@HotSpotIntrinsicCandidate |
|
965 |
Object getAndAdd(Object... args); |
|
966 |
||
967 |
/** |
|
968 |
* Atomically adds the {@code value} to the current value of a variable with |
|
969 |
* the memory semantics of {@link #setVolatile}, and returns the variable's |
|
970 |
* current (updated) value, as accessed with the memory semantics of |
|
971 |
* {@link #getVolatile}. |
|
972 |
* |
|
973 |
* <p>The method signature is of the form {@code (CT, T value)T}. |
|
974 |
* |
|
975 |
* <p>The symbolic type descriptor at the call site of {@code addAndGet} |
|
976 |
* must match the access mode type that is the result of calling |
|
977 |
* {@code accessModeType(VarHandle.AccessMode.addAndGet)} on this VarHandle. |
|
978 |
* |
|
979 |
* @param args the signature-polymorphic parameter list of the form |
|
980 |
* {@code (CT, T value)} |
|
981 |
* , statically represented using varargs. |
|
982 |
* @return the signature-polymorphic result that is the current value of |
|
983 |
* the variable |
|
984 |
* , statically represented using {@code Object}. |
|
985 |
* @throws UnsupportedOperationException if the access mode is unsupported |
|
986 |
* for this VarHandle. |
|
987 |
* @throws WrongMethodTypeException if the access mode type is not |
|
988 |
* compatible with the caller's symbolic type descriptor. |
|
989 |
* @see #setVolatile(Object...) |
|
990 |
* @see #getVolatile(Object...) |
|
991 |
*/ |
|
992 |
public final native |
|
993 |
@MethodHandle.PolymorphicSignature |
|
994 |
@HotSpotIntrinsicCandidate |
|
995 |
Object addAndGet(Object... args); |
|
996 |
||
997 |
enum AccessType { |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
998 |
GET, // 0 |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
999 |
SET, // 1 |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1000 |
COMPARE_AND_SWAP, // 2 |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1001 |
COMPARE_AND_EXCHANGE, // 3 |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1002 |
GET_AND_UPDATE; // 4 |
36934 | 1003 |
|
1004 |
MethodType getMethodType(VarHandle vh) { |
|
1005 |
return getMethodType(this.ordinal(), vh); |
|
1006 |
} |
|
1007 |
||
1008 |
@ForceInline |
|
1009 |
static MethodType getMethodType(int ordinal, VarHandle vh) { |
|
1010 |
if (ordinal == 0) { |
|
1011 |
return vh.typeGet; |
|
1012 |
} |
|
1013 |
else if (ordinal == 1) { |
|
1014 |
return vh.typeSet; |
|
1015 |
} |
|
1016 |
else if (ordinal == 2) { |
|
1017 |
return vh.typeCompareSwap; |
|
1018 |
} |
|
1019 |
else if (ordinal == 3) { |
|
1020 |
return vh.typeCompareExchange; |
|
1021 |
} |
|
1022 |
else if (ordinal == 4) { |
|
1023 |
return vh.typeGetAndUpdate; |
|
1024 |
} |
|
1025 |
else { |
|
1026 |
throw new IllegalStateException("Illegal access type: " + ordinal); |
|
1027 |
} |
|
1028 |
} |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* The set of access modes that specify how a variable, referenced by a |
|
1033 |
* VarHandle, is accessed. |
|
1034 |
*/ |
|
1035 |
public enum AccessMode { |
|
1036 |
/** |
|
1037 |
* The access mode whose access is specified by the corresponding |
|
1038 |
* method |
|
1039 |
* {@link VarHandle#get VarHandle.get} |
|
1040 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1041 |
GET("get", AccessType.GET, Object.class), // 0 |
36934 | 1042 |
/** |
1043 |
* The access mode whose access is specified by the corresponding |
|
1044 |
* method |
|
1045 |
* {@link VarHandle#set VarHandle.set} |
|
1046 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1047 |
SET("set", AccessType.SET, void.class), // 1 |
36934 | 1048 |
/** |
1049 |
* The access mode whose access is specified by the corresponding |
|
1050 |
* method |
|
1051 |
* {@link VarHandle#getVolatile VarHandle.getVolatile} |
|
1052 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1053 |
GET_VOLATILE("getVolatile", AccessType.GET, Object.class), // 2 |
36934 | 1054 |
/** |
1055 |
* The access mode whose access is specified by the corresponding |
|
1056 |
* method |
|
1057 |
* {@link VarHandle#setVolatile VarHandle.setVolatile} |
|
1058 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1059 |
SET_VOLATILE("setVolatile", AccessType.SET, void.class), // 3 |
36934 | 1060 |
/** |
1061 |
* The access mode whose access is specified by the corresponding |
|
1062 |
* method |
|
1063 |
* {@link VarHandle#getAcquire VarHandle.getAcquire} |
|
1064 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1065 |
GET_ACQUIRE("getAcquire", AccessType.GET, Object.class), // 4 |
36934 | 1066 |
/** |
1067 |
* The access mode whose access is specified by the corresponding |
|
1068 |
* method |
|
1069 |
* {@link VarHandle#setRelease VarHandle.setRelease} |
|
1070 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1071 |
SET_RELEASE("setRelease", AccessType.SET, void.class), // 5 |
36934 | 1072 |
/** |
1073 |
* The access mode whose access is specified by the corresponding |
|
1074 |
* method |
|
1075 |
* {@link VarHandle#getOpaque VarHandle.getOpaque} |
|
1076 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1077 |
GET_OPAQUE("getOpaque", AccessType.GET, Object.class), // 6 |
36934 | 1078 |
/** |
1079 |
* The access mode whose access is specified by the corresponding |
|
1080 |
* method |
|
1081 |
* {@link VarHandle#setOpaque VarHandle.setOpaque} |
|
1082 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1083 |
SET_OPAQUE("setOpaque", AccessType.SET, void.class), // 7 |
36934 | 1084 |
/** |
1085 |
* The access mode whose access is specified by the corresponding |
|
1086 |
* method |
|
1087 |
* {@link VarHandle#compareAndSet VarHandle.compareAndSet} |
|
1088 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1089 |
COMPARE_AND_SET("compareAndSet", AccessType.COMPARE_AND_SWAP, boolean.class), // 8 |
36934 | 1090 |
/** |
1091 |
* The access mode whose access is specified by the corresponding |
|
1092 |
* method |
|
1093 |
* {@link VarHandle#compareAndExchangeVolatile VarHandle.compareAndExchangeVolatile} |
|
1094 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1095 |
COMPARE_AND_EXCHANGE_VOLATILE("compareAndExchangeVolatile", AccessType.COMPARE_AND_EXCHANGE, Object.class), // 9 |
36934 | 1096 |
/** |
1097 |
* The access mode whose access is specified by the corresponding |
|
1098 |
* method |
|
1099 |
* {@link VarHandle#compareAndExchangeAcquire VarHandle.compareAndExchangeAcquire} |
|
1100 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1101 |
COMPARE_AND_EXCHANGE_ACQUIRE("compareAndExchangeAcquire", AccessType.COMPARE_AND_EXCHANGE, Object.class), // 10 |
36934 | 1102 |
/** |
1103 |
* The access mode whose access is specified by the corresponding |
|
1104 |
* method |
|
1105 |
* {@link VarHandle#compareAndExchangeRelease VarHandle.compareAndExchangeRelease} |
|
1106 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1107 |
COMPARE_AND_EXCHANGE_RELEASE("compareAndExchangeRelease", AccessType.COMPARE_AND_EXCHANGE, Object.class), // 11 |
36934 | 1108 |
/** |
1109 |
* The access mode whose access is specified by the corresponding |
|
1110 |
* method |
|
1111 |
* {@link VarHandle#weakCompareAndSet VarHandle.weakCompareAndSet} |
|
1112 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1113 |
WEAK_COMPARE_AND_SET("weakCompareAndSet", AccessType.COMPARE_AND_SWAP, boolean.class), // 12 |
36934 | 1114 |
/** |
1115 |
* The access mode whose access is specified by the corresponding |
|
1116 |
* method |
|
1117 |
* {@link VarHandle#weakCompareAndSetAcquire VarHandle.weakCompareAndSetAcquire} |
|
1118 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1119 |
WEAK_COMPARE_AND_SET_ACQUIRE("weakCompareAndSetAcquire", AccessType.COMPARE_AND_SWAP, boolean.class), // 13 |
36934 | 1120 |
/** |
1121 |
* The access mode whose access is specified by the corresponding |
|
1122 |
* method |
|
1123 |
* {@link VarHandle#weakCompareAndSetRelease VarHandle.weakCompareAndSetRelease} |
|
1124 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1125 |
WEAK_COMPARE_AND_SET_RELEASE("weakCompareAndSetRelease", AccessType.COMPARE_AND_SWAP, boolean.class), // 14 |
36934 | 1126 |
/** |
1127 |
* The access mode whose access is specified by the corresponding |
|
1128 |
* method |
|
1129 |
* {@link VarHandle#getAndSet VarHandle.getAndSet} |
|
1130 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1131 |
GET_AND_SET("getAndSet", AccessType.GET_AND_UPDATE, Object.class), // 15 |
36934 | 1132 |
/** |
1133 |
* The access mode whose access is specified by the corresponding |
|
1134 |
* method |
|
1135 |
* {@link VarHandle#getAndAdd VarHandle.getAndAdd} |
|
1136 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1137 |
GET_AND_ADD("getAndAdd", AccessType.GET_AND_UPDATE, Object.class), // 16 |
36934 | 1138 |
/** |
1139 |
* The access mode whose access is specified by the corresponding |
|
1140 |
* method |
|
1141 |
* {@link VarHandle#addAndGet VarHandle.addAndGet} |
|
1142 |
*/ |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1143 |
ADD_AND_GET("addAndGet", AccessType.GET_AND_UPDATE, Object.class), // 17 |
36934 | 1144 |
; |
1145 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1146 |
static final Map<String, AccessMode> methodNameToAccessMode; |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1147 |
static { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1148 |
// Initial capacity of # values is sufficient to avoid resizes |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1149 |
// for the smallest table size (32) |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1150 |
methodNameToAccessMode = new HashMap<>(AccessMode.values().length); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1151 |
for (AccessMode am : AccessMode.values()) { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1152 |
methodNameToAccessMode.put(am.methodName, am); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1153 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1154 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1155 |
|
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1156 |
final String methodName; |
36934 | 1157 |
final AccessType at; |
1158 |
final boolean isPolyMorphicInReturnType; |
|
1159 |
final Class<?> returnType; |
|
1160 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1161 |
AccessMode(final String methodName, AccessType at, Class<?> returnType) { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1162 |
this.methodName = methodName; |
36934 | 1163 |
this.at = at; |
1164 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1165 |
// Assert method name is correctly derived from value name |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1166 |
assert methodName.equals(toMethodName(name())); |
36934 | 1167 |
// Assert that return type is correct |
1168 |
// Otherwise, when disabled avoid using reflection |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1169 |
assert returnType == getReturnType(methodName); |
36934 | 1170 |
|
1171 |
this.returnType = returnType; |
|
1172 |
isPolyMorphicInReturnType = returnType != Object.class; |
|
1173 |
} |
|
1174 |
||
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1175 |
/** |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1176 |
* Returns the {@code VarHandle} signature-polymorphic method name |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1177 |
* associated with this {@code AccessMode} value |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1178 |
* |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1179 |
* @return the signature-polymorphic method name |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1180 |
* @see #valueFromMethodName |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1181 |
*/ |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1182 |
public String methodName() { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1183 |
return methodName; |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1184 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1185 |
|
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1186 |
/** |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1187 |
* Returns the {@code AccessMode} value associated with the specified |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1188 |
* {@code VarHandle} signature-polymorphic method name. |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1189 |
* |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1190 |
* @param methodName the signature-polymorphic method name |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1191 |
* @return the {@code AccessMode} value |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1192 |
* @throws IllegalArgumentException if there is no {@code AccessMode} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1193 |
* value associated with method name (indicating the method |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1194 |
* name does not correspond to a {@code VarHandle} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1195 |
* signature-polymorphic method name). |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1196 |
* @see #methodName |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1197 |
*/ |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1198 |
public static AccessMode valueFromMethodName(String methodName) { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1199 |
AccessMode am = methodNameToAccessMode.get(methodName); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1200 |
if (am != null) return am; |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1201 |
throw new IllegalArgumentException("No AccessMode value for method name " + methodName); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1202 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1203 |
|
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1204 |
private static String toMethodName(String name) { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1205 |
StringBuilder s = new StringBuilder(name.toLowerCase()); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1206 |
int i; |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1207 |
while ((i = s.indexOf("_")) != -1) { |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1208 |
s.deleteCharAt(i); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1209 |
s.setCharAt(i, Character.toUpperCase(s.charAt(i))); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1210 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1211 |
return s.toString(); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1212 |
} |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36973
diff
changeset
|
1213 |
|
36934 | 1214 |
private static Class<?> getReturnType(String name) { |
1215 |
try { |
|
1216 |
Method m = VarHandle.class.getMethod(name, Object[].class); |
|
1217 |
return m.getReturnType(); |
|
1218 |
} |
|
1219 |
catch (Exception e) { |
|
1220 |
throw newInternalError(e); |
|
1221 |
} |
|
1222 |
} |
|
1223 |
||
1224 |
@ForceInline |
|
1225 |
static MemberName getMemberName(int ordinal, VarForm vform) { |
|
36973
951bb58383a4
8151706: Update VarHandle implementation to use @Stable arrays
psandoz
parents:
36934
diff
changeset
|
1226 |
return vform.table[ordinal]; |
36934 | 1227 |
} |
1228 |
} |
|
1229 |
||
1230 |
static final class AccessDescriptor { |
|
1231 |
final MethodType symbolicMethodType; |
|
1232 |
final int type; |
|
1233 |
final int mode; |
|
1234 |
||
1235 |
public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) { |
|
1236 |
this.symbolicMethodType = symbolicMethodType; |
|
1237 |
this.type = type; |
|
1238 |
this.mode = mode; |
|
1239 |
} |
|
1240 |
} |
|
1241 |
||
1242 |
/** |
|
1243 |
* Returns the variable type of variables referenced by this VarHandle. |
|
1244 |
* |
|
1245 |
* @return the variable type of variables referenced by this VarHandle |
|
1246 |
*/ |
|
1247 |
public final Class<?> varType() { |
|
1248 |
return typeSet.parameterType(typeSet.parameterCount() - 1); |
|
1249 |
} |
|
1250 |
||
1251 |
/** |
|
1252 |
* Returns the coordinate types for this VarHandle. |
|
1253 |
* |
|
1254 |
* @return the coordinate types for this VarHandle. The returned |
|
1255 |
* list is unmodifiable |
|
1256 |
*/ |
|
1257 |
public final List<Class<?>> coordinateTypes() { |
|
1258 |
return typeGet.parameterList(); |
|
1259 |
} |
|
1260 |
||
1261 |
/** |
|
1262 |
* Obtains the canonical access mode type for this VarHandle and a given |
|
1263 |
* access mode. |
|
1264 |
* |
|
1265 |
* <p>The access mode type's parameter types will consist of a prefix that |
|
1266 |
* is the coordinate types of this VarHandle followed by further |
|
1267 |
* types as defined by the access mode's method. |
|
1268 |
* The access mode type's return type is defined by the return type of the |
|
1269 |
* access mode's method. |
|
1270 |
* |
|
1271 |
* @param accessMode the access mode, corresponding to the |
|
1272 |
* signature-polymorphic method of the same name |
|
1273 |
* @return the access mode type for the given access mode |
|
1274 |
*/ |
|
1275 |
public final MethodType accessModeType(AccessMode accessMode) { |
|
1276 |
return accessMode.at.getMethodType(this); |
|
1277 |
} |
|
1278 |
||
1279 |
||
1280 |
/** |
|
1281 |
* Returns {@code true} if the given access mode is supported, otherwise |
|
1282 |
* {@code false}. |
|
1283 |
* |
|
1284 |
* <p>The return of a {@code false} value for a given access mode indicates |
|
1285 |
* that an {@code UnsupportedOperationException} is thrown on invocation |
|
1286 |
* of the corresponding access mode's signature-polymorphic method. |
|
1287 |
* |
|
1288 |
* @param accessMode the access mode, corresponding to the |
|
1289 |
* signature-polymorphic method of the same name |
|
1290 |
* @return {@code true} if the given access mode is supported, otherwise |
|
1291 |
* {@code false}. |
|
1292 |
*/ |
|
1293 |
public final boolean isAccessModeSupported(AccessMode accessMode) { |
|
1294 |
return AccessMode.getMemberName(accessMode.ordinal(), vform) != null; |
|
1295 |
} |
|
1296 |
||
1297 |
/** |
|
1298 |
* Obtains a method handle bound to this VarHandle and the given access |
|
1299 |
* mode. |
|
1300 |
* |
|
1301 |
* @apiNote This method, for a VarHandle {@code vh} and access mode |
|
1302 |
* {@code {access-mode}}, returns a method handle that is equivalent to |
|
1303 |
* method handle {@code bhm} in the following code (though it may be more |
|
1304 |
* efficient): |
|
1305 |
* <pre>{@code |
|
1306 |
* MethodHandle mh = MethodHandles.varHandleExactInvoker( |
|
1307 |
* vh.accessModeType(VarHandle.AccessMode.{access-mode})); |
|
1308 |
* |
|
1309 |
* MethodHandle bmh = mh.bindTo(vh); |
|
1310 |
* }</pre> |
|
1311 |
* |
|
1312 |
* @param accessMode the access mode, corresponding to the |
|
1313 |
* signature-polymorphic method of the same name |
|
1314 |
* @return a method handle bound to this VarHandle and the given access mode |
|
1315 |
*/ |
|
1316 |
public final MethodHandle toMethodHandle(AccessMode accessMode) { |
|
1317 |
MemberName mn = AccessMode.getMemberName(accessMode.ordinal(), vform); |
|
1318 |
if (mn != null) { |
|
1319 |
return DirectMethodHandle.make(mn). |
|
1320 |
bindTo(this). |
|
1321 |
asType(accessMode.at.getMethodType(this)); |
|
1322 |
} |
|
1323 |
else { |
|
1324 |
// Ensure an UnsupportedOperationException is thrown |
|
1325 |
return MethodHandles.varHandleInvoker(accessMode, accessModeType(accessMode)). |
|
1326 |
bindTo(this); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
||
1330 |
/*non-public*/ |
|
1331 |
final void updateVarForm(VarForm newVForm) { |
|
1332 |
if (vform == newVForm) return; |
|
1333 |
UNSAFE.putObject(this, VFORM_OFFSET, newVForm); |
|
1334 |
UNSAFE.fullFence(); |
|
1335 |
} |
|
1336 |
||
1337 |
static final BiFunction<Integer, Integer, ArrayIndexOutOfBoundsException> AIOOBE_SUPPLIER = new BiFunction<>() { |
|
1338 |
@Override |
|
1339 |
public ArrayIndexOutOfBoundsException apply(Integer a, Integer b) { |
|
1340 |
return new ArrayIndexOutOfBoundsException(a, b); |
|
1341 |
} |
|
1342 |
}; |
|
1343 |
||
1344 |
private static final long VFORM_OFFSET; |
|
1345 |
||
1346 |
static { |
|
1347 |
try { |
|
1348 |
VFORM_OFFSET = UNSAFE.objectFieldOffset(VarHandle.class.getDeclaredField("vform")); |
|
1349 |
} |
|
1350 |
catch (ReflectiveOperationException e) { |
|
1351 |
throw newInternalError(e); |
|
1352 |
} |
|
1353 |
} |
|
1354 |
||
1355 |
||
1356 |
// Fence methods |
|
1357 |
||
1358 |
/** |
|
1359 |
* Ensures that loads and stores before the fence will not be reordered |
|
1360 |
* with |
|
1361 |
* loads and stores after the fence. |
|
1362 |
* |
|
1363 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
1364 |
* method has memory ordering effects compatible with |
|
1365 |
* {@code atomic_thread_fence(memory_order_seq_cst)} |
|
1366 |
*/ |
|
1367 |
@ForceInline |
|
1368 |
public static void fullFence() { |
|
1369 |
UNSAFE.fullFence(); |
|
1370 |
} |
|
1371 |
||
1372 |
/** |
|
1373 |
* Ensures that loads before the fence will not be reordered with loads and |
|
1374 |
* stores after the fence. |
|
1375 |
* |
|
1376 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
1377 |
* method has memory ordering effects compatible with |
|
1378 |
* {@code atomic_thread_fence(memory_order_acquire)} |
|
1379 |
*/ |
|
1380 |
@ForceInline |
|
1381 |
public static void acquireFence() { |
|
1382 |
UNSAFE.loadFence(); |
|
1383 |
} |
|
1384 |
||
1385 |
/** |
|
1386 |
* Ensures that loads and stores before the fence will not be |
|
1387 |
* reordered with stores after the fence. |
|
1388 |
* |
|
1389 |
* @apiNote Ignoring the many semantic differences from C and C++, this |
|
1390 |
* method has memory ordering effects compatible with |
|
1391 |
* {@code atomic_thread_fence(memory_order_release)} |
|
1392 |
*/ |
|
1393 |
@ForceInline |
|
1394 |
public static void releaseFence() { |
|
1395 |
UNSAFE.storeFence(); |
|
1396 |
} |
|
1397 |
||
1398 |
/** |
|
1399 |
* Ensures that loads before the fence will not be reordered with |
|
1400 |
* loads after the fence. |
|
1401 |
*/ |
|
1402 |
@ForceInline |
|
1403 |
public static void loadLoadFence() { |
|
1404 |
UNSAFE.loadLoadFence(); |
|
1405 |
} |
|
1406 |
||
1407 |
/** |
|
1408 |
* Ensures that stores before the fence will not be reordered with |
|
1409 |
* stores after the fence. |
|
1410 |
*/ |
|
1411 |
@ForceInline |
|
1412 |
public static void storeStoreFence() { |
|
1413 |
UNSAFE.storeStoreFence(); |
|
1414 |
} |
|
1415 |
} |