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