jdk/src/java.base/share/classes/java/util/concurrent/atomic/AtomicLong.java
author dl
Tue, 29 Sep 2015 16:44:29 +0100
changeset 32838 caeef2c79243
parent 25859 3317bb8137f4
child 33674 566777f73c32
permissions -rw-r--r--
8134854: Bulk integration of java.util.concurrent.atomic classes Reviewed-by: martin, psandoz, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.atomic;
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    37
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
    38
import java.util.function.LongBinaryOperator;
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    39
import java.util.function.LongUnaryOperator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A {@code long} value that may be updated atomically.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@link java.util.concurrent.atomic} package specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * description of the properties of atomic variables. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@code AtomicLong} is used in applications such as atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * incremented sequence numbers, and cannot be used as a replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * for a {@link java.lang.Long}. However, this class does extend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@code Number} to allow uniform access by tools and utilities that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * deal with numerically-based classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class AtomicLong extends Number implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long serialVersionUID = 1927816293512124184L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    57
    private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    58
    private static final long VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Records whether the underlying JVM supports lockless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * compareAndSwap for longs. While the Unsafe.compareAndSwapLong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * method works in either case, some constructions should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * handled at Java level to avoid locking user-visible locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final boolean VM_SUPPORTS_LONG_CAS = VMSupportsCS8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns whether underlying JVM supports lockless CompareAndSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static native boolean VMSupportsCS8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static {
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 10067
diff changeset
    75
        try {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    76
            VALUE = U.objectFieldOffset
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 10067
diff changeset
    77
                (AtomicLong.class.getDeclaredField("value"));
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    78
        } catch (ReflectiveOperationException e) {
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    79
            throw new Error(e);
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    80
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private volatile long value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates a new AtomicLong with the given initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param initialValue the initial value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public AtomicLong(long initialValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        value = initialValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Creates a new AtomicLong with initial value {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public AtomicLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Gets the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public final long get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Sets to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public final void set(long newValue) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   115
        // Use putLongVolatile instead of ordinary volatile store when
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   116
        // using compareAndSwapLong, for sake of some 32bit systems.
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   117
        U.putLongVolatile(this, VALUE, newValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Eventually sets to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public final void lazySet(long newValue) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   127
        U.putOrderedLong(this, VALUE, newValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Atomically sets to the given value and returns the old value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public final long getAndSet(long newValue) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   137
        return U.getAndSetLong(this, VALUE, newValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Atomically sets the value to the given updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * if the current value {@code ==} the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param update the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 16011
diff changeset
   146
     * @return {@code true} if successful. False return indicates that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the actual value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public final boolean compareAndSet(long expect, long update) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   150
        return U.compareAndSwapLong(this, VALUE, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Atomically sets the value to the given updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * if the current value {@code ==} the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 16011
diff changeset
   157
     * <p><a href="package-summary.html#weakCompareAndSet">May fail
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 16011
diff changeset
   158
     * spuriously and does not provide ordering guarantees</a>, so is
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 16011
diff changeset
   159
     * only rarely an appropriate alternative to {@code compareAndSet}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param expect the expected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param update the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 16011
diff changeset
   163
     * @return {@code true} if successful
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public final boolean weakCompareAndSet(long expect, long update) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   166
        return U.compareAndSwapLong(this, VALUE, expect, update);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Atomically increments by one the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public final long getAndIncrement() {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   175
        return U.getAndAddLong(this, VALUE, 1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Atomically decrements by one the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public final long getAndDecrement() {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   184
        return U.getAndAddLong(this, VALUE, -1L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Atomically adds the given value to the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public final long getAndAdd(long delta) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   194
        return U.getAndAddLong(this, VALUE, delta);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Atomically increments by one the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public final long incrementAndGet() {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   203
        return U.getAndAddLong(this, VALUE, 1L) + 1L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Atomically decrements by one the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public final long decrementAndGet() {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   212
        return U.getAndAddLong(this, VALUE, -1L) - 1L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Atomically adds the given value to the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param delta the value to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return the updated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public final long addAndGet(long delta) {
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   222
        return U.getAndAddLong(this, VALUE, delta) + delta;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   226
     * Atomically updates the current value with the results of
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   227
     * applying the given function, returning the previous value. The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   228
     * function should be side-effect-free, since it may be re-applied
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   229
     * when attempted updates fail due to contention among threads.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   230
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   231
     * @param updateFunction a side-effect-free function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   232
     * @return the previous value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   233
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   234
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   235
    public final long getAndUpdate(LongUnaryOperator updateFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   236
        long prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   237
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   238
            prev = get();
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   239
            next = updateFunction.applyAsLong(prev);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   240
        } while (!compareAndSet(prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   241
        return prev;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   242
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   243
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   244
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   245
     * Atomically updates the current value with the results of
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   246
     * applying the given function, returning the updated value. The
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   247
     * function should be side-effect-free, since it may be re-applied
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   248
     * when attempted updates fail due to contention among threads.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   249
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   250
     * @param updateFunction a side-effect-free function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   251
     * @return the updated value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   252
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   253
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   254
    public final long updateAndGet(LongUnaryOperator updateFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   255
        long prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   256
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   257
            prev = get();
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   258
            next = updateFunction.applyAsLong(prev);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   259
        } while (!compareAndSet(prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   260
        return next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   261
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   262
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   263
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   264
     * Atomically updates the current value with the results of
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   265
     * applying the given function to the current and given values,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   266
     * returning the previous value. The function should be
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   267
     * side-effect-free, since it may be re-applied when attempted
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   268
     * updates fail due to contention among threads.  The function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   269
     * is applied with the current value as its first argument,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   270
     * and the given update as the second argument.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   271
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   272
     * @param x the update value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   273
     * @param accumulatorFunction a side-effect-free function of two arguments
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   274
     * @return the previous value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   275
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   276
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   277
    public final long getAndAccumulate(long x,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   278
                                       LongBinaryOperator accumulatorFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   279
        long prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   280
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   281
            prev = get();
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   282
            next = accumulatorFunction.applyAsLong(prev, x);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   283
        } while (!compareAndSet(prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   284
        return prev;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   285
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   286
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   287
    /**
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   288
     * Atomically updates the current value with the results of
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   289
     * applying the given function to the current and given values,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   290
     * returning the updated value. The function should be
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   291
     * side-effect-free, since it may be re-applied when attempted
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   292
     * updates fail due to contention among threads.  The function
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   293
     * is applied with the current value as its first argument,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   294
     * and the given update as the second argument.
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   295
     *
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   296
     * @param x the update value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   297
     * @param accumulatorFunction a side-effect-free function of two arguments
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   298
     * @return the updated value
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   299
     * @since 1.8
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   300
     */
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   301
    public final long accumulateAndGet(long x,
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   302
                                       LongBinaryOperator accumulatorFunction) {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   303
        long prev, next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   304
        do {
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   305
            prev = get();
16011
890a7ed97f6c 8004561: Additional functional interfaces, extension methods and name changes
mduigou
parents: 15267
diff changeset
   306
            next = accumulatorFunction.applyAsLong(prev, x);
15267
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   307
        } while (!compareAndSet(prev, next));
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   308
        return next;
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   309
    }
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   310
c884f548a25f 8001666: Add lambda-compatible atomics and accumulators to the ActomicXXX classes
dl
parents: 15020
diff changeset
   311
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the String representation of the current value.
15020
50394fa17c1b 8006007: j.u.c.atomic classes should use intrinsic getAndXXX provided by 7023898
chegar
parents: 14325
diff changeset
   313
     * @return the String representation of the current value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return Long.toString(get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   319
    /**
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   320
     * Returns the value of this {@code AtomicLong} as an {@code int}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   321
     * after a narrowing primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   322
     * @jls 5.1.3 Narrowing Primitive Conversions
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   323
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return (int)get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   328
    /**
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 10067
diff changeset
   329
     * Returns the value of this {@code AtomicLong} as a {@code long}.
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
   330
     * Equivalent to {@link #get()}.
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   331
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   336
    /**
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   337
     * Returns the value of this {@code AtomicLong} as a {@code float}
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   338
     * after a widening primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   339
     * @jls 5.1.2 Widening Primitive Conversions
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   340
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return (float)get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   345
    /**
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 10067
diff changeset
   346
     * Returns the value of this {@code AtomicLong} as a {@code double}
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 10067
diff changeset
   347
     * after a widening primitive conversion.
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   348
     * @jls 5.1.2 Widening Primitive Conversions
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9242
diff changeset
   349
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return (double)get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}