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.7 by dl, Sun Oct 28 22:35:45 2012 UTC vs.
Revision 1.11 by dl, Wed Oct 31 12:49:13 2012 UTC

# Line 44 | Line 44 | import java.util.concurrent.locks.Condit
44   * tasks that are never joined.
45   *
46   * <p>A static {@link #commonPool} is available and appropriate for
47 < * most applications. The common pool is constructed upon first
48 < * access, or upon usage by any ForkJoinTask that is not explictly
49 < * submitted to a specified pool. Using the common pool normally
50 < * reduces resource usage (its threads are slowly reclaimed during
51 < * periods of non-use, and reinstated upon subsequent use).  The
52 < * common pool is by default constructed with default parameters, but
53 < * these may be controlled by setting any or all of the three
54 < * properties {@code
47 > * most applications. The common pool is used by any ForkJoinTask that
48 > * is not explicitly submitted to a specified pool. Using the common
49 > * pool normally reduces resource usage (its threads are slowly
50 > * reclaimed during periods of non-use, and reinstated upon subsequent
51 > * use).  The common pool is by default constructed with default
52 > * parameters, but these may be controlled by setting any or all of
53 > * the three properties {@code
54   * java.util.concurrent.ForkJoinPool.common.{parallelism,
55   * threadFactory, exceptionHandler}}.
56   *
# Line 237 | Line 236 | public class ForkJoinPool extends Abstra
236       * when locked remains available to check consistency.
237       *
238       * Recording WorkQueues.  WorkQueues are recorded in the
239 <     * "workQueues" array that is created upon pool construction and
240 <     * expanded if necessary.  Updates to the array while recording
241 <     * new workers and unrecording terminated ones are protected from
242 <     * each other by a lock but the array is otherwise concurrently
243 <     * readable, and accessed directly.  To simplify index-based
244 <     * operations, the array size is always a power of two, and all
245 <     * readers must tolerate null slots. Shared (submission) queues
246 <     * are at even indices, worker queues at odd indices. Grouping
247 <     * them together in this way simplifies and speeds up task
249 <     * scanning.
239 >     * "workQueues" array that is created upon first use and expanded
240 >     * if necessary.  Updates to the array while recording new workers
241 >     * and unrecording terminated ones are protected from each other
242 >     * by a lock but the array is otherwise concurrently readable, and
243 >     * accessed directly.  To simplify index-based operations, the
244 >     * array size is always a power of two, and all readers must
245 >     * tolerate null slots. Shared (submission) queues are at even
246 >     * indices, worker queues at odd indices. Grouping them together
247 >     * in this way simplifies and speeds up task scanning.
248       *
249       * All worker thread creation is on-demand, triggered by task
250       * submissions, replacement of terminated workers, and/or
# Line 504 | Line 502 | public class ForkJoinPool extends Abstra
502      }
503  
504      /**
507     * A simple non-reentrant lock used for exclusion when managing
508     * queues and workers. We use a custom lock so that we can readily
509     * probe lock state in constructions that check among alternative
510     * actions. The lock is normally only very briefly held, and
511     * sometimes treated as a spinlock, but other usages block to
512     * reduce overall contention in those cases where locked code
513     * bodies perform allocation/resizing.
514     */
515    static final class Mutex extends AbstractQueuedSynchronizer {
516        public final boolean tryAcquire(int ignore) {
517            return compareAndSetState(0, 1);
518        }
519        public final boolean tryRelease(int ignore) {
520            setState(0);
521            return true;
522        }
523        public final void lock() { acquire(0); }
524        public final void unlock() { release(0); }
525        public final boolean isHeldExclusively() { return getState() == 1; }
526        public final Condition newCondition() { return new ConditionObject(); }
527    }
528
529    /**
505       * Class for artificial tasks that are used to replace the target
506       * of local joins if they are removed from an interior queue slot
507       * in WorkQueue.tryRemoveAndExec. We don't need the proxy to
# Line 717 | Line 692 | public class ForkJoinPool extends Abstra
692  
693          /**
694           * Takes next task, if one exists, in LIFO order.  Call only
695 <         * by owner in unshared queues. (We do not have a shared
721 <         * version of this method because it is never needed.)
695 >         * by owner in unshared queues.
696           */
697          final ForkJoinTask<?> pop() {
698              ForkJoinTask<?>[] a; ForkJoinTask<?> t; int m;
# Line 736 | Line 710 | public class ForkJoinPool extends Abstra
710              return null;
711          }
712  
713 +        final ForkJoinTask<?> sharedPop() {
714 +            ForkJoinTask<?> task = null;
715 +            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
716 +                try {
717 +                    ForkJoinTask<?>[] a; int m;
718 +                    if ((a = array) != null && (m = a.length - 1) >= 0) {
719 +                        for (int s; (s = top - 1) - base >= 0;) {
720 +                            long j = ((m & s) << ASHIFT) + ABASE;
721 +                            ForkJoinTask<?> t =
722 +                                (ForkJoinTask<?>)U.getObject(a, j);
723 +                            if (t == null)
724 +                                break;
725 +                            if (U.compareAndSwapObject(a, j, t, null)) {
726 +                                top = s;
727 +                                task = t;
728 +                                break;
729 +                            }
730 +                        }
731 +                    }
732 +                } finally {
733 +                    runState = 0;
734 +                }
735 +            }
736 +            return task;
737 +        }
738 +
739 +
740          /**
741           * Takes a task in FIFO order if b is base of queue and a task
742           * can be claimed without contention. Specialized versions
# Line 814 | Line 815 | public class ForkJoinPool extends Abstra
815  
816          /**
817           * Version of tryUnpush for shared queues; called by non-FJ
818 <         * submitters. Conservatively fails to unpush if all workers
818 <         * are active unless there are multiple tasks in queue.
818 >         * submitters after prechecking that task probably exists.
819           */
820 <        final boolean trySharedUnpush(ForkJoinTask<?> task, ForkJoinPool p) {
820 >        final boolean trySharedUnpush(ForkJoinTask<?> t) {
821              boolean success = false;
822 <            if (task != null && top != base && runState == 0 &&
823 <                U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
822 >            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
823                  try {
824 <                    ForkJoinTask<?>[] a; int n, s;
825 <                    if ((a = array) != null && (n = (s = top) - base) > 0 &&
826 <                        (n > 1 || p == null || (int)(p.ctl >> AC_SHIFT) < 0)) {
827 <                        int j = (((a.length - 1) & --s) << ASHIFT) + ABASE;
828 <                        if (U.getObjectVolatile(a, j) == task &&
829 <                            U.compareAndSwapObject(a, j, task, null)) {
831 <                            top = s;
832 <                            success = true;
833 <                        }
824 >                    ForkJoinTask<?>[] a; int s;
825 >                    if ((a = array) != null && (s = top) != base &&
826 >                        U.compareAndSwapObject
827 >                        (a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) {
828 >                        top = s;
829 >                        success = true;
830                      }
831                  } finally {
832                      runState = 0;                         // unlock
# Line 914 | Line 910 | public class ForkJoinPool extends Abstra
910              return seed = r ^= r << 5;
911          }
912  
913 <        // Execution methods
913 >        // Specialized execution methods
914  
915          /**
916           * Pops and runs tasks until empty.
# Line 993 | Line 989 | public class ForkJoinPool extends Abstra
989          }
990  
991          /**
992 +         * Version of shared pop that takes top element only if it
993 +         * its root is the given CountedCompleter.
994 +         */
995 +        final CountedCompleter<?> sharedPopCC(CountedCompleter<?> root) {
996 +            CountedCompleter<?> task = null;
997 +            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
998 +                try {
999 +                    ForkJoinTask<?>[] a; int m;
1000 +                    if ((a = array) != null && (m = a.length - 1) >= 0) {
1001 +                        outer:for (int s; (s = top - 1) - base >= 0;) {
1002 +                            long j = ((m & s) << ASHIFT) + ABASE;
1003 +                            ForkJoinTask<?> t =
1004 +                                (ForkJoinTask<?>)U.getObject(a, j);
1005 +                            if (t == null || !(t instanceof CountedCompleter))
1006 +                                break;
1007 +                            CountedCompleter<?> cc = (CountedCompleter<?>)t;
1008 +                            for (CountedCompleter<?> q = cc, p;;) {
1009 +                                if (q == root) {
1010 +                                    if (U.compareAndSwapObject(a, j, cc, null)) {
1011 +                                        top = s;
1012 +                                        task = cc;
1013 +                                        break outer;
1014 +                                    }
1015 +                                    break;
1016 +                                }
1017 +                                if ((p = q.completer) == null)
1018 +                                    break outer;
1019 +                                q = p;
1020 +                            }
1021 +                        }
1022 +                    }
1023 +                } finally {
1024 +                    runState = 0;
1025 +                }
1026 +            }
1027 +            return task;
1028 +        }
1029 +
1030 +        /**
1031           * Executes a top-level task and any local tasks remaining
1032           * after execution.
1033           */
# Line 1113 | Line 1148 | public class ForkJoinPool extends Abstra
1148      public static final ForkJoinWorkerThreadFactory
1149          defaultForkJoinWorkerThreadFactory;
1150  
1151 +    /** Property prefix for constructing common pool */
1152 +    private static final String propPrefix =
1153 +        "java.util.concurrent.ForkJoinPool.common.";
1154 +
1155 +    /**
1156 +     * Common (static) pool. Non-null for public use unless a static
1157 +     * construction exception, but internal usages must null-check on
1158 +     * use.
1159 +     */
1160 +    static final ForkJoinPool commonPool;
1161 +
1162 +    /**
1163 +     * Common pool parallelism. Must equal commonPool.parallelism.
1164 +     */
1165 +    static final int commonPoolParallelism;
1166 +
1167      /**
1168       * Generator for assigning sequence numbers as pool names.
1169       */
# Line 1137 | Line 1188 | public class ForkJoinPool extends Abstra
1188       */
1189      private static final ThreadSubmitter submitters;
1190  
1140    /** Common default pool */
1141    static volatile ForkJoinPool commonPool;
1142
1143    // commonPool construction parameters
1144    private static final String propPrefix =
1145        "java.util.concurrent.ForkJoinPool.common.";
1146    private static final Thread.UncaughtExceptionHandler commonPoolUEH;
1147    private static final ForkJoinWorkerThreadFactory commonPoolFactory;
1148    static final int commonPoolParallelism;
1149
1150    /** Static initialization lock */
1151    private static final Mutex initializationLock;
1152
1191      // static constants
1192  
1193      /**
1194 <     * Initial timeout value (in nanoseconds) for the tread triggering
1194 >     * Initial timeout value (in nanoseconds) for the thread triggering
1195       * quiescence to park waiting for new work. On timeout, the thread
1196       * will instead try to shrink the number of workers.
1197       */
# Line 1282 | Line 1320 | public class ForkJoinPool extends Abstra
1320       * empirically works OK on current JVMs.
1321       */
1322  
1323 +    volatile long stealCount;                  // collects worker counts
1324      volatile long ctl;                         // main pool control
1325      final int parallelism;                     // parallelism level
1326      final int localMode;                       // per-worker scheduling mode
1327 +    volatile int nextWorkerNumber;             // to create worker name string
1328      final int submitMask;                      // submit queue index bound
1329      int nextSeed;                              // for initializing worker seeds
1330 +    volatile int mainLock;                     // spinlock for array updates
1331      volatile int runState;                     // shutdown status and seq
1332      WorkQueue[] workQueues;                    // main registry
1292    final Mutex lock;                          // for registration
1293    final Condition termination;               // for awaitTermination
1333      final ForkJoinWorkerThreadFactory factory; // factory for new workers
1334      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1335 <    final AtomicLong stealCount;               // collect counts when terminated
1336 <    final AtomicInteger nextWorkerNumber;      // to create worker name string
1337 <    String workerNamePrefix;                   // to create worker name string
1335 >    final String workerNamePrefix;             // to create worker name string
1336 >
1337 >    /*
1338 >     * Mechanics for main lock protecting worker array updates.  Uses
1339 >     * the same strategy as ConcurrentHashMap bins -- a spinLock for
1340 >     * normal cases, but falling back to builtin lock when (rarely)
1341 >     * needed.  See internal ConcurrentHashMap documentation for
1342 >     * explanation.
1343 >     */
1344 >
1345 >    static final int LOCK_WAITING = 2; // bit to indicate need for signal
1346 >    static final int MAX_LOCK_SPINS = 1 << 8;
1347 >
1348 >    private void tryAwaitMainLock() {
1349 >        int spins = MAX_LOCK_SPINS, r = 0, h;
1350 >        while (((h = mainLock) & 1) != 0) {
1351 >            if (r == 0)
1352 >                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1353 >            else if (spins >= 0) {
1354 >                r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1355 >                if (r >= 0)
1356 >                    --spins;
1357 >            }
1358 >            else if (U.compareAndSwapInt(this, MAINLOCK, h, h | LOCK_WAITING)) {
1359 >                synchronized (this) {
1360 >                    if ((mainLock & LOCK_WAITING) != 0) {
1361 >                        try {
1362 >                            wait();
1363 >                        } catch (InterruptedException ie) {
1364 >                            try {
1365 >                                Thread.currentThread().interrupt();
1366 >                            } catch (SecurityException ignore) {
1367 >                            }
1368 >                        }
1369 >                    }
1370 >                    else
1371 >                        notifyAll(); // possibly won race vs signaller
1372 >                }
1373 >                break;
1374 >            }
1375 >        }
1376 >    }
1377  
1378      //  Creating, registering, and deregistering workers
1379  
# Line 1323 | Line 1401 | public class ForkJoinPool extends Abstra
1401       * ForkJoinWorkerThread.
1402       */
1403      final String nextWorkerName() {
1404 <        return workerNamePrefix.concat
1405 <            (Integer.toString(nextWorkerNumber.addAndGet(1)));
1404 >        int n;
1405 >        do {} while (!U.compareAndSwapInt(this, NEXTWORKERNUMBER,
1406 >                                          n = nextWorkerNumber, ++n));
1407 >        return workerNamePrefix.concat(Integer.toString(n));
1408      }
1409  
1410      /**
# Line 1337 | Line 1417 | public class ForkJoinPool extends Abstra
1417       * @param w the worker's queue
1418       */
1419      final void registerWorker(WorkQueue w) {
1420 <        Mutex lock = this.lock;
1421 <        lock.lock();
1420 >        while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1421 >            tryAwaitMainLock();
1422          try {
1423 <            WorkQueue[] ws = workQueues;
1424 <            if (w != null && ws != null) {          // skip on shutdown/failure
1423 >            WorkQueue[] ws;
1424 >            if ((ws = workQueues) == null)
1425 >                ws = workQueues = new WorkQueue[submitMask + 1];
1426 >            if (w != null) {
1427                  int rs, n =  ws.length, m = n - 1;
1428                  int s = nextSeed += SEED_INCREMENT; // rarely-colliding sequence
1429                  w.seed = (s == 0) ? 1 : s;          // ensure non-zero seed
# Line 1362 | Line 1444 | public class ForkJoinPool extends Abstra
1444                  runState = ((rs = runState) & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN);
1445              }
1446          } finally {
1447 <            lock.unlock();
1447 >            if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1448 >                mainLock = 0;
1449 >                synchronized (this) { notifyAll(); };
1450 >            }
1451          }
1452      }
1453  
# Line 1376 | Line 1461 | public class ForkJoinPool extends Abstra
1461       * @param ex the exception causing failure, or null if none
1462       */
1463      final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
1379        Mutex lock = this.lock;
1464          WorkQueue w = null;
1465          if (wt != null && (w = wt.workQueue) != null) {
1466              w.runState = -1;                // ensure runState is set
1467 <            stealCount.getAndAdd(w.totalSteals + w.nsteals);
1467 >            long steals = w.totalSteals + w.nsteals, sc;
1468 >            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1469 >                                               sc = stealCount, sc + steals));
1470              int idx = w.poolIndex;
1471 <            lock.lock();
1472 <            try {                           // remove record from array
1471 >            while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1472 >                tryAwaitMainLock();
1473 >            try {
1474                  WorkQueue[] ws = workQueues;
1475                  if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w)
1476                      ws[idx] = null;
1477              } finally {
1478 <                lock.unlock();
1478 >                if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1479 >                    mainLock = 0;
1480 >                    synchronized (this) { notifyAll(); };
1481 >                }
1482              }
1483          }
1484  
# Line 1407 | Line 1497 | public class ForkJoinPool extends Abstra
1497          }
1498  
1499          if (ex != null)                     // rethrow
1500 <            U.throwException(ex);
1500 >            ForkJoinTask.rethrow(ex);
1501      }
1502  
1503      // Submissions
# Line 1427 | Line 1517 | public class ForkJoinPool extends Abstra
1517          for (int r = s.seed, m = submitMask;;) {
1518              WorkQueue[] ws; WorkQueue q;
1519              int k = r & m & SQMASK;          // use only even indices
1520 <            if (runState < 0 || (ws = workQueues) == null || ws.length <= k)
1520 >            if (runState < 0)
1521                  throw new RejectedExecutionException(); // shutting down
1522 +            else if ((ws = workQueues) == null || ws.length <= k) {
1523 +                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1524 +                    tryAwaitMainLock();
1525 +                try {
1526 +                    if (workQueues == null)
1527 +                        workQueues = new WorkQueue[submitMask + 1];
1528 +                } finally {
1529 +                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1530 +                        mainLock = 0;
1531 +                        synchronized (this) { notifyAll(); };
1532 +                    }
1533 +                }
1534 +            }
1535              else if ((q = ws[k]) == null) {  // create new queue
1536                  WorkQueue nq = new WorkQueue(this, null, SHARED_QUEUE);
1537 <                Mutex lock = this.lock;      // construct outside lock
1538 <                lock.lock();
1539 <                try {                        // recheck under lock
1537 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1538 >                    tryAwaitMainLock();
1539 >                try {
1540                      int rs = runState;       // to update seq
1541                      if (ws == workQueues && ws[k] == null) {
1542                          ws[k] = nq;
1543                          runState = ((rs & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN));
1544                      }
1545                  } finally {
1546 <                    lock.unlock();
1546 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1547 >                        mainLock = 0;
1548 >                        synchronized (this) { notifyAll(); };
1549 >                    }
1550                  }
1551              }
1552              else if (q.trySharedPush(task)) {
# Line 1463 | Line 1569 | public class ForkJoinPool extends Abstra
1569      static void submitToCommonPool(ForkJoinTask<?> task) {
1570          ForkJoinPool p;
1571          if ((p = commonPool) == null)
1572 <            p = ensureCommonPool();
1572 >            throw new RejectedExecutionException("Common Pool Unavailable");
1573          p.doSubmit(task);
1574      }
1575  
# Line 1477 | Line 1583 | public class ForkJoinPool extends Abstra
1583       * @return true if successful
1584       */
1585      static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) {
1586 <        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
1587 <        int k = submitters.get().seed & SQMASK;
1588 <        return ((p = commonPool) != null &&
1589 <                (ws = p.workQueues) != null &&
1590 <                ws.length > (k &= p.submitMask) &&
1591 <                (q = ws[k]) != null &&
1592 <                q.trySharedUnpush(task, p));
1586 >        // If not oversaturating platform, peek, looking for task and
1587 >        // eligibility before using trySharedUnpush to actually take
1588 >        // it under lock
1589 >        ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q;
1590 >        ForkJoinTask<?>[] a; int ac, s, m;
1591 >        if ((p = commonPool) != null && (ws = p.workQueues) != null) {
1592 >            int k = submitters.get().seed & p.submitMask & SQMASK;
1593 >            if ((m = ws.length - 1) >= k && (q = ws[k]) != null &&
1594 >                (ac = (int)(p.ctl >> AC_SHIFT)) <= 0) {
1595 >                if (ac == 0) { // double check if all workers active
1596 >                    for (int i = 1; i <= m; i += 2) {
1597 >                        if ((w = ws[i]) != null && w.parker != null) {
1598 >                            ac = -1;
1599 >                            break;
1600 >                        }
1601 >                    }
1602 >                }
1603 >                return (ac < 0 && (a = q.array) != null &&
1604 >                        (s = q.top - 1) - q.base >= 0 &&
1605 >                        s >= 0 && s < a.length &&
1606 >                        a[s] == task &&
1607 >                        q.trySharedUnpush(task));
1608 >            }
1609 >        }
1610 >        return false;
1611 >    }
1612 >
1613 >    /**
1614 >     * Tries to pop and run a task within same computation from common pool
1615 >     */
1616 >    static void popAndExecCCFromCommonPool(CountedCompleter<?> cc) {
1617 >        ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; int m, ac;
1618 >        CountedCompleter<?> par, task;
1619 >        if ((p = commonPool) != null && (ws = p.workQueues) != null) {
1620 >            while ((par = cc.completer) != null) // find root
1621 >                cc = par;
1622 >            int k = submitters.get().seed & p.submitMask & SQMASK;
1623 >            if ((m = ws.length - 1) >= k && (q = ws[k]) != null &&
1624 >                (ac = (int)(p.ctl >> AC_SHIFT)) <= 0) {
1625 >                if (ac == 0) {
1626 >                    for (int i = 1; i <= m; i += 2) {
1627 >                        if ((w = ws[i]) != null && w.parker != null) {
1628 >                            ac = -1;
1629 >                            break;
1630 >                        }
1631 >                    }
1632 >                }
1633 >                if (ac < 0 && q.top - q.base > 0 &&
1634 >                    (task = q.sharedPopCC(cc)) != null)
1635 >                    task.exec();
1636 >            }
1637 >        }
1638      }
1639  
1640      // Maintaining ctl counts
# Line 1957 | Line 2108 | public class ForkJoinPool extends Abstra
2108       */
2109      private WorkQueue findNonEmptyStealQueue(WorkQueue w) {
2110          // Similar to loop in scan(), but ignoring submissions
2111 <        int r;
1961 <        if (w == null) // allow external callers
1962 <            r = ThreadLocalRandom.current().nextInt();
1963 <        else {
1964 <            r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1965 <        }
2111 >        int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
2112          int step = (r >>> 16) | 1;
2113          for (WorkQueue[] ws;;) {
2114              int rs = runState, m;
# Line 2026 | Line 2172 | public class ForkJoinPool extends Abstra
2172       * Restricted version of helpQuiescePool for non-FJ callers
2173       */
2174      static void externalHelpQuiescePool() {
2175 <        ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q;
2176 <        ForkJoinTask<?> t; int b;
2175 >        ForkJoinPool p; WorkQueue[] ws; WorkQueue q, sq;
2176 >        ForkJoinTask<?>[] a; int b;
2177 >        ForkJoinTask<?> t = null;
2178          int k = submitters.get().seed & SQMASK;
2179          if ((p = commonPool) != null &&
2180              (ws = p.workQueues) != null &&
2181              ws.length > (k &= p.submitMask) &&
2182 <            (w = ws[k]) != null &&
2183 <            (q = p.findNonEmptyStealQueue(w)) != null &&
2184 <            (b = q.base) - q.top < 0 &&
2185 <            (t = q.pollAt(b)) != null)
2186 <            t.doExec();
2182 >            (q = ws[k]) != null) {
2183 >            while (q.top - q.base > 0) {
2184 >                if ((t = q.sharedPop()) != null)
2185 >                    break;
2186 >            }
2187 >            if (t == null && (sq = p.findNonEmptyStealQueue(q)) != null &&
2188 >                (b = sq.base) - sq.top < 0)
2189 >                t = sq.pollAt(b);
2190 >            if (t != null)
2191 >                t.doExec();
2192 >        }
2193      }
2194  
2195      /**
# Line 2078 | Line 2231 | public class ForkJoinPool extends Abstra
2231      static int getEstimatedSubmitterQueueLength() {
2232          ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
2233          int k = submitters.get().seed & SQMASK;
2234 <        return ((p = commonPool) != null &&
2082 <                p.runState >= 0 &&
2083 <                (ws = p.workQueues) != null &&
2234 >        return ((p = commonPool) != null && (ws = p.workQueues) != null &&
2235                  ws.length > (k &= p.submitMask) &&
2236                  (q = ws[k]) != null) ?
2237              q.queueSize() : 0;
# Line 2103 | Line 2254 | public class ForkJoinPool extends Abstra
2254       * @return true if now terminating or terminated
2255       */
2256      private boolean tryTerminate(boolean now, boolean enable) {
2106        Mutex lock = this.lock;
2257          for (long c;;) {
2258              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2259                  if ((short)(c >>> TC_SHIFT) == -parallelism) {
2260 <                    lock.lock();                    // don't need try/finally
2261 <                    termination.signalAll();        // signal when 0 workers
2262 <                    lock.unlock();
2260 >                    synchronized (this) {
2261 >                        notifyAll();                // signal when 0 workers
2262 >                    }
2263                  }
2264                  return true;
2265              }
2266              if (runState >= 0) {                    // not yet enabled
2267                  if (!enable)
2268                      return false;
2269 <                lock.lock();
2270 <                runState |= SHUTDOWN;
2271 <                lock.unlock();
2269 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
2270 >                    tryAwaitMainLock();
2271 >                try {
2272 >                    runState |= SHUTDOWN;
2273 >                } finally {
2274 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
2275 >                        mainLock = 0;
2276 >                        synchronized (this) { notifyAll(); };
2277 >                    }
2278 >                }
2279              }
2280              if (!now) {                             // check if idle & no tasks
2281                  if ((int)(c >> AC_SHIFT) != -parallelism ||
# Line 2251 | Line 2408 | public class ForkJoinPool extends Abstra
2408          // Use nearest power 2 for workQueues size. See Hackers Delight sec 3.2.
2409          int n = parallelism - 1;
2410          n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
2411 <        int size = (n + 1) << 1;        // #slots = 2*#workers
2255 <        this.submitMask = size - 1;     // room for max # of submit queues
2256 <        this.workQueues = new WorkQueue[size];
2257 <        this.termination = (this.lock = new Mutex()).newCondition();
2258 <        this.stealCount = new AtomicLong();
2259 <        this.nextWorkerNumber = new AtomicInteger();
2411 >        this.submitMask = ((n + 1) << 1) - 1;
2412          int pn = poolNumberGenerator.incrementAndGet();
2413          StringBuilder sb = new StringBuilder("ForkJoinPool-");
2414          sb.append(Integer.toString(pn));
2415          sb.append("-worker-");
2416          this.workerNamePrefix = sb.toString();
2265        lock.lock();
2417          this.runState = 1;              // set init flag
2267        lock.unlock();
2418      }
2419  
2420      /**
2421 <     * Returns the common pool instance
2421 >     * Constructor for common pool, suitable only for static initialization.
2422 >     * Basically the same as above, but uses smallest possible initial footprint.
2423 >     */
2424 >    ForkJoinPool(int parallelism, int submitMask,
2425 >                 ForkJoinWorkerThreadFactory factory,
2426 >                 Thread.UncaughtExceptionHandler handler) {
2427 >        this.factory = factory;
2428 >        this.ueh = handler;
2429 >        this.submitMask = submitMask;
2430 >        this.parallelism = parallelism;
2431 >        long np = (long)(-parallelism);
2432 >        this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2433 >        this.localMode = LIFO_QUEUE;
2434 >        this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2435 >        this.runState = 1;
2436 >    }
2437 >
2438 >    /**
2439 >     * Returns the common pool instance.
2440       *
2441       * @return the common pool instance
2442       */
2443      public static ForkJoinPool commonPool() {
2444          ForkJoinPool p;
2445 <        return (p = commonPool) != null? p : ensureCommonPool();
2446 <    }
2279 <
2280 <    private static ForkJoinPool ensureCommonPool() {
2281 <        ForkJoinPool p;
2282 <        if ((p = commonPool) == null) {
2283 <            final Mutex lock = initializationLock;
2284 <            lock.lock();
2285 <            try {
2286 <                if ((p = commonPool) == null) {
2287 <                    p = commonPool = new ForkJoinPool(commonPoolParallelism,
2288 <                                                      commonPoolFactory,
2289 <                                                      commonPoolUEH, false);
2290 <                    // use a more informative name string for workers
2291 <                    p.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2292 <                }
2293 <            } finally {
2294 <                lock.unlock();
2295 <            }
2296 <        }
2445 >        if ((p = commonPool) == null)
2446 >            throw new Error("Common Pool Unavailable");
2447          return p;
2448      }
2449  
# Line 2559 | Line 2709 | public class ForkJoinPool extends Abstra
2709       * @return the number of steals
2710       */
2711      public long getStealCount() {
2712 <        long count = stealCount.get();
2712 >        long count = stealCount;
2713          WorkQueue[] ws; WorkQueue w;
2714          if ((ws = workQueues) != null) {
2715              for (int i = 1; i < ws.length; i += 2) {
# Line 2689 | Line 2839 | public class ForkJoinPool extends Abstra
2839      public String toString() {
2840          // Use a single pass through workQueues to collect counts
2841          long qt = 0L, qs = 0L; int rc = 0;
2842 <        long st = stealCount.get();
2842 >        long st = stealCount;
2843          long c = ctl;
2844          WorkQueue[] ws; WorkQueue w;
2845          if ((ws = workQueues) != null) {
# Line 2827 | Line 2977 | public class ForkJoinPool extends Abstra
2977      public boolean awaitTermination(long timeout, TimeUnit unit)
2978          throws InterruptedException {
2979          long nanos = unit.toNanos(timeout);
2980 <        final Mutex lock = this.lock;
2981 <        lock.lock();
2982 <        try {
2983 <            for (;;) {
2984 <                if (isTerminated())
2985 <                    return true;
2986 <                if (nanos <= 0)
2987 <                    return false;
2988 <                nanos = termination.awaitNanos(nanos);
2980 >        if (isTerminated())
2981 >            return true;
2982 >        long startTime = System.nanoTime();
2983 >        boolean terminated = false;
2984 >        synchronized (this) {
2985 >            for (long waitTime = nanos, millis = 0L;;) {
2986 >                if (terminated = isTerminated() ||
2987 >                    waitTime <= 0L ||
2988 >                    (millis = unit.toMillis(waitTime)) <= 0L)
2989 >                    break;
2990 >                wait(millis);
2991 >                waitTime = nanos - (System.nanoTime() - startTime);
2992              }
2840        } finally {
2841            lock.unlock();
2993          }
2994 +        return terminated;
2995      }
2996  
2997      /**
# Line 2971 | Line 3123 | public class ForkJoinPool extends Abstra
3123      private static final long PARKBLOCKER;
3124      private static final int ABASE;
3125      private static final int ASHIFT;
3126 +    private static final long NEXTWORKERNUMBER;
3127 +    private static final long STEALCOUNT;
3128 +    private static final long MAINLOCK;
3129  
3130      static {
3131          poolNumberGenerator = new AtomicInteger();
# Line 2979 | Line 3134 | public class ForkJoinPool extends Abstra
3134          defaultForkJoinWorkerThreadFactory =
3135              new DefaultForkJoinWorkerThreadFactory();
3136          submitters = new ThreadSubmitter();
2982        initializationLock = new Mutex();
3137          int s;
3138          try {
3139              U = getUnsafe();
# Line 2987 | Line 3141 | public class ForkJoinPool extends Abstra
3141              Class<?> ak = ForkJoinTask[].class;
3142              CTL = U.objectFieldOffset
3143                  (k.getDeclaredField("ctl"));
3144 +            NEXTWORKERNUMBER = U.objectFieldOffset
3145 +                (k.getDeclaredField("nextWorkerNumber"));
3146 +            STEALCOUNT = U.objectFieldOffset
3147 +                (k.getDeclaredField("stealCount"));
3148 +            MAINLOCK = U.objectFieldOffset
3149 +                (k.getDeclaredField("mainLock"));
3150              Class<?> tk = Thread.class;
3151              PARKBLOCKER = U.objectFieldOffset
3152                  (tk.getDeclaredField("parkBlocker"));
3153              ABASE = U.arrayBaseOffset(ak);
3154              s = U.arrayIndexScale(ak);
3155 +            ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3156          } catch (Exception e) {
3157              throw new Error(e);
3158          }
3159          if ((s & (s-1)) != 0)
3160              throw new Error("data type scale not a power of two");
3161 <        ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3001 <
3002 <        // Establish configuration for default pool
3003 <        try {
3161 >        try { // Establish common pool
3162              String pp = System.getProperty(propPrefix + "parallelism");
3163              String fp = System.getProperty(propPrefix + "threadFactory");
3164              String up = System.getProperty(propPrefix + "exceptionHandler");
3165 +            ForkJoinWorkerThreadFactory fac = (fp == null) ?
3166 +                defaultForkJoinWorkerThreadFactory :
3167 +                ((ForkJoinWorkerThreadFactory)ClassLoader.
3168 +                 getSystemClassLoader().loadClass(fp).newInstance());
3169 +            Thread.UncaughtExceptionHandler ueh = (up == null) ? null :
3170 +                ((Thread.UncaughtExceptionHandler)ClassLoader.
3171 +                 getSystemClassLoader().loadClass(up).newInstance());
3172              int par;
3173              if ((pp == null || (par = Integer.parseInt(pp)) <= 0))
3174                  par = Runtime.getRuntime().availableProcessors();
3175 +            if (par > MAX_CAP)
3176 +                par = MAX_CAP;
3177              commonPoolParallelism = par;
3178 <            if (fp != null)
3179 <                commonPoolFactory = (ForkJoinWorkerThreadFactory)
3180 <                    ClassLoader.getSystemClassLoader().loadClass(fp).newInstance();
3181 <            else
3182 <                commonPoolFactory = defaultForkJoinWorkerThreadFactory;
3016 <            if (up != null)
3017 <                commonPoolUEH = (Thread.UncaughtExceptionHandler)
3018 <                    ClassLoader.getSystemClassLoader().loadClass(up).newInstance();
3019 <            else
3020 <                commonPoolUEH = null;
3178 >            int n = par - 1; // precompute submit mask
3179 >            n |= n >>> 1; n |= n >>> 2; n |= n >>> 4;
3180 >            n |= n >>> 8; n |= n >>> 16;
3181 >            int mask = ((n + 1) << 1) - 1;
3182 >            commonPool = new ForkJoinPool(par, mask, fac, ueh);
3183          } catch (Exception e) {
3184              throw new Error(e);
3185          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines