src/java.base/share/classes/java/util/concurrent/atomic/AtomicBoolean.java
author dl
Fri, 08 Dec 2017 15:26:56 -0800
changeset 48232 bf476235671a
parent 47216 71c04702a3d5
child 49565 b5705ade8c8d
permissions -rw-r--r--
8192943: Optimize atomic accumulators using VarHandle getAndSet 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    39
import java.lang.invoke.VarHandle;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A {@code boolean} value that may be updated atomically. See the
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    43
 * {@link VarHandle} specification for descriptions of the properties
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    44
 * of atomic accesses. An {@code AtomicBoolean} is used in
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    45
 * applications such as atomically updated flags, and cannot be used
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    46
 * as a replacement for a {@link java.lang.Boolean}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class AtomicBoolean implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final long serialVersionUID = 4654671469794556979L;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    53
    private static final VarHandle VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static {
11134
9ff7640994bf 7117360: Warnings in java.util.concurrent.atomic package
dl
parents: 9242
diff changeset
    55
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    56
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    57
            VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
32838
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    58
        } catch (ReflectiveOperationException e) {
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    59
            throw new Error(e);
caeef2c79243 8134854: Bulk integration of java.util.concurrent.atomic classes
dl
parents: 25859
diff changeset
    60
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private volatile int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a new {@code AtomicBoolean} with the given initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param initialValue the initial value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public AtomicBoolean(boolean initialValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        value = initialValue ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Creates a new {@code AtomicBoolean} with initial value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public AtomicBoolean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    81
     * Returns the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    82
     * with memory effects as specified by {@link VarHandle#getVolatile}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public final boolean get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return value != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    91
     * Atomically sets the value to {@code newValue}
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    92
     * if the current value {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    93
     * with memory effects as specified by {@link VarHandle#compareAndSet}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    95
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
    96
     * @param newValue the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15020
diff changeset
    97
     * @return {@code true} if successful. False return indicates that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * the actual value was not equal to the expected value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   100
    public final boolean compareAndSet(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   101
        return VALUE.compareAndSet(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   102
                                   (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   103
                                   (newValue ? 1 : 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   107
     * Possibly atomically sets the value to {@code newValue}
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   108
     * if the current value {@code == expectedValue},
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   109
     * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
41415
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   111
     * @deprecated This method has plain memory effects but the method
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   112
     * name implies volatile memory effects (see methods such as
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   113
     * {@link #compareAndExchange} and {@link #compareAndSet}).  To avoid
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   114
     * confusion over plain or volatile memory effects it is recommended that
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   115
     * the method {@link #weakCompareAndSetPlain} be used instead.
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   116
     *
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   117
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   118
     * @param newValue the new value
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 15020
diff changeset
   119
     * @return {@code true} if successful
41415
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   120
     * @see #weakCompareAndSetPlain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
41415
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   122
    @Deprecated(since="9")
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   123
    public boolean weakCompareAndSet(boolean expectedValue, boolean newValue) {
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   124
        return VALUE.weakCompareAndSetPlain(this,
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   125
                                            (expectedValue ? 1 : 0),
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   126
                                            (newValue ? 1 : 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
41415
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   130
     * Possibly atomically sets the value to {@code newValue}
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   131
     * if the current value {@code == expectedValue},
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   132
     * with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}.
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   133
     *
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   134
     * @param expectedValue the expected value
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   135
     * @param newValue the new value
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   136
     * @return {@code true} if successful
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   137
     * @since 9
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   138
     */
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   139
    public boolean weakCompareAndSetPlain(boolean expectedValue, boolean newValue) {
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   140
        return VALUE.weakCompareAndSetPlain(this,
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   141
                                            (expectedValue ? 1 : 0),
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   142
                                            (newValue ? 1 : 0));
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   143
    }
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   144
9c298252e385 8164814: Deprecate Atomic*.weakCompareAndSet and defer to Atomic*.weakCompareAndSetPlain
psandoz
parents: 40734
diff changeset
   145
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   146
     * Sets the value to {@code newValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   147
     * with memory effects as specified by {@link VarHandle#setVolatile}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public final void set(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        value = newValue ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   156
     * Sets the value to {@code newValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   157
     * with memory effects as specified by {@link VarHandle#setRelease}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public final void lazySet(boolean newValue) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   163
        VALUE.setRelease(this, (newValue ? 1 : 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   167
     * Atomically sets the value to {@code newValue} and returns the old value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   168
     * with memory effects as specified by {@link VarHandle#getAndSet}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param newValue the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public final boolean getAndSet(boolean newValue) {
40277
fd3c777aed08 8162805: Optimize AtomicBoolean.getAndSet
dl
parents: 39725
diff changeset
   174
        return (int)VALUE.getAndSet(this, (newValue ? 1 : 0)) != 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * 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: 11134
diff changeset
   179
     * @return the String representation of the current value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return Boolean.toString(get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   185
    // jdk9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   186
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   187
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   188
     * Returns the current value, with memory semantics of reading as
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   189
     * if the variable was declared non-{@code volatile}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   190
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   191
     * @return the value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   192
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   193
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   194
    public final boolean getPlain() {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   195
        return (int)VALUE.get(this) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   196
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   197
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   198
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   199
     * Sets the value to {@code newValue}, with memory semantics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   200
     * of setting as if the variable was declared non-{@code volatile}
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   201
     * and non-{@code final}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   202
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   203
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   204
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   205
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   206
    public final void setPlain(boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   207
        VALUE.set(this, newValue ? 1 : 0);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   208
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   209
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   210
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   211
     * Returns the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   212
     * with memory effects as specified by {@link VarHandle#getOpaque}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   213
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   214
     * @return the value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   215
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   216
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   217
    public final boolean getOpaque() {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   218
        return (int)VALUE.getOpaque(this) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   219
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   220
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   221
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   222
     * Sets the value to {@code newValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   223
     * with memory effects as specified by {@link VarHandle#setOpaque}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   224
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   225
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   226
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   227
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   228
    public final void setOpaque(boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   229
        VALUE.setOpaque(this, newValue ? 1 : 0);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   230
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   231
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   232
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   233
     * Returns the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   234
     * with memory effects as specified by {@link VarHandle#getAcquire}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   235
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   236
     * @return the value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   237
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   238
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   239
    public final boolean getAcquire() {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   240
        return (int)VALUE.getAcquire(this) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   241
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   242
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   243
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   244
     * Sets the value to {@code newValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   245
     * with memory effects as specified by {@link VarHandle#setRelease}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   246
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   247
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   248
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   249
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   250
    public final void setRelease(boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   251
        VALUE.setRelease(this, newValue ? 1 : 0);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   252
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   253
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   254
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   255
     * Atomically sets the value to {@code newValue} if the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   256
     * referred to as the <em>witness value</em>, {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   257
     * with memory effects as specified by
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   258
     * {@link VarHandle#compareAndExchange}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   259
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   260
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   261
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   262
     * @return the witness value, which will be the same as the
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   263
     * expected value if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   264
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   265
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   266
    public final boolean compareAndExchange(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   267
        return (int)VALUE.compareAndExchange(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   268
                                             (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   269
                                             (newValue ? 1 : 0)) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   270
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   271
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   272
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   273
     * Atomically sets the value to {@code newValue} if the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   274
     * referred to as the <em>witness value</em>, {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   275
     * with memory effects as specified by
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   276
     * {@link VarHandle#compareAndExchangeAcquire}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   277
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   278
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   279
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   280
     * @return the witness value, which will be the same as the
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   281
     * expected value if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   282
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   283
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   284
    public final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   285
        return (int)VALUE.compareAndExchangeAcquire(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   286
                                                    (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   287
                                                    (newValue ? 1 : 0)) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   288
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   289
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   290
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   291
     * Atomically sets the value to {@code newValue} if the current value,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   292
     * referred to as the <em>witness value</em>, {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   293
     * with memory effects as specified by
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   294
     * {@link VarHandle#compareAndExchangeRelease}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   295
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   296
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   297
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   298
     * @return the witness value, which will be the same as the
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   299
     * expected value if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   300
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   301
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   302
    public final boolean compareAndExchangeRelease(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   303
        return (int)VALUE.compareAndExchangeRelease(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   304
                                                    (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   305
                                                    (newValue ? 1 : 0)) != 0;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   306
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   307
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   308
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   309
     * Possibly atomically sets the value to {@code newValue} if the current
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   310
     * value {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   311
     * with memory effects as specified by
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   312
     * {@link VarHandle#weakCompareAndSet}.
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   313
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   314
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   315
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   316
     * @return {@code true} if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   317
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   318
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   319
    public final boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue) {
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   320
        return VALUE.weakCompareAndSet(this,
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   321
                                       (expectedValue ? 1 : 0),
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 40277
diff changeset
   322
                                       (newValue ? 1 : 0));
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   323
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   324
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   325
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   326
     * Possibly atomically sets the value to {@code newValue} if the current
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   327
     * value {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   328
     * with memory effects as specified by
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   329
     * {@link VarHandle#weakCompareAndSetAcquire}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   330
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   331
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   332
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   333
     * @return {@code true} if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   334
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   335
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   336
    public final boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   337
        return VALUE.weakCompareAndSetAcquire(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   338
                                              (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   339
                                              (newValue ? 1 : 0));
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   340
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   341
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   342
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   343
     * Possibly atomically sets the value to {@code newValue} if the current
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   344
     * value {@code == expectedValue},
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   345
     * with memory effects as specified by
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   346
     * {@link VarHandle#weakCompareAndSetRelease}.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   347
     *
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   348
     * @param expectedValue the expected value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   349
     * @param newValue the new value
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   350
     * @return {@code true} if successful
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   351
     * @since 9
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   352
     */
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   353
    public final boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   354
        return VALUE.weakCompareAndSetRelease(this,
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   355
                                              (expectedValue ? 1 : 0),
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   356
                                              (newValue ? 1 : 0));
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   357
    }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 36936
diff changeset
   358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}