1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/licenses/publicdomain 34 */ 35 36 package java.util.concurrent.atomic; 37 38 /** 39 * An {@code AtomicStampedReference} maintains an object reference 40 * along with an integer "stamp", that can be updated atomically. 41 * 42 * <p> Implementation note. This implementation maintains stamped 43 * references by creating internal objects representing "boxed" 44 * [reference, integer] pairs. 45 * 46 * @since 1.5 47 * @author Doug Lea 48 * @param <V> The type of object referred to by this reference 49 */ 50 public class AtomicStampedReference<V> { 51 52 private static class ReferenceIntegerPair<T> { 53 private final T reference; 54 private final int integer; 55 ReferenceIntegerPair(T r, int i) { 56 reference = r; integer = i; 57 } 58 } 59 60 private final AtomicReference<ReferenceIntegerPair<V>> atomicRef; 61 62 /** 63 * Creates a new {@code AtomicStampedReference} with the given 64 * initial values. 65 * 66 * @param initialRef the initial reference 67 * @param initialStamp the initial stamp 68 */ 69 public AtomicStampedReference(V initialRef, int initialStamp) { 70 atomicRef = new AtomicReference<ReferenceIntegerPair<V>> 71 (new ReferenceIntegerPair<V>(initialRef, initialStamp)); 72 } 73 74 /** 75 * Returns the current value of the reference. 76 * 77 * @return the current value of the reference 78 */ 79 public V getReference() { 80 return atomicRef.get().reference; 81 } 82 83 /** 84 * Returns the current value of the stamp. 85 * 86 * @return the current value of the stamp 87 */ 88 public int getStamp() { 89 return atomicRef.get().integer; 90 } 91 92 /** 93 * Returns the current values of both the reference and the stamp. 94 * Typical usage is {@code int[1] holder; ref = v.get(holder); }. 95 * 96 * @param stampHolder an array of size of at least one. On return, 97 * {@code stampholder[0]} will hold the value of the stamp. 98 * @return the current value of the reference 99 */ 100 public V get(int[] stampHolder) { 101 ReferenceIntegerPair<V> p = atomicRef.get(); 102 stampHolder[0] = p.integer; 103 return p.reference; 104 } 105 106 /** 107 * Atomically sets the value of both the reference and stamp 108 * to the given update values if the 109 * current reference is {@code ==} to the expected reference 110 * and the current stamp is equal to the expected stamp. 111 * 112 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 113 * and does not provide ordering guarantees, so is only rarely an 114 * appropriate alternative to {@code compareAndSet}. 115 * 116 * @param expectedReference the expected value of the reference 117 * @param newReference the new value for the reference 118 * @param expectedStamp the expected value of the stamp 119 * @param newStamp the new value for the stamp 120 * @return true if successful 121 */ 122 public boolean weakCompareAndSet(V expectedReference, 123 V newReference, 124 int expectedStamp, 125 int newStamp) { 126 ReferenceIntegerPair<V> current = atomicRef.get(); 127 return expectedReference == current.reference && 128 expectedStamp == current.integer && 129 ((newReference == current.reference && 130 newStamp == current.integer) || 131 atomicRef.weakCompareAndSet(current, 132 new ReferenceIntegerPair<V>(newReference, 133 newStamp))); 134 } 135 136 /** 137 * Atomically sets the value of both the reference and stamp 138 * to the given update values if the 139 * current reference is {@code ==} to the expected reference 140 * and the current stamp is equal to the expected stamp. 141 * 142 * @param expectedReference the expected value of the reference 143 * @param newReference the new value for the reference 144 * @param expectedStamp the expected value of the stamp 145 * @param newStamp the new value for the stamp 146 * @return true if successful 147 */ 148 public boolean compareAndSet(V expectedReference, 149 V newReference, 150 int expectedStamp, 151 int newStamp) { 152 ReferenceIntegerPair<V> current = atomicRef.get(); 153 return expectedReference == current.reference && 154 expectedStamp == current.integer && 155 ((newReference == current.reference && 156 newStamp == current.integer) || 157 atomicRef.compareAndSet(current, 158 new ReferenceIntegerPair<V>(newReference, 159 newStamp))); 160 } 161 162 163 /** 164 * Unconditionally sets the value of both the reference and stamp. 165 * 166 * @param newReference the new value for the reference 167 * @param newStamp the new value for the stamp 168 */ 169 public void set(V newReference, int newStamp) { 170 ReferenceIntegerPair<V> current = atomicRef.get(); 171 if (newReference != current.reference || newStamp != current.integer) 172 atomicRef.set(new ReferenceIntegerPair<V>(newReference, newStamp)); 173 } 174 175 /** 176 * Atomically sets the value of the stamp to the given update value 177 * if the current reference is {@code ==} to the expected 178 * reference. Any given invocation of this operation may fail 179 * (return {@code false}) spuriously, but repeated invocation 180 * when the current value holds the expected value and no other 181 * thread is also attempting to set the value will eventually 182 * succeed. 183 * 184 * @param expectedReference the expected value of the reference 185 * @param newStamp the new value for the stamp 186 * @return true if successful 187 */ 188 public boolean attemptStamp(V expectedReference, int newStamp) { 189 ReferenceIntegerPair<V> current = atomicRef.get(); 190 return expectedReference == current.reference && 191 (newStamp == current.integer || 192 atomicRef.compareAndSet(current, 193 new ReferenceIntegerPair<V>(expectedReference, 194 newStamp))); 195 } 196 } --- EOF ---