1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util.concurrent.atomic; 37 38 import java.lang.reflect.Field; 39 import java.lang.reflect.Modifier; 40 import java.security.AccessController; 41 import java.security.PrivilegedActionException; 42 import java.security.PrivilegedExceptionAction; 43 import java.util.function.LongBinaryOperator; 44 import java.util.function.LongUnaryOperator; 45 import sun.reflect.CallerSensitive; 46 import sun.reflect.Reflection; 47 48 /** 49 * A reflection-based utility that enables atomic updates to 50 * designated {@code volatile long} fields of designated classes. 51 * This class is designed for use in atomic data structures in which 52 * several fields of the same node are independently subject to atomic 53 * updates. 54 * 55 * <p>Note that the guarantees of the {@code compareAndSet} 56 * method in this class are weaker than in other atomic classes. 57 * Because this class cannot ensure that all uses of the field 58 * are appropriate for purposes of atomic access, it can 59 * guarantee atomicity only with respect to other invocations of 60 * {@code compareAndSet} and {@code set} on the same updater. 61 * 62 * @since 1.5 63 * @author Doug Lea 64 * @param <T> The type of the object holding the updatable field 65 */ 66 public abstract class AtomicLongFieldUpdater<T> { 67 /** 68 * Creates and returns an updater for objects with the given field. 69 * The Class argument is needed to check that reflective types and 70 * generic types match. 71 * 72 * @param tclass the class of the objects holding the field 73 * @param fieldName the name of the field to be updated 74 * @param <U> the type of instances of tclass 75 * @return the updater 76 * @throws IllegalArgumentException if the field is not a 77 * volatile long type 78 * @throws RuntimeException with a nested reflection-based 79 * exception if the class does not hold field or is the wrong type, 80 * or the field is inaccessible to the caller according to Java language 81 * access control 82 */ 83 @CallerSensitive newUpdater(Class<U> tclass, String fieldName)84 public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, 85 String fieldName) { 86 Class<?> caller = Reflection.getCallerClass(); 87 if (AtomicLong.VM_SUPPORTS_LONG_CAS) 88 return new CASUpdater<U>(tclass, fieldName, caller); 89 else 90 return new LockedUpdater<U>(tclass, fieldName, caller); 91 } 92 93 /** 94 * Protected do-nothing constructor for use by subclasses. 95 */ AtomicLongFieldUpdater()96 protected AtomicLongFieldUpdater() { 97 } 98 99 /** 100 * Atomically sets the field of the given object managed by this updater 101 * to the given updated value if the current value {@code ==} the 102 * expected value. This method is guaranteed to be atomic with respect to 103 * other calls to {@code compareAndSet} and {@code set}, but not 104 * necessarily with respect to other changes in the field. 105 * 106 * @param obj An object whose field to conditionally set 107 * @param expect the expected value 108 * @param update the new value 109 * @return {@code true} if successful 110 * @throws ClassCastException if {@code obj} is not an instance 111 * of the class possessing the field established in the constructor 112 */ compareAndSet(T obj, long expect, long update)113 public abstract boolean compareAndSet(T obj, long expect, long update); 114 115 /** 116 * Atomically sets the field of the given object managed by this updater 117 * to the given updated value if the current value {@code ==} the 118 * expected value. This method is guaranteed to be atomic with respect to 119 * other calls to {@code compareAndSet} and {@code set}, but not 120 * necessarily with respect to other changes in the field. 121 * 122 * <p><a href="package-summary.html#weakCompareAndSet">May fail 123 * spuriously and does not provide ordering guarantees</a>, so is 124 * only rarely an appropriate alternative to {@code compareAndSet}. 125 * 126 * @param obj An object whose field to conditionally set 127 * @param expect the expected value 128 * @param update the new value 129 * @return {@code true} if successful 130 * @throws ClassCastException if {@code obj} is not an instance 131 * of the class possessing the field established in the constructor 132 */ weakCompareAndSet(T obj, long expect, long update)133 public abstract boolean weakCompareAndSet(T obj, long expect, long update); 134 135 /** 136 * Sets the field of the given object managed by this updater to the 137 * given updated value. This operation is guaranteed to act as a volatile 138 * store with respect to subsequent invocations of {@code compareAndSet}. 139 * 140 * @param obj An object whose field to set 141 * @param newValue the new value 142 */ set(T obj, long newValue)143 public abstract void set(T obj, long newValue); 144 145 /** 146 * Eventually sets the field of the given object managed by this 147 * updater to the given updated value. 148 * 149 * @param obj An object whose field to set 150 * @param newValue the new value 151 * @since 1.6 152 */ lazySet(T obj, long newValue)153 public abstract void lazySet(T obj, long newValue); 154 155 /** 156 * Gets the current value held in the field of the given object managed 157 * by this updater. 158 * 159 * @param obj An object whose field to get 160 * @return the current value 161 */ get(T obj)162 public abstract long get(T obj); 163 164 /** 165 * Atomically sets the field of the given object managed by this updater 166 * to the given value and returns the old value. 167 * 168 * @param obj An object whose field to get and set 169 * @param newValue the new value 170 * @return the previous value 171 */ getAndSet(T obj, long newValue)172 public long getAndSet(T obj, long newValue) { 173 long prev; 174 do { 175 prev = get(obj); 176 } while (!compareAndSet(obj, prev, newValue)); 177 return prev; 178 } 179 180 /** 181 * Atomically increments by one the current value of the field of the 182 * given object managed by this updater. 183 * 184 * @param obj An object whose field to get and set 185 * @return the previous value 186 */ getAndIncrement(T obj)187 public long getAndIncrement(T obj) { 188 long prev, next; 189 do { 190 prev = get(obj); 191 next = prev + 1; 192 } while (!compareAndSet(obj, prev, next)); 193 return prev; 194 } 195 196 /** 197 * Atomically decrements by one the current value of the field of the 198 * given object managed by this updater. 199 * 200 * @param obj An object whose field to get and set 201 * @return the previous value 202 */ getAndDecrement(T obj)203 public long getAndDecrement(T obj) { 204 long prev, next; 205 do { 206 prev = get(obj); 207 next = prev - 1; 208 } while (!compareAndSet(obj, prev, next)); 209 return prev; 210 } 211 212 /** 213 * Atomically adds the given value to the current value of the field of 214 * the given object managed by this updater. 215 * 216 * @param obj An object whose field to get and set 217 * @param delta the value to add 218 * @return the previous value 219 */ getAndAdd(T obj, long delta)220 public long getAndAdd(T obj, long delta) { 221 long prev, next; 222 do { 223 prev = get(obj); 224 next = prev + delta; 225 } while (!compareAndSet(obj, prev, next)); 226 return prev; 227 } 228 229 /** 230 * Atomically increments by one the current value of the field of the 231 * given object managed by this updater. 232 * 233 * @param obj An object whose field to get and set 234 * @return the updated value 235 */ incrementAndGet(T obj)236 public long incrementAndGet(T obj) { 237 long prev, next; 238 do { 239 prev = get(obj); 240 next = prev + 1; 241 } while (!compareAndSet(obj, prev, next)); 242 return next; 243 } 244 245 /** 246 * Atomically decrements by one the current value of the field of the 247 * given object managed by this updater. 248 * 249 * @param obj An object whose field to get and set 250 * @return the updated value 251 */ decrementAndGet(T obj)252 public long decrementAndGet(T obj) { 253 long prev, next; 254 do { 255 prev = get(obj); 256 next = prev - 1; 257 } while (!compareAndSet(obj, prev, next)); 258 return next; 259 } 260 261 /** 262 * Atomically adds the given value to the current value of the field of 263 * the given object managed by this updater. 264 * 265 * @param obj An object whose field to get and set 266 * @param delta the value to add 267 * @return the updated value 268 */ addAndGet(T obj, long delta)269 public long addAndGet(T obj, long delta) { 270 long prev, next; 271 do { 272 prev = get(obj); 273 next = prev + delta; 274 } while (!compareAndSet(obj, prev, next)); 275 return next; 276 } 277 278 /** 279 * Atomically updates the field of the given object managed by this updater 280 * with the results of applying the given function, returning the previous 281 * value. The function should be side-effect-free, since it may be 282 * re-applied when attempted updates fail due to contention among threads. 283 * 284 * @param obj An object whose field to get and set 285 * @param updateFunction a side-effect-free function 286 * @return the previous value 287 * @since 1.8 288 */ getAndUpdate(T obj, LongUnaryOperator updateFunction)289 public final long getAndUpdate(T obj, LongUnaryOperator updateFunction) { 290 long prev, next; 291 do { 292 prev = get(obj); 293 next = updateFunction.applyAsLong(prev); 294 } while (!compareAndSet(obj, prev, next)); 295 return prev; 296 } 297 298 /** 299 * Atomically updates the field of the given object managed by this updater 300 * with the results of applying the given function, returning the updated 301 * value. The function should be side-effect-free, since it may be 302 * re-applied when attempted updates fail due to contention among threads. 303 * 304 * @param obj An object whose field to get and set 305 * @param updateFunction a side-effect-free function 306 * @return the updated value 307 * @since 1.8 308 */ updateAndGet(T obj, LongUnaryOperator updateFunction)309 public final long updateAndGet(T obj, LongUnaryOperator updateFunction) { 310 long prev, next; 311 do { 312 prev = get(obj); 313 next = updateFunction.applyAsLong(prev); 314 } while (!compareAndSet(obj, prev, next)); 315 return next; 316 } 317 318 /** 319 * Atomically updates the field of the given object managed by this 320 * updater with the results of applying the given function to the 321 * current and given values, returning the previous value. The 322 * function should be side-effect-free, since it may be re-applied 323 * when attempted updates fail due to contention among threads. The 324 * function is applied with the current value as its first argument, 325 * and the given update as the second argument. 326 * 327 * @param obj An object whose field to get and set 328 * @param x the update value 329 * @param accumulatorFunction a side-effect-free function of two arguments 330 * @return the previous value 331 * @since 1.8 332 */ getAndAccumulate(T obj, long x, LongBinaryOperator accumulatorFunction)333 public final long getAndAccumulate(T obj, long x, 334 LongBinaryOperator accumulatorFunction) { 335 long prev, next; 336 do { 337 prev = get(obj); 338 next = accumulatorFunction.applyAsLong(prev, x); 339 } while (!compareAndSet(obj, prev, next)); 340 return prev; 341 } 342 343 /** 344 * Atomically updates the field of the given object managed by this 345 * updater with the results of applying the given function to the 346 * current and given values, returning the updated value. The 347 * function should be side-effect-free, since it may be re-applied 348 * when attempted updates fail due to contention among threads. The 349 * function is applied with the current value as its first argument, 350 * and the given update as the second argument. 351 * 352 * @param obj An object whose field to get and set 353 * @param x the update value 354 * @param accumulatorFunction a side-effect-free function of two arguments 355 * @return the updated value 356 * @since 1.8 357 */ accumulateAndGet(T obj, long x, LongBinaryOperator accumulatorFunction)358 public final long accumulateAndGet(T obj, long x, 359 LongBinaryOperator accumulatorFunction) { 360 long prev, next; 361 do { 362 prev = get(obj); 363 next = accumulatorFunction.applyAsLong(prev, x); 364 } while (!compareAndSet(obj, prev, next)); 365 return next; 366 } 367 368 private static final class CASUpdater<T> extends AtomicLongFieldUpdater<T> { 369 private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); 370 private final long offset; 371 /** 372 * if field is protected, the subclass constructing updater, else 373 * the same as tclass 374 */ 375 private final Class<?> cclass; 376 /** class holding the field */ 377 private final Class<T> tclass; 378 CASUpdater(final Class<T> tclass, final String fieldName, final Class<?> caller)379 CASUpdater(final Class<T> tclass, final String fieldName, 380 final Class<?> caller) { 381 final Field field; 382 final int modifiers; 383 try { 384 // Android-changed: Skip privilege escalation which is a noop on Android. 385 /* 386 field = AccessController.doPrivileged( 387 new PrivilegedExceptionAction<Field>() { 388 public Field run() throws NoSuchFieldException { 389 return tclass.getDeclaredField(fieldName); 390 } 391 }); 392 */ 393 field = tclass.getDeclaredField(fieldName); 394 modifiers = field.getModifiers(); 395 sun.reflect.misc.ReflectUtil.ensureMemberAccess( 396 caller, tclass, null, modifiers); 397 // Android-removed: Skip checkPackageAccess which is a noop on Android. 398 /* 399 ClassLoader cl = tclass.getClassLoader(); 400 ClassLoader ccl = caller.getClassLoader(); 401 if ((ccl != null) && (ccl != cl) && 402 ((cl == null) || !isAncestor(cl, ccl))) { 403 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); 404 } 405 */ 406 // Android-removed: Skip privilege escalation which is a noop on Android. 407 /* 408 } catch (PrivilegedActionException pae) { 409 throw new RuntimeException(pae.getException()); 410 */ 411 } catch (Exception ex) { 412 throw new RuntimeException(ex); 413 } 414 415 if (field.getType() != long.class) 416 throw new IllegalArgumentException("Must be long type"); 417 418 if (!Modifier.isVolatile(modifiers)) 419 throw new IllegalArgumentException("Must be volatile type"); 420 421 this.cclass = (Modifier.isProtected(modifiers)) ? caller : tclass; 422 this.tclass = tclass; 423 this.offset = U.objectFieldOffset(field); 424 } 425 426 /** 427 * Checks that target argument is instance of cclass. On 428 * failure, throws cause. 429 */ accessCheck(T obj)430 private final void accessCheck(T obj) { 431 if (!cclass.isInstance(obj)) 432 throwAccessCheckException(obj); 433 } 434 435 /** 436 * Throws access exception if accessCheck failed due to 437 * protected access, else ClassCastException. 438 */ throwAccessCheckException(T obj)439 private final void throwAccessCheckException(T obj) { 440 if (cclass == tclass) 441 throw new ClassCastException(); 442 else 443 throw new RuntimeException( 444 new IllegalAccessException( 445 "Class " + 446 cclass.getName() + 447 " can not access a protected member of class " + 448 tclass.getName() + 449 " using an instance of " + 450 obj.getClass().getName())); 451 } 452 compareAndSet(T obj, long expect, long update)453 public final boolean compareAndSet(T obj, long expect, long update) { 454 accessCheck(obj); 455 return U.compareAndSwapLong(obj, offset, expect, update); 456 } 457 weakCompareAndSet(T obj, long expect, long update)458 public final boolean weakCompareAndSet(T obj, long expect, long update) { 459 accessCheck(obj); 460 return U.compareAndSwapLong(obj, offset, expect, update); 461 } 462 set(T obj, long newValue)463 public final void set(T obj, long newValue) { 464 accessCheck(obj); 465 U.putLongVolatile(obj, offset, newValue); 466 } 467 lazySet(T obj, long newValue)468 public final void lazySet(T obj, long newValue) { 469 accessCheck(obj); 470 U.putOrderedLong(obj, offset, newValue); 471 } 472 get(T obj)473 public final long get(T obj) { 474 accessCheck(obj); 475 return U.getLongVolatile(obj, offset); 476 } 477 getAndSet(T obj, long newValue)478 public final long getAndSet(T obj, long newValue) { 479 accessCheck(obj); 480 return U.getAndSetLong(obj, offset, newValue); 481 } 482 getAndAdd(T obj, long delta)483 public final long getAndAdd(T obj, long delta) { 484 accessCheck(obj); 485 return U.getAndAddLong(obj, offset, delta); 486 } 487 getAndIncrement(T obj)488 public final long getAndIncrement(T obj) { 489 return getAndAdd(obj, 1); 490 } 491 getAndDecrement(T obj)492 public final long getAndDecrement(T obj) { 493 return getAndAdd(obj, -1); 494 } 495 incrementAndGet(T obj)496 public final long incrementAndGet(T obj) { 497 return getAndAdd(obj, 1) + 1; 498 } 499 decrementAndGet(T obj)500 public final long decrementAndGet(T obj) { 501 return getAndAdd(obj, -1) - 1; 502 } 503 addAndGet(T obj, long delta)504 public final long addAndGet(T obj, long delta) { 505 return getAndAdd(obj, delta) + delta; 506 } 507 } 508 509 private static final class LockedUpdater<T> extends AtomicLongFieldUpdater<T> { 510 private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); 511 private final long offset; 512 /** 513 * if field is protected, the subclass constructing updater, else 514 * the same as tclass 515 */ 516 private final Class<?> cclass; 517 /** class holding the field */ 518 private final Class<T> tclass; 519 LockedUpdater(final Class<T> tclass, final String fieldName, final Class<?> caller)520 LockedUpdater(final Class<T> tclass, final String fieldName, 521 final Class<?> caller) { 522 Field field = null; 523 int modifiers = 0; 524 try { 525 // Android-changed: Skip privilege escalation which is a noop on Android. 526 /* 527 field = AccessController.doPrivileged( 528 new PrivilegedExceptionAction<Field>() { 529 public Field run() throws NoSuchFieldException { 530 return tclass.getDeclaredField(fieldName); 531 } 532 }); 533 */ 534 field = tclass.getDeclaredField(fieldName); 535 modifiers = field.getModifiers(); 536 sun.reflect.misc.ReflectUtil.ensureMemberAccess( 537 caller, tclass, null, modifiers); 538 // Android-removed: Skip checkPackageAccess which is a noop on Android. 539 /* 540 ClassLoader cl = tclass.getClassLoader(); 541 ClassLoader ccl = caller.getClassLoader(); 542 if ((ccl != null) && (ccl != cl) && 543 ((cl == null) || !isAncestor(cl, ccl))) { 544 sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); 545 } 546 */ 547 // Android-removed: Skip privilege escalation which is a noop on Android. 548 /* 549 } catch (PrivilegedActionException pae) { 550 throw new RuntimeException(pae.getException()); 551 */ 552 } catch (Exception ex) { 553 throw new RuntimeException(ex); 554 } 555 556 if (field.getType() != long.class) 557 throw new IllegalArgumentException("Must be long type"); 558 559 if (!Modifier.isVolatile(modifiers)) 560 throw new IllegalArgumentException("Must be volatile type"); 561 562 this.cclass = (Modifier.isProtected(modifiers)) ? caller : tclass; 563 this.tclass = tclass; 564 this.offset = U.objectFieldOffset(field); 565 } 566 567 /** 568 * Checks that target argument is instance of cclass. On 569 * failure, throws cause. 570 */ accessCheck(T obj)571 private final void accessCheck(T obj) { 572 if (!cclass.isInstance(obj)) 573 throw accessCheckException(obj); 574 } 575 576 /** 577 * Returns access exception if accessCheck failed due to 578 * protected access, else ClassCastException. 579 */ accessCheckException(T obj)580 private final RuntimeException accessCheckException(T obj) { 581 if (cclass == tclass) 582 return new ClassCastException(); 583 else 584 return new RuntimeException( 585 new IllegalAccessException( 586 "Class " + 587 cclass.getName() + 588 " can not access a protected member of class " + 589 tclass.getName() + 590 " using an instance of " + 591 obj.getClass().getName())); 592 } 593 compareAndSet(T obj, long expect, long update)594 public final boolean compareAndSet(T obj, long expect, long update) { 595 accessCheck(obj); 596 synchronized (this) { 597 long v = U.getLong(obj, offset); 598 if (v != expect) 599 return false; 600 U.putLong(obj, offset, update); 601 return true; 602 } 603 } 604 weakCompareAndSet(T obj, long expect, long update)605 public final boolean weakCompareAndSet(T obj, long expect, long update) { 606 return compareAndSet(obj, expect, update); 607 } 608 set(T obj, long newValue)609 public final void set(T obj, long newValue) { 610 accessCheck(obj); 611 synchronized (this) { 612 U.putLong(obj, offset, newValue); 613 } 614 } 615 lazySet(T obj, long newValue)616 public final void lazySet(T obj, long newValue) { 617 set(obj, newValue); 618 } 619 get(T obj)620 public final long get(T obj) { 621 accessCheck(obj); 622 synchronized (this) { 623 return U.getLong(obj, offset); 624 } 625 } 626 } 627 628 // Android-removed: isAncestor's only usage was removed above. 629 /* 630 /** 631 * Returns true if the second classloader can be found in the first 632 * classloader's delegation chain. 633 * Equivalent to the inaccessible: first.isAncestor(second). 634 * 635 static boolean isAncestor(ClassLoader first, ClassLoader second) { 636 ClassLoader acl = first; 637 do { 638 acl = acl.getParent(); 639 if (second == acl) { 640 return true; 641 } 642 } while (acl != null); 643 return false; 644 } 645 */ 646 } 647