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