rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicLong.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 10:52:01 +0200
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
permissions -rw-r--r--
Concurrency utilities
     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 /**
    39  * A {@code long} value that may be updated atomically.  See the
    40  * {@link java.util.concurrent.atomic} package specification for
    41  * description of the properties of atomic variables. An
    42  * {@code AtomicLong} is used in applications such as atomically
    43  * incremented sequence numbers, and cannot be used as a replacement
    44  * for a {@link java.lang.Long}. However, this class does extend
    45  * {@code Number} to allow uniform access by tools and utilities that
    46  * deal with numerically-based classes.
    47  *
    48  * @since 1.5
    49  * @author Doug Lea
    50  */
    51 public class AtomicLong extends Number implements java.io.Serializable {
    52     private static final long serialVersionUID = 1927816293512124184L;
    53 
    54 
    55     /**
    56      * Returns whether underlying JVM supports lockless CompareAndSet
    57      * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
    58      */
    59     private static native boolean VMSupportsCS8();
    60 
    61     private volatile long value;
    62 
    63     /**
    64      * Creates a new AtomicLong with the given initial value.
    65      *
    66      * @param initialValue the initial value
    67      */
    68     public AtomicLong(long initialValue) {
    69         value = initialValue;
    70     }
    71 
    72     /**
    73      * Creates a new AtomicLong with initial value {@code 0}.
    74      */
    75     public AtomicLong() {
    76     }
    77 
    78     /**
    79      * Gets the current value.
    80      *
    81      * @return the current value
    82      */
    83     public final long get() {
    84         return value;
    85     }
    86 
    87     /**
    88      * Sets to the given value.
    89      *
    90      * @param newValue the new value
    91      */
    92     public final void set(long newValue) {
    93         value = newValue;
    94     }
    95 
    96     /**
    97      * Eventually sets to the given value.
    98      *
    99      * @param newValue the new value
   100      * @since 1.6
   101      */
   102     public final void lazySet(long newValue) {
   103         value = newValue;
   104     }
   105 
   106     /**
   107      * Atomically sets to the given value and returns the old value.
   108      *
   109      * @param newValue the new value
   110      * @return the previous value
   111      */
   112     public final long getAndSet(long newValue) {
   113         while (true) {
   114             long current = get();
   115             if (compareAndSet(current, newValue))
   116                 return current;
   117         }
   118     }
   119 
   120     /**
   121      * Atomically sets the value to the given updated value
   122      * if the current value {@code ==} the expected value.
   123      *
   124      * @param expect the expected value
   125      * @param update the new value
   126      * @return true if successful. False return indicates that
   127      * the actual value was not equal to the expected value.
   128      */
   129     public final boolean compareAndSet(long expect, long update) {
   130         if (value == expect) {
   131             value = update;
   132             return true;
   133         } else {
   134             return false;
   135         }
   136     }
   137 
   138     /**
   139      * Atomically sets the value to the given updated value
   140      * if the current value {@code ==} the expected value.
   141      *
   142      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   143      * and does not provide ordering guarantees, so is only rarely an
   144      * appropriate alternative to {@code compareAndSet}.
   145      *
   146      * @param expect the expected value
   147      * @param update the new value
   148      * @return true if successful.
   149      */
   150     public final boolean weakCompareAndSet(long expect, long update) {
   151         return compareAndSet(expect, update);
   152     }
   153 
   154     /**
   155      * Atomically increments by one the current value.
   156      *
   157      * @return the previous value
   158      */
   159     public final long getAndIncrement() {
   160         while (true) {
   161             long current = get();
   162             long next = current + 1;
   163             if (compareAndSet(current, next))
   164                 return current;
   165         }
   166     }
   167 
   168     /**
   169      * Atomically decrements by one the current value.
   170      *
   171      * @return the previous value
   172      */
   173     public final long getAndDecrement() {
   174         while (true) {
   175             long current = get();
   176             long next = current - 1;
   177             if (compareAndSet(current, next))
   178                 return current;
   179         }
   180     }
   181 
   182     /**
   183      * Atomically adds the given value to the current value.
   184      *
   185      * @param delta the value to add
   186      * @return the previous value
   187      */
   188     public final long getAndAdd(long delta) {
   189         while (true) {
   190             long current = get();
   191             long next = current + delta;
   192             if (compareAndSet(current, next))
   193                 return current;
   194         }
   195     }
   196 
   197     /**
   198      * Atomically increments by one the current value.
   199      *
   200      * @return the updated value
   201      */
   202     public final long incrementAndGet() {
   203         for (;;) {
   204             long current = get();
   205             long next = current + 1;
   206             if (compareAndSet(current, next))
   207                 return next;
   208         }
   209     }
   210 
   211     /**
   212      * Atomically decrements by one the current value.
   213      *
   214      * @return the updated value
   215      */
   216     public final long decrementAndGet() {
   217         for (;;) {
   218             long current = get();
   219             long next = current - 1;
   220             if (compareAndSet(current, next))
   221                 return next;
   222         }
   223     }
   224 
   225     /**
   226      * Atomically adds the given value to the current value.
   227      *
   228      * @param delta the value to add
   229      * @return the updated value
   230      */
   231     public final long addAndGet(long delta) {
   232         for (;;) {
   233             long current = get();
   234             long next = current + delta;
   235             if (compareAndSet(current, next))
   236                 return next;
   237         }
   238     }
   239 
   240     /**
   241      * Returns the String representation of the current value.
   242      * @return the String representation of the current value.
   243      */
   244     public String toString() {
   245         return Long.toString(get());
   246     }
   247 
   248 
   249     public int intValue() {
   250         return (int)get();
   251     }
   252 
   253     public long longValue() {
   254         return get();
   255     }
   256 
   257     public float floatValue() {
   258         return (float)get();
   259     }
   260 
   261     public double doubleValue() {
   262         return (double)get();
   263     }
   264 
   265 }