• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014, 2018, 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 dalvik.system.VMRuntime;
29 import jdk.internal.HotSpotIntrinsicCandidate;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.EnumSet;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Objects;
37 
38 /**
39  * A VarHandle is a dynamically strongly typed reference to a variable, or to a
40  * parametrically-defined family of variables, including static fields,
41  * non-static fields, array elements, or components of an off-heap data
42  * structure.  Access to such variables is supported under various
43  * <em>access modes</em>, including plain read/write access, volatile
44  * read/write access, and compare-and-set.
45  *
46  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
47  * subclassed by the user.
48  *
49  * <p>A VarHandle has:
50  * <ul>
51  * <li>a {@link #varType variable type} T, the type of every variable referenced
52  * by this VarHandle; and
53  * <li>a list of {@link #coordinateTypes coordinate types}
54  * {@code CT1, CT2, ..., CTn}, the types of <em>coordinate expressions</em> that
55  * jointly locate a variable referenced by this VarHandle.
56  * </ul>
57  * Variable and coordinate types may be primitive or reference, and are
58  * represented by {@code Class} objects.  The list of coordinate types may be
59  * empty.
60  *
61  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
62  * lookup} VarHandle instances document the supported variable type and the list
63  * of coordinate types.
64  *
65  * <p>Each access mode is associated with one <em>access mode method</em>, a
66  * <a href="MethodHandle.html#sigpoly">signature polymorphic</a> method named
67  * for the access mode.  When an access mode method is invoked on a VarHandle
68  * instance, the initial arguments to the invocation are coordinate expressions
69  * that indicate in precisely which object the variable is to be accessed.
70  * Trailing arguments to the invocation represent values of importance to the
71  * access mode.  For example, the various compare-and-set or compare-and-exchange
72  * access modes require two trailing arguments for the variable's expected value
73  * and new value.
74  *
75  * <p>The arity and types of arguments to the invocation of an access mode
76  * method are not checked statically.  Instead, each access mode method
77  * specifies an {@link #accessModeType(AccessMode) access mode type},
78  * represented as an instance of {@link MethodType}, that serves as a kind of
79  * method signature against which the arguments are checked dynamically.  An
80  * access mode type gives formal parameter types in terms of the coordinate
81  * types of a VarHandle instance and the types for values of importance to the
82  * access mode.  An access mode type also gives a return type, often in terms of
83  * the variable type of a VarHandle instance.  When an access mode method is
84  * invoked on a VarHandle instance, the symbolic type descriptor at the
85  * call site, the run time types of arguments to the invocation, and the run
86  * time type of the return value, must <a href="#invoke">match</a> the types
87  * given in the access mode type.  A runtime exception will be thrown if the
88  * match fails.
89  *
90  * For example, the access mode method {@link #compareAndSet} specifies that if
91  * its receiver is a VarHandle instance with coordinate types
92  * {@code CT1, ..., CTn} and variable type {@code T}, then its access mode type
93  * is {@code (CT1 c1, ..., CTn cn, T expectedValue, T newValue)boolean}.
94  * Suppose that a VarHandle instance can access array elements, and that its
95  * coordinate types are {@code String[]} and {@code int} while its variable type
96  * is {@code String}.  The access mode type for {@code compareAndSet} on this
97  * VarHandle instance would be
98  * {@code (String[] c1, int c2, String expectedValue, String newValue)boolean}.
99  * Such a VarHandle instance may be produced by the
100  * {@link MethodHandles#arrayElementVarHandle(Class) array factory method} and
101  * access array elements as follows:
102  * <pre> {@code
103  * String[] sa = ...
104  * VarHandle avh = MethodHandles.arrayElementVarHandle(String[].class);
105  * boolean r = avh.compareAndSet(sa, 10, "expected", "new");
106  * }</pre>
107  *
108  * <p>Access modes control atomicity and consistency properties.
109  * <em>Plain</em> read ({@code get}) and write ({@code set})
110  * accesses are guaranteed to be bitwise atomic only for references
111  * and for primitive values of at most 32 bits, and impose no observable
112  * ordering constraints with respect to threads other than the
113  * executing thread. <em>Opaque</em> operations are bitwise atomic and
114  * coherently ordered with respect to accesses to the same variable.
115  * In addition to obeying Opaque properties, <em>Acquire</em> mode
116  * reads and their subsequent accesses are ordered after matching
117  * <em>Release</em> mode writes and their previous accesses.  In
118  * addition to obeying Acquire and Release properties, all
119  * <em>Volatile</em> operations are totally ordered with respect to
120  * each other.
121  *
122  * <p>Access modes are grouped into the following categories:
123  * <ul>
124  * <li>read access modes that get the value of a variable under specified
125  * memory ordering effects.
126  * The set of corresponding access mode methods belonging to this group
127  * consists of the methods
128  * {@link #get get},
129  * {@link #getVolatile getVolatile},
130  * {@link #getAcquire getAcquire},
131  * {@link #getOpaque getOpaque}.
132  * <li>write access modes that set the value of a variable under specified
133  * memory ordering effects.
134  * The set of corresponding access mode methods belonging to this group
135  * consists of the methods
136  * {@link #set set},
137  * {@link #setVolatile setVolatile},
138  * {@link #setRelease setRelease},
139  * {@link #setOpaque setOpaque}.
140  * <li>atomic update access modes that, for example, atomically compare and set
141  * the value of a variable under specified memory ordering effects.
142  * The set of corresponding access mode methods belonging to this group
143  * consists of the methods
144  * {@link #compareAndSet compareAndSet},
145  * {@link #weakCompareAndSetPlain weakCompareAndSetPlain},
146  * {@link #weakCompareAndSet weakCompareAndSet},
147  * {@link #weakCompareAndSetAcquire weakCompareAndSetAcquire},
148  * {@link #weakCompareAndSetRelease weakCompareAndSetRelease},
149  * {@link #compareAndExchangeAcquire compareAndExchangeAcquire},
150  * {@link #compareAndExchange compareAndExchange},
151  * {@link #compareAndExchangeRelease compareAndExchangeRelease},
152  * {@link #getAndSet getAndSet},
153  * {@link #getAndSetAcquire getAndSetAcquire},
154  * {@link #getAndSetRelease getAndSetRelease}.
155  * <li>numeric atomic update access modes that, for example, atomically get and
156  * set with addition the value of a variable under specified memory ordering
157  * effects.
158  * The set of corresponding access mode methods belonging to this group
159  * consists of the methods
160  * {@link #getAndAdd getAndAdd},
161  * {@link #getAndAddAcquire getAndAddAcquire},
162  * {@link #getAndAddRelease getAndAddRelease},
163  * <li>bitwise atomic update access modes that, for example, atomically get and
164  * bitwise OR the value of a variable under specified memory ordering
165  * effects.
166  * The set of corresponding access mode methods belonging to this group
167  * consists of the methods
168  * {@link #getAndBitwiseOr getAndBitwiseOr},
169  * {@link #getAndBitwiseOrAcquire getAndBitwiseOrAcquire},
170  * {@link #getAndBitwiseOrRelease getAndBitwiseOrRelease},
171  * {@link #getAndBitwiseAnd getAndBitwiseAnd},
172  * {@link #getAndBitwiseAndAcquire getAndBitwiseAndAcquire},
173  * {@link #getAndBitwiseAndRelease getAndBitwiseAndRelease},
174  * {@link #getAndBitwiseXor getAndBitwiseXor},
175  * {@link #getAndBitwiseXorAcquire getAndBitwiseXorAcquire},
176  * {@link #getAndBitwiseXorRelease getAndBitwiseXorRelease}.
177  * </ul>
178  *
179  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
180  * lookup} VarHandle instances document the set of access modes that are
181  * supported, which may also include documenting restrictions based on the
182  * variable type and whether a variable is read-only.  If an access mode is not
183  * supported then the corresponding access mode method will on invocation throw
184  * an {@code UnsupportedOperationException}.  Factory methods should document
185  * any additional undeclared exceptions that may be thrown by access mode
186  * methods.
187  * The {@link #get get} access mode is supported for all
188  * VarHandle instances and the corresponding method never throws
189  * {@code UnsupportedOperationException}.
190  * If a VarHandle references a read-only variable (for example a {@code final}
191  * field) then write, atomic update, numeric atomic update, and bitwise atomic
192  * update access modes are not supported and corresponding methods throw
193  * {@code UnsupportedOperationException}.
194  * Read/write access modes (if supported), with the exception of
195  * {@code get} and {@code set}, provide atomic access for
196  * reference types and all primitive types.
197  * Unless stated otherwise in the documentation of a factory method, the access
198  * modes {@code get} and {@code set} (if supported) provide atomic access for
199  * reference types and all primitives types, with the exception of {@code long}
200  * and {@code double} on 32-bit platforms.
201  *
202  * <p>Access modes will override any memory ordering effects specified at
203  * the declaration site of a variable.  For example, a VarHandle accessing
204  * a field using the {@code get} access mode will access the field as
205  * specified <em>by its access mode</em> even if that field is declared
206  * {@code volatile}.  When mixed access is performed extreme care should be
207  * taken since the Java Memory Model may permit surprising results.
208  *
209  * <p>In addition to supporting access to variables under various access modes,
210  * a set of static methods, referred to as memory fence methods, is also
211  * provided for fine-grained control of memory ordering.
212  *
213  * The Java Language Specification permits other threads to observe operations
214  * as if they were executed in orders different than are apparent in program
215  * source code, subject to constraints arising, for example, from the use of
216  * locks, {@code volatile} fields or VarHandles.  The static methods,
217  * {@link #fullFence fullFence}, {@link #acquireFence acquireFence},
218  * {@link #releaseFence releaseFence}, {@link #loadLoadFence loadLoadFence} and
219  * {@link #storeStoreFence storeStoreFence}, can also be used to impose
220  * constraints.  Their specifications, as is the case for certain access modes,
221  * are phrased in terms of the lack of "reorderings" -- observable ordering
222  * effects that might otherwise occur if the fence was not present.  More
223  * precise phrasing of the specification of access mode methods and memory fence
224  * methods may accompany future updates of the Java Language Specification.
225  *
226  * <h1>Compiling invocation of access mode methods</h1>
227  * A Java method call expression naming an access mode method can invoke a
228  * VarHandle from Java source code.  From the viewpoint of source code, these
229  * methods can take any arguments and their polymorphic result (if expressed)
230  * can be cast to any return type.  Formally this is accomplished by giving the
231  * access mode methods variable arity {@code Object} arguments and
232  * {@code Object} return types (if the return type is polymorphic), but they
233  * have an additional quality called <em>signature polymorphism</em> which
234  * connects this freedom of invocation directly to the JVM execution stack.
235  * <p>
236  * As is usual with virtual methods, source-level calls to access mode methods
237  * compile to an {@code invokevirtual} instruction.  More unusually, the
238  * compiler must record the actual argument types, and may not perform method
239  * invocation conversions on the arguments.  Instead, it must generate
240  * instructions to push them on the stack according to their own unconverted
241  * types.  The VarHandle object itself will be pushed on the stack before the
242  * arguments.  The compiler then generates an {@code invokevirtual} instruction
243  * that invokes the access mode method with a symbolic type descriptor which
244  * describes the argument and return types.
245  * <p>
246  * To issue a complete symbolic type descriptor, the compiler must also
247  * determine the return type (if polymorphic).  This is based on a cast on the
248  * method invocation expression, if there is one, or else {@code Object} if the
249  * invocation is an expression, or else {@code void} if the invocation is a
250  * statement.  The cast may be to a primitive type (but not {@code void}).
251  * <p>
252  * As a corner case, an uncasted {@code null} argument is given a symbolic type
253  * descriptor of {@code java.lang.Void}.  The ambiguity with the type
254  * {@code Void} is harmless, since there are no references of type {@code Void}
255  * except the null reference.
256  *
257  *
258  * <h1><a id="invoke">Performing invocation of access mode methods</a></h1>
259  * The first time an {@code invokevirtual} instruction is executed it is linked
260  * by symbolically resolving the names in the instruction and verifying that
261  * the method call is statically legal.  This also holds for calls to access mode
262  * methods.  In this case, the symbolic type descriptor emitted by the compiler
263  * is checked for correct syntax, and names it contains are resolved.  Thus, an
264  * {@code invokevirtual} instruction which invokes an access mode method will
265  * always link, as long as the symbolic type descriptor is syntactically
266  * well-formed and the types exist.
267  * <p>
268  * When the {@code invokevirtual} is executed after linking, the receiving
269  * VarHandle's access mode type is first checked by the JVM to ensure that it
270  * matches the symbolic type descriptor.  If the type
271  * match fails, it means that the access mode method which the caller is
272  * invoking is not present on the individual VarHandle being invoked.
273  *
274  * <p>
275  * Invocation of an access mode method behaves as if an invocation of
276  * {@link MethodHandle#invoke}, where the receiving method handle accepts the
277  * VarHandle instance as the leading argument.  More specifically, the
278  * following, where {@code {access-mode}} corresponds to the access mode method
279  * name:
280  * <pre> {@code
281  * VarHandle vh = ..
282  * R r = (R) vh.{access-mode}(p1, p2, ..., pN);
283  * }</pre>
284  * behaves as if:
285  * <pre> {@code
286  * VarHandle vh = ..
287  * VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
288  * MethodHandle mh = MethodHandles.varHandleExactInvoker(
289  *                       am,
290  *                       vh.accessModeType(am));
291  *
292  * R r = (R) mh.invoke(vh, p1, p2, ..., pN)
293  * }</pre>
294  * (modulo access mode methods do not declare throwing of {@code Throwable}).
295  * This is equivalent to:
296  * <pre> {@code
297  * MethodHandle mh = MethodHandles.lookup().findVirtual(
298  *                       VarHandle.class,
299  *                       "{access-mode}",
300  *                       MethodType.methodType(R, p1, p2, ..., pN));
301  *
302  * R r = (R) mh.invokeExact(vh, p1, p2, ..., pN)
303  * }</pre>
304  * where the desired method type is the symbolic type descriptor and a
305  * {@link MethodHandle#invokeExact} is performed, since before invocation of the
306  * target, the handle will apply reference casts as necessary and box, unbox, or
307  * widen primitive values, as if by {@link MethodHandle#asType asType} (see also
308  * {@link MethodHandles#varHandleInvoker}).
309  *
310  * More concisely, such behaviour is equivalent to:
311  * <pre> {@code
312  * VarHandle vh = ..
313  * VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
314  * MethodHandle mh = vh.toMethodHandle(am);
315  *
316  * R r = (R) mh.invoke(p1, p2, ..., pN)
317  * }</pre>
318  * Where, in this case, the method handle is bound to the VarHandle instance.
319  *
320  *
321  * <h1>Invocation checking</h1>
322  * In typical programs, VarHandle access mode type matching will usually
323  * succeed.  But if a match fails, the JVM will throw a
324  * {@link WrongMethodTypeException}.
325  * <p>
326  * Thus, an access mode type mismatch which might show up as a linkage error
327  * in a statically typed program can show up as a dynamic
328  * {@code WrongMethodTypeException} in a program which uses VarHandles.
329  * <p>
330  * Because access mode types contain "live" {@code Class} objects, method type
331  * matching takes into account both type names and class loaders.
332  * Thus, even if a VarHandle {@code VH} is created in one class loader
333  * {@code L1} and used in another {@code L2}, VarHandle access mode method
334  * calls are type-safe, because the caller's symbolic type descriptor, as
335  * resolved in {@code L2}, is matched against the original callee method's
336  * symbolic type descriptor, as resolved in {@code L1}.  The resolution in
337  * {@code L1} happens when {@code VH} is created and its access mode types are
338  * assigned, while the resolution in {@code L2} happens when the
339  * {@code invokevirtual} instruction is linked.
340  * <p>
341  * Apart from type descriptor checks, a VarHandles's capability to
342  * access it's variables is unrestricted.
343  * If a VarHandle is formed on a non-public variable by a class that has access
344  * to that variable, the resulting VarHandle can be used in any place by any
345  * caller who receives a reference to it.
346  * <p>
347  * Unlike with the Core Reflection API, where access is checked every time a
348  * reflective method is invoked, VarHandle access checking is performed
349  * <a href="MethodHandles.Lookup.html#access">when the VarHandle is
350  * created</a>.
351  * Thus, VarHandles to non-public variables, or to variables in non-public
352  * classes, should generally be kept secret.  They should not be passed to
353  * untrusted code unless their use from the untrusted code would be harmless.
354  *
355  *
356  * <h1>VarHandle creation</h1>
357  * Java code can create a VarHandle that directly accesses any field that is
358  * accessible to that code.  This is done via a reflective, capability-based
359  * API called {@link java.lang.invoke.MethodHandles.Lookup
360  * MethodHandles.Lookup}.
361  * For example, a VarHandle for a non-static field can be obtained
362  * from {@link java.lang.invoke.MethodHandles.Lookup#findVarHandle
363  * Lookup.findVarHandle}.
364  * There is also a conversion method from Core Reflection API objects,
365  * {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle
366  * Lookup.unreflectVarHandle}.
367  * <p>
368  * Access to protected field members is restricted to receivers only of the
369  * accessing class, or one of its subclasses, and the accessing class must in
370  * turn be a subclass (or package sibling) of the protected member's defining
371  * class.  If a VarHandle refers to a protected non-static field of a declaring
372  * class outside the current package, the receiver argument will be narrowed to
373  * the type of the accessing class.
374  *
375  * <h1>Interoperation between VarHandles and the Core Reflection API</h1>
376  * Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup
377  * Lookup} API, any field represented by a Core Reflection API object
378  * can be converted to a behaviorally equivalent VarHandle.
379  * For example, a reflective {@link java.lang.reflect.Field Field} can
380  * be converted to a VarHandle using
381  * {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle
382  * Lookup.unreflectVarHandle}.
383  * The resulting VarHandles generally provide more direct and efficient
384  * access to the underlying fields.
385  * <p>
386  * As a special case, when the Core Reflection API is used to view the
387  * signature polymorphic access mode methods in this class, they appear as
388  * ordinary non-polymorphic methods.  Their reflective appearance, as viewed by
389  * {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod},
390  * is unaffected by their special status in this API.
391  * For example, {@link java.lang.reflect.Method#getModifiers
392  * Method.getModifiers}
393  * will report exactly those modifier bits required for any similarly
394  * declared method, including in this case {@code native} and {@code varargs}
395  * bits.
396  * <p>
397  * As with any reflected method, these methods (when reflected) may be invoked
398  * directly via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke},
399  * via JNI, or indirectly via
400  * {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
401  * However, such reflective calls do not result in access mode method
402  * invocations.  Such a call, if passed the required argument (a single one, of
403  * type {@code Object[]}), will ignore the argument and will throw an
404  * {@code UnsupportedOperationException}.
405  * <p>
406  * Since {@code invokevirtual} instructions can natively invoke VarHandle
407  * access mode methods under any symbolic type descriptor, this reflective view
408  * conflicts with the normal presentation of these methods via bytecodes.
409  * Thus, these native methods, when reflectively viewed by
410  * {@code Class.getDeclaredMethod}, may be regarded as placeholders only.
411  * <p>
412  * In order to obtain an invoker method for a particular access mode type,
413  * use {@link java.lang.invoke.MethodHandles#varHandleExactInvoker} or
414  * {@link java.lang.invoke.MethodHandles#varHandleInvoker}.  The
415  * {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual}
416  * API is also able to return a method handle to call an access mode method for
417  * any specified access mode type and is equivalent in behaviour to
418  * {@link java.lang.invoke.MethodHandles#varHandleInvoker}.
419  *
420  * <h1>Interoperation between VarHandles and Java generics</h1>
421  * A VarHandle can be obtained for a variable, such as a field, which is
422  * declared with Java generic types.  As with the Core Reflection API, the
423  * VarHandle's variable type will be constructed from the erasure of the
424  * source-level type.  When a VarHandle access mode method is invoked, the
425  * types
426  * of its arguments or the return value cast type may be generic types or type
427  * instances.  If this occurs, the compiler will replace those types by their
428  * erasures when it constructs the symbolic type descriptor for the
429  * {@code invokevirtual} instruction.
430  *
431  * @see MethodHandle
432  * @see MethodHandles
433  * @see MethodType
434  * @since 9
435  */
436 public abstract class VarHandle {
437     // Android-added: Using sun.misc.Unsafe for fence implementation.
438     private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
439 
440     // BEGIN Android-removed: No VarForm in Android implementation.
441     /*
442     final VarForm vform;
443 
444     VarHandle(VarForm vform) {
445         this.vform = vform;
446     }
447     */
448     // END Android-removed: No VarForm in Android implementation.
449 
450     // BEGIN Android-added: fields for common metadata.
451     /** The target type for accesses. */
452     private final Class<?> varType;
453 
454     /** This VarHandle's first coordinate, or null if this VarHandle has no coordinates. */
455     private final Class<?> coordinateType0;
456 
457     /** This VarHandle's second coordinate, or null if this VarHandle has less than two
458      * coordinates. */
459     private final Class<?> coordinateType1;
460 
461     /** BitMask of supported access mode indexed by AccessMode.ordinal(). */
462     private final int accessModesBitMask;
463     // END Android-added: fields for common metadata.
464 
465     // Plain accessors
466 
467     /**
468      * Returns the value of a variable, with memory semantics of reading as
469      * if the variable was declared non-{@code volatile}.  Commonly referred to
470      * as plain read access.
471      *
472      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
473      *
474      * <p>The symbolic type descriptor at the call site of {@code get}
475      * must match the access mode type that is the result of calling
476      * {@code accessModeType(VarHandle.AccessMode.GET)} on this VarHandle.
477      *
478      * <p>This access mode is supported by all VarHandle instances and never
479      * throws {@code UnsupportedOperationException}.
480      *
481      * @param args the signature-polymorphic parameter list of the form
482      * {@code (CT1 ct1, ..., CTn)}
483      * , statically represented using varargs.
484      * @return the signature-polymorphic result that is the value of the
485      * variable
486      * , statically represented using {@code Object}.
487      * @throws WrongMethodTypeException if the access mode type does not
488      * match the caller's symbolic type descriptor.
489      * @throws ClassCastException if the access mode type matches the caller's
490      * symbolic type descriptor, but a reference cast fails.
491      */
492     public final native
493     @MethodHandle.PolymorphicSignature
494     @HotSpotIntrinsicCandidate
get(Object... args)495     Object get(Object... args);
496 
497     /**
498      * Sets the value of a variable to the {@code newValue}, with memory
499      * semantics of setting as if the variable was declared non-{@code volatile}
500      * and non-{@code final}.  Commonly referred to as plain write access.
501      *
502      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}
503      *
504      * <p>The symbolic type descriptor at the call site of {@code set}
505      * must match the access mode type that is the result of calling
506      * {@code accessModeType(VarHandle.AccessMode.SET)} on this VarHandle.
507      *
508      * @param args the signature-polymorphic parameter list of the form
509      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
510      * , statically represented using varargs.
511      * @throws UnsupportedOperationException if the access mode is unsupported
512      * for this VarHandle.
513      * @throws WrongMethodTypeException if the access mode type does not
514      * match the caller's symbolic type descriptor.
515      * @throws ClassCastException if the access mode type matches the caller's
516      * symbolic type descriptor, but a reference cast fails.
517      */
518     public final native
519     @MethodHandle.PolymorphicSignature
520     @HotSpotIntrinsicCandidate
set(Object... args)521     void set(Object... args);
522 
523 
524     // Volatile accessors
525 
526     /**
527      * Returns the value of a variable, with memory semantics of reading as if
528      * the variable was declared {@code volatile}.
529      *
530      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
531      *
532      * <p>The symbolic type descriptor at the call site of {@code getVolatile}
533      * must match the access mode type that is the result of calling
534      * {@code accessModeType(VarHandle.AccessMode.GET_VOLATILE)} on this
535      * VarHandle.
536      *
537      * @param args the signature-polymorphic parameter list of the form
538      * {@code (CT1 ct1, ..., CTn ctn)}
539      * , statically represented using varargs.
540      * @return the signature-polymorphic result that is the value of the
541      * variable
542      * , statically represented using {@code Object}.
543      * @throws UnsupportedOperationException if the access mode is unsupported
544      * for this VarHandle.
545      * @throws WrongMethodTypeException if the access mode type does not
546      * match the caller's symbolic type descriptor.
547      * @throws ClassCastException if the access mode type matches the caller's
548      * symbolic type descriptor, but a reference cast fails.
549      */
550     public final native
551     @MethodHandle.PolymorphicSignature
552     @HotSpotIntrinsicCandidate
getVolatile(Object... args)553     Object getVolatile(Object... args);
554 
555     /**
556      * Sets the value of a variable to the {@code newValue}, with memory
557      * semantics of setting as if the variable was declared {@code volatile}.
558      *
559      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
560      *
561      * <p>The symbolic type descriptor at the call site of {@code setVolatile}
562      * must match the access mode type that is the result of calling
563      * {@code accessModeType(VarHandle.AccessMode.SET_VOLATILE)} on this
564      * VarHandle.
565      *
566      * @apiNote
567      * Ignoring the many semantic differences from C and C++, this method has
568      * memory ordering effects compatible with {@code memory_order_seq_cst}.
569      *
570      * @param args the signature-polymorphic parameter list of the form
571      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
572      * , statically represented using varargs.
573      * @throws UnsupportedOperationException if the access mode is unsupported
574      * for this VarHandle.
575      * @throws WrongMethodTypeException if the access mode type does not
576      * match the caller's symbolic type descriptor.
577      * @throws ClassCastException if the access mode type matches the caller's
578      * symbolic type descriptor, but a reference cast fails.
579      */
580     public final native
581     @MethodHandle.PolymorphicSignature
582     @HotSpotIntrinsicCandidate
setVolatile(Object... args)583     void setVolatile(Object... args);
584 
585 
586     /**
587      * Returns the value of a variable, accessed in program order, but with no
588      * assurance of memory ordering effects with respect to other threads.
589      *
590      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
591      *
592      * <p>The symbolic type descriptor at the call site of {@code getOpaque}
593      * must match the access mode type that is the result of calling
594      * {@code accessModeType(VarHandle.AccessMode.GET_OPAQUE)} on this
595      * VarHandle.
596      *
597      * @param args the signature-polymorphic parameter list of the form
598      * {@code (CT1 ct1, ..., CTn ctn)}
599      * , statically represented using varargs.
600      * @return the signature-polymorphic result that is the value of the
601      * variable
602      * , statically represented using {@code Object}.
603      * @throws UnsupportedOperationException if the access mode is unsupported
604      * for this VarHandle.
605      * @throws WrongMethodTypeException if the access mode type does not
606      * match the caller's symbolic type descriptor.
607      * @throws ClassCastException if the access mode type matches the caller's
608      * symbolic type descriptor, but a reference cast fails.
609      */
610     public final native
611     @MethodHandle.PolymorphicSignature
612     @HotSpotIntrinsicCandidate
getOpaque(Object... args)613     Object getOpaque(Object... args);
614 
615     /**
616      * Sets the value of a variable to the {@code newValue}, in program order,
617      * but with no assurance of memory ordering effects with respect to other
618      * threads.
619      *
620      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
621      *
622      * <p>The symbolic type descriptor at the call site of {@code setOpaque}
623      * must match the access mode type that is the result of calling
624      * {@code accessModeType(VarHandle.AccessMode.SET_OPAQUE)} on this
625      * VarHandle.
626      *
627      * @param args the signature-polymorphic parameter list of the form
628      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
629      * , statically represented using varargs.
630      * @throws UnsupportedOperationException if the access mode is unsupported
631      * for this VarHandle.
632      * @throws WrongMethodTypeException if the access mode type does not
633      * match the caller's symbolic type descriptor.
634      * @throws ClassCastException if the access mode type matches the caller's
635      * symbolic type descriptor, but a reference cast fails.
636      */
637     public final native
638     @MethodHandle.PolymorphicSignature
639     @HotSpotIntrinsicCandidate
setOpaque(Object... args)640     void setOpaque(Object... args);
641 
642 
643     // Lazy accessors
644 
645     /**
646      * Returns the value of a variable, and ensures that subsequent loads and
647      * stores are not reordered before this access.
648      *
649      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
650      *
651      * <p>The symbolic type descriptor at the call site of {@code getAcquire}
652      * must match the access mode type that is the result of calling
653      * {@code accessModeType(VarHandle.AccessMode.GET_ACQUIRE)} on this
654      * VarHandle.
655      *
656      * @apiNote
657      * Ignoring the many semantic differences from C and C++, this method has
658      * memory ordering effects compatible with {@code memory_order_acquire}
659      * ordering.
660      *
661      * @param args the signature-polymorphic parameter list of the form
662      * {@code (CT1 ct1, ..., CTn ctn)}
663      * , statically represented using varargs.
664      * @return the signature-polymorphic result that is the value of the
665      * variable
666      * , statically represented using {@code Object}.
667      * @throws UnsupportedOperationException if the access mode is unsupported
668      * for this VarHandle.
669      * @throws WrongMethodTypeException if the access mode type does not
670      * match the caller's symbolic type descriptor.
671      * @throws ClassCastException if the access mode type matches the caller's
672      * symbolic type descriptor, but a reference cast fails.
673      */
674     public final native
675     @MethodHandle.PolymorphicSignature
676     @HotSpotIntrinsicCandidate
getAcquire(Object... args)677     Object getAcquire(Object... args);
678 
679     /**
680      * Sets the value of a variable to the {@code newValue}, and ensures that
681      * prior loads and stores are not reordered after this access.
682      *
683      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
684      *
685      * <p>The symbolic type descriptor at the call site of {@code setRelease}
686      * must match the access mode type that is the result of calling
687      * {@code accessModeType(VarHandle.AccessMode.SET_RELEASE)} on this
688      * VarHandle.
689      *
690      * @apiNote
691      * Ignoring the many semantic differences from C and C++, this method has
692      * memory ordering effects compatible with {@code memory_order_release}
693      * ordering.
694      *
695      * @param args the signature-polymorphic parameter list of the form
696      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
697      * , statically represented using varargs.
698      * @throws UnsupportedOperationException if the access mode is unsupported
699      * for this VarHandle.
700      * @throws WrongMethodTypeException if the access mode type does not
701      * match the caller's symbolic type descriptor.
702      * @throws ClassCastException if the access mode type matches the caller's
703      * symbolic type descriptor, but a reference cast fails.
704      */
705     public final native
706     @MethodHandle.PolymorphicSignature
707     @HotSpotIntrinsicCandidate
setRelease(Object... args)708     void setRelease(Object... args);
709 
710 
711     // Compare and set accessors
712 
713     /**
714      * Atomically sets the value of a variable to the {@code newValue} with the
715      * memory semantics of {@link #setVolatile} if the variable's current value,
716      * referred to as the <em>witness value</em>, {@code ==} the
717      * {@code expectedValue}, as accessed with the memory semantics of
718      * {@link #getVolatile}.
719      *
720      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
721      *
722      * <p>The symbolic type descriptor at the call site of {@code
723      * compareAndSet} must match the access mode type that is the result of
724      * calling {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_SET)} on
725      * this VarHandle.
726      *
727      * @param args the signature-polymorphic parameter list of the form
728      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
729      * , statically represented using varargs.
730      * @return {@code true} if successful, otherwise {@code false} if the
731      * witness value was not the same as the {@code expectedValue}.
732      * @throws UnsupportedOperationException if the access mode is unsupported
733      * for this VarHandle.
734      * @throws WrongMethodTypeException if the access mode type does not
735      * match the caller's symbolic type descriptor.
736      * @throws ClassCastException if the access mode type matches the caller's
737      * symbolic type descriptor, but a reference cast fails.
738      * @see #setVolatile(Object...)
739      * @see #getVolatile(Object...)
740      */
741     public final native
742     @MethodHandle.PolymorphicSignature
743     @HotSpotIntrinsicCandidate
compareAndSet(Object... args)744     boolean compareAndSet(Object... args);
745 
746     /**
747      * Atomically sets the value of a variable to the {@code newValue} with the
748      * memory semantics of {@link #setVolatile} if the variable's current value,
749      * referred to as the <em>witness value</em>, {@code ==} the
750      * {@code expectedValue}, as accessed with the memory semantics of
751      * {@link #getVolatile}.
752      *
753      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
754      *
755      * <p>The symbolic type descriptor at the call site of {@code
756      * compareAndExchange}
757      * must match the access mode type that is the result of calling
758      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)}
759      * on this VarHandle.
760      *
761      * @param args the signature-polymorphic parameter list of the form
762      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
763      * , statically represented using varargs.
764      * @return the signature-polymorphic result that is the witness value, which
765      * will be the same as the {@code expectedValue} if successful
766      * , statically represented using {@code Object}.
767      * @throws UnsupportedOperationException if the access mode is unsupported
768      * for this VarHandle.
769      * @throws WrongMethodTypeException if the access mode type is not
770      * compatible with the caller's symbolic type descriptor.
771      * @throws ClassCastException if the access mode type is compatible with the
772      * caller's symbolic type descriptor, but a reference cast fails.
773      * @see #setVolatile(Object...)
774      * @see #getVolatile(Object...)
775      */
776     public final native
777     @MethodHandle.PolymorphicSignature
778     @HotSpotIntrinsicCandidate
compareAndExchange(Object... args)779     Object compareAndExchange(Object... args);
780 
781     /**
782      * Atomically sets the value of a variable to the {@code newValue} with the
783      * memory semantics of {@link #set} if the variable's current value,
784      * referred to as the <em>witness value</em>, {@code ==} the
785      * {@code expectedValue}, as accessed with the memory semantics of
786      * {@link #getAcquire}.
787      *
788      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
789      *
790      * <p>The symbolic type descriptor at the call site of {@code
791      * compareAndExchangeAcquire}
792      * must match the access mode type that is the result of calling
793      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)} on
794      * this VarHandle.
795      *
796      * @param args the signature-polymorphic parameter list of the form
797      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
798      * , statically represented using varargs.
799      * @return the signature-polymorphic result that is the witness value, which
800      * will be the same as the {@code expectedValue} if successful
801      * , statically represented using {@code Object}.
802      * @throws UnsupportedOperationException if the access mode is unsupported
803      * for this VarHandle.
804      * @throws WrongMethodTypeException if the access mode type does not
805      * match the caller's symbolic type descriptor.
806      * @throws ClassCastException if the access mode type matches the caller's
807      * symbolic type descriptor, but a reference cast fails.
808      * @see #set(Object...)
809      * @see #getAcquire(Object...)
810      */
811     public final native
812     @MethodHandle.PolymorphicSignature
813     @HotSpotIntrinsicCandidate
compareAndExchangeAcquire(Object... args)814     Object compareAndExchangeAcquire(Object... args);
815 
816     /**
817      * Atomically sets the value of a variable to the {@code newValue} with the
818      * memory semantics of {@link #setRelease} if the variable's current value,
819      * referred to as the <em>witness value</em>, {@code ==} the
820      * {@code expectedValue}, as accessed with the memory semantics of
821      * {@link #get}.
822      *
823      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
824      *
825      * <p>The symbolic type descriptor at the call site of {@code
826      * compareAndExchangeRelease}
827      * must match the access mode type that is the result of calling
828      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)}
829      * on this VarHandle.
830      *
831      * @param args the signature-polymorphic parameter list of the form
832      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
833      * , statically represented using varargs.
834      * @return the signature-polymorphic result that is the witness value, which
835      * will be the same as the {@code expectedValue} if successful
836      * , statically represented using {@code Object}.
837      * @throws UnsupportedOperationException if the access mode is unsupported
838      * for this VarHandle.
839      * @throws WrongMethodTypeException if the access mode type does not
840      * match the caller's symbolic type descriptor.
841      * @throws ClassCastException if the access mode type matches the caller's
842      * symbolic type descriptor, but a reference cast fails.
843      * @see #setRelease(Object...)
844      * @see #get(Object...)
845      */
846     public final native
847     @MethodHandle.PolymorphicSignature
848     @HotSpotIntrinsicCandidate
compareAndExchangeRelease(Object... args)849     Object compareAndExchangeRelease(Object... args);
850 
851     // Weak (spurious failures allowed)
852 
853     /**
854      * Possibly atomically sets the value of a variable to the {@code newValue}
855      * with the semantics of {@link #set} if the variable's current value,
856      * referred to as the <em>witness value</em>, {@code ==} the
857      * {@code expectedValue}, as accessed with the memory semantics of
858      * {@link #get}.
859      *
860      * <p>This operation may fail spuriously (typically, due to memory
861      * contention) even if the witness value does match the expected value.
862      *
863      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
864      *
865      * <p>The symbolic type descriptor at the call site of {@code
866      * weakCompareAndSetPlain} must match the access mode type that is the result of
867      * calling {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN)}
868      * on this VarHandle.
869      *
870      * @param args the signature-polymorphic parameter list of the form
871      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
872      * , statically represented using varargs.
873      * @return {@code true} if successful, otherwise {@code false} if the
874      * witness value was not the same as the {@code expectedValue} or if this
875      * operation spuriously failed.
876      * @throws UnsupportedOperationException if the access mode is unsupported
877      * for this VarHandle.
878      * @throws WrongMethodTypeException if the access mode type does not
879      * match the caller's symbolic type descriptor.
880      * @throws ClassCastException if the access mode type matches the caller's
881      * symbolic type descriptor, but a reference cast fails.
882      * @see #set(Object...)
883      * @see #get(Object...)
884      */
885     public final native
886     @MethodHandle.PolymorphicSignature
887     @HotSpotIntrinsicCandidate
weakCompareAndSetPlain(Object... args)888     boolean weakCompareAndSetPlain(Object... args);
889 
890     /**
891      * Possibly atomically sets the value of a variable to the {@code newValue}
892      * with the memory semantics of {@link #setVolatile} if the variable's
893      * current value, referred to as the <em>witness value</em>, {@code ==} the
894      * {@code expectedValue}, as accessed with the memory semantics of
895      * {@link #getVolatile}.
896      *
897      * <p>This operation may fail spuriously (typically, due to memory
898      * contention) even if the witness value does match the expected value.
899      *
900      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
901      *
902      * <p>The symbolic type descriptor at the call site of {@code
903      * weakCompareAndSet} must match the access mode type that is the
904      * result of calling {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)}
905      * on this VarHandle.
906      *
907      * @param args the signature-polymorphic parameter list of the form
908      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
909      * , statically represented using varargs.
910      * @return {@code true} if successful, otherwise {@code false} if the
911      * witness value was not the same as the {@code expectedValue} or if this
912      * operation spuriously failed.
913      * @throws UnsupportedOperationException if the access mode is unsupported
914      * for this VarHandle.
915      * @throws WrongMethodTypeException if the access mode type does not
916      * match the caller's symbolic type descriptor.
917      * @throws ClassCastException if the access mode type matches the caller's
918      * symbolic type descriptor, but a reference cast fails.
919      * @see #setVolatile(Object...)
920      * @see #getVolatile(Object...)
921      */
922     public final native
923     @MethodHandle.PolymorphicSignature
924     @HotSpotIntrinsicCandidate
weakCompareAndSet(Object... args)925     boolean weakCompareAndSet(Object... args);
926 
927     /**
928      * Possibly atomically sets the value of a variable to the {@code newValue}
929      * with the semantics of {@link #set} if the variable's current value,
930      * referred to as the <em>witness value</em>, {@code ==} the
931      * {@code expectedValue}, as accessed with the memory semantics of
932      * {@link #getAcquire}.
933      *
934      * <p>This operation may fail spuriously (typically, due to memory
935      * contention) even if the witness value does match the expected value.
936      *
937      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
938      *
939      * <p>The symbolic type descriptor at the call site of {@code
940      * weakCompareAndSetAcquire}
941      * must match the access mode type that is the result of calling
942      * {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)}
943      * on this VarHandle.
944      *
945      * @param args the signature-polymorphic parameter list of the form
946      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
947      * , statically represented using varargs.
948      * @return {@code true} if successful, otherwise {@code false} if the
949      * witness value was not the same as the {@code expectedValue} or if this
950      * operation spuriously failed.
951      * @throws UnsupportedOperationException if the access mode is unsupported
952      * for this VarHandle.
953      * @throws WrongMethodTypeException if the access mode type does not
954      * match the caller's symbolic type descriptor.
955      * @throws ClassCastException if the access mode type matches the caller's
956      * symbolic type descriptor, but a reference cast fails.
957      * @see #set(Object...)
958      * @see #getAcquire(Object...)
959      */
960     public final native
961     @MethodHandle.PolymorphicSignature
962     @HotSpotIntrinsicCandidate
weakCompareAndSetAcquire(Object... args)963     boolean weakCompareAndSetAcquire(Object... args);
964 
965     /**
966      * Possibly atomically sets the value of a variable to the {@code newValue}
967      * with the semantics of {@link #setRelease} if the variable's current
968      * value, referred to as the <em>witness value</em>, {@code ==} the
969      * {@code expectedValue}, as accessed with the memory semantics of
970      * {@link #get}.
971      *
972      * <p>This operation may fail spuriously (typically, due to memory
973      * contention) even if the witness value does match the expected value.
974      *
975      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
976      *
977      * <p>The symbolic type descriptor at the call site of {@code
978      * weakCompareAndSetRelease}
979      * must match the access mode type that is the result of calling
980      * {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)}
981      * on this VarHandle.
982      *
983      * @param args the signature-polymorphic parameter list of the form
984      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
985      * , statically represented using varargs.
986      * @return {@code true} if successful, otherwise {@code false} if the
987      * witness value was not the same as the {@code expectedValue} or if this
988      * operation spuriously failed.
989      * @throws UnsupportedOperationException if the access mode is unsupported
990      * for this VarHandle.
991      * @throws WrongMethodTypeException if the access mode type does not
992      * match the caller's symbolic type descriptor.
993      * @throws ClassCastException if the access mode type matches the caller's
994      * symbolic type descriptor, but a reference cast fails.
995      * @see #setRelease(Object...)
996      * @see #get(Object...)
997      */
998     public final native
999     @MethodHandle.PolymorphicSignature
1000     @HotSpotIntrinsicCandidate
weakCompareAndSetRelease(Object... args)1001     boolean weakCompareAndSetRelease(Object... args);
1002 
1003     /**
1004      * Atomically sets the value of a variable to the {@code newValue} with the
1005      * memory semantics of {@link #setVolatile} and returns the variable's
1006      * previous value, as accessed with the memory semantics of
1007      * {@link #getVolatile}.
1008      *
1009      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1010      *
1011      * <p>The symbolic type descriptor at the call site of {@code getAndSet}
1012      * must match the access mode type that is the result of calling
1013      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET)} on this
1014      * VarHandle.
1015      *
1016      * @param args the signature-polymorphic parameter list of the form
1017      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1018      * , statically represented using varargs.
1019      * @return the signature-polymorphic result that is the previous value of
1020      * the variable
1021      * , statically represented using {@code Object}.
1022      * @throws UnsupportedOperationException if the access mode is unsupported
1023      * for this VarHandle.
1024      * @throws WrongMethodTypeException if the access mode type does not
1025      * match the caller's symbolic type descriptor.
1026      * @throws ClassCastException if the access mode type matches the caller's
1027      * symbolic type descriptor, but a reference cast fails.
1028      * @see #setVolatile(Object...)
1029      * @see #getVolatile(Object...)
1030      */
1031     public final native
1032     @MethodHandle.PolymorphicSignature
1033     @HotSpotIntrinsicCandidate
getAndSet(Object... args)1034     Object getAndSet(Object... args);
1035 
1036     /**
1037      * Atomically sets the value of a variable to the {@code newValue} with the
1038      * memory semantics of {@link #set} and returns the variable's
1039      * previous value, as accessed with the memory semantics of
1040      * {@link #getAcquire}.
1041      *
1042      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1043      *
1044      * <p>The symbolic type descriptor at the call site of {@code getAndSetAcquire}
1045      * must match the access mode type that is the result of calling
1046      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)} on this
1047      * VarHandle.
1048      *
1049      * @param args the signature-polymorphic parameter list of the form
1050      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1051      * , statically represented using varargs.
1052      * @return the signature-polymorphic result that is the previous value of
1053      * the variable
1054      * , statically represented using {@code Object}.
1055      * @throws UnsupportedOperationException if the access mode is unsupported
1056      * for this VarHandle.
1057      * @throws WrongMethodTypeException if the access mode type does not
1058      * match the caller's symbolic type descriptor.
1059      * @throws ClassCastException if the access mode type matches the caller's
1060      * symbolic type descriptor, but a reference cast fails.
1061      * @see #setVolatile(Object...)
1062      * @see #getVolatile(Object...)
1063      */
1064     public final native
1065     @MethodHandle.PolymorphicSignature
1066     @HotSpotIntrinsicCandidate
getAndSetAcquire(Object... args)1067     Object getAndSetAcquire(Object... args);
1068 
1069     /**
1070      * Atomically sets the value of a variable to the {@code newValue} with the
1071      * memory semantics of {@link #setRelease} and returns the variable's
1072      * previous value, as accessed with the memory semantics of
1073      * {@link #get}.
1074      *
1075      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1076      *
1077      * <p>The symbolic type descriptor at the call site of {@code getAndSetRelease}
1078      * must match the access mode type that is the result of calling
1079      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET_RELEASE)} on this
1080      * VarHandle.
1081      *
1082      * @param args the signature-polymorphic parameter list of the form
1083      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1084      * , statically represented using varargs.
1085      * @return the signature-polymorphic result that is the previous value of
1086      * the variable
1087      * , statically represented using {@code Object}.
1088      * @throws UnsupportedOperationException if the access mode is unsupported
1089      * for this VarHandle.
1090      * @throws WrongMethodTypeException if the access mode type does not
1091      * match the caller's symbolic type descriptor.
1092      * @throws ClassCastException if the access mode type matches the caller's
1093      * symbolic type descriptor, but a reference cast fails.
1094      * @see #setVolatile(Object...)
1095      * @see #getVolatile(Object...)
1096      */
1097     public final native
1098     @MethodHandle.PolymorphicSignature
1099     @HotSpotIntrinsicCandidate
getAndSetRelease(Object... args)1100     Object getAndSetRelease(Object... args);
1101 
1102     // Primitive adders
1103     // Throw UnsupportedOperationException for refs
1104 
1105     /**
1106      * Atomically adds the {@code value} to the current value of a variable with
1107      * the memory semantics of {@link #setVolatile}, and returns the variable's
1108      * previous value, as accessed with the memory semantics of
1109      * {@link #getVolatile}.
1110      *
1111      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1112      *
1113      * <p>The symbolic type descriptor at the call site of {@code getAndAdd}
1114      * must match the access mode type that is the result of calling
1115      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD)} on this
1116      * VarHandle.
1117      *
1118      * @param args the signature-polymorphic parameter list of the form
1119      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1120      * , statically represented using varargs.
1121      * @return the signature-polymorphic result that is the previous value of
1122      * the variable
1123      * , statically represented using {@code Object}.
1124      * @throws UnsupportedOperationException if the access mode is unsupported
1125      * for this VarHandle.
1126      * @throws WrongMethodTypeException if the access mode type does not
1127      * match the caller's symbolic type descriptor.
1128      * @throws ClassCastException if the access mode type matches the caller's
1129      * symbolic type descriptor, but a reference cast fails.
1130      * @see #setVolatile(Object...)
1131      * @see #getVolatile(Object...)
1132      */
1133     public final native
1134     @MethodHandle.PolymorphicSignature
1135     @HotSpotIntrinsicCandidate
getAndAdd(Object... args)1136     Object getAndAdd(Object... args);
1137 
1138     /**
1139      * Atomically adds the {@code value} to the current value of a variable with
1140      * the memory semantics of {@link #set}, and returns the variable's
1141      * previous value, as accessed with the memory semantics of
1142      * {@link #getAcquire}.
1143      *
1144      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1145      *
1146      * <p>The symbolic type descriptor at the call site of {@code getAndAddAcquire}
1147      * must match the access mode type that is the result of calling
1148      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)} on this
1149      * VarHandle.
1150      *
1151      * @param args the signature-polymorphic parameter list of the form
1152      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1153      * , statically represented using varargs.
1154      * @return the signature-polymorphic result that is the previous value of
1155      * the variable
1156      * , statically represented using {@code Object}.
1157      * @throws UnsupportedOperationException if the access mode is unsupported
1158      * for this VarHandle.
1159      * @throws WrongMethodTypeException if the access mode type does not
1160      * match the caller's symbolic type descriptor.
1161      * @throws ClassCastException if the access mode type matches the caller's
1162      * symbolic type descriptor, but a reference cast fails.
1163      * @see #setVolatile(Object...)
1164      * @see #getVolatile(Object...)
1165      */
1166     public final native
1167     @MethodHandle.PolymorphicSignature
1168     @HotSpotIntrinsicCandidate
getAndAddAcquire(Object... args)1169     Object getAndAddAcquire(Object... args);
1170 
1171     /**
1172      * Atomically adds the {@code value} to the current value of a variable with
1173      * the memory semantics of {@link #setRelease}, and returns the variable's
1174      * previous value, as accessed with the memory semantics of
1175      * {@link #get}.
1176      *
1177      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1178      *
1179      * <p>The symbolic type descriptor at the call site of {@code getAndAddRelease}
1180      * must match the access mode type that is the result of calling
1181      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD_RELEASE)} on this
1182      * VarHandle.
1183      *
1184      * @param args the signature-polymorphic parameter list of the form
1185      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1186      * , statically represented using varargs.
1187      * @return the signature-polymorphic result that is the previous value of
1188      * the variable
1189      * , statically represented using {@code Object}.
1190      * @throws UnsupportedOperationException if the access mode is unsupported
1191      * for this VarHandle.
1192      * @throws WrongMethodTypeException if the access mode type does not
1193      * match the caller's symbolic type descriptor.
1194      * @throws ClassCastException if the access mode type matches the caller's
1195      * symbolic type descriptor, but a reference cast fails.
1196      * @see #setVolatile(Object...)
1197      * @see #getVolatile(Object...)
1198      */
1199     public final native
1200     @MethodHandle.PolymorphicSignature
1201     @HotSpotIntrinsicCandidate
getAndAddRelease(Object... args)1202     Object getAndAddRelease(Object... args);
1203 
1204 
1205     // Bitwise operations
1206     // Throw UnsupportedOperationException for refs
1207 
1208     /**
1209      * Atomically sets the value of a variable to the result of
1210      * bitwise OR between the variable's current value and the {@code mask}
1211      * with the memory semantics of {@link #setVolatile} and returns the
1212      * variable's previous value, as accessed with the memory semantics of
1213      * {@link #getVolatile}.
1214      *
1215      * <p>If the variable type is the non-integral {@code boolean} type then a
1216      * logical OR is performed instead of a bitwise OR.
1217      *
1218      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1219      *
1220      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOr}
1221      * must match the access mode type that is the result of calling
1222      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR)} on this
1223      * VarHandle.
1224      *
1225      * @param args the signature-polymorphic parameter list of the form
1226      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1227      * , statically represented using varargs.
1228      * @return the signature-polymorphic result that is the previous value of
1229      * the variable
1230      * , statically represented using {@code Object}.
1231      * @throws UnsupportedOperationException if the access mode is unsupported
1232      * for this VarHandle.
1233      * @throws WrongMethodTypeException if the access mode type does not
1234      * match the caller's symbolic type descriptor.
1235      * @throws ClassCastException if the access mode type matches the caller's
1236      * symbolic type descriptor, but a reference cast fails.
1237      * @see #setVolatile(Object...)
1238      * @see #getVolatile(Object...)
1239      */
1240     public final native
1241     @MethodHandle.PolymorphicSignature
1242     @HotSpotIntrinsicCandidate
getAndBitwiseOr(Object... args)1243     Object getAndBitwiseOr(Object... args);
1244 
1245     /**
1246      * Atomically sets the value of a variable to the result of
1247      * bitwise OR between the variable's current value and the {@code mask}
1248      * with the memory semantics of {@link #set} and returns the
1249      * variable's previous value, as accessed with the memory semantics of
1250      * {@link #getAcquire}.
1251      *
1252      * <p>If the variable type is the non-integral {@code boolean} type then a
1253      * logical OR is performed instead of a bitwise OR.
1254      *
1255      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1256      *
1257      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOrAcquire}
1258      * must match the access mode type that is the result of calling
1259      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)} on this
1260      * VarHandle.
1261      *
1262      * @param args the signature-polymorphic parameter list of the form
1263      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1264      * , statically represented using varargs.
1265      * @return the signature-polymorphic result that is the previous value of
1266      * the variable
1267      * , statically represented using {@code Object}.
1268      * @throws UnsupportedOperationException if the access mode is unsupported
1269      * for this VarHandle.
1270      * @throws WrongMethodTypeException if the access mode type does not
1271      * match the caller's symbolic type descriptor.
1272      * @throws ClassCastException if the access mode type matches the caller's
1273      * symbolic type descriptor, but a reference cast fails.
1274      * @see #set(Object...)
1275      * @see #getAcquire(Object...)
1276      */
1277     public final native
1278     @MethodHandle.PolymorphicSignature
1279     @HotSpotIntrinsicCandidate
getAndBitwiseOrAcquire(Object... args)1280     Object getAndBitwiseOrAcquire(Object... args);
1281 
1282     /**
1283      * Atomically sets the value of a variable to the result of
1284      * bitwise OR between the variable's current value and the {@code mask}
1285      * with the memory semantics of {@link #setRelease} and returns the
1286      * variable's previous value, as accessed with the memory semantics of
1287      * {@link #get}.
1288      *
1289      * <p>If the variable type is the non-integral {@code boolean} type then a
1290      * logical OR is performed instead of a bitwise OR.
1291      *
1292      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1293      *
1294      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOrRelease}
1295      * must match the access mode type that is the result of calling
1296      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)} on this
1297      * VarHandle.
1298      *
1299      * @param args the signature-polymorphic parameter list of the form
1300      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1301      * , statically represented using varargs.
1302      * @return the signature-polymorphic result that is the previous value of
1303      * the variable
1304      * , statically represented using {@code Object}.
1305      * @throws UnsupportedOperationException if the access mode is unsupported
1306      * for this VarHandle.
1307      * @throws WrongMethodTypeException if the access mode type does not
1308      * match the caller's symbolic type descriptor.
1309      * @throws ClassCastException if the access mode type matches the caller's
1310      * symbolic type descriptor, but a reference cast fails.
1311      * @see #setRelease(Object...)
1312      * @see #get(Object...)
1313      */
1314     public final native
1315     @MethodHandle.PolymorphicSignature
1316     @HotSpotIntrinsicCandidate
getAndBitwiseOrRelease(Object... args)1317     Object getAndBitwiseOrRelease(Object... args);
1318 
1319     /**
1320      * Atomically sets the value of a variable to the result of
1321      * bitwise AND between the variable's current value and the {@code mask}
1322      * with the memory semantics of {@link #setVolatile} and returns the
1323      * variable's previous value, as accessed with the memory semantics of
1324      * {@link #getVolatile}.
1325      *
1326      * <p>If the variable type is the non-integral {@code boolean} type then a
1327      * logical AND is performed instead of a bitwise AND.
1328      *
1329      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1330      *
1331      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAnd}
1332      * must match the access mode type that is the result of calling
1333      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND)} on this
1334      * VarHandle.
1335      *
1336      * @param args the signature-polymorphic parameter list of the form
1337      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1338      * , statically represented using varargs.
1339      * @return the signature-polymorphic result that is the previous value of
1340      * the variable
1341      * , statically represented using {@code Object}.
1342      * @throws UnsupportedOperationException if the access mode is unsupported
1343      * for this VarHandle.
1344      * @throws WrongMethodTypeException if the access mode type does not
1345      * match the caller's symbolic type descriptor.
1346      * @throws ClassCastException if the access mode type matches the caller's
1347      * symbolic type descriptor, but a reference cast fails.
1348      * @see #setVolatile(Object...)
1349      * @see #getVolatile(Object...)
1350      */
1351     public final native
1352     @MethodHandle.PolymorphicSignature
1353     @HotSpotIntrinsicCandidate
getAndBitwiseAnd(Object... args)1354     Object getAndBitwiseAnd(Object... args);
1355 
1356     /**
1357      * Atomically sets the value of a variable to the result of
1358      * bitwise AND between the variable's current value and the {@code mask}
1359      * with the memory semantics of {@link #set} and returns the
1360      * variable's previous value, as accessed with the memory semantics of
1361      * {@link #getAcquire}.
1362      *
1363      * <p>If the variable type is the non-integral {@code boolean} type then a
1364      * logical AND is performed instead of a bitwise AND.
1365      *
1366      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1367      *
1368      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAndAcquire}
1369      * must match the access mode type that is the result of calling
1370      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)} on this
1371      * VarHandle.
1372      *
1373      * @param args the signature-polymorphic parameter list of the form
1374      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1375      * , statically represented using varargs.
1376      * @return the signature-polymorphic result that is the previous value of
1377      * the variable
1378      * , statically represented using {@code Object}.
1379      * @throws UnsupportedOperationException if the access mode is unsupported
1380      * for this VarHandle.
1381      * @throws WrongMethodTypeException if the access mode type does not
1382      * match the caller's symbolic type descriptor.
1383      * @throws ClassCastException if the access mode type matches the caller's
1384      * symbolic type descriptor, but a reference cast fails.
1385      * @see #set(Object...)
1386      * @see #getAcquire(Object...)
1387      */
1388     public final native
1389     @MethodHandle.PolymorphicSignature
1390     @HotSpotIntrinsicCandidate
getAndBitwiseAndAcquire(Object... args)1391     Object getAndBitwiseAndAcquire(Object... args);
1392 
1393     /**
1394      * Atomically sets the value of a variable to the result of
1395      * bitwise AND between the variable's current value and the {@code mask}
1396      * with the memory semantics of {@link #setRelease} and returns the
1397      * variable's previous value, as accessed with the memory semantics of
1398      * {@link #get}.
1399      *
1400      * <p>If the variable type is the non-integral {@code boolean} type then a
1401      * logical AND is performed instead of a bitwise AND.
1402      *
1403      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1404      *
1405      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAndRelease}
1406      * must match the access mode type that is the result of calling
1407      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)} on this
1408      * VarHandle.
1409      *
1410      * @param args the signature-polymorphic parameter list of the form
1411      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1412      * , statically represented using varargs.
1413      * @return the signature-polymorphic result that is the previous value of
1414      * the variable
1415      * , statically represented using {@code Object}.
1416      * @throws UnsupportedOperationException if the access mode is unsupported
1417      * for this VarHandle.
1418      * @throws WrongMethodTypeException if the access mode type does not
1419      * match the caller's symbolic type descriptor.
1420      * @throws ClassCastException if the access mode type matches the caller's
1421      * symbolic type descriptor, but a reference cast fails.
1422      * @see #setRelease(Object...)
1423      * @see #get(Object...)
1424      */
1425     public final native
1426     @MethodHandle.PolymorphicSignature
1427     @HotSpotIntrinsicCandidate
getAndBitwiseAndRelease(Object... args)1428     Object getAndBitwiseAndRelease(Object... args);
1429 
1430     /**
1431      * Atomically sets the value of a variable to the result of
1432      * bitwise XOR between the variable's current value and the {@code mask}
1433      * with the memory semantics of {@link #setVolatile} and returns the
1434      * variable's previous value, as accessed with the memory semantics of
1435      * {@link #getVolatile}.
1436      *
1437      * <p>If the variable type is the non-integral {@code boolean} type then a
1438      * logical XOR is performed instead of a bitwise XOR.
1439      *
1440      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1441      *
1442      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXor}
1443      * must match the access mode type that is the result of calling
1444      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR)} on this
1445      * VarHandle.
1446      *
1447      * @param args the signature-polymorphic parameter list of the form
1448      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1449      * , statically represented using varargs.
1450      * @return the signature-polymorphic result that is the previous value of
1451      * the variable
1452      * , statically represented using {@code Object}.
1453      * @throws UnsupportedOperationException if the access mode is unsupported
1454      * for this VarHandle.
1455      * @throws WrongMethodTypeException if the access mode type does not
1456      * match the caller's symbolic type descriptor.
1457      * @throws ClassCastException if the access mode type matches the caller's
1458      * symbolic type descriptor, but a reference cast fails.
1459      * @see #setVolatile(Object...)
1460      * @see #getVolatile(Object...)
1461      */
1462     public final native
1463     @MethodHandle.PolymorphicSignature
1464     @HotSpotIntrinsicCandidate
getAndBitwiseXor(Object... args)1465     Object getAndBitwiseXor(Object... args);
1466 
1467     /**
1468      * Atomically sets the value of a variable to the result of
1469      * bitwise XOR between the variable's current value and the {@code mask}
1470      * with the memory semantics of {@link #set} and returns the
1471      * variable's previous value, as accessed with the memory semantics of
1472      * {@link #getAcquire}.
1473      *
1474      * <p>If the variable type is the non-integral {@code boolean} type then a
1475      * logical XOR is performed instead of a bitwise XOR.
1476      *
1477      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1478      *
1479      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXorAcquire}
1480      * must match the access mode type that is the result of calling
1481      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)} on this
1482      * VarHandle.
1483      *
1484      * @param args the signature-polymorphic parameter list of the form
1485      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1486      * , statically represented using varargs.
1487      * @return the signature-polymorphic result that is the previous value of
1488      * the variable
1489      * , statically represented using {@code Object}.
1490      * @throws UnsupportedOperationException if the access mode is unsupported
1491      * for this VarHandle.
1492      * @throws WrongMethodTypeException if the access mode type does not
1493      * match the caller's symbolic type descriptor.
1494      * @throws ClassCastException if the access mode type matches the caller's
1495      * symbolic type descriptor, but a reference cast fails.
1496      * @see #set(Object...)
1497      * @see #getAcquire(Object...)
1498      */
1499     public final native
1500     @MethodHandle.PolymorphicSignature
1501     @HotSpotIntrinsicCandidate
getAndBitwiseXorAcquire(Object... args)1502     Object getAndBitwiseXorAcquire(Object... args);
1503 
1504     /**
1505      * Atomically sets the value of a variable to the result of
1506      * bitwise XOR between the variable's current value and the {@code mask}
1507      * with the memory semantics of {@link #setRelease} and returns the
1508      * variable's previous value, as accessed with the memory semantics of
1509      * {@link #get}.
1510      *
1511      * <p>If the variable type is the non-integral {@code boolean} type then a
1512      * logical XOR is performed instead of a bitwise XOR.
1513      *
1514      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1515      *
1516      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXorRelease}
1517      * must match the access mode type that is the result of calling
1518      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE)} on this
1519      * VarHandle.
1520      *
1521      * @param args the signature-polymorphic parameter list of the form
1522      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1523      * , statically represented using varargs.
1524      * @return the signature-polymorphic result that is the previous value of
1525      * the variable
1526      * , statically represented using {@code Object}.
1527      * @throws UnsupportedOperationException if the access mode is unsupported
1528      * for this VarHandle.
1529      * @throws WrongMethodTypeException if the access mode type does not
1530      * match the caller's symbolic type descriptor.
1531      * @throws ClassCastException if the access mode type matches the caller's
1532      * symbolic type descriptor, but a reference cast fails.
1533      * @see #setRelease(Object...)
1534      * @see #get(Object...)
1535      */
1536     public final native
1537     @MethodHandle.PolymorphicSignature
1538     @HotSpotIntrinsicCandidate
getAndBitwiseXorRelease(Object... args)1539     Object getAndBitwiseXorRelease(Object... args);
1540 
1541 
1542     // Android-changed: remove unused return type in AccessType constructor.
1543     enum AccessType {
1544         GET,
1545         SET,
1546         COMPARE_AND_SET,
1547         COMPARE_AND_EXCHANGE,
1548         GET_AND_UPDATE,
1549         // Android-added: Finer grained access types.
1550         // These are used to help categorize the access modes that a VarHandle supports.
1551         GET_AND_UPDATE_BITWISE,
1552         GET_AND_UPDATE_NUMERIC;
1553 
accessModeType(Class<?> receiver, Class<?> value, Class<?>... intermediate)1554         MethodType accessModeType(Class<?> receiver, Class<?> value,
1555                                   Class<?>... intermediate) {
1556             Class<?>[] ps;
1557             int i;
1558             switch (this) {
1559                 case GET:
1560                     ps = allocateParameters(0, receiver, intermediate);
1561                     fillParameters(ps, receiver, intermediate);
1562                     return MethodType.methodType(value, ps);
1563                 case SET:
1564                     ps = allocateParameters(1, receiver, intermediate);
1565                     i = fillParameters(ps, receiver, intermediate);
1566                     ps[i] = value;
1567                     return MethodType.methodType(void.class, ps);
1568                 case COMPARE_AND_SET:
1569                     ps = allocateParameters(2, receiver, intermediate);
1570                     i = fillParameters(ps, receiver, intermediate);
1571                     ps[i++] = value;
1572                     ps[i] = value;
1573                     return MethodType.methodType(boolean.class, ps);
1574                 case COMPARE_AND_EXCHANGE:
1575                     ps = allocateParameters(2, receiver, intermediate);
1576                     i = fillParameters(ps, receiver, intermediate);
1577                     ps[i++] = value;
1578                     ps[i] = value;
1579                     return MethodType.methodType(value, ps);
1580                 case GET_AND_UPDATE:
1581                 case GET_AND_UPDATE_BITWISE:
1582                 case GET_AND_UPDATE_NUMERIC:
1583                     ps = allocateParameters(1, receiver, intermediate);
1584                     i = fillParameters(ps, receiver, intermediate);
1585                     ps[i] = value;
1586                     return MethodType.methodType(value, ps);
1587                 default:
1588                     throw new InternalError("Unknown AccessType");
1589             }
1590         }
1591 
allocateParameters(int values, Class<?> receiver, Class<?>... intermediate)1592         private static Class<?>[] allocateParameters(int values,
1593                                                      Class<?> receiver, Class<?>... intermediate) {
1594             int size = ((receiver != null) ? 1 : 0) + intermediate.length + values;
1595             return new Class<?>[size];
1596         }
1597 
fillParameters(Class<?>[] ps, Class<?> receiver, Class<?>... intermediate)1598         private static int fillParameters(Class<?>[] ps,
1599                                           Class<?> receiver, Class<?>... intermediate) {
1600             int i = 0;
1601             if (receiver != null)
1602                 ps[i++] = receiver;
1603             for (int j = 0; j < intermediate.length; j++)
1604                 ps[i++] = intermediate[j];
1605             return i;
1606         }
1607     }
1608 
1609     /**
1610      * The set of access modes that specify how a variable, referenced by a
1611      * VarHandle, is accessed.
1612      */
1613     public enum AccessMode {
1614         /**
1615          * The access mode whose access is specified by the corresponding
1616          * method
1617          * {@link VarHandle#get VarHandle.get}
1618          */
1619         GET("get", AccessType.GET),
1620         /**
1621          * The access mode whose access is specified by the corresponding
1622          * method
1623          * {@link VarHandle#set VarHandle.set}
1624          */
1625         SET("set", AccessType.SET),
1626         /**
1627          * The access mode whose access is specified by the corresponding
1628          * method
1629          * {@link VarHandle#getVolatile VarHandle.getVolatile}
1630          */
1631         GET_VOLATILE("getVolatile", AccessType.GET),
1632         /**
1633          * The access mode whose access is specified by the corresponding
1634          * method
1635          * {@link VarHandle#setVolatile VarHandle.setVolatile}
1636          */
1637         SET_VOLATILE("setVolatile", AccessType.SET),
1638         /**
1639          * The access mode whose access is specified by the corresponding
1640          * method
1641          * {@link VarHandle#getAcquire VarHandle.getAcquire}
1642          */
1643         GET_ACQUIRE("getAcquire", AccessType.GET),
1644         /**
1645          * The access mode whose access is specified by the corresponding
1646          * method
1647          * {@link VarHandle#setRelease VarHandle.setRelease}
1648          */
1649         SET_RELEASE("setRelease", AccessType.SET),
1650         /**
1651          * The access mode whose access is specified by the corresponding
1652          * method
1653          * {@link VarHandle#getOpaque VarHandle.getOpaque}
1654          */
1655         GET_OPAQUE("getOpaque", AccessType.GET),
1656         /**
1657          * The access mode whose access is specified by the corresponding
1658          * method
1659          * {@link VarHandle#setOpaque VarHandle.setOpaque}
1660          */
1661         SET_OPAQUE("setOpaque", AccessType.SET),
1662         /**
1663          * The access mode whose access is specified by the corresponding
1664          * method
1665          * {@link VarHandle#compareAndSet VarHandle.compareAndSet}
1666          */
1667         COMPARE_AND_SET("compareAndSet", AccessType.COMPARE_AND_SET),
1668         /**
1669          * The access mode whose access is specified by the corresponding
1670          * method
1671          * {@link VarHandle#compareAndExchange VarHandle.compareAndExchange}
1672          */
1673         COMPARE_AND_EXCHANGE("compareAndExchange", AccessType.COMPARE_AND_EXCHANGE),
1674         /**
1675          * The access mode whose access is specified by the corresponding
1676          * method
1677          * {@link VarHandle#compareAndExchangeAcquire VarHandle.compareAndExchangeAcquire}
1678          */
1679         COMPARE_AND_EXCHANGE_ACQUIRE("compareAndExchangeAcquire", AccessType.COMPARE_AND_EXCHANGE),
1680         /**
1681          * The access mode whose access is specified by the corresponding
1682          * method
1683          * {@link VarHandle#compareAndExchangeRelease VarHandle.compareAndExchangeRelease}
1684          */
1685         COMPARE_AND_EXCHANGE_RELEASE("compareAndExchangeRelease", AccessType.COMPARE_AND_EXCHANGE),
1686         /**
1687          * The access mode whose access is specified by the corresponding
1688          * method
1689          * {@link VarHandle#weakCompareAndSetPlain VarHandle.weakCompareAndSetPlain}
1690          */
1691         WEAK_COMPARE_AND_SET_PLAIN("weakCompareAndSetPlain", AccessType.COMPARE_AND_SET),
1692         /**
1693          * The access mode whose access is specified by the corresponding
1694          * method
1695          * {@link VarHandle#weakCompareAndSet VarHandle.weakCompareAndSet}
1696          */
1697         WEAK_COMPARE_AND_SET("weakCompareAndSet", AccessType.COMPARE_AND_SET),
1698         /**
1699          * The access mode whose access is specified by the corresponding
1700          * method
1701          * {@link VarHandle#weakCompareAndSetAcquire VarHandle.weakCompareAndSetAcquire}
1702          */
1703         WEAK_COMPARE_AND_SET_ACQUIRE("weakCompareAndSetAcquire", AccessType.COMPARE_AND_SET),
1704         /**
1705          * The access mode whose access is specified by the corresponding
1706          * method
1707          * {@link VarHandle#weakCompareAndSetRelease VarHandle.weakCompareAndSetRelease}
1708          */
1709         WEAK_COMPARE_AND_SET_RELEASE("weakCompareAndSetRelease", AccessType.COMPARE_AND_SET),
1710         /**
1711          * The access mode whose access is specified by the corresponding
1712          * method
1713          * {@link VarHandle#getAndSet VarHandle.getAndSet}
1714          */
1715         GET_AND_SET("getAndSet", AccessType.GET_AND_UPDATE),
1716         /**
1717          * The access mode whose access is specified by the corresponding
1718          * method
1719          * {@link VarHandle#getAndSetAcquire VarHandle.getAndSetAcquire}
1720          */
1721         GET_AND_SET_ACQUIRE("getAndSetAcquire", AccessType.GET_AND_UPDATE),
1722         /**
1723          * The access mode whose access is specified by the corresponding
1724          * method
1725          * {@link VarHandle#getAndSetRelease VarHandle.getAndSetRelease}
1726          */
1727         GET_AND_SET_RELEASE("getAndSetRelease", AccessType.GET_AND_UPDATE),
1728         /**
1729          * The access mode whose access is specified by the corresponding
1730          * method
1731          * {@link VarHandle#getAndAdd VarHandle.getAndAdd}
1732          */
1733         GET_AND_ADD("getAndAdd", AccessType.GET_AND_UPDATE_NUMERIC),
1734         /**
1735          * The access mode whose access is specified by the corresponding
1736          * method
1737          * {@link VarHandle#getAndAddAcquire VarHandle.getAndAddAcquire}
1738          */
1739         GET_AND_ADD_ACQUIRE("getAndAddAcquire", AccessType.GET_AND_UPDATE_NUMERIC),
1740         /**
1741          * The access mode whose access is specified by the corresponding
1742          * method
1743          * {@link VarHandle#getAndAddRelease VarHandle.getAndAddRelease}
1744          */
1745         GET_AND_ADD_RELEASE("getAndAddRelease", AccessType.GET_AND_UPDATE_NUMERIC),
1746         /**
1747          * The access mode whose access is specified by the corresponding
1748          * method
1749          * {@link VarHandle#getAndBitwiseOr VarHandle.getAndBitwiseOr}
1750          */
1751         GET_AND_BITWISE_OR("getAndBitwiseOr", AccessType.GET_AND_UPDATE_BITWISE),
1752         /**
1753          * The access mode whose access is specified by the corresponding
1754          * method
1755          * {@link VarHandle#getAndBitwiseOrRelease VarHandle.getAndBitwiseOrRelease}
1756          */
1757         GET_AND_BITWISE_OR_RELEASE("getAndBitwiseOrRelease", AccessType.GET_AND_UPDATE_BITWISE),
1758         /**
1759          * The access mode whose access is specified by the corresponding
1760          * method
1761          * {@link VarHandle#getAndBitwiseOrAcquire VarHandle.getAndBitwiseOrAcquire}
1762          */
1763         GET_AND_BITWISE_OR_ACQUIRE("getAndBitwiseOrAcquire", AccessType.GET_AND_UPDATE_BITWISE),
1764         /**
1765          * The access mode whose access is specified by the corresponding
1766          * method
1767          * {@link VarHandle#getAndBitwiseAnd VarHandle.getAndBitwiseAnd}
1768          */
1769         GET_AND_BITWISE_AND("getAndBitwiseAnd", AccessType.GET_AND_UPDATE_BITWISE),
1770         /**
1771          * The access mode whose access is specified by the corresponding
1772          * method
1773          * {@link VarHandle#getAndBitwiseAndRelease VarHandle.getAndBitwiseAndRelease}
1774          */
1775         GET_AND_BITWISE_AND_RELEASE("getAndBitwiseAndRelease", AccessType.GET_AND_UPDATE_BITWISE),
1776         /**
1777          * The access mode whose access is specified by the corresponding
1778          * method
1779          * {@link VarHandle#getAndBitwiseAndAcquire VarHandle.getAndBitwiseAndAcquire}
1780          */
1781         GET_AND_BITWISE_AND_ACQUIRE("getAndBitwiseAndAcquire", AccessType.GET_AND_UPDATE_BITWISE),
1782         /**
1783          * The access mode whose access is specified by the corresponding
1784          * method
1785          * {@link VarHandle#getAndBitwiseXor VarHandle.getAndBitwiseXor}
1786          */
1787         GET_AND_BITWISE_XOR("getAndBitwiseXor", AccessType.GET_AND_UPDATE_BITWISE),
1788         /**
1789          * The access mode whose access is specified by the corresponding
1790          * method
1791          * {@link VarHandle#getAndBitwiseXorRelease VarHandle.getAndBitwiseXorRelease}
1792          */
1793         GET_AND_BITWISE_XOR_RELEASE("getAndBitwiseXorRelease", AccessType.GET_AND_UPDATE_BITWISE),
1794         /**
1795          * The access mode whose access is specified by the corresponding
1796          * method
1797          * {@link VarHandle#getAndBitwiseXorAcquire VarHandle.getAndBitwiseXorAcquire}
1798          */
1799         GET_AND_BITWISE_XOR_ACQUIRE("getAndBitwiseXorAcquire", AccessType.GET_AND_UPDATE_BITWISE),
1800         ;
1801 
1802         static final Map<String, AccessMode> methodNameToAccessMode;
1803         static {
1804             AccessMode[] values = AccessMode.values();
1805             // Initial capacity of # values divided by the load factor is sufficient
1806             // to avoid resizes for the smallest table size (64)
1807             int initialCapacity = (int)(values.length / 0.75f) + 1;
1808             methodNameToAccessMode = new HashMap<>(initialCapacity);
1809             for (AccessMode am : values) {
methodNameToAccessMode.put(am.methodName, am)1810                 methodNameToAccessMode.put(am.methodName, am);
1811             }
1812         }
1813 
1814         final String methodName;
1815         final AccessType at;
1816 
AccessMode(final String methodName, AccessType at)1817         AccessMode(final String methodName, AccessType at) {
1818             this.methodName = methodName;
1819             this.at = at;
1820         }
1821 
1822         /**
1823          * Returns the {@code VarHandle} signature-polymorphic method name
1824          * associated with this {@code AccessMode} value.
1825          *
1826          * @return the signature-polymorphic method name
1827          * @see #valueFromMethodName
1828          */
methodName()1829         public String methodName() {
1830             return methodName;
1831         }
1832 
1833         /**
1834          * Returns the {@code AccessMode} value associated with the specified
1835          * {@code VarHandle} signature-polymorphic method name.
1836          *
1837          * @param methodName the signature-polymorphic method name
1838          * @return the {@code AccessMode} value
1839          * @throws IllegalArgumentException if there is no {@code AccessMode}
1840          *         value associated with method name (indicating the method
1841          *         name does not correspond to a {@code VarHandle}
1842          *         signature-polymorphic method name).
1843          * @see #methodName()
1844          */
valueFromMethodName(String methodName)1845         public static AccessMode valueFromMethodName(String methodName) {
1846             AccessMode am = methodNameToAccessMode.get(methodName);
1847             if (am != null) return am;
1848             throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
1849         }
1850 
1851         // BEGIN Android-removed: MemberName and VarForm are not used in the Android implementation.
1852         /*
1853         @ForceInline
1854         static MemberName getMemberName(int ordinal, VarForm vform) {
1855             return vform.memberName_table[ordinal];
1856         }
1857         */
1858         // END Android-removed: MemberName and VarForm are not used in the Android implementation.
1859     }
1860 
1861     // BEGIN Android-removed: AccessDescriptor not used in Android implementation.
1862     /*
1863     static final class AccessDescriptor {
1864         final MethodType symbolicMethodTypeErased;
1865         final MethodType symbolicMethodTypeInvoker;
1866         final Class<?> returnType;
1867         final int type;
1868         final int mode;
1869 
1870         public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) {
1871             this.symbolicMethodTypeErased = symbolicMethodType.erase();
1872             this.symbolicMethodTypeInvoker = symbolicMethodType.insertParameterTypes(0, VarHandle.class);
1873             this.returnType = symbolicMethodType.returnType();
1874             this.type = type;
1875             this.mode = mode;
1876         }
1877     }
1878     */
1879     // END Android-removed: AccessDescriptor not used in Android implementation.
1880 
1881     /**
1882      * Returns the variable type of variables referenced by this VarHandle.
1883      *
1884      * @return the variable type of variables referenced by this VarHandle
1885      */
varType()1886     public final Class<?> varType() {
1887         // Android-removed: existing implementation.
1888         // MethodType typeSet = accessModeType(AccessMode.SET);
1889         // return typeSet.parameterType(typeSet.parameterCount() - 1)
1890         // Android-added: return instance field.
1891         return varType;
1892     }
1893 
1894     /**
1895      * Returns the coordinate types for this VarHandle.
1896      *
1897      * @return the coordinate types for this VarHandle. The returned
1898      * list is unmodifiable
1899      */
coordinateTypes()1900     public final List<Class<?>> coordinateTypes() {
1901         // Android-removed: existing implementation.
1902         // MethodType typeGet = accessModeType(AccessMode.GET);
1903         // return typeGet.parameterList();
1904         // Android-added: Android specific implementation.
1905         if (coordinateType0 == null) {
1906             return Collections.EMPTY_LIST;
1907         } else if (coordinateType1 == null) {
1908             return Collections.singletonList(coordinateType0);
1909         } else {
1910             return Collections.unmodifiableList(Arrays.asList(coordinateType0, coordinateType1));
1911         }
1912     }
1913 
1914     /**
1915      * Obtains the access mode type for this VarHandle and a given access mode.
1916      *
1917      * <p>The access mode type's parameter types will consist of a prefix that
1918      * is the coordinate types of this VarHandle followed by further
1919      * types as defined by the access mode method.
1920      * The access mode type's return type is defined by the return type of the
1921      * access mode method.
1922      *
1923      * @param accessMode the access mode, corresponding to the
1924      * signature-polymorphic method of the same name
1925      * @return the access mode type for the given access mode
1926      */
accessModeType(AccessMode accessMode)1927     public final MethodType accessModeType(AccessMode accessMode) {
1928         // BEGIN Android-removed: Relies on internal class that is not part of the
1929         // Android implementation.
1930         /*
1931         TypesAndInvokers tis = getTypesAndInvokers();
1932         MethodType mt = tis.methodType_table[accessMode.at.ordinal()];
1933         if (mt == null) {
1934             mt = tis.methodType_table[accessMode.at.ordinal()] =
1935                     accessModeTypeUncached(accessMode);
1936         }
1937         return mt;
1938         */
1939         // END Android-removed: Relies on internal class that is not part of the
1940         // Android implementation.
1941         // Android-added: alternative implementation.
1942         if (coordinateType1 == null) {
1943             // accessModeType() treats the first argument as the
1944             // receiver and adapts accordingly if it is null.
1945             return accessMode.at.accessModeType(coordinateType0, varType);
1946         } else {
1947             return accessMode.at.accessModeType(coordinateType0, varType, coordinateType1);
1948         }
1949     }
1950 
1951     // Android-removed: Not part of the Android implementation.
1952     // abstract MethodType accessModeTypeUncached(AccessMode accessMode);
1953 
1954     /**
1955      * Returns {@code true} if the given access mode is supported, otherwise
1956      * {@code false}.
1957      *
1958      * <p>The return of a {@code false} value for a given access mode indicates
1959      * that an {@code UnsupportedOperationException} is thrown on invocation
1960      * of the corresponding access mode method.
1961      *
1962      * @param accessMode the access mode, corresponding to the
1963      * signature-polymorphic method of the same name
1964      * @return {@code true} if the given access mode is supported, otherwise
1965      * {@code false}.
1966      */
isAccessModeSupported(AccessMode accessMode)1967     public final boolean isAccessModeSupported(AccessMode accessMode) {
1968         // Android-removed: Refers to unused field vform.
1969         // return AccessMode.getMemberName(accessMode.ordinal(), vform) != null;
1970         // Android-added: use accessModesBitsMask field.
1971         final int testBit = 1 << accessMode.ordinal();
1972         return (accessModesBitMask & testBit) == testBit;
1973     }
1974 
1975     /**
1976      * Obtains a method handle bound to this VarHandle and the given access
1977      * mode.
1978      *
1979      * @apiNote This method, for a VarHandle {@code vh} and access mode
1980      * {@code {access-mode}}, returns a method handle that is equivalent to
1981      * method handle {@code bmh} in the following code (though it may be more
1982      * efficient):
1983      * <pre>{@code
1984      * MethodHandle mh = MethodHandles.varHandleExactInvoker(
1985      *                       vh.accessModeType(VarHandle.AccessMode.{access-mode}));
1986      *
1987      * MethodHandle bmh = mh.bindTo(vh);
1988      * }</pre>
1989      *
1990      * @param accessMode the access mode, corresponding to the
1991      * signature-polymorphic method of the same name
1992      * @return a method handle bound to this VarHandle and the given access mode
1993      */
toMethodHandle(AccessMode accessMode)1994     public final MethodHandle toMethodHandle(AccessMode accessMode) {
1995         // BEGIN Android-removed: no vform field in Android implementation.
1996         /*
1997         MemberName mn = AccessMode.getMemberName(accessMode.ordinal(), vform);
1998         if (mn != null) {
1999             MethodHandle mh = getMethodHandle(accessMode.ordinal());
2000             return mh.bindTo(this);
2001         }
2002         else {
2003             // Ensure an UnsupportedOperationException is thrown
2004             return MethodHandles.varHandleInvoker(accessMode, accessModeType(accessMode)).
2005                     bindTo(this);
2006         }
2007         */
2008         // END Android-removed: no vform field in Android implementation.
2009 
2010         // Android-added: basic implementation following description in javadoc for this method.
2011         MethodType type = accessModeType(accessMode);
2012         return MethodHandles.varHandleExactInvoker(accessMode, type).bindTo(this);
2013     }
2014 
2015     // BEGIN Android-removed: Not used in Android implementation.
2016     /*
2017     @Stable
2018     TypesAndInvokers typesAndInvokers;
2019 
2020     static class TypesAndInvokers {
2021         final @Stable
2022         MethodType[] methodType_table =
2023                 new MethodType[VarHandle.AccessType.values().length];
2024 
2025         final @Stable
2026         MethodHandle[] methodHandle_table =
2027                 new MethodHandle[AccessMode.values().length];
2028     }
2029 
2030     @ForceInline
2031     private final TypesAndInvokers getTypesAndInvokers() {
2032         TypesAndInvokers tis = typesAndInvokers;
2033         if (tis == null) {
2034             tis = typesAndInvokers = new TypesAndInvokers();
2035         }
2036         return tis;
2037     }
2038 
2039     @ForceInline
2040     final MethodHandle getMethodHandle(int mode) {
2041         TypesAndInvokers tis = getTypesAndInvokers();
2042         MethodHandle mh = tis.methodHandle_table[mode];
2043         if (mh == null) {
2044             mh = tis.methodHandle_table[mode] = getMethodHandleUncached(mode);
2045         }
2046         return mh;
2047     }
2048     private final MethodHandle getMethodHandleUncached(int mode) {
2049         MethodType mt = accessModeType(AccessMode.values()[mode]).
2050                 insertParameterTypes(0, VarHandle.class);
2051         MemberName mn = vform.getMemberName(mode);
2052         DirectMethodHandle dmh = DirectMethodHandle.make(mn);
2053         // Such a method handle must not be publically exposed directly
2054         // otherwise it can be cracked, it must be transformed or rebound
2055         // before exposure
2056         MethodHandle mh = dmh.copyWith(mt, dmh.form);
2057         assert mh.type().erase() == mn.getMethodType().erase();
2058         return mh;
2059     }
2060     */
2061     // END Android-removed: Not used in Android implementation.
2062 
2063     // BEGIN Android-removed: No VarForm in Android implementation.
2064     /*non-public*/
2065     /*
2066     final void updateVarForm(VarForm newVForm) {
2067         if (vform == newVForm) return;
2068         UNSAFE.putObject(this, VFORM_OFFSET, newVForm);
2069         UNSAFE.fullFence();
2070     }
2071 
2072     static final BiFunction<String, List<Integer>, ArrayIndexOutOfBoundsException>
2073             AIOOBE_SUPPLIER = Preconditions.outOfBoundsExceptionFormatter(
2074             new Function<String, ArrayIndexOutOfBoundsException>() {
2075                 @Override
2076                 public ArrayIndexOutOfBoundsException apply(String s) {
2077                     return new ArrayIndexOutOfBoundsException(s);
2078                 }
2079             });
2080 
2081     private static final long VFORM_OFFSET;
2082 
2083     static {
2084         VFORM_OFFSET = UNSAFE.objectFieldOffset(VarHandle.class, "vform");
2085 
2086         // The VarHandleGuards must be initialized to ensure correct
2087         // compilation of the guard methods
2088         UNSAFE.ensureClassInitialized(VarHandleGuards.class);
2089     }
2090     */
2091     // END Android-removed: No VarForm in Android implementation.
2092 
2093     // Fence methods
2094 
2095     /**
2096      * Ensures that loads and stores before the fence will not be reordered
2097      * with
2098      * loads and stores after the fence.
2099      *
2100      * @apiNote Ignoring the many semantic differences from C and C++, this
2101      * method has memory ordering effects compatible with
2102      * {@code atomic_thread_fence(memory_order_seq_cst)}
2103      */
2104     // Android-removed: @ForceInline is an unsupported attribute.
2105     // @ForceInline
fullFence()2106     public static void fullFence() {
2107         UNSAFE.fullFence();
2108     }
2109 
2110     /**
2111      * Ensures that loads before the fence will not be reordered with loads and
2112      * stores after the fence.
2113      *
2114      * @apiNote Ignoring the many semantic differences from C and C++, this
2115      * method has memory ordering effects compatible with
2116      * {@code atomic_thread_fence(memory_order_acquire)}
2117      */
2118     // Android-removed: @ForceInline is an unsupported attribute.
2119     // @ForceInline
acquireFence()2120     public static void acquireFence() {
2121         UNSAFE.loadFence();
2122     }
2123 
2124     /**
2125      * Ensures that loads and stores before the fence will not be
2126      * reordered with stores after the fence.
2127      *
2128      * @apiNote Ignoring the many semantic differences from C and C++, this
2129      * method has memory ordering effects compatible with
2130      * {@code atomic_thread_fence(memory_order_release)}
2131      */
2132     // Android-removed: @ForceInline is an unsupported attribute.
2133     // @ForceInline
releaseFence()2134     public static void releaseFence() {
2135         UNSAFE.storeFence();
2136     }
2137 
2138     /**
2139      * Ensures that loads before the fence will not be reordered with
2140      * loads after the fence.
2141      */
2142     // Android-removed: @ForceInline is an unsupported attribute.
2143     // @ForceInline
loadLoadFence()2144     public static void loadLoadFence() {
2145         // Android-changed: Not using UNSAFE.loadLoadFence() as not present on Android.
2146         // NB The compiler recognizes all the fences here as intrinsics.
2147         UNSAFE.loadFence();
2148     }
2149 
2150     /**
2151      * Ensures that stores before the fence will not be reordered with
2152      * stores after the fence.
2153      */
2154     // Android-removed: @ForceInline is an unsupported attribute.
2155     // @ForceInline
storeStoreFence()2156     public static void storeStoreFence() {
2157         // Android-changed: Not using UNSAFE.storeStoreFence() as not present on Android.
2158         // NB The compiler recognizes all the fences here as intrinsics.
2159         UNSAFE.storeFence();
2160     }
2161 
2162     // BEGIN Android-added: package private constructors.
2163     /**
2164      * Constructor for VarHandle with no coordinates.
2165      *
2166      * @param varType the variable type of variables to be referenced
2167      * @param isFinal whether the target variables are final (non-modifiable)
2168      * @hide
2169      */
VarHandle(Class<?> varType, boolean isFinal)2170     VarHandle(Class<?> varType, boolean isFinal) {
2171         this.varType = Objects.requireNonNull(varType);
2172         this.coordinateType0 = null;
2173         this.coordinateType1 = null;
2174         this.accessModesBitMask = alignedAccessModesBitMask(varType, isFinal);
2175     }
2176 
2177     /**
2178      * Constructor for VarHandle with one coordinate.
2179      *
2180      * @param varType the variable type of variables to be referenced
2181      * @param isFinal  whether the target variables are final (non-modifiable)
2182      * @param coordinateType the coordinate
2183      * @hide
2184      */
VarHandle(Class<?> varType, boolean isFinal, Class<?> coordinateType)2185     VarHandle(Class<?> varType, boolean isFinal, Class<?> coordinateType) {
2186         this.varType = Objects.requireNonNull(varType);
2187         this.coordinateType0 = Objects.requireNonNull(coordinateType);
2188         this.coordinateType1 = null;
2189         this.accessModesBitMask = alignedAccessModesBitMask(varType, isFinal);
2190     }
2191 
2192     /**
2193      * Constructor for VarHandle with two coordinates.
2194      *
2195      * @param varType the variable type of variables to be referenced
2196      * @param backingArrayType the type of the array accesses will be performed on
2197      * @param isFinal whether the target variables are final (non-modifiable)
2198      * @param coordinateType0 the first coordinate
2199      * @param coordinateType1 the second coordinate
2200      * @hide
2201      */
VarHandle(Class<?> varType, Class<?> backingArrayType, boolean isFinal, Class<?> coordinateType0, Class<?> coordinateType1)2202     VarHandle(Class<?> varType, Class<?> backingArrayType,  boolean isFinal,
2203               Class<?> coordinateType0, Class<?> coordinateType1) {
2204         this.varType = Objects.requireNonNull(varType);
2205         this.coordinateType0 = Objects.requireNonNull(coordinateType0);
2206         this.coordinateType1 = Objects.requireNonNull(coordinateType1);
2207         Objects.requireNonNull(backingArrayType);
2208         Class<?> backingArrayComponentType = backingArrayType.getComponentType();
2209         if (backingArrayComponentType != varType && backingArrayComponentType != byte.class) {
2210             throw new InternalError("Unsupported backingArrayType: " + backingArrayType);
2211         }
2212 
2213         if (backingArrayType.getComponentType() == varType) {
2214             this.accessModesBitMask = alignedAccessModesBitMask(varType, isFinal);
2215         } else {
2216             this.accessModesBitMask = unalignedAccessModesBitMask(varType);
2217         }
2218     }
2219     // END Android-added: package private constructors.
2220 
2221     // BEGIN Android-added: helper state for VarHandle properties.
2222 
2223     /** BitMask of access modes that do not change the memory referenced by a VarHandle.
2224      * An example being a read of a variable with volatile ordering effects. */
2225     private final static int READ_ACCESS_MODES_BIT_MASK;
2226 
2227     /** BitMask of access modes that write to the memory referenced by
2228      * a VarHandle.  This does not include any compare and update
2229      * access modes, nor any bitwise or numeric access modes. An
2230      * example being a write to variable with release ordering
2231      * effects.
2232      */
2233     private final static int WRITE_ACCESS_MODES_BIT_MASK;
2234 
2235     /** BitMask of access modes that are applicable to types
2236      * supporting for atomic updates.  This includes access modes that
2237      * both read and write a variable such as compare-and-set.
2238      */
2239     private final static int ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2240 
2241     /** BitMask of access modes that are applicable to types
2242      * supporting numeric atomic update operations. */
2243     private final static int NUMERIC_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2244 
2245     /** BitMask of access modes that are applicable to types
2246      * supporting bitwise atomic update operations. */
2247     private final static int BITWISE_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2248 
2249     /** BitMask of all access modes. */
2250     private final static int ALL_MODES_BIT_MASK;
2251 
2252     static {
2253         // Check we're not about to overflow the storage of the
2254         // bitmasks here and in the accessModesBitMask field.
2255         if (AccessMode.values().length > Integer.SIZE) {
2256             throw new InternalError("accessModes overflow");
2257         }
2258 
2259         // Access modes bit mask declarations and initialization order
2260         // follows the presentation order in JEP193.
2261         READ_ACCESS_MODES_BIT_MASK = accessTypesToBitMask(EnumSet.of(AccessType.GET));
2262 
2263         WRITE_ACCESS_MODES_BIT_MASK = accessTypesToBitMask(EnumSet.of(AccessType.SET));
2264 
2265         ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK =
2266                 accessTypesToBitMask(EnumSet.of(AccessType.COMPARE_AND_EXCHANGE,
2267                                                 AccessType.COMPARE_AND_SET,
2268                                                 AccessType.GET_AND_UPDATE));
2269 
2270         NUMERIC_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK =
2271                 accessTypesToBitMask(EnumSet.of(AccessType.GET_AND_UPDATE_NUMERIC));
2272 
2273         BITWISE_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK =
2274                 accessTypesToBitMask(EnumSet.of(AccessType.GET_AND_UPDATE_BITWISE));
2275 
2276         ALL_MODES_BIT_MASK = (READ_ACCESS_MODES_BIT_MASK |
2277                               WRITE_ACCESS_MODES_BIT_MASK |
2278                               ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK |
2279                               NUMERIC_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK |
2280                               BITWISE_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK);
2281     }
2282 
accessTypesToBitMask(final EnumSet<AccessType> accessTypes)2283     static int accessTypesToBitMask(final EnumSet<AccessType> accessTypes) {
2284         int m = 0;
2285         for (AccessMode accessMode : AccessMode.values()) {
2286             if (accessTypes.contains(accessMode.at)) {
2287                 m |= 1 << accessMode.ordinal();
2288             }
2289         }
2290         return m;
2291     }
2292 
alignedAccessModesBitMask(Class<?> varType, boolean isFinal)2293     static int alignedAccessModesBitMask(Class<?> varType, boolean isFinal) {
2294         // For aligned accesses, the supported access modes are described in:
2295         // @see java.lang.invoke.MethodHandles.Lookup#findVarHandle
2296         int bitMask = ALL_MODES_BIT_MASK;
2297 
2298         // If the field is declared final, keep only the read access modes.
2299         if (isFinal) {
2300             bitMask &= READ_ACCESS_MODES_BIT_MASK;
2301         }
2302 
2303         // If the field is anything other than byte, short, char, int,
2304         // long, float, double then remove the numeric atomic update
2305         // access modes.
2306         if (varType != byte.class && varType != short.class && varType != char.class &&
2307             varType != int.class && varType != long.class
2308             && varType != float.class && varType != double.class) {
2309             bitMask &= ~NUMERIC_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2310         }
2311 
2312         // If the field is not integral, remove the bitwise atomic update access modes.
2313         if (varType != boolean.class && varType != byte.class && varType != short.class &&
2314             varType != char.class && varType != int.class && varType != long.class) {
2315             bitMask &= ~BITWISE_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2316         }
2317         return bitMask;
2318     }
2319 
unalignedAccessModesBitMask(Class<?> varType)2320     static int unalignedAccessModesBitMask(Class<?> varType) {
2321         // The VarHandle refers to a view of byte array or a
2322         // view of a byte buffer.  The corresponding accesses
2323         // maybe unaligned so the access modes are more
2324         // restrictive than field or array element accesses.
2325         //
2326         // The supported access modes are described in:
2327         // @see java.lang.invoke.MethodHandles#byteArrayViewVarHandle
2328 
2329         // Read/write access modes supported for all types including
2330         // long and double on 32-bit platforms (though these accesses
2331         // may not be atomic).
2332         int bitMask = READ_ACCESS_MODES_BIT_MASK | WRITE_ACCESS_MODES_BIT_MASK;
2333 
2334         // int, long, float, double support atomic update modes per documentation.
2335         if (varType == int.class || varType == long.class ||
2336             varType == float.class || varType == double.class) {
2337             bitMask |= ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2338         }
2339 
2340         // int and long support numeric updates per documentation.
2341         if (varType == int.class || varType == long.class) {
2342             bitMask |= NUMERIC_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2343         }
2344 
2345         // int and long support bitwise updates per documentation.
2346         if (varType == int.class || varType == long.class) {
2347             bitMask |= BITWISE_ATOMIC_UPDATE_ACCESS_MODES_BIT_MASK;
2348         }
2349         return bitMask;
2350     }
2351     // END Android-added: helper state for VarHandle properties.
2352 }
2353