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 AtomicMarkableReference} maintains an object reference 40 * along with a mark bit, that can be updated atomically. 41 * <p> 42 * <p> Implementation note. This implementation maintains markable 43 * references by creating internal objects representing "boxed" 44 * [reference, boolean] 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 AtomicMarkableReference<V> { 51 52 private static class ReferenceBooleanPair<T> { 53 private final T reference; 54 private final boolean bit; 55 ReferenceBooleanPair(T r, boolean i) { 56 reference = r; bit = i; 57 } 58 } 59 60 private final AtomicReference<ReferenceBooleanPair<V>> atomicRef; 61 62 /** 63 * Creates a new {@code AtomicMarkableReference} with the given 64 * initial values. 65 * 66 * @param initialRef the initial reference 67 * @param initialMark the initial mark 68 */ 69 public AtomicMarkableReference(V initialRef, boolean initialMark) { 70 atomicRef = new AtomicReference<ReferenceBooleanPair<V>> (new ReferenceBooleanPair<V>(initialRef, initialMark)); 71 } 72 73 /** 74 * Returns the current value of the reference. 75 * 76 * @return the current value of the reference 77 */ 78 public V getReference() { 79 return atomicRef.get().reference; 80 } 81 82 /** 83 * Returns the current value of the mark. 84 * 85 * @return the current value of the mark 86 */ 87 public boolean isMarked() { 88 return atomicRef.get().bit; 89 } 90 91 /** 92 * Returns the current values of both the reference and the mark. 93 * Typical usage is {@code boolean[1] holder; ref = v.get(holder); }. 94 * 95 * @param markHolder an array of size of at least one. On return, 96 * {@code markholder[0]} will hold the value of the mark. 97 * @return the current value of the reference 98 */ 99 public V get(boolean[] markHolder) { 100 ReferenceBooleanPair<V> p = atomicRef.get(); 101 markHolder[0] = p.bit; 102 return p.reference; 103 } 104 105 /** 106 * Atomically sets the value of both the reference and mark 107 * to the given update values if the 108 * current reference is {@code ==} to the expected reference 109 * and the current mark is equal to the expected mark. 110 * 111 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 112 * and does not provide ordering guarantees, so is only rarely an 113 * appropriate alternative to {@code compareAndSet}. 114 * 115 * @param expectedReference the expected value of the reference 116 * @param newReference the new value for the reference 117 * @param expectedMark the expected value of the mark 118 * @param newMark the new value for the mark 119 * @return true if successful 120 */ 121 public boolean weakCompareAndSet(V expectedReference, 122 V newReference, 123 boolean expectedMark, 124 boolean newMark) { 125 ReferenceBooleanPair<V> current = atomicRef.get(); 126 return expectedReference == current.reference && 127 expectedMark == current.bit && 128 ((newReference == current.reference && newMark == current.bit) || 129 atomicRef.weakCompareAndSet(current, 130 new ReferenceBooleanPair<V>(newReference, 131 newMark))); 132 } 133 134 /** 135 * Atomically sets the value of both the reference and mark 136 * to the given update values if the 137 * current reference is {@code ==} to the expected reference 138 * and the current mark is equal to the expected mark. 139 * 140 * @param expectedReference the expected value of the reference 141 * @param newReference the new value for the reference 142 * @param expectedMark the expected value of the mark 143 * @param newMark the new value for the mark 144 * @return true if successful 145 */ 146 public boolean compareAndSet(V expectedReference, 147 V newReference, 148 boolean expectedMark, 149 boolean newMark) { 150 ReferenceBooleanPair<V> current = atomicRef.get(); 151 return expectedReference == current.reference && 152 expectedMark == current.bit && 153 ((newReference == current.reference && newMark == current.bit) || 154 atomicRef.compareAndSet(current, 155 new ReferenceBooleanPair<V>(newReference, 156 newMark))); 157 } 158 159 /** 160 * Unconditionally sets the value of both the reference and mark. 161 * 162 * @param newReference the new value for the reference 163 * @param newMark the new value for the mark 164 */ 165 public void set(V newReference, boolean newMark) { 166 ReferenceBooleanPair<V> current = atomicRef.get(); 167 if (newReference != current.reference || newMark != current.bit) 168 atomicRef.set(new ReferenceBooleanPair<V>(newReference, newMark)); 169 } 170 171 /** 172 * Atomically sets the value of the mark to the given update value 173 * if the current reference is {@code ==} to the expected 174 * reference. Any given invocation of this operation may fail 175 * (return {@code false}) spuriously, but repeated invocation 176 * when the current value holds the expected value and no other 177 * thread is also attempting to set the value will eventually 178 * succeed. 179 * 180 * @param expectedReference the expected value of the reference 181 * @param newMark the new value for the mark 182 * @return true if successful 183 */ 184 public boolean attemptMark(V expectedReference, boolean newMark) { 185 ReferenceBooleanPair<V> current = atomicRef.get(); 186 return expectedReference == current.reference && 187 (newMark == current.bit || 188 atomicRef.compareAndSet 189 (current, new ReferenceBooleanPair<V>(expectedReference, 190 newMark))); 191 } 192 } | 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 AtomicMarkableReference} maintains an object reference 40 * along with a mark bit, that can be updated atomically. 41 * 42 * <p>Implementation note: This implementation maintains markable 43 * references by creating internal objects representing "boxed" 44 * [reference, boolean] 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 AtomicMarkableReference<V> { 51 52 private static class Pair<T> { 53 final T reference; 54 final boolean mark; 55 private Pair(T reference, boolean mark) { 56 this.reference = reference; 57 this.mark = mark; 58 } 59 static <T> Pair<T> of(T reference, boolean mark) { 60 return new Pair<T>(reference, mark); 61 } 62 } 63 64 private volatile Pair<V> pair; 65 66 /** 67 * Creates a new {@code AtomicMarkableReference} with the given 68 * initial values. 69 * 70 * @param initialRef the initial reference 71 * @param initialMark the initial mark 72 */ 73 public AtomicMarkableReference(V initialRef, boolean initialMark) { 74 pair = Pair.of(initialRef, initialMark); 75 } 76 77 /** 78 * Returns the current value of the reference. 79 * 80 * @return the current value of the reference 81 */ 82 public V getReference() { 83 return pair.reference; 84 } 85 86 /** 87 * Returns the current value of the mark. 88 * 89 * @return the current value of the mark 90 */ 91 public boolean isMarked() { 92 return pair.mark; 93 } 94 95 /** 96 * Returns the current values of both the reference and the mark. 97 * Typical usage is {@code boolean[1] holder; ref = v.get(holder); }. 98 * 99 * @param markHolder an array of size of at least one. On return, 100 * {@code markholder[0]} will hold the value of the mark. 101 * @return the current value of the reference 102 */ 103 public V get(boolean[] markHolder) { 104 Pair<V> pair = this.pair; 105 markHolder[0] = pair.mark; 106 return pair.reference; 107 } 108 109 /** 110 * Atomically sets the value of both the reference and mark 111 * to the given update values if the 112 * current reference is {@code ==} to the expected reference 113 * and the current mark is equal to the expected mark. 114 * 115 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 116 * and does not provide ordering guarantees, so is only rarely an 117 * appropriate alternative to {@code compareAndSet}. 118 * 119 * @param expectedReference the expected value of the reference 120 * @param newReference the new value for the reference 121 * @param expectedMark the expected value of the mark 122 * @param newMark the new value for the mark 123 * @return true if successful 124 */ 125 public boolean weakCompareAndSet(V expectedReference, 126 V newReference, 127 boolean expectedMark, 128 boolean newMark) { 129 return compareAndSet(expectedReference, newReference, 130 expectedMark, newMark); 131 } 132 133 /** 134 * Atomically sets the value of both the reference and mark 135 * to the given update values if the 136 * current reference is {@code ==} to the expected reference 137 * and the current mark is equal to the expected mark. 138 * 139 * @param expectedReference the expected value of the reference 140 * @param newReference the new value for the reference 141 * @param expectedMark the expected value of the mark 142 * @param newMark the new value for the mark 143 * @return true if successful 144 */ 145 public boolean compareAndSet(V expectedReference, 146 V newReference, 147 boolean expectedMark, 148 boolean newMark) { 149 Pair<V> current = pair; 150 return 151 expectedReference == current.reference && 152 expectedMark == current.mark && 153 ((newReference == current.reference && 154 newMark == current.mark) || 155 casPair(current, Pair.of(newReference, newMark))); 156 } 157 158 /** 159 * Unconditionally sets the value of both the reference and mark. 160 * 161 * @param newReference the new value for the reference 162 * @param newMark the new value for the mark 163 */ 164 public void set(V newReference, boolean newMark) { 165 Pair<V> current = pair; 166 if (newReference != current.reference || newMark != current.mark) 167 this.pair = Pair.of(newReference, newMark); 168 } 169 170 /** 171 * Atomically sets the value of the mark to the given update value 172 * if the current reference is {@code ==} to the expected 173 * reference. Any given invocation of this operation may fail 174 * (return {@code false}) spuriously, but repeated invocation 175 * when the current value holds the expected value and no other 176 * thread is also attempting to set the value will eventually 177 * succeed. 178 * 179 * @param expectedReference the expected value of the reference 180 * @param newMark the new value for the mark 181 * @return true if successful 182 */ 183 public boolean attemptMark(V expectedReference, boolean newMark) { 184 Pair<V> current = pair; 185 return 186 expectedReference == current.reference && 187 (newMark == current.mark || 188 casPair(current, Pair.of(expectedReference, newMark))); 189 } 190 191 // Unsafe mechanics 192 193 private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe(); 194 private static final long pairOffset = 195 objectFieldOffset(UNSAFE, "pair", AtomicMarkableReference.class); 196 197 private boolean casPair(Pair<V> cmp, Pair<V> val) { 198 return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val); 199 } 200 201 static long objectFieldOffset(sun.misc.Unsafe UNSAFE, 202 String field, Class<?> klazz) { 203 try { 204 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); 205 } catch (NoSuchFieldException e) { 206 // Convert Exception to corresponding Error 207 NoSuchFieldError error = new NoSuchFieldError(field); 208 error.initCause(e); 209 throw error; 210 } 211 } 212 } |