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

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.135 by dl, Sun Oct 28 22:36:01 2012 UTC vs.
Revision 1.137 by dl, Tue Oct 30 14:23:11 2012 UTC

# Line 43 | Line 43 | import java.util.concurrent.locks.Condit
43   * tasks that are never joined.
44   *
45   * <p>A static {@link #commonPool} is available and appropriate for
46 < * most applications. The common pool is constructed upon first
47 < * access, or upon usage by any ForkJoinTask that is not explictly
48 < * submitted to a specified pool. Using the common pool normally
49 < * reduces resource usage (its threads are slowly reclaimed during
50 < * periods of non-use, and reinstated upon subsequent use).  The
51 < * common pool is by default constructed with default parameters, but
52 < * these may be controlled by setting any or all of the three
53 < * properties {@code
46 > * most applications. The common pool is used by any ForkJoinTask that
47 > * is not explicitly submitted to a specified pool. Using the common
48 > * pool normally reduces resource usage (its threads are slowly
49 > * reclaimed during periods of non-use, and reinstated upon subsequent
50 > * use).  The common pool is by default constructed with default
51 > * parameters, but these may be controlled by setting any or all of
52 > * the three properties {@code
53   * java.util.concurrent.ForkJoinPool.common.{parallelism,
54   * threadFactory, exceptionHandler}}.
55   *
# Line 236 | Line 235 | public class ForkJoinPool extends Abstra
235       * when locked remains available to check consistency.
236       *
237       * Recording WorkQueues.  WorkQueues are recorded in the
238 <     * "workQueues" array that is created upon pool construction and
239 <     * expanded if necessary.  Updates to the array while recording
240 <     * new workers and unrecording terminated ones are protected from
241 <     * each other by a lock but the array is otherwise concurrently
242 <     * readable, and accessed directly.  To simplify index-based
243 <     * operations, the array size is always a power of two, and all
244 <     * readers must tolerate null slots. Shared (submission) queues
245 <     * are at even indices, worker queues at odd indices. Grouping
246 <     * them together in this way simplifies and speeds up task
248 <     * scanning.
238 >     * "workQueues" array that is created upon first use and expanded
239 >     * if necessary.  Updates to the array while recording new workers
240 >     * and unrecording terminated ones are protected from each other
241 >     * by a lock but the array is otherwise concurrently readable, and
242 >     * accessed directly.  To simplify index-based operations, the
243 >     * array size is always a power of two, and all readers must
244 >     * tolerate null slots. Shared (submission) queues are at even
245 >     * indices, worker queues at odd indices. Grouping them together
246 >     * in this way simplifies and speeds up task scanning.
247       *
248       * All worker thread creation is on-demand, triggered by task
249       * submissions, replacement of terminated workers, and/or
# Line 503 | Line 501 | public class ForkJoinPool extends Abstra
501      }
502  
503      /**
506     * A simple non-reentrant lock used for exclusion when managing
507     * queues and workers. We use a custom lock so that we can readily
508     * probe lock state in constructions that check among alternative
509     * actions. The lock is normally only very briefly held, and
510     * sometimes treated as a spinlock, but other usages block to
511     * reduce overall contention in those cases where locked code
512     * bodies perform allocation/resizing.
513     */
514    static final class Mutex extends AbstractQueuedSynchronizer {
515        public final boolean tryAcquire(int ignore) {
516            return compareAndSetState(0, 1);
517        }
518        public final boolean tryRelease(int ignore) {
519            setState(0);
520            return true;
521        }
522        public final void lock() { acquire(0); }
523        public final void unlock() { release(0); }
524        public final boolean isHeldExclusively() { return getState() == 1; }
525        public final Condition newCondition() { return new ConditionObject(); }
526    }
527
528    /**
504       * Class for artificial tasks that are used to replace the target
505       * of local joins if they are removed from an interior queue slot
506       * in WorkQueue.tryRemoveAndExec. We don't need the proxy to
# Line 716 | Line 691 | public class ForkJoinPool extends Abstra
691  
692          /**
693           * Takes next task, if one exists, in LIFO order.  Call only
694 <         * by owner in unshared queues. (We do not have a shared
720 <         * version of this method because it is never needed.)
694 >         * by owner in unshared queues.
695           */
696          final ForkJoinTask<?> pop() {
697              ForkJoinTask<?>[] a; ForkJoinTask<?> t; int m;
# Line 735 | Line 709 | public class ForkJoinPool extends Abstra
709              return null;
710          }
711  
712 +        final ForkJoinTask<?> sharedPop() {
713 +            ForkJoinTask<?> task = null;
714 +            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
715 +                try {
716 +                    ForkJoinTask<?>[] a; int m;
717 +                    if ((a = array) != null && (m = a.length - 1) >= 0) {
718 +                        for (int s; (s = top - 1) - base >= 0;) {
719 +                            long j = ((m & s) << ASHIFT) + ABASE;
720 +                            ForkJoinTask<?> t =
721 +                                (ForkJoinTask<?>)U.getObject(a, j);
722 +                            if (t == null)
723 +                                break;
724 +                            if (U.compareAndSwapObject(a, j, t, null)) {
725 +                                top = s;
726 +                                task = t;
727 +                                break;
728 +                            }
729 +                        }
730 +                    }
731 +                } finally {
732 +                    runState = 0;
733 +                }
734 +            }
735 +            return task;
736 +        }
737 +
738 +        /**
739 +         * Version of pop that takes top element only if it
740 +         * its root is the given CountedCompleter.
741 +         */
742 +        final ForkJoinTask<?> popCC(CountedCompleter<?> root) {
743 +            ForkJoinTask<?>[] a; int m;
744 +            if (root != null && (a = array) != null && (m = a.length - 1) >= 0) {
745 +                for (int s; (s = top - 1) - base >= 0;) {
746 +                    long j = ((m & s) << ASHIFT) + ABASE;
747 +                    ForkJoinTask<?> t =
748 +                        (ForkJoinTask<?>)U.getObject(a, j);
749 +                    if (t == null || !(t instanceof CountedCompleter) ||
750 +                        ((CountedCompleter<?>)t).getRoot() != root)
751 +                        break;
752 +                    if (U.compareAndSwapObject(a, j, t, null)) {
753 +                        top = s;
754 +                        return t;
755 +                    }
756 +                    if (root.status < 0)
757 +                        break;
758 +                }
759 +            }
760 +            return null;
761 +        }
762 +
763 +        /**
764 +         * Shared version of popCC
765 +         */
766 +        final ForkJoinTask<?> sharedPopCC(CountedCompleter<?> root) {
767 +            ForkJoinTask<?> task = null;
768 +            if (root != null &&
769 +                runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
770 +                try {
771 +                    ForkJoinTask<?>[] a; int m;
772 +                    if ((a = array) != null && (m = a.length - 1) >= 0) {
773 +                        for (int s; (s = top - 1) - base >= 0;) {
774 +                            long j = ((m & s) << ASHIFT) + ABASE;
775 +                            ForkJoinTask<?> t =
776 +                                (ForkJoinTask<?>)U.getObject(a, j);
777 +                            if (t == null || !(t instanceof CountedCompleter) ||
778 +                                ((CountedCompleter<?>)t).getRoot() != root)
779 +                                break;
780 +                            if (U.compareAndSwapObject(a, j, t, null)) {
781 +                                top = s;
782 +                                task = t;
783 +                                break;
784 +                            }
785 +                            if (root.status < 0)
786 +                                break;
787 +                        }
788 +                    }
789 +                } finally {
790 +                    runState = 0;
791 +                }
792 +            }
793 +            return task;
794 +        }
795 +
796          /**
797           * Takes a task in FIFO order if b is base of queue and a task
798           * can be claimed without contention. Specialized versions
# Line 813 | Line 871 | public class ForkJoinPool extends Abstra
871  
872          /**
873           * Version of tryUnpush for shared queues; called by non-FJ
874 <         * submitters. Conservatively fails to unpush if all workers
817 <         * are active unless there are multiple tasks in queue.
874 >         * submitters after prechecking that task probably exists.
875           */
876 <        final boolean trySharedUnpush(ForkJoinTask<?> task, ForkJoinPool p) {
876 >        final boolean trySharedUnpush(ForkJoinTask<?> t) {
877              boolean success = false;
878 <            if (task != null && top != base && runState == 0 &&
822 <                U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
878 >            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
879                  try {
880 <                    ForkJoinTask<?>[] a; int n, s;
881 <                    if ((a = array) != null && (n = (s = top) - base) > 0 &&
882 <                        (n > 1 || p == null || (int)(p.ctl >> AC_SHIFT) < 0)) {
883 <                        int j = (((a.length - 1) & --s) << ASHIFT) + ABASE;
884 <                        if (U.getObjectVolatile(a, j) == task &&
885 <                            U.compareAndSwapObject(a, j, task, null)) {
830 <                            top = s;
831 <                            success = true;
832 <                        }
880 >                    ForkJoinTask<?>[] a; int s;
881 >                    if ((a = array) != null && (s = top) != base &&
882 >                        U.compareAndSwapObject
883 >                        (a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) {
884 >                        top = s;
885 >                        success = true;
886                      }
887                  } finally {
888                      runState = 0;                         // unlock
# Line 1112 | Line 1165 | public class ForkJoinPool extends Abstra
1165      public static final ForkJoinWorkerThreadFactory
1166          defaultForkJoinWorkerThreadFactory;
1167  
1168 +
1169 +    /** Property prefix for constructing common pool */
1170 +    private static final String propPrefix =
1171 +        "java.util.concurrent.ForkJoinPool.common.";
1172 +
1173 +    /**
1174 +     * Common (static) pool. Non-null for public use unless a static
1175 +     * construction exception, but internal usages must null-check on
1176 +     * use.
1177 +     */
1178 +    static final ForkJoinPool commonPool;
1179 +
1180 +    /**
1181 +     * Common pool parallelism. Must equal commonPool.parallelism.
1182 +     */
1183 +    static final int commonPoolParallelism;
1184 +
1185      /**
1186       * Generator for assigning sequence numbers as pool names.
1187       */
# Line 1136 | Line 1206 | public class ForkJoinPool extends Abstra
1206       */
1207      private static final ThreadSubmitter submitters;
1208  
1139    /** Common default pool */
1140    static volatile ForkJoinPool commonPool;
1141
1142    // commonPool construction parameters
1143    private static final String propPrefix =
1144        "java.util.concurrent.ForkJoinPool.common.";
1145    private static final Thread.UncaughtExceptionHandler commonPoolUEH;
1146    private static final ForkJoinWorkerThreadFactory commonPoolFactory;
1147    static final int commonPoolParallelism;
1148
1149    /** Static initialization lock */
1150    private static final Mutex initializationLock;
1151
1209      // static constants
1210  
1211      /**
1212 <     * Initial timeout value (in nanoseconds) for the tread triggering
1212 >     * Initial timeout value (in nanoseconds) for the thread triggering
1213       * quiescence to park waiting for new work. On timeout, the thread
1214       * will instead try to shrink the number of workers.
1215       */
# Line 1281 | Line 1338 | public class ForkJoinPool extends Abstra
1338       * empirically works OK on current JVMs.
1339       */
1340  
1341 +    volatile long stealCount;                  // collects worker counts
1342      volatile long ctl;                         // main pool control
1343      final int parallelism;                     // parallelism level
1344      final int localMode;                       // per-worker scheduling mode
1345 +    volatile int nextWorkerNumber;             // to create worker name string
1346      final int submitMask;                      // submit queue index bound
1347      int nextSeed;                              // for initializing worker seeds
1348 +    volatile int mainLock;                     // spinlock for array updates
1349      volatile int runState;                     // shutdown status and seq
1350      WorkQueue[] workQueues;                    // main registry
1291    final Mutex lock;                          // for registration
1292    final Condition termination;               // for awaitTermination
1351      final ForkJoinWorkerThreadFactory factory; // factory for new workers
1352      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1353 <    final AtomicLong stealCount;               // collect counts when terminated
1354 <    final AtomicInteger nextWorkerNumber;      // to create worker name string
1355 <    String workerNamePrefix;                   // to create worker name string
1353 >    final String workerNamePrefix;             // to create worker name string
1354 >
1355 >    /*
1356 >     * Mechanics for main lock protecting worker array updates.  Uses
1357 >     * the same strategy as ConcurrentHashMap bins -- a spinLock for
1358 >     * normal cases, but falling back to builtin lock when (rarely)
1359 >     * needed.  See internal ConcurrentHashMap documentation for
1360 >     * explanation.
1361 >     */
1362 >
1363 >    static final int LOCK_WAITING = 2; // bit to indicate need for signal
1364 >    static final int MAX_LOCK_SPINS = 1 << 8;
1365 >
1366 >    private void tryAwaitMainLock() {
1367 >        int spins = MAX_LOCK_SPINS, r = 0, h;
1368 >        while (((h = mainLock) & 1) != 0) {
1369 >            if (r == 0)
1370 >                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1371 >            else if (spins >= 0) {
1372 >                r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1373 >                if (r >= 0)
1374 >                    --spins;
1375 >            }
1376 >            else if (U.compareAndSwapInt(this, MAINLOCK, h, h | LOCK_WAITING)) {
1377 >                synchronized (this) {
1378 >                    if ((mainLock & LOCK_WAITING) != 0) {
1379 >                        try {
1380 >                            wait();
1381 >                        } catch (InterruptedException ie) {
1382 >                            Thread.currentThread().interrupt();
1383 >                        }
1384 >                    }
1385 >                    else
1386 >                        notifyAll(); // possibly won race vs signaller
1387 >                }
1388 >                break;
1389 >            }
1390 >        }
1391 >    }
1392  
1393      //  Creating, registering, and deregistering workers
1394  
# Line 1322 | Line 1416 | public class ForkJoinPool extends Abstra
1416       * ForkJoinWorkerThread.
1417       */
1418      final String nextWorkerName() {
1419 <        return workerNamePrefix.concat
1420 <            (Integer.toString(nextWorkerNumber.addAndGet(1)));
1419 >        int n;
1420 >        do {} while(!U.compareAndSwapInt(this, NEXTWORKERNUMBER,
1421 >                                         n = nextWorkerNumber, ++n));
1422 >        return workerNamePrefix.concat(Integer.toString(n));
1423      }
1424  
1425      /**
# Line 1336 | Line 1432 | public class ForkJoinPool extends Abstra
1432       * @param w the worker's queue
1433       */
1434      final void registerWorker(WorkQueue w) {
1435 <        Mutex lock = this.lock;
1436 <        lock.lock();
1435 >        while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1436 >            tryAwaitMainLock();
1437          try {
1438 <            WorkQueue[] ws = workQueues;
1439 <            if (w != null && ws != null) {          // skip on shutdown/failure
1440 <                int rs, n = ws.length, m = n - 1;
1438 >            WorkQueue[] ws;
1439 >            if ((ws = workQueues) == null)
1440 >                ws = workQueues = new WorkQueue[submitMask + 1];
1441 >            if (w != null) {
1442 >                int rs, n =  ws.length, m = n - 1;
1443                  int s = nextSeed += SEED_INCREMENT; // rarely-colliding sequence
1444                  w.seed = (s == 0) ? 1 : s;          // ensure non-zero seed
1445                  int r = (s << 1) | 1;               // use odd-numbered indices
# Line 1361 | Line 1459 | public class ForkJoinPool extends Abstra
1459                  runState = ((rs = runState) & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN);
1460              }
1461          } finally {
1462 <            lock.unlock();
1462 >            if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1463 >                mainLock = 0;
1464 >                synchronized (this) { notifyAll(); };
1465 >            }
1466          }
1467 +
1468      }
1469  
1470      /**
# Line 1375 | Line 1477 | public class ForkJoinPool extends Abstra
1477       * @param ex the exception causing failure, or null if none
1478       */
1479      final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
1378        Mutex lock = this.lock;
1480          WorkQueue w = null;
1481          if (wt != null && (w = wt.workQueue) != null) {
1482              w.runState = -1;                // ensure runState is set
1483 <            stealCount.getAndAdd(w.totalSteals + w.nsteals);
1483 >            long steals = w.totalSteals + w.nsteals, sc;
1484 >            do {} while(!U.compareAndSwapLong(this, STEALCOUNT,
1485 >                                              sc = stealCount, sc + steals));
1486              int idx = w.poolIndex;
1487 <            lock.lock();
1488 <            try {                           // remove record from array
1487 >            while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1488 >                tryAwaitMainLock();
1489 >            try {
1490                  WorkQueue[] ws = workQueues;
1491                  if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w)
1492                      ws[idx] = null;
1493              } finally {
1494 <                lock.unlock();
1494 >                if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1495 >                    mainLock = 0;
1496 >                    synchronized (this) { notifyAll(); };
1497 >                }
1498              }
1499          }
1500  
# Line 1426 | Line 1533 | public class ForkJoinPool extends Abstra
1533          for (int r = s.seed, m = submitMask;;) {
1534              WorkQueue[] ws; WorkQueue q;
1535              int k = r & m & SQMASK;          // use only even indices
1536 <            if (runState < 0 || (ws = workQueues) == null || ws.length <= k)
1536 >            if (runState < 0)
1537                  throw new RejectedExecutionException(); // shutting down
1538 +            else if ((ws = workQueues) == null || ws.length <= k) {
1539 +                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1540 +                    tryAwaitMainLock();
1541 +                try {
1542 +                    if (workQueues == null)
1543 +                        workQueues = new WorkQueue[submitMask + 1];
1544 +                } finally {
1545 +                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1546 +                        mainLock = 0;
1547 +                        synchronized (this) { notifyAll(); };
1548 +                    }
1549 +                }
1550 +            }
1551              else if ((q = ws[k]) == null) {  // create new queue
1552                  WorkQueue nq = new WorkQueue(this, null, SHARED_QUEUE);
1553 <                Mutex lock = this.lock;      // construct outside lock
1554 <                lock.lock();
1555 <                try {                        // recheck under lock
1553 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1554 >                    tryAwaitMainLock();
1555 >                try {
1556                      int rs = runState;       // to update seq
1557                      if (ws == workQueues && ws[k] == null) {
1558                          ws[k] = nq;
1559                          runState = ((rs & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN));
1560                      }
1561                  } finally {
1562 <                    lock.unlock();
1562 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1563 >                        mainLock = 0;
1564 >                        synchronized (this) { notifyAll(); };
1565 >                    }
1566                  }
1567              }
1568              else if (q.trySharedPush(task)) {
# Line 1462 | Line 1585 | public class ForkJoinPool extends Abstra
1585      static void submitToCommonPool(ForkJoinTask<?> task) {
1586          ForkJoinPool p;
1587          if ((p = commonPool) == null)
1588 <            p = ensureCommonPool();
1588 >            throw new RejectedExecutionException("Common Pool Unavailable");
1589          p.doSubmit(task);
1590      }
1591  
1592      /**
1593 +     * Returns true if caller is (or may be) submitter to the common
1594 +     * pool, and not all workers are active, and there appear to be
1595 +     * tasks in the associated submission queue.
1596 +     */
1597 +    static boolean canHelpCommonPool() {
1598 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
1599 +        int k = submitters.get().seed & SQMASK;
1600 +        return ((p = commonPool) != null &&
1601 +                (int)(p.ctl >> AC_SHIFT) < 0 &&
1602 +                (ws = p.workQueues) != null &&
1603 +                ws.length > (k &= p.submitMask) &&
1604 +                (q = ws[k]) != null &&
1605 +                q.top - q.base > 0);
1606 +    }
1607 +
1608 +    /**
1609       * Returns true if the given task was submitted to common pool
1610       * and has not yet commenced execution, and is available for
1611       * removal according to execution policies; if so removing the
# Line 1476 | Line 1615 | public class ForkJoinPool extends Abstra
1615       * @return true if successful
1616       */
1617      static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) {
1618 +        // Peek, looking for task and eligibility before
1619 +        // using trySharedUnpush to actually take it under lock
1620          ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
1621 +        ForkJoinTask<?>[] a; int s;
1622          int k = submitters.get().seed & SQMASK;
1623          return ((p = commonPool) != null &&
1624 +                (int)(p.ctl >> AC_SHIFT) < 0 &&
1625                  (ws = p.workQueues) != null &&
1626                  ws.length > (k &= p.submitMask) &&
1627                  (q = ws[k]) != null &&
1628 <                q.trySharedUnpush(task, p));
1628 >                (a = q.array) != null &&
1629 >                (s = q.top - 1) - q.base >= 0 &&
1630 >                s >= 0 && s < a.length &&
1631 >                a[s] == task &&
1632 >                q.trySharedUnpush(task));
1633      }
1634  
1635 +    /**
1636 +     * Tries to pop a task from common pool with given root
1637 +     */
1638 +    static ForkJoinTask<?> popCCFromCommonPool(CountedCompleter<?> root) {
1639 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
1640 +        ForkJoinTask<?> t;
1641 +        int k = submitters.get().seed & SQMASK;
1642 +        if (root != null &&
1643 +            (p = commonPool) != null &&
1644 +            (int)(p.ctl >> AC_SHIFT) < 0 &&
1645 +            (ws = p.workQueues) != null &&
1646 +            ws.length > (k &= p.submitMask) &&
1647 +            (q = ws[k]) != null && q.top - q.base > 0 &&
1648 +            root.status < 0 &&
1649 +            (t = q.sharedPopCC(root)) != null)
1650 +            return t;
1651 +        return null;
1652 +    }
1653 +
1654 +
1655      // Maintaining ctl counts
1656  
1657      /**
# Line 1956 | Line 2123 | public class ForkJoinPool extends Abstra
2123       */
2124      private WorkQueue findNonEmptyStealQueue(WorkQueue w) {
2125          // Similar to loop in scan(), but ignoring submissions
2126 <        int r;
1960 <        if (w == null) // allow external callers
1961 <            r = ThreadLocalRandom.current().nextInt();
1962 <        else {
1963 <            r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1964 <        }
2126 >        int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
2127          int step = (r >>> 16) | 1;
2128          for (WorkQueue[] ws;;) {
2129              int rs = runState, m;
# Line 2025 | Line 2187 | public class ForkJoinPool extends Abstra
2187       * Restricted version of helpQuiescePool for non-FJ callers
2188       */
2189      static void externalHelpQuiescePool() {
2190 <        ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q;
2191 <        ForkJoinTask<?> t; int b;
2190 >        ForkJoinPool p; WorkQueue[] ws; WorkQueue q, sq;
2191 >        ForkJoinTask<?>[] a; int b;
2192 >        ForkJoinTask<?> t = null;
2193          int k = submitters.get().seed & SQMASK;
2194          if ((p = commonPool) != null &&
2195 +            (int)(p.ctl >> AC_SHIFT) < 0 &&
2196              (ws = p.workQueues) != null &&
2197              ws.length > (k &= p.submitMask) &&
2198 <            (w = ws[k]) != null &&
2199 <            (q = p.findNonEmptyStealQueue(w)) != null &&
2200 <            (b = q.base) - q.top < 0 &&
2201 <            (t = q.pollAt(b)) != null)
2202 <            t.doExec();
2198 >            (q = ws[k]) != null) {
2199 >            while (q.top - q.base > 0) {
2200 >                if ((t = q.sharedPop()) != null)
2201 >                    break;
2202 >            }
2203 >            if (t == null && (sq = p.findNonEmptyStealQueue(q)) != null &&
2204 >                (b = sq.base) - sq.top < 0)
2205 >                t = sq.pollAt(b);
2206 >            if (t != null)
2207 >                t.doExec();
2208 >        }
2209      }
2210  
2211      /**
# Line 2102 | Line 2272 | public class ForkJoinPool extends Abstra
2272       * @return true if now terminating or terminated
2273       */
2274      private boolean tryTerminate(boolean now, boolean enable) {
2105        Mutex lock = this.lock;
2275          for (long c;;) {
2276              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2277                  if ((short)(c >>> TC_SHIFT) == -parallelism) {
2278 <                    lock.lock();                    // don't need try/finally
2279 <                    termination.signalAll();        // signal when 0 workers
2280 <                    lock.unlock();
2278 >                    synchronized(this) {
2279 >                        notifyAll();                // signal when 0 workers
2280 >                    }
2281                  }
2282                  return true;
2283              }
2284              if (runState >= 0) {                    // not yet enabled
2285                  if (!enable)
2286                      return false;
2287 <                lock.lock();
2288 <                runState |= SHUTDOWN;
2289 <                lock.unlock();
2287 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
2288 >                    tryAwaitMainLock();
2289 >                try {
2290 >                    runState |= SHUTDOWN;
2291 >                } finally {
2292 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
2293 >                        mainLock = 0;
2294 >                        synchronized (this) { notifyAll(); };
2295 >                    }
2296 >                }
2297              }
2298              if (!now) {                             // check if idle & no tasks
2299                  if ((int)(c >> AC_SHIFT) != -parallelism ||
# Line 2250 | Line 2426 | public class ForkJoinPool extends Abstra
2426          // Use nearest power 2 for workQueues size. See Hackers Delight sec 3.2.
2427          int n = parallelism - 1;
2428          n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
2429 <        int size = (n + 1) << 1;        // #slots = 2*#workers
2254 <        this.submitMask = size - 1;     // room for max # of submit queues
2255 <        this.workQueues = new WorkQueue[size];
2256 <        this.termination = (this.lock = new Mutex()).newCondition();
2257 <        this.stealCount = new AtomicLong();
2258 <        this.nextWorkerNumber = new AtomicInteger();
2429 >        this.submitMask = ((n + 1) << 1) - 1;
2430          int pn = poolNumberGenerator.incrementAndGet();
2431          StringBuilder sb = new StringBuilder("ForkJoinPool-");
2432          sb.append(Integer.toString(pn));
2433          sb.append("-worker-");
2434          this.workerNamePrefix = sb.toString();
2264        lock.lock();
2435          this.runState = 1;              // set init flag
2266        lock.unlock();
2436      }
2437  
2438      /**
2439 <     * Returns the common pool instance
2439 >     * Constructor for common pool, suitable only for static initialization.
2440 >     * Basically the same as above, but uses smallest possible initial footprint.
2441 >     */
2442 >    ForkJoinPool(int parallelism, int submitMask,
2443 >                 ForkJoinWorkerThreadFactory factory,
2444 >                 Thread.UncaughtExceptionHandler handler) {
2445 >        this.factory = factory;
2446 >        this.ueh = handler;
2447 >        this.submitMask = submitMask;
2448 >        this.parallelism = parallelism;
2449 >        long np = (long)(-parallelism);
2450 >        this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2451 >        this.localMode = LIFO_QUEUE;
2452 >        this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2453 >        this.runState = 1;
2454 >    }
2455 >
2456 >    /**
2457 >     * Returns the common pool instance.
2458       *
2459       * @return the common pool instance
2460       */
2461      public static ForkJoinPool commonPool() {
2462          ForkJoinPool p;
2463 <        return (p = commonPool) != null? p : ensureCommonPool();
2464 <    }
2278 <
2279 <    private static ForkJoinPool ensureCommonPool() {
2280 <        ForkJoinPool p;
2281 <        if ((p = commonPool) == null) {
2282 <            final Mutex lock = initializationLock;
2283 <            lock.lock();
2284 <            try {
2285 <                if ((p = commonPool) == null) {
2286 <                    p = commonPool = new ForkJoinPool(commonPoolParallelism,
2287 <                                                      commonPoolFactory,
2288 <                                                      commonPoolUEH, false);
2289 <                    // use a more informative name string for workers
2290 <                    p.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2291 <                }
2292 <            } finally {
2293 <                lock.unlock();
2294 <            }
2295 <        }
2463 >        if ((p = commonPool) == null)
2464 >            throw new Error("Common Pool Unavailable");
2465          return p;
2466      }
2467  
# Line 2558 | Line 2727 | public class ForkJoinPool extends Abstra
2727       * @return the number of steals
2728       */
2729      public long getStealCount() {
2730 <        long count = stealCount.get();
2730 >        long count = stealCount;
2731          WorkQueue[] ws; WorkQueue w;
2732          if ((ws = workQueues) != null) {
2733              for (int i = 1; i < ws.length; i += 2) {
# Line 2688 | Line 2857 | public class ForkJoinPool extends Abstra
2857      public String toString() {
2858          // Use a single pass through workQueues to collect counts
2859          long qt = 0L, qs = 0L; int rc = 0;
2860 <        long st = stealCount.get();
2860 >        long st = stealCount;
2861          long c = ctl;
2862          WorkQueue[] ws; WorkQueue w;
2863          if ((ws = workQueues) != null) {
# Line 2826 | Line 2995 | public class ForkJoinPool extends Abstra
2995      public boolean awaitTermination(long timeout, TimeUnit unit)
2996          throws InterruptedException {
2997          long nanos = unit.toNanos(timeout);
2998 <        final Mutex lock = this.lock;
2999 <        lock.lock();
3000 <        try {
3001 <            for (;;) {
3002 <                if (isTerminated())
3003 <                    return true;
3004 <                if (nanos <= 0)
3005 <                    return false;
3006 <                nanos = termination.awaitNanos(nanos);
2998 >        if (isTerminated())
2999 >            return true;
3000 >        long startTime = System.nanoTime();
3001 >        boolean terminated = false;
3002 >        synchronized(this) {
3003 >            for (long waitTime = nanos, millis = 0L;;) {
3004 >                if (terminated = isTerminated() ||
3005 >                    waitTime <= 0L ||
3006 >                    (millis = unit.toMillis(waitTime)) <= 0L)
3007 >                    break;
3008 >                wait(millis);
3009 >                waitTime = nanos - (System.nanoTime() - startTime);
3010              }
2839        } finally {
2840            lock.unlock();
3011          }
3012 +        return terminated;
3013      }
3014  
3015      /**
# Line 2970 | Line 3141 | public class ForkJoinPool extends Abstra
3141      private static final long PARKBLOCKER;
3142      private static final int ABASE;
3143      private static final int ASHIFT;
3144 +    private static final long NEXTWORKERNUMBER;
3145 +    private static final long STEALCOUNT;
3146 +    private static final long MAINLOCK;
3147  
3148      static {
3149          poolNumberGenerator = new AtomicInteger();
# Line 2978 | Line 3152 | public class ForkJoinPool extends Abstra
3152          defaultForkJoinWorkerThreadFactory =
3153              new DefaultForkJoinWorkerThreadFactory();
3154          submitters = new ThreadSubmitter();
2981        initializationLock = new Mutex();
3155          int s;
3156          try {
3157              U = getUnsafe();
# Line 2986 | Line 3159 | public class ForkJoinPool extends Abstra
3159              Class<?> ak = ForkJoinTask[].class;
3160              CTL = U.objectFieldOffset
3161                  (k.getDeclaredField("ctl"));
3162 +            NEXTWORKERNUMBER = U.objectFieldOffset
3163 +                (k.getDeclaredField("nextWorkerNumber"));
3164 +            STEALCOUNT = U.objectFieldOffset
3165 +                (k.getDeclaredField("stealCount"));
3166 +            MAINLOCK = U.objectFieldOffset
3167 +                (k.getDeclaredField("mainLock"));
3168              Class<?> tk = Thread.class;
3169              PARKBLOCKER = U.objectFieldOffset
3170                  (tk.getDeclaredField("parkBlocker"));
3171              ABASE = U.arrayBaseOffset(ak);
3172              s = U.arrayIndexScale(ak);
3173 +            ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3174          } catch (Exception e) {
3175              throw new Error(e);
3176          }
3177          if ((s & (s-1)) != 0)
3178              throw new Error("data type scale not a power of two");
3179 <        ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3000 <
3001 <        // Establish configuration for default pool
3002 <        try {
3179 >        try { // Establish common pool
3180              String pp = System.getProperty(propPrefix + "parallelism");
3181              String fp = System.getProperty(propPrefix + "threadFactory");
3182              String up = System.getProperty(propPrefix + "exceptionHandler");
3183 +            ForkJoinWorkerThreadFactory fac = (fp == null) ?
3184 +                defaultForkJoinWorkerThreadFactory :
3185 +                ((ForkJoinWorkerThreadFactory)ClassLoader.
3186 +                 getSystemClassLoader().loadClass(fp).newInstance());
3187 +            Thread.UncaughtExceptionHandler ueh = (up == null)? null :
3188 +                ((Thread.UncaughtExceptionHandler)ClassLoader.
3189 +                 getSystemClassLoader().loadClass(up).newInstance());
3190              int par;
3191              if ((pp == null || (par = Integer.parseInt(pp)) <= 0))
3192                  par = Runtime.getRuntime().availableProcessors();
3193 +            if (par > MAX_CAP)
3194 +                par = MAX_CAP;
3195              commonPoolParallelism = par;
3196 <            if (fp != null)
3197 <                commonPoolFactory = (ForkJoinWorkerThreadFactory)
3198 <                    ClassLoader.getSystemClassLoader().loadClass(fp).newInstance();
3199 <            else
3200 <                commonPoolFactory = defaultForkJoinWorkerThreadFactory;
3015 <            if (up != null)
3016 <                commonPoolUEH = (Thread.UncaughtExceptionHandler)
3017 <                    ClassLoader.getSystemClassLoader().loadClass(up).newInstance();
3018 <            else
3019 <                commonPoolUEH = null;
3196 >            int n = par - 1; // precompute submit mask
3197 >            n |= n >>> 1; n |= n >>> 2; n |= n >>> 4;
3198 >            n |= n >>> 8; n |= n >>> 16;
3199 >            int mask = ((n + 1) << 1) - 1;
3200 >            commonPool = new ForkJoinPool(par, mask, fac, ueh);
3201          } catch (Exception e) {
3202              throw new Error(e);
3203          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines