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.32 by dl, Sat Jan 26 01:22:37 2013 UTC vs.
Revision 1.33 by dl, Sat Jan 26 15:00:15 2013 UTC

# Line 253 | Line 253 | public class StampedLock implements java
253       * motivation to further spread out contended locations, but might
254       * be subject to future improvements.
255       */
256 +
257      private static final long serialVersionUID = -6001602636862214147L;
258  
259      /** Number of processors, for spin control */
# Line 443 | Line 444 | public class StampedLock implements java
444       */
445      public long tryReadLock(long time, TimeUnit unit)
446          throws InterruptedException {
447 <        long next, deadline;
447 >        long s, m, next, deadline;
448          long nanos = unit.toNanos(time);
449          if (!Thread.interrupted()) {
450 <            if ((next = tryReadLock()) != 0L)
451 <                return next;
450 >            if ((m = (s = state) & ABITS) != WBIT) {
451 >                if (m < RFULL) {
452 >                    if (U.compareAndSwapLong(this, STATE, s, next = s + RUNIT))
453 >                        return next;
454 >                }
455 >                else if ((next = tryIncReaderOverflow(s)) != 0L)
456 >                    return next;
457 >            }
458              if (nanos <= 0L)
459                  return 0L;
460              if ((deadline = System.nanoTime() + nanos) == 0L)
# Line 1033 | Line 1040 | public class StampedLock implements java
1040                  if (deadline == 0L)
1041                      time = 0L;
1042                  else if ((time = deadline - System.nanoTime()) <= 0L)
1043 <                    return cancelWaiter(node, null, false);
1043 >                    return cancelWaiter(node, node, false);
1044                  node.thread = Thread.currentThread();
1045                  if (node.prev == p && p.status == WAITING && // recheck
1046                      (p != whead || (state & ABITS) != 0L))
1047                      U.park(false, time);
1048                  node.thread = null;
1049                  if (interruptible && Thread.interrupted())
1050 <                    return cancelWaiter(node, null, true);
1050 >                    return cancelWaiter(node, node, true);
1051              }
1052          }
1053      }
# Line 1173 | Line 1180 | public class StampedLock implements java
1180                  if (deadline == 0L)
1181                      time = 0L;
1182                  else if ((time = deadline - System.nanoTime()) <= 0L)
1183 <                    return cancelWaiter(node, null, false);
1183 >                    return cancelWaiter(node, node, false);
1184                  node.thread = Thread.currentThread();
1185                  if (node.prev == p && p.status == WAITING &&
1186                      (p != whead || (state & ABITS) != WBIT))
1187                      U.park(false, time);
1188                  node.thread = null;
1189                  if (interruptible && Thread.interrupted())
1190 <                    return cancelWaiter(node, null, true);
1190 >                    return cancelWaiter(node, node, true);
1191              }
1192          }
1193      }
1194  
1195      /**
1196       * If node non-null, forces cancel status and unsplices it from
1197 <     * queue if possible and wakes up any cowaiters. This is a variant
1198 <     * of cancellation methods in AbstractQueuedSynchronizer (see its
1199 <     * detailed explanation in AQS internal documentation) that more
1200 <     * conservatively wakes up other threads that may have had their
1201 <     * links changed, so as to preserve liveness in the main
1202 <     * signalling methods.
1197 >     * queue if possible and wakes up any cowaiters (of the node, or
1198 >     * group, as applicable), and in any case helps release current
1199 >     * first waiter if lock is free. (Calling with null arguments
1200 >     * serves as a conditional form of release, which is not currently
1201 >     * needed but may be needed under possible future cancellation
1202 >     * policies). This is a variant of cancellation methods in
1203 >     * AbstractQueuedSynchronizer (see its detailed explanation in AQS
1204 >     * internal documentation).
1205       *
1206       * @param node if nonnull, the waiter
1207 <     * @param group, if nonnull, the group current thread is cowaiting with
1207 >     * @param group, either node or the group node is cowaiting with
1208       * @param interrupted if already interrupted
1209       * @return INTERRUPTED if interrupted or Thread.interrupted, else zero
1210       */
1211      private long cancelWaiter(WNode node, WNode group, boolean interrupted) {
1212 <        if (node != null) {
1213 <            node.thread = null;
1212 >        if (node != null && group != null) {
1213 >            Thread w;
1214              node.status = CANCELLED;
1215 <            Thread w; // wake up co-waiters; unsplice cancelled ones
1216 <            for (WNode q, p = (group != null) ? group : node; p != null; ) {
1217 <                if ((q = p.cowait) == null)
1209 <                    break;
1210 <                if ((w = q.thread) != null) {
1211 <                    q.thread = null;
1212 <                    U.unpark(w);
1213 <                }
1215 >            node.thread = null;
1216 >            // unsplice cancelled nodes from group
1217 >            for (WNode p = group, q; (q = p.cowait) != null;) {
1218                  if (q.status == CANCELLED)
1219 <                    U.compareAndSwapObject(p, WCOWAIT, q, q.cowait);
1219 >                    U.compareAndSwapObject(p, WNEXT, q, q.next);
1220                  else
1221                      p = q;
1222              }
1223 <            if (group == null)  {        // unsplice both prev and next links
1224 <                for (WNode pred = node.prev; pred != null; ) {
1225 <                    WNode succ, pp;      // first unsplice next
1223 >            if (group == node) {
1224 >                WNode r; // detach and wake up uncancelled co-waiters
1225 >                while ((r = node.cowait) != null) {
1226 >                    if (U.compareAndSwapObject(node, WCOWAIT, r, r.cowait) &&
1227 >                        (w = r.thread) != null) {
1228 >                        r.thread = null;
1229 >                        U.unpark(w);
1230 >                    }
1231 >                }
1232 >                for (WNode pred = node.prev; pred != null; ) { // unsplice
1233 >                    WNode succ, pp;        // find valid successor
1234                      while ((succ = node.next) == null ||
1235                             succ.status == CANCELLED) {
1236 <                        WNode q = null;  // find successor the slow way
1236 >                        WNode q = null;    // find successor the slow way
1237                          for (WNode t = wtail; t != null && t != node; t = t.prev)
1238                              if (t.status != CANCELLED)
1239 <                                q = t;   // don't link if succ cancelled
1240 <                        if (succ == q || // ensure accurate successor
1239 >                                q = t;     // don't link if succ cancelled
1240 >                        if (succ == q ||   // ensure accurate successor
1241                              U.compareAndSwapObject(node, WNEXT,
1242                                                     succ, succ = q)) {
1243                              if (succ == null && node == wtail)
# Line 1237 | Line 1249 | public class StampedLock implements java
1249                          U.compareAndSwapObject(pred, WNEXT, node, succ);
1250                      if (succ != null && (w = succ.thread) != null) {
1251                          succ.thread = null;
1252 <                        U.unpark(w);      // conservatively wake up new succ
1252 >                        U.unpark(w);       // wake up succ to observe new pred
1253                      }
1254                      if (pred.status != CANCELLED || (pp = pred.prev) == null)
1255                          break;
1256 <                    node.prev = pp; // repeat in case new pred wrong/cancelled
1256 >                    node.prev = pp;        // repeat if new pred wrong/cancelled
1257                      U.compareAndSwapObject(pp, WNEXT, pred, succ);
1258                      pred = pp;
1259                  }
1260              }
1261          }
1262 <        release(whead);
1262 >        WNode h; // Possibly release first waiter
1263 >        while ((h = whead) != null) {
1264 >            long s; WNode q; // similar to release() but check eligibility
1265 >            if ((q = h.next) == null || q.status == CANCELLED) {
1266 >                for (WNode t = wtail; t != null && t != h; t = t.prev)
1267 >                    if (t.status <= 0)
1268 >                        q = t;
1269 >            }
1270 >            if (h == whead) {
1271 >                if (q != null && h.status == 0 &&
1272 >                    ((s = state) & ABITS) != WBIT && // waiter is eligible
1273 >                    (s == 0L || q.mode == RMODE))
1274 >                    release(h);
1275 >                break;
1276 >            }
1277 >        }
1278          return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L;
1279      }
1280  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines