src/java.base/share/classes/java/util/concurrent/atomic/AtomicMarkableReference.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


 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 {@code 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 U = sun.misc.Unsafe.getUnsafe();
 194     private static final long PAIR;
 195     static {
 196         try {
 197             PAIR = U.objectFieldOffset
 198                 (AtomicMarkableReference.class.getDeclaredField("pair"));
 199         } catch (ReflectiveOperationException e) {
 200             throw new Error(e);
 201         }
 202     }
 203 
 204     private boolean casPair(Pair<V> cmp, Pair<V> val) {
 205         return U.compareAndSwapObject(this, PAIR, cmp, val);
 206     }
 207 }


 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 {@code 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 jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 194     private static final long PAIR;
 195     static {
 196         try {
 197             PAIR = U.objectFieldOffset
 198                 (AtomicMarkableReference.class.getDeclaredField("pair"));
 199         } catch (ReflectiveOperationException e) {
 200             throw new Error(e);
 201         }
 202     }
 203 
 204     private boolean casPair(Pair<V> cmp, Pair<V> val) {
 205         return U.compareAndSwapObject(this, PAIR, cmp, val);
 206     }
 207 }