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.54 by dl, Sun Apr 18 12:51:18 2010 UTC vs.
Revision 1.64 by dl, Tue Aug 17 18:30:32 2010 UTC

# Line 21 | Line 21 | import java.util.concurrent.CountDownLat
21   /**
22   * An {@link ExecutorService} for running {@link ForkJoinTask}s.
23   * A {@code ForkJoinPool} provides the entry point for submissions
24 < * from non-{@code ForkJoinTask}s, as well as management and
24 > * from non-{@code ForkJoinTask} clients, as well as management and
25   * monitoring operations.
26   *
27   * <p>A {@code ForkJoinPool} differs from other kinds of {@link
# Line 30 | Line 30 | import java.util.concurrent.CountDownLat
30   * execute subtasks created by other active tasks (eventually blocking
31   * waiting for work if none exist). This enables efficient processing
32   * when most tasks spawn other subtasks (as do most {@code
33 < * ForkJoinTask}s). A {@code ForkJoinPool} may also be used for mixed
34 < * execution of some plain {@code Runnable}- or {@code Callable}-
35 < * based activities along with {@code ForkJoinTask}s. When setting
36 < * {@linkplain #setAsyncMode async mode}, a {@code ForkJoinPool} may
37 < * also be appropriate for use with fine-grained tasks of any form
38 < * that are never joined. Otherwise, other {@code ExecutorService}
39 < * implementations are typically more appropriate choices.
33 > * ForkJoinTask}s). When setting <em>asyncMode</em> to true in
34 > * constructors, {@code ForkJoinPool}s may also be appropriate for use
35 > * with event-style tasks that are never joined.
36   *
37   * <p>A {@code ForkJoinPool} is constructed with a given target
38   * parallelism level; by default, equal to the number of available
39 < * processors. Unless configured otherwise via {@link
40 < * #setMaintainsParallelism}, the pool attempts to maintain this
41 < * number of active (or available) threads by dynamically adding,
42 < * suspending, or resuming internal worker threads, even if some tasks
43 < * are stalled waiting to join others. However, no such adjustments
44 < * are performed in the face of blocked IO or other unmanaged
45 < * synchronization. The nested {@link ManagedBlocker} interface
50 < * enables extension of the kinds of synchronization accommodated.
51 < * The target parallelism level may also be changed dynamically
52 < * ({@link #setParallelism}). The total number of threads may be
53 < * limited using method {@link #setMaximumPoolSize}, in which case it
54 < * may become possible for the activities of a pool to stall due to
55 < * the lack of available threads to process new tasks. When the pool
56 < * is executing tasks, these and other configuration setting methods
57 < * may only gradually affect actual pool sizes. It is normally best
58 < * practice to invoke these methods only when the pool is known to be
59 < * quiescent.
39 > * processors. The pool attempts to maintain enough active (or
40 > * available) threads by dynamically adding, suspending, or resuming
41 > * internal worker threads, even if some tasks are stalled waiting to
42 > * join others. However, no such adjustments are guaranteed in the
43 > * face of blocked IO or other unmanaged synchronization. The nested
44 > * {@link ManagedBlocker} interface enables extension of the kinds of
45 > * synchronization accommodated.
46   *
47   * <p>In addition to execution and lifecycle control methods, this
48   * class provides status check methods (for example
# Line 65 | Line 51 | import java.util.concurrent.CountDownLat
51   * {@link #toString} returns indications of pool state in a
52   * convenient form for informal monitoring.
53   *
54 + * <p> As is the case with other ExecutorServices, there are three
55 + * main task execution methods summarized in the following
56 + * table. These are designed to be used by clients not already engaged
57 + * in fork/join computations in the current pool.  The main forms of
58 + * these methods accept instances of {@code ForkJoinTask}, but
59 + * overloaded forms also allow mixed execution of plain {@code
60 + * Runnable}- or {@code Callable}- based activities as well.  However,
61 + * tasks that are already executing in a pool should normally
62 + * <em>NOT</em> use these pool execution methods, but instead use the
63 + * within-computation forms listed in the table.
64 + *
65 + * <table BORDER CELLPADDING=3 CELLSPACING=1>
66 + *  <tr>
67 + *    <td></td>
68 + *    <td ALIGN=CENTER> <b>Call from non-fork/join clients</b></td>
69 + *    <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td>
70 + *  </tr>
71 + *  <tr>
72 + *    <td> <b>Arange async execution</td>
73 + *    <td> {@link #execute(ForkJoinTask)}</td>
74 + *    <td> {@link ForkJoinTask#fork}</td>
75 + *  </tr>
76 + *  <tr>
77 + *    <td> <b>Await and obtain result</td>
78 + *    <td> {@link #invoke(ForkJoinTask)}</td>
79 + *    <td> {@link ForkJoinTask#invoke}</td>
80 + *  </tr>
81 + *  <tr>
82 + *    <td> <b>Arrange exec and obtain Future</td>
83 + *    <td> {@link #submit(ForkJoinTask)}</td>
84 + *    <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td>
85 + *  </tr>
86 + * </table>
87 + *
88   * <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is
89   * used for all parallel task execution in a program or subsystem.
90   * Otherwise, use would not usually outweigh the construction and
# Line 89 | Line 109 | import java.util.concurrent.CountDownLat
109   * {@code IllegalArgumentException}.
110   *
111   * <p>This implementation rejects submitted tasks (that is, by throwing
112 < * {@link RejectedExecutionException}) only when the pool is shut down.
112 > * {@link RejectedExecutionException}) only when the pool is shut down
113 > * or internal resources have been exhausted.
114   *
115   * @since 1.7
116   * @author Doug Lea
# Line 116 | Line 137 | public class ForkJoinPool extends Abstra
137       * of tasks profit from cache affinities, but others are harmed by
138       * cache pollution effects.)
139       *
140 +     * Beyond work-stealing support and essential bookkeeping, the
141 +     * main responsibility of this framework is to take actions when
142 +     * one worker is waiting to join a task stolen (or always held by)
143 +     * another.  Becauae we are multiplexing many tasks on to a pool
144 +     * of workers, we can't just let them block (as in Thread.join).
145 +     * We also cannot just reassign the joiner's run-time stack with
146 +     * another and replace it later, which would be a form of
147 +     * "continuation", that even if possible is not necessarily a good
148 +     * idea. Given that the creation costs of most threads on most
149 +     * systems mainly surrounds setting up runtime stacks, thread
150 +     * creation and switching is usually not much more expensive than
151 +     * stack creation and switching, and is more flexible). Instead we
152 +     * combine two tactics:
153 +     *
154 +     *   Helping: Arranging for the joiner to execute some task that it
155 +     *      would be running if the steal had not occurred.  Method
156 +     *      ForkJoinWorkerThread.helpJoinTask tracks joining->stealing
157 +     *      links to try to find such a task.
158 +     *
159 +     *   Compensating: Unless there are already enough live threads,
160 +     *      method helpMaintainParallelism() may create or or
161 +     *      re-activate a spare thread to compensate for blocked
162 +     *      joiners until they unblock.
163 +     *
164 +     * Because the determining existence of conservatively safe
165 +     * helping targets, the availability of already-created spares,
166 +     * and the apparent need to create new spares are all racy and
167 +     * require heuristic guidance, we rely on multiple retries of
168 +     * each. Further, because it is impossible to keep exactly the
169 +     * target (parallelism) number of threads running at any given
170 +     * time, we allow compensation during joins to fail, and enlist
171 +     * all other threads to help out whenever they are not otherwise
172 +     * occupied (i.e., mainly in method preStep).
173 +     *
174 +     * The ManagedBlocker extension API can't use helping so relies
175 +     * only on compensation in method awaitBlocker.
176 +     *
177       * The main throughput advantages of work-stealing stem from
178       * decentralized control -- workers mostly steal tasks from each
179       * other. We do not want to negate this by creating bottlenecks
180 <     * implementing the management responsibilities of this class. So
181 <     * we use a collection of techniques that avoid, reduce, or cope
182 <     * well with contention. These entail several instances of
183 <     * bit-packing into CASable fields to maintain only the minimally
184 <     * required atomicity. To enable such packing, we restrict maximum
185 <     * parallelism to (1<<15)-1 (enabling twice this to fit into a 16
186 <     * bit field), which is far in excess of normal operating range.
187 <     * Even though updates to some of these bookkeeping fields do
188 <     * sometimes contend with each other, they don't normally
189 <     * cache-contend with updates to others enough to warrant memory
190 <     * padding or isolation. So they are all held as fields of
191 <     * ForkJoinPool objects.  The main capabilities are as follows:
180 >     * implementing other management responsibilities. So we use a
181 >     * collection of techniques that avoid, reduce, or cope well with
182 >     * contention. These entail several instances of bit-packing into
183 >     * CASable fields to maintain only the minimally required
184 >     * atomicity. To enable such packing, we restrict maximum
185 >     * parallelism to (1<<15)-1 (enabling twice this (to accommodate
186 >     * unbalanced increments and decrements) to fit into a 16 bit
187 >     * field, which is far in excess of normal operating range.  Even
188 >     * though updates to some of these bookkeeping fields do sometimes
189 >     * contend with each other, they don't normally cache-contend with
190 >     * updates to others enough to warrant memory padding or
191 >     * isolation. So they are all held as fields of ForkJoinPool
192 >     * objects.  The main capabilities are as follows:
193       *
194       * 1. Creating and removing workers. Workers are recorded in the
195       * "workers" array. This is an array as opposed to some other data
# Line 140 | Line 199 | public class ForkJoinPool extends Abstra
199       * (workerLock) but the array is otherwise concurrently readable,
200       * and accessed directly by workers. To simplify index-based
201       * operations, the array size is always a power of two, and all
202 <     * readers must tolerate null slots. Currently, all but the first
203 <     * worker thread creation is on-demand, triggered by task
204 <     * submissions, replacement of terminated workers, and/or
205 <     * compensation for blocked workers. However, all other support
206 <     * code is set up to work with other policies.
202 >     * readers must tolerate null slots. Currently, all worker thread
203 >     * creation is on-demand, triggered by task submissions,
204 >     * replacement of terminated workers, and/or compensation for
205 >     * blocked workers. However, all other support code is set up to
206 >     * work with other policies.
207 >     *
208 >     * To ensure that we do not hold on to worker references that
209 >     * would prevent GC, ALL accesses to workers are via indices into
210 >     * the workers array (which is one source of some of the unusual
211 >     * code constructions here). In essence, the workers array serves
212 >     * as a WeakReference mechanism. Thus for example the event queue
213 >     * stores worker indices, not worker references. Access to the
214 >     * workers in associated methods (for example releaseEventWaiters)
215 >     * must both index-check and null-check the IDs. All such accesses
216 >     * ignore bad IDs by returning out early from what they are doing,
217 >     * since this can only be associated with shutdown, in which case
218 >     * it is OK to give up. On termination, we just clobber these
219 >     * data structures without trying to use them.
220       *
221       * 2. Bookkeeping for dynamically adding and removing workers. We
222 <     * maintain a given level of parallelism (or, if
223 <     * maintainsParallelism is false, at least avoid starvation). When
152 <     * some workers are known to be blocked (on joins or via
222 >     * aim to approximately maintain the given level of parallelism.
223 >     * When some workers are known to be blocked (on joins or via
224       * ManagedBlocker), we may create or resume others to take their
225       * place until they unblock (see below). Implementing this
226       * requires counts of the number of "running" threads (i.e., those
227       * that are neither blocked nor artifically suspended) as well as
228       * the total number.  These two values are packed into one field,
229       * "workerCounts" because we need accurate snapshots when deciding
230 <     * to create, resume or suspend.  To support these decisions,
231 <     * updates must be prospective (not retrospective).  For example,
232 <     * the running count is decremented before blocking by a thread
233 <     * about to block, but incremented by the thread about to unblock
163 <     * it. (In a few cases, these prospective updates may need to be
164 <     * rolled back, for example when deciding to create a new worker
165 <     * but the thread factory fails or returns null. In these cases,
166 <     * we are no worse off wrt other decisions than we would be
167 <     * otherwise.)  Updates to the workerCounts field sometimes
168 <     * transiently encounter a fair amount of contention when join
169 <     * dependencies are such that many threads block or unblock at
170 <     * about the same time. We alleviate this by sometimes bundling
171 <     * updates (for example blocking one thread on join and resuming a
172 <     * spare cancel each other out), and in most other cases
173 <     * performing an alternative action (like releasing waiters and
174 <     * finding spares; see below) as a more productive form of
175 <     * backoff.
230 >     * to create, resume or suspend.  Note however that the
231 >     * correspondance of these counts to reality is not guaranteed. In
232 >     * particular updates for unblocked threads may lag until they
233 >     * actually wake up.
234       *
235       * 3. Maintaining global run state. The run state of the pool
236       * consists of a runLevel (SHUTDOWN, TERMINATING, etc) similar to
# Line 201 | Line 259 | public class ForkJoinPool extends Abstra
259       * workers that previously could not find a task to now find one:
260       * Submission of a new task to the pool, or another worker pushing
261       * a task onto a previously empty queue.  (We also use this
262 <     * mechanism for termination and reconfiguration actions that
262 >     * mechanism for configuration and termination actions that
263       * require wakeups of idle workers).  Each worker maintains its
264       * last known event count, and blocks when a scan for work did not
265       * find a task AND its lastEventCount matches the current
# Line 212 | Line 270 | public class ForkJoinPool extends Abstra
270       * a record (field nextEventWaiter) for the next waiting worker.
271       * In addition to allowing simpler decisions about need for
272       * wakeup, the event count bits in eventWaiters serve the role of
273 <     * tags to avoid ABA errors in Treiber stacks.  To reduce delays
274 <     * in task diffusion, workers not otherwise occupied may invoke
275 <     * method releaseWaiters, that removes and signals (unparks)
276 <     * workers not waiting on current count. To minimize task
277 <     * production stalls associate with signalling, any worker pushing
278 <     * a task on an empty queue invokes the weaker method signalWork,
279 <     * that only releases idle workers until it detects interference
222 <     * by other threads trying to release, and lets them take
223 <     * over. The net effect is a tree-like diffusion of signals, where
224 <     * released threads and possibly others) help with unparks.  To
225 <     * further reduce contention effects a bit, failed CASes to
226 <     * increment field eventCount are tolerated without retries.
273 >     * tags to avoid ABA errors in Treiber stacks. Upon any wakeup,
274 >     * released threads also try to release others (but give up upon
275 >     * contention to reduce useless flailing).  The net effect is a
276 >     * tree-like diffusion of signals, where released threads (and
277 >     * possibly others) help with unparks.  To further reduce
278 >     * contention effects a bit, failed CASes to increment field
279 >     * eventCount are tolerated without retries in signalWork.
280       * Conceptually they are merged into the same event, which is OK
281       * when their only purpose is to enable workers to scan for work.
282       *
283       * 5. Managing suspension of extra workers. When a worker is about
284       * to block waiting for a join (or via ManagedBlockers), we may
285       * create a new thread to maintain parallelism level, or at least
286 <     * avoid starvation (see below). Usually, extra threads are needed
287 <     * for only very short periods, yet join dependencies are such
288 <     * that we sometimes need them in bursts. Rather than create new
289 <     * threads each time this happens, we suspend no-longer-needed
290 <     * extra ones as "spares". For most purposes, we don't distinguish
291 <     * "extra" spare threads from normal "core" threads: On each call
292 <     * to preStep (the only point at which we can do this) a worker
286 >     * avoid starvation. Usually, extra threads are needed for only
287 >     * very short periods, yet join dependencies are such that we
288 >     * sometimes need them in bursts. Rather than create new threads
289 >     * each time this happens, we suspend no-longer-needed extra ones
290 >     * as "spares". For most purposes, we don't distinguish "extra"
291 >     * spare threads from normal "core" threads: On each call to
292 >     * preStep (the only point at which we can do this) a worker
293       * checks to see if there are now too many running workers, and if
294 <     * so, suspends itself.  Methods preJoin and doBlock look for
294 >     * so, suspends itself.  Method helpMaintainParallelism looks for
295       * suspended threads to resume before considering creating a new
296 <     * replacement. We don't need a special data structure to maintain
297 <     * spares; simply scanning the workers array looking for
298 <     * worker.isSuspended() is fine because the calling thread is
246 <     * otherwise not doing anything useful anyway; we are at least as
247 <     * happy if after locating a spare, the caller doesn't actually
248 <     * block because the join is ready before we try to adjust and
249 <     * compensate.  Note that this is intrinsically racy.  One thread
296 >     * replacement. The spares themselves are encoded on another
297 >     * variant of a Treiber Stack, headed at field "spareWaiters".
298 >     * Note that the use of spares is intrinsically racy.  One thread
299       * may become a spare at about the same time as another is
300       * needlessly being created. We counteract this and related slop
301       * in part by requiring resumed spares to immediately recheck (in
302 <     * preStep) to see whether they they should re-suspend. The only
303 <     * effective difference between "extra" and "core" threads is that
304 <     * we allow the "extra" ones to time out and die if they are not
305 <     * resumed within a keep-alive interval of a few seconds. This is
306 <     * implemented mainly within ForkJoinWorkerThread, but requires
307 <     * some coordination (isTrimmed() -- meaning killed while
308 <     * suspended) to correctly maintain pool counts.
309 <     *
310 <     * 6. Deciding when to create new workers. The main dynamic
311 <     * control in this class is deciding when to create extra threads,
312 <     * in methods preJoin and doBlock. We always need to create one
313 <     * when the number of running threads becomes zero. But because
314 <     * blocked joins are typically dependent, we don't necessarily
315 <     * need or want one-to-one replacement. Using a one-to-one
316 <     * compensation rule often leads to enough useless overhead
317 <     * creating, suspending, resuming, and/or killing threads to
318 <     * signficantly degrade throughput.  We use a rule reflecting the
319 <     * idea that, the more spare threads you already have, the more
320 <     * evidence you need to create another one; where "evidence" is
321 <     * expressed as the current deficit -- target minus running
322 <     * threads. To reduce flickering and drift around target values,
323 <     * the relation is quadratic: adding a spare if (dc*dc)>=(sc*pc)
324 <     * (where dc is deficit, sc is number of spare threads and pc is
325 <     * target parallelism.)  This effectively reduces churn at the
326 <     * price of systematically undershooting target parallelism when
327 <     * many threads are blocked.  However, biasing toward undeshooting
328 <     * partially compensates for the above mechanics to suspend extra
329 <     * threads, that normally lead to overshoot because we can only
330 <     * suspend workers in-between top-level actions. It also better
331 <     * copes with the fact that some of the methods in this class tend
332 <     * to never become compiled (but are interpreted), so some
333 <     * components of the entire set of controls might execute many
302 >     * preStep) to see whether they they should re-suspend.
303 >     *
304 >     * 6. Killing off unneeded workers. The Spare and Event queues use
305 >     * similar mechanisms to shed unused workers: The oldest (first)
306 >     * waiter uses a timed rather than hard wait. When this wait times
307 >     * out without a normal wakeup, it tries to shutdown any one (for
308 >     * convenience the newest) other waiter via tryShutdownSpare or
309 >     * tryShutdownWaiter, respectively. The wakeup rates for spares
310 >     * are much shorter than for waiters. Together, they will
311 >     * eventually reduce the number of worker threads to a minimum of
312 >     * one after a long enough period without use.
313 >     *
314 >     * 7. Deciding when to create new workers. The main dynamic
315 >     * control in this class is deciding when to create extra threads
316 >     * in method helpMaintainParallelism. We would like to keep
317 >     * exactly #parallelism threads running, which is an impossble
318 >     * task. We always need to create one when the number of running
319 >     * threads would become zero and all workers are busy. Beyond
320 >     * this, we must rely on heuristics that work well in the the
321 >     * presence of transients phenomena such as GC stalls, dynamic
322 >     * compilation, and wake-up lags. These transients are extremely
323 >     * common -- we are normally trying to fully saturate the CPUs on
324 >     * a machine, so almost any activity other than running tasks
325 >     * impedes accuracy. Our main defense is to allow some slack in
326 >     * creation thresholds, using rules that reflect the fact that the
327 >     * more threads we have running, the more likely that we are
328 >     * underestimating the number running threads. (We also include
329 >     * some heuristic use of Thread.yield when all workers appear to
330 >     * be busy, to improve likelihood of counts settling.) The rules
331 >     * also better cope with the fact that some of the methods in this
332 >     * class tend to never become compiled (but are interpreted), so
333 >     * some components of the entire set of controls might execute 100
334       * times faster than others. And similarly for cases where the
335       * apparent lack of work is just due to GC stalls and other
336       * transient system activity.
337       *
289     * 7. Maintaining other configuration parameters and monitoring
290     * statistics. Updates to fields controlling parallelism level,
291     * max size, etc can only meaningfully take effect for individual
292     * threads upon their next top-level actions; i.e., between
293     * stealing/running tasks/submission, which are separated by calls
294     * to preStep.  Memory ordering for these (assumed infrequent)
295     * reconfiguration calls is ensured by using reads and writes to
296     * volatile field workerCounts (that must be read in preStep anyway)
297     * as "fences" -- user-level reads are preceded by reads of
298     * workCounts, and writes are followed by no-op CAS to
299     * workerCounts. The values reported by other management and
300     * monitoring methods are either computed on demand, or are kept
301     * in fields that are only updated when threads are otherwise
302     * idle.
303     *
338       * Beware that there is a lot of representation-level coupling
339       * among classes ForkJoinPool, ForkJoinWorkerThread, and
340       * ForkJoinTask.  For example, direct access to "workers" array by
# Line 312 | Line 346 | public class ForkJoinPool extends Abstra
346       *
347       * Style notes: There are lots of inline assignments (of form
348       * "while ((local = field) != 0)") which are usually the simplest
349 <     * way to ensure read orderings. Also several occurrences of the
350 <     * unusual "do {} while(!cas...)" which is the simplest way to
351 <     * force an update of a CAS'ed variable. There are also a few
352 <     * other coding oddities that help some methods perform reasonably
353 <     * even when interpreted (not compiled).
349 >     * way to ensure the required read orderings (which are sometimes
350 >     * critical). Also several occurrences of the unusual "do {}
351 >     * while(!cas...)" which is the simplest way to force an update of
352 >     * a CAS'ed variable. There are also other coding oddities that
353 >     * help some methods perform reasonably even when interpreted (not
354 >     * compiled), at the expense of some messy constructions that
355 >     * reduce byte code counts.
356       *
357       * The order of declarations in this file is: (1) statics (2)
358       * fields (along with constants used when unpacking some of them)
# Line 345 | Line 381 | public class ForkJoinPool extends Abstra
381       * Default ForkJoinWorkerThreadFactory implementation; creates a
382       * new ForkJoinWorkerThread.
383       */
384 <    static class  DefaultForkJoinWorkerThreadFactory
384 >    static class DefaultForkJoinWorkerThreadFactory
385          implements ForkJoinWorkerThreadFactory {
386          public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
387              return new ForkJoinWorkerThread(pool);
# Line 384 | Line 420 | public class ForkJoinPool extends Abstra
420          new AtomicInteger();
421  
422      /**
423 <     * Absolute bound for parallelism level. Twice this number must
424 <     * fit into a 16bit field to enable word-packing for some counts.
423 >     * The wakeup interval (in nanoseconds) for the oldest worker
424 >     * worker waiting for an event invokes tryShutdownWaiter to shrink
425 >     * the number of workers.  The exact value does not matter too
426 >     * much, but should be long enough to slowly release resources
427 >     * during long periods without use without disrupting normal use.
428       */
429 <    private static final int MAX_THREADS = 0x7fff;
429 >    private static final long SHRINK_RATE_NANOS =
430 >        60L * 1000L * 1000L * 1000L; // one minute
431 >
432 >    /**
433 >     * Absolute bound for parallelism level. Twice this number plus
434 >     * one (i.e., 0xfff) must fit into a 16bit field to enable
435 >     * word-packing for some counts and indices.
436 >     */
437 >    private static final int MAX_WORKERS   = 0x7fff;
438  
439      /**
440       * Array holding all worker threads in the pool.  Array size must
# Line 413 | Line 460 | public class ForkJoinPool extends Abstra
460      /**
461       * Latch released upon termination.
462       */
463 <    private final CountDownLatch terminationLatch;
463 >    private final Phaser termination;
464  
465      /**
466       * Creation factory for worker threads.
# Line 429 | Line 476 | public class ForkJoinPool extends Abstra
476      /**
477       * Encoded record of top of treiber stack of threads waiting for
478       * events. The top 32 bits contain the count being waited for. The
479 <     * bottom word contains one plus the pool index of waiting worker
480 <     * thread.
479 >     * bottom 16 bits contains one plus the pool index of waiting
480 >     * worker thread. (Bits 16-31 are unused.)
481       */
482      private volatile long eventWaiters;
483  
484      private static final int  EVENT_COUNT_SHIFT = 32;
485 <    private static final long WAITER_INDEX_MASK = (1L << EVENT_COUNT_SHIFT)-1L;
485 >    private static final long WAITER_ID_MASK    = (1L << 16) - 1L;
486  
487      /**
488       * A counter for events that may wake up worker threads:
489       *   - Submission of a new task to the pool
490       *   - A worker pushing a task on an empty queue
491 <     *   - termination and reconfiguration
491 >     *   - termination
492       */
493      private volatile int eventCount;
494  
495      /**
496 +     * Encoded record of top of treiber stack of spare threads waiting
497 +     * for resumption. The top 16 bits contain an arbitrary count to
498 +     * avoid ABA effects. The bottom 16bits contains one plus the pool
499 +     * index of waiting worker thread.
500 +     */
501 +    private volatile int spareWaiters;
502 +
503 +    private static final int SPARE_COUNT_SHIFT = 16;
504 +    private static final int SPARE_ID_MASK     = (1 << 16) - 1;
505 +
506 +    /**
507       * Lifecycle control. The low word contains the number of workers
508       * that are (probably) executing tasks. This value is atomically
509       * incremented before a worker gets a task to run, and decremented
# Line 456 | Line 514 | public class ForkJoinPool extends Abstra
514       * These are bundled together to ensure consistent read for
515       * termination checks (i.e., that runLevel is at least SHUTDOWN
516       * and active threads is zero).
517 +     *
518 +     * Notes: Most direct CASes are dependent on these bitfield
519 +     * positions.  Also, this field is non-private to enable direct
520 +     * performance-sensitive CASes in ForkJoinWorkerThread.
521       */
522 <    private volatile int runState;
522 >    volatile int runState;
523  
524      // Note: The order among run level values matters.
525      private static final int RUNLEVEL_SHIFT     = 16;
# Line 465 | Line 527 | public class ForkJoinPool extends Abstra
527      private static final int TERMINATING        = 1 << (RUNLEVEL_SHIFT + 1);
528      private static final int TERMINATED         = 1 << (RUNLEVEL_SHIFT + 2);
529      private static final int ACTIVE_COUNT_MASK  = (1 << RUNLEVEL_SHIFT) - 1;
468    private static final int ONE_ACTIVE         = 1; // active update delta
530  
531      /**
532       * Holds number of total (i.e., created and not yet terminated)
# Line 474 | Line 535 | public class ForkJoinPool extends Abstra
535       * making decisions about creating and suspending spare
536       * threads. Updated only by CAS. Note that adding a new worker
537       * requires incrementing both counts, since workers start off in
538 <     * running state.  This field is also used for memory-fencing
478 <     * configuration parameters.
538 >     * running state.
539       */
540      private volatile int workerCounts;
541  
# Line 484 | Line 544 | public class ForkJoinPool extends Abstra
544      private static final int ONE_RUNNING        = 1;
545      private static final int ONE_TOTAL          = 1 << TOTAL_COUNT_SHIFT;
546  
487    /*
488     * Fields parallelism. maxPoolSize, locallyFifo,
489     * maintainsParallelism, and ueh are non-volatile, but external
490     * reads/writes use workerCount fences to ensure visability.
491     */
492
547      /**
548       * The target parallelism level.
549 +     * Accessed directly by ForkJoinWorkerThreads.
550       */
551 <    private int parallelism;
497 <
498 <    /**
499 <     * The maximum allowed pool size.
500 <     */
501 <    private int maxPoolSize;
551 >    final int parallelism;
552  
553      /**
554       * True if use local fifo, not default lifo, for local polling
555 <     * Replicated by ForkJoinWorkerThreads
555 >     * Read by, and replicated by ForkJoinWorkerThreads
556       */
557 <    private boolean locallyFifo;
557 >    final boolean locallyFifo;
558  
559      /**
560 <     * Controls whether to add spares to maintain parallelism
560 >     * The uncaught exception handler used when any worker abruptly
561 >     * terminates.
562       */
563 <    private boolean maintainsParallelism;
513 <
514 <    /**
515 <     * The uncaught exception handler used when any worker
516 <     * abruptly terminates
517 <     */
518 <    private Thread.UncaughtExceptionHandler ueh;
563 >    private final Thread.UncaughtExceptionHandler ueh;
564  
565      /**
566       * Pool number, just for assigning useful names to worker threads
567       */
568      private final int poolNumber;
569  
570 <    // utilities for updating fields
570 >
571 >    // Utilities for CASing fields. Note that most of these
572 >    // are usually manually inlined by callers
573  
574      /**
575 <     * Adds delta to running count.  Used mainly by ForkJoinTask.
529 <     *
530 <     * @param delta the number to add
575 >     * Increments running count part of workerCounts
576       */
577 <    final void updateRunningCount(int delta) {
578 <        int wc;
577 >    final void incrementRunningCount() {
578 >        int c;
579          do {} while (!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
580 <                                               wc = workerCounts,
581 <                                               wc + delta));
580 >                                               c = workerCounts,
581 >                                               c + ONE_RUNNING));
582      }
583  
584      /**
585 <     * Write fence for user modifications of pool parameters
541 <     * (parallelism. etc).  Note that it doesn't matter if CAS fails.
585 >     * Tries to decrement running count unless already zero
586       */
587 <    private void workerCountWriteFence() {
588 <        int wc;
589 <        UNSAFE.compareAndSwapInt(this, workerCountsOffset,
590 <                                 wc = workerCounts, wc);
587 >    final boolean tryDecrementRunningCount() {
588 >        int wc = workerCounts;
589 >        if ((wc & RUNNING_COUNT_MASK) == 0)
590 >            return false;
591 >        return UNSAFE.compareAndSwapInt(this, workerCountsOffset,
592 >                                        wc, wc - ONE_RUNNING);
593      }
594  
595      /**
596 <     * Read fence for external reads of pool parameters
597 <     * (parallelism. maxPoolSize, etc).
596 >     * Forces decrement of encoded workerCounts, awaiting nonzero if
597 >     * (rarely) necessary when other count updates lag.
598 >     *
599 >     * @param dr -- either zero or ONE_RUNNING
600 >     * @param dt == either zero or ONE_TOTAL
601       */
602 <    private void workerCountReadFence() {
603 <        int ignore = workerCounts;
602 >    private void decrementWorkerCounts(int dr, int dt) {
603 >        for (;;) {
604 >            int wc = workerCounts;
605 >            if ((wc & RUNNING_COUNT_MASK)  - dr < 0 ||
606 >                (wc >>> TOTAL_COUNT_SHIFT) - dt < 0) {
607 >                if ((runState & TERMINATED) != 0)
608 >                    return; // lagging termination on a backout
609 >                Thread.yield();
610 >            }
611 >            if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
612 >                                         wc, wc - (dr + dt)))
613 >                return;
614 >        }
615 >    }
616 >
617 >    /**
618 >     * Increments event count
619 >     */
620 >    private void advanceEventCount() {
621 >        int c;
622 >        do {} while(!UNSAFE.compareAndSwapInt(this, eventCountOffset,
623 >                                              c = eventCount, c+1));
624      }
625  
626      /**
# Line 563 | Line 632 | public class ForkJoinPool extends Abstra
632      final boolean tryIncrementActiveCount() {
633          int c;
634          return UNSAFE.compareAndSwapInt(this, runStateOffset,
635 <                                        c = runState, c + ONE_ACTIVE);
635 >                                        c = runState, c + 1);
636      }
637  
638      /**
# Line 573 | Line 642 | public class ForkJoinPool extends Abstra
642      final boolean tryDecrementActiveCount() {
643          int c;
644          return UNSAFE.compareAndSwapInt(this, runStateOffset,
645 <                                        c = runState, c - ONE_ACTIVE);
645 >                                        c = runState, c - 1);
646      }
647  
648      /**
# Line 602 | Line 671 | public class ForkJoinPool extends Abstra
671          lock.lock();
672          try {
673              ForkJoinWorkerThread[] ws = workers;
674 <            int len = ws.length;
675 <            if (k < 0 || k >= len || ws[k] != null) {
676 <                for (k = 0; k < len && ws[k] != null; ++k)
674 >            int n = ws.length;
675 >            if (k < 0 || k >= n || ws[k] != null) {
676 >                for (k = 0; k < n && ws[k] != null; ++k)
677                      ;
678 <                if (k == len)
679 <                    ws = Arrays.copyOf(ws, len << 1);
678 >                if (k == n)
679 >                    ws = Arrays.copyOf(ws, n << 1);
680              }
681              ws[k] = w;
682              workers = ws; // volatile array write ensures slot visibility
# Line 641 | Line 710 | public class ForkJoinPool extends Abstra
710       * are already updated to accommodate the worker, so adjusts on
711       * failure.
712       *
713 <     * @return new worker or null if creation failed
713 >     * @return the worker, or null on failure
714       */
715      private ForkJoinWorkerThread addWorker() {
716          ForkJoinWorkerThread w = null;
# Line 649 | Line 718 | public class ForkJoinPool extends Abstra
718              w = factory.newThread(this);
719          } finally { // Adjust on either null or exceptional factory return
720              if (w == null) {
721 <                onWorkerCreationFailure();
722 <                return null;
721 >                decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL);
722 >                tryTerminate(false); // in case of failure during shutdown
723              }
724          }
725 <        w.start(recordWorker(w), locallyFifo, ueh);
726 <        return w;
727 <    }
659 <
660 <    /**
661 <     * Adjusts counts upon failure to create worker
662 <     */
663 <    private void onWorkerCreationFailure() {
664 <        int c;
665 <        do {} while (!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
666 <                                               c = workerCounts,
667 <                                               c - (ONE_RUNNING|ONE_TOTAL)));
668 <        tryTerminate(false); // in case of failure during shutdown
669 <    }
670 <
671 <    /**
672 <     * Create enough total workers to establish target parallelism,
673 <     * giving up if terminating or addWorker fails
674 <     */
675 <    private void ensureEnoughTotalWorkers() {
676 <        int wc;
677 <        while (runState < TERMINATING &&
678 <               ((wc = workerCounts) >>> TOTAL_COUNT_SHIFT) < parallelism) {
679 <            if ((UNSAFE.compareAndSwapInt(this, workerCountsOffset,
680 <                                          wc, wc + (ONE_RUNNING|ONE_TOTAL)) &&
681 <                 addWorker() == null))
682 <                break;
725 >        if (w != null) {
726 >            w.start(recordWorker(w), ueh);
727 >            advanceEventCount();
728          }
729 +        return w;
730      }
731  
732      /**
733       * Final callback from terminating worker.  Removes record of
734       * worker from array, and adjusts counts. If pool is shutting
735 <     * down, tries to complete terminatation, else possibly replaces
690 <     * the worker.
735 >     * down, tries to complete terminatation.
736       *
737       * @param w the worker
738       */
739      final void workerTerminated(ForkJoinWorkerThread w) {
695        if (w.active) { // force inactive
696            w.active = false;
697            do {} while (!tryDecrementActiveCount());
698        }
740          forgetWorker(w);
741 <
742 <        // decrement total count, and if was running, running count
743 <        int unit = w.isTrimmed()? ONE_TOTAL : (ONE_RUNNING|ONE_TOTAL);
744 <        int wc;
704 <        do {} while (!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
705 <                                               wc = workerCounts, wc - unit));
706 <
707 <        accumulateStealCount(w); // collect final count
708 <        if (!tryTerminate(false))
709 <            ensureEnoughTotalWorkers();
741 >        decrementWorkerCounts(w.isTrimmed()? 0 : ONE_RUNNING, ONE_TOTAL);
742 >        while (w.stealCount != 0) // collect final count
743 >            tryAccumulateStealCount(w);
744 >        tryTerminate(false);
745      }
746  
747      // Waiting for and signalling events
748  
749      /**
750 <     * Ensures eventCount on exit is different (mod 2^32) than on
751 <     * entry.  CAS failures are OK -- any change in count suffices.
752 <     */
753 <    private void advanceEventCount() {
754 <        int c;
755 <        UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
750 >     * Releases workers blocked on a count not equal to current count.
751 >     * Normally called after precheck that eventWaiters isn't zero to
752 >     * avoid wasted array checks. Gives up upon a change in count or
753 >     * contention, letting other workers take over.
754 >     */
755 >    private void releaseEventWaiters() {
756 >        ForkJoinWorkerThread[] ws = workers;
757 >        int n = ws.length;
758 >        long h = eventWaiters;
759 >        int ec = eventCount;
760 >        ForkJoinWorkerThread w; int id;
761 >        while ((int)(h >>> EVENT_COUNT_SHIFT) != ec &&
762 >               (id = ((int)(h & WAITER_ID_MASK)) - 1) >= 0 &&
763 >               id < n && (w = ws[id]) != null &&
764 >               UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
765 >                                         h,  h = w.nextWaiter)) {
766 >            LockSupport.unpark(w);
767 >            if (eventWaiters != h || eventCount != ec)
768 >                break;
769 >        }
770      }
771  
772      /**
773 <     * Releases workers blocked on a count not equal to current count.
773 >     * Tries to advance eventCount and releases waiters. Called only
774 >     * from workers.
775       */
776 <    final void releaseWaiters() {
777 <        long top;
778 <        int id;
779 <        while ((id = (int)((top = eventWaiters) & WAITER_INDEX_MASK)) > 0 &&
780 <               (int)(top >>> EVENT_COUNT_SHIFT) != eventCount) {
731 <            ForkJoinWorkerThread[] ws = workers;
732 <            ForkJoinWorkerThread w;
733 <            if (ws.length >= id && (w = ws[id - 1]) != null &&
734 <                UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
735 <                                          top, w.nextWaiter))
736 <                LockSupport.unpark(w);
737 <        }
776 >    final void signalWork() {
777 >        int c; // try to increment event count -- CAS failure OK
778 >        UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
779 >        if (eventWaiters != 0L)
780 >            releaseEventWaiters();
781      }
782  
783      /**
784 <     * Advances eventCount and releases waiters until interference by
785 <     * other releasing threads is detected.
784 >     * Adds the given worker to event queue and blocks until
785 >     * terminating or event count advances from the workers
786 >     * lastEventCount value
787 >     *
788 >     * @param w the calling worker thread
789       */
790 <    final void signalWork() {
791 <        int ec;
792 <        UNSAFE.compareAndSwapInt(this, eventCountOffset, ec=eventCount, ec+1);
793 <        outer:for (;;) {
794 <            long top = eventWaiters;
795 <            ec = eventCount;
796 <            for (;;) {
797 <                ForkJoinWorkerThread[] ws; ForkJoinWorkerThread w;
798 <                int id = (int)(top & WAITER_INDEX_MASK);
799 <                if (id <= 0 || (int)(top >>> EVENT_COUNT_SHIFT) == ec)
800 <                    return;
801 <                if ((ws = workers).length < id || (w = ws[id - 1]) == null ||
756 <                    !UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
757 <                                               top, top = w.nextWaiter))
758 <                    continue outer;      // possibly stale; reread
759 <                LockSupport.unpark(w);
760 <                if (top != eventWaiters) // let someone else take over
761 <                    return;
790 >    private void eventSync(ForkJoinWorkerThread w) {
791 >        int ec = w.lastEventCount;
792 >        long nh = (((long)ec) << EVENT_COUNT_SHIFT) | ((long)(w.poolIndex+1));
793 >        long h;
794 >        while ((runState < SHUTDOWN || !tryTerminate(false)) &&
795 >               (((int)((h = eventWaiters) & WAITER_ID_MASK)) == 0 ||
796 >                (int)(h >>> EVENT_COUNT_SHIFT) == ec) &&
797 >               eventCount == ec) {
798 >            if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
799 >                                          w.nextWaiter = h, nh)) {
800 >                awaitEvent(w, ec);
801 >                break;
802              }
803          }
804      }
805  
806      /**
807 <     * If worker is inactive, blocks until terminating or event count
808 <     * advances from last value held by worker; in any case helps
809 <     * release others.
807 >     * Blocks the given worker (that has already been entered as an
808 >     * event waiter) until terminating or event count advances from
809 >     * the given value. The oldest (first) waiter uses a timed wait to
810 >     * occasionally one-by-one shrink the number of workers (to a
811 >     * minumum of one) if the pool has not been used for extended
812 >     * periods.
813       *
814       * @param w the calling worker thread
815 +     * @param ec the count
816       */
817 <    private void eventSync(ForkJoinWorkerThread w) {
818 <        if (!w.active) {
819 <            int prev = w.lastEventCount;
820 <            long nextTop = (((long)prev << EVENT_COUNT_SHIFT) |
821 <                            ((long)(w.poolIndex + 1)));
822 <            long top;
823 <            while ((runState < SHUTDOWN || !tryTerminate(false)) &&
824 <                   (((int)(top = eventWaiters) & WAITER_INDEX_MASK) == 0 ||
825 <                    (int)(top >>> EVENT_COUNT_SHIFT) == prev) &&
782 <                   eventCount == prev) {
783 <                if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
784 <                                              w.nextWaiter = top, nextTop)) {
785 <                    accumulateStealCount(w); // transfer steals while idle
786 <                    Thread.interrupted();    // clear/ignore interrupt
787 <                    while (eventCount == prev)
788 <                        w.doPark();
817 >    private void awaitEvent(ForkJoinWorkerThread w, int ec) {
818 >        while (eventCount == ec) {
819 >            if (tryAccumulateStealCount(w)) { // transfer while idle
820 >                boolean untimed = (w.nextWaiter != 0L ||
821 >                                   (workerCounts & RUNNING_COUNT_MASK) <= 1);
822 >                long startTime = untimed? 0 : System.nanoTime();
823 >                Thread.interrupted();         // clear/ignore interrupt
824 >                if (eventCount != ec || !w.isRunning() ||
825 >                    runState >= TERMINATING)  // recheck after clear
826                      break;
827 +                if (untimed)
828 +                    LockSupport.park(w);
829 +                else {
830 +                    LockSupport.parkNanos(w, SHRINK_RATE_NANOS);
831 +                    if (eventCount != ec || !w.isRunning() ||
832 +                        runState >= TERMINATING)
833 +                        break;
834 +                    if (System.nanoTime() - startTime >= SHRINK_RATE_NANOS)
835 +                        tryShutdownWaiter(ec);
836                  }
837              }
792            w.lastEventCount = eventCount;
838          }
794        releaseWaiters();
839      }
840  
841      /**
842 <     * Callback from workers invoked upon each top-level action (i.e.,
843 <     * stealing a task or taking a submission and running
844 <     * it). Performs one or both of the following:
845 <     *
846 <     * * If the worker cannot find work, updates its active status to
847 <     * inactive and updates activeCount unless there is contention, in
848 <     * which case it may try again (either in this or a subsequent
849 <     * call).  Additionally, awaits the next task event and/or helps
850 <     * wake up other releasable waiters.
851 <     *
852 <     * * If there are too many running threads, suspends this worker
853 <     * (first forcing inactivation if necessary).  If it is not
854 <     * resumed before a keepAlive elapses, the worker may be "trimmed"
855 <     * -- killed while suspended within suspendAsSpare. Otherwise,
856 <     * upon resume it rechecks to make sure that it is still needed.
842 >     * Callback from the oldest waiter in awaitEvent waking up after a
843 >     * period of non-use. Tries (once) to shutdown an event waiter (or
844 >     * a spare, if one exists). Note that we don't need CAS or locks
845 >     * here because the method is called only from one thread
846 >     * occasionally waking (and even misfires are OK). Note that
847 >     * until the shutdown worker fully terminates, workerCounts
848 >     * will overestimate total count, which is tolerable.
849 >     *
850 >     * @param ec the event count waited on by caller (to abort
851 >     * attempt if count has since changed).
852 >     */
853 >    private void tryShutdownWaiter(int ec) {
854 >        if (spareWaiters != 0) { // prefer killing spares
855 >            tryShutdownSpare();
856 >            return;
857 >        }
858 >        ForkJoinWorkerThread[] ws = workers;
859 >        int n = ws.length;
860 >        long h = eventWaiters;
861 >        ForkJoinWorkerThread w; int id; long nh;
862 >        if (runState == 0 &&
863 >            submissionQueue.isEmpty() &&
864 >            eventCount == ec &&
865 >            (id = ((int)(h & WAITER_ID_MASK)) - 1) >= 0 &&
866 >            id < n && (w = ws[id]) != null &&
867 >            (nh = w.nextWaiter) != 0L && // keep at least one worker
868 >            UNSAFE.compareAndSwapLong(this, eventWaitersOffset, h, nh)) {
869 >            w.shutdown();
870 >            LockSupport.unpark(w);
871 >        }
872 >        releaseEventWaiters();
873 >    }
874 >
875 >    // Maintaining spares
876 >
877 >    /**
878 >     * Pushes worker onto the spare stack
879 >     */
880 >    final void pushSpare(ForkJoinWorkerThread w) {
881 >        int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex + 1);
882 >        do {} while (!UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
883 >                                               w.nextSpare = spareWaiters,ns));
884 >    }
885 >
886 >    /**
887 >     * Callback from oldest spare occasionally waking up.  Tries
888 >     * (once) to shutdown a spare. Same idea as tryShutdownWaiter.
889 >     */
890 >    final void tryShutdownSpare() {
891 >        int sw, id;
892 >        ForkJoinWorkerThread w;
893 >        ForkJoinWorkerThread[] ws;
894 >        if ((id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
895 >            id < (ws = workers).length && (w = ws[id]) != null &&
896 >            (workerCounts & RUNNING_COUNT_MASK) >= parallelism &&
897 >            UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
898 >                                     sw, w.nextSpare)) {
899 >            w.shutdown();
900 >            LockSupport.unpark(w);
901 >            advanceEventCount();
902 >        }
903 >    }
904 >
905 >    /**
906 >     * Tries (once) to resume a spare if worker counts match
907 >     * the given count.
908       *
909 <     * @param w the worker
815 <     * @param worked false if the worker scanned for work but didn't
816 <     * find any (in which case it may block waiting for work).
909 >     * @param wc workerCounts value on invocation of this method
910       */
911 <    final void preStep(ForkJoinWorkerThread w, boolean worked) {
912 <        boolean active = w.active;
913 <        boolean inactivate = !worked & active;
914 <        for (;;) {
915 <            if (inactivate) {
916 <                int c = runState;
917 <                if (UNSAFE.compareAndSwapInt(this, runStateOffset,
918 <                                             c, c - ONE_ACTIVE))
919 <                    inactivate = active = w.active = false;
920 <            }
921 <            int wc = workerCounts;
922 <            if ((wc & RUNNING_COUNT_MASK) <= parallelism) {
923 <                if (!worked)
924 <                    eventSync(w);
925 <                return;
911 >    private void tryResumeSpare(int wc) {
912 >        ForkJoinWorkerThread[] ws = workers;
913 >        int n = ws.length;
914 >        int sw, id, rs;  ForkJoinWorkerThread w;
915 >        if ((id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
916 >            id < n && (w = ws[id]) != null &&
917 >            (rs = runState) < TERMINATING &&
918 >            eventWaiters == 0L && workerCounts == wc) {
919 >            // In case all workers busy, heuristically back off to let settle
920 >            Thread.yield();
921 >            if (eventWaiters == 0L && runState == rs && // recheck
922 >                workerCounts == wc && spareWaiters == sw &&
923 >                UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
924 >                                         sw, w.nextSpare)) {
925 >                int c;              // increment running count before resume
926 >                do {} while(!UNSAFE.compareAndSwapInt
927 >                            (this, workerCountsOffset,
928 >                             c = workerCounts, c + ONE_RUNNING));
929 >                if (w.tryUnsuspend())
930 >                    LockSupport.unpark(w);
931 >                else               // back out if w was shutdown
932 >                    decrementWorkerCounts(ONE_RUNNING, 0);
933              }
834            if (!(inactivate |= active) &&  // must inactivate to suspend
835                UNSAFE.compareAndSwapInt(this, workerCountsOffset,
836                                         wc, wc - ONE_RUNNING) &&
837                !w.suspendAsSpare())        // false if trimmed
838                return;
934          }
935      }
936  
937 +    // adding workers on demand
938 +
939      /**
940 <     * Adjusts counts and creates or resumes compensating threads for
941 <     * a worker about to block on task joinMe, returning early if
845 <     * joinMe becomes ready. First tries resuming an existing spare
846 <     * (which usually also avoids any count adjustment), but must then
847 <     * decrement running count to determine whether a new thread is
848 <     * needed. See above for fuller explanation.
940 >     * Adds one or more workers if needed to establish target parallelism.
941 >     * Retries upon contention.
942       */
943 <    final void preJoin(ForkJoinTask<?> joinMe) {
944 <        boolean dec = false;       // true when running count decremented
945 <        for (;;) {
946 <            releaseWaiters();      // help other threads progress
947 <
948 <            if (joinMe.status < 0) // surround spare search with done checks
949 <                return;
950 <            ForkJoinWorkerThread spare = null;
858 <            for (ForkJoinWorkerThread w : workers) {
859 <                if (w != null && w.isSuspended()) {
860 <                    spare = w;
943 >    private void addWorkerIfBelowTarget() {
944 >        int pc = parallelism;
945 >        int wc;
946 >        while (((wc = workerCounts) >>> TOTAL_COUNT_SHIFT) < pc &&
947 >               runState < TERMINATING) {
948 >            if (UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
949 >                                         wc + (ONE_RUNNING|ONE_TOTAL))) {
950 >                if (addWorker() == null)
951                      break;
862                }
863            }
864            if (joinMe.status < 0)
865                return;
866
867            if (spare != null && spare.tryUnsuspend()) {
868                if (dec || joinMe.requestSignal() < 0) {
869                    int c;
870                    do {} while (!UNSAFE.compareAndSwapInt(this,
871                                                           workerCountsOffset,
872                                                           c = workerCounts,
873                                                           c + ONE_RUNNING));
874                } // else no net count change
875                LockSupport.unpark(spare);
876                return;
952              }
953 +        }
954 +    }
955  
956 <            int wc = workerCounts; // decrement running count
957 <            if (!dec && (wc & RUNNING_COUNT_MASK) != 0 &&
958 <                (dec = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
959 <                                                wc, wc -= ONE_RUNNING)) &&
960 <                joinMe.requestSignal() < 0) { // cannot block
961 <                int c;                        // back out
962 <                do {} while (!UNSAFE.compareAndSwapInt(this,
963 <                                                       workerCountsOffset,
964 <                                                       c = workerCounts,
965 <                                                       c + ONE_RUNNING));
966 <                return;
967 <            }
956 >    /**
957 >     * Tries (once) to add a new worker if all existing workers are
958 >     * busy, and there are either no running workers or the deficit is
959 >     * at least twice the surplus.
960 >     *
961 >     * @param wc workerCounts value on invocation of this method
962 >     */
963 >    private void tryAddWorkerIfBusy(int wc) {
964 >        int tc, rc, rs;
965 >        int pc = parallelism;
966 >        if ((tc = wc >>> TOTAL_COUNT_SHIFT) < MAX_WORKERS &&
967 >            ((rc = wc & RUNNING_COUNT_MASK) == 0 ||
968 >             rc < pc - ((tc - pc) << 1)) &&
969 >            (rs = runState) < TERMINATING &&
970 >            (rs & ACTIVE_COUNT_MASK) == tc) {
971 >            // Since all workers busy, heuristically back off to let settle
972 >            Thread.yield();
973 >            if (eventWaiters == 0L && spareWaiters == 0 && // recheck
974 >                runState == rs && workerCounts == wc &&
975 >                UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
976 >                                         wc + (ONE_RUNNING|ONE_TOTAL)))
977 >                addWorker();
978 >        }
979 >    }
980  
981 <            if (dec) {
982 <                int tc = wc >>> TOTAL_COUNT_SHIFT;
983 <                int pc = parallelism;
984 <                int dc = pc - (wc & RUNNING_COUNT_MASK); // deficit count
985 <                if ((dc < pc && (dc <= 0 || (dc * dc < (tc - pc) * pc) ||
986 <                                 !maintainsParallelism)) ||
987 <                    tc >= maxPoolSize) // cannot add
988 <                    return;
989 <                if (spare == null &&
990 <                    UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
991 <                                             wc + (ONE_RUNNING|ONE_TOTAL))) {
992 <                    addWorker();
993 <                    return;
994 <                }
995 <            }
981 >    /**
982 >     * Does at most one of:
983 >     *
984 >     * 1. Help wake up existing workers waiting for work via
985 >     *    releaseEventWaiters. (If any exist, then it doesn't
986 >     *    matter right now if under target parallelism level.)
987 >     *
988 >     * 2. If a spare exists, try (once) to resume it via tryResumeSpare.
989 >     *
990 >     * 3. If there are not enough total workers, add some
991 >     *    via addWorkerIfBelowTarget;
992 >     *
993 >     * 4. Try (once) to add a new worker if all existing workers
994 >     *     are busy, via tryAddWorkerIfBusy
995 >     */
996 >    private void helpMaintainParallelism() {
997 >        long h; int pc, wc;
998 >        if (((int)((h = eventWaiters) & WAITER_ID_MASK)) != 0) {
999 >            if ((int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
1000 >                releaseEventWaiters(); // avoid useless call
1001 >        }
1002 >        else if ((pc = parallelism) >
1003 >                 ((wc = workerCounts) & RUNNING_COUNT_MASK)) {
1004 >            if (spareWaiters != 0)
1005 >                tryResumeSpare(wc);
1006 >            else if ((wc >>> TOTAL_COUNT_SHIFT) < pc)
1007 >                addWorkerIfBelowTarget();
1008 >            else
1009 >                tryAddWorkerIfBusy(wc);
1010          }
1011      }
1012  
1013      /**
1014 <     * Same idea as preJoin but with too many differing details to
1015 <     * integrate: There are no task-based signal counts, and only one
1016 <     * way to do the actual blocking. So for simplicity it is directly
1017 <     * incorporated into this method.
1014 >     * Callback from workers invoked upon each top-level action (i.e.,
1015 >     * stealing a task or taking a submission and running it).
1016 >     * Performs one or more of the following:
1017 >     *
1018 >     * 1. If the worker is active, try to set its active status to
1019 >     *    inactive and update activeCount. On contention, we may try
1020 >     *    again on this or subsequent call.
1021 >     *
1022 >     * 2. Release any existing event waiters that are now relesable
1023 >     *
1024 >     * 3. If there are too many running threads, suspend this worker
1025 >     *    (first forcing inactive if necessary).  If it is not
1026 >     *    needed, it may be killed while suspended via
1027 >     *    tryShutdownSpare. Otherwise, upon resume it rechecks to make
1028 >     *    sure that it is still needed.
1029 >     *
1030 >     * 4. If more than 1 miss, await the next task event via
1031 >     *    eventSync (first forcing inactivation if necessary), upon
1032 >     *    which worker may also be killed, via tryShutdownWaiter.
1033 >     *
1034 >     * 5. Help reactivate other workers via helpMaintainParallelism
1035 >     *
1036 >     * @param w the worker
1037 >     * @param misses the number of scans by caller failing to find work
1038 >     * (saturating at 2 to avoid wraparound)
1039       */
1040 <    final void doBlock(ManagedBlocker blocker, boolean maintainPar)
1041 <        throws InterruptedException {
1042 <        maintainPar &= maintainsParallelism; // override
919 <        boolean dec = false;
920 <        boolean done = false;
1040 >    final void preStep(ForkJoinWorkerThread w, int misses) {
1041 >        boolean active = w.active;
1042 >        int pc = parallelism;
1043          for (;;) {
1044 <            releaseWaiters();
1045 <            if (done = blocker.isReleasable())
1046 <                break;
1047 <            ForkJoinWorkerThread spare = null;
1048 <            for (ForkJoinWorkerThread w : workers) {
1049 <                if (w != null && w.isSuspended()) {
1050 <                    spare = w;
1051 <                    break;
1052 <                }
1053 <            }
1054 <            if (done = blocker.isReleasable())
1055 <                break;
1056 <            if (spare != null && spare.tryUnsuspend()) {
1057 <                if (dec) {
1058 <                    int c;
1059 <                    do {} while (!UNSAFE.compareAndSwapInt(this,
1060 <                                                           workerCountsOffset,
1061 <                                                           c = workerCounts,
940 <                                                           c + ONE_RUNNING));
1044 >            int rs, wc, rc, ec; long h;
1045 >            if (active && UNSAFE.compareAndSwapInt(this, runStateOffset,
1046 >                                                   rs = runState, rs - 1))
1047 >                active = w.active = false;
1048 >            if (((int)((h = eventWaiters) & WAITER_ID_MASK)) != 0 &&
1049 >                (int)(h >>> EVENT_COUNT_SHIFT) != eventCount) {
1050 >                releaseEventWaiters();
1051 >                if (misses > 1)
1052 >                    continue;                  // clear before sync below
1053 >            }
1054 >            if ((rc = ((wc = workerCounts) & RUNNING_COUNT_MASK)) > pc) {
1055 >                if (!active &&                 // must inactivate to suspend
1056 >                    workerCounts == wc &&      // try to suspend as spare
1057 >                    UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1058 >                                             wc, wc - ONE_RUNNING)) {
1059 >                    w.suspendAsSpare();
1060 >                    if (!w.isRunning())
1061 >                        break;                 // was killed while spare
1062                  }
1063 <                LockSupport.unpark(spare);
943 <                break;
1063 >                continue;
1064              }
1065 <            int wc = workerCounts;
1066 <            if (!dec && (wc & RUNNING_COUNT_MASK) != 0)
1067 <                dec = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1068 <                                               wc, wc -= ONE_RUNNING);
1069 <            if (dec) {
1070 <                int tc = wc >>> TOTAL_COUNT_SHIFT;
1071 <                int pc = parallelism;
1072 <                int dc = pc - (wc & RUNNING_COUNT_MASK);
1073 <                if ((dc < pc && (dc <= 0 || (dc * dc < (tc - pc) * pc) ||
1074 <                                 !maintainPar)) ||
955 <                    tc >= maxPoolSize)
956 <                    break;
957 <                if (spare == null &&
958 <                    UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
959 <                                             wc + (ONE_RUNNING|ONE_TOTAL))){
960 <                    addWorker();
961 <                    break;
1065 >            if (misses > 0) {
1066 >                if ((ec = eventCount) == w.lastEventCount && misses > 1) {
1067 >                    if (!active) {             // must inactivate to sync
1068 >                        eventSync(w);
1069 >                        if (w.isRunning())
1070 >                            misses = 1;        // don't re-sync
1071 >                        else
1072 >                            break;             // was killed while waiting
1073 >                    }
1074 >                    continue;
1075                  }
1076 +                w.lastEventCount = ec;
1077              }
1078 <        }
1079 <
1080 <        try {
967 <            if (!done)
968 <                do {} while (!blocker.isReleasable() && !blocker.block());
969 <        } finally {
970 <            if (dec) {
971 <                int c;
972 <                do {} while (!UNSAFE.compareAndSwapInt(this,
973 <                                                       workerCountsOffset,
974 <                                                       c = workerCounts,
975 <                                                       c + ONE_RUNNING));
976 <            }
1078 >            if (rc < pc)
1079 >                helpMaintainParallelism();
1080 >            break;
1081          }
1082      }
1083  
1084      /**
1085 <     * Unless there are not enough other running threads, adjusts
1086 <     * counts for a a worker in performing helpJoin that cannot find
1087 <     * any work, so that this worker can now block.
1085 >     * Helps and/or blocks awaiting join of the given task.
1086 >     * Alternates between helpJoinTask() and helpMaintainParallelism()
1087 >     * as many times as there is a deficit in running count (or longer
1088 >     * if running count would become zero), then blocks if task still
1089 >     * not done.
1090       *
1091 <     * @return true if worker may block
1091 >     * @param joinMe the task to join
1092       */
1093 <    final boolean preBlockHelpingJoin(ForkJoinTask<?> joinMe) {
1093 >    final void awaitJoin(ForkJoinTask<?> joinMe, ForkJoinWorkerThread worker) {
1094 >        int threshold = parallelism;         // descend blocking thresholds
1095          while (joinMe.status >= 0) {
1096 <            releaseWaiters(); // help other threads progress
1097 <
1098 <            // if a spare exists, resume it to maintain parallelism level
992 <            if ((workerCounts & RUNNING_COUNT_MASK) <= parallelism) {
993 <                ForkJoinWorkerThread spare = null;
994 <                for (ForkJoinWorkerThread w : workers) {
995 <                    if (w != null && w.isSuspended()) {
996 <                        spare = w;
997 <                        break;
998 <                    }
999 <                }
1000 <                if (joinMe.status < 0)
1001 <                    break;
1002 <                if (spare != null) {
1003 <                    if (spare.tryUnsuspend()) {
1004 <                        boolean canBlock = true;
1005 <                        if (joinMe.requestSignal() < 0) {
1006 <                            canBlock = false; // already done
1007 <                            int c;
1008 <                            do {} while (!UNSAFE.compareAndSwapInt
1009 <                                         (this, workerCountsOffset,
1010 <                                          c = workerCounts, c + ONE_RUNNING));
1011 <                        }
1012 <                        LockSupport.unpark(spare);
1013 <                        return canBlock;
1014 <                    }
1015 <                    continue; // recheck -- another spare may exist
1016 <                }
1017 <            }
1018 <
1019 <            int wc = workerCounts; // reread to shorten CAS window
1020 <            int rc = wc & RUNNING_COUNT_MASK;
1021 <            if (rc <= 2) // keep this and at most one other thread alive
1096 >            boolean block; int wc;
1097 >            worker.helpJoinTask(joinMe);
1098 >            if (joinMe.status < 0)
1099                  break;
1100 <
1101 <            if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1102 <                                         wc, wc - ONE_RUNNING)) {
1103 <                if (joinMe.requestSignal() >= 0)
1104 <                    return true;
1105 <                int c;                        // back out
1100 >            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
1101 >                if (threshold > 0)
1102 >                    --threshold;
1103 >                else
1104 >                    advanceEventCount(); // force release
1105 >                block = false;
1106 >            }
1107 >            else
1108 >                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1109 >                                                 wc, wc - ONE_RUNNING);
1110 >            helpMaintainParallelism();
1111 >            if (block) {
1112 >                int c;
1113 >                joinMe.internalAwaitDone();
1114                  do {} while (!UNSAFE.compareAndSwapInt
1115                               (this, workerCountsOffset,
1116                                c = workerCounts, c + ONE_RUNNING));
1117                  break;
1118              }
1119          }
1120 <        return false;
1120 >    }
1121 >
1122 >    /**
1123 >     * Same idea as awaitJoin, but no helping
1124 >     */
1125 >    final void awaitBlocker(ManagedBlocker blocker)
1126 >        throws InterruptedException {
1127 >        int threshold = parallelism;
1128 >        while (!blocker.isReleasable()) {
1129 >            boolean block; int wc;
1130 >            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
1131 >                if (threshold > 0)
1132 >                    --threshold;
1133 >                else
1134 >                    advanceEventCount();
1135 >                block = false;
1136 >            }
1137 >            else
1138 >                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1139 >                                                 wc, wc - ONE_RUNNING);
1140 >            helpMaintainParallelism();
1141 >            if (block) {
1142 >                try {
1143 >                    do {} while (!blocker.isReleasable() && !blocker.block());
1144 >                } finally {
1145 >                    int c;
1146 >                    do {} while (!UNSAFE.compareAndSwapInt
1147 >                                 (this, workerCountsOffset,
1148 >                                  c = workerCounts, c + ONE_RUNNING));
1149 >                }
1150 >                break;
1151 >            }
1152 >        }
1153      }
1154  
1155      /**
# Line 1056 | Line 1173 | public class ForkJoinPool extends Abstra
1173          // Finish now if all threads terminated; else in some subsequent call
1174          if ((workerCounts >>> TOTAL_COUNT_SHIFT) == 0) {
1175              advanceRunLevel(TERMINATED);
1176 <            terminationLatch.countDown();
1176 >            termination.arrive();
1177          }
1178          return true;
1179      }
1180  
1181      /**
1182       * Actions on transition to TERMINATING
1183 +     *
1184 +     * Runs up to four passes through workers: (0) shutting down each
1185 +     * (without waking up if parked) to quickly spread notifications
1186 +     * without unnecessary bouncing around event queues etc (1) wake
1187 +     * up and help cancel tasks (2) interrupt (3) mop up races with
1188 +     * interrupted workers
1189       */
1190      private void startTerminating() {
1191 <        // Clear out and cancel submissions, ignoring exceptions
1191 >        cancelSubmissions();
1192 >        for (int passes = 0; passes < 4 && workerCounts != 0; ++passes) {
1193 >            advanceEventCount();
1194 >            eventWaiters = 0L; // clobber lists
1195 >            spareWaiters = 0;
1196 >            ForkJoinWorkerThread[] ws = workers;
1197 >            int n = ws.length;
1198 >            for (int i = 0; i < n; ++i) {
1199 >                ForkJoinWorkerThread w = ws[i];
1200 >                if (w != null) {
1201 >                    w.shutdown();
1202 >                    if (passes > 0 && !w.isTerminated()) {
1203 >                        w.cancelTasks();
1204 >                        LockSupport.unpark(w);
1205 >                        if (passes > 1) {
1206 >                            try {
1207 >                                w.interrupt();
1208 >                            } catch (SecurityException ignore) {
1209 >                            }
1210 >                        }
1211 >                    }
1212 >                }
1213 >            }
1214 >        }
1215 >    }
1216 >
1217 >    /**
1218 >     * Clear out and cancel submissions, ignoring exceptions
1219 >     */
1220 >    private void cancelSubmissions() {
1221          ForkJoinTask<?> task;
1222          while ((task = submissionQueue.poll()) != null) {
1223              try {
# Line 1073 | Line 1225 | public class ForkJoinPool extends Abstra
1225              } catch (Throwable ignore) {
1226              }
1227          }
1076        // Propagate run level
1077        for (ForkJoinWorkerThread w : workers) {
1078            if (w != null)
1079                w.shutdown();    // also resumes suspended workers
1080        }
1081        // Ensure no straggling local tasks
1082        for (ForkJoinWorkerThread w : workers) {
1083            if (w != null)
1084                w.cancelTasks();
1085        }
1086        // Wake up idle workers
1087        advanceEventCount();
1088        releaseWaiters();
1089        // Unstick pending joins
1090        for (ForkJoinWorkerThread w : workers) {
1091            if (w != null && !w.isTerminated()) {
1092                try {
1093                    w.interrupt();
1094                } catch (SecurityException ignore) {
1095                }
1096            }
1097        }
1228      }
1229  
1230      // misc support for ForkJoinWorkerThread
# Line 1107 | Line 1237 | public class ForkJoinPool extends Abstra
1237      }
1238  
1239      /**
1240 <     * Accumulates steal count from a worker, clearing
1241 <     * the worker's value
1240 >     * Tries to accumulates steal count from a worker, clearing
1241 >     * the worker's value.
1242 >     *
1243 >     * @return true if worker steal count now zero
1244       */
1245 <    final void accumulateStealCount(ForkJoinWorkerThread w) {
1245 >    final boolean tryAccumulateStealCount(ForkJoinWorkerThread w) {
1246          int sc = w.stealCount;
1247 <        if (sc != 0) {
1248 <            long c;
1249 <            w.stealCount = 0;
1250 <            do {} while (!UNSAFE.compareAndSwapLong(this, stealCountOffset,
1251 <                                                    c = stealCount, c + sc));
1247 >        long c = stealCount;
1248 >        // CAS even if zero, for fence effects
1249 >        if (UNSAFE.compareAndSwapLong(this, stealCountOffset, c, c + sc)) {
1250 >            if (sc != 0)
1251 >                w.stealCount = 0;
1252 >            return true;
1253          }
1254 +        return sc == 0;
1255      }
1256  
1257      /**
# Line 1125 | Line 1259 | public class ForkJoinPool extends Abstra
1259       * active thread.
1260       */
1261      final int idlePerActive() {
1262 +        int pc = parallelism; // use parallelism, not rc
1263          int ac = runState;    // no mask -- artifically boosts during shutdown
1129        int pc = parallelism; // use targeted parallelism, not rc
1264          // Use exact results for small values, saturate past 4
1265          return pc <= ac? 0 : pc >>> 1 <= ac? 1 : pc >>> 2 <= ac? 3 : pc >>> 3;
1266      }
# Line 1137 | Line 1271 | public class ForkJoinPool extends Abstra
1271  
1272      /**
1273       * Creates a {@code ForkJoinPool} with parallelism equal to {@link
1274 <     * java.lang.Runtime#availableProcessors}, and using the {@linkplain
1275 <     * #defaultForkJoinWorkerThreadFactory default thread factory}.
1274 >     * java.lang.Runtime#availableProcessors}, using the {@linkplain
1275 >     * #defaultForkJoinWorkerThreadFactory default thread factory},
1276 >     * no UncaughtExceptionHandler, and non-async LIFO processing mode.
1277       *
1278       * @throws SecurityException if a security manager exists and
1279       *         the caller is not permitted to modify threads
# Line 1147 | Line 1282 | public class ForkJoinPool extends Abstra
1282       */
1283      public ForkJoinPool() {
1284          this(Runtime.getRuntime().availableProcessors(),
1285 <             defaultForkJoinWorkerThreadFactory);
1285 >             defaultForkJoinWorkerThreadFactory, null, false);
1286      }
1287  
1288      /**
1289       * Creates a {@code ForkJoinPool} with the indicated parallelism
1290 <     * level and using the {@linkplain
1291 <     * #defaultForkJoinWorkerThreadFactory default thread factory}.
1290 >     * level, the {@linkplain
1291 >     * #defaultForkJoinWorkerThreadFactory default thread factory},
1292 >     * no UncaughtExceptionHandler, and non-async LIFO processing mode.
1293       *
1294       * @param parallelism the parallelism level
1295       * @throws IllegalArgumentException if parallelism less than or
# Line 1164 | Line 1300 | public class ForkJoinPool extends Abstra
1300       *         java.lang.RuntimePermission}{@code ("modifyThread")}
1301       */
1302      public ForkJoinPool(int parallelism) {
1303 <        this(parallelism, defaultForkJoinWorkerThreadFactory);
1303 >        this(parallelism, defaultForkJoinWorkerThreadFactory, null, false);
1304      }
1305  
1306      /**
1307 <     * Creates a {@code ForkJoinPool} with parallelism equal to {@link
1172 <     * java.lang.Runtime#availableProcessors}, and using the given
1173 <     * thread factory.
1307 >     * Creates a {@code ForkJoinPool} with the given parameters.
1308       *
1309 <     * @param factory the factory for creating new threads
1310 <     * @throws NullPointerException if the factory is null
1311 <     * @throws SecurityException if a security manager exists and
1312 <     *         the caller is not permitted to modify threads
1313 <     *         because it does not hold {@link
1314 <     *         java.lang.RuntimePermission}{@code ("modifyThread")}
1315 <     */
1316 <    public ForkJoinPool(ForkJoinWorkerThreadFactory factory) {
1317 <        this(Runtime.getRuntime().availableProcessors(), factory);
1318 <    }
1319 <
1320 <    /**
1321 <     * Creates a {@code ForkJoinPool} with the given parallelism and
1188 <     * thread factory.
1189 <     *
1190 <     * @param parallelism the parallelism level
1191 <     * @param factory the factory for creating new threads
1309 >     * @param parallelism the parallelism level. For default value,
1310 >     * use {@link java.lang.Runtime#availableProcessors}.
1311 >     * @param factory the factory for creating new threads. For default value,
1312 >     * use {@link #defaultForkJoinWorkerThreadFactory}.
1313 >     * @param handler the handler for internal worker threads that
1314 >     * terminate due to unrecoverable errors encountered while executing
1315 >     * tasks. For default value, use <code>null</code>.
1316 >     * @param asyncMode if true,
1317 >     * establishes local first-in-first-out scheduling mode for forked
1318 >     * tasks that are never joined. This mode may be more appropriate
1319 >     * than default locally stack-based mode in applications in which
1320 >     * worker threads only process event-style asynchronous tasks.
1321 >     * For default value, use <code>false</code>.
1322       * @throws IllegalArgumentException if parallelism less than or
1323       *         equal to zero, or greater than implementation limit
1324       * @throws NullPointerException if the factory is null
# Line 1197 | Line 1327 | public class ForkJoinPool extends Abstra
1327       *         because it does not hold {@link
1328       *         java.lang.RuntimePermission}{@code ("modifyThread")}
1329       */
1330 <    public ForkJoinPool(int parallelism, ForkJoinWorkerThreadFactory factory) {
1330 >    public ForkJoinPool(int parallelism,
1331 >                        ForkJoinWorkerThreadFactory factory,
1332 >                        Thread.UncaughtExceptionHandler handler,
1333 >                        boolean asyncMode) {
1334          checkPermission();
1335          if (factory == null)
1336              throw new NullPointerException();
1337 <        if (parallelism <= 0 || parallelism > MAX_THREADS)
1337 >        if (parallelism <= 0 || parallelism > MAX_WORKERS)
1338              throw new IllegalArgumentException();
1206        this.poolNumber = poolNumberGenerator.incrementAndGet();
1207        int arraySize = initialArraySizeFor(parallelism);
1339          this.parallelism = parallelism;
1340          this.factory = factory;
1341 <        this.maxPoolSize = MAX_THREADS;
1342 <        this.maintainsParallelism = true;
1341 >        this.ueh = handler;
1342 >        this.locallyFifo = asyncMode;
1343 >        int arraySize = initialArraySizeFor(parallelism);
1344          this.workers = new ForkJoinWorkerThread[arraySize];
1345          this.submissionQueue = new LinkedTransferQueue<ForkJoinTask<?>>();
1346          this.workerLock = new ReentrantLock();
1347 <        this.terminationLatch = new CountDownLatch(1);
1348 <        // Start first worker; remaining workers added upon first submission
1217 <        workerCounts = ONE_RUNNING | ONE_TOTAL;
1218 <        addWorker();
1347 >        this.termination = new Phaser(1);
1348 >        this.poolNumber = poolNumberGenerator.incrementAndGet();
1349      }
1350  
1351      /**
# Line 1223 | Line 1353 | public class ForkJoinPool extends Abstra
1353       * @param pc the initial parallelism level
1354       */
1355      private static int initialArraySizeFor(int pc) {
1356 <        // See Hackers Delight, sec 3.2. We know MAX_THREADS < (1 >>> 16)
1357 <        int size = pc < MAX_THREADS ? pc + 1 : MAX_THREADS;
1356 >        // See Hackers Delight, sec 3.2. We know MAX_WORKERS < (1 >>> 16)
1357 >        int size = pc < MAX_WORKERS ? pc + 1 : MAX_WORKERS;
1358          size |= size >>> 1;
1359          size |= size >>> 2;
1360          size |= size >>> 4;
# Line 1244 | Line 1374 | public class ForkJoinPool extends Abstra
1374              throw new RejectedExecutionException();
1375          submissionQueue.offer(task);
1376          advanceEventCount();
1377 <        releaseWaiters();
1377 >        if (eventWaiters != 0L)
1378 >            releaseEventWaiters();
1379          if ((workerCounts >>> TOTAL_COUNT_SHIFT) < parallelism)
1380 <            ensureEnoughTotalWorkers();
1380 >            addWorkerIfBelowTarget();
1381      }
1382  
1383      /**
# Line 1292 | Line 1423 | public class ForkJoinPool extends Abstra
1423      }
1424  
1425      /**
1426 +     * Submits a ForkJoinTask for execution.
1427 +     *
1428 +     * @param task the task to submit
1429 +     * @return the task
1430 +     * @throws NullPointerException if the task is null
1431 +     * @throws RejectedExecutionException if the task cannot be
1432 +     *         scheduled for execution
1433 +     */
1434 +    public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
1435 +        doSubmit(task);
1436 +        return task;
1437 +    }
1438 +
1439 +    /**
1440       * @throws NullPointerException if the task is null
1441       * @throws RejectedExecutionException if the task cannot be
1442       *         scheduled for execution
# Line 1329 | Line 1474 | public class ForkJoinPool extends Abstra
1474      }
1475  
1476      /**
1332     * Submits a ForkJoinTask for execution.
1333     *
1334     * @param task the task to submit
1335     * @return the task
1336     * @throws NullPointerException if the task is null
1337     * @throws RejectedExecutionException if the task cannot be
1338     *         scheduled for execution
1339     */
1340    public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
1341        doSubmit(task);
1342        return task;
1343    }
1344
1345    /**
1477       * @throws NullPointerException       {@inheritDoc}
1478       * @throws RejectedExecutionException {@inheritDoc}
1479       */
# Line 1384 | Line 1515 | public class ForkJoinPool extends Abstra
1515       * @return the handler, or {@code null} if none
1516       */
1517      public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() {
1387        workerCountReadFence();
1518          return ueh;
1519      }
1520  
1521      /**
1392     * Sets the handler for internal worker threads that terminate due
1393     * to unrecoverable errors encountered while executing tasks.
1394     * Unless set, the current default or ThreadGroup handler is used
1395     * as handler.
1396     *
1397     * @param h the new handler
1398     * @return the old handler, or {@code null} if none
1399     * @throws SecurityException if a security manager exists and
1400     *         the caller is not permitted to modify threads
1401     *         because it does not hold {@link
1402     *         java.lang.RuntimePermission}{@code ("modifyThread")}
1403     */
1404    public Thread.UncaughtExceptionHandler
1405        setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler h) {
1406        checkPermission();
1407        workerCountReadFence();
1408        Thread.UncaughtExceptionHandler old = ueh;
1409        if (h != old) {
1410            ueh = h;
1411            workerCountWriteFence();
1412            for (ForkJoinWorkerThread w : workers) {
1413                if (w != null)
1414                    w.setUncaughtExceptionHandler(h);
1415            }
1416        }
1417        return old;
1418    }
1419
1420    /**
1421     * Sets the target parallelism level of this pool.
1422     *
1423     * @param parallelism the target parallelism
1424     * @throws IllegalArgumentException if parallelism less than or
1425     * equal to zero or greater than maximum size bounds
1426     * @throws SecurityException if a security manager exists and
1427     *         the caller is not permitted to modify threads
1428     *         because it does not hold {@link
1429     *         java.lang.RuntimePermission}{@code ("modifyThread")}
1430     */
1431    public void setParallelism(int parallelism) {
1432        checkPermission();
1433        if (parallelism <= 0 || parallelism > maxPoolSize)
1434            throw new IllegalArgumentException();
1435        workerCountReadFence();
1436        int pc = this.parallelism;
1437        if (pc != parallelism) {
1438            this.parallelism = parallelism;
1439            workerCountWriteFence();
1440            // Release spares. If too many, some will die after re-suspend
1441            for (ForkJoinWorkerThread w : workers) {
1442                if (w != null && w.tryUnsuspend()) {
1443                    updateRunningCount(1);
1444                    LockSupport.unpark(w);
1445                }
1446            }
1447            ensureEnoughTotalWorkers();
1448            advanceEventCount();
1449            releaseWaiters(); // force config recheck by existing workers
1450        }
1451    }
1452
1453    /**
1522       * Returns the targeted parallelism level of this pool.
1523       *
1524       * @return the targeted parallelism level of this pool
1525       */
1526      public int getParallelism() {
1459        //        workerCountReadFence(); // inlined below
1460        int ignore = workerCounts;
1527          return parallelism;
1528      }
1529  
# Line 1474 | Line 1540 | public class ForkJoinPool extends Abstra
1540      }
1541  
1542      /**
1477     * Returns the maximum number of threads allowed to exist in the
1478     * pool. Unless set using {@link #setMaximumPoolSize}, the
1479     * maximum is an implementation-defined value designed only to
1480     * prevent runaway growth.
1481     *
1482     * @return the maximum
1483     */
1484    public int getMaximumPoolSize() {
1485        workerCountReadFence();
1486        return maxPoolSize;
1487    }
1488
1489    /**
1490     * Sets the maximum number of threads allowed to exist in the
1491     * pool. The given value should normally be greater than or equal
1492     * to the {@link #getParallelism parallelism} level. Setting this
1493     * value has no effect on current pool size. It controls
1494     * construction of new threads. The use of this method may cause
1495     * tasks that intrinsically require extra threads for dependent
1496     * computations to indefinitely stall. If you are instead trying
1497     * to minimize internal thread creation, consider setting {link
1498     * #setMaintainsParallelism} as false.
1499     *
1500     * @throws IllegalArgumentException if negative or greater than
1501     * internal implementation limit
1502     */
1503    public void setMaximumPoolSize(int newMax) {
1504        if (newMax < 0 || newMax > MAX_THREADS)
1505            throw new IllegalArgumentException();
1506        maxPoolSize = newMax;
1507        workerCountWriteFence();
1508    }
1509
1510    /**
1511     * Returns {@code true} if this pool dynamically maintains its
1512     * target parallelism level. If false, new threads are added only
1513     * to avoid possible starvation.  This setting is by default true.
1514     *
1515     * @return {@code true} if maintains parallelism
1516     */
1517    public boolean getMaintainsParallelism() {
1518        workerCountReadFence();
1519        return maintainsParallelism;
1520    }
1521
1522    /**
1523     * Sets whether this pool dynamically maintains its target
1524     * parallelism level. If false, new threads are added only to
1525     * avoid possible starvation.
1526     *
1527     * @param enable {@code true} to maintain parallelism
1528     */
1529    public void setMaintainsParallelism(boolean enable) {
1530        maintainsParallelism = enable;
1531        workerCountWriteFence();
1532    }
1533
1534    /**
1535     * Establishes local first-in-first-out scheduling mode for forked
1536     * tasks that are never joined. This mode may be more appropriate
1537     * than default locally stack-based mode in applications in which
1538     * worker threads only process asynchronous tasks.  This method is
1539     * designed to be invoked only when the pool is quiescent, and
1540     * typically only before any tasks are submitted. The effects of
1541     * invocations at other times may be unpredictable.
1542     *
1543     * @param async if {@code true}, use locally FIFO scheduling
1544     * @return the previous mode
1545     * @see #getAsyncMode
1546     */
1547    public boolean setAsyncMode(boolean async) {
1548        workerCountReadFence();
1549        boolean oldMode = locallyFifo;
1550        if (oldMode != async) {
1551            locallyFifo = async;
1552            workerCountWriteFence();
1553            for (ForkJoinWorkerThread w : workers) {
1554                if (w != null)
1555                    w.setAsyncMode(async);
1556            }
1557        }
1558        return oldMode;
1559    }
1560
1561    /**
1543       * Returns {@code true} if this pool uses local first-in-first-out
1544       * scheduling mode for forked tasks that are never joined.
1545       *
1546       * @return {@code true} if this pool uses async mode
1566     * @see #setAsyncMode
1547       */
1548      public boolean getAsyncMode() {
1569        workerCountReadFence();
1549          return locallyFifo;
1550      }
1551  
# Line 1635 | Line 1614 | public class ForkJoinPool extends Abstra
1614       */
1615      public long getQueuedTaskCount() {
1616          long count = 0;
1617 <        for (ForkJoinWorkerThread w : workers) {
1617 >        ForkJoinWorkerThread[] ws = workers;
1618 >        int n = ws.length;
1619 >        for (int i = 0; i < n; ++i) {
1620 >            ForkJoinWorkerThread w = ws[i];
1621              if (w != null)
1622                  count += w.getQueueSize();
1623          }
# Line 1692 | Line 1674 | public class ForkJoinPool extends Abstra
1674       * @return the number of elements transferred
1675       */
1676      protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
1677 <        int n = submissionQueue.drainTo(c);
1678 <        for (ForkJoinWorkerThread w : workers) {
1677 >        int count = submissionQueue.drainTo(c);
1678 >        ForkJoinWorkerThread[] ws = workers;
1679 >        int n = ws.length;
1680 >        for (int i = 0; i < n; ++i) {
1681 >            ForkJoinWorkerThread w = ws[i];
1682              if (w != null)
1683 <                n += w.drainTasksTo(c);
1683 >                count += w.drainTasksTo(c);
1684          }
1685 <        return n;
1685 >        return count;
1686      }
1687  
1688      /**
# Line 1821 | Line 1806 | public class ForkJoinPool extends Abstra
1806       */
1807      public boolean awaitTermination(long timeout, TimeUnit unit)
1808          throws InterruptedException {
1809 <        return terminationLatch.await(timeout, unit);
1809 >        try {
1810 >            return termination.awaitAdvanceInterruptibly(0, timeout, unit) > 0;
1811 >        } catch(TimeoutException ex) {
1812 >            return false;
1813 >        }
1814      }
1815  
1816      /**
1817       * Interface for extending managed parallelism for tasks running
1818       * in {@link ForkJoinPool}s.
1819       *
1820 <     * <p>A {@code ManagedBlocker} provides two methods.
1821 <     * Method {@code isReleasable} must return {@code true} if
1822 <     * blocking is not necessary. Method {@code block} blocks the
1823 <     * current thread if necessary (perhaps internally invoking
1824 <     * {@code isReleasable} before actually blocking).
1820 >     * <p>A {@code ManagedBlocker} provides two methods.  Method
1821 >     * {@code isReleasable} must return {@code true} if blocking is
1822 >     * not necessary. Method {@code block} blocks the current thread
1823 >     * if necessary (perhaps internally invoking {@code isReleasable}
1824 >     * before actually blocking). The unusual methods in this API
1825 >     * accommodate synchronizers that may, but don't usually, block
1826 >     * for long periods. Similarly, they allow more efficient internal
1827 >     * handling of cases in which additional workers may be, but
1828 >     * usually are not, needed to ensure sufficient parallelism.
1829 >     * Toward this end, implementations of method {@code isReleasable}
1830 >     * must be amenable to repeated invocation.
1831       *
1832       * <p>For example, here is a ManagedBlocker based on a
1833       * ReentrantLock:
# Line 1850 | Line 1845 | public class ForkJoinPool extends Abstra
1845       *     return hasLock || (hasLock = lock.tryLock());
1846       *   }
1847       * }}</pre>
1848 +     *
1849 +     * <p>Here is a class that possibly blocks waiting for an
1850 +     * item on a given queue:
1851 +     *  <pre> {@code
1852 +     * class QueueTaker<E> implements ManagedBlocker {
1853 +     *   final BlockingQueue<E> queue;
1854 +     *   volatile E item = null;
1855 +     *   QueueTaker(BlockingQueue<E> q) { this.queue = q; }
1856 +     *   public boolean block() throws InterruptedException {
1857 +     *     if (item == null)
1858 +     *       item = queue.take
1859 +     *     return true;
1860 +     *   }
1861 +     *   public boolean isReleasable() {
1862 +     *     return item != null || (item = queue.poll) != null;
1863 +     *   }
1864 +     *   public E getItem() { // call after pool.managedBlock completes
1865 +     *     return item;
1866 +     *   }
1867 +     * }}</pre>
1868       */
1869      public static interface ManagedBlocker {
1870          /**
# Line 1873 | Line 1888 | public class ForkJoinPool extends Abstra
1888       * Blocks in accord with the given blocker.  If the current thread
1889       * is a {@link ForkJoinWorkerThread}, this method possibly
1890       * arranges for a spare thread to be activated if necessary to
1891 <     * ensure parallelism while the current thread is blocked.
1877 <     *
1878 <     * <p>If {@code maintainParallelism} is {@code true} and the pool
1879 <     * supports it ({@link #getMaintainsParallelism}), this method
1880 <     * attempts to maintain the pool's nominal parallelism. Otherwise
1881 <     * it activates a thread only if necessary to avoid complete
1882 <     * starvation. This option may be preferable when blockages use
1883 <     * timeouts, or are almost always brief.
1891 >     * ensure sufficient parallelism while the current thread is blocked.
1892       *
1893       * <p>If the caller is not a {@link ForkJoinTask}, this method is
1894       * behaviorally equivalent to
# Line 1894 | Line 1902 | public class ForkJoinPool extends Abstra
1902       * first be expanded to ensure parallelism, and later adjusted.
1903       *
1904       * @param blocker the blocker
1897     * @param maintainParallelism if {@code true} and supported by
1898     * this pool, attempt to maintain the pool's nominal parallelism;
1899     * otherwise activate a thread only if necessary to avoid
1900     * complete starvation.
1905       * @throws InterruptedException if blocker.block did so
1906       */
1907 <    public static void managedBlock(ManagedBlocker blocker,
1904 <                                    boolean maintainParallelism)
1907 >    public static void managedBlock(ManagedBlocker blocker)
1908          throws InterruptedException {
1909          Thread t = Thread.currentThread();
1910 <        if (t instanceof ForkJoinWorkerThread)
1911 <            ((ForkJoinWorkerThread) t).pool.
1912 <                doBlock(blocker, maintainParallelism);
1913 <        else
1914 <            awaitBlocker(blocker);
1915 <    }
1916 <
1914 <    /**
1915 <     * Performs Non-FJ blocking
1916 <     */
1917 <    private static void awaitBlocker(ManagedBlocker blocker)
1918 <        throws InterruptedException {
1919 <        do {} while (!blocker.isReleasable() && !blocker.block());
1910 >        if (t instanceof ForkJoinWorkerThread) {
1911 >            ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
1912 >            w.pool.awaitBlocker(blocker);
1913 >        }
1914 >        else {
1915 >            do {} while (!blocker.isReleasable() && !blocker.block());
1916 >        }
1917      }
1918  
1919      // AbstractExecutorService overrides.  These rely on undocumented
# Line 1944 | Line 1941 | public class ForkJoinPool extends Abstra
1941          objectFieldOffset("eventWaiters",ForkJoinPool.class);
1942      private static final long stealCountOffset =
1943          objectFieldOffset("stealCount",ForkJoinPool.class);
1944 <
1944 >    private static final long spareWaitersOffset =
1945 >        objectFieldOffset("spareWaiters",ForkJoinPool.class);
1946  
1947      private static long objectFieldOffset(String field, Class<?> klazz) {
1948          try {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines