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.2 by jsr166, Mon Aug 13 18:25:53 2012 UTC vs.
Revision 1.11 by dl, Wed Oct 31 12:49:13 2012 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166e;
8 +
9   import java.util.ArrayList;
10   import java.util.Arrays;
11   import java.util.Collection;
# Line 17 | Line 18 | import java.util.concurrent.ExecutorServ
18   import java.util.concurrent.Future;
19   import java.util.concurrent.RejectedExecutionException;
20   import java.util.concurrent.RunnableFuture;
21 + import java.util.concurrent.ThreadLocalRandom;
22   import java.util.concurrent.TimeUnit;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import java.util.concurrent.atomic.AtomicLong;
# Line 41 | Line 43 | import java.util.concurrent.locks.Condit
43   * ForkJoinPool}s may also be appropriate for use with event-style
44   * tasks that are never joined.
45   *
46 < * <p>A {@code ForkJoinPool} is constructed with a given target
47 < * parallelism level; by default, equal to the number of available
48 < * processors. The pool attempts to maintain enough active (or
49 < * available) threads by dynamically adding, suspending, or resuming
50 < * internal worker threads, even if some tasks are stalled waiting to
51 < * join others. However, no such adjustments are guaranteed in the
52 < * face of blocked IO or other unmanaged synchronization. The nested
53 < * {@link ManagedBlocker} interface enables extension of the kinds of
46 > * <p>A static {@link #commonPool} is available and appropriate for
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 > *
57 > * <p>For applications that require separate or custom pools, a {@code
58 > * ForkJoinPool} may be constructed with a given target parallelism
59 > * level; by default, equal to the number of available processors. The
60 > * pool attempts to maintain enough active (or available) threads by
61 > * dynamically adding, suspending, or resuming internal worker
62 > * threads, even if some tasks are stalled waiting to join
63 > * others. However, no such adjustments are guaranteed in the face of
64 > * blocked IO or other unmanaged synchronization. The nested {@link
65 > * ManagedBlocker} interface enables extension of the kinds of
66   * synchronization accommodated.
67   *
68   * <p>In addition to execution and lifecycle control methods, this
# Line 93 | Line 107 | import java.util.concurrent.locks.Condit
107   *  </tr>
108   * </table>
109   *
96 * <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is
97 * used for all parallel task execution in a program or subsystem.
98 * Otherwise, use would not usually outweigh the construction and
99 * bookkeeping overhead of creating a large set of threads. For
100 * example, a common pool could be used for the {@code SortTasks}
101 * illustrated in {@link RecursiveAction}. Because {@code
102 * ForkJoinPool} uses threads in {@linkplain java.lang.Thread#isDaemon
103 * daemon} mode, there is typically no need to explicitly {@link
104 * #shutdown} such a pool upon program exit.
105 *
106 *  <pre> {@code
107 * static final ForkJoinPool mainPool = new ForkJoinPool();
108 * ...
109 * public void sort(long[] array) {
110 *   mainPool.invoke(new SortTask(array, 0, array.length));
111 * }}</pre>
112 *
110   * <p><b>Implementation notes</b>: This implementation restricts the
111   * maximum number of running threads to 32767. Attempts to create
112   * pools with greater than the maximum number result in
# Line 239 | 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
251 <     * 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 320 | Line 316 | public class ForkJoinPool extends Abstra
316       *
317       * Trimming workers. To release resources after periods of lack of
318       * use, a worker starting to wait when the pool is quiescent will
319 <     * time out and terminate if the pool has remained quiescent for
320 <     * SHRINK_RATE nanosecs. This will slowly propagate, eventually
321 <     * terminating all workers after long periods of non-use.
319 >     * time out and terminate if the pool has remained quiescent for a
320 >     * given period -- a short period if there are more threads than
321 >     * parallelism, longer as the number of threads decreases. This
322 >     * will slowly propagate, eventually terminating all workers after
323 >     * periods of non-use.
324       *
325       * Shutdown and Termination. A call to shutdownNow atomically sets
326       * a runState bit and then (non-atomically) sets each worker's
# 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 813 | Line 814 | public class ForkJoinPool extends Abstra
814          }
815  
816          /**
817 +         * Version of tryUnpush for shared queues; called by non-FJ
818 +         * submitters after prechecking that task probably exists.
819 +         */
820 +        final boolean trySharedUnpush(ForkJoinTask<?> t) {
821 +            boolean success = false;
822 +            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
823 +                try {
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
833 +                }
834 +            }
835 +            return success;
836 +        }
837 +
838 +        /**
839           * Polls the given task only if it is at the current base.
840           */
841          final boolean pollFor(ForkJoinTask<?> task) {
# Line 887 | 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 966 | 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 1047 | Line 1109 | public class ForkJoinPool extends Abstra
1109              ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
1110          }
1111      }
1112 +
1113      /**
1114       * Per-thread records for threads that submit to pools. Currently
1115       * holds only pseudo-random seed / index that is used to choose
# Line 1085 | 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 1112 | Line 1191 | public class ForkJoinPool extends Abstra
1191      // static constants
1192  
1193      /**
1194 <     * The wakeup interval (in nanoseconds) for a worker waiting for a
1195 <     * task when the pool is quiescent to instead try to shrink the
1196 <     * number of workers.  The exact value does not matter too
1118 <     * much. It must be short enough to release resources during
1119 <     * sustained periods of idleness, but not so short that threads
1120 <     * are continually re-created.
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       */
1198 <    private static final long SHRINK_RATE =
1123 <        4L * 1000L * 1000L * 1000L; // 4 seconds
1198 >    private static final long IDLE_TIMEOUT      = 1000L * 1000L * 1000L; // 1sec
1199  
1200      /**
1201 <     * The timeout value for attempted shrinkage, includes
1127 <     * some slop to cope with system timer imprecision.
1201 >     * Timeout value when there are more threads than parallelism level
1202       */
1203 <    private static final long SHRINK_TIMEOUT = SHRINK_RATE - (SHRINK_RATE / 10);
1203 >    private static final long FAST_IDLE_TIMEOUT =  100L * 1000L * 1000L;
1204  
1205      /**
1206       * The maximum stolen->joining link depth allowed in method
# Line 1246 | 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
1256    final Mutex lock;                          // for registration
1257    final Condition termination;               // for awaitTermination
1333      final ForkJoinWorkerThreadFactory factory; // factory for new workers
1334      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1260    final AtomicLong stealCount;               // collect counts when terminated
1261    final AtomicInteger nextWorkerNumber;      // 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  
1380      /**
# Line 1287 | 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 1300 | Line 1416 | public class ForkJoinPool extends Abstra
1416       *
1417       * @param w the worker's queue
1418       */
1303
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 1327 | 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 1341 | 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) {
1344        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 1372 | Line 1497 | public class ForkJoinPool extends Abstra
1497          }
1498  
1499          if (ex != null)                     // rethrow
1500 <            U.throwException(ex);
1500 >            ForkJoinTask.rethrow(ex);
1501      }
1502  
1378
1503      // Submissions
1504  
1505      /**
# Line 1393 | 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 1423 | Line 1563 | public class ForkJoinPool extends Abstra
1563          }
1564      }
1565  
1566 +    /**
1567 +     * Submits the given (non-null) task to the common pool, if possible.
1568 +     */
1569 +    static void submitToCommonPool(ForkJoinTask<?> task) {
1570 +        ForkJoinPool p;
1571 +        if ((p = commonPool) == null)
1572 +            throw new RejectedExecutionException("Common Pool Unavailable");
1573 +        p.doSubmit(task);
1574 +    }
1575 +
1576 +    /**
1577 +     * Returns true if the given task was submitted to common pool
1578 +     * and has not yet commenced execution, and is available for
1579 +     * removal according to execution policies; if so removing the
1580 +     * submission from the pool.
1581 +     *
1582 +     * @param task the task
1583 +     * @return true if successful
1584 +     */
1585 +    static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) {
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
1641  
1642      /**
# Line 1434 | Line 1648 | public class ForkJoinPool extends Abstra
1648      }
1649  
1650      /**
1651 <     * Tries to activate or create a worker if too few are active.
1651 >     * Tries to create one or activate one or more workers if too few are active.
1652       */
1653      final void signalWork() {
1654          long c; int u;
# Line 1518 | Line 1732 | public class ForkJoinPool extends Abstra
1732       * awaiting signal,
1733       *
1734       * @param w the worker (via its WorkQueue)
1735 <     * @return a task or null of none found
1735 >     * @return a task or null if none found
1736       */
1737      private final ForkJoinTask<?> scan(WorkQueue w) {
1738          WorkQueue[] ws;                       // first update random seed
# Line 1535 | Line 1749 | public class ForkJoinPool extends Abstra
1749                      t = (ForkJoinTask<?>)U.getObjectVolatile(a, i);
1750                      if (q.base == b && ec >= 0 && t != null &&
1751                          U.compareAndSwapObject(a, i, t, null)) {
1752 <                        if (q.top - (q.base = b + 1) > 1)
1752 >                        if (q.top - (q.base = b + 1) > 0)
1753                              signalWork();    // help pushes signal
1754                          return t;
1755                      }
# Line 1581 | Line 1795 | public class ForkJoinPool extends Abstra
1795                  }
1796              }
1797              else if (w.eventCount < 0) {      // already queued
1798 <                if ((nr = w.rescans) > 0) {   // continue rescanning
1799 <                    int ac = a + parallelism;
1800 <                    if (((w.rescans = (ac < nr) ? ac : nr - 1) & 3) == 0)
1801 <                        Thread.yield();       // yield before block
1588 <                }
1589 <                else {
1798 >                int ac = a + parallelism;
1799 >                if ((nr = w.rescans) > 0)     // continue rescanning
1800 >                    w.rescans = (ac < nr) ? ac : nr - 1;
1801 >                else if (((w.seed >>> 16) & ac) == 0) { // randomize park
1802                      Thread.interrupted();     // clear status
1803                      Thread wt = Thread.currentThread();
1804                      U.putObject(wt, PARKBLOCKER, this);
# Line 1604 | Line 1816 | public class ForkJoinPool extends Abstra
1816      /**
1817       * If inactivating worker w has caused the pool to become
1818       * quiescent, checks for pool termination, and, so long as this is
1819 <     * not the only worker, waits for event for up to SHRINK_RATE
1820 <     * nanosecs.  On timeout, if ctl has not changed, terminates the
1819 >     * not the only worker, waits for event for up to a given
1820 >     * duration.  On timeout, if ctl has not changed, terminates the
1821       * worker, which will in turn wake up another worker to possibly
1822       * repeat this process.
1823       *
# Line 1616 | Line 1828 | public class ForkJoinPool extends Abstra
1828      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1829          if (w.eventCount < 0 && !tryTerminate(false, false) &&
1830              (int)prevCtl != 0 && !hasQueuedSubmissions() && ctl == currentCtl) {
1831 +            int dc = -(short)(currentCtl >>> TC_SHIFT);
1832 +            long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1833 +            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1834              Thread wt = Thread.currentThread();
1620            Thread.yield();            // yield before block
1835              while (ctl == currentCtl) {
1622                long startTime = System.nanoTime();
1836                  Thread.interrupted();  // timed variant of version in scan()
1837                  U.putObject(wt, PARKBLOCKER, this);
1838                  w.parker = wt;
1839                  if (ctl == currentCtl)
1840 <                    U.park(false, SHRINK_RATE);
1840 >                    U.park(false, parkTime);
1841                  w.parker = null;
1842                  U.putObject(wt, PARKBLOCKER, null);
1843                  if (ctl != currentCtl)
1844                      break;
1845 <                if (System.nanoTime() - startTime >= SHRINK_TIMEOUT &&
1845 >                if (deadline - System.nanoTime() <= 0L &&
1846                      U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) {
1847                      w.eventCount = (w.eventCount + E_SEQ) | E_MASK;
1848                      w.runState = -1;   // shrink
# Line 1914 | Line 2127 | public class ForkJoinPool extends Abstra
2127          }
2128      }
2129  
1917
2130      /**
2131       * Runs tasks until {@code isQuiescent()}. We piggyback on
2132       * active count ctl maintenance, but rather than blocking
# Line 1957 | Line 2169 | public class ForkJoinPool extends Abstra
2169      }
2170  
2171      /**
2172 +     * Restricted version of helpQuiescePool for non-FJ callers
2173 +     */
2174 +    static void externalHelpQuiescePool() {
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 +            (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 +    /**
2196       * Gets and removes a local or stolen task for the given worker.
2197       *
2198       * @return a task, if available
# Line 1989 | Line 2225 | public class ForkJoinPool extends Abstra
2225                  8);
2226      }
2227  
2228 +    /**
2229 +     * Returns approximate submission queue length for the given caller
2230 +     */
2231 +    static int getEstimatedSubmitterQueueLength() {
2232 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
2233 +        int k = submitters.get().seed & SQMASK;
2234 +        return ((p = commonPool) != null && (ws = p.workQueues) != null &&
2235 +                ws.length > (k &= p.submitMask) &&
2236 +                (q = ws[k]) != null) ?
2237 +            q.queueSize() : 0;
2238 +    }
2239 +
2240      //  Termination
2241  
2242      /**
# Line 2006 | Line 2254 | public class ForkJoinPool extends Abstra
2254       * @return true if now terminating or terminated
2255       */
2256      private boolean tryTerminate(boolean now, boolean enable) {
2009        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 2154 | 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
2158 <        this.submitMask = size - 1;     // room for max # of submit queues
2159 <        this.workQueues = new WorkQueue[size];
2160 <        this.termination = (this.lock = new Mutex()).newCondition();
2161 <        this.stealCount = new AtomicLong();
2162 <        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();
2168        lock.lock();
2417          this.runState = 1;              // set init flag
2418 <        lock.unlock();
2418 >    }
2419 >
2420 >    /**
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 >        if ((p = commonPool) == null)
2446 >            throw new Error("Common Pool Unavailable");
2447 >        return p;
2448      }
2449  
2450      // Execution methods
# Line 2343 | Line 2620 | public class ForkJoinPool extends Abstra
2620      }
2621  
2622      /**
2623 +     * Returns the targeted parallelism level of the common pool.
2624 +     *
2625 +     * @return the targeted parallelism level of the common pool
2626 +     */
2627 +    public static int getCommonPoolParallelism() {
2628 +        return commonPoolParallelism;
2629 +    }
2630 +
2631 +    /**
2632       * Returns the number of worker threads that have started but not
2633       * yet terminated.  The result returned by this method may differ
2634       * from {@link #getParallelism} when threads are created to
# Line 2423 | 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 2553 | 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 2594 | Line 2880 | public class ForkJoinPool extends Abstra
2880      }
2881  
2882      /**
2883 <     * Initiates an orderly shutdown in which previously submitted
2884 <     * tasks are executed, but no new tasks will be accepted.
2885 <     * Invocation has no additional effect if already shut down.
2886 <     * Tasks that are in the process of being submitted concurrently
2887 <     * during the course of this method may or may not be rejected.
2883 >     * Possibly initiates an orderly shutdown in which previously
2884 >     * submitted tasks are executed, but no new tasks will be
2885 >     * accepted. Invocation has no effect on execution state if this
2886 >     * is the {@link #commonPool}, and no additional effect if
2887 >     * already shut down.  Tasks that are in the process of being
2888 >     * submitted concurrently during the course of this method may or
2889 >     * may not be rejected.
2890       *
2891       * @throws SecurityException if a security manager exists and
2892       *         the caller is not permitted to modify threads
# Line 2607 | Line 2895 | public class ForkJoinPool extends Abstra
2895       */
2896      public void shutdown() {
2897          checkPermission();
2898 <        tryTerminate(false, true);
2898 >        if (this != commonPool)
2899 >            tryTerminate(false, true);
2900      }
2901  
2902      /**
2903 <     * Attempts to cancel and/or stop all tasks, and reject all
2904 <     * subsequently submitted tasks.  Tasks that are in the process of
2905 <     * being submitted or executed concurrently during the course of
2906 <     * this method may or may not be rejected. This method cancels
2907 <     * both existing and unexecuted tasks, in order to permit
2908 <     * termination in the presence of task dependencies. So the method
2909 <     * always returns an empty list (unlike the case for some other
2910 <     * Executors).
2903 >     * Possibly attempts to cancel and/or stop all tasks, and reject
2904 >     * all subsequently submitted tasks.  Invocation has no effect on
2905 >     * execution state if this is the {@link #commonPool}, and no
2906 >     * additional effect if already shut down. Otherwise, tasks that
2907 >     * are in the process of being submitted or executed concurrently
2908 >     * during the course of this method may or may not be
2909 >     * rejected. This method cancels both existing and unexecuted
2910 >     * tasks, in order to permit termination in the presence of task
2911 >     * dependencies. So the method always returns an empty list
2912 >     * (unlike the case for some other Executors).
2913       *
2914       * @return an empty list
2915       * @throws SecurityException if a security manager exists and
# Line 2628 | Line 2919 | public class ForkJoinPool extends Abstra
2919       */
2920      public List<Runnable> shutdownNow() {
2921          checkPermission();
2922 <        tryTerminate(true, true);
2922 >        if (this != commonPool)
2923 >            tryTerminate(true, true);
2924          return Collections.emptyList();
2925      }
2926  
# Line 2685 | 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              }
2698        } finally {
2699            lock.unlock();
2993          }
2994 +        return terminated;
2995      }
2996  
2997      /**
# Line 2829 | 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 2844 | 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);
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 >            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 >        }
3186      }
3187  
3188      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines