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
jtulach@1334
     1
/*
jtulach@1334
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     3
 *
jtulach@1334
     4
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     5
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     6
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     7
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     8
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
     9
 *
jtulach@1334
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    14
 * accompanied this code).
jtulach@1334
    15
 *
jtulach@1334
    16
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    19
 *
jtulach@1334
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    21
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    22
 * questions.
jtulach@1334
    23
 */
jtulach@1334
    24
jtulach@1334
    25
/*
jtulach@1334
    26
 * This file is available under and governed by the GNU General Public
jtulach@1334
    27
 * License version 2 only, as published by the Free Software Foundation.
jtulach@1334
    28
 * However, the following notice accompanied the original version of this
jtulach@1334
    29
 * file:
jtulach@1334
    30
 *
jtulach@1334
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jtulach@1334
    32
 * Expert Group and released to the public domain, as explained at
jtulach@1334
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jtulach@1334
    34
 */
jtulach@1334
    35
jtulach@1334
    36
package java.util.concurrent.atomic;
jtulach@1334
    37
jtulach@1334
    38
/**
jtulach@1334
    39
 * A {@code long} value that may be updated atomically.  See the
jtulach@1334
    40
 * {@link java.util.concurrent.atomic} package specification for
jtulach@1334
    41
 * description of the properties of atomic variables. An
jtulach@1334
    42
 * {@code AtomicLong} is used in applications such as atomically
jtulach@1334
    43
 * incremented sequence numbers, and cannot be used as a replacement
jtulach@1334
    44
 * for a {@link java.lang.Long}. However, this class does extend
jtulach@1334
    45
 * {@code Number} to allow uniform access by tools and utilities that
jtulach@1334
    46
 * deal with numerically-based classes.
jtulach@1334
    47
 *
jtulach@1334
    48
 * @since 1.5
jtulach@1334
    49
 * @author Doug Lea
jtulach@1334
    50
 */
jtulach@1334
    51
public class AtomicLong extends Number implements java.io.Serializable {
jtulach@1334
    52
    private static final long serialVersionUID = 1927816293512124184L;
jtulach@1334
    53
jtulach@1334
    54
jtulach@1334
    55
    /**
jtulach@1334
    56
     * Returns whether underlying JVM supports lockless CompareAndSet
jtulach@1334
    57
     * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
jtulach@1334
    58
     */
jtulach@1334
    59
    private static native boolean VMSupportsCS8();
jtulach@1334
    60
jtulach@1334
    61
    private volatile long value;
jtulach@1334
    62
jtulach@1334
    63
    /**
jtulach@1334
    64
     * Creates a new AtomicLong with the given initial value.
jtulach@1334
    65
     *
jtulach@1334
    66
     * @param initialValue the initial value
jtulach@1334
    67
     */
jtulach@1334
    68
    public AtomicLong(long initialValue) {
jtulach@1334
    69
        value = initialValue;
jtulach@1334
    70
    }
jtulach@1334
    71
jtulach@1334
    72
    /**
jtulach@1334
    73
     * Creates a new AtomicLong with initial value {@code 0}.
jtulach@1334
    74
     */
jtulach@1334
    75
    public AtomicLong() {
jtulach@1334
    76
    }
jtulach@1334
    77
jtulach@1334
    78
    /**
jtulach@1334
    79
     * Gets the current value.
jtulach@1334
    80
     *
jtulach@1334
    81
     * @return the current value
jtulach@1334
    82
     */
jtulach@1334
    83
    public final long get() {
jtulach@1334
    84
        return value;
jtulach@1334
    85
    }
jtulach@1334
    86
jtulach@1334
    87
    /**
jtulach@1334
    88
     * Sets to the given value.
jtulach@1334
    89
     *
jtulach@1334
    90
     * @param newValue the new value
jtulach@1334
    91
     */
jtulach@1334
    92
    public final void set(long newValue) {
jtulach@1334
    93
        value = newValue;
jtulach@1334
    94
    }
jtulach@1334
    95
jtulach@1334
    96
    /**
jtulach@1334
    97
     * Eventually sets to the given value.
jtulach@1334
    98
     *
jtulach@1334
    99
     * @param newValue the new value
jtulach@1334
   100
     * @since 1.6
jtulach@1334
   101
     */
jtulach@1334
   102
    public final void lazySet(long newValue) {
jaroslav@1338
   103
        value = newValue;
jtulach@1334
   104
    }
jtulach@1334
   105
jtulach@1334
   106
    /**
jtulach@1334
   107
     * Atomically sets to the given value and returns the old value.
jtulach@1334
   108
     *
jtulach@1334
   109
     * @param newValue the new value
jtulach@1334
   110
     * @return the previous value
jtulach@1334
   111
     */
jtulach@1334
   112
    public final long getAndSet(long newValue) {
jtulach@1334
   113
        while (true) {
jtulach@1334
   114
            long current = get();
jtulach@1334
   115
            if (compareAndSet(current, newValue))
jtulach@1334
   116
                return current;
jtulach@1334
   117
        }
jtulach@1334
   118
    }
jtulach@1334
   119
jtulach@1334
   120
    /**
jtulach@1334
   121
     * Atomically sets the value to the given updated value
jtulach@1334
   122
     * if the current value {@code ==} the expected value.
jtulach@1334
   123
     *
jtulach@1334
   124
     * @param expect the expected value
jtulach@1334
   125
     * @param update the new value
jtulach@1334
   126
     * @return true if successful. False return indicates that
jtulach@1334
   127
     * the actual value was not equal to the expected value.
jtulach@1334
   128
     */
jtulach@1334
   129
    public final boolean compareAndSet(long expect, long update) {
jaroslav@1338
   130
        if (value == expect) {
jaroslav@1338
   131
            value = update;
jaroslav@1338
   132
            return true;
jaroslav@1338
   133
        } else {
jaroslav@1338
   134
            return false;
jaroslav@1338
   135
        }
jtulach@1334
   136
    }
jtulach@1334
   137
jtulach@1334
   138
    /**
jtulach@1334
   139
     * Atomically sets the value to the given updated value
jtulach@1334
   140
     * if the current value {@code ==} the expected value.
jtulach@1334
   141
     *
jtulach@1334
   142
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
jtulach@1334
   143
     * and does not provide ordering guarantees, so is only rarely an
jtulach@1334
   144
     * appropriate alternative to {@code compareAndSet}.
jtulach@1334
   145
     *
jtulach@1334
   146
     * @param expect the expected value
jtulach@1334
   147
     * @param update the new value
jtulach@1334
   148
     * @return true if successful.
jtulach@1334
   149
     */
jtulach@1334
   150
    public final boolean weakCompareAndSet(long expect, long update) {
jaroslav@1338
   151
        return compareAndSet(expect, update);
jtulach@1334
   152
    }
jtulach@1334
   153
jtulach@1334
   154
    /**
jtulach@1334
   155
     * Atomically increments by one the current value.
jtulach@1334
   156
     *
jtulach@1334
   157
     * @return the previous value
jtulach@1334
   158
     */
jtulach@1334
   159
    public final long getAndIncrement() {
jtulach@1334
   160
        while (true) {
jtulach@1334
   161
            long current = get();
jtulach@1334
   162
            long next = current + 1;
jtulach@1334
   163
            if (compareAndSet(current, next))
jtulach@1334
   164
                return current;
jtulach@1334
   165
        }
jtulach@1334
   166
    }
jtulach@1334
   167
jtulach@1334
   168
    /**
jtulach@1334
   169
     * Atomically decrements by one the current value.
jtulach@1334
   170
     *
jtulach@1334
   171
     * @return the previous value
jtulach@1334
   172
     */
jtulach@1334
   173
    public final long getAndDecrement() {
jtulach@1334
   174
        while (true) {
jtulach@1334
   175
            long current = get();
jtulach@1334
   176
            long next = current - 1;
jtulach@1334
   177
            if (compareAndSet(current, next))
jtulach@1334
   178
                return current;
jtulach@1334
   179
        }
jtulach@1334
   180
    }
jtulach@1334
   181
jtulach@1334
   182
    /**
jtulach@1334
   183
     * Atomically adds the given value to the current value.
jtulach@1334
   184
     *
jtulach@1334
   185
     * @param delta the value to add
jtulach@1334
   186
     * @return the previous value
jtulach@1334
   187
     */
jtulach@1334
   188
    public final long getAndAdd(long delta) {
jtulach@1334
   189
        while (true) {
jtulach@1334
   190
            long current = get();
jtulach@1334
   191
            long next = current + delta;
jtulach@1334
   192
            if (compareAndSet(current, next))
jtulach@1334
   193
                return current;
jtulach@1334
   194
        }
jtulach@1334
   195
    }
jtulach@1334
   196
jtulach@1334
   197
    /**
jtulach@1334
   198
     * Atomically increments by one the current value.
jtulach@1334
   199
     *
jtulach@1334
   200
     * @return the updated value
jtulach@1334
   201
     */
jtulach@1334
   202
    public final long incrementAndGet() {
jtulach@1334
   203
        for (;;) {
jtulach@1334
   204
            long current = get();
jtulach@1334
   205
            long next = current + 1;
jtulach@1334
   206
            if (compareAndSet(current, next))
jtulach@1334
   207
                return next;
jtulach@1334
   208
        }
jtulach@1334
   209
    }
jtulach@1334
   210
jtulach@1334
   211
    /**
jtulach@1334
   212
     * Atomically decrements by one the current value.
jtulach@1334
   213
     *
jtulach@1334
   214
     * @return the updated value
jtulach@1334
   215
     */
jtulach@1334
   216
    public final long decrementAndGet() {
jtulach@1334
   217
        for (;;) {
jtulach@1334
   218
            long current = get();
jtulach@1334
   219
            long next = current - 1;
jtulach@1334
   220
            if (compareAndSet(current, next))
jtulach@1334
   221
                return next;
jtulach@1334
   222
        }
jtulach@1334
   223
    }
jtulach@1334
   224
jtulach@1334
   225
    /**
jtulach@1334
   226
     * Atomically adds the given value to the current value.
jtulach@1334
   227
     *
jtulach@1334
   228
     * @param delta the value to add
jtulach@1334
   229
     * @return the updated value
jtulach@1334
   230
     */
jtulach@1334
   231
    public final long addAndGet(long delta) {
jtulach@1334
   232
        for (;;) {
jtulach@1334
   233
            long current = get();
jtulach@1334
   234
            long next = current + delta;
jtulach@1334
   235
            if (compareAndSet(current, next))
jtulach@1334
   236
                return next;
jtulach@1334
   237
        }
jtulach@1334
   238
    }
jtulach@1334
   239
jtulach@1334
   240
    /**
jtulach@1334
   241
     * Returns the String representation of the current value.
jtulach@1334
   242
     * @return the String representation of the current value.
jtulach@1334
   243
     */
jtulach@1334
   244
    public String toString() {
jtulach@1334
   245
        return Long.toString(get());
jtulach@1334
   246
    }
jtulach@1334
   247
jtulach@1334
   248
jtulach@1334
   249
    public int intValue() {
jtulach@1334
   250
        return (int)get();
jtulach@1334
   251
    }
jtulach@1334
   252
jtulach@1334
   253
    public long longValue() {
jtulach@1334
   254
        return get();
jtulach@1334
   255
    }
jtulach@1334
   256
jtulach@1334
   257
    public float floatValue() {
jtulach@1334
   258
        return (float)get();
jtulach@1334
   259
    }
jtulach@1334
   260
jtulach@1334
   261
    public double doubleValue() {
jtulach@1334
   262
        return (double)get();
jtulach@1334
   263
    }
jtulach@1334
   264
jtulach@1334
   265
}