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