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.2 by dl, Wed Jan 7 16:07:37 2009 UTC vs.
Revision 1.21 by jsr166, Fri Jul 24 23:47:01 2009 UTC

# Line 9 | Line 9 | import java.util.*;
9   import java.util.concurrent.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.atomic.*;
12 import sun.misc.Unsafe;
13 import java.lang.reflect.*;
12  
13   /**
14   * An {@link ExecutorService} for running {@link ForkJoinTask}s.  A
# Line 27 | Line 25 | import java.lang.reflect.*;
25   * (eventually blocking if none exist). This makes them efficient when
26   * most tasks spawn other subtasks (as do most ForkJoinTasks), as well
27   * as the mixed execution of some plain Runnable- or Callable- based
28 < * activities along with ForkJoinTasks. Otherwise, other
28 > * activities along with ForkJoinTasks. When setting
29 > * {@code setAsyncMode}, a ForkJoinPools may also be appropriate for
30 > * use with fine-grained tasks that are never joined. Otherwise, other
31   * ExecutorService implementations are typically more appropriate
32   * choices.
33   *
# Line 36 | Line 36 | import java.lang.reflect.*;
36   * adding, suspending, or resuming threads, even if some tasks are
37   * waiting to join others. However, no such adjustments are performed
38   * in the face of blocked IO or other unmanaged synchronization. The
39 < * nested <code>ManagedBlocker</code> interface enables extension of
39 > * nested {@code ManagedBlocker} interface enables extension of
40   * the kinds of synchronization accommodated.  The target parallelism
41 < * level may also be changed dynamically (<code>setParallelism</code>)
42 < * and dynamically thread construction can be limited using methods
43 < * <code>setMaximumPoolSize</code> and/or
44 < * <code>setMaintainsParallelism</code>.
41 > * level may also be changed dynamically ({@code setParallelism})
42 > * and thread construction can be limited using methods
43 > * {@code setMaximumPoolSize} and/or
44 > * {@code setMaintainsParallelism}.
45   *
46   * <p>In addition to execution and lifecycle control methods, this
47   * class provides status check methods (for example
48 < * <code>getStealCount</code>) that are intended to aid in developing,
48 > * {@code getStealCount}) that are intended to aid in developing,
49   * tuning, and monitoring fork/join applications. Also, method
50 < * <code>toString</code> returns indications of pool state in a
50 > * {@code toString} returns indications of pool state in a
51   * convenient form for informal monitoring.
52   *
53   * <p><b>Implementation notes</b>: This implementation restricts the
54   * maximum number of running threads to 32767. Attempts to create
55   * pools with greater than the maximum result in
56   * IllegalArgumentExceptions.
57 + *
58 + * @since 1.7
59 + * @author Doug Lea
60   */
61   public class ForkJoinPool extends AbstractExecutorService {
62  
# Line 79 | Line 82 | public class ForkJoinPool extends Abstra
82           * Returns a new worker thread operating in the given pool.
83           *
84           * @param pool the pool this thread works in
85 <         * @throws NullPointerException if pool is null;
85 >         * @throws NullPointerException if pool is null
86           */
87          public ForkJoinWorkerThread newThread(ForkJoinPool pool);
88      }
89  
90      /**
91 <     * Default ForkJoinWorkerThreadFactory implementation, creates a
91 >     * Default ForkJoinWorkerThreadFactory implementation; creates a
92       * new ForkJoinWorkerThread.
93       */
94      static class  DefaultForkJoinWorkerThreadFactory
# Line 131 | Line 134 | public class ForkJoinPool extends Abstra
134          new AtomicInteger();
135  
136      /**
137 <     * Array holding all worker threads in the pool. Array size must
138 <     * be a power of two.  Updates and replacements are protected by
139 <     * workerLock, but it is always kept in a consistent enough state
140 <     * to be randomly accessed without locking by workers performing
141 <     * work-stealing.
137 >     * Array holding all worker threads in the pool. Initialized upon
138 >     * first use. Array size must be a power of two.  Updates and
139 >     * replacements are protected by workerLock, but it is always kept
140 >     * in a consistent enough state to be randomly accessed without
141 >     * locking by workers performing work-stealing.
142       */
143      volatile ForkJoinWorkerThread[] workers;
144  
# Line 151 | Line 154 | public class ForkJoinPool extends Abstra
154  
155      /**
156       * The uncaught exception handler used when any worker
157 <     * abrupty terminates
157 >     * abruptly terminates
158       */
159      private Thread.UncaughtExceptionHandler ueh;
160  
# Line 179 | Line 182 | public class ForkJoinPool extends Abstra
182      private final LinkedTransferQueue<ForkJoinTask<?>> submissionQueue;
183  
184      /**
185 <     * Head of Treiber stack for barrier sync. See below for explanation
185 >     * Head of Treiber stack for barrier sync. See below for explanation.
186       */
187 <    private volatile WaitQueueNode barrierStack;
187 >    private volatile WaitQueueNode syncStack;
188  
189      /**
190       * The count for event barrier
# Line 204 | Line 207 | public class ForkJoinPool extends Abstra
207      private volatile int parallelism;
208  
209      /**
210 +     * True if use local fifo, not default lifo, for local polling
211 +     */
212 +    private volatile boolean locallyFifo;
213 +
214 +    /**
215       * Holds number of total (i.e., created and not yet terminated)
216       * and running (i.e., not blocked on joins or other managed sync)
217       * threads, packed into one int to ensure consistent snapshot when
218       * making decisions about creating and suspending spare
219       * threads. Updated only by CAS.  Note: CASes in
220 <     * updateRunningCount and preJoin running active count is in low
221 <     * word, so need to be modified if this changes
220 >     * updateRunningCount and preJoin assume that running active count
221 >     * is in low word, so need to be modified if this changes.
222       */
223      private volatile int workerCounts;
224  
# Line 219 | Line 227 | public class ForkJoinPool extends Abstra
227      private static int workerCountsFor(int t, int r) { return (t << 16) + r; }
228  
229      /**
230 <     * Add delta (which may be negative) to running count.  This must
230 >     * Adds delta (which may be negative) to running count.  This must
231       * be called before (with negative arg) and after (with positive)
232 <     * any managed synchronization (i.e., mainly, joins)
232 >     * any managed synchronization (i.e., mainly, joins).
233 >     *
234       * @param delta the number to add
235       */
236      final void updateRunningCount(int delta) {
237          int s;
238 <        do;while (!casWorkerCounts(s = workerCounts, s + delta));
238 >        do {} while (!casWorkerCounts(s = workerCounts, s + delta));
239      }
240  
241      /**
242 <     * Add delta (which may be negative) to both total and running
242 >     * Adds delta (which may be negative) to both total and running
243       * count.  This must be called upon creation and termination of
244       * worker threads.
245 +     *
246       * @param delta the number to add
247       */
248      private void updateWorkerCount(int delta) {
249          int d = delta + (delta << 16); // add to both lo and hi parts
250          int s;
251 <        do;while (!casWorkerCounts(s = workerCounts, s + d));
251 >        do {} while (!casWorkerCounts(s = workerCounts, s + d));
252      }
253  
254      /**
# Line 264 | Line 274 | public class ForkJoinPool extends Abstra
274      private static int runControlFor(int r, int a)   { return (r << 16) + a; }
275  
276      /**
277 <     * Increment active count. Called by workers before/during
278 <     * executing tasks.
277 >     * Tries incrementing active count; fails on contention.
278 >     * Called by workers before/during executing tasks.
279 >     *
280 >     * @return true on success
281       */
282 <    final void incrementActiveCount() {
283 <        int c;
284 <        do;while (!casRunControl(c = runControl, c+1));
282 >    final boolean tryIncrementActiveCount() {
283 >        int c = runControl;
284 >        return casRunControl(c, c+1);
285      }
286  
287      /**
288 <     * Decrement active count; possibly trigger termination.
288 >     * Tries decrementing active count; fails on contention.
289 >     * Possibly triggers termination on success.
290       * Called by workers when they can't find tasks.
291 +     *
292 +     * @return true on success
293       */
294 <    final void decrementActiveCount() {
295 <        int c, nextc;
296 <        do;while (!casRunControl(c = runControl, nextc = c-1));
294 >    final boolean tryDecrementActiveCount() {
295 >        int c = runControl;
296 >        int nextc = c - 1;
297 >        if (!casRunControl(c, nextc))
298 >            return false;
299          if (canTerminateOnShutdown(nextc))
300              terminateOnShutdown();
301 +        return true;
302      }
303  
304      /**
305 <     * Return true if argument represents zero active count and
305 >     * Returns true if argument represents zero active count and
306       * nonzero runstate, which is the triggering condition for
307       * terminating on shutdown.
308       */
309      private static boolean canTerminateOnShutdown(int c) {
310 <        return ((c & -c) >>> 16) != 0; // i.e. least bit is nonzero runState bit
310 >        // i.e. least bit is nonzero runState bit
311 >        return ((c & -c) >>> 16) != 0;
312      }
313  
314      /**
# Line 315 | Line 334 | public class ForkJoinPool extends Abstra
334  
335      /**
336       * Creates a ForkJoinPool with a pool size equal to the number of
337 <     * processors available on the system and using the default
338 <     * ForkJoinWorkerThreadFactory,
337 >     * processors available on the system, using the default
338 >     * ForkJoinWorkerThreadFactory.
339 >     *
340       * @throws SecurityException if a security manager exists and
341       *         the caller is not permitted to modify threads
342       *         because it does not hold {@link
343 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
343 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
344       */
345      public ForkJoinPool() {
346          this(Runtime.getRuntime().availableProcessors(),
# Line 328 | Line 348 | public class ForkJoinPool extends Abstra
348      }
349  
350      /**
351 <     * Creates a ForkJoinPool with the indicated parellelism level
352 <     * threads, and using the default ForkJoinWorkerThreadFactory,
351 >     * Creates a ForkJoinPool with the indicated parallelism level
352 >     * threads and using the default ForkJoinWorkerThreadFactory.
353 >     *
354       * @param parallelism the number of worker threads
355       * @throws IllegalArgumentException if parallelism less than or
356       * equal to zero
357       * @throws SecurityException if a security manager exists and
358       *         the caller is not permitted to modify threads
359       *         because it does not hold {@link
360 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
360 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
361       */
362      public ForkJoinPool(int parallelism) {
363          this(parallelism, defaultForkJoinWorkerThreadFactory);
# Line 345 | Line 366 | public class ForkJoinPool extends Abstra
366      /**
367       * Creates a ForkJoinPool with parallelism equal to the number of
368       * processors available on the system and using the given
369 <     * ForkJoinWorkerThreadFactory,
369 >     * ForkJoinWorkerThreadFactory.
370 >     *
371       * @param factory the factory for creating new threads
372       * @throws NullPointerException if factory is null
373       * @throws SecurityException if a security manager exists and
374       *         the caller is not permitted to modify threads
375       *         because it does not hold {@link
376 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
376 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
377       */
378      public ForkJoinPool(ForkJoinWorkerThreadFactory factory) {
379          this(Runtime.getRuntime().availableProcessors(), factory);
# Line 363 | Line 385 | public class ForkJoinPool extends Abstra
385       * @param parallelism the targeted number of worker threads
386       * @param factory the factory for creating new threads
387       * @throws IllegalArgumentException if parallelism less than or
388 <     * equal to zero, or greater than implementation limit.
388 >     * equal to zero, or greater than implementation limit
389       * @throws NullPointerException if factory is null
390       * @throws SecurityException if a security manager exists and
391       *         the caller is not permitted to modify threads
392       *         because it does not hold {@link
393 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
393 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
394       */
395      public ForkJoinPool(int parallelism, ForkJoinWorkerThreadFactory factory) {
396          if (parallelism <= 0 || parallelism > MAX_THREADS)
# Line 385 | Line 407 | public class ForkJoinPool extends Abstra
407          this.termination = workerLock.newCondition();
408          this.stealCount = new AtomicLong();
409          this.submissionQueue = new LinkedTransferQueue<ForkJoinTask<?>>();
410 <        createAndStartInitialWorkers(parallelism);
410 >        // worker array and workers are lazily constructed
411      }
412  
413      /**
414 <     * Create new worker using factory.
414 >     * Creates a new worker thread using factory.
415 >     *
416       * @param index the index to assign worker
417       * @return new worker, or null of factory failed
418       */
# Line 399 | Line 422 | public class ForkJoinPool extends Abstra
422          if (w != null) {
423              w.poolIndex = index;
424              w.setDaemon(true);
425 +            w.setAsyncMode(locallyFifo);
426              w.setName("ForkJoinPool-" + poolNumber + "-worker-" + index);
427              if (h != null)
428                  w.setUncaughtExceptionHandler(h);
# Line 407 | Line 431 | public class ForkJoinPool extends Abstra
431      }
432  
433      /**
434 <     * Return a good size for worker array given pool size.
434 >     * Returns a good size for worker array given pool size.
435       * Currently requires size to be a power of two.
436       */
437 <    private static int arraySizeFor(int ps) {
438 <        return ps <= 1? 1 : (1 << (32 - Integer.numberOfLeadingZeros(ps-1)));
437 >    private static int arraySizeFor(int poolSize) {
438 >        return (poolSize <= 1) ? 1 :
439 >            (1 << (32 - Integer.numberOfLeadingZeros(poolSize-1)));
440      }
441  
442      /**
443 <     * Create or resize array if necessary to hold newLength
443 >     * Creates or resizes array if necessary to hold newLength.
444 >     * Call only under exclusion.
445 >     *
446       * @return the array
447       */
448      private ForkJoinWorkerThread[] ensureWorkerArrayCapacity(int newLength) {
# Line 429 | Line 456 | public class ForkJoinPool extends Abstra
456      }
457  
458      /**
459 <     * Try to shrink workers into smaller array after one or more terminate
459 >     * Tries to shrink workers into smaller array after one or more terminate.
460       */
461      private void tryShrinkWorkerArray() {
462          ForkJoinWorkerThread[] ws = workers;
463 <        int len = ws.length;
464 <        int last = len - 1;
465 <        while (last >= 0 && ws[last] == null)
466 <            --last;
467 <        int newLength = arraySizeFor(last+1);
468 <        if (newLength < len)
469 <            workers = Arrays.copyOf(ws, newLength);
463 >        if (ws != null) {
464 >            int len = ws.length;
465 >            int last = len - 1;
466 >            while (last >= 0 && ws[last] == null)
467 >                --last;
468 >            int newLength = arraySizeFor(last+1);
469 >            if (newLength < len)
470 >                workers = Arrays.copyOf(ws, newLength);
471 >        }
472      }
473  
474      /**
475 <     * Initial worker array and worker creation and startup. (This
447 <     * must be done under lock to avoid interference by some of the
448 <     * newly started threads while creating others.)
475 >     * Initializes workers if necessary.
476       */
477 <    private void createAndStartInitialWorkers(int ps) {
478 <        final ReentrantLock lock = this.workerLock;
479 <        lock.lock();
480 <        try {
481 <            ForkJoinWorkerThread[] ws = ensureWorkerArrayCapacity(ps);
482 <            for (int i = 0; i < ps; ++i) {
483 <                ForkJoinWorkerThread w = createWorker(i);
484 <                if (w != null) {
485 <                    ws[i] = w;
486 <                    w.start();
487 <                    updateWorkerCount(1);
477 >    final void ensureWorkerInitialization() {
478 >        ForkJoinWorkerThread[] ws = workers;
479 >        if (ws == null) {
480 >            final ReentrantLock lock = this.workerLock;
481 >            lock.lock();
482 >            try {
483 >                ws = workers;
484 >                if (ws == null) {
485 >                    int ps = parallelism;
486 >                    ws = ensureWorkerArrayCapacity(ps);
487 >                    for (int i = 0; i < ps; ++i) {
488 >                        ForkJoinWorkerThread w = createWorker(i);
489 >                        if (w != null) {
490 >                            ws[i] = w;
491 >                            w.start();
492 >                            updateWorkerCount(1);
493 >                        }
494 >                    }
495                  }
496 +            } finally {
497 +                lock.unlock();
498              }
463        } finally {
464            lock.unlock();
499          }
500      }
501  
# Line 507 | Line 541 | public class ForkJoinPool extends Abstra
541      private <T> void doSubmit(ForkJoinTask<T> task) {
542          if (isShutdown())
543              throw new RejectedExecutionException();
544 +        if (workers == null)
545 +            ensureWorkerInitialization();
546          submissionQueue.offer(task);
547 <        signalIdleWorkers(true);
547 >        signalIdleWorkers();
548      }
549  
550      /**
551 <     * Performs the given task; returning its result upon completion
551 >     * Performs the given task, returning its result upon completion.
552 >     *
553       * @param task the task
554       * @return the task's result
555       * @throws NullPointerException if task is null
# Line 525 | Line 562 | public class ForkJoinPool extends Abstra
562  
563      /**
564       * Arranges for (asynchronous) execution of the given task.
565 +     *
566       * @param task the task
567       * @throws NullPointerException if task is null
568       * @throws RejectedExecutionException if pool is shut down
# Line 559 | Line 597 | public class ForkJoinPool extends Abstra
597  
598      /**
599       * Adaptor for Runnables. This implements RunnableFuture
600 <     * to be compliant with AbstractExecutorService constraints
600 >     * to be compliant with AbstractExecutorService constraints.
601       */
602      static final class AdaptedRunnable<T> extends ForkJoinTask<T>
603          implements RunnableFuture<T> {
# Line 579 | Line 617 | public class ForkJoinPool extends Abstra
617              return true;
618          }
619          public void run() { invoke(); }
620 +        private static final long serialVersionUID = 5232453952276885070L;
621      }
622  
623      /**
# Line 607 | Line 646 | public class ForkJoinPool extends Abstra
646              }
647          }
648          public void run() { invoke(); }
649 +        private static final long serialVersionUID = 2838392045355241008L;
650      }
651  
652      public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
653 <        ArrayList<ForkJoinTask<T>> ts =
653 >        ArrayList<ForkJoinTask<T>> forkJoinTasks =
654              new ArrayList<ForkJoinTask<T>>(tasks.size());
655 <        for (Callable<T> c : tasks)
656 <            ts.add(new AdaptedCallable<T>(c));
657 <        invoke(new InvokeAll<T>(ts));
658 <        return (List<Future<T>>)(List)ts;
655 >        for (Callable<T> task : tasks)
656 >            forkJoinTasks.add(new AdaptedCallable<T>(task));
657 >        invoke(new InvokeAll<T>(forkJoinTasks));
658 >
659 >        @SuppressWarnings({"unchecked", "rawtypes"})
660 >        List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks;
661 >        return futures;
662      }
663  
664      static final class InvokeAll<T> extends RecursiveAction {
665          final ArrayList<ForkJoinTask<T>> tasks;
666          InvokeAll(ArrayList<ForkJoinTask<T>> tasks) { this.tasks = tasks; }
667          public void compute() {
668 <            try { invokeAll(tasks); } catch(Exception ignore) {}
668 >            try { invokeAll(tasks); }
669 >            catch (Exception ignore) {}
670          }
671 +        private static final long serialVersionUID = -7914297376763021607L;
672      }
673  
674      // Configuration and status settings and queries
675  
676      /**
677 <     * Returns the factory used for constructing new workers
677 >     * Returns the factory used for constructing new workers.
678       *
679       * @return the factory used for constructing new workers
680       */
# Line 640 | Line 685 | public class ForkJoinPool extends Abstra
685      /**
686       * Returns the handler for internal worker threads that terminate
687       * due to unrecoverable errors encountered while executing tasks.
688 +     *
689       * @return the handler, or null if none
690       */
691      public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() {
# Line 665 | Line 711 | public class ForkJoinPool extends Abstra
711       * @throws SecurityException if a security manager exists and
712       *         the caller is not permitted to modify threads
713       *         because it does not hold {@link
714 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
714 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
715       */
716      public Thread.UncaughtExceptionHandler
717          setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler h) {
# Line 677 | Line 723 | public class ForkJoinPool extends Abstra
723              old = ueh;
724              ueh = h;
725              ForkJoinWorkerThread[] ws = workers;
726 <            for (int i = 0; i < ws.length; ++i) {
727 <                ForkJoinWorkerThread w = ws[i];
728 <                if (w != null)
729 <                    w.setUncaughtExceptionHandler(h);
726 >            if (ws != null) {
727 >                for (int i = 0; i < ws.length; ++i) {
728 >                    ForkJoinWorkerThread w = ws[i];
729 >                    if (w != null)
730 >                        w.setUncaughtExceptionHandler(h);
731 >                }
732              }
733          } finally {
734              lock.unlock();
# Line 690 | Line 738 | public class ForkJoinPool extends Abstra
738  
739  
740      /**
741 <     * Sets the target paralleism level of this pool.
741 >     * Sets the target parallelism level of this pool.
742 >     *
743       * @param parallelism the target parallelism
744       * @throws IllegalArgumentException if parallelism less than or
745 <     * equal to zero or greater than maximum size bounds.
745 >     * equal to zero or greater than maximum size bounds
746       * @throws SecurityException if a security manager exists and
747       *         the caller is not permitted to modify threads
748       *         because it does not hold {@link
749 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
749 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
750       */
751      public void setParallelism(int parallelism) {
752          checkPermission();
# Line 717 | Line 766 | public class ForkJoinPool extends Abstra
766          } finally {
767              lock.unlock();
768          }
769 <        signalIdleWorkers(false);
769 >        signalIdleWorkers();
770      }
771  
772      /**
# Line 732 | Line 781 | public class ForkJoinPool extends Abstra
781      /**
782       * Returns the number of worker threads that have started but not
783       * yet terminated.  This result returned by this method may differ
784 <     * from <code>getParallelism</code> when threads are created to
784 >     * from {@code getParallelism} when threads are created to
785       * maintain parallelism when others are cooperatively blocked.
786       *
787       * @return the number of worker threads
# Line 744 | Line 793 | public class ForkJoinPool extends Abstra
793      /**
794       * Returns the maximum number of threads allowed to exist in the
795       * pool, even if there are insufficient unblocked running threads.
796 +     *
797       * @return the maximum
798       */
799      public int getMaximumPoolSize() {
# Line 755 | Line 805 | public class ForkJoinPool extends Abstra
805       * pool, even if there are insufficient unblocked running threads.
806       * Setting this value has no effect on current pool size. It
807       * controls construction of new threads.
808 +     *
809       * @throws IllegalArgumentException if negative or greater then
810 <     * internal implementation limit.
810 >     * internal implementation limit
811       */
812      public void setMaximumPoolSize(int newMax) {
813          if (newMax < 0 || newMax > MAX_THREADS)
# Line 769 | Line 820 | public class ForkJoinPool extends Abstra
820       * Returns true if this pool dynamically maintains its target
821       * parallelism level. If false, new threads are added only to
822       * avoid possible starvation.
823 <     * This setting is by default true;
823 >     * This setting is by default true.
824 >     *
825       * @return true if maintains parallelism
826       */
827      public boolean getMaintainsParallelism() {
# Line 780 | Line 832 | public class ForkJoinPool extends Abstra
832       * Sets whether this pool dynamically maintains its target
833       * parallelism level. If false, new threads are added only to
834       * avoid possible starvation.
835 +     *
836       * @param enable true to maintains parallelism
837       */
838      public void setMaintainsParallelism(boolean enable) {
# Line 787 | Line 840 | public class ForkJoinPool extends Abstra
840      }
841  
842      /**
843 +     * Establishes local first-in-first-out scheduling mode for forked
844 +     * tasks that are never joined. This mode may be more appropriate
845 +     * than default locally stack-based mode in applications in which
846 +     * worker threads only process asynchronous tasks.  This method is
847 +     * designed to be invoked only when pool is quiescent, and
848 +     * typically only before any tasks are submitted. The effects of
849 +     * invocations at other times may be unpredictable.
850 +     *
851 +     * @param async if true, use locally FIFO scheduling
852 +     * @return the previous mode
853 +     */
854 +    public boolean setAsyncMode(boolean async) {
855 +        boolean oldMode = locallyFifo;
856 +        locallyFifo = async;
857 +        ForkJoinWorkerThread[] ws = workers;
858 +        if (ws != null) {
859 +            for (int i = 0; i < ws.length; ++i) {
860 +                ForkJoinWorkerThread t = ws[i];
861 +                if (t != null)
862 +                    t.setAsyncMode(async);
863 +            }
864 +        }
865 +        return oldMode;
866 +    }
867 +
868 +    /**
869 +     * Returns true if this pool uses local first-in-first-out
870 +     * scheduling mode for forked tasks that are never joined.
871 +     *
872 +     * @return true if this pool uses async mode
873 +     */
874 +    public boolean getAsyncMode() {
875 +        return locallyFifo;
876 +    }
877 +
878 +    /**
879       * Returns an estimate of the number of worker threads that are
880       * not blocked waiting to join tasks or for other managed
881       * synchronization.
# Line 801 | Line 890 | public class ForkJoinPool extends Abstra
890       * Returns an estimate of the number of threads that are currently
891       * stealing or executing tasks. This method may overestimate the
892       * number of active threads.
893 <     * @return the number of active threads.
893 >     *
894 >     * @return the number of active threads
895       */
896      public int getActiveThreadCount() {
897          return activeCountOf(runControl);
# Line 811 | Line 901 | public class ForkJoinPool extends Abstra
901       * Returns an estimate of the number of threads that are currently
902       * idle waiting for tasks. This method may underestimate the
903       * number of idle threads.
904 <     * @return the number of idle threads.
904 >     *
905 >     * @return the number of idle threads
906       */
907      final int getIdleThreadCount() {
908          int c = runningCountOf(workerCounts) - activeCountOf(runControl);
909 <        return (c <= 0)? 0 : c;
909 >        return (c <= 0) ? 0 : c;
910      }
911  
912      /**
913       * Returns true if all worker threads are currently idle. An idle
914       * worker is one that cannot obtain a task to execute because none
915       * are available to steal from other threads, and there are no
916 <     * pending submissions to the pool. This method is conservative:
917 <     * It might not return true immediately upon idleness of all
916 >     * pending submissions to the pool. This method is conservative;
917 >     * it might not return true immediately upon idleness of all
918       * threads, but will eventually become true if threads remain
919       * inactive.
920 +     *
921       * @return true if all threads are currently idle
922       */
923      public boolean isQuiescent() {
# Line 837 | Line 929 | public class ForkJoinPool extends Abstra
929       * one thread's work queue by another. The reported value
930       * underestimates the actual total number of steals when the pool
931       * is not quiescent. This value may be useful for monitoring and
932 <     * tuning fork/join programs: In general, steal counts should be
932 >     * tuning fork/join programs: in general, steal counts should be
933       * high enough to keep threads busy, but low enough to avoid
934       * overhead and contention across threads.
935 <     * @return the number of steals.
935 >     *
936 >     * @return the number of steals
937       */
938      public long getStealCount() {
939          return stealCount.get();
940      }
941  
942      /**
943 <     * Accumulate steal count from a worker. Call only
944 <     * when worker known to be idle.
943 >     * Accumulates steal count from a worker.
944 >     * Call only when worker known to be idle.
945       */
946      private void updateStealCount(ForkJoinWorkerThread w) {
947          int sc = w.getAndClearStealCount();
# Line 863 | Line 956 | public class ForkJoinPool extends Abstra
956       * an approximation, obtained by iterating across all threads in
957       * the pool. This method may be useful for tuning task
958       * granularities.
959 <     * @return the number of queued tasks.
959 >     *
960 >     * @return the number of queued tasks
961       */
962      public long getQueuedTaskCount() {
963          long count = 0;
964          ForkJoinWorkerThread[] ws = workers;
965 <        for (int i = 0; i < ws.length; ++i) {
966 <            ForkJoinWorkerThread t = ws[i];
967 <            if (t != null)
968 <                count += t.getQueueSize();
965 >        if (ws != null) {
966 >            for (int i = 0; i < ws.length; ++i) {
967 >                ForkJoinWorkerThread t = ws[i];
968 >                if (t != null)
969 >                    count += t.getQueueSize();
970 >            }
971          }
972          return count;
973      }
# Line 880 | Line 976 | public class ForkJoinPool extends Abstra
976       * Returns an estimate of the number tasks submitted to this pool
977       * that have not yet begun executing. This method takes time
978       * proportional to the number of submissions.
979 <     * @return the number of queued submissions.
979 >     *
980 >     * @return the number of queued submissions
981       */
982      public int getQueuedSubmissionCount() {
983          return submissionQueue.size();
# Line 889 | Line 986 | public class ForkJoinPool extends Abstra
986      /**
987       * Returns true if there are any tasks submitted to this pool
988       * that have not yet begun executing.
989 <     * @return <code>true</code> if there are any queued submissions.
989 >     *
990 >     * @return {@code true} if there are any queued submissions
991       */
992      public boolean hasQueuedSubmissions() {
993          return !submissionQueue.isEmpty();
# Line 899 | Line 997 | public class ForkJoinPool extends Abstra
997       * Removes and returns the next unexecuted submission if one is
998       * available.  This method may be useful in extensions to this
999       * class that re-assign work in systems with multiple pools.
1000 +     *
1001       * @return the next submission, or null if none
1002       */
1003      protected ForkJoinTask<?> pollSubmission() {
# Line 906 | Line 1005 | public class ForkJoinPool extends Abstra
1005      }
1006  
1007      /**
1008 +     * Removes all available unexecuted submitted and forked tasks
1009 +     * from scheduling queues and adds them to the given collection,
1010 +     * without altering their execution status. These may include
1011 +     * artificially generated or wrapped tasks. This method is designed
1012 +     * to be invoked only when the pool is known to be
1013 +     * quiescent. Invocations at other times may not remove all
1014 +     * tasks. A failure encountered while attempting to add elements
1015 +     * to collection {@code c} may result in elements being in
1016 +     * neither, either or both collections when the associated
1017 +     * exception is thrown.  The behavior of this operation is
1018 +     * undefined if the specified collection is modified while the
1019 +     * operation is in progress.
1020 +     *
1021 +     * @param c the collection to transfer elements into
1022 +     * @return the number of elements transferred
1023 +     */
1024 +    protected int drainTasksTo(Collection<ForkJoinTask<?>> c) {
1025 +        int n = submissionQueue.drainTo(c);
1026 +        ForkJoinWorkerThread[] ws = workers;
1027 +        if (ws != null) {
1028 +            for (int i = 0; i < ws.length; ++i) {
1029 +                ForkJoinWorkerThread w = ws[i];
1030 +                if (w != null)
1031 +                    n += w.drainTasksTo(c);
1032 +            }
1033 +        }
1034 +        return n;
1035 +    }
1036 +
1037 +    /**
1038       * Returns a string identifying this pool, as well as its state,
1039       * including indications of run state, parallelism level, and
1040       * worker and task counts.
# Line 949 | Line 1078 | public class ForkJoinPool extends Abstra
1078       * Invocation has no additional effect if already shut down.
1079       * Tasks that are in the process of being submitted concurrently
1080       * during the course of this method may or may not be rejected.
1081 +     *
1082       * @throws SecurityException if a security manager exists and
1083       *         the caller is not permitted to modify threads
1084       *         because it does not hold {@link
1085 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
1085 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
1086       */
1087      public void shutdown() {
1088          checkPermission();
# Line 966 | Line 1096 | public class ForkJoinPool extends Abstra
1096       * waiting tasks.  Tasks that are in the process of being
1097       * submitted or executed concurrently during the course of this
1098       * method may or may not be rejected. Unlike some other executors,
1099 <     * this method cancels rather than collects non-executed tasks,
1100 <     * so always returns an empty list.
1099 >     * this method cancels rather than collects non-executed tasks
1100 >     * upon termination, so always returns an empty list. However, you
1101 >     * can use method {@code drainTasksTo} before invoking this
1102 >     * method to transfer unexecuted tasks to another collection.
1103 >     *
1104       * @return an empty list
1105       * @throws SecurityException if a security manager exists and
1106       *         the caller is not permitted to modify threads
1107       *         because it does not hold {@link
1108 <     *         java.lang.RuntimePermission}<code>("modifyThread")</code>,
1108 >     *         java.lang.RuntimePermission}{@code ("modifyThread")}
1109       */
1110      public List<Runnable> shutdownNow() {
1111          checkPermission();
# Line 981 | Line 1114 | public class ForkJoinPool extends Abstra
1114      }
1115  
1116      /**
1117 <     * Returns <code>true</code> if all tasks have completed following shut down.
1117 >     * Returns {@code true} if all tasks have completed following shut down.
1118       *
1119 <     * @return <code>true</code> if all tasks have completed following shut down
1119 >     * @return {@code true} if all tasks have completed following shut down
1120       */
1121      public boolean isTerminated() {
1122          return runStateOf(runControl) == TERMINATED;
1123      }
1124  
1125      /**
1126 <     * Returns <code>true</code> if the process of termination has
1126 >     * Returns {@code true} if the process of termination has
1127       * commenced but possibly not yet completed.
1128       *
1129 <     * @return <code>true</code> if terminating
1129 >     * @return {@code true} if terminating
1130       */
1131      public boolean isTerminating() {
1132          return runStateOf(runControl) >= TERMINATING;
1133      }
1134  
1135      /**
1136 <     * Returns <code>true</code> if this pool has been shut down.
1136 >     * Returns {@code true} if this pool has been shut down.
1137       *
1138 <     * @return <code>true</code> if this pool has been shut down
1138 >     * @return {@code true} if this pool has been shut down
1139       */
1140      public boolean isShutdown() {
1141          return runStateOf(runControl) >= SHUTDOWN;
# Line 1015 | Line 1148 | public class ForkJoinPool extends Abstra
1148       *
1149       * @param timeout the maximum time to wait
1150       * @param unit the time unit of the timeout argument
1151 <     * @return <code>true</code> if this executor terminated and
1152 <     *         <code>false</code> if the timeout elapsed before termination
1151 >     * @return {@code true} if this executor terminated and
1152 >     *         {@code false} if the timeout elapsed before termination
1153       * @throws InterruptedException if interrupted while waiting
1154       */
1155      public boolean awaitTermination(long timeout, TimeUnit unit)
# Line 1040 | Line 1173 | public class ForkJoinPool extends Abstra
1173      // Shutdown and termination support
1174  
1175      /**
1176 <     * Callback from terminating worker. Null out the corresponding
1177 <     * workers slot, and if terminating, try to terminate, else try to
1178 <     * shrink workers array.
1176 >     * Callback from terminating worker. Nulls out the corresponding
1177 >     * workers slot, and if terminating, tries to terminate; else
1178 >     * tries to shrink workers array.
1179 >     *
1180       * @param w the worker
1181       */
1182      final void workerTerminated(ForkJoinWorkerThread w) {
# Line 1052 | Line 1186 | public class ForkJoinPool extends Abstra
1186          lock.lock();
1187          try {
1188              ForkJoinWorkerThread[] ws = workers;
1189 <            int idx = w.poolIndex;
1190 <            if (idx >= 0 && idx < ws.length && ws[idx] == w)
1191 <                ws[idx] = null;
1192 <            if (totalCountOf(workerCounts) == 0) {
1193 <                terminate(); // no-op if already terminating
1194 <                transitionRunStateTo(TERMINATED);
1195 <                termination.signalAll();
1196 <            }
1197 <            else if (!isTerminating()) {
1198 <                tryShrinkWorkerArray();
1199 <                tryResumeSpare(true); // allow replacement
1189 >            if (ws != null) {
1190 >                int idx = w.poolIndex;
1191 >                if (idx >= 0 && idx < ws.length && ws[idx] == w)
1192 >                    ws[idx] = null;
1193 >                if (totalCountOf(workerCounts) == 0) {
1194 >                    terminate(); // no-op if already terminating
1195 >                    transitionRunStateTo(TERMINATED);
1196 >                    termination.signalAll();
1197 >                }
1198 >                else if (!isTerminating()) {
1199 >                    tryShrinkWorkerArray();
1200 >                    tryResumeSpare(true); // allow replacement
1201 >                }
1202              }
1203          } finally {
1204              lock.unlock();
1205          }
1206 <        signalIdleWorkers(false);
1206 >        signalIdleWorkers();
1207      }
1208  
1209      /**
1210 <     * Initiate termination.
1210 >     * Initiates termination.
1211       */
1212      private void terminate() {
1213          if (transitionRunStateTo(TERMINATING)) {
1214              stopAllWorkers();
1215              resumeAllSpares();
1216 <            signalIdleWorkers(true);
1216 >            signalIdleWorkers();
1217              cancelQueuedSubmissions();
1218              cancelQueuedWorkerTasks();
1219              interruptUnterminatedWorkers();
1220 <            signalIdleWorkers(true); // resignal after interrupt
1220 >            signalIdleWorkers(); // resignal after interrupt
1221          }
1222      }
1223  
1224      /**
1225 <     * Possibly terminate when on shutdown state
1225 >     * Possibly terminates when on shutdown state.
1226       */
1227      private void terminateOnShutdown() {
1228          if (!hasQueuedSubmissions() && canTerminateOnShutdown(runControl))
# Line 1094 | Line 1230 | public class ForkJoinPool extends Abstra
1230      }
1231  
1232      /**
1233 <     * Clear out and cancel submissions
1233 >     * Clears out and cancels submissions.
1234       */
1235      private void cancelQueuedSubmissions() {
1236          ForkJoinTask<?> task;
# Line 1103 | Line 1239 | public class ForkJoinPool extends Abstra
1239      }
1240  
1241      /**
1242 <     * Clean out worker queues.
1242 >     * Cleans out worker queues.
1243       */
1244      private void cancelQueuedWorkerTasks() {
1245          final ReentrantLock lock = this.workerLock;
1246          lock.lock();
1247          try {
1248              ForkJoinWorkerThread[] ws = workers;
1249 <            for (int i = 0; i < ws.length; ++i) {
1250 <                ForkJoinWorkerThread t = ws[i];
1251 <                if (t != null)
1252 <                    t.cancelTasks();
1249 >            if (ws != null) {
1250 >                for (int i = 0; i < ws.length; ++i) {
1251 >                    ForkJoinWorkerThread t = ws[i];
1252 >                    if (t != null)
1253 >                        t.cancelTasks();
1254 >                }
1255              }
1256          } finally {
1257              lock.unlock();
# Line 1121 | Line 1259 | public class ForkJoinPool extends Abstra
1259      }
1260  
1261      /**
1262 <     * Set each worker's status to terminating. Requires lock to avoid
1263 <     * conflicts with add/remove
1262 >     * Sets each worker's status to terminating. Requires lock to avoid
1263 >     * conflicts with add/remove.
1264       */
1265      private void stopAllWorkers() {
1266          final ReentrantLock lock = this.workerLock;
1267          lock.lock();
1268          try {
1269              ForkJoinWorkerThread[] ws = workers;
1270 <            for (int i = 0; i < ws.length; ++i) {
1271 <                ForkJoinWorkerThread t = ws[i];
1272 <                if (t != null)
1273 <                    t.shutdownNow();
1270 >            if (ws != null) {
1271 >                for (int i = 0; i < ws.length; ++i) {
1272 >                    ForkJoinWorkerThread t = ws[i];
1273 >                    if (t != null)
1274 >                        t.shutdownNow();
1275 >                }
1276              }
1277          } finally {
1278              lock.unlock();
# Line 1140 | Line 1280 | public class ForkJoinPool extends Abstra
1280      }
1281  
1282      /**
1283 <     * Interrupt all unterminated workers.  This is not required for
1283 >     * Interrupts all unterminated workers.  This is not required for
1284       * sake of internal control, but may help unstick user code during
1285       * shutdown.
1286       */
# Line 1149 | Line 1289 | public class ForkJoinPool extends Abstra
1289          lock.lock();
1290          try {
1291              ForkJoinWorkerThread[] ws = workers;
1292 <            for (int i = 0; i < ws.length; ++i) {
1293 <                ForkJoinWorkerThread t = ws[i];
1294 <                if (t != null && !t.isTerminated()) {
1295 <                    try {
1296 <                        t.interrupt();
1297 <                    } catch (SecurityException ignore) {
1292 >            if (ws != null) {
1293 >                for (int i = 0; i < ws.length; ++i) {
1294 >                    ForkJoinWorkerThread t = ws[i];
1295 >                    if (t != null && !t.isTerminated()) {
1296 >                        try {
1297 >                            t.interrupt();
1298 >                        } catch (SecurityException ignore) {
1299 >                        }
1300                      }
1301                  }
1302              }
# Line 1165 | Line 1307 | public class ForkJoinPool extends Abstra
1307  
1308  
1309      /*
1310 <     * Nodes for event barrier to manage idle threads.
1310 >     * Nodes for event barrier to manage idle threads.  Queue nodes
1311 >     * are basic Treiber stack nodes, also used for spare stack.
1312       *
1313       * The event barrier has an event count and a wait queue (actually
1314       * a Treiber stack).  Workers are enabled to look for work when
1315 <     * the eventCount is incremented. If they fail to find some,
1316 <     * they may wait for next count. Synchronization events occur only
1317 <     * in enough contexts to maintain overall liveness:
1315 >     * the eventCount is incremented. If they fail to find work, they
1316 >     * may wait for next count. Upon release, threads help others wake
1317 >     * up.
1318 >     *
1319 >     * Synchronization events occur only in enough contexts to
1320 >     * maintain overall liveness:
1321       *
1322       *   - Submission of a new task to the pool
1323 <     *   - Creation or termination of a worker
1323 >     *   - Resizes or other changes to the workers array
1324       *   - pool termination
1325       *   - A worker pushing a task on an empty queue
1326       *
1327 <     * The last case (pushing a task) occurs often enough, and is
1328 <     * heavy enough compared to simple stack pushes to require some
1329 <     * special handling: Method signalNonEmptyWorkerQueue returns
1330 <     * without advancing count if the queue appears to be empty.  This
1331 <     * would ordinarily result in races causing some queued waiters
1332 <     * not to be woken up. To avoid this, a worker in sync
1333 <     * rescans for tasks after being enqueued if it was the first to
1334 <     * enqueue, and aborts the wait if finding one, also helping to
1335 <     * signal others. This works well because the worker has nothing
1336 <     * better to do anyway, and so might as well help alleviate the
1337 <     * overhead and contention on the threads actually doing work.
1338 <     *
1339 <     * Queue nodes are basic Treiber stack nodes, also used for spare
1340 <     * stack.
1327 >     * The case of pushing a task occurs often enough, and is heavy
1328 >     * enough compared to simple stack pushes, to require special
1329 >     * handling: Method signalWork returns without advancing count if
1330 >     * the queue appears to be empty.  This would ordinarily result in
1331 >     * races causing some queued waiters not to be woken up. To avoid
1332 >     * this, the first worker enqueued in method sync (see
1333 >     * syncIsReleasable) rescans for tasks after being enqueued, and
1334 >     * helps signal if any are found. This works well because the
1335 >     * worker has nothing better to do, and so might as well help
1336 >     * alleviate the overhead and contention on the threads actually
1337 >     * doing work.  Also, since event counts increments on task
1338 >     * availability exist to maintain liveness (rather than to force
1339 >     * refreshes etc), it is OK for callers to exit early if
1340 >     * contending with another signaller.
1341       */
1342      static final class WaitQueueNode {
1343          WaitQueueNode next; // only written before enqueued
1344          volatile ForkJoinWorkerThread thread; // nulled to cancel wait
1345          final long count; // unused for spare stack
1346 <        WaitQueueNode(ForkJoinWorkerThread w, long c) {
1346 >
1347 >        WaitQueueNode(long c, ForkJoinWorkerThread w) {
1348              count = c;
1349              thread = w;
1350          }
1351 <        final boolean signal() {
1351 >
1352 >        /**
1353 >         * Wakes up waiter, returning false if known to already
1354 >         */
1355 >        boolean signal() {
1356              ForkJoinWorkerThread t = thread;
1357 +            if (t == null)
1358 +                return false;
1359              thread = null;
1360 <            if (t != null) {
1361 <                LockSupport.unpark(t);
1362 <                return true;
1360 >            LockSupport.unpark(t);
1361 >            return true;
1362 >        }
1363 >
1364 >        /**
1365 >         * Awaits release on sync.
1366 >         */
1367 >        void awaitSyncRelease(ForkJoinPool p) {
1368 >            while (thread != null && !p.syncIsReleasable(this))
1369 >                LockSupport.park(this);
1370 >        }
1371 >
1372 >        /**
1373 >         * Awaits resumption as spare.
1374 >         */
1375 >        void awaitSpareRelease() {
1376 >            while (thread != null) {
1377 >                if (!Thread.interrupted())
1378 >                    LockSupport.park(this);
1379              }
1211            return false;
1380          }
1381      }
1382  
1383      /**
1384 <     * Release at least one thread waiting for event count to advance,
1385 <     * if one exists. If initial attempt fails, release all threads.
1386 <     * @param all if false, at first try to only release one thread
1387 <     * @return current event
1384 >     * Ensures that no thread is waiting for count to advance from the
1385 >     * current value of eventCount read on entry to this method, by
1386 >     * releasing waiting threads if necessary.
1387 >     *
1388 >     * @return the count
1389       */
1390 <    private long releaseIdleWorkers(boolean all) {
1391 <        long c;
1392 <        for (;;) {
1393 <            WaitQueueNode q = barrierStack;
1394 <            c = eventCount;
1226 <            long qc;
1227 <            if (q == null || (qc = q.count) >= c)
1228 <                break;
1229 <            if (!all) {
1230 <                if (casBarrierStack(q, q.next) && q.signal())
1231 <                    break;
1232 <                all = true;
1233 <            }
1234 <            else if (casBarrierStack(q, null)) {
1390 >    final long ensureSync() {
1391 >        long c = eventCount;
1392 >        WaitQueueNode q;
1393 >        while ((q = syncStack) != null && q.count < c) {
1394 >            if (casBarrierStack(q, null)) {
1395                  do {
1396 <                 q.signal();
1396 >                    q.signal();
1397                  } while ((q = q.next) != null);
1398                  break;
1399              }
# Line 1242 | Line 1402 | public class ForkJoinPool extends Abstra
1402      }
1403  
1404      /**
1405 <     * Returns current barrier event count
1246 <     * @return current barrier event count
1247 <     */
1248 <    final long getEventCount() {
1249 <        long ec = eventCount;
1250 <        releaseIdleWorkers(true); // release to ensure accurate result
1251 <        return ec;
1252 <    }
1253 <
1254 <    /**
1255 <     * Increment event count and release at least one waiting thread,
1256 <     * if one exists (released threads will in turn wake up others).
1257 <     * @param all if true, try to wake up all
1405 >     * Increments event count and releases waiting threads.
1406       */
1407 <    final void signalIdleWorkers(boolean all) {
1407 >    private void signalIdleWorkers() {
1408          long c;
1409 <        do;while (!casEventCount(c = eventCount, c+1));
1410 <        releaseIdleWorkers(all);
1409 >        do {} while (!casEventCount(c = eventCount, c+1));
1410 >        ensureSync();
1411      }
1412  
1413      /**
1414 <     * Wake up threads waiting to steal a task. Because method
1415 <     * sync rechecks availability, it is OK to only proceed if
1416 <     * queue appears to be non-empty.
1414 >     * Signals threads waiting to poll a task. Because method sync
1415 >     * rechecks availability, it is OK to only proceed if queue
1416 >     * appears to be non-empty, and OK to skip under contention to
1417 >     * increment count (since some other thread succeeded).
1418       */
1419 <    final void signalNonEmptyWorkerQueue() {
1271 <        // If CAS fails another signaller must have succeeded
1419 >    final void signalWork() {
1420          long c;
1421 <        if (barrierStack != null && casEventCount(c = eventCount, c+1))
1422 <            releaseIdleWorkers(false);
1421 >        WaitQueueNode q;
1422 >        if (syncStack != null &&
1423 >            casEventCount(c = eventCount, c+1) &&
1424 >            (((q = syncStack) != null && q.count <= c) &&
1425 >             (!casBarrierStack(q, q.next) || !q.signal())))
1426 >            ensureSync();
1427      }
1428  
1429      /**
1430 <     * Waits until event count advances from count, or some thread is
1431 <     * waiting on a previous count, or there is stealable work
1432 <     * available. Help wake up others on release.
1430 >     * Waits until event count advances from last value held by
1431 >     * caller, or if excess threads, caller is resumed as spare, or
1432 >     * caller or pool is terminating. Updates caller's event on exit.
1433 >     *
1434       * @param w the calling worker thread
1282     * @param prev previous value returned by sync (or 0)
1283     * @return current event count
1435       */
1436 <    final long sync(ForkJoinWorkerThread w, long prev) {
1437 <        updateStealCount(w);
1436 >    final void sync(ForkJoinWorkerThread w) {
1437 >        updateStealCount(w); // Transfer w's count while it is idle
1438  
1439 <        while (!w.isShutdown() && !isTerminating() &&
1440 <               (parallelism >= runningCountOf(workerCounts) ||
1290 <                !suspendIfSpare(w))) { // prefer suspend to waiting here
1439 >        while (!w.isShutdown() && !isTerminating() && !suspendIfSpare(w)) {
1440 >            long prev = w.lastEventCount;
1441              WaitQueueNode node = null;
1442 <            boolean queued = false;
1443 <            for (;;) {
1444 <                if (!queued) {
1445 <                    if (eventCount != prev)
1446 <                        break;
1447 <                    WaitQueueNode h = barrierStack;
1448 <                    if (h != null && h.count != prev)
1299 <                        break; // release below and maybe retry
1300 <                    if (node == null)
1301 <                        node = new WaitQueueNode(w, prev);
1302 <                    queued = casBarrierStack(node.next = h, node);
1303 <                }
1304 <                else if (Thread.interrupted() ||
1305 <                         node.thread == null ||
1306 <                         (node.next == null && w.prescan()) ||
1307 <                         eventCount != prev) {
1308 <                    node.thread = null;
1309 <                    if (eventCount == prev) // help trigger
1310 <                        casEventCount(prev, prev+1);
1442 >            WaitQueueNode h;
1443 >            while (eventCount == prev &&
1444 >                   ((h = syncStack) == null || h.count == prev)) {
1445 >                if (node == null)
1446 >                    node = new WaitQueueNode(prev, w);
1447 >                if (casBarrierStack(node.next = h, node)) {
1448 >                    node.awaitSyncRelease(this);
1449                      break;
1450                  }
1313                else
1314                    LockSupport.park(this);
1451              }
1452 +            long ec = ensureSync();
1453 +            if (ec != prev) {
1454 +                w.lastEventCount = ec;
1455 +                break;
1456 +            }
1457 +        }
1458 +    }
1459 +
1460 +    /**
1461 +     * Returns true if worker waiting on sync can proceed:
1462 +     *  - on signal (thread == null)
1463 +     *  - on event count advance (winning race to notify vs signaller)
1464 +     *  - on interrupt
1465 +     *  - if the first queued node, we find work available
1466 +     * If node was not signalled and event count not advanced on exit,
1467 +     * then we also help advance event count.
1468 +     *
1469 +     * @return true if node can be released
1470 +     */
1471 +    final boolean syncIsReleasable(WaitQueueNode node) {
1472 +        long prev = node.count;
1473 +        if (!Thread.interrupted() && node.thread != null &&
1474 +            (node.next != null ||
1475 +             !ForkJoinWorkerThread.hasQueuedTasks(workers)) &&
1476 +            eventCount == prev)
1477 +            return false;
1478 +        if (node.thread != null) {
1479 +            node.thread = null;
1480              long ec = eventCount;
1481 <            if (releaseIdleWorkers(false) != prev)
1482 <                return ec;
1481 >            if (prev <= ec) // help signal
1482 >                casEventCount(ec, ec+1);
1483          }
1484 <        return prev; // return old count if aborted
1484 >        return true;
1485 >    }
1486 >
1487 >    /**
1488 >     * Returns true if a new sync event occurred since last call to
1489 >     * sync or this method, if so, updating caller's count.
1490 >     */
1491 >    final boolean hasNewSyncEvent(ForkJoinWorkerThread w) {
1492 >        long lc = w.lastEventCount;
1493 >        long ec = ensureSync();
1494 >        if (ec == lc)
1495 >            return false;
1496 >        w.lastEventCount = ec;
1497 >        return true;
1498      }
1499  
1500      //  Parallelism maintenance
1501  
1502      /**
1503 <     * Decrement running count; if too low, add spare.
1503 >     * Decrements running count; if too low, adds spare.
1504       *
1505       * Conceptually, all we need to do here is add or resume a
1506       * spare thread when one is about to block (and remove or
1507       * suspend it later when unblocked -- see suspendIfSpare).
1508       * However, implementing this idea requires coping with
1509 <     * several problems: We have imperfect information about the
1509 >     * several problems: we have imperfect information about the
1510       * states of threads. Some count updates can and usually do
1511       * lag run state changes, despite arrangements to keep them
1512       * accurate (for example, when possible, updating counts
# Line 1343 | Line 1520 | public class ForkJoinPool extends Abstra
1520       * only be suspended or removed when they are idle, not
1521       * immediately when they aren't needed. So adding threads will
1522       * raise parallelism level for longer than necessary.  Also,
1523 <     * FJ applications often enounter highly transient peaks when
1523 >     * FJ applications often encounter highly transient peaks when
1524       * many threads are blocked joining, but for less time than it
1525       * takes to create or resume spares.
1526       *
# Line 1352 | Line 1529 | public class ForkJoinPool extends Abstra
1529       * target counts, else create only to avoid starvation
1530       * @return true if joinMe known to be done
1531       */
1532 <    final boolean preJoin(ForkJoinTask<?> joinMe, boolean maintainParallelism) {
1532 >    final boolean preJoin(ForkJoinTask<?> joinMe,
1533 >                          boolean maintainParallelism) {
1534          maintainParallelism &= maintainsParallelism; // overrride
1535          boolean dec = false;  // true when running count decremented
1536          while (spareStack == null || !tryResumeSpare(dec)) {
1537              int counts = workerCounts;
1538 <            if (dec || (dec = casWorkerCounts(counts, --counts))) { // CAS cheat
1538 >            if (dec || (dec = casWorkerCounts(counts, --counts))) {
1539 >                // CAS cheat
1540                  if (!needSpare(counts, maintainParallelism))
1541                      break;
1542                  if (joinMe.status < 0)
# Line 1372 | Line 1551 | public class ForkJoinPool extends Abstra
1551      /**
1552       * Same idea as preJoin
1553       */
1554 <    final boolean preBlock(ManagedBlocker blocker, boolean maintainParallelism){
1554 >    final boolean preBlock(ManagedBlocker blocker,
1555 >                           boolean maintainParallelism) {
1556          maintainParallelism &= maintainsParallelism;
1557          boolean dec = false;
1558          while (spareStack == null || !tryResumeSpare(dec)) {
# Line 1396 | Line 1576 | public class ForkJoinPool extends Abstra
1576       * there is apparently some work to do.  This self-limiting rule
1577       * means that the more threads that have already been added, the
1578       * less parallelism we will tolerate before adding another.
1579 +     *
1580       * @param counts current worker counts
1581       * @param maintainParallelism try to maintain parallelism
1582       */
# Line 1408 | Line 1589 | public class ForkJoinPool extends Abstra
1589          return (tc < maxPoolSize &&
1590                  (rc == 0 || totalSurplus < 0 ||
1591                   (maintainParallelism &&
1592 <                  runningDeficit > totalSurplus && mayHaveQueuedWork())));
1593 <    }
1413 <
1414 <    /**
1415 <     * Returns true if at least one worker queue appears to be
1416 <     * nonempty. This is expensive but not often called. It is not
1417 <     * critical that this be accurate, but if not, more or fewer
1418 <     * running threads than desired might be maintained.
1419 <     */
1420 <    private boolean mayHaveQueuedWork() {
1421 <        ForkJoinWorkerThread[] ws = workers;
1422 <        int len = ws.length;
1423 <        ForkJoinWorkerThread v;
1424 <        for (int i = 0; i < len; ++i) {
1425 <            if ((v = ws[i]) != null && v.getRawQueueSize() > 0) {
1426 <                releaseIdleWorkers(false); // help wake up stragglers
1427 <                return true;
1428 <            }
1429 <        }
1430 <        return false;
1592 >                  runningDeficit > totalSurplus &&
1593 >                  ForkJoinWorkerThread.hasQueuedTasks(workers))));
1594      }
1595  
1596      /**
1597 <     * Add a spare worker if lock available and no more than the
1598 <     * expected numbers of threads exist
1597 >     * Adds a spare worker if lock available and no more than the
1598 >     * expected numbers of threads exist.
1599 >     *
1600       * @return true if successful
1601       */
1602      private boolean tryAddSpare(int expectedCounts) {
# Line 1465 | Line 1629 | public class ForkJoinPool extends Abstra
1629      }
1630  
1631      /**
1632 <     * Add the kth spare worker. On entry, pool coounts are already
1632 >     * Adds the kth spare worker. On entry, pool counts are already
1633       * adjusted to reflect addition.
1634       */
1635      private void createAndStartSpare(int k) {
# Line 1477 | Line 1641 | public class ForkJoinPool extends Abstra
1641              for (k = 0; k < len && ws[k] != null; ++k)
1642                  ;
1643          }
1644 <        if (k < len && (w = createWorker(k)) != null) {
1644 >        if (k < len && !isTerminating() && (w = createWorker(k)) != null) {
1645              ws[k] = w;
1646              w.start();
1647          }
1648          else
1649              updateWorkerCount(-1); // adjust on failure
1650 <        signalIdleWorkers(false);
1650 >        signalIdleWorkers();
1651      }
1652  
1653      /**
1654 <     * Suspend calling thread w if there are excess threads.  Called
1655 <     * only from sync.  Spares are enqueued in a Treiber stack
1656 <     * using the same WaitQueueNodes as barriers.  They are resumed
1657 <     * mainly in preJoin, but are also woken on pool events that
1658 <     * require all threads to check run state.
1654 >     * Suspends calling thread w if there are excess threads.  Called
1655 >     * only from sync.  Spares are enqueued in a Treiber stack using
1656 >     * the same WaitQueueNodes as barriers.  They are resumed mainly
1657 >     * in preJoin, but are also woken on pool events that require all
1658 >     * threads to check run state.
1659 >     *
1660       * @param w the caller
1661       */
1662      private boolean suspendIfSpare(ForkJoinWorkerThread w) {
# Line 1499 | Line 1664 | public class ForkJoinPool extends Abstra
1664          int s;
1665          while (parallelism < runningCountOf(s = workerCounts)) {
1666              if (node == null)
1667 <                node = new WaitQueueNode(w, 0);
1667 >                node = new WaitQueueNode(0, w);
1668              if (casWorkerCounts(s, s-1)) { // representation-dependent
1669                  // push onto stack
1670 <                do;while (!casSpareStack(node.next = spareStack, node));
1506 <
1670 >                do {} while (!casSpareStack(node.next = spareStack, node));
1671                  // block until released by resumeSpare
1672 <                while (node.thread != null) {
1509 <                    if (!Thread.interrupted())
1510 <                        LockSupport.park(this);
1511 <                }
1512 <                w.activate(); // help warm up
1672 >                node.awaitSpareRelease();
1673                  return true;
1674              }
1675          }
# Line 1517 | Line 1677 | public class ForkJoinPool extends Abstra
1677      }
1678  
1679      /**
1680 <     * Try to pop and resume a spare thread.
1680 >     * Tries to pop and resume a spare thread.
1681 >     *
1682       * @param updateCount if true, increment running count on success
1683       * @return true if successful
1684       */
# Line 1535 | Line 1696 | public class ForkJoinPool extends Abstra
1696      }
1697  
1698      /**
1699 <     * Pop and resume all spare threads. Same idea as
1700 <     * releaseIdleWorkers.
1699 >     * Pops and resumes all spare threads. Same idea as ensureSync.
1700 >     *
1701       * @return true if any spares released
1702       */
1703      private boolean resumeAllSpares() {
# Line 1554 | Line 1715 | public class ForkJoinPool extends Abstra
1715      }
1716  
1717      /**
1718 <     * Pop and shutdown excessive spare threads. Call only while
1718 >     * Pops and shuts down excessive spare threads. Call only while
1719       * holding lock. This is not guaranteed to eliminate all excess
1720       * threads, only those suspended as spares, which are the ones
1721       * unlikely to be needed in the future.
# Line 1577 | Line 1738 | public class ForkJoinPool extends Abstra
1738      }
1739  
1740      /**
1580     * Returns approximate number of spares, just for diagnostics.
1581     */
1582    private int countSpares() {
1583        int sum = 0;
1584        for (WaitQueueNode q = spareStack; q != null; q = q.next)
1585            ++sum;
1586        return sum;
1587    }
1588
1589    /**
1741       * Interface for extending managed parallelism for tasks running
1742       * in ForkJoinPools. A ManagedBlocker provides two methods.
1743 <     * Method <code>isReleasable</code> must return true if blocking is not
1744 <     * necessary. Method <code>block</code> blocks the current thread
1745 <     * if necessary (perhaps internally invoking isReleasable before
1746 <     * actually blocking.).
1743 >     * Method {@code isReleasable} must return true if blocking is not
1744 >     * necessary. Method {@code block} blocks the current thread if
1745 >     * necessary (perhaps internally invoking {@code isReleasable}
1746 >     * before actually blocking.).
1747 >     *
1748       * <p>For example, here is a ManagedBlocker based on a
1749       * ReentrantLock:
1750 <     * <pre>
1751 <     *   class ManagedLocker implements ManagedBlocker {
1752 <     *     final ReentrantLock lock;
1753 <     *     boolean hasLock = false;
1754 <     *     ManagedLocker(ReentrantLock lock) { this.lock = lock; }
1755 <     *     public boolean block() {
1756 <     *        if (!hasLock)
1757 <     *           lock.lock();
1758 <     *        return true;
1759 <     *     }
1760 <     *     public boolean isReleasable() {
1761 <     *        return hasLock || (hasLock = lock.tryLock());
1610 <     *     }
1750 >     *  <pre> {@code
1751 >     * class ManagedLocker implements ManagedBlocker {
1752 >     *   final ReentrantLock lock;
1753 >     *   boolean hasLock = false;
1754 >     *   ManagedLocker(ReentrantLock lock) { this.lock = lock; }
1755 >     *   public boolean block() {
1756 >     *     if (!hasLock)
1757 >     *       lock.lock();
1758 >     *     return true;
1759 >     *   }
1760 >     *   public boolean isReleasable() {
1761 >     *     return hasLock || (hasLock = lock.tryLock());
1762       *   }
1763 <     * </pre>
1763 >     * }}</pre>
1764       */
1765      public static interface ManagedBlocker {
1766          /**
1767           * Possibly blocks the current thread, for example waiting for
1768           * a lock or condition.
1769 +         *
1770           * @return true if no additional blocking is necessary (i.e.,
1771 <         * if isReleasable would return true).
1771 >         * if isReleasable would return true)
1772           * @throws InterruptedException if interrupted while waiting
1773 <         * (the method is not required to do so, but is allowe to).
1773 >         * (the method is not required to do so, but is allowed to)
1774           */
1775          boolean block() throws InterruptedException;
1776  
# Line 1633 | Line 1785 | public class ForkJoinPool extends Abstra
1785       * is a ForkJoinWorkerThread, this method possibly arranges for a
1786       * spare thread to be activated if necessary to ensure parallelism
1787       * while the current thread is blocked.  If
1788 <     * <code>maintainParallelism</code> is true and the pool supports
1788 >     * {@code maintainParallelism} is true and the pool supports
1789       * it ({@link #getMaintainsParallelism}), this method attempts to
1790 <     * maintain the pool's nominal parallelism. Otherwise if activates
1790 >     * maintain the pool's nominal parallelism. Otherwise it activates
1791       * a thread only if necessary to avoid complete starvation. This
1792       * option may be preferable when blockages use timeouts, or are
1793       * almost always brief.
1794       *
1795       * <p> If the caller is not a ForkJoinTask, this method is behaviorally
1796       * equivalent to
1797 <     * <pre>
1798 <     *   while (!blocker.isReleasable())
1799 <     *      if (blocker.block())
1800 <     *         return;
1801 <     * </pre>
1797 >     *  <pre> {@code
1798 >     * while (!blocker.isReleasable())
1799 >     *   if (blocker.block())
1800 >     *     return;
1801 >     * }</pre>
1802       * If the caller is a ForkJoinTask, then the pool may first
1803       * be expanded to ensure parallelism, and later adjusted.
1804       *
# Line 1655 | Line 1807 | public class ForkJoinPool extends Abstra
1807       * attempt to maintain the pool's nominal parallelism; otherwise
1808       * activate a thread only if necessary to avoid complete
1809       * starvation.
1810 <     * @throws InterruptedException if blocker.block did so.
1810 >     * @throws InterruptedException if blocker.block did so
1811       */
1812      public static void managedBlock(ManagedBlocker blocker,
1813                                      boolean maintainParallelism)
1814          throws InterruptedException {
1815          Thread t = Thread.currentThread();
1816 <        ForkJoinPool pool = (t instanceof ForkJoinWorkerThread?
1817 <                             ((ForkJoinWorkerThread)t).pool : null);
1816 >        ForkJoinPool pool = ((t instanceof ForkJoinWorkerThread) ?
1817 >                             ((ForkJoinWorkerThread) t).pool : null);
1818          if (!blocker.isReleasable()) {
1819              try {
1820                  if (pool == null ||
# Line 1677 | Line 1829 | public class ForkJoinPool extends Abstra
1829  
1830      private static void awaitBlocker(ManagedBlocker blocker)
1831          throws InterruptedException {
1832 <        do;while (!blocker.isReleasable() && !blocker.block());
1832 >        do {} while (!blocker.isReleasable() && !blocker.block());
1833      }
1834  
1835      // AbstractExecutorService overrides
1836  
1837      protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
1838 <        return new AdaptedRunnable(runnable, value);
1838 >        return new AdaptedRunnable<T>(runnable, value);
1839      }
1840  
1841      protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
1842 <        return new AdaptedCallable(callable);
1842 >        return new AdaptedCallable<T>(callable);
1843      }
1844  
1845  
1846 <    // Temporary Unsafe mechanics for preliminary release
1846 >    // Unsafe mechanics for jsr166y 3rd party package.
1847 >    private static sun.misc.Unsafe getUnsafe() {
1848 >        try {
1849 >            return sun.misc.Unsafe.getUnsafe();
1850 >        } catch (SecurityException se) {
1851 >            try {
1852 >                return java.security.AccessController.doPrivileged
1853 >                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1854 >                        public sun.misc.Unsafe run() throws Exception {
1855 >                            return getUnsafeByReflection();
1856 >                        }});
1857 >            } catch (java.security.PrivilegedActionException e) {
1858 >                throw new RuntimeException("Could not initialize intrinsics",
1859 >                                           e.getCause());
1860 >            }
1861 >        }
1862 >    }
1863  
1864 <    static final Unsafe _unsafe;
1865 <    static final long eventCountOffset;
1866 <    static final long workerCountsOffset;
1867 <    static final long runControlOffset;
1868 <    static final long barrierStackOffset;
1869 <    static final long spareStackOffset;
1864 >    private static sun.misc.Unsafe getUnsafeByReflection()
1865 >            throws NoSuchFieldException, IllegalAccessException {
1866 >        java.lang.reflect.Field f =
1867 >            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1868 >        f.setAccessible(true);
1869 >        return (sun.misc.Unsafe) f.get(null);
1870 >    }
1871  
1872 <    static {
1872 >    private static long fieldOffset(String fieldName, Class<?> klazz) {
1873          try {
1874 <            if (ForkJoinPool.class.getClassLoader() != null) {
1875 <                Field f = Unsafe.class.getDeclaredField("theUnsafe");
1876 <                f.setAccessible(true);
1877 <                _unsafe = (Unsafe)f.get(null);
1878 <            }
1879 <            else
1711 <                _unsafe = Unsafe.getUnsafe();
1712 <            eventCountOffset = _unsafe.objectFieldOffset
1713 <                (ForkJoinPool.class.getDeclaredField("eventCount"));
1714 <            workerCountsOffset = _unsafe.objectFieldOffset
1715 <                (ForkJoinPool.class.getDeclaredField("workerCounts"));
1716 <            runControlOffset = _unsafe.objectFieldOffset
1717 <                (ForkJoinPool.class.getDeclaredField("runControl"));
1718 <            barrierStackOffset = _unsafe.objectFieldOffset
1719 <                (ForkJoinPool.class.getDeclaredField("barrierStack"));
1720 <            spareStackOffset = _unsafe.objectFieldOffset
1721 <                (ForkJoinPool.class.getDeclaredField("spareStack"));
1722 <        } catch (Exception e) {
1723 <            throw new RuntimeException("Could not initialize intrinsics", e);
1874 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1875 >        } catch (NoSuchFieldException e) {
1876 >            // Convert Exception to Error
1877 >            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1878 >            error.initCause(e);
1879 >            throw error;
1880          }
1881      }
1882  
1883 +    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1884 +    static final long eventCountOffset =
1885 +        fieldOffset("eventCount", ForkJoinPool.class);
1886 +    static final long workerCountsOffset =
1887 +        fieldOffset("workerCounts", ForkJoinPool.class);
1888 +    static final long runControlOffset =
1889 +        fieldOffset("runControl", ForkJoinPool.class);
1890 +    static final long syncStackOffset =
1891 +        fieldOffset("syncStack",ForkJoinPool.class);
1892 +    static final long spareStackOffset =
1893 +        fieldOffset("spareStack", ForkJoinPool.class);
1894 +
1895      private boolean casEventCount(long cmp, long val) {
1896 <        return _unsafe.compareAndSwapLong(this, eventCountOffset, cmp, val);
1896 >        return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val);
1897      }
1898      private boolean casWorkerCounts(int cmp, int val) {
1899 <        return _unsafe.compareAndSwapInt(this, workerCountsOffset, cmp, val);
1899 >        return UNSAFE.compareAndSwapInt(this, workerCountsOffset, cmp, val);
1900      }
1901      private boolean casRunControl(int cmp, int val) {
1902 <        return _unsafe.compareAndSwapInt(this, runControlOffset, cmp, val);
1902 >        return UNSAFE.compareAndSwapInt(this, runControlOffset, cmp, val);
1903      }
1904      private boolean casSpareStack(WaitQueueNode cmp, WaitQueueNode val) {
1905 <        return _unsafe.compareAndSwapObject(this, spareStackOffset, cmp, val);
1905 >        return UNSAFE.compareAndSwapObject(this, spareStackOffset, cmp, val);
1906      }
1907      private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) {
1908 <        return _unsafe.compareAndSwapObject(this, barrierStackOffset, cmp, val);
1908 >        return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val);
1909      }
1910   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines