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

Comparing jsr166/src/jsr166e/ForkJoinPool.java (file contents):
Revision 1.21 by dl, Wed Nov 21 19:54:32 2012 UTC vs.
Revision 1.46 by jsr166, Sun Jan 13 21:56:12 2013 UTC

# Line 51 | Line 51 | import java.util.concurrent.TimeUnit;
51   * dynamically adding, suspending, or resuming internal worker
52   * threads, even if some tasks are stalled waiting to join
53   * others. However, no such adjustments are guaranteed in the face of
54 < * blocked IO or other unmanaged synchronization. The nested {@link
54 > * blocked I/O or other unmanaged synchronization. The nested {@link
55   * ManagedBlocker} interface enables extension of the kinds of
56   * synchronization accommodated.
57   *
# Line 99 | Line 99 | import java.util.concurrent.TimeUnit;
99   *
100   * <p>The common pool is by default constructed with default
101   * parameters, but these may be controlled by setting three {@link
102 < * System#getProperty properties} with prefix {@code
102 > * System#getProperty system properties} with prefix {@code
103   * java.util.concurrent.ForkJoinPool.common}: {@code parallelism} --
104   * an integer greater than zero, {@code threadFactory} -- the class
105   * name of a {@link ForkJoinWorkerThreadFactory}, and {@code
# Line 440 | Line 440 | public class ForkJoinPool extends Abstra
440       * Common Pool
441       * ===========
442       *
443 <     * The static commonPool always exists after static
443 >     * The static common Pool always exists after static
444       * initialization.  Since it (or any other created pool) need
445       * never be used, we minimize initial construction overhead and
446       * footprint to the setup of about a dozen fields, with no nested
# Line 987 | Line 987 | public class ForkJoinPool extends Abstra
987              if (t != null) {
988                  (currentSteal = t).doExec();
989                  currentSteal = null;
990 +                ++nsteals;
991                  if (base - top < 0) {       // process remaining local tasks
992                      if (mode == 0)
993                          popAndExecAll();
994                      else
995                          pollAndExecAll();
996                  }
996                ++nsteals;
997                hint = -1;
997              }
998          }
999  
# Line 1021 | Line 1020 | public class ForkJoinPool extends Abstra
1020                      s != Thread.State.TIMED_WAITING);
1021          }
1022  
1024        /**
1025         * If this owned and is not already interrupted, try to
1026         * interrupt and/or unpark, ignoring exceptions.
1027         */
1028        final void interruptOwner() {
1029            Thread wt, p;
1030            if ((wt = owner) != null && !wt.isInterrupted()) {
1031                try {
1032                    wt.interrupt();
1033                } catch (SecurityException ignore) {
1034                }
1035            }
1036            if ((p = parker) != null)
1037                U.unpark(p);
1038        }
1039
1023          // Unsafe mechanics
1024          private static final sun.misc.Unsafe U;
1025          private static final long QLOCK;
# Line 1091 | Line 1074 | public class ForkJoinPool extends Abstra
1074       * to paranoically avoid potential initialization circularities
1075       * as well as to simplify generated code.
1076       */
1077 <    static final ForkJoinPool commonPool;
1077 >    static final ForkJoinPool common;
1078  
1079      /**
1080 <     * Common pool parallelism. Must equal commonPool.parallelism.
1080 >     * Common pool parallelism. Must equal common.parallelism.
1081       */
1082 <    static final int commonPoolParallelism;
1082 >    static final int commonParallelism;
1083  
1084      /**
1085       * Sequence number for creating workerNamePrefix.
# Line 1104 | Line 1087 | public class ForkJoinPool extends Abstra
1087      private static int poolNumberSequence;
1088  
1089      /**
1090 <     * Return the next sequence number. We don't expect this to
1091 <     * ever contend so use simple builtin sync.
1090 >     * Returns the next sequence number. We don't expect this to
1091 >     * ever contend, so use simple builtin sync.
1092       */
1093      private static final synchronized int nextPoolId() {
1094          return ++poolNumberSequence;
# Line 1129 | Line 1112 | public class ForkJoinPool extends Abstra
1112      private static final long FAST_IDLE_TIMEOUT =  200L * 1000L * 1000L;
1113  
1114      /**
1115 +     * Tolerance for idle timeouts, to cope with timer undershoots
1116 +     */
1117 +    private static final long TIMEOUT_SLOP = 2000000L;
1118 +
1119 +    /**
1120       * The maximum stolen->joining link depth allowed in method
1121       * tryHelpStealer.  Must be a power of two.  Depths for legitimate
1122       * chains are unbounded, but we use a fixed constant to avoid
# Line 1260 | Line 1248 | public class ForkJoinPool extends Abstra
1248      /*
1249       * Acquires the plock lock to protect worker array and related
1250       * updates. This method is called only if an initial CAS on plock
1251 <     * fails. This acts as a spinLock for normal cases, but falls back
1251 >     * fails. This acts as a spinlock for normal cases, but falls back
1252       * to builtin monitor to block when (rarely) needed. This would be
1253       * a terrible idea for a highly contended lock, but works fine as
1254 <     * a more conservative alternative to a pure spinlock.  See
1267 <     * internal ConcurrentHashMap documentation for further
1268 <     * explanation of nearly the same construction.
1254 >     * a more conservative alternative to a pure spinlock.
1255       */
1256      private int acquirePlock() {
1257          int spins = PL_SPINS, r = 0, ps, nps;
# Line 1317 | Line 1303 | public class ForkJoinPool extends Abstra
1303      }
1304  
1305      /**
1306 <     * Performs secondary initialization, called when plock is zero.
1307 <     * Creates workQueue array and sets plock to a valid value.  The
1322 <     * lock body must be exception-free (so no try/finally) so we
1323 <     * optimistically allocate new array outside the lock and throw
1324 <     * away if (very rarely) not needed. (A similar tactic is used in
1325 <     * fullExternalPush.)  Because the plock seq value can eventually
1326 <     * wrap around zero, this method harmlessly fails to reinitialize
1327 <     * if workQueues exists, while still advancing plock.
1328 <     *
1329 <     * Additonally tries to create the first worker.
1330 <     */
1331 <    private void initWorkers() {
1332 <        WorkQueue[] ws, nws; int ps;
1333 <        int p = config & SMASK;        // find power of two table size
1334 <        int n = (p > 1) ? p - 1 : 1;   // ensure at least 2 slots
1335 <        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1336 <        n = (n + 1) << 1;
1337 <        if ((ws = workQueues) == null || ws.length == 0)
1338 <            nws = new WorkQueue[n];
1339 <        else
1340 <            nws = null;
1341 <        if (((ps = plock) & PL_LOCK) != 0 ||
1342 <            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1343 <            ps = acquirePlock();
1344 <        if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1345 <            workQueues = nws;
1346 <        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1347 <        if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1348 <            releasePlock(nps);
1349 <        tryAddWorker();
1350 <    }
1351 <
1352 <    /**
1353 <     * Tries to create and start one worker. Adjusts counts etc on
1354 <     * failure.
1306 >     * Tries to create and start one worker if fewer than target
1307 >     * parallelism level exist. Adjusts counts etc on failure.
1308       */
1309      private void tryAddWorker() {
1310          long c; int u;
# Line 1461 | Line 1414 | public class ForkJoinPool extends Abstra
1414              }
1415          }
1416  
1417 <        long c;                             // adjust ctl counts
1417 >        long c;                          // adjust ctl counts
1418          do {} while (!U.compareAndSwapLong
1419                       (this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) |
1420                                             ((c - TC_UNIT) & TC_MASK) |
1421                                             (c & ~(AC_MASK|TC_MASK)))));
1422  
1423 <        if (!tryTerminate(false, false) && w != null) {
1424 <            w.cancelAll();                  // cancel remaining tasks
1425 <            if (w.array != null)            // suppress signal if never ran
1426 <                tryAddWorker();             // create replacement
1427 <            if (ex == null)                 // help clean refs on way out
1428 <                ForkJoinTask.helpExpungeStaleExceptions();
1423 >        if (!tryTerminate(false, false) && w != null && w.array != null) {
1424 >            w.cancelAll();               // cancel remaining tasks
1425 >            WorkQueue[] ws; WorkQueue v; Thread p; int u, i, e;
1426 >            while ((u = (int)((c = ctl) >>> 32)) < 0 && (e = (int)c) >= 0) {
1427 >                if (e > 0) {             // activate or create replacement
1428 >                    if ((ws = workQueues) == null ||
1429 >                        (i = e & SMASK) >= ws.length ||
1430 >                        (v = ws[i]) == null)
1431 >                        break;
1432 >                    long nc = (((long)(v.nextWait & E_MASK)) |
1433 >                               ((long)(u + UAC_UNIT) << 32));
1434 >                    if (v.eventCount != (e | INT_SIGN))
1435 >                        break;
1436 >                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1437 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1438 >                        if ((p = v.parker) != null)
1439 >                            U.unpark(p);
1440 >                        break;
1441 >                    }
1442 >                }
1443 >                else {
1444 >                    if ((short)u < 0)
1445 >                        tryAddWorker();
1446 >                    break;
1447 >                }
1448 >            }
1449          }
1450 <
1451 <        if (ex != null)                     // rethrow
1450 >        if (ex == null)                     // help clean refs on way out
1451 >            ForkJoinTask.helpExpungeStaleExceptions();
1452 >        else                                // rethrow
1453              ForkJoinTask.rethrow(ex);
1454      }
1455  
# Line 1513 | Line 1487 | public class ForkJoinPool extends Abstra
1487      /**
1488       * Full version of externalPush. This method is called, among
1489       * other times, upon the first submission of the first task to the
1490 <     * pool, so must perform secondary initialization (via
1491 <     * initWorkers). It also detects first submission by an external
1492 <     * thread by looking up its ThreadLocal, and creates a new shared
1493 <     * queue if the one at index if empty or contended. The plock lock
1494 <     * body must be exception-free (so no try/finally) so we
1495 <     * optimistically allocate new queues outside the lock and throw
1496 <     * them away if (very rarely) not needed.
1490 >     * pool, so must perform secondary initialization.  It also
1491 >     * detects first submission by an external thread by looking up
1492 >     * its ThreadLocal, and creates a new shared queue if the one at
1493 >     * index if empty or contended. The plock lock body must be
1494 >     * exception-free (so no try/finally) so we optimistically
1495 >     * allocate new queues outside the lock and throw them away if
1496 >     * (very rarely) not needed.
1497 >     *
1498 >     * Secondary initialization occurs when plock is zero, to create
1499 >     * workQueue array and set plock to a valid value.  This lock body
1500 >     * must also be exception-free. Because the plock seq value can
1501 >     * eventually wrap around zero, this method harmlessly fails to
1502 >     * reinitialize if workQueues exists, while still advancing plock.
1503       */
1504      private void fullExternalPush(ForkJoinTask<?> task) {
1505          int r = 0; // random index seed
# Line 1530 | Line 1510 | public class ForkJoinPool extends Abstra
1510                                          r += SEED_INCREMENT) && r != 0)
1511                      submitters.set(z = new Submitter(r));
1512              }
1513 <            else if (r == 0) {               // move to a different index
1513 >            else if (r == 0) {                  // move to a different index
1514                  r = z.seed;
1515 <                r ^= r << 13;                // same xorshift as WorkQueues
1515 >                r ^= r << 13;                   // same xorshift as WorkQueues
1516                  r ^= r >>> 17;
1517                  z.seed = r ^ (r << 5);
1518              }
1519              else if ((ps = plock) < 0)
1520                  throw new RejectedExecutionException();
1521              else if (ps == 0 || (ws = workQueues) == null ||
1522 <                     (m = ws.length - 1) < 0)
1523 <                initWorkers();
1522 >                     (m = ws.length - 1) < 0) { // initialize workQueues
1523 >                int p = config & SMASK;         // find power of two table size
1524 >                int n = (p > 1) ? p - 1 : 1;    // ensure at least 2 slots
1525 >                n |= n >>> 1; n |= n >>> 2;  n |= n >>> 4;
1526 >                n |= n >>> 8; n |= n >>> 16; n = (n + 1) << 1;
1527 >                WorkQueue[] nws = ((ws = workQueues) == null || ws.length == 0 ?
1528 >                                   new WorkQueue[n] : null);
1529 >                if (((ps = plock) & PL_LOCK) != 0 ||
1530 >                    !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1531 >                    ps = acquirePlock();
1532 >                if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1533 >                    workQueues = nws;
1534 >                int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1535 >                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1536 >                    releasePlock(nps);
1537 >            }
1538              else if ((q = ws[k = r & m & SQMASK]) != null) {
1539                  if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
1540                      ForkJoinTask<?>[] a = q.array;
# Line 1660 | Line 1654 | public class ForkJoinPool extends Abstra
1654       *
1655       * * If not already enqueued, try to inactivate and enqueue the
1656       * worker on wait queue. Or, if inactivating has caused the pool
1657 <     * to be quiescent, relay to idleAwaitWork to check for
1658 <     * termination and possibly shrink pool.
1657 >     * to be quiescent, relay to idleAwaitWork to possibly shrink
1658 >     * pool.
1659       *
1660       * * If already enqueued and none of the above apply, possibly
1661 <     * (with 1/2 probability) park awaiting signal, else lingering to
1662 <     * help scan and signal.
1661 >     * park awaiting signal, else lingering to help scan and signal.
1662 >     *
1663 >     * * If a non-empty queue discovered or left as a hint,
1664 >     * help wake up other workers before return.
1665       *
1666       * @param w the worker (via its WorkQueue)
1667       * @return a task or null if none found
# Line 1676 | Line 1672 | public class ForkJoinPool extends Abstra
1672          if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1673              int ec = w.eventCount;               // ec is negative if inactive
1674              int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1675 +            w.hint = -1;                         // update seed and clear hint
1676              int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN;
1677              do {
1678                  WorkQueue q; ForkJoinTask<?>[] a; int b;
# Line 1697 | Line 1694 | public class ForkJoinPool extends Abstra
1694                  }
1695              } while (--j >= 0);
1696  
1697 <            long c, sc; int e, ns, h;
1698 <            if ((h = w.hint) < 0) {
1699 <                if ((ns = w.nsteals) != 0) {
1700 <                    if (U.compareAndSwapLong(this, STEALCOUNT,
1701 <                                             sc = stealCount, sc + ns))
1702 <                        w.nsteals = 0;           // collect steals
1703 <                }
1704 <                else if (plock != ps)            // consistency check
1705 <                    ;                            // skip
1706 <                else if ((e = (int)(c = ctl)) < 0)
1707 <                    w.qlock = -1;                // pool is terminating
1708 <                else if (ec >= 0) {              // try to enqueue/inactivate
1709 <                    long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1710 <                    w.nextWait = e;              // link and mark inactive
1711 <                    w.eventCount = ec | INT_SIGN;
1712 <                    if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1713 <                        w.eventCount = ec;       // unmark on CAS failure
1714 <                    else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1715 <                        idleAwaitWork(w, nc, c);
1716 <                }
1717 <                else if (w.eventCount < 0) {     // block
1718 <                    Thread wt = Thread.currentThread();
1719 <                    Thread.interrupted();        // clear status
1720 <                    U.putObject(wt, PARKBLOCKER, this);
1721 <                    w.parker = wt;               // emulate LockSupport.park
1722 <                    if (w.eventCount < 0)        // recheck
1723 <                        U.park(false, 0L);
1724 <                    w.parker = null;
1725 <                    U.putObject(wt, PARKBLOCKER, null);
1726 <                }
1727 <            }
1728 <            if (h >= 0 || (h = w.hint) >= 0) {   // signal others before retry
1729 <                w.hint = -1;                     // reset
1730 <                helpSignal(null, h, true);
1697 >            int h, e, ns; long c, sc; WorkQueue q;
1698 >            if ((ns = w.nsteals) != 0) {
1699 >                if (U.compareAndSwapLong(this, STEALCOUNT,
1700 >                                         sc = stealCount, sc + ns))
1701 >                    w.nsteals = 0;               // collect steals and rescan
1702 >            }
1703 >            else if (plock != ps)                // consistency check
1704 >                ;                                // skip
1705 >            else if ((e = (int)(c = ctl)) < 0)
1706 >                w.qlock = -1;                    // pool is terminating
1707 >            else {
1708 >                if ((h = w.hint) < 0) {
1709 >                    if (ec >= 0) {               // try to enqueue/inactivate
1710 >                        long nc = (((long)ec |
1711 >                                    ((c - AC_UNIT) & (AC_MASK|TC_MASK))));
1712 >                        w.nextWait = e;          // link and mark inactive
1713 >                        w.eventCount = ec | INT_SIGN;
1714 >                        if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1715 >                            w.eventCount = ec;   // unmark on CAS failure
1716 >                        else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1717 >                            idleAwaitWork(w, nc, c);
1718 >                    }
1719 >                    else if (w.eventCount < 0 && ctl == c) {
1720 >                        Thread wt = Thread.currentThread();
1721 >                        Thread.interrupted();    // clear status
1722 >                        U.putObject(wt, PARKBLOCKER, this);
1723 >                        w.parker = wt;           // emulate LockSupport.park
1724 >                        if (w.eventCount < 0)    // recheck
1725 >                            U.park(false, 0L);   // block
1726 >                        w.parker = null;
1727 >                        U.putObject(wt, PARKBLOCKER, null);
1728 >                    }
1729 >                }
1730 >                if ((h >= 0 || (h = w.hint) >= 0) &&
1731 >                    (ws = workQueues) != null && h < ws.length &&
1732 >                    (q = ws[h]) != null) {      // signal others before retry
1733 >                    WorkQueue v; Thread p; int u, i, s;
1734 >                    for (int n = (config & SMASK) - 1;;) {
1735 >                        int idleCount = (w.eventCount < 0) ? 0 : -1;
1736 >                        if (((s = idleCount - q.base + q.top) <= n &&
1737 >                             (n = s) <= 0) ||
1738 >                            (u = (int)((c = ctl) >>> 32)) >= 0 ||
1739 >                            (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1740 >                            (v = ws[i]) == null)
1741 >                            break;
1742 >                        long nc = (((long)(v.nextWait & E_MASK)) |
1743 >                                   ((long)(u + UAC_UNIT) << 32));
1744 >                        if (v.eventCount != (e | INT_SIGN) ||
1745 >                            !U.compareAndSwapLong(this, CTL, c, nc))
1746 >                            break;
1747 >                        v.hint = h;
1748 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1749 >                        if ((p = v.parker) != null)
1750 >                            U.unpark(p);
1751 >                        if (--n <= 0)
1752 >                            break;
1753 >                    }
1754 >                }
1755              }
1756          }
1757          return null;
# Line 1750 | Line 1771 | public class ForkJoinPool extends Abstra
1771       */
1772      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1773          if (w != null && w.eventCount < 0 &&
1774 <            !tryTerminate(false, false) && (int)prevCtl != 0) {
1774 >            !tryTerminate(false, false) && (int)prevCtl != 0 &&
1775 >            ctl == currentCtl) {
1776              int dc = -(short)(currentCtl >>> TC_SHIFT);
1777              long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1778 <            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1778 >            long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP;
1779              Thread wt = Thread.currentThread();
1780              while (ctl == currentCtl) {
1781                  Thread.interrupted();  // timed variant of version in scan()
# Line 1768 | Line 1790 | public class ForkJoinPool extends Abstra
1790                  if (deadline - System.nanoTime() <= 0L &&
1791                      U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) {
1792                      w.eventCount = (w.eventCount + E_SEQ) | E_MASK;
1793 +                    w.hint = -1;
1794                      w.qlock = -1;   // shrink
1795                      break;
1796                  }
# Line 1776 | Line 1799 | public class ForkJoinPool extends Abstra
1799      }
1800  
1801      /**
1802 <     * Scans through queues looking for work (optionally, while
1803 <     * joining a task); if any present, signals. May return early if
1804 <     * more signalling is detectably unneeded.
1802 >     * Scans through queues looking for work while joining a task; if
1803 >     * any present, signals. May return early if more signalling is
1804 >     * detectably unneeded.
1805       *
1806 <     * @param task if non-null, return early if done
1806 >     * @param task return early if done
1807       * @param origin an index to start scan
1785     * @param once if only the origin should be checked
1808       */
1809 <    private void helpSignal(ForkJoinTask<?> task, int origin, boolean once) {
1809 >    private void helpSignal(ForkJoinTask<?> task, int origin) {
1810          WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s;
1811 <        if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1811 >        if (task != null && task.status >= 0 &&
1812 >            (u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1813              (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1814 <            outer: for (int k = origin, j = once? 0 : m; j >= 0; --j) {
1814 >            outer: for (int k = origin, j = m; j >= 0; --j) {
1815                  WorkQueue q = ws[k++ & m];
1816                  for (int n = m;;) { // limit to at most m signals
1817 <                    if (task != null && task.status < 0)
1817 >                    if (task.status < 0)
1818                          break outer;
1819                      if (q == null ||
1820 <                        ((s = (task == null ? -1 : 0) - q.base + q.top) <= n &&
1798 <                         (n = s) <= 0))
1820 >                        ((s = -q.base + q.top) <= n && (n = s) <= 0))
1821                          break;
1822                      if ((u = (int)((c = ctl) >>> 32)) >= 0 ||
1823                          (e = (int)c) <= 0 || m < (i = e & SMASK) ||
# Line 1803 | Line 1825 | public class ForkJoinPool extends Abstra
1825                          break outer;
1826                      long nc = (((long)(w.nextWait & E_MASK)) |
1827                                 ((long)(u + UAC_UNIT) << 32));
1828 <                    if (w.eventCount == (e | INT_SIGN) &&
1829 <                        U.compareAndSwapLong(this, CTL, c, nc)) {
1828 >                    if (w.eventCount != (e | INT_SIGN))
1829 >                        break outer;
1830 >                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1831                          w.eventCount = (e + E_SEQ) & E_MASK;
1832                          if ((p = w.parker) != null)
1833                              U.unpark(p);
# Line 1909 | Line 1932 | public class ForkJoinPool extends Abstra
1932       * @param task the task to join
1933       * @param mode if shared, exit upon completing any task
1934       * if all workers are active
1912     *
1935       */
1936      private int helpComplete(ForkJoinTask<?> task, int mode) {
1937          WorkQueue[] ws; WorkQueue q; int m, n, s, u;
# Line 1996 | Line 2018 | public class ForkJoinPool extends Abstra
2018              do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2019                           joiner.tryRemoveAndExec(task)); // process local tasks
2020              if (s >= 0 && (s = task.status) >= 0) {
2021 <                helpSignal(task, joiner.poolIndex, false);
2021 >                helpSignal(task, joiner.poolIndex);
2022                  if ((s = task.status) >= 0 &&
2023                      (task instanceof CountedCompleter))
2024                      s = helpComplete(task, LIFO_QUEUE);
# Line 2005 | Line 2027 | public class ForkJoinPool extends Abstra
2027                  if ((!joiner.isEmpty() ||           // try helping
2028                       (s = tryHelpStealer(joiner, task)) == 0) &&
2029                      (s = task.status) >= 0) {
2030 <                    helpSignal(task, joiner.poolIndex, false);
2030 >                    helpSignal(task, joiner.poolIndex);
2031                      if ((s = task.status) >= 0 && tryCompensate()) {
2032                          if (task.trySetSignal() && (s = task.status) >= 0) {
2033                              synchronized (task) {
# Line 2046 | Line 2068 | public class ForkJoinPool extends Abstra
2068              do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2069                           joiner.tryRemoveAndExec(task));
2070              if (s >= 0 && (s = task.status) >= 0) {
2071 <                helpSignal(task, joiner.poolIndex, false);
2071 >                helpSignal(task, joiner.poolIndex);
2072                  if ((s = task.status) >= 0 &&
2073                      (task instanceof CountedCompleter))
2074                      s = helpComplete(task, LIFO_QUEUE);
# Line 2061 | Line 2083 | public class ForkJoinPool extends Abstra
2083  
2084      /**
2085       * Returns a (probably) non-empty steal queue, if one is found
2086 <     * during a random, then cyclic scan, else null.  This method must
2087 <     * be retried by caller if, by the time it tries to use the queue,
2066 <     * it is empty.
2086 >     * during a scan, else null.  This method must be retried by
2087 >     * caller if, by the time it tries to use the queue, it is empty.
2088       * @param r a (random) seed for scanning
2089       */
2090      private WorkQueue findNonEmptyStealQueue(int r) {
2091 <        for (WorkQueue[] ws;;) {
2092 <            int ps = plock, m, n;
2093 <            if ((ws = workQueues) == null || (m = ws.length - 1) < 1)
2094 <                return null;
2095 <            for (int j = (m + 1) << 2; ;) {
2096 <                WorkQueue q = ws[(((r + j) << 1) | 1) & m];
2097 <                if (q != null && (n = q.base - q.top) < 0) {
2077 <                    if (n < -1)
2078 <                        signalWork(q);
2079 <                    return q;
2080 <                }
2081 <                else if (--j < 0) {
2082 <                    if (plock == ps)
2083 <                        return null;
2084 <                    break;
2091 >        for (;;) {
2092 >            int ps = plock, m; WorkQueue[] ws; WorkQueue q;
2093 >            if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
2094 >                for (int j = (m + 1) << 2; j >= 0; --j) {
2095 >                    if ((q = ws[(((r + j) << 1) | 1) & m]) != null &&
2096 >                        q.base - q.top < 0)
2097 >                        return q;
2098                  }
2099              }
2100 +            if (plock == ps)
2101 +                return null;
2102          }
2103      }
2104  
# Line 2095 | Line 2110 | public class ForkJoinPool extends Abstra
2110       */
2111      final void helpQuiescePool(WorkQueue w) {
2112          for (boolean active = true;;) {
2113 <            ForkJoinTask<?> localTask; // exhaust local queue
2114 <            while ((localTask = w.nextLocalTask()) != null)
2115 <                localTask.doExec();
2116 <            // Similar to loop in scan(), but ignoring submissions
2117 <            WorkQueue q = findNonEmptyStealQueue(w.nextSeed());
2118 <            if (q != null) {
2119 <                ForkJoinTask<?> t; int b;
2113 >            long c; WorkQueue q; ForkJoinTask<?> t; int b;
2114 >            while ((t = w.nextLocalTask()) != null) {
2115 >                if (w.base - w.top < 0)
2116 >                    signalWork(w);
2117 >                t.doExec();
2118 >            }
2119 >            if ((q = findNonEmptyStealQueue(w.nextSeed())) != null) {
2120                  if (!active) {      // re-establish active count
2106                    long c;
2121                      active = true;
2122                      do {} while (!U.compareAndSwapLong
2123                                   (this, CTL, c = ctl, c + AC_UNIT));
2124                  }
2125 <                if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null)
2125 >                if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) {
2126 >                    if (q.base - q.top < 0)
2127 >                        signalWork(q);
2128                      w.runSubtask(t);
2129 +                }
2130              }
2131 <            else {
2132 <                long c;
2133 <                if (active) {       // decrement active count without queuing
2131 >            else if (active) {       // decrement active count without queuing
2132 >                long nc = (c = ctl) - AC_UNIT;
2133 >                if ((int)(nc >> AC_SHIFT) + (config & SMASK) == 0)
2134 >                    return;          // bypass decrement-then-increment
2135 >                if (U.compareAndSwapLong(this, CTL, c, nc))
2136                      active = false;
2118                    do {} while (!U.compareAndSwapLong
2119                                 (this, CTL, c = ctl, c -= AC_UNIT));
2120                }
2121                else
2122                    c = ctl;        // re-increment on exit
2123                if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) {
2124                    do {} while (!U.compareAndSwapLong
2125                                 (this, CTL, c = ctl, c + AC_UNIT));
2126                    break;
2127                }
2137              }
2138 +            else if ((int)((c = ctl) >> AC_SHIFT) + (config & SMASK) == 0 &&
2139 +                     U.compareAndSwapLong(this, CTL, c, c + AC_UNIT))
2140 +                return;
2141          }
2142      }
2143  
# Line 2141 | Line 2153 | public class ForkJoinPool extends Abstra
2153                  return t;
2154              if ((q = findNonEmptyStealQueue(w.nextSeed())) == null)
2155                  return null;
2156 <            if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null)
2156 >            if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) {
2157 >                if (q.base - q.top < 0)
2158 >                    signalWork(q);
2159                  return t;
2160 +            }
2161          }
2162      }
2163  
# Line 2224 | Line 2239 | public class ForkJoinPool extends Abstra
2239       * @return true if now terminating or terminated
2240       */
2241      private boolean tryTerminate(boolean now, boolean enable) {
2242 <        if (this == commonPool)                     // cannot shut down
2242 >        int ps;
2243 >        if (this == common)                    // cannot shut down
2244              return false;
2245 +        if ((ps = plock) >= 0) {                   // enable by setting plock
2246 +            if (!enable)
2247 +                return false;
2248 +            if ((ps & PL_LOCK) != 0 ||
2249 +                !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
2250 +                ps = acquirePlock();
2251 +            int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN;
2252 +            if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
2253 +                releasePlock(nps);
2254 +        }
2255          for (long c;;) {
2256 <            if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2256 >            if (((c = ctl) & STOP_BIT) != 0) {     // already terminating
2257                  if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) {
2258                      synchronized (this) {
2259 <                        notifyAll();                // signal when 0 workers
2259 >                        notifyAll();               // signal when 0 workers
2260                      }
2261                  }
2262                  return true;
2263              }
2264 <            if (plock >= 0) {                       // not yet enabled
2265 <                int ps;
2266 <                if (!enable)
2241 <                    return false;
2242 <                if (((ps = plock) & PL_LOCK) != 0 ||
2243 <                    !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
2244 <                    ps = acquirePlock();
2245 <                int nps = SHUTDOWN;
2246 <                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
2247 <                    releasePlock(nps);
2248 <            }
2249 <            if (!now) {                             // check if idle & no tasks
2250 <                if ((int)(c >> AC_SHIFT) != -(config & SMASK) ||
2251 <                    hasQueuedSubmissions())
2264 >            if (!now) {                            // check if idle & no tasks
2265 >                WorkQueue[] ws; WorkQueue w;
2266 >                if ((int)(c >> AC_SHIFT) != -(config & SMASK))
2267                      return false;
2268 <                // Check for unqueued inactive workers. One pass suffices.
2269 <                WorkQueue[] ws = workQueues; WorkQueue w;
2270 <                if (ws != null) {
2271 <                    for (int i = 1; i < ws.length; i += 2) {
2272 <                        if ((w = ws[i]) != null && w.eventCount >= 0)
2273 <                            return false;
2268 >                if ((ws = workQueues) != null) {
2269 >                    for (int i = 0; i < ws.length; ++i) {
2270 >                        if ((w = ws[i]) != null) {
2271 >                            if (!w.isEmpty()) {    // signal unprocessed tasks
2272 >                                signalWork(w);
2273 >                                return false;
2274 >                            }
2275 >                            if ((i & 1) != 0 && w.eventCount >= 0)
2276 >                                return false;      // unqueued inactive worker
2277 >                        }
2278                      }
2279                  }
2280              }
2281              if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) {
2282                  for (int pass = 0; pass < 3; ++pass) {
2283 <                    WorkQueue[] ws = workQueues;
2284 <                    if (ws != null) {
2266 <                        WorkQueue w;
2283 >                    WorkQueue[] ws; WorkQueue w; Thread wt;
2284 >                    if ((ws = workQueues) != null) {
2285                          int n = ws.length;
2286                          for (int i = 0; i < n; ++i) {
2287                              if ((w = ws[i]) != null) {
2288                                  w.qlock = -1;
2289                                  if (pass > 0) {
2290                                      w.cancelAll();
2291 <                                    if (pass > 1)
2292 <                                        w.interruptOwner();
2291 >                                    if (pass > 1 && (wt = w.owner) != null) {
2292 >                                        if (!wt.isInterrupted()) {
2293 >                                            try {
2294 >                                                wt.interrupt();
2295 >                                            } catch (Throwable ignore) {
2296 >                                            }
2297 >                                        }
2298 >                                        U.unpark(wt);
2299 >                                    }
2300                                  }
2301                              }
2302                          }
2303                          // Wake up workers parked on event queue
2304                          int i, e; long cc; Thread p;
2305                          while ((e = (int)(cc = ctl) & E_MASK) != 0 &&
2306 <                               (i = e & SMASK) < n &&
2306 >                               (i = e & SMASK) < n && i >= 0 &&
2307                                 (w = ws[i]) != null) {
2308                              long nc = ((long)(w.nextWait & E_MASK) |
2309                                         ((cc + AC_UNIT) & AC_MASK) |
# Line 2306 | Line 2331 | public class ForkJoinPool extends Abstra
2331      static WorkQueue commonSubmitterQueue() {
2332          ForkJoinPool p; WorkQueue[] ws; int m; Submitter z;
2333          return ((z = submitters.get()) != null &&
2334 <                (p = commonPool) != null &&
2334 >                (p = common) != null &&
2335                  (ws = p.workQueues) != null &&
2336                  (m = ws.length - 1) >= 0) ?
2337              ws[m & z.seed & SQMASK] : null;
# Line 2320 | Line 2345 | public class ForkJoinPool extends Abstra
2345          ForkJoinTask<?>[] a;  int m, s;
2346          if (t != null &&
2347              (z = submitters.get()) != null &&
2348 <            (p = commonPool) != null &&
2348 >            (p = common) != null &&
2349              (ws = p.workQueues) != null &&
2350              (m = ws.length - 1) >= 0 &&
2351              (q = ws[m & z.seed & SQMASK]) != null &&
# Line 2378 | Line 2403 | public class ForkJoinPool extends Abstra
2403                      (u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
2404                      break;
2405                  if (task == null) {
2406 <                    helpSignal(root, q.poolIndex, false);
2406 >                    helpSignal(root, q.poolIndex);
2407                      if (root.status >= 0)
2408                          helpComplete(root, SHARED_QUEUE);
2409                      break;
# Line 2397 | Line 2422 | public class ForkJoinPool extends Abstra
2422          ForkJoinTask<?>[] a;  int m, s, n;
2423          if (t != null &&
2424              (z = submitters.get()) != null &&
2425 <            (p = commonPool) != null &&
2425 >            (p = common) != null &&
2426              (ws = p.workQueues) != null &&
2427              (m = ws.length - 1) >= 0 &&
2428              (q = ws[m & z.seed & SQMASK]) != null &&
# Line 2421 | Line 2446 | public class ForkJoinPool extends Abstra
2446                  if (t instanceof CountedCompleter)
2447                      p.externalHelpComplete(q, t);
2448                  else
2449 <                    p.helpSignal(t, q.poolIndex, false);
2449 >                    p.helpSignal(t, q.poolIndex);
2450              }
2451          }
2452      }
2453  
2429    /**
2430     * Restricted version of helpQuiescePool for external callers
2431     */
2432    static void externalHelpQuiescePool() {
2433        ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b;
2434        if ((p = commonPool) != null &&
2435            (q = p.findNonEmptyStealQueue(1)) != null &&
2436            (b = q.base) - q.top < 0 &&
2437            (t = q.pollAt(b)) != null)
2438            t.doExec();
2439    }
2440
2454      // Exported methods
2455  
2456      // Constructors
# Line 2536 | Line 2549 | public class ForkJoinPool extends Abstra
2549      }
2550  
2551      /**
2552 <     * Returns the common pool instance.
2552 >     * Returns the common pool instance. This pool is statically
2553 >     * constructed; its run state is unaffected by attempts to {@link
2554 >     * #shutdown} or {@link #shutdownNow}. However this pool and any
2555 >     * ongoing processing are automatically terminated upon program
2556 >     * {@link System#exit}.  Any program that relies on asynchronous
2557 >     * task processing to complete before program termination should
2558 >     * invoke {@code commonPool().}{@link #awaitQuiescence}, before
2559 >     * exit.
2560       *
2561       * @return the common pool instance
2562 +     * @since 1.8
2563       */
2564      public static ForkJoinPool commonPool() {
2565 <        // assert commonPool != null : "static init error";
2566 <        return commonPool;
2565 >        // assert common != null : "static init error";
2566 >        return common;
2567      }
2568  
2569      // Execution methods
# Line 2721 | Line 2742 | public class ForkJoinPool extends Abstra
2742       * Returns the targeted parallelism level of the common pool.
2743       *
2744       * @return the targeted parallelism level of the common pool
2745 +     * @since 1.8
2746       */
2747      public static int getCommonPoolParallelism() {
2748 <        return commonPoolParallelism;
2748 >        return commonParallelism;
2749      }
2750  
2751      /**
# Line 2981 | Line 3003 | public class ForkJoinPool extends Abstra
3003       * Possibly initiates an orderly shutdown in which previously
3004       * submitted tasks are executed, but no new tasks will be
3005       * accepted. Invocation has no effect on execution state if this
3006 <     * is the {@link #commonPool}, and no additional effect if
3006 >     * is the {@link #commonPool()}, and no additional effect if
3007       * already shut down.  Tasks that are in the process of being
3008       * submitted concurrently during the course of this method may or
3009       * may not be rejected.
# Line 2999 | Line 3021 | public class ForkJoinPool extends Abstra
3021      /**
3022       * Possibly attempts to cancel and/or stop all tasks, and reject
3023       * all subsequently submitted tasks.  Invocation has no effect on
3024 <     * execution state if this is the {@link #commonPool}, and no
3024 >     * execution state if this is the {@link #commonPool()}, and no
3025       * additional effect if already shut down. Otherwise, tasks that
3026       * are in the process of being submitted or executed concurrently
3027       * during the course of this method may or may not be
# Line 3036 | Line 3058 | public class ForkJoinPool extends Abstra
3058       * commenced but not yet completed.  This method may be useful for
3059       * debugging. A return of {@code true} reported a sufficient
3060       * period after shutdown may indicate that submitted tasks have
3061 <     * ignored or suppressed interruption, or are waiting for IO,
3061 >     * ignored or suppressed interruption, or are waiting for I/O,
3062       * causing this executor not to properly terminate. (See the
3063       * advisory notes for class {@link ForkJoinTask} stating that
3064       * tasks should not normally entail blocking operations.  But if
# Line 3062 | Line 3084 | public class ForkJoinPool extends Abstra
3084      /**
3085       * Blocks until all tasks have completed execution after a
3086       * shutdown request, or the timeout occurs, or the current thread
3087 <     * is interrupted, whichever happens first. Note that the {@link
3088 <     * #commonPool()} never terminates until program shutdown so
3089 <     * this method will always time out.
3087 >     * is interrupted, whichever happens first. Because the {@link
3088 >     * #commonPool()} never terminates until program shutdown, when
3089 >     * applied to the common pool, this method is equivalent to {@link
3090 >     * #awaitQuiescence} but always returns {@code false}.
3091       *
3092       * @param timeout the maximum time to wait
3093       * @param unit the time unit of the timeout argument
# Line 3074 | Line 3097 | public class ForkJoinPool extends Abstra
3097       */
3098      public boolean awaitTermination(long timeout, TimeUnit unit)
3099          throws InterruptedException {
3100 +        if (Thread.interrupted())
3101 +            throw new InterruptedException();
3102 +        if (this == common) {
3103 +            awaitQuiescence(timeout, unit);
3104 +            return false;
3105 +        }
3106          long nanos = unit.toNanos(timeout);
3107          if (isTerminated())
3108              return true;
# Line 3093 | Line 3122 | public class ForkJoinPool extends Abstra
3122      }
3123  
3124      /**
3125 +     * If called by a ForkJoinTask operating in this pool, equivalent
3126 +     * in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise,
3127 +     * waits and/or attempts to assist performing tasks until this
3128 +     * pool {@link #isQuiescent} or the indicated timeout elapses.
3129 +     *
3130 +     * @param timeout the maximum time to wait
3131 +     * @param unit the time unit of the timeout argument
3132 +     * @return {@code true} if quiescent; {@code false} if the
3133 +     * timeout elapsed.
3134 +     */
3135 +    public boolean awaitQuiescence(long timeout, TimeUnit unit) {
3136 +        long nanos = unit.toNanos(timeout);
3137 +        ForkJoinWorkerThread wt;
3138 +        Thread thread = Thread.currentThread();
3139 +        if ((thread instanceof ForkJoinWorkerThread) &&
3140 +            (wt = (ForkJoinWorkerThread)thread).pool == this) {
3141 +            helpQuiescePool(wt.workQueue);
3142 +            return true;
3143 +        }
3144 +        long startTime = System.nanoTime();
3145 +        WorkQueue[] ws;
3146 +        int r = 0, m;
3147 +        boolean found = true;
3148 +        while (!isQuiescent() && (ws = workQueues) != null &&
3149 +               (m = ws.length - 1) >= 0) {
3150 +            if (!found) {
3151 +                if ((System.nanoTime() - startTime) > nanos)
3152 +                    return false;
3153 +                Thread.yield(); // cannot block
3154 +            }
3155 +            found = false;
3156 +            for (int j = (m + 1) << 2; j >= 0; --j) {
3157 +                ForkJoinTask<?> t; WorkQueue q; int b;
3158 +                if ((q = ws[r++ & m]) != null && (b = q.base) - q.top < 0) {
3159 +                    found = true;
3160 +                    if ((t = q.pollAt(b)) != null) {
3161 +                        if (q.base - q.top < 0)
3162 +                            signalWork(q);
3163 +                        t.doExec();
3164 +                    }
3165 +                    break;
3166 +                }
3167 +            }
3168 +        }
3169 +        return true;
3170 +    }
3171 +
3172 +    /**
3173 +     * Waits and/or attempts to assist performing tasks indefinitely
3174 +     * until the {@link #commonPool()} {@link #isQuiescent}
3175 +     */
3176 +    static void quiesceCommonPool() {
3177 +        common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
3178 +    }
3179 +
3180 +    /**
3181       * Interface for extending managed parallelism for tasks running
3182       * in {@link ForkJoinPool}s.
3183       *
# Line 3308 | Line 3393 | public class ForkJoinPool extends Abstra
3393              par = Runtime.getRuntime().availableProcessors();
3394          if (par > MAX_CAP)
3395              par = MAX_CAP;
3396 <        commonPoolParallelism = par;
3396 >        commonParallelism = par;
3397          long np = (long)(-par); // precompute initial ctl value
3398          long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
3399  
3400 <        commonPool = new ForkJoinPool(par, ct, fac, handler);
3400 >        common = new ForkJoinPool(par, ct, fac, handler);
3401      }
3402  
3403      /**
# Line 3325 | Line 3410 | public class ForkJoinPool extends Abstra
3410      private static sun.misc.Unsafe getUnsafe() {
3411          try {
3412              return sun.misc.Unsafe.getUnsafe();
3413 <        } catch (SecurityException se) {
3414 <            try {
3415 <                return java.security.AccessController.doPrivileged
3416 <                    (new java.security
3417 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
3418 <                        public sun.misc.Unsafe run() throws Exception {
3419 <                            java.lang.reflect.Field f = sun.misc
3420 <                                .Unsafe.class.getDeclaredField("theUnsafe");
3421 <                            f.setAccessible(true);
3422 <                            return (sun.misc.Unsafe) f.get(null);
3423 <                        }});
3424 <            } catch (java.security.PrivilegedActionException e) {
3425 <                throw new RuntimeException("Could not initialize intrinsics",
3426 <                                           e.getCause());
3427 <            }
3413 >        } catch (SecurityException tryReflectionInstead) {}
3414 >        try {
3415 >            return java.security.AccessController.doPrivileged
3416 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
3417 >                public sun.misc.Unsafe run() throws Exception {
3418 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
3419 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
3420 >                        f.setAccessible(true);
3421 >                        Object x = f.get(null);
3422 >                        if (k.isInstance(x))
3423 >                            return k.cast(x);
3424 >                    }
3425 >                    throw new NoSuchFieldError("the Unsafe");
3426 >                }});
3427 >        } catch (java.security.PrivilegedActionException e) {
3428 >            throw new RuntimeException("Could not initialize intrinsics",
3429 >                                       e.getCause());
3430          }
3431      }
3345
3432   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines