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.4 by jsr166, Fri Oct 12 16:46:37 2012 UTC vs.
Revision 1.8 by dl, Mon Oct 29 17:23:26 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 813 | Line 788 | public class ForkJoinPool extends Abstra
788          }
789  
790          /**
791 +         * Version of tryUnpush for shared queues; called by non-FJ
792 +         * submitters after prechecking that task probably exists.
793 +         */
794 +        final boolean trySharedUnpush(ForkJoinTask<?> t) {
795 +            boolean success = false;
796 +            if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) {
797 +                try {
798 +                    ForkJoinTask<?>[] a; int s;
799 +                    if ((a = array) != null && (s = top) != base &&
800 +                        U.compareAndSwapObject
801 +                        (a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) {
802 +                        top = s;
803 +                        success = true;
804 +                    }
805 +                } finally {
806 +                    runState = 0;                         // unlock
807 +                }
808 +            }
809 +            return success;
810 +        }
811 +
812 +        /**
813           * Polls the given task only if it is at the current base.
814           */
815          final boolean pollFor(ForkJoinTask<?> task) {
# Line 1086 | Line 1083 | public class ForkJoinPool extends Abstra
1083      public static final ForkJoinWorkerThreadFactory
1084          defaultForkJoinWorkerThreadFactory;
1085  
1086 +
1087 +    /** Property prefix for constructing common pool */
1088 +    private static final String propPrefix =
1089 +        "java.util.concurrent.ForkJoinPool.common.";
1090 +
1091 +    /**
1092 +     * Common (static) pool. Non-null for public use unless a static
1093 +     * construction exception, but internal usages must null-check on
1094 +     * use.
1095 +     */
1096 +    static final ForkJoinPool commonPool;
1097 +
1098 +    /**
1099 +     * Common pool parallelism. Must equal commonPool.parallelism.
1100 +     */
1101 +    static final int commonPoolParallelism;
1102 +
1103      /**
1104       * Generator for assigning sequence numbers as pool names.
1105       */
# Line 1113 | Line 1127 | public class ForkJoinPool extends Abstra
1127      // static constants
1128  
1129      /**
1130 <     * The wakeup interval (in nanoseconds) for a worker waiting for a
1131 <     * task when the pool is quiescent to instead try to shrink the
1132 <     * number of workers.  The exact value does not matter too
1119 <     * much. It must be short enough to release resources during
1120 <     * sustained periods of idleness, but not so short that threads
1121 <     * are continually re-created.
1130 >     * Initial timeout value (in nanoseconds) for the thread triggering
1131 >     * quiescence to park waiting for new work. On timeout, the thread
1132 >     * will instead try to shrink the number of workers.
1133       */
1134 <    private static final long SHRINK_RATE =
1124 <        4L * 1000L * 1000L * 1000L; // 4 seconds
1134 >    private static final long IDLE_TIMEOUT      = 1000L * 1000L * 1000L; // 1sec
1135  
1136      /**
1137 <     * The timeout value for attempted shrinkage, includes
1128 <     * some slop to cope with system timer imprecision.
1137 >     * Timeout value when there are more threads than parallelism level
1138       */
1139 <    private static final long SHRINK_TIMEOUT = SHRINK_RATE - (SHRINK_RATE / 10);
1139 >    private static final long FAST_IDLE_TIMEOUT =  100L * 1000L * 1000L;
1140  
1141      /**
1142       * The maximum stolen->joining link depth allowed in method
# Line 1247 | Line 1256 | public class ForkJoinPool extends Abstra
1256       * empirically works OK on current JVMs.
1257       */
1258  
1259 +    volatile long stealCount;                  // collects worker counts
1260      volatile long ctl;                         // main pool control
1261      final int parallelism;                     // parallelism level
1262      final int localMode;                       // per-worker scheduling mode
1263 +    volatile int nextWorkerNumber;             // to create worker name string
1264      final int submitMask;                      // submit queue index bound
1265      int nextSeed;                              // for initializing worker seeds
1266 +    volatile int mainLock;                     // spinlock for array updates
1267      volatile int runState;                     // shutdown status and seq
1268      WorkQueue[] workQueues;                    // main registry
1257    final Mutex lock;                          // for registration
1258    final Condition termination;               // for awaitTermination
1269      final ForkJoinWorkerThreadFactory factory; // factory for new workers
1270      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1261    final AtomicLong stealCount;               // collect counts when terminated
1262    final AtomicInteger nextWorkerNumber;      // to create worker name string
1271      final String workerNamePrefix;             // to create worker name string
1272  
1273 +    /*
1274 +     * Mechanics for main lock protecting worker array updates.  Uses
1275 +     * the same strategy as ConcurrentHashMap bins -- a spinLock for
1276 +     * normal cases, but falling back to builtin lock when (rarely)
1277 +     * needed.  See internal ConcurrentHashMap documentation for
1278 +     * explanation.
1279 +     */
1280 +
1281 +    static final int LOCK_WAITING = 2; // bit to indicate need for signal
1282 +    static final int MAX_LOCK_SPINS = 1 << 8;
1283 +
1284 +    private void tryAwaitMainLock() {
1285 +        int spins = MAX_LOCK_SPINS, r = 0, h;
1286 +        while (((h = mainLock) & 1) != 0) {
1287 +            if (r == 0)
1288 +                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1289 +            else if (spins >= 0) {
1290 +                r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1291 +                if (r >= 0)
1292 +                    --spins;
1293 +            }
1294 +            else if (U.compareAndSwapInt(this, MAINLOCK, h, h | LOCK_WAITING)) {
1295 +                synchronized (this) {
1296 +                    if ((mainLock & LOCK_WAITING) != 0) {
1297 +                        try {
1298 +                            wait();
1299 +                        } catch (InterruptedException ie) {
1300 +                            Thread.currentThread().interrupt();
1301 +                        }
1302 +                    }
1303 +                    else
1304 +                        notifyAll(); // possibly won race vs signaller
1305 +                }
1306 +                break;
1307 +            }
1308 +        }
1309 +    }
1310 +
1311      //  Creating, registering, and deregistering workers
1312  
1313      /**
# Line 1288 | Line 1334 | public class ForkJoinPool extends Abstra
1334       * ForkJoinWorkerThread.
1335       */
1336      final String nextWorkerName() {
1337 <        return workerNamePrefix.concat
1338 <            (Integer.toString(nextWorkerNumber.addAndGet(1)));
1337 >        int n;
1338 >        do {} while(!U.compareAndSwapInt(this, NEXTWORKERNUMBER,
1339 >                                         n = nextWorkerNumber, ++n));
1340 >        return workerNamePrefix.concat(Integer.toString(n));
1341      }
1342  
1343      /**
# Line 1301 | Line 1349 | public class ForkJoinPool extends Abstra
1349       *
1350       * @param w the worker's queue
1351       */
1304
1352      final void registerWorker(WorkQueue w) {
1353 <        Mutex lock = this.lock;
1354 <        lock.lock();
1353 >        while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1354 >            tryAwaitMainLock();
1355          try {
1356 <            WorkQueue[] ws = workQueues;
1357 <            if (w != null && ws != null) {          // skip on shutdown/failure
1358 <                int rs, n = ws.length, m = n - 1;
1356 >            WorkQueue[] ws;
1357 >            if ((ws = workQueues) == null)
1358 >                ws = workQueues = new WorkQueue[submitMask + 1];
1359 >            if (w != null) {
1360 >                int rs, n =  ws.length, m = n - 1;
1361                  int s = nextSeed += SEED_INCREMENT; // rarely-colliding sequence
1362                  w.seed = (s == 0) ? 1 : s;          // ensure non-zero seed
1363                  int r = (s << 1) | 1;               // use odd-numbered indices
# Line 1328 | Line 1377 | public class ForkJoinPool extends Abstra
1377                  runState = ((rs = runState) & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN);
1378              }
1379          } finally {
1380 <            lock.unlock();
1380 >            if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1381 >                mainLock = 0;
1382 >                synchronized (this) { notifyAll(); };
1383 >            }
1384          }
1385 +
1386      }
1387  
1388      /**
# Line 1342 | Line 1395 | public class ForkJoinPool extends Abstra
1395       * @param ex the exception causing failure, or null if none
1396       */
1397      final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
1345        Mutex lock = this.lock;
1398          WorkQueue w = null;
1399          if (wt != null && (w = wt.workQueue) != null) {
1400              w.runState = -1;                // ensure runState is set
1401 <            stealCount.getAndAdd(w.totalSteals + w.nsteals);
1401 >            long steals = w.totalSteals + w.nsteals, sc;
1402 >            do {} while(!U.compareAndSwapLong(this, STEALCOUNT,
1403 >                                              sc = stealCount, sc + steals));
1404              int idx = w.poolIndex;
1405 <            lock.lock();
1406 <            try {                           // remove record from array
1405 >            while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1406 >                tryAwaitMainLock();
1407 >            try {
1408                  WorkQueue[] ws = workQueues;
1409                  if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w)
1410                      ws[idx] = null;
1411              } finally {
1412 <                lock.unlock();
1412 >                if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1413 >                    mainLock = 0;
1414 >                    synchronized (this) { notifyAll(); };
1415 >                }
1416              }
1417          }
1418  
# Line 1376 | Line 1434 | public class ForkJoinPool extends Abstra
1434              U.throwException(ex);
1435      }
1436  
1379
1437      // Submissions
1438  
1439      /**
# Line 1394 | Line 1451 | public class ForkJoinPool extends Abstra
1451          for (int r = s.seed, m = submitMask;;) {
1452              WorkQueue[] ws; WorkQueue q;
1453              int k = r & m & SQMASK;          // use only even indices
1454 <            if (runState < 0 || (ws = workQueues) == null || ws.length <= k)
1454 >            if (runState < 0)
1455                  throw new RejectedExecutionException(); // shutting down
1456 +            else if ((ws = workQueues) == null || ws.length <= k) {
1457 +                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1458 +                    tryAwaitMainLock();
1459 +                try {
1460 +                    if (workQueues == null)
1461 +                        workQueues = new WorkQueue[submitMask + 1];
1462 +                } finally {
1463 +                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1464 +                        mainLock = 0;
1465 +                        synchronized (this) { notifyAll(); };
1466 +                    }
1467 +                }
1468 +            }
1469              else if ((q = ws[k]) == null) {  // create new queue
1470                  WorkQueue nq = new WorkQueue(this, null, SHARED_QUEUE);
1471 <                Mutex lock = this.lock;      // construct outside lock
1472 <                lock.lock();
1473 <                try {                        // recheck under lock
1471 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
1472 >                    tryAwaitMainLock();
1473 >                try {
1474                      int rs = runState;       // to update seq
1475                      if (ws == workQueues && ws[k] == null) {
1476                          ws[k] = nq;
1477                          runState = ((rs & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN));
1478                      }
1479                  } finally {
1480 <                    lock.unlock();
1480 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
1481 >                        mainLock = 0;
1482 >                        synchronized (this) { notifyAll(); };
1483 >                    }
1484                  }
1485              }
1486              else if (q.trySharedPush(task)) {
# Line 1424 | Line 1497 | public class ForkJoinPool extends Abstra
1497          }
1498      }
1499  
1500 +    /**
1501 +     * Submits the given (non-null) task to the common pool, if possible.
1502 +     */
1503 +    static void submitToCommonPool(ForkJoinTask<?> task) {
1504 +        ForkJoinPool p;
1505 +        if ((p = commonPool) == null)
1506 +            throw new RejectedExecutionException("Common Pool Unavailable");
1507 +        p.doSubmit(task);
1508 +    }
1509 +
1510 +    /**
1511 +     * Returns true if the given task was submitted to common pool
1512 +     * and has not yet commenced execution, and is available for
1513 +     * removal according to execution policies; if so removing the
1514 +     * submission from the pool.
1515 +     *
1516 +     * @param task the task
1517 +     * @return true if successful
1518 +     */
1519 +    static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) {
1520 +        // Peek, looking for task and eligibility before
1521 +        // using trySharedUnpush to actually take it under lock
1522 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
1523 +        ForkJoinTask<?>[] a; int t, s, n;
1524 +        int k = submitters.get().seed & SQMASK;
1525 +        return ((p = commonPool) != null &&
1526 +                (ws = p.workQueues) != null &&
1527 +                ws.length > (k &= p.submitMask) &&
1528 +                (q = ws[k]) != null &&
1529 +                (a = q.array) != null &&
1530 +                (n = (t = q.top) - q.base) > 0 &&
1531 +                (n > 1 || (int)(p.ctl >> AC_SHIFT) < 0) &&
1532 +                (s = t - 1) >= 0 && s < a.length && a[s] == task &&
1533 +                q.trySharedUnpush(task));
1534 +    }
1535 +
1536      // Maintaining ctl counts
1537  
1538      /**
# Line 1435 | Line 1544 | public class ForkJoinPool extends Abstra
1544      }
1545  
1546      /**
1547 <     * Tries to activate or create a worker if too few are active.
1547 >     * Tries to create one or activate one or more workers if too few are active.
1548       */
1549      final void signalWork() {
1550          long c; int u;
# Line 1519 | Line 1628 | public class ForkJoinPool extends Abstra
1628       * awaiting signal,
1629       *
1630       * @param w the worker (via its WorkQueue)
1631 <     * @return a task or null of none found
1631 >     * @return a task or null if none found
1632       */
1633      private final ForkJoinTask<?> scan(WorkQueue w) {
1634          WorkQueue[] ws;                       // first update random seed
# Line 1536 | Line 1645 | public class ForkJoinPool extends Abstra
1645                      t = (ForkJoinTask<?>)U.getObjectVolatile(a, i);
1646                      if (q.base == b && ec >= 0 && t != null &&
1647                          U.compareAndSwapObject(a, i, t, null)) {
1648 <                        if (q.top - (q.base = b + 1) > 1)
1648 >                        if (q.top - (q.base = b + 1) > 0)
1649                              signalWork();    // help pushes signal
1650                          return t;
1651                      }
# Line 1582 | Line 1691 | public class ForkJoinPool extends Abstra
1691                  }
1692              }
1693              else if (w.eventCount < 0) {      // already queued
1694 <                if ((nr = w.rescans) > 0) {   // continue rescanning
1695 <                    int ac = a + parallelism;
1696 <                    if (((w.rescans = (ac < nr) ? ac : nr - 1) & 3) == 0)
1697 <                        Thread.yield();       // yield before block
1589 <                }
1590 <                else {
1694 >                int ac = a + parallelism;
1695 >                if ((nr = w.rescans) > 0)     // continue rescanning
1696 >                    w.rescans = (ac < nr) ? ac : nr - 1;
1697 >                else if (((w.seed >>> 16) & ac) == 0) { // randomize park
1698                      Thread.interrupted();     // clear status
1699                      Thread wt = Thread.currentThread();
1700                      U.putObject(wt, PARKBLOCKER, this);
# Line 1605 | Line 1712 | public class ForkJoinPool extends Abstra
1712      /**
1713       * If inactivating worker w has caused the pool to become
1714       * quiescent, checks for pool termination, and, so long as this is
1715 <     * not the only worker, waits for event for up to SHRINK_RATE
1716 <     * nanosecs.  On timeout, if ctl has not changed, terminates the
1715 >     * not the only worker, waits for event for up to a given
1716 >     * duration.  On timeout, if ctl has not changed, terminates the
1717       * worker, which will in turn wake up another worker to possibly
1718       * repeat this process.
1719       *
# Line 1617 | Line 1724 | public class ForkJoinPool extends Abstra
1724      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1725          if (w.eventCount < 0 && !tryTerminate(false, false) &&
1726              (int)prevCtl != 0 && !hasQueuedSubmissions() && ctl == currentCtl) {
1727 +            int dc = -(short)(currentCtl >>> TC_SHIFT);
1728 +            long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1729 +            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1730              Thread wt = Thread.currentThread();
1621            Thread.yield();            // yield before block
1731              while (ctl == currentCtl) {
1623                long startTime = System.nanoTime();
1732                  Thread.interrupted();  // timed variant of version in scan()
1733                  U.putObject(wt, PARKBLOCKER, this);
1734                  w.parker = wt;
1735                  if (ctl == currentCtl)
1736 <                    U.park(false, SHRINK_RATE);
1736 >                    U.park(false, parkTime);
1737                  w.parker = null;
1738                  U.putObject(wt, PARKBLOCKER, null);
1739                  if (ctl != currentCtl)
1740                      break;
1741 <                if (System.nanoTime() - startTime >= SHRINK_TIMEOUT &&
1741 >                if (deadline - System.nanoTime() <= 0L &&
1742                      U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) {
1743                      w.eventCount = (w.eventCount + E_SEQ) | E_MASK;
1744                      w.runState = -1;   // shrink
# Line 1915 | Line 2023 | public class ForkJoinPool extends Abstra
2023          }
2024      }
2025  
1918
2026      /**
2027       * Runs tasks until {@code isQuiescent()}. We piggyback on
2028       * active count ctl maintenance, but rather than blocking
# Line 1958 | Line 2065 | public class ForkJoinPool extends Abstra
2065      }
2066  
2067      /**
2068 +     * Restricted version of helpQuiescePool for non-FJ callers
2069 +     */
2070 +    static void externalHelpQuiescePool() {
2071 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q;
2072 +        ForkJoinTask<?> t; int b;
2073 +        int k = submitters.get().seed & SQMASK;
2074 +        if ((p = commonPool) != null &&
2075 +            (ws = p.workQueues) != null &&
2076 +            ws.length > (k &= p.submitMask) &&
2077 +            (w = ws[k]) != null &&
2078 +            (q = p.findNonEmptyStealQueue(w)) != null &&
2079 +            (b = q.base) - q.top < 0 &&
2080 +            (t = q.pollAt(b)) != null)
2081 +            t.doExec();
2082 +    }
2083 +
2084 +    /**
2085       * Gets and removes a local or stolen task for the given worker.
2086       *
2087       * @return a task, if available
# Line 1990 | Line 2114 | public class ForkJoinPool extends Abstra
2114                  8);
2115      }
2116  
2117 +    /**
2118 +     * Returns approximate submission queue length for the given caller
2119 +     */
2120 +    static int getEstimatedSubmitterQueueLength() {
2121 +        ForkJoinPool p; WorkQueue[] ws; WorkQueue q;
2122 +        int k = submitters.get().seed & SQMASK;
2123 +        return ((p = commonPool) != null &&
2124 +                p.runState >= 0 &&
2125 +                (ws = p.workQueues) != null &&
2126 +                ws.length > (k &= p.submitMask) &&
2127 +                (q = ws[k]) != null) ?
2128 +            q.queueSize() : 0;
2129 +    }
2130 +
2131      //  Termination
2132  
2133      /**
# Line 2007 | Line 2145 | public class ForkJoinPool extends Abstra
2145       * @return true if now terminating or terminated
2146       */
2147      private boolean tryTerminate(boolean now, boolean enable) {
2010        Mutex lock = this.lock;
2148          for (long c;;) {
2149              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2150                  if ((short)(c >>> TC_SHIFT) == -parallelism) {
2151 <                    lock.lock();                    // don't need try/finally
2152 <                    termination.signalAll();        // signal when 0 workers
2153 <                    lock.unlock();
2151 >                    synchronized(this) {
2152 >                        notifyAll();                // signal when 0 workers
2153 >                    }
2154                  }
2155                  return true;
2156              }
2157              if (runState >= 0) {                    // not yet enabled
2158                  if (!enable)
2159                      return false;
2160 <                lock.lock();
2161 <                runState |= SHUTDOWN;
2162 <                lock.unlock();
2160 >                while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1))
2161 >                    tryAwaitMainLock();
2162 >                try {
2163 >                    runState |= SHUTDOWN;
2164 >                } finally {
2165 >                    if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) {
2166 >                        mainLock = 0;
2167 >                        synchronized (this) { notifyAll(); };
2168 >                    }
2169 >                }
2170              }
2171              if (!now) {                             // check if idle & no tasks
2172                  if ((int)(c >> AC_SHIFT) != -parallelism ||
# Line 2155 | Line 2299 | public class ForkJoinPool extends Abstra
2299          // Use nearest power 2 for workQueues size. See Hackers Delight sec 3.2.
2300          int n = parallelism - 1;
2301          n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
2302 <        int size = (n + 1) << 1;        // #slots = 2*#workers
2159 <        this.submitMask = size - 1;     // room for max # of submit queues
2160 <        this.workQueues = new WorkQueue[size];
2161 <        this.termination = (this.lock = new Mutex()).newCondition();
2162 <        this.stealCount = new AtomicLong();
2163 <        this.nextWorkerNumber = new AtomicInteger();
2302 >        this.submitMask = ((n + 1) << 1) - 1;
2303          int pn = poolNumberGenerator.incrementAndGet();
2304          StringBuilder sb = new StringBuilder("ForkJoinPool-");
2305          sb.append(Integer.toString(pn));
2306          sb.append("-worker-");
2307          this.workerNamePrefix = sb.toString();
2169        lock.lock();
2308          this.runState = 1;              // set init flag
2309 <        lock.unlock();
2309 >    }
2310 >
2311 >    /**
2312 >     * Constructor for common pool, suitable only for static initialization.
2313 >     * Basically the same as above, but uses smallest possible initial footprint.
2314 >     */
2315 >    ForkJoinPool(int parallelism, int submitMask,
2316 >                 ForkJoinWorkerThreadFactory factory,
2317 >                 Thread.UncaughtExceptionHandler handler) {
2318 >        this.factory = factory;
2319 >        this.ueh = handler;
2320 >        this.submitMask = submitMask;
2321 >        this.parallelism = parallelism;
2322 >        long np = (long)(-parallelism);
2323 >        this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2324 >        this.localMode = LIFO_QUEUE;
2325 >        this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2326 >        this.runState = 1;
2327 >    }
2328 >
2329 >    /**
2330 >     * Returns the common pool instance.
2331 >     *
2332 >     * @return the common pool instance
2333 >     */
2334 >    public static ForkJoinPool commonPool() {
2335 >        ForkJoinPool p;
2336 >        if ((p = commonPool) == null)
2337 >            throw new Error("Common Pool Unavailable");
2338 >        return p;
2339      }
2340  
2341      // Execution methods
# Line 2344 | Line 2511 | public class ForkJoinPool extends Abstra
2511      }
2512  
2513      /**
2514 +     * Returns the targeted parallelism level of the common pool.
2515 +     *
2516 +     * @return the targeted parallelism level of the common pool
2517 +     */
2518 +    public static int getCommonPoolParallelism() {
2519 +        return commonPoolParallelism;
2520 +    }
2521 +
2522 +    /**
2523       * Returns the number of worker threads that have started but not
2524       * yet terminated.  The result returned by this method may differ
2525       * from {@link #getParallelism} when threads are created to
# Line 2424 | Line 2600 | public class ForkJoinPool extends Abstra
2600       * @return the number of steals
2601       */
2602      public long getStealCount() {
2603 <        long count = stealCount.get();
2603 >        long count = stealCount;
2604          WorkQueue[] ws; WorkQueue w;
2605          if ((ws = workQueues) != null) {
2606              for (int i = 1; i < ws.length; i += 2) {
# Line 2554 | Line 2730 | public class ForkJoinPool extends Abstra
2730      public String toString() {
2731          // Use a single pass through workQueues to collect counts
2732          long qt = 0L, qs = 0L; int rc = 0;
2733 <        long st = stealCount.get();
2733 >        long st = stealCount;
2734          long c = ctl;
2735          WorkQueue[] ws; WorkQueue w;
2736          if ((ws = workQueues) != null) {
# Line 2595 | Line 2771 | public class ForkJoinPool extends Abstra
2771      }
2772  
2773      /**
2774 <     * Initiates an orderly shutdown in which previously submitted
2775 <     * tasks are executed, but no new tasks will be accepted.
2776 <     * Invocation has no additional effect if already shut down.
2777 <     * Tasks that are in the process of being submitted concurrently
2778 <     * during the course of this method may or may not be rejected.
2774 >     * Possibly initiates an orderly shutdown in which previously
2775 >     * submitted tasks are executed, but no new tasks will be
2776 >     * accepted. Invocation has no effect on execution state if this
2777 >     * is the {@link #commonPool}, and no additional effect if
2778 >     * already shut down.  Tasks that are in the process of being
2779 >     * submitted concurrently during the course of this method may or
2780 >     * may not be rejected.
2781       *
2782       * @throws SecurityException if a security manager exists and
2783       *         the caller is not permitted to modify threads
# Line 2608 | Line 2786 | public class ForkJoinPool extends Abstra
2786       */
2787      public void shutdown() {
2788          checkPermission();
2789 <        tryTerminate(false, true);
2789 >        if (this != commonPool)
2790 >            tryTerminate(false, true);
2791      }
2792  
2793      /**
2794 <     * Attempts to cancel and/or stop all tasks, and reject all
2795 <     * subsequently submitted tasks.  Tasks that are in the process of
2796 <     * being submitted or executed concurrently during the course of
2797 <     * this method may or may not be rejected. This method cancels
2798 <     * both existing and unexecuted tasks, in order to permit
2799 <     * termination in the presence of task dependencies. So the method
2800 <     * always returns an empty list (unlike the case for some other
2801 <     * Executors).
2794 >     * Possibly attempts to cancel and/or stop all tasks, and reject
2795 >     * all subsequently submitted tasks.  Invocation has no effect on
2796 >     * execution state if this is the {@link #commonPool}, and no
2797 >     * additional effect if already shut down. Otherwise, tasks that
2798 >     * are in the process of being submitted or executed concurrently
2799 >     * during the course of this method may or may not be
2800 >     * rejected. This method cancels both existing and unexecuted
2801 >     * tasks, in order to permit termination in the presence of task
2802 >     * dependencies. So the method always returns an empty list
2803 >     * (unlike the case for some other Executors).
2804       *
2805       * @return an empty list
2806       * @throws SecurityException if a security manager exists and
# Line 2629 | Line 2810 | public class ForkJoinPool extends Abstra
2810       */
2811      public List<Runnable> shutdownNow() {
2812          checkPermission();
2813 <        tryTerminate(true, true);
2813 >        if (this != commonPool)
2814 >            tryTerminate(true, true);
2815          return Collections.emptyList();
2816      }
2817  
# Line 2686 | Line 2868 | public class ForkJoinPool extends Abstra
2868      public boolean awaitTermination(long timeout, TimeUnit unit)
2869          throws InterruptedException {
2870          long nanos = unit.toNanos(timeout);
2871 <        final Mutex lock = this.lock;
2872 <        lock.lock();
2873 <        try {
2874 <            for (;;) {
2875 <                if (isTerminated())
2876 <                    return true;
2877 <                if (nanos <= 0)
2878 <                    return false;
2879 <                nanos = termination.awaitNanos(nanos);
2871 >        if (isTerminated())
2872 >            return true;
2873 >        long startTime = System.nanoTime();
2874 >        boolean terminated = false;
2875 >        synchronized(this) {
2876 >            for (long waitTime = nanos, millis = 0L;;) {
2877 >                if (terminated = isTerminated() ||
2878 >                    waitTime <= 0L ||
2879 >                    (millis = unit.toMillis(waitTime)) <= 0L)
2880 >                    break;
2881 >                wait(millis);
2882 >                waitTime = nanos - (System.nanoTime() - startTime);
2883              }
2699        } finally {
2700            lock.unlock();
2884          }
2885 +        return terminated;
2886      }
2887  
2888      /**
# Line 2830 | Line 3014 | public class ForkJoinPool extends Abstra
3014      private static final long PARKBLOCKER;
3015      private static final int ABASE;
3016      private static final int ASHIFT;
3017 +    private static final long NEXTWORKERNUMBER;
3018 +    private static final long STEALCOUNT;
3019 +    private static final long MAINLOCK;
3020  
3021      static {
3022          poolNumberGenerator = new AtomicInteger();
# Line 2845 | Line 3032 | public class ForkJoinPool extends Abstra
3032              Class<?> ak = ForkJoinTask[].class;
3033              CTL = U.objectFieldOffset
3034                  (k.getDeclaredField("ctl"));
3035 +            NEXTWORKERNUMBER = U.objectFieldOffset
3036 +                (k.getDeclaredField("nextWorkerNumber"));
3037 +            STEALCOUNT = U.objectFieldOffset
3038 +                (k.getDeclaredField("stealCount"));
3039 +            MAINLOCK = U.objectFieldOffset
3040 +                (k.getDeclaredField("mainLock"));
3041              Class<?> tk = Thread.class;
3042              PARKBLOCKER = U.objectFieldOffset
3043                  (tk.getDeclaredField("parkBlocker"));
3044              ABASE = U.arrayBaseOffset(ak);
3045              s = U.arrayIndexScale(ak);
3046 +            ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3047          } catch (Exception e) {
3048              throw new Error(e);
3049          }
3050          if ((s & (s-1)) != 0)
3051              throw new Error("data type scale not a power of two");
3052 <        ASHIFT = 31 - Integer.numberOfLeadingZeros(s);
3052 >        try { // Establish common pool
3053 >            String pp = System.getProperty(propPrefix + "parallelism");
3054 >            String fp = System.getProperty(propPrefix + "threadFactory");
3055 >            String up = System.getProperty(propPrefix + "exceptionHandler");
3056 >            ForkJoinWorkerThreadFactory fac = (fp == null) ?
3057 >                defaultForkJoinWorkerThreadFactory :
3058 >                ((ForkJoinWorkerThreadFactory)ClassLoader.
3059 >                 getSystemClassLoader().loadClass(fp).newInstance());
3060 >            Thread.UncaughtExceptionHandler ueh = (up == null)? null :
3061 >                ((Thread.UncaughtExceptionHandler)ClassLoader.
3062 >                 getSystemClassLoader().loadClass(up).newInstance());
3063 >            int par;
3064 >            if ((pp == null || (par = Integer.parseInt(pp)) <= 0))
3065 >                par = Runtime.getRuntime().availableProcessors();
3066 >            if (par > MAX_CAP)
3067 >                par = MAX_CAP;
3068 >            commonPoolParallelism = par;
3069 >            int n = par - 1; // precompute submit mask
3070 >            n |= n >>> 1; n |= n >>> 2; n |= n >>> 4;
3071 >            n |= n >>> 8; n |= n >>> 16;
3072 >            int mask = ((n + 1) << 1) - 1;
3073 >            commonPool = new ForkJoinPool(par, mask, fac, ueh);
3074 >        } catch (Exception e) {
3075 >            throw new Error(e);
3076 >        }
3077      }
3078  
3079      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines