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.37 by jsr166, Sun Jul 14 19:55:05 2013 UTC vs.
Revision 1.38 by dl, Mon Aug 19 14:30:37 2013 UTC

# Line 196 | 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 240 | Line 244 | public class StampedLock implements java
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 388 | 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 666 | 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 985 | 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
990 <                    if ((w = r.thread) != null) {
991 <                        r.thread = null;
992 <                        U.unpark(w);
993 <                    }
994 <                    if ((r = q.cowait) == null)
995 <                        break;
996 <                    U.compareAndSwapObject(q, WCOWAIT, r, r.cowait);
997 <                }
998 <            }
995 >            if (q != null && (w = q.thread) != null)
996 >                U.unpark(w);
997          }
998      }
999  
# Line 1011 | 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              }
1028            else if (spins < 0)
1029                spins = (p == whead) ? SPINS : 0;
1028              else if (node == null)
1029                  node = new WNode(WMODE, p);
1030              else if (node.prev != p)
# Line 1037 | 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 1054 | Line 1056 | public class StampedLock implements java
1056                               --k <= 0)
1057                          break;
1058                  }
1057                if (spins < MAX_HEAD_SPINS)
1058                    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, node, false);
1074 <                Thread wt = Thread.currentThread();
1075 <                U.putObject(wt, PARKBLOCKER, this); // emulate LockSupport.park
1076 <                node.thread = wt;
1077 <                if (node.prev == p && p.status == WAITING && // recheck
1078 <                    (p != whead || (state & ABITS) != 0L))
1079 <                    U.park(false, time);
1080 <                node.thread = null;
1081 <                U.putObject(wt, PARKBLOCKER, null);
1082 <                if (interruptible && Thread.interrupted())
1083 <                    return cancelWaiter(node, node, 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 1095 | 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)
1115 <                                break;
1116 <                            U.compareAndSwapObject(group, WCOWAIT, r, r.cowait);
1132 >                            spins = SPINS;
1133                          }
1134                      }
1119                    return ns;
1135                  }
1121                if (m >= WBIT)
1122                    break;
1123            }
1124            if (spins > 0) {
1125                if (ThreadLocalRandom.current().nextInt() >= 0)
1126                    --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              }
1133            else if (spins < 0)
1134                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;;) {
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);
1150                        if (node.thread == null)
1151                            break;
1152                        if (p.prev != pp || p.status == CANCELLED ||
1153                            p == whead || p.prev != pp) {
1154                            node.thread = null;
1155                            break;
1156                        }
1183                          Thread wt = Thread.currentThread();
1184                          U.putObject(wt, PARKBLOCKER, this);
1185 <                        if (node.thread == null) // must recheck
1186 <                            break;
1187 <                        U.park(false, time);
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                      }
1166                    group = p;
1194                  }
1168                node = null; // throw away
1169            }
1170            else if (U.compareAndSwapObject(this, WTAIL, p, node)) {
1171                p.next = node;
1172                break;
1195              }
1196          }
1197  
1198 <        for (int spins = SPINS;;) {
1199 <            WNode np, pp, r; int ps; long m, s, ns; Thread w;
1200 <            while ((np = node.prev) != p && np != null)
1201 <                (p = np).next = node;
1202 <            if (whead == p) {
1203 <                for (int k = spins;;) {
1204 <                    if ((m = (s = state) & ABITS) != WBIT) {
1205 <                        if (m < RFULL ?
1206 <                            U.compareAndSwapLong(this, STATE, s, ns = s + RUNIT):
1207 <                            (ns = tryIncReaderOverflow(s)) != 0L) {
1208 <                            whead = node;
1209 <                            node.prev = null;
1210 <                            while ((r = node.cowait) != null) {
1211 <                                if (U.compareAndSwapObject(node, WCOWAIT,
1212 <                                                           r, r.cowait) &&
1213 <                                    (w = r.thread) != null) {
1214 <                                    r.thread = null;
1215 <                                    U.unpark(w); // release co-waiter
1216 <                                }
1217 <                            }
1196 <                            return ns;
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 +                        return ns;
1220                      }
1221 <                    else if (ThreadLocalRandom.current().nextInt() >= 0 &&
1222 <                             --k <= 0)
1221 >                    else if (m >= WBIT &&
1222 >                             ThreadLocalRandom.current().nextInt() >= 0 && --k <= 0)
1223                          break;
1224                  }
1203                if (spins < MAX_HEAD_SPINS)
1204                    spins <<= 1;
1225              }
1226 <            if ((ps = p.status) == 0)
1227 <                U.compareAndSwapInt(p, WSTATUS, 0, WAITING);
1228 <            else if (ps == CANCELLED) {
1229 <                if ((pp = p.prev) != null) {
1230 <                    node.prev = pp;
1231 <                    pp.next = node;
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 <            else {
1235 <                long time;
1236 <                if (deadline == 0L)
1237 <                    time = 0L;
1238 <                else if ((time = deadline - System.nanoTime()) <= 0L)
1239 <                    return cancelWaiter(node, node, false);
1240 <                Thread wt = Thread.currentThread();
1241 <                U.putObject(wt, PARKBLOCKER, this);
1242 <                node.thread = wt;
1243 <                if (node.prev == p && p.status == WAITING &&
1244 <                    (p != whead || (state & ABITS) != WBIT))
1245 <                    U.park(false, time);
1246 <                node.thread = null;
1247 <                U.putObject(wt, PARKBLOCKER, null);
1248 <                if (interruptible && Thread.interrupted())
1249 <                    return cancelWaiter(node, node, true);
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      }
# Line 1251 | Line 1286 | public class StampedLock implements java
1286          if (node != null && group != null) {
1287              Thread w;
1288              node.status = CANCELLED;
1254            node.thread = null;
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, WNEXT, q, q.next);
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 <                WNode r; // detach and wake up uncancelled co-waiters
1300 <                while ((r = node.cowait) != null) {
1301 <                    if (U.compareAndSwapObject(node, WCOWAIT, r, r.cowait) &&
1266 <                        (w = r.thread) != null) {
1267 <                        r.thread = null;
1268 <                        U.unpark(w);
1269 <                    }
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines