ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/StampedLock.java
(Generate patch)

Comparing jsr166/src/jsr166e/StampedLock.java (file contents):
Revision 1.31 by dl, Thu Jan 24 21:35:03 2013 UTC vs.
Revision 1.38 by dl, Mon Aug 19 14:30:37 2013 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8
9 import java.util.concurrent.ThreadLocalRandom;
8   import java.util.concurrent.TimeUnit;
9   import java.util.concurrent.locks.Lock;
10   import java.util.concurrent.locks.Condition;
# Line 40 | Line 38 | import java.util.concurrent.locks.LockSu
38   *  <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead}
39   *   returns a non-zero stamp only if the lock is not currently held
40   *   in write mode. Method {@link #validate} returns true if the lock
41 < *   has not since been acquired in write mode. This mode can be
42 < *   thought of as an extremely weak version of a read-lock, that can
43 < *   be broken by a writer at any time.  The use of optimistic mode
44 < *   for short read-only code segments often reduces contention and
45 < *   improves throughput.  However, its use is inherently fragile.
46 < *   Optimistic read sections should only read fields and hold them in
47 < *   local variables for later use after validation. Fields read while
48 < *   in optimistic mode may be wildly inconsistent, so usage applies
49 < *   only when you are familiar enough with data representations to
50 < *   check consistency and/or repeatedly invoke method {@code
51 < *   validate()}.  For example, such steps are typically required when
52 < *   first reading an object or array reference, and then accessing
53 < *   one of its fields, elements or methods. </li>
41 > *   has not been acquired in write mode since obtaining a given
42 > *   stamp.  This mode can be thought of as an extremely weak version
43 > *   of a read-lock, that can be broken by a writer at any time.  The
44 > *   use of optimistic mode for short read-only code segments often
45 > *   reduces contention and improves throughput.  However, its use is
46 > *   inherently fragile.  Optimistic read sections should only read
47 > *   fields and hold them in local variables for later use after
48 > *   validation. Fields read while in optimistic mode may be wildly
49 > *   inconsistent, so usage applies only when you are familiar enough
50 > *   with data representations to check consistency and/or repeatedly
51 > *   invoke method {@code validate()}.  For example, such steps are
52 > *   typically required when first reading an object or array
53 > *   reference, and then accessing one of its fields, elements or
54 > *   methods. </li>
55   *
56   * </ul>
57   *
# Line 118 | Line 117 | import java.util.concurrent.locks.LockSu
117   *     }
118   *   }
119   *
120 < *   double distanceFromOriginV1() { // A read-only method
121 < *     long stamp;
122 < *     if ((stamp = sl.tryOptimisticRead()) != 0L) { // optimistic
123 < *       double currentX = x;
124 < *       double currentY = y;
125 < *       if (sl.validate(stamp))
126 < *         return Math.sqrt(currentX * currentX + currentY * currentY);
127 < *     }
128 < *     stamp = sl.readLock(); // fall back to read lock
129 < *     try {
130 < *       double currentX = x;
132 < *       double currentY = y;
133 < *         return Math.sqrt(currentX * currentX + currentY * currentY);
134 < *     } finally {
135 < *       sl.unlockRead(stamp);
136 < *     }
137 < *   }
138 < *
139 < *   double distanceFromOriginV2() { // combines code paths
140 < *     double currentX = 0.0, currentY = 0.0;
141 < *     for (long stamp = sl.tryOptimisticRead(); ; stamp = sl.readLock()) {
142 < *       try {
143 < *         currentX = x;
144 < *         currentY = y;
145 < *       } finally {
146 < *         if (sl.tryConvertToOptimisticRead(stamp) != 0L) // unlock or validate
147 < *           break;
148 < *       }
120 > *   double distanceFromOrigin() { // A read-only method
121 > *     long stamp = sl.tryOptimisticRead();
122 > *     double currentX = x, currentY = y;
123 > *     if (!sl.validate(stamp)) {
124 > *        stamp = sl.readLock();
125 > *        try {
126 > *          currentX = x;
127 > *          currentY = y;
128 > *        } finally {
129 > *           sl.unlockRead(stamp);
130 > *        }
131   *     }
132   *     return Math.sqrt(currentX * currentX + currentY * currentY);
133   *   }
# Line 214 | Line 196 | public class StampedLock implements java
196       * incoming reader arrives while read lock is held but there is a
197       * queued writer, this incoming reader is queued.  (This rule is
198       * responsible for some of the complexity of method acquireRead,
199 <     * but without it, the lock becomes highly unfair.)
199 >     * but without it, the lock becomes highly unfair.) Method release
200 >     * does not (and sometimes cannot) itself wake up cowaiters. This
201 >     * is done by the primary thread, but helped by any other threads
202 >     * with nothing better to do in methods acquireRead and
203 >     * acquireWrite.
204       *
205       * These rules apply to threads actually queued. All tryLock forms
206       * opportunistically try to acquire locks regardless of preference
# Line 252 | Line 238 | public class StampedLock implements java
238       * motivation to further spread out contended locations, but might
239       * be subject to future improvements.
240       */
241 +
242      private static final long serialVersionUID = -6001602636862214147L;
243  
244      /** Number of processors, for spin control */
245      private static final int NCPU = Runtime.getRuntime().availableProcessors();
246  
247 <    /** Maximum number of retries before blocking on acquisition */
247 >    /** Maximum number of retries before enqueuing on acquisition */
248      private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0;
249  
250 +    /** Maximum number of retries before blocking at head on acquisition */
251 +    private static final int HEAD_SPINS = (NCPU > 1) ? 1 << 10 : 0;
252 +
253      /** Maximum number of retries before re-blocking */
254 <    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 12 : 0;
254 >    private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 16 : 0;
255  
256      /** The period for yielding when waiting for overflow spinlock */
257      private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1
# Line 356 | Line 346 | public class StampedLock implements java
346       * Behavior under timeout and interruption matches that specified
347       * for method {@link Lock#tryLock(long,TimeUnit)}.
348       *
349 +     * @param time the maximum time to wait for the lock
350 +     * @param unit the time unit of the {@code time} argument
351       * @return a stamp that can be used to unlock or convert mode,
352       * or zero if the lock is not available
353       * @throws InterruptedException if the current thread is interrupted
# Line 403 | Line 395 | public class StampedLock implements java
395       * @return a stamp that can be used to unlock or convert mode
396       */
397      public long readLock() {
398 <        long s, next;  // bypass acquireRead on fully unlocked case only
399 <        return ((((s = state) & ABITS) == 0L &&
398 >        long s = state, next;  // bypass acquireRead on common uncontended case
399 >        return ((whead == wtail && (s & ABITS) < RFULL &&
400                   U.compareAndSwapLong(this, STATE, s, next = s + RUNIT)) ?
401                  next : acquireRead(false, 0L));
402      }
# Line 435 | Line 427 | public class StampedLock implements java
427       * Behavior under timeout and interruption matches that specified
428       * for method {@link Lock#tryLock(long,TimeUnit)}.
429       *
430 +     * @param time the maximum time to wait for the lock
431 +     * @param unit the time unit of the {@code time} argument
432       * @return a stamp that can be used to unlock or convert mode,
433       * or zero if the lock is not available
434       * @throws InterruptedException if the current thread is interrupted
# Line 442 | Line 436 | public class StampedLock implements java
436       */
437      public long tryReadLock(long time, TimeUnit unit)
438          throws InterruptedException {
439 <        long next, deadline;
439 >        long s, m, next, deadline;
440          long nanos = unit.toNanos(time);
441          if (!Thread.interrupted()) {
442 <            if ((next = tryReadLock()) != 0L)
443 <                return next;
442 >            if ((m = (s = state) & ABITS) != WBIT) {
443 >                if (m < RFULL) {
444 >                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
445 >                        return next;
446 >                }
447 >                else if ((next = tryIncReaderOverflow(s)) != 0L)
448 >                    return next;
449 >            }
450              if (nanos <= 0L)
451                  return 0L;
452              if ((deadline = System.nanoTime() + nanos) == 0L)
# Line 490 | Line 490 | public class StampedLock implements java
490       * Returns true if the lock has not been exclusively acquired
491       * since issuance of the given stamp. Always returns false if the
492       * stamp is zero. Always returns true if the stamp represents a
493 <     * currently held lock.
493 >     * currently held lock. Invoking this method with a value not
494 >     * obtained from {@link #tryOptimisticRead} or a locking method
495 >     * for this lock has no defined effect or result.
496       *
497 <     * @return true if the lock has not been exclusively acquired
497 >     * @param stamp a stamp
498 >     * @return {@code true} if the lock has not been exclusively acquired
499       * since issuance of the given stamp; else false
500       */
501      public boolean validate(long stamp) {
# Line 670 | Line 673 | public class StampedLock implements java
673          long a = stamp & ABITS, m, s, next; WNode h;
674          for (;;) {
675              s = U.getLongVolatile(this, STATE); // see above
676 <            if ((s & SBITS) != (stamp & SBITS))
676 >            if (((s = state) & SBITS) != (stamp & SBITS))
677                  break;
678              if ((m = s & ABITS) == 0L) {
679                  if (a != 0L)
# Line 705 | Line 708 | public class StampedLock implements java
708       * stamp value. This method may be useful for recovery after
709       * errors.
710       *
711 <     * @return true if the lock was held, else false
711 >     * @return {@code true} if the lock was held, else false
712       */
713      public boolean tryUnlockWrite() {
714          long s; WNode h;
# Line 723 | Line 726 | public class StampedLock implements java
726       * requiring a stamp value. This method may be useful for recovery
727       * after errors.
728       *
729 <     * @return true if the read lock was held, else false
729 >     * @return {@code true} if the read lock was held, else false
730       */
731      public boolean tryUnlockRead() {
732          long s, m; WNode h;
# Line 741 | Line 744 | public class StampedLock implements java
744          return false;
745      }
746  
747 +    // status monitoring methods
748 +
749      /**
750 <     * Returns true if the lock is currently held exclusively.
750 >     * Returns combined state-held and overflow read count for given
751 >     * state s.
752 >     */
753 >    private int getReadLockCount(long s) {
754 >        long readers;
755 >        if ((readers = s & RBITS) >= RFULL)
756 >            readers = RFULL + readerOverflow;
757 >        return (int) readers;
758 >    }
759 >
760 >    /**
761 >     * Returns {@code true} if the lock is currently held exclusively.
762       *
763 <     * @return true if the lock is currently held exclusively
763 >     * @return {@code true} if the lock is currently held exclusively
764       */
765      public boolean isWriteLocked() {
766          return (state & WBIT) != 0L;
767      }
768  
769      /**
770 <     * Returns true if the lock is currently held non-exclusively.
770 >     * Returns {@code true} if the lock is currently held non-exclusively.
771       *
772 <     * @return true if the lock is currently held non-exclusively
772 >     * @return {@code true} if the lock is currently held non-exclusively
773       */
774      public boolean isReadLocked() {
775          return (state & RBITS) != 0L;
776      }
777  
778 <    private void readObject(java.io.ObjectInputStream s)
779 <        throws java.io.IOException, ClassNotFoundException {
780 <        s.defaultReadObject();
781 <        state = ORIGIN; // reset to unlocked state
778 >    /**
779 >     * Queries the number of read locks held for this lock. This
780 >     * method is designed for use in monitoring system state, not for
781 >     * synchronization control.
782 >     * @return the number of read locks held
783 >     */
784 >    public int getReadLockCount() {
785 >        return getReadLockCount(state);
786      }
787  
788      /**
789 +     * Returns a string identifying this lock, as well as its lock
790 +     * state.  The state, in brackets, includes the String {@code
791 +     * "Unlocked"} or the String {@code "Write-locked"} or the String
792 +     * {@code "Read-locks:"} followed by the current number of
793 +     * read-locks held.
794 +     *
795 +     * @return a string identifying this lock, as well as its lock state
796 +     */
797 +    public String toString() {
798 +        long s = state;
799 +        return super.toString() +
800 +            ((s & ABITS) == 0L ? "[Unlocked]" :
801 +             (s & WBIT) != 0L ? "[Write-locked]" :
802 +             "[Read-locks:" + getReadLockCount(s) + "]");
803 +    }
804 +
805 +    // views
806 +
807 +    /**
808       * Returns a plain {@link Lock} view of this StampedLock in which
809       * the {@link Lock#lock} method is mapped to {@link #readLock},
810       * and similarly for other methods. The returned Lock does not
# Line 879 | Line 918 | public class StampedLock implements java
918          }
919      }
920  
921 +    private void readObject(java.io.ObjectInputStream s)
922 +        throws java.io.IOException, ClassNotFoundException {
923 +        s.defaultReadObject();
924 +        state = ORIGIN; // reset to unlocked state
925 +    }
926 +
927      // internals
928  
929      /**
# Line 886 | Line 931 | public class StampedLock implements java
931       * access bits value to RBITS, indicating hold of spinlock,
932       * then updating, then releasing.
933       *
934 <     * @param s, assumed that (s & ABITS) >= RFULL
934 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
935       * @return new stamp on success, else zero
936       */
937      private long tryIncReaderOverflow(long s) {
938 +        // assert (s & ABITS) >= RFULL;
939          if ((s & ABITS) == RFULL) {
940              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
941                  ++readerOverflow;
# Line 906 | Line 952 | public class StampedLock implements java
952      /**
953       * Tries to decrement readerOverflow.
954       *
955 <     * @param s, assumed that (s & ABITS) >= RFULL
955 >     * @param s a reader overflow stamp: (s & ABITS) >= RFULL
956       * @return new stamp on success, else zero
957       */
958      private long tryDecReaderOverflow(long s) {
959 +        // assert (s & ABITS) >= RFULL;
960          if ((s & ABITS) == RFULL) {
961              if (U.compareAndSwapLong(this, STATE, s, s | RBITS)) {
962                  int r; long next;
# Line 929 | Line 976 | public class StampedLock implements java
976          return 0L;
977      }
978  
979 <    /*
979 >    /**
980       * Wakes up the successor of h (normally whead). This is normally
981       * just h.next, but may require traversal from wtail if next
982       * pointers are lagging. This may fail to wake up an acquiring
# Line 945 | Line 992 | public class StampedLock implements java
992                      if (t.status <= 0)
993                          q = t;
994              }
995 <            if (q != null) {
996 <                for (WNode r = q;;) {  // release co-waiters too
950 <                    if ((w = r.thread) != null) {
951 <                        r.thread = null;
952 <                        U.unpark(w);
953 <                    }
954 <                    if ((r = q.cowait) == null)
955 <                        break;
956 <                    U.compareAndSwapObject(q, WCOWAIT, r, r.cowait);
957 <                }
958 <            }
995 >            if (q != null && (w = q.thread) != null)
996 >                U.unpark(w);
997          }
998      }
999  
# Line 971 | Line 1009 | public class StampedLock implements java
1009      private long acquireWrite(boolean interruptible, long deadline) {
1010          WNode node = null, p;
1011          for (int spins = -1;;) { // spin while enqueuing
1012 <            long s, ns;
1013 <            if (((s = state) & ABITS) == 0L) {
1012 >            long m, s, ns;
1013 >            if ((m = (s = state) & ABITS) == 0L) {
1014                  if (U.compareAndSwapLong(this, STATE, s, ns = s + WBIT))
1015                      return ns;
1016              }
1017 +            else if (spins < 0)
1018 +                spins = (m == WBIT && wtail == whead) ? SPINS : 0;
1019              else if (spins > 0) {
1020                  if (ThreadLocalRandom.current().nextInt() >= 0)
1021                      --spins;
1022              }
1023              else if ((p = wtail) == null) { // initialize queue
1024 <                WNode h = new WNode(WMODE, null);
1025 <                if (U.compareAndSwapObject(this, WHEAD, null, h))
1026 <                    wtail = h;
1024 >                WNode hd = new WNode(WMODE, null);
1025 >                if (U.compareAndSwapObject(this, WHEAD, null, hd))
1026 >                    wtail = hd;
1027              }
988            else if (spins < 0)
989                spins = (p == whead) ? SPINS : 0;
1028              else if (node == null)
1029                  node = new WNode(WMODE, p);
1030              else if (node.prev != p)
# Line 997 | Line 1035 | public class StampedLock implements java
1035              }
1036          }
1037  
1038 <        for (int spins = SPINS;;) {
1039 <            WNode np, pp; int ps; long s, ns; Thread w;
1040 <            while ((np = node.prev) != p && np != null)
1041 <                (p = np).next = node;   // stale
1042 <            if (whead == p) {
1038 >        for (int spins = -1;;) {
1039 >            WNode h, np, pp; int ps;
1040 >            if ((h = whead) == p) {
1041 >                if (spins < 0)
1042 >                    spins = HEAD_SPINS;
1043 >                else if (spins < MAX_HEAD_SPINS)
1044 >                    spins <<= 1;
1045                  for (int k = spins;;) { // spin at head
1046 +                    long s, ns;
1047                      if (((s = state) & ABITS) == 0L) {
1048 <                        if (U.compareAndSwapLong(this, STATE, s, ns = s+WBIT)) {
1048 >                        if (U.compareAndSwapLong(this, STATE, s,
1049 >                                                 ns = s + WBIT)) {
1050                              whead = node;
1051                              node.prev = null;
1052                              return ns;
# Line 1014 | Line 1056 | public class StampedLock implements java
1056                               --k <= 0)
1057                          break;
1058                  }
1017                if (spins < MAX_HEAD_SPINS)
1018                    spins <<= 1;
1059              }
1060 <            if ((ps = p.status) == 0)
1061 <                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1062 <            else if (ps == CANCELLED) {
1063 <                if ((pp = p.prev) != null) {
1064 <                    node.prev = pp;
1065 <                    pp.next = node;
1060 >            else if (h != null) { // help release stale waiters
1061 >                WNode c; Thread w;
1062 >                while ((c = h.cowait) != null) {
1063 >                    if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1064 >                        (w = c.thread) != null)
1065 >                        U.unpark(w);
1066                  }
1067              }
1068 <            else {
1069 <                long time; // 0 argument to park means no timeout
1070 <                if (deadline == 0L)
1071 <                    time = 0L;
1072 <                else if ((time = deadline - System.nanoTime()) <= 0L)
1073 <                    return cancelWaiter(node, null, false);
1074 <                node.thread = Thread.currentThread();
1075 <                if (node.prev == p && p.status == WAITING && // recheck
1076 <                    (p != whead || (state & ABITS) != 0L))
1077 <                    U.park(false, time);
1078 <                node.thread = null;
1079 <                if (interruptible && Thread.interrupted())
1080 <                    return cancelWaiter(node, null, true);
1068 >            if (whead == h) {
1069 >                if ((np = node.prev) != p) {
1070 >                    if (np != null)
1071 >                        (p = np).next = node;   // stale
1072 >                }
1073 >                else if ((ps = p.status) == 0)
1074 >                    U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1075 >                else if (ps == CANCELLED) {
1076 >                    if ((pp = p.prev) != null) {
1077 >                        node.prev = pp;
1078 >                        pp.next = node;
1079 >                    }
1080 >                }
1081 >                else {
1082 >                    long time; // 0 argument to park means no timeout
1083 >                    if (deadline == 0L)
1084 >                        time = 0L;
1085 >                    else if ((time = deadline - System.nanoTime()) <= 0L)
1086 >                        return cancelWaiter(node, node, false);
1087 >                    Thread wt = Thread.currentThread();
1088 >                    U.putObject(wt, PARKBLOCKER, this);
1089 >                    node.thread = wt;
1090 >                    if (p.status < 0 && (p != h || (state & ABITS) != 0L) &&
1091 >                        whead == h && node.prev == p)
1092 >                        U.park(false, time);  // emulate LockSupport.park
1093 >                    node.thread = null;
1094 >                    U.putObject(wt, PARKBLOCKER, null);
1095 >                    if (interruptible && Thread.interrupted())
1096 >                        return cancelWaiter(node, node, true);
1097 >                }
1098              }
1099          }
1100      }
# Line 1052 | Line 1109 | public class StampedLock implements java
1109       * @return next state, or INTERRUPTED
1110       */
1111      private long acquireRead(boolean interruptible, long deadline) {
1112 <        WNode node = null, group = null, p;
1112 >        WNode node = null, p;
1113          for (int spins = -1;;) {
1114 <            for (;;) {
1115 <                long s, m, ns; WNode h, q; Thread w; // anti-barging guard
1116 <                if (group == null && (h = whead) != null &&
1117 <                    (q = h.next) != null && q.mode != RMODE)
1118 <                    break;
1119 <                if ((m = (s = state) & ABITS) < RFULL ?
1120 <                    U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1121 <                    (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) {
1122 <                    if (group != null) {  // help release others
1123 <                        for (WNode r = group;;) {
1124 <                            if ((w = r.thread) != null) {
1125 <                                r.thread = null;
1126 <                                U.unpark(w);
1114 >            WNode h;
1115 >            if ((h = whead) == (p = wtail)) {
1116 >                for (long m, s, ns;;) {
1117 >                    if ((m = (s = state) & ABITS) < RFULL ?
1118 >                        U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1119 >                        (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L))
1120 >                        return ns;
1121 >                    else if (m >= WBIT) {
1122 >                        if (spins > 0) {
1123 >                            if (ThreadLocalRandom.current().nextInt() >= 0)
1124 >                                --spins;
1125 >                        }
1126 >                        else {
1127 >                            if (spins == 0) {
1128 >                                WNode nh = whead, np = wtail;
1129 >                                if ((nh == h && np == p) || (h = nh) != (p = np))
1130 >                                    break;
1131                              }
1132 <                            if ((r = group.cowait) == null)
1072 <                                break;
1073 <                            U.compareAndSwapObject(group, WCOWAIT, r, r.cowait);
1132 >                            spins = SPINS;
1133                          }
1134                      }
1076                    return ns;
1135                  }
1078                if (m >= WBIT)
1079                    break;
1080            }
1081            if (spins > 0) {
1082                if (ThreadLocalRandom.current().nextInt() >= 0)
1083                    --spins;
1136              }
1137 <            else if ((p = wtail) == null) {
1138 <                WNode h = new WNode(WMODE, null);
1139 <                if (U.compareAndSwapObject(this, WHEAD, null, h))
1140 <                    wtail = h;
1137 >            if (p == null) { // initialize queue
1138 >                WNode hd = new WNode(WMODE, null);
1139 >                if (U.compareAndSwapObject(this, WHEAD, null, hd))
1140 >                    wtail = hd;
1141              }
1090            else if (spins < 0)
1091                spins = (p == whead) ? SPINS : 0;
1142              else if (node == null)
1143 <                node = new WNode(WMODE, p);
1144 <            else if (node.prev != p)
1145 <                node.prev = p;
1146 <            else if (p.mode == RMODE && p != whead) {
1147 <                WNode pp = p.prev;  // become co-waiter with group p
1148 <                if (pp != null && p == wtail &&
1149 <                    U.compareAndSwapObject(p, WCOWAIT,
1150 <                                           node.cowait = p.cowait, node)) {
1151 <                    node.thread = Thread.currentThread();
1152 <                    for (long time;;) {
1153 <                        if (interruptible && Thread.interrupted())
1154 <                            return cancelWaiter(node, p, true);
1143 >                node = new WNode(RMODE, p);
1144 >            else if (h == p || p.mode != RMODE) {
1145 >                if (node.prev != p)
1146 >                    node.prev = p;
1147 >                else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1148 >                    p.next = node;
1149 >                    break;
1150 >                }
1151 >            }
1152 >            else if (!U.compareAndSwapObject(p, WCOWAIT,
1153 >                                             node.cowait = p.cowait, node))
1154 >                node.cowait = null;
1155 >            else {
1156 >                for (;;) {
1157 >                    WNode pp, c; Thread w;
1158 >                    if ((h = whead) != null && (c = h.cowait) != null &&
1159 >                        U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1160 >                        (w = c.thread) != null) // help release
1161 >                        U.unpark(w);
1162 >                    if (h == (pp = p.prev) || h == p || pp == null) {
1163 >                        long m, s, ns;
1164 >                        do {
1165 >                            if ((m = (s = state) & ABITS) < RFULL ?
1166 >                                U.compareAndSwapLong(this, STATE, s,
1167 >                                                     ns = s + RUNIT) :
1168 >                                (m < WBIT &&
1169 >                                 (ns = tryIncReaderOverflow(s)) != 0L))
1170 >                                return ns;
1171 >                        } while (m < WBIT);
1172 >                    }
1173 >                    if (whead == h && p.prev == pp) {
1174 >                        long time;
1175 >                        if (pp == null || h == p || p.status > 0) {
1176 >                            node = null; // throw away
1177 >                            break;
1178 >                        }
1179                          if (deadline == 0L)
1180                              time = 0L;
1181                          else if ((time = deadline - System.nanoTime()) <= 0L)
1182                              return cancelWaiter(node, p, false);
1183 <                        if (node.thread == null)
1184 <                            break;
1185 <                        if (p.prev != pp || p.status == CANCELLED ||
1186 <                            p == whead || p.prev != pp) {
1187 <                            node.thread = null;
1188 <                            break;
1183 >                        Thread wt = Thread.currentThread();
1184 >                        U.putObject(wt, PARKBLOCKER, this);
1185 >                        node.thread = wt;
1186 >                        if ((h != pp || (state & ABITS) == WBIT) &&
1187 >                            whead == h && p.prev == pp)
1188 >                            U.park(false, time);
1189 >                        node.thread = null;
1190 >                        U.putObject(wt, PARKBLOCKER, null);
1191 >                        if (interruptible && Thread.interrupted())
1192 >                            return cancelWaiter(node, p, true);
1193 >                    }
1194 >                }
1195 >            }
1196 >        }
1197 >
1198 >        for (int spins = -1;;) {
1199 >            WNode h, np, pp; int ps;
1200 >            if ((h = whead) == p) {
1201 >                if (spins < 0)
1202 >                    spins = HEAD_SPINS;
1203 >                else if (spins < MAX_HEAD_SPINS)
1204 >                    spins <<= 1;
1205 >                for (int k = spins;;) { // spin at head
1206 >                    long m, s, ns;
1207 >                    if ((m = (s = state) & ABITS) < RFULL ?
1208 >                        U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT) :
1209 >                        (m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) {
1210 >                        WNode c; Thread w;
1211 >                        whead = node;
1212 >                        node.prev = null;
1213 >                        while ((c = node.cowait) != null) {
1214 >                            if (U.compareAndSwapObject(node, WCOWAIT,
1215 >                                                       c, c.cowait) &&
1216 >                                (w = c.thread) != null)
1217 >                                U.unpark(w);
1218                          }
1219 <                        if (node.thread == null) // must recheck
1117 <                            break;
1118 <                        U.park(false, time);
1219 >                        return ns;
1220                      }
1221 <                    group = p;
1221 >                    else if (m >= WBIT &&
1222 >                             ThreadLocalRandom.current().nextInt() >= 0 && --k <= 0)
1223 >                        break;
1224                  }
1122                node = null; // throw away
1225              }
1226 <            else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1227 <                p.next = node;
1228 <                break;
1226 >            else if (h != null) {
1227 >                WNode c; Thread w;
1228 >                while ((c = h.cowait) != null) {
1229 >                    if (U.compareAndSwapObject(h, WCOWAIT, c, c.cowait) &&
1230 >                        (w = c.thread) != null)
1231 >                        U.unpark(w);
1232 >                }
1233 >            }
1234 >            if (whead == h) {
1235 >                if ((np = node.prev) != p) {
1236 >                    if (np != null)
1237 >                        (p = np).next = node;   // stale
1238 >                }
1239 >                else if ((ps = p.status) == 0)
1240 >                    U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1241 >                else if (ps == CANCELLED) {
1242 >                    if ((pp = p.prev) != null) {
1243 >                        node.prev = pp;
1244 >                        pp.next = node;
1245 >                    }
1246 >                }
1247 >                else {
1248 >                    long time;
1249 >                    if (deadline == 0L)
1250 >                        time = 0L;
1251 >                    else if ((time = deadline - System.nanoTime()) <= 0L)
1252 >                        return cancelWaiter(node, node, false);
1253 >                    Thread wt = Thread.currentThread();
1254 >                    U.putObject(wt, PARKBLOCKER, this);
1255 >                    node.thread = wt;
1256 >                    if (p.status < 0 &&
1257 >                        (p != h || (state & ABITS) == WBIT) &&
1258 >                        whead == h && node.prev == p)
1259 >                        U.park(false, time);
1260 >                    node.thread = null;
1261 >                    U.putObject(wt, PARKBLOCKER, null);
1262 >                    if (interruptible && Thread.interrupted())
1263 >                        return cancelWaiter(node, node, true);
1264 >                }
1265              }
1266          }
1267 +    }
1268  
1269 <        for (int spins = SPINS;;) {
1270 <            WNode np, pp, r; int ps; long m, s, ns; Thread w;
1271 <            while ((np = node.prev) != p && np != null)
1272 <                (p = np).next = node;
1273 <            if (whead == p) {
1274 <                for (int k = spins;;) {
1275 <                    if ((m = (s = state) & ABITS) != WBIT) {
1276 <                        if (m < RFULL ?
1277 <                            U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT):
1278 <                            (ns = tryIncReaderOverflow(s)) != 0L) {
1279 <                            whead = node;
1280 <                            node.prev = null;
1281 <                            while ((r = node.cowait) != null) {
1282 <                                if (U.compareAndSwapObject(node, WCOWAIT,
1283 <                                                           r, r.cowait) &&
1284 <                                    (w = r.thread) != null) {
1285 <                                    r.thread = null;
1286 <                                    U.unpark(w); // release co-waiter
1287 <                                }
1288 <                            }
1289 <                            return ns;
1269 >    /**
1270 >     * If node non-null, forces cancel status and unsplices it from
1271 >     * queue if possible and wakes up any cowaiters (of the node, or
1272 >     * group, as applicable), and in any case helps release current
1273 >     * first waiter if lock is free. (Calling with null arguments
1274 >     * serves as a conditional form of release, which is not currently
1275 >     * needed but may be needed under possible future cancellation
1276 >     * policies). This is a variant of cancellation methods in
1277 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1278 >     * internal documentation).
1279 >     *
1280 >     * @param node if nonnull, the waiter
1281 >     * @param group either node or the group node is cowaiting with
1282 >     * @param interrupted if already interrupted
1283 >     * @return INTERRUPTED if interrupted or Thread.interrupted, else zero
1284 >     */
1285 >    private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
1286 >        if (node != null && group != null) {
1287 >            Thread w;
1288 >            node.status = CANCELLED;
1289 >            // unsplice cancelled nodes from group
1290 >            for (WNode p = group, q; (q = p.cowait) != null;) {
1291 >                if (q.status == CANCELLED) {
1292 >                    U.compareAndSwapObject(p, WCOWAIT, q, q.cowait);
1293 >                    p = group; // restart
1294 >                }
1295 >                else
1296 >                    p = q;
1297 >            }
1298 >            if (group == node) {
1299 >                for (WNode r = group.cowait; r != null; r = r.cowait) {
1300 >                    if ((w = r.thread) != null)
1301 >                        U.unpark(w);       // wake up uncancelled co-waiters
1302 >                }
1303 >                for (WNode pred = node.prev; pred != null; ) { // unsplice
1304 >                    WNode succ, pp;        // find valid successor
1305 >                    while ((succ = node.next) == null ||
1306 >                           succ.status == CANCELLED) {
1307 >                        WNode q = null;    // find successor the slow way
1308 >                        for (WNode t = wtail; t != null && t != node; t = t.prev)
1309 >                            if (t.status != CANCELLED)
1310 >                                q = t;     // don't link if succ cancelled
1311 >                        if (succ == q ||   // ensure accurate successor
1312 >                            U.compareAndSwapObject(node, WNEXT,
1313 >                                                   succ, succ = q)) {
1314 >                            if (succ == null && node == wtail)
1315 >                                U.compareAndSwapObject(this, WTAIL, node, pred);
1316 >                            break;
1317                          }
1318                      }
1319 <                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1320 <                             --k <= 0)
1319 >                    if (pred.next == node) // unsplice pred link
1320 >                        U.compareAndSwapObject(pred, WNEXT, node, succ);
1321 >                    if (succ != null && (w = succ.thread) != null) {
1322 >                        succ.thread = null;
1323 >                        U.unpark(w);       // wake up succ to observe new pred
1324 >                    }
1325 >                    if (pred.status != CANCELLED || (pp = pred.prev) == null)
1326                          break;
1327 +                    node.prev = pp;        // repeat if new pred wrong/cancelled
1328 +                    U.compareAndSwapObject(pp, WNEXT, pred, succ);
1329 +                    pred = pp;
1330                  }
1157                if (spins < MAX_HEAD_SPINS)
1158                    spins <<= 1;
1331              }
1332 <            if ((ps = p.status) == 0)
1333 <                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1334 <            else if (ps == CANCELLED) {
1335 <                if ((pp = p.prev) != null) {
1336 <                    node.prev = pp;
1337 <                    pp.next = node;
1338 <                }
1332 >        }
1333 >        WNode h; // Possibly release first waiter
1334 >        while ((h = whead) != null) {
1335 >            long s; WNode q; // similar to release() but check eligibility
1336 >            if ((q = h.next) == null || q.status == CANCELLED) {
1337 >                for (WNode t = wtail; t != null && t != h; t = t.prev)
1338 >                    if (t.status <= 0)
1339 >                        q = t;
1340              }
1341 <            else {
1342 <                long time;
1343 <                if (deadline == 0L)
1344 <                    time = 0L;
1345 <                else if ((time = deadline - System.nanoTime()) <= 0L)
1346 <                    return cancelWaiter(node, null, false);
1174 <                node.thread = Thread.currentThread();
1175 <                if (node.prev == p && p.status == WAITING &&
1176 <                    (p != whead || (state & ABITS) != WBIT))
1177 <                    U.park(false, time);
1178 <                node.thread = null;
1179 <                if (interruptible && Thread.interrupted())
1180 <                    return cancelWaiter(node, null, true);
1341 >            if (h == whead) {
1342 >                if (q != null && h.status == 0 &&
1343 >                    ((s = state) & ABITS) != WBIT && // waiter is eligible
1344 >                    (s == 0L || q.mode == RMODE))
1345 >                    release(h);
1346 >                break;
1347              }
1348          }
1349 +        return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1350 +    }
1351 +
1352 +    // Unsafe mechanics
1353 +    private static final sun.misc.Unsafe U;
1354 +    private static final long STATE;
1355 +    private static final long WHEAD;
1356 +    private static final long WTAIL;
1357 +    private static final long WNEXT;
1358 +    private static final long WSTATUS;
1359 +    private static final long WCOWAIT;
1360 +    private static final long PARKBLOCKER;
1361 +
1362 +    static {
1363 +        try {
1364 +            U = getUnsafe();
1365 +            Class<?> k = StampedLock.class;
1366 +            Class<?> wk = WNode.class;
1367 +            STATE = U.objectFieldOffset
1368 +                (k.getDeclaredField("state"));
1369 +            WHEAD = U.objectFieldOffset
1370 +                (k.getDeclaredField("whead"));
1371 +            WTAIL = U.objectFieldOffset
1372 +                (k.getDeclaredField("wtail"));
1373 +            WSTATUS = U.objectFieldOffset
1374 +                (wk.getDeclaredField("status"));
1375 +            WNEXT = U.objectFieldOffset
1376 +                (wk.getDeclaredField("next"));
1377 +            WCOWAIT = U.objectFieldOffset
1378 +                (wk.getDeclaredField("cowait"));
1379 +            Class<?> tk = Thread.class;
1380 +            PARKBLOCKER = U.objectFieldOffset
1381 +                (tk.getDeclaredField("parkBlocker"));
1382 +
1383 +        } catch (Exception e) {
1384 +            throw new Error(e);
1385 +        }
1386      }
1387  
1388      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines