rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
changeset 1894 75ee4eca04e3
parent 1890 212417b74b72
     1.1 --- a/rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedSynchronizer.java	Sat Mar 19 10:46:31 2016 +0100
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/locks/AbstractQueuedSynchronizer.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   * Provides a framework for implementing blocking locks and related
    1.12 @@ -563,7 +561,11 @@
    1.13       */
    1.14      protected final boolean compareAndSetState(int expect, int update) {
    1.15          // See below for intrinsics setup to support this
    1.16 -        return unsafe.compareAndSwapInt(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 @@ -2272,41 +2274,28 @@
    1.26       * are at it, we do the same for other CASable fields (which could
    1.27       * otherwise be done with atomic field updaters).
    1.28       */
    1.29 -    private static final Unsafe unsafe = Unsafe.getUnsafe();
    1.30 -    private static final long stateOffset;
    1.31 -    private static final long headOffset;
    1.32 -    private static final long tailOffset;
    1.33 -    private static final long waitStatusOffset;
    1.34 -    private static final long nextOffset;
    1.35  
    1.36 -    static {
    1.37 -        try {
    1.38 -            stateOffset = unsafe.objectFieldOffset
    1.39 -                (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
    1.40 -            headOffset = unsafe.objectFieldOffset
    1.41 -                (AbstractQueuedSynchronizer.class.getDeclaredField("head"));
    1.42 -            tailOffset = unsafe.objectFieldOffset
    1.43 -                (AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
    1.44 -            waitStatusOffset = unsafe.objectFieldOffset
    1.45 -                (Node.class.getDeclaredField("waitStatus"));
    1.46 -            nextOffset = unsafe.objectFieldOffset
    1.47 -                (Node.class.getDeclaredField("next"));
    1.48 -
    1.49 -        } catch (Exception ex) { throw new Error(ex); }
    1.50 -    }
    1.51  
    1.52      /**
    1.53       * CAS head field. Used only by enq.
    1.54       */
    1.55      private final boolean compareAndSetHead(Node update) {
    1.56 -        return unsafe.compareAndSwapObject(this, headOffset, null, update);
    1.57 +        if (head == null) {
    1.58 +            head = update;
    1.59 +            return true;
    1.60 +        }
    1.61 +        return false;
    1.62      }
    1.63  
    1.64      /**
    1.65       * CAS tail field. Used only by enq.
    1.66       */
    1.67      private final boolean compareAndSetTail(Node expect, Node update) {
    1.68 -        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
    1.69 +        if (tail == null) {
    1.70 +            tail = update;
    1.71 +            return true;
    1.72 +        }
    1.73 +        return false;
    1.74      }
    1.75  
    1.76      /**
    1.77 @@ -2315,8 +2304,11 @@
    1.78      private static final boolean compareAndSetWaitStatus(Node node,
    1.79                                                           int expect,
    1.80                                                           int update) {
    1.81 -        return unsafe.compareAndSwapInt(node, waitStatusOffset,
    1.82 -                                        expect, update);
    1.83 +        if (node.waitStatus == expect) {
    1.84 +            node.waitStatus = update;
    1.85 +            return true;
    1.86 +        }
    1.87 +        return false;
    1.88      }
    1.89  
    1.90      /**
    1.91 @@ -2325,6 +2317,10 @@
    1.92      private static final boolean compareAndSetNext(Node node,
    1.93                                                     Node expect,
    1.94                                                     Node update) {
    1.95 -        return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
    1.96 +        if (node.next == expect) {
    1.97 +            node.next = update;
    1.98 +            return true;
    1.99 +        }
   1.100 +        return false;
   1.101      }
   1.102  }