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