0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/Apackage java.util.concurrent.atomic;
0N/A
0N/A/**
0N/A * An {@code AtomicStampedReference} maintains an object reference
0N/A * along with an integer "stamp", that can be updated atomically.
0N/A *
3387N/A * <p>Implementation note: This implementation maintains stamped
0N/A * references by creating internal objects representing "boxed"
0N/A * [reference, integer] pairs.
0N/A *
0N/A * @since 1.5
0N/A * @author Doug Lea
0N/A * @param <V> The type of object referred to by this reference
0N/A */
3387N/Apublic class AtomicStampedReference<V> {
0N/A
3387N/A private static class Pair<T> {
3387N/A final T reference;
3387N/A final int stamp;
3387N/A private Pair(T reference, int stamp) {
3387N/A this.reference = reference;
3387N/A this.stamp = stamp;
3387N/A }
3387N/A static <T> Pair<T> of(T reference, int stamp) {
3387N/A return new Pair<T>(reference, stamp);
0N/A }
0N/A }
0N/A
3387N/A private volatile Pair<V> pair;
0N/A
0N/A /**
0N/A * Creates a new {@code AtomicStampedReference} with the given
0N/A * initial values.
0N/A *
0N/A * @param initialRef the initial reference
0N/A * @param initialStamp the initial stamp
0N/A */
0N/A public AtomicStampedReference(V initialRef, int initialStamp) {
3387N/A pair = Pair.of(initialRef, initialStamp);
0N/A }
0N/A
0N/A /**
0N/A * Returns the current value of the reference.
0N/A *
0N/A * @return the current value of the reference
0N/A */
0N/A public V getReference() {
3387N/A return pair.reference;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current value of the stamp.
0N/A *
0N/A * @return the current value of the stamp
0N/A */
0N/A public int getStamp() {
3387N/A return pair.stamp;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current values of both the reference and the stamp.
0N/A * Typical usage is {@code int[1] holder; ref = v.get(holder); }.
0N/A *
0N/A * @param stampHolder an array of size of at least one. On return,
0N/A * {@code stampholder[0]} will hold the value of the stamp.
0N/A * @return the current value of the reference
0N/A */
0N/A public V get(int[] stampHolder) {
3387N/A Pair<V> pair = this.pair;
3387N/A stampHolder[0] = pair.stamp;
3387N/A return pair.reference;
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value of both the reference and stamp
0N/A * to the given update values if the
0N/A * current reference is {@code ==} to the expected reference
0N/A * and the current stamp is equal to the expected stamp.
0N/A *
0N/A * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
0N/A * and does not provide ordering guarantees, so is only rarely an
0N/A * appropriate alternative to {@code compareAndSet}.
0N/A *
0N/A * @param expectedReference the expected value of the reference
0N/A * @param newReference the new value for the reference
0N/A * @param expectedStamp the expected value of the stamp
0N/A * @param newStamp the new value for the stamp
0N/A * @return true if successful
0N/A */
3387N/A public boolean weakCompareAndSet(V expectedReference,
3387N/A V newReference,
3387N/A int expectedStamp,
3387N/A int newStamp) {
3387N/A return compareAndSet(expectedReference, newReference,
3387N/A expectedStamp, newStamp);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value of both the reference and stamp
0N/A * to the given update values if the
0N/A * current reference is {@code ==} to the expected reference
0N/A * and the current stamp is equal to the expected stamp.
0N/A *
0N/A * @param expectedReference the expected value of the reference
0N/A * @param newReference the new value for the reference
0N/A * @param expectedStamp the expected value of the stamp
0N/A * @param newStamp the new value for the stamp
0N/A * @return true if successful
0N/A */
3387N/A public boolean compareAndSet(V expectedReference,
3387N/A V newReference,
3387N/A int expectedStamp,
3387N/A int newStamp) {
3387N/A Pair<V> current = pair;
3387N/A return
3387N/A expectedReference == current.reference &&
3387N/A expectedStamp == current.stamp &&
0N/A ((newReference == current.reference &&
3387N/A newStamp == current.stamp) ||
3387N/A casPair(current, Pair.of(newReference, newStamp)));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Unconditionally sets the value of both the reference and stamp.
0N/A *
0N/A * @param newReference the new value for the reference
0N/A * @param newStamp the new value for the stamp
0N/A */
0N/A public void set(V newReference, int newStamp) {
3387N/A Pair<V> current = pair;
3387N/A if (newReference != current.reference || newStamp != current.stamp)
3387N/A this.pair = Pair.of(newReference, newStamp);
0N/A }
0N/A
0N/A /**
0N/A * Atomically sets the value of the stamp to the given update value
0N/A * if the current reference is {@code ==} to the expected
0N/A * reference. Any given invocation of this operation may fail
0N/A * (return {@code false}) spuriously, but repeated invocation
0N/A * when the current value holds the expected value and no other
0N/A * thread is also attempting to set the value will eventually
0N/A * succeed.
0N/A *
0N/A * @param expectedReference the expected value of the reference
0N/A * @param newStamp the new value for the stamp
0N/A * @return true if successful
0N/A */
0N/A public boolean attemptStamp(V expectedReference, int newStamp) {
3387N/A Pair<V> current = pair;
3387N/A return
3387N/A expectedReference == current.reference &&
3387N/A (newStamp == current.stamp ||
3387N/A casPair(current, Pair.of(expectedReference, newStamp)));
3387N/A }
3387N/A
3387N/A // Unsafe mechanics
3387N/A
3387N/A private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
3387N/A private static final long pairOffset =
3387N/A objectFieldOffset(UNSAFE, "pair", AtomicStampedReference.class);
3387N/A
3387N/A private boolean casPair(Pair<V> cmp, Pair<V> val) {
3387N/A return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
3387N/A }
3387N/A
3387N/A static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
3387N/A String field, Class<?> klazz) {
3387N/A try {
3387N/A return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
3387N/A } catch (NoSuchFieldException e) {
3387N/A // Convert Exception to corresponding Error
3387N/A NoSuchFieldError error = new NoSuchFieldError(field);
3387N/A error.initCause(e);
3387N/A throw error;
3387N/A }
0N/A }
0N/A}