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