rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java
changeset 1894 75ee4eca04e3
parent 1890 212417b74b72
     1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java	Sat Mar 19 10:46:31 2016 +0100
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java	Sat Mar 19 11:01:40 2016 +0100
     1.3 @@ -36,8 +36,6 @@
     1.4  package java.util.concurrent.locks;
     1.5  import java.util.*;
     1.6  import java.util.concurrent.*;
     1.7 -import java.util.concurrent.atomic.*;
     1.8 -import sun.misc.Unsafe;
     1.9  
    1.10  /**
    1.11   * A version of {@link AbstractQueuedSynchronizer} in
    1.12 @@ -340,7 +338,11 @@
    1.13       */
    1.14      protected final boolean compareAndSetState(long expect, long update) {
    1.15          // See below for intrinsics setup to support this
    1.16 -        return unsafe.compareAndSwapLong(this, stateOffset, expect, update);
    1.17 +        if (this.state == expect) {
    1.18 +            this.state = update;
    1.19 +            return true;
    1.20 +        }
    1.21 +        return false;
    1.22      }
    1.23  
    1.24      // Queuing utilities
    1.25 @@ -2043,49 +2045,25 @@
    1.26      }
    1.27  
    1.28      /**
    1.29 -     * Setup to support compareAndSet. We need to natively implement
    1.30 -     * this here: For the sake of permitting future enhancements, we
    1.31 -     * cannot explicitly subclass AtomicLong, which would be
    1.32 -     * efficient and useful otherwise. So, as the lesser of evils, we
    1.33 -     * natively implement using hotspot intrinsics API. And while we
    1.34 -     * are at it, we do the same for other CASable fields (which could
    1.35 -     * otherwise be done with atomic field updaters).
    1.36 -     */
    1.37 -    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.38 -    private static final long stateOffset;
    1.39 -    private static final long headOffset;
    1.40 -    private static final long tailOffset;
    1.41 -    private static final long waitStatusOffset;
    1.42 -    private static final long nextOffset;
    1.43 -
    1.44 -    static {
    1.45 -        try {
    1.46 -            stateOffset = unsafe.objectFieldOffset
    1.47 -                (AbstractQueuedLongSynchronizer.class.getDeclaredField("state"));
    1.48 -            headOffset = unsafe.objectFieldOffset
    1.49 -                (AbstractQueuedLongSynchronizer.class.getDeclaredField("head"));
    1.50 -            tailOffset = unsafe.objectFieldOffset
    1.51 -                (AbstractQueuedLongSynchronizer.class.getDeclaredField("tail"));
    1.52 -            waitStatusOffset = unsafe.objectFieldOffset
    1.53 -                (Node.class.getDeclaredField("waitStatus"));
    1.54 -            nextOffset = unsafe.objectFieldOffset
    1.55 -                (Node.class.getDeclaredField("next"));
    1.56 -
    1.57 -        } catch (Exception ex) { throw new Error(ex); }
    1.58 -    }
    1.59 -
    1.60 -    /**
    1.61       * CAS head field. Used only by enq.
    1.62       */
    1.63      private final boolean compareAndSetHead(Node update) {
    1.64 -        return unsafe.compareAndSwapObject(this, headOffset, null, update);
    1.65 +        if (head == null) {
    1.66 +            head = update;
    1.67 +            return true;
    1.68 +        }
    1.69 +        return false;
    1.70      }
    1.71  
    1.72      /**
    1.73       * CAS tail field. Used only by enq.
    1.74       */
    1.75      private final boolean compareAndSetTail(Node expect, Node update) {
    1.76 -        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
    1.77 +        if (tail == null) {
    1.78 +            tail = update;
    1.79 +            return true;
    1.80 +        }
    1.81 +        return false;
    1.82      }
    1.83  
    1.84      /**
    1.85 @@ -2094,8 +2072,11 @@
    1.86      private static final boolean compareAndSetWaitStatus(Node node,
    1.87                                                           int expect,
    1.88                                                           int update) {
    1.89 -        return unsafe.compareAndSwapInt(node, waitStatusOffset,
    1.90 -                                        expect, update);
    1.91 +        if (node.waitStatus == expect) {
    1.92 +            node.waitStatus = update;
    1.93 +            return true;
    1.94 +        }
    1.95 +        return false;
    1.96      }
    1.97  
    1.98      /**
    1.99 @@ -2104,6 +2085,10 @@
   1.100      private static final boolean compareAndSetNext(Node node,
   1.101                                                     Node expect,
   1.102                                                     Node update) {
   1.103 -        return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
   1.104 +        if (node.next == expect) {
   1.105 +            node.next = update;
   1.106 +            return true;
   1.107 +        }
   1.108 +        return false;
   1.109      }
   1.110  }