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.invoke.MethodHandles; 39 import java.lang.invoke.VarHandle; 40 import java.util.function.LongBinaryOperator; 41 import java.util.function.LongUnaryOperator; 42 43 /** 44 * A {@code long} value that may be updated atomically. See the 45 * {@link VarHandle} specification for descriptions of the properties 46 * of atomic accesses. An {@code AtomicLong} is used in applications 47 * such as atomically incremented sequence numbers, and cannot be used 48 * as a replacement for a {@link java.lang.Long}. However, this class 49 * does extend {@code Number} to allow uniform access by tools and 50 * utilities that deal with numerically-based classes. 51 * 52 * @since 1.5 53 * @author Doug Lea 54 */ 55 public class AtomicLong extends Number implements java.io.Serializable { 56 private static final long serialVersionUID = 1927816293512124184L; 57 58 /** 59 * Records whether the underlying JVM supports lockless 60 * compareAndSet for longs. While the intrinsic compareAndSetLong 61 * method works in either case, some constructions should be 62 * handled at Java level to avoid locking user-visible locks. 63 */ 64 static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8(); 65 66 /** 67 * Returns whether underlying JVM supports lockless CompareAndSet 68 * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS. 69 */ VMSupportsCS8()70 private static native boolean VMSupportsCS8(); 71 72 /* 73 * This class intended to be implemented using VarHandles, but there 74 * are unresolved cyclic startup dependencies. 75 */ 76 // BEGIN Android-changed: Using VarHandle instead of Unsafe 77 // private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); 78 // private static final long VALUE = U.objectFieldOffset(AtomicLong.class, "value"); 79 private static final VarHandle VALUE; 80 static { 81 try { 82 MethodHandles.Lookup l = MethodHandles.lookup(); 83 VALUE = l.findVarHandle(AtomicLong.class, "value", long.class); 84 } catch (ReflectiveOperationException e) { 85 throw new ExceptionInInitializerError(e); 86 } 87 } 88 // END Android-changed: Using VarHandle instead of Unsafe 89 90 private volatile long value; 91 92 /** 93 * Creates a new AtomicLong with the given initial value. 94 * 95 * @param initialValue the initial value 96 */ AtomicLong(long initialValue)97 public AtomicLong(long initialValue) { 98 value = initialValue; 99 } 100 101 /** 102 * Creates a new AtomicLong with initial value {@code 0}. 103 */ AtomicLong()104 public AtomicLong() { 105 } 106 107 /** 108 * Returns the current value, 109 * with memory effects as specified by {@link VarHandle#getVolatile}. 110 * 111 * @return the current value 112 */ get()113 public final long get() { 114 return value; 115 } 116 117 /** 118 * Sets the value to {@code newValue}, 119 * with memory effects as specified by {@link VarHandle#setVolatile}. 120 * 121 * @param newValue the new value 122 */ set(long newValue)123 public final void set(long newValue) { 124 // See JDK-8180620: Clarify VarHandle mixed-access subtleties 125 // Android-changed: Using VarHandle instead of Unsafe 126 // U.putLongVolatile(this, VALUE, newValue); 127 VALUE.setVolatile(this, newValue); 128 } 129 130 /** 131 * Sets the value to {@code newValue}, 132 * with memory effects as specified by {@link VarHandle#setRelease}. 133 * 134 * @param newValue the new value 135 * @since 1.6 136 */ lazySet(long newValue)137 public final void lazySet(long newValue) { 138 // Android-changed: Using VarHandle instead of Unsafe 139 // U.putLongRelease(this, VALUE, newValue); 140 VALUE.setRelease(this, newValue); 141 } 142 143 /** 144 * Atomically sets the value to {@code newValue} and returns the old value, 145 * with memory effects as specified by {@link VarHandle#getAndSet}. 146 * 147 * @param newValue the new value 148 * @return the previous value 149 */ getAndSet(long newValue)150 public final long getAndSet(long newValue) { 151 // Android-changed: Using VarHandle instead of Unsafe 152 // return U.getAndSetLong(this, VALUE, newValue); 153 return (long)VALUE.getAndSet(this, newValue); 154 } 155 156 /** 157 * Atomically sets the value to {@code newValue} 158 * if the current value {@code == expectedValue}, 159 * with memory effects as specified by {@link VarHandle#compareAndSet}. 160 * 161 * @param expectedValue the expected value 162 * @param newValue the new value 163 * @return {@code true} if successful. False return indicates that 164 * the actual value was not equal to the expected value. 165 */ compareAndSet(long expectedValue, long newValue)166 public final boolean compareAndSet(long expectedValue, long newValue) { 167 // Android-changed: Using VarHandle instead of Unsafe 168 // return U.compareAndSetLong(this, VALUE, expectedValue, newValue); 169 return VALUE.compareAndSet(this, expectedValue, newValue); 170 } 171 172 /** 173 * Possibly atomically sets the value to {@code newValue} 174 * if the current value {@code == expectedValue}, 175 * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. 176 * 177 * @deprecated This method has plain memory effects but the method 178 * name implies volatile memory effects (see methods such as 179 * {@link #compareAndExchange} and {@link #compareAndSet}). To avoid 180 * confusion over plain or volatile memory effects it is recommended that 181 * the method {@link #weakCompareAndSetPlain} be used instead. 182 * 183 * @param expectedValue the expected value 184 * @param newValue the new value 185 * @return {@code true} if successful 186 * @see #weakCompareAndSetPlain 187 */ 188 @Deprecated(since="9") weakCompareAndSet(long expectedValue, long newValue)189 public final boolean weakCompareAndSet(long expectedValue, long newValue) { 190 // Android-changed: Using VarHandle instead of Unsafe 191 // return U.weakCompareAndSetLongPlain(this, VALUE, expectedValue, newValue); 192 return VALUE.weakCompareAndSetPlain(this, expectedValue, newValue); 193 } 194 195 /** 196 * Possibly atomically sets the value to {@code newValue} 197 * if the current value {@code == expectedValue}, 198 * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. 199 * 200 * @param expectedValue the expected value 201 * @param newValue the new value 202 * @return {@code true} if successful 203 * @since 9 204 */ weakCompareAndSetPlain(long expectedValue, long newValue)205 public final boolean weakCompareAndSetPlain(long expectedValue, long newValue) { 206 // Android-changed: Using VarHandle instead of Unsafe 207 // return U.weakCompareAndSetLongPlain(this, VALUE, expectedValue, newValue); 208 return VALUE.weakCompareAndSetPlain(this, expectedValue, newValue); 209 } 210 211 /** 212 * Atomically increments the current value, 213 * with memory effects as specified by {@link VarHandle#getAndAdd}. 214 * 215 * <p>Equivalent to {@code getAndAdd(1)}. 216 * 217 * @return the previous value 218 */ getAndIncrement()219 public final long getAndIncrement() { 220 // Android-changed: Using VarHandle instead of Unsafe 221 // return U.getAndAddLong(this, VALUE, 1L); 222 return (long)VALUE.getAndAdd(this, 1L); 223 } 224 225 /** 226 * Atomically decrements the current value, 227 * with memory effects as specified by {@link VarHandle#getAndAdd}. 228 * 229 * <p>Equivalent to {@code getAndAdd(-1)}. 230 * 231 * @return the previous value 232 */ getAndDecrement()233 public final long getAndDecrement() { 234 // Android-changed: Using VarHandle instead of Unsafe 235 // return U.getAndAddLong(this, VALUE, -1L); 236 return (long)VALUE.getAndAdd(this, -1L); 237 } 238 239 /** 240 * Atomically adds the given value to the current value, 241 * with memory effects as specified by {@link VarHandle#getAndAdd}. 242 * 243 * @param delta the value to add 244 * @return the previous value 245 */ getAndAdd(long delta)246 public final long getAndAdd(long delta) { 247 // Android-changed: Using VarHandle instead of Unsafe 248 // return U.getAndAddLong(this, VALUE, delta); 249 return (long)VALUE.getAndAdd(this, delta); 250 } 251 252 /** 253 * Atomically increments the current value, 254 * with memory effects as specified by {@link VarHandle#getAndAdd}. 255 * 256 * <p>Equivalent to {@code addAndGet(1)}. 257 * 258 * @return the updated value 259 */ incrementAndGet()260 public final long incrementAndGet() { 261 // Android-changed: Using VarHandle instead of Unsafe 262 // return U.getAndAddLong(this, VALUE, 1L) + 1L; 263 return (long)VALUE.getAndAdd(this, 1L) + 1L; 264 } 265 266 /** 267 * Atomically decrements the current value, 268 * with memory effects as specified by {@link VarHandle#getAndAdd}. 269 * 270 * <p>Equivalent to {@code addAndGet(-1)}. 271 * 272 * @return the updated value 273 */ decrementAndGet()274 public final long decrementAndGet() { 275 // Android-changed: Using VarHandle instead of Unsafe 276 // return U.getAndAddLong(this, VALUE, -1L) - 1L; 277 return (long)VALUE.getAndAdd(this, -1L) - 1L; 278 } 279 280 /** 281 * Atomically adds the given value to the current value, 282 * with memory effects as specified by {@link VarHandle#getAndAdd}. 283 * 284 * @param delta the value to add 285 * @return the updated value 286 */ addAndGet(long delta)287 public final long addAndGet(long delta) { 288 // Android-changed: Using VarHandle instead of Unsafe 289 // return U.getAndAddLong(this, VALUE, delta) + delta; 290 return (long)VALUE.getAndAdd(this, delta) + delta; 291 } 292 293 /** 294 * Atomically updates (with memory effects as specified by {@link 295 * VarHandle#compareAndSet}) the current value with the results of 296 * applying the given function, returning the previous value. The 297 * function should be side-effect-free, since it may be re-applied 298 * when attempted updates fail due to contention among threads. 299 * 300 * @param updateFunction a side-effect-free function 301 * @return the previous value 302 * @since 1.8 303 */ getAndUpdate(LongUnaryOperator updateFunction)304 public final long getAndUpdate(LongUnaryOperator updateFunction) { 305 long prev = get(), next = 0L; 306 for (boolean haveNext = false;;) { 307 if (!haveNext) 308 next = updateFunction.applyAsLong(prev); 309 if (weakCompareAndSetVolatile(prev, next)) 310 return prev; 311 haveNext = (prev == (prev = get())); 312 } 313 } 314 315 /** 316 * Atomically updates (with memory effects as specified by {@link 317 * VarHandle#compareAndSet}) the current value with the results of 318 * applying the given function, returning the updated value. The 319 * function should be side-effect-free, since it may be re-applied 320 * when attempted updates fail due to contention among threads. 321 * 322 * @param updateFunction a side-effect-free function 323 * @return the updated value 324 * @since 1.8 325 */ updateAndGet(LongUnaryOperator updateFunction)326 public final long updateAndGet(LongUnaryOperator updateFunction) { 327 long prev = get(), next = 0L; 328 for (boolean haveNext = false;;) { 329 if (!haveNext) 330 next = updateFunction.applyAsLong(prev); 331 if (weakCompareAndSetVolatile(prev, next)) 332 return next; 333 haveNext = (prev == (prev = get())); 334 } 335 } 336 337 /** 338 * Atomically updates (with memory effects as specified by {@link 339 * VarHandle#compareAndSet}) the current value with the results of 340 * applying the given function to the current and given values, 341 * returning the previous value. The function should be 342 * side-effect-free, since it may be re-applied when attempted 343 * updates fail due to contention among threads. The function is 344 * applied with the current value as its first argument, and the 345 * given update as the second argument. 346 * 347 * @param x the update value 348 * @param accumulatorFunction a side-effect-free function of two arguments 349 * @return the previous value 350 * @since 1.8 351 */ getAndAccumulate(long x, LongBinaryOperator accumulatorFunction)352 public final long getAndAccumulate(long x, 353 LongBinaryOperator accumulatorFunction) { 354 long prev = get(), next = 0L; 355 for (boolean haveNext = false;;) { 356 if (!haveNext) 357 next = accumulatorFunction.applyAsLong(prev, x); 358 if (weakCompareAndSetVolatile(prev, next)) 359 return prev; 360 haveNext = (prev == (prev = get())); 361 } 362 } 363 364 /** 365 * Atomically updates (with memory effects as specified by {@link 366 * VarHandle#compareAndSet}) the current value with the results of 367 * applying the given function to the current and given values, 368 * returning the updated value. The function should be 369 * side-effect-free, since it may be re-applied when attempted 370 * updates fail due to contention among threads. The function is 371 * applied with the current value as its first argument, and the 372 * given update as the second argument. 373 * 374 * @param x the update value 375 * @param accumulatorFunction a side-effect-free function of two arguments 376 * @return the updated value 377 * @since 1.8 378 */ accumulateAndGet(long x, LongBinaryOperator accumulatorFunction)379 public final long accumulateAndGet(long x, 380 LongBinaryOperator accumulatorFunction) { 381 long prev = get(), next = 0L; 382 for (boolean haveNext = false;;) { 383 if (!haveNext) 384 next = accumulatorFunction.applyAsLong(prev, x); 385 if (weakCompareAndSetVolatile(prev, next)) 386 return next; 387 haveNext = (prev == (prev = get())); 388 } 389 } 390 391 /** 392 * Returns the String representation of the current value. 393 * @return the String representation of the current value 394 */ toString()395 public String toString() { 396 return Long.toString(get()); 397 } 398 399 /** 400 * Returns the current value of this {@code AtomicLong} as an {@code int} 401 * after a narrowing primitive conversion, 402 * with memory effects as specified by {@link VarHandle#getVolatile}. 403 * @jls 5.1.3 Narrowing Primitive Conversions 404 */ intValue()405 public int intValue() { 406 return (int)get(); 407 } 408 409 /** 410 * Returns the current value of this {@code AtomicLong} as a {@code long}, 411 * with memory effects as specified by {@link VarHandle#getVolatile}. 412 * Equivalent to {@link #get()}. 413 */ longValue()414 public long longValue() { 415 return get(); 416 } 417 418 /** 419 * Returns the current value of this {@code AtomicLong} as a {@code float} 420 * after a widening primitive conversion, 421 * with memory effects as specified by {@link VarHandle#getVolatile}. 422 * @jls 5.1.2 Widening Primitive Conversions 423 */ floatValue()424 public float floatValue() { 425 return (float)get(); 426 } 427 428 /** 429 * Returns the current value of this {@code AtomicLong} as a {@code double} 430 * after a widening primitive conversion, 431 * with memory effects as specified by {@link VarHandle#getVolatile}. 432 * @jls 5.1.2 Widening Primitive Conversions 433 */ doubleValue()434 public double doubleValue() { 435 return (double)get(); 436 } 437 438 // jdk9 439 440 /** 441 * Returns the current value, with memory semantics of reading as if the 442 * variable was declared non-{@code volatile}. 443 * 444 * @return the value 445 * @since 9 446 */ getPlain()447 public final long getPlain() { 448 // Android-changed: Using VarHandle instead of Unsafe 449 // return U.getLong(this, VALUE); 450 return (long)VALUE.get(this); 451 } 452 453 /** 454 * Sets the value to {@code newValue}, with memory semantics 455 * of setting as if the variable was declared non-{@code volatile} 456 * and non-{@code final}. 457 * 458 * @param newValue the new value 459 * @since 9 460 */ setPlain(long newValue)461 public final void setPlain(long newValue) { 462 // Android-changed: Using VarHandle instead of Unsafe 463 // U.putLong(this, VALUE, newValue); 464 VALUE.set(this, newValue); 465 } 466 467 /** 468 * Returns the current value, 469 * with memory effects as specified by {@link VarHandle#getOpaque}. 470 * 471 * @return the value 472 * @since 9 473 */ getOpaque()474 public final long getOpaque() { 475 // Android-changed: Using VarHandle instead of Unsafe 476 // return U.getLongOpaque(this, VALUE); 477 return (long)VALUE.getOpaque(this); 478 } 479 480 /** 481 * Sets the value to {@code newValue}, 482 * with memory effects as specified by {@link VarHandle#setOpaque}. 483 * 484 * @param newValue the new value 485 * @since 9 486 */ setOpaque(long newValue)487 public final void setOpaque(long newValue) { 488 // Android-changed: Using VarHandle instead of Unsafe 489 // U.putLongOpaque(this, VALUE, newValue); 490 VALUE.setOpaque(this, newValue); 491 } 492 493 /** 494 * Returns the current value, 495 * with memory effects as specified by {@link VarHandle#getAcquire}. 496 * 497 * @return the value 498 * @since 9 499 */ getAcquire()500 public final long getAcquire() { 501 // Android-changed: Using VarHandle instead of Unsafe 502 // return U.getLongAcquire(this, VALUE); 503 return (long)VALUE.getAcquire(this); 504 } 505 506 /** 507 * Sets the value to {@code newValue}, 508 * with memory effects as specified by {@link VarHandle#setRelease}. 509 * 510 * @param newValue the new value 511 * @since 9 512 */ setRelease(long newValue)513 public final void setRelease(long newValue) { 514 // Android-changed: Using VarHandle instead of Unsafe 515 // U.putLongRelease(this, VALUE, newValue); 516 VALUE.setRelease(this, newValue); 517 } 518 519 /** 520 * Atomically sets the value to {@code newValue} if the current value, 521 * referred to as the <em>witness value</em>, {@code == expectedValue}, 522 * with memory effects as specified by 523 * {@link VarHandle#compareAndExchange}. 524 * 525 * @param expectedValue the expected value 526 * @param newValue the new value 527 * @return the witness value, which will be the same as the 528 * expected value if successful 529 * @since 9 530 */ compareAndExchange(long expectedValue, long newValue)531 public final long compareAndExchange(long expectedValue, long newValue) { 532 // Android-changed: Using VarHandle instead of Unsafe 533 // return U.compareAndExchangeLong(this, VALUE, expectedValue, newValue); 534 return (long)VALUE.compareAndExchange(this, expectedValue, newValue); 535 } 536 537 /** 538 * Atomically sets the value to {@code newValue} if the current value, 539 * referred to as the <em>witness value</em>, {@code == expectedValue}, 540 * with memory effects as specified by 541 * {@link VarHandle#compareAndExchangeAcquire}. 542 * 543 * @param expectedValue the expected value 544 * @param newValue the new value 545 * @return the witness value, which will be the same as the 546 * expected value if successful 547 * @since 9 548 */ compareAndExchangeAcquire(long expectedValue, long newValue)549 public final long compareAndExchangeAcquire(long expectedValue, long newValue) { 550 // Android-changed: Using VarHandle instead of Unsafe 551 // return U.compareAndExchangeLongAcquire(this, VALUE, expectedValue, newValue); 552 return (long)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue); 553 } 554 555 /** 556 * Atomically sets the value to {@code newValue} if the current value, 557 * referred to as the <em>witness value</em>, {@code == expectedValue}, 558 * with memory effects as specified by 559 * {@link VarHandle#compareAndExchangeRelease}. 560 * 561 * @param expectedValue the expected value 562 * @param newValue the new value 563 * @return the witness value, which will be the same as the 564 * expected value if successful 565 * @since 9 566 */ compareAndExchangeRelease(long expectedValue, long newValue)567 public final long compareAndExchangeRelease(long expectedValue, long newValue) { 568 // Android-changed: Using VarHandle instead of Unsafe 569 // return U.compareAndExchangeLongRelease(this, VALUE, expectedValue, newValue); 570 return (long)VALUE.compareAndExchangeRelease(this, expectedValue, newValue); 571 } 572 573 /** 574 * Possibly atomically sets the value to {@code newValue} 575 * if the current value {@code == expectedValue}, 576 * with memory effects as specified by 577 * {@link VarHandle#weakCompareAndSet}. 578 * 579 * @param expectedValue the expected value 580 * @param newValue the new value 581 * @return {@code true} if successful 582 * @since 9 583 */ weakCompareAndSetVolatile(long expectedValue, long newValue)584 public final boolean weakCompareAndSetVolatile(long expectedValue, long newValue) { 585 // Android-changed: Using VarHandle instead of Unsafe 586 // return U.weakCompareAndSetLong(this, VALUE, expectedValue, newValue); 587 return VALUE.weakCompareAndSet(this, expectedValue, newValue); 588 } 589 590 /** 591 * Possibly atomically sets the value to {@code newValue} 592 * if the current value {@code == expectedValue}, 593 * with memory effects as specified by 594 * {@link VarHandle#weakCompareAndSetAcquire}. 595 * 596 * @param expectedValue the expected value 597 * @param newValue the new value 598 * @return {@code true} if successful 599 * @since 9 600 */ weakCompareAndSetAcquire(long expectedValue, long newValue)601 public final boolean weakCompareAndSetAcquire(long expectedValue, long newValue) { 602 // Android-changed: Using VarHandle instead of Unsafe 603 // return U.weakCompareAndSetLongAcquire(this, VALUE, expectedValue, newValue); 604 return VALUE.weakCompareAndSetAcquire(this, expectedValue, newValue); 605 } 606 607 /** 608 * Possibly atomically sets the value to {@code newValue} 609 * if the current value {@code == expectedValue}, 610 * with memory effects as specified by 611 * {@link VarHandle#weakCompareAndSetRelease}. 612 * 613 * @param expectedValue the expected value 614 * @param newValue the new value 615 * @return {@code true} if successful 616 * @since 9 617 */ weakCompareAndSetRelease(long expectedValue, long newValue)618 public final boolean weakCompareAndSetRelease(long expectedValue, long newValue) { 619 // Android-changed: Using VarHandle instead of Unsafe 620 // return U.weakCompareAndSetLongRelease(this, VALUE, expectedValue, newValue); 621 return VALUE.weakCompareAndSetRelease(this, expectedValue, newValue); 622 } 623 624 } 625