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

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.66 by dl, Sun Aug 29 23:34:46 2010 UTC vs.
Revision 1.79 by jsr166, Tue Sep 7 23:49:30 2010 UTC

# Line 6 | Line 6
6  
7   package jsr166y;
8  
9 import java.util.concurrent.*;
10
9   import java.util.ArrayList;
10   import java.util.Arrays;
11   import java.util.Collection;
12   import java.util.Collections;
13   import java.util.List;
14 + import java.util.concurrent.AbstractExecutorService;
15 + import java.util.concurrent.Callable;
16 + import java.util.concurrent.CountDownLatch;
17 + import java.util.concurrent.ExecutorService;
18 + import java.util.concurrent.Future;
19 + import java.util.concurrent.RejectedExecutionException;
20 + import java.util.concurrent.RunnableFuture;
21 + import java.util.concurrent.TimeUnit;
22 + import java.util.concurrent.TimeoutException;
23 + import java.util.concurrent.atomic.AtomicInteger;
24   import java.util.concurrent.locks.LockSupport;
25   import java.util.concurrent.locks.ReentrantLock;
18 import java.util.concurrent.atomic.AtomicInteger;
19 import java.util.concurrent.CountDownLatch;
26  
27   /**
28   * An {@link ExecutorService} for running {@link ForkJoinTask}s.
# Line 69 | Line 75 | import java.util.concurrent.CountDownLat
75   *    <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td>
76   *  </tr>
77   *  <tr>
78 < *    <td> <b>Arange async execution</td>
78 > *    <td> <b>Arrange async execution</td>
79   *    <td> {@link #execute(ForkJoinTask)}</td>
80   *    <td> {@link ForkJoinTask#fork}</td>
81   *  </tr>
# Line 140 | Line 146 | public class ForkJoinPool extends Abstra
146       * Beyond work-stealing support and essential bookkeeping, the
147       * main responsibility of this framework is to take actions when
148       * one worker is waiting to join a task stolen (or always held by)
149 <     * another.  Becauae we are multiplexing many tasks on to a pool
149 >     * another.  Because we are multiplexing many tasks on to a pool
150       * of workers, we can't just let them block (as in Thread.join).
151       * We also cannot just reassign the joiner's run-time stack with
152       * another and replace it later, which would be a form of
# Line 157 | Line 163 | public class ForkJoinPool extends Abstra
163       *      links to try to find such a task.
164       *
165       *   Compensating: Unless there are already enough live threads,
166 <     *      method helpMaintainParallelism() may create or or
166 >     *      method helpMaintainParallelism() may create or
167       *      re-activate a spare thread to compensate for blocked
168       *      joiners until they unblock.
169       *
# Line 226 | Line 232 | public class ForkJoinPool extends Abstra
232       * ManagedBlocker), we may create or resume others to take their
233       * place until they unblock (see below). Implementing this
234       * requires counts of the number of "running" threads (i.e., those
235 <     * that are neither blocked nor artifically suspended) as well as
235 >     * that are neither blocked nor artificially suspended) as well as
236       * the total number.  These two values are packed into one field,
237       * "workerCounts" because we need accurate snapshots when deciding
238       * to create, resume or suspend.  Note however that the
239 <     * correspondance of these counts to reality is not guaranteed. In
239 >     * correspondence of these counts to reality is not guaranteed. In
240       * particular updates for unblocked threads may lag until they
241       * actually wake up.
242       *
# Line 301 | Line 307 | public class ForkJoinPool extends Abstra
307       * about the same time as another is needlessly being created. We
308       * counteract this and related slop in part by requiring resumed
309       * spares to immediately recheck (in preStep) to see whether they
310 <     * they should re-suspend.
310 >     * should re-suspend.
311       *
312       * 6. Killing off unneeded workers. A timeout mechanism is used to
313       * shed unused workers: The oldest (first) event queue waiter uses
# Line 315 | Line 321 | public class ForkJoinPool extends Abstra
321       * 7. Deciding when to create new workers. The main dynamic
322       * control in this class is deciding when to create extra threads
323       * in method helpMaintainParallelism. We would like to keep
324 <     * exactly #parallelism threads running, which is an impossble
324 >     * exactly #parallelism threads running, which is an impossible
325       * task. We always need to create one when the number of running
326       * threads would become zero and all workers are busy. Beyond
327 <     * this, we must rely on heuristics that work well in the the
328 <     * presence of transients phenomena such as GC stalls, dynamic
327 >     * this, we must rely on heuristics that work well in the
328 >     * presence of transient phenomena such as GC stalls, dynamic
329       * compilation, and wake-up lags. These transients are extremely
330       * common -- we are normally trying to fully saturate the CPUs on
331       * a machine, so almost any activity other than running tasks
# Line 346 | Line 352 | public class ForkJoinPool extends Abstra
352       * "while ((local = field) != 0)") which are usually the simplest
353       * way to ensure the required read orderings (which are sometimes
354       * critical). Also several occurrences of the unusual "do {}
355 <     * while(!cas...)" which is the simplest way to force an update of
355 >     * while (!cas...)" which is the simplest way to force an update of
356       * a CAS'ed variable. There are also other coding oddities that
357       * help some methods perform reasonably even when interpreted (not
358       * compiled), at the expense of some messy constructions that
# Line 420 | Line 426 | public class ForkJoinPool extends Abstra
426      /**
427       * The time to block in a join (see awaitJoin) before checking if
428       * a new worker should be (re)started to maintain parallelism
429 <     * level. The value should be short enough to maintain gloabal
429 >     * level. The value should be short enough to maintain global
430       * responsiveness and progress but long enough to avoid
431       * counterproductive firings during GC stalls or unrelated system
432       * activity, and to not bog down systems with continual re-firings
# Line 430 | Line 436 | public class ForkJoinPool extends Abstra
436  
437      /**
438       * The wakeup interval (in nanoseconds) for the oldest worker
439 <     * worker waiting for an event invokes tryShutdownUnusedWorker to shrink
440 <     * the number of workers.  The exact value does not matter too
441 <     * much, but should be long enough to slowly release resources
442 <     * during long periods without use without disrupting normal use.
439 >     * waiting for an event to invoke tryShutdownUnusedWorker to
440 >     * shrink the number of workers.  The exact value does not matter
441 >     * too much. It must be short enough to release resources during
442 >     * sustained periods of idleness, but not so short that threads
443 >     * are continually re-created.
444       */
445      private static final long SHRINK_RATE_NANOS =
446          30L * 1000L * 1000L * 1000L; // 2 per minute
# Line 483 | Line 490 | public class ForkJoinPool extends Abstra
490      private volatile long stealCount;
491  
492      /**
493 <     * Encoded record of top of treiber stack of threads waiting for
493 >     * Encoded record of top of Treiber stack of threads waiting for
494       * events. The top 32 bits contain the count being waited for. The
495       * bottom 16 bits contains one plus the pool index of waiting
496       * worker thread. (Bits 16-31 are unused.)
# Line 502 | Line 509 | public class ForkJoinPool extends Abstra
509      private volatile int eventCount;
510  
511      /**
512 <     * Encoded record of top of treiber stack of spare threads waiting
512 >     * Encoded record of top of Treiber stack of spare threads waiting
513       * for resumption. The top 16 bits contain an arbitrary count to
514       * avoid ABA effects. The bottom 16bits contains one plus the pool
515       * index of waiting worker thread.
# Line 516 | Line 523 | public class ForkJoinPool extends Abstra
523       * Lifecycle control. The low word contains the number of workers
524       * that are (probably) executing tasks. This value is atomically
525       * incremented before a worker gets a task to run, and decremented
526 <     * when worker has no tasks and cannot find any.  Bits 16-18
526 >     * when a worker has no tasks and cannot find any.  Bits 16-18
527       * contain runLevel value. When all are zero, the pool is
528       * running. Level transitions are monotonic (running -> shutdown
529       * -> terminating -> terminated) so each transition adds a bit.
# Line 605 | Line 612 | public class ForkJoinPool extends Abstra
612       * (rarely) necessary when other count updates lag.
613       *
614       * @param dr -- either zero or ONE_RUNNING
615 <     * @param dt == either zero or ONE_TOTAL
615 >     * @param dt -- either zero or ONE_TOTAL
616       */
617      private void decrementWorkerCounts(int dr, int dt) {
618          for (;;) {
# Line 674 | Line 681 | public class ForkJoinPool extends Abstra
681      }
682  
683      /**
684 <     * Nulls out record of worker in workers array
684 >     * Nulls out record of worker in workers array.
685       */
686      private void forgetWorker(ForkJoinWorkerThread w) {
687          int idx = w.poolIndex;
688 <        // Locking helps method recordWorker avoid unecessary expansion
688 >        // Locking helps method recordWorker avoid unnecessary expansion
689          final ReentrantLock lock = this.workerLock;
690          lock.lock();
691          try {
# Line 693 | Line 700 | public class ForkJoinPool extends Abstra
700      /**
701       * Final callback from terminating worker.  Removes record of
702       * worker from array, and adjusts counts. If pool is shutting
703 <     * down, tries to complete terminatation.
703 >     * down, tries to complete termination.
704       *
705       * @param w the worker
706       */
# Line 807 | Line 814 | public class ForkJoinPool extends Abstra
814      // Maintaining parallelism
815  
816      /**
817 <     * Pushes worker onto the spare stack
817 >     * Pushes worker onto the spare stack.
818       */
819      final void pushSpare(ForkJoinWorkerThread w) {
820          int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex + 1);
# Line 832 | Line 839 | public class ForkJoinPool extends Abstra
839              UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
840                                       sw, w.nextSpare)) {
841              int c; // increment running count before resume
842 <            do {} while(!UNSAFE.compareAndSwapInt
843 <                        (this, workerCountsOffset,
844 <                         c = workerCounts, c + ONE_RUNNING));
842 >            do {} while (!UNSAFE.compareAndSwapInt
843 >                         (this, workerCountsOffset,
844 >                          c = workerCounts, c + ONE_RUNNING));
845              if (w.tryUnsuspend())
846                  LockSupport.unpark(w);
847              else   // back out if w was shutdown
# Line 846 | Line 853 | public class ForkJoinPool extends Abstra
853       * Tries to increase the number of running workers if below target
854       * parallelism: If a spare exists tries to resume it via
855       * tryResumeSpare.  Otherwise, if not enough total workers or all
856 <     * existing workers are busy, adds a new worker. In all casses also
856 >     * existing workers are busy, adds a new worker. In all cases also
857       * helps wake up releasable workers waiting for work.
858       */
859      private void helpMaintainParallelism() {
# Line 985 | Line 992 | public class ForkJoinPool extends Abstra
992                      w.lastEventCount = ec;     // no need to wait
993                      break;
994                  }
995 <                else if (!(inactivate |= active))  
995 >                else if (!(inactivate |= active))
996                      eventSync(w, wec);         // must inactivate before sync
997              }
998              else
# Line 1107 | Line 1114 | public class ForkJoinPool extends Abstra
1114                                       c = eventCount, c+1);
1115              eventWaiters = 0L; // clobber lists
1116              spareWaiters = 0;
1117 <            ForkJoinWorkerThread[] ws = workers;
1111 <            int n = ws.length;
1112 <            for (int i = 0; i < n; ++i) {
1113 <                ForkJoinWorkerThread w = ws[i];
1117 >            for (ForkJoinWorkerThread w : workers) {
1118                  if (w != null) {
1119                      w.shutdown();
1120                      if (passes > 0 && !w.isTerminated()) {
# Line 1129 | Line 1133 | public class ForkJoinPool extends Abstra
1133      }
1134  
1135      /**
1136 <     * Clear out and cancel submissions, ignoring exceptions
1136 >     * Clears out and cancels submissions, ignoring exceptions.
1137       */
1138      private void cancelSubmissions() {
1139          ForkJoinTask<?> task;
# Line 1144 | Line 1148 | public class ForkJoinPool extends Abstra
1148      // misc support for ForkJoinWorkerThread
1149  
1150      /**
1151 <     * Returns pool number
1151 >     * Returns pool number.
1152       */
1153      final int getPoolNumber() {
1154          return poolNumber;
1155      }
1156  
1157      /**
1158 <     * Tries to accumulates steal count from a worker, clearing
1159 <     * the worker's value.
1158 >     * Tries to accumulate steal count from a worker, clearing
1159 >     * the worker's value if successful.
1160       *
1161       * @return true if worker steal count now zero
1162       */
# Line 1174 | Line 1178 | public class ForkJoinPool extends Abstra
1178       */
1179      final int idlePerActive() {
1180          int pc = parallelism; // use parallelism, not rc
1181 <        int ac = runState;    // no mask -- artifically boosts during shutdown
1181 >        int ac = runState;    // no mask -- artificially boosts during shutdown
1182          // Use exact results for small values, saturate past 4
1183 <        return pc <= ac? 0 : pc >>> 1 <= ac? 1 : pc >>> 2 <= ac? 3 : pc >>> 3;
1183 >        return ((pc <= ac) ? 0 :
1184 >                (pc >>> 1 <= ac) ? 1 :
1185 >                (pc >>> 2 <= ac) ? 3 :
1186 >                pc >>> 3);
1187      }
1188  
1189      // Public and protected methods
# Line 1226 | Line 1233 | public class ForkJoinPool extends Abstra
1233       * use {@link #defaultForkJoinWorkerThreadFactory}.
1234       * @param handler the handler for internal worker threads that
1235       * terminate due to unrecoverable errors encountered while executing
1236 <     * tasks. For default value, use <code>null</code>.
1236 >     * tasks. For default value, use {@code null}.
1237       * @param asyncMode if true,
1238       * establishes local first-in-first-out scheduling mode for forked
1239       * tasks that are never joined. This mode may be more appropriate
1240       * than default locally stack-based mode in applications in which
1241       * worker threads only process event-style asynchronous tasks.
1242 <     * For default value, use <code>false</code>.
1242 >     * For default value, use {@code false}.
1243       * @throws IllegalArgumentException if parallelism less than or
1244       *         equal to zero, or greater than implementation limit
1245       * @throws NullPointerException if the factory is null
# Line 1442 | Line 1449 | public class ForkJoinPool extends Abstra
1449  
1450      /**
1451       * Returns the number of worker threads that have started but not
1452 <     * yet terminated.  This result returned by this method may differ
1452 >     * yet terminated.  The result returned by this method may differ
1453       * from {@link #getParallelism} when threads are created to
1454       * maintain parallelism when others are cooperatively blocked.
1455       *
# Line 1527 | Line 1534 | public class ForkJoinPool extends Abstra
1534       */
1535      public long getQueuedTaskCount() {
1536          long count = 0;
1537 <        ForkJoinWorkerThread[] ws = workers;
1531 <        int n = ws.length;
1532 <        for (int i = 0; i < n; ++i) {
1533 <            ForkJoinWorkerThread w = ws[i];
1537 >        for (ForkJoinWorkerThread w : workers)
1538              if (w != null)
1539                  count += w.getQueueSize();
1536        }
1540          return count;
1541      }
1542  
# Line 1588 | Line 1591 | public class ForkJoinPool extends Abstra
1591       */
1592      protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
1593          int count = submissionQueue.drainTo(c);
1594 <        ForkJoinWorkerThread[] ws = workers;
1592 <        int n = ws.length;
1593 <        for (int i = 0; i < n; ++i) {
1594 <            ForkJoinWorkerThread w = ws[i];
1594 >        for (ForkJoinWorkerThread w : workers)
1595              if (w != null)
1596                  count += w.drainTasksTo(c);
1597        }
1597          return count;
1598      }
1599  
# Line 1721 | Line 1720 | public class ForkJoinPool extends Abstra
1720          throws InterruptedException {
1721          try {
1722              return termination.awaitAdvanceInterruptibly(0, timeout, unit) > 0;
1723 <        } catch(TimeoutException ex) {
1723 >        } catch (TimeoutException ex) {
1724              return false;
1725          }
1726      }
# Line 1851 | Line 1850 | public class ForkJoinPool extends Abstra
1850      private static final long eventCountOffset =
1851          objectFieldOffset("eventCount", ForkJoinPool.class);
1852      private static final long eventWaitersOffset =
1853 <        objectFieldOffset("eventWaiters",ForkJoinPool.class);
1853 >        objectFieldOffset("eventWaiters", ForkJoinPool.class);
1854      private static final long stealCountOffset =
1855 <        objectFieldOffset("stealCount",ForkJoinPool.class);
1855 >        objectFieldOffset("stealCount", ForkJoinPool.class);
1856      private static final long spareWaitersOffset =
1857 <        objectFieldOffset("spareWaiters",ForkJoinPool.class);
1857 >        objectFieldOffset("spareWaiters", ForkJoinPool.class);
1858  
1859      private static long objectFieldOffset(String field, Class<?> klazz) {
1860          try {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines