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

Comparing jsr166/src/main/java/util/concurrent/ForkJoinPool.java (file contents):
Revision 1.18 by dl, Wed Jul 7 20:41:24 2010 UTC vs.
Revision 1.19 by dl, Wed Aug 11 18:45:45 2010 UTC

# Line 50 | Line 50 | import java.util.concurrent.CountDownLat
50   * convenient form for informal monitoring.
51   *
52   * <p> As is the case with other ExecutorServices, there are three
53 < * main task execution methods summarized in the follwoing
53 > * main task execution methods summarized in the following
54   * table. These are designed to be used by clients not already engaged
55   * in fork/join computations in the current pool.  The main forms of
56   * these methods accept instances of {@code ForkJoinTask}, but
# Line 58 | Line 58 | import java.util.concurrent.CountDownLat
58   * Runnable}- or {@code Callable}- based activities as well.  However,
59   * tasks that are already executing in a pool should normally
60   * <em>NOT</em> use these pool execution methods, but instead use the
61 < * within-computation forms listed in the table. To avoid inadvertant
62 < * cyclic task dependencies and to improve performance, task
63 < * submissions to the current pool by an ongoing fork/join
64 < * computations may be implicitly translated to the corresponding
65 < * ForkJoinTask forms.
61 > * within-computation forms listed in the table.
62   *
63   * <table BORDER CELLPADDING=3 CELLSPACING=1>
64   *  <tr>
# Line 86 | Line 82 | import java.util.concurrent.CountDownLat
82   *    <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td>
83   *  </tr>
84   * </table>
85 < *
85 > *
86   * <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is
87   * used for all parallel task execution in a program or subsystem.
88   * Otherwise, use would not usually outweigh the construction and
# Line 111 | Line 107 | import java.util.concurrent.CountDownLat
107   * {@code IllegalArgumentException}.
108   *
109   * <p>This implementation rejects submitted tasks (that is, by throwing
110 < * {@link RejectedExecutionException}) only when the pool is shut down.
110 > * {@link RejectedExecutionException}) only when the pool is shut down
111 > * or internal resources have been exhuasted.
112   *
113   * @since 1.7
114   * @author Doug Lea
# Line 138 | Line 135 | public class ForkJoinPool extends Abstra
135       * of tasks profit from cache affinities, but others are harmed by
136       * cache pollution effects.)
137       *
138 +     * Beyond work-stealing support and essential bookkeeping, the
139 +     * main responsibility of this framework is to take actions when
140 +     * one worker is waiting to join a task stolen (or always held by)
141 +     * another.  Becauae we are multiplexing many tasks on to a pool
142 +     * of workers, we can't just let them block (as in Thread.join).
143 +     * We also cannot just reassign the joiner's run-time stack with
144 +     * another and replace it later, which would be a form of
145 +     * "continuation", that even if possible is not necessarily a good
146 +     * idea. Given that the creation costs of most threads on most
147 +     * systems mainly surrounds setting up runtime stacks, thread
148 +     * creation and switching is usually not much more expensive than
149 +     * stack creation and switching, and is more flexible). Instead we
150 +     * combine two tactics:
151 +     *
152 +     *   Helping: Arranging for the joiner to execute some task that it
153 +     *      would be running if the steal had not occurred.  Method
154 +     *      ForkJoinWorkerThread.helpJoinTask tracks joining->stealing
155 +     *      links to try to find such a task.
156 +     *
157 +     *   Compensating: Unless there are already enough live threads,
158 +     *      method helpMaintainParallelism() may create or or
159 +     *      re-activate a spare thread to compensate for blocked
160 +     *      joiners until they unblock.
161 +     *
162 +     * Because the determining existence of conservatively safe
163 +     * helping targets, the availability of already-created spares,
164 +     * and the apparent need to create new spares are all racy and
165 +     * require heuristic guidance, we rely on multiple retries of
166 +     * each. Further, because it is impossible to keep exactly the
167 +     * target (parallelism) number of threads running at any given
168 +     * time, we allow compensation during joins to fail, and enlist
169 +     * all other threads to help out whenever they are not otherwise
170 +     * occupied (i.e., mainly in method preStep).
171 +     *
172 +     * The ManagedBlocker extension API can't use helping so relies
173 +     * only on compensation in method awaitBlocker.
174 +     *
175       * The main throughput advantages of work-stealing stem from
176       * decentralized control -- workers mostly steal tasks from each
177       * other. We do not want to negate this by creating bottlenecks
178 <     * implementing the management responsibilities of this class. So
179 <     * we use a collection of techniques that avoid, reduce, or cope
180 <     * well with contention. These entail several instances of
181 <     * bit-packing into CASable fields to maintain only the minimally
182 <     * required atomicity. To enable such packing, we restrict maximum
183 <     * parallelism to (1<<15)-1 (enabling twice this to fit into a 16
184 <     * bit field), which is far in excess of normal operating range.
185 <     * Even though updates to some of these bookkeeping fields do
186 <     * sometimes contend with each other, they don't normally
187 <     * cache-contend with updates to others enough to warrant memory
188 <     * padding or isolation. So they are all held as fields of
189 <     * ForkJoinPool objects.  The main capabilities are as follows:
178 >     * implementing other management responsibilities. So we use a
179 >     * collection of techniques that avoid, reduce, or cope well with
180 >     * contention. These entail several instances of bit-packing into
181 >     * CASable fields to maintain only the minimally required
182 >     * atomicity. To enable such packing, we restrict maximum
183 >     * parallelism to (1<<15)-1 (enabling twice this (to accommodate
184 >     * unbalanced increments and decrements) to fit into a 16 bit
185 >     * field, which is far in excess of normal operating range.  Even
186 >     * though updates to some of these bookkeeping fields do sometimes
187 >     * contend with each other, they don't normally cache-contend with
188 >     * updates to others enough to warrant memory padding or
189 >     * isolation. So they are all held as fields of ForkJoinPool
190 >     * objects.  The main capabilities are as follows:
191       *
192       * 1. Creating and removing workers. Workers are recorded in the
193       * "workers" array. This is an array as opposed to some other data
# Line 168 | Line 203 | public class ForkJoinPool extends Abstra
203       * blocked workers. However, all other support code is set up to
204       * work with other policies.
205       *
206 +     * To ensure that we do not hold on to worker references that
207 +     * would prevent GC, ALL accesses to workers are via indices into
208 +     * the workers array (which is one source of some of the unusual
209 +     * code constructions here). In essence, the workers array serves
210 +     * as a WeakReference mechanism. Thus for example the event queue
211 +     * stores worker indices, not worker references. Access to the
212 +     * workers in associated methods (for example releaseEventWaiters)
213 +     * must both index-check and null-check the IDs. All such accesses
214 +     * ignore bad IDs by returning out early from what they are doing,
215 +     * since this can only be associated with shutdown, in which case
216 +     * it is OK to give up. On termination, we just clobber these
217 +     * data structures without trying to use them.
218 +     *
219       * 2. Bookkeeping for dynamically adding and removing workers. We
220       * aim to approximately maintain the given level of parallelism.
221       * When some workers are known to be blocked (on joins or via
# Line 177 | Line 225 | public class ForkJoinPool extends Abstra
225       * that are neither blocked nor artifically suspended) as well as
226       * the total number.  These two values are packed into one field,
227       * "workerCounts" because we need accurate snapshots when deciding
228 <     * to create, resume or suspend.  To support these decisions,
229 <     * updates to spare counts must be prospective (not
230 <     * retrospective).  For example, the running count is decremented
231 <     * before blocking by a thread about to block as a spare, but
184 <     * incremented by the thread about to unblock it. Updates upon
185 <     * resumption ofr threads blocking in awaitJoin or awaitBlocker
186 <     * cannot usually be prospective, so the running count is in
187 <     * general an upper bound of the number of productively running
188 <     * threads Updates to the workerCounts field sometimes transiently
189 <     * encounter a fair amount of contention when join dependencies
190 <     * are such that many threads block or unblock at about the same
191 <     * time. We alleviate this by sometimes performing an alternative
192 <     * action on contention like releasing waiters or locating spares.
228 >     * to create, resume or suspend.  Note however that the
229 >     * correspondance of these counts to reality is not guaranteed. In
230 >     * particular updates for unblocked threads may lag until they
231 >     * actually wake up.
232       *
233       * 3. Maintaining global run state. The run state of the pool
234       * consists of a runLevel (SHUTDOWN, TERMINATING, etc) similar to
# Line 218 | Line 257 | public class ForkJoinPool extends Abstra
257       * workers that previously could not find a task to now find one:
258       * Submission of a new task to the pool, or another worker pushing
259       * a task onto a previously empty queue.  (We also use this
260 <     * mechanism for termination and reconfiguration actions that
261 <     * require wakeups of idle workers).  Each worker maintains its
262 <     * last known event count, and blocks when a scan for work did not
263 <     * find a task AND its lastEventCount matches the current
264 <     * eventCount. Waiting idle workers are recorded in a variant of
265 <     * Treiber stack headed by field eventWaiters which, when nonzero,
266 <     * encodes the thread index and count awaited for by the worker
267 <     * thread most recently calling eventSync. This thread in turn has
268 <     * a record (field nextEventWaiter) for the next waiting worker.
269 <     * In addition to allowing simpler decisions about need for
270 <     * wakeup, the event count bits in eventWaiters serve the role of
271 <     * tags to avoid ABA errors in Treiber stacks.  To reduce delays
272 <     * in task diffusion, workers not otherwise occupied may invoke
273 <     * method releaseWaiters, that removes and signals (unparks)
274 <     * workers not waiting on current count. To minimize task
275 <     * production stalls associate with signalling, any worker pushing
276 <     * a task on an empty queue invokes the weaker method signalWork,
277 <     * that only releases idle workers until it detects interference
278 <     * by other threads trying to release, and lets them take
279 <     * over. The net effect is a tree-like diffusion of signals, where
280 <     * released threads (and possibly others) help with unparks.  To
281 <     * further reduce contention effects a bit, failed CASes to
260 >     * mechanism for termination actions that require wakeups of idle
261 >     * workers).  Each worker maintains its last known event count,
262 >     * and blocks when a scan for work did not find a task AND its
263 >     * lastEventCount matches the current eventCount. Waiting idle
264 >     * workers are recorded in a variant of Treiber stack headed by
265 >     * field eventWaiters which, when nonzero, encodes the thread
266 >     * index and count awaited for by the worker thread most recently
267 >     * calling eventSync. This thread in turn has a record (field
268 >     * nextEventWaiter) for the next waiting worker.  In addition to
269 >     * allowing simpler decisions about need for wakeup, the event
270 >     * count bits in eventWaiters serve the role of tags to avoid ABA
271 >     * errors in Treiber stacks.  To reduce delays in task diffusion,
272 >     * workers not otherwise occupied may invoke method
273 >     * releaseEventWaiters, that removes and signals (unparks) workers
274 >     * not waiting on current count. To reduce stalls, To minimize
275 >     * task production stalls associate with signalling, any worker
276 >     * pushing a task on an empty queue invokes the weaker method
277 >     * signalWork, that only releases idle workers until it detects
278 >     * interference by other threads trying to release, and lets them
279 >     * take over.  The net effect is a tree-like diffusion of signals,
280 >     * where released threads (and possibly others) help with unparks.
281 >     * To further reduce contention effects a bit, failed CASes to
282       * increment field eventCount are tolerated without retries.
283       * Conceptually they are merged into the same event, which is OK
284       * when their only purpose is to enable workers to scan for work.
# Line 247 | Line 286 | public class ForkJoinPool extends Abstra
286       * 5. Managing suspension of extra workers. When a worker is about
287       * to block waiting for a join (or via ManagedBlockers), we may
288       * create a new thread to maintain parallelism level, or at least
289 <     * avoid starvation (see below). Usually, extra threads are needed
290 <     * for only very short periods, yet join dependencies are such
291 <     * that we sometimes need them in bursts. Rather than create new
292 <     * threads each time this happens, we suspend no-longer-needed
293 <     * extra ones as "spares". For most purposes, we don't distinguish
294 <     * "extra" spare threads from normal "core" threads: On each call
295 <     * to preStep (the only point at which we can do this) a worker
289 >     * avoid starvation. Usually, extra threads are needed for only
290 >     * very short periods, yet join dependencies are such that we
291 >     * sometimes need them in bursts. Rather than create new threads
292 >     * each time this happens, we suspend no-longer-needed extra ones
293 >     * as "spares". For most purposes, we don't distinguish "extra"
294 >     * spare threads from normal "core" threads: On each call to
295 >     * preStep (the only point at which we can do this) a worker
296       * checks to see if there are now too many running workers, and if
297 <     * so, suspends itself.  Methods awaitJoin and awaitBlocker look
298 <     * for suspended threads to resume before considering creating a
299 <     * new replacement. We don't need a special data structure to
300 <     * maintain spares; simply scanning the workers array looking for
301 <     * worker.isSuspended() is fine because the calling thread is
263 <     * otherwise not doing anything useful anyway; we are at least as
264 <     * happy if after locating a spare, the caller doesn't actually
265 <     * block because the join is ready before we try to adjust and
266 <     * compensate.  Note that this is intrinsically racy.  One thread
297 >     * so, suspends itself.  Method helpMaintainParallelism looks for
298 >     * suspended threads to resume before considering creating a new
299 >     * replacement. The spares themselves are encoded on another
300 >     * variant of a Treiber Stack, headed at field "spareWaiters".
301 >     * Note that the use of spares is intrinsically racy.  One thread
302       * may become a spare at about the same time as another is
303       * needlessly being created. We counteract this and related slop
304       * in part by requiring resumed spares to immediately recheck (in
305 <     * preStep) to see whether they they should re-suspend. The only
306 <     * effective difference between "extra" and "core" threads is that
307 <     * we allow the "extra" ones to time out and die if they are not
308 <     * resumed within a keep-alive interval of a few seconds. This is
309 <     * implemented mainly within ForkJoinWorkerThread, but requires
310 <     * some coordination (isTrimmed() -- meaning killed while
311 <     * suspended) to correctly maintain pool counts.
305 >     * preStep) to see whether they they should re-suspend.  To avoid
306 >     * long-term build-up of spares, the oldest spare (see
307 >     * ForkJoinWorkerThread.suspendAsSpare) occasionally wakes up if
308 >     * not signalled and calls tryTrimSpare, which uses two different
309 >     * thresholds: Always killing if the number of spares is greater
310 >     * that 25% of total, and killing others only at a slower rate
311 >     * (UNUSED_SPARE_TRIM_RATE_NANOS).
312       *
313       * 6. Deciding when to create new workers. The main dynamic
314 <     * control in this class is deciding when to create extra threads,
315 <     * in methods awaitJoin and awaitBlocker. We always need to create
316 <     * one when the number of running threads becomes zero. But
317 <     * because blocked joins are typically dependent, we don't
318 <     * necessarily need or want one-to-one replacement. Instead, we
319 <     * use a combination of heuristics that adds threads only when the
320 <     * pool appears to be approaching starvation.  These effectively
321 <     * reduce churn at the price of systematically undershooting
322 <     * target parallelism when many threads are blocked.  However,
323 <     * biasing toward undeshooting partially compensates for the above
324 <     * mechanics to suspend extra threads, that normally lead to
325 <     * overshoot because we can only suspend workers in-between
326 <     * top-level actions. It also better copes with the fact that some
327 <     * of the methods in this class tend to never become compiled (but
328 <     * are interpreted), so some components of the entire set of
329 <     * controls might execute many times faster than others. And
330 <     * similarly for cases where the apparent lack of work is just due
331 <     * to GC stalls and other transient system activity.
314 >     * control in this class is deciding when to create extra threads
315 >     * in method helpMaintainParallelism. We would like to keep
316 >     * exactly #parallelism threads running, which is an impossble
317 >     * task. We always need to create one when the number of running
318 >     * threads would become zero and all workers are busy. Beyond
319 >     * this, we must rely on heuristics that work well in the the
320 >     * presence of transients phenomena such as GC stalls, dynamic
321 >     * compilation, and wake-up lags. These transients are extremely
322 >     * common -- we are normally trying to fully saturate the CPUs on
323 >     * a machine, so almost any activity other than running tasks
324 >     * impedes accuracy. Our main defense is to allow some slack in
325 >     * creation thresholds, using rules that reflect the fact that the
326 >     * more threads we have running, the more likely that we are
327 >     * underestimating the number running threads. The rules also
328 >     * better cope with the fact that some of the methods in this
329 >     * class tend to never become compiled (but are interpreted), so
330 >     * some components of the entire set of controls might execute 100
331 >     * times faster than others. And similarly for cases where the
332 >     * apparent lack of work is just due to GC stalls and other
333 >     * transient system activity.
334       *
335       * Beware that there is a lot of representation-level coupling
336       * among classes ForkJoinPool, ForkJoinWorkerThread, and
# Line 306 | Line 343 | public class ForkJoinPool extends Abstra
343       *
344       * Style notes: There are lots of inline assignments (of form
345       * "while ((local = field) != 0)") which are usually the simplest
346 <     * way to ensure read orderings. Also several occurrences of the
347 <     * unusual "do {} while(!cas...)" which is the simplest way to
348 <     * force an update of a CAS'ed variable. There are also a few
349 <     * other coding oddities that help some methods perform reasonably
350 <     * even when interpreted (not compiled).
346 >     * way to ensure the required read orderings (which are sometimes
347 >     * critical). Also several occurrences of the unusual "do {}
348 >     * while(!cas...)" which is the simplest way to force an update of
349 >     * a CAS'ed variable. There are also other coding oddities that
350 >     * help some methods perform reasonably even when interpreted (not
351 >     * compiled), at the expense of some messy constructions that
352 >     * reduce byte code counts.
353       *
354       * The order of declarations in this file is: (1) statics (2)
355       * fields (along with constants used when unpacking some of them)
# Line 378 | Line 417 | public class ForkJoinPool extends Abstra
417          new AtomicInteger();
418  
419      /**
420 <     * Absolute bound for parallelism level. Twice this number must
421 <     * fit into a 16bit field to enable word-packing for some counts.
420 >     * Absolute bound for parallelism level. Twice this number plus
421 >     * one (i.e., 0xfff) must fit into a 16bit field to enable
422 >     * word-packing for some counts and indices.
423       */
424 <    private static final int MAX_THREADS = 0x7fff;
424 >    private static final int MAX_WORKERS   = 0x7fff;
425  
426      /**
427       * Array holding all worker threads in the pool.  Array size must
# Line 421 | Line 461 | public class ForkJoinPool extends Abstra
461      private volatile long stealCount;
462  
463      /**
464 +     * The last nanoTime that a spare thread was trimmed
465 +     */
466 +    private volatile long trimTime;
467 +
468 +    /**
469 +     * The rate at which to trim unused spares
470 +     */
471 +    static final long UNUSED_SPARE_TRIM_RATE_NANOS =
472 +        1000L * 1000L * 1000L; // 1 sec
473 +
474 +    /**
475       * Encoded record of top of treiber stack of threads waiting for
476       * events. The top 32 bits contain the count being waited for. The
477 <     * bottom word contains one plus the pool index of waiting worker
478 <     * thread.
477 >     * bottom 16 bits contains one plus the pool index of waiting
478 >     * worker thread. (Bits 16-31 are unused.)
479       */
480      private volatile long eventWaiters;
481  
482      private static final int  EVENT_COUNT_SHIFT = 32;
483 <    private static final long WAITER_INDEX_MASK = (1L << EVENT_COUNT_SHIFT)-1L;
483 >    private static final long WAITER_ID_MASK    = (1L << 16) - 1L;
484  
485      /**
486       * A counter for events that may wake up worker threads:
487       *   - Submission of a new task to the pool
488       *   - A worker pushing a task on an empty queue
489 <     *   - termination and reconfiguration
489 >     *   - termination
490       */
491      private volatile int eventCount;
492  
493      /**
494 +     * Encoded record of top of treiber stack of spare threads waiting
495 +     * for resumption. The top 16 bits contain an arbitrary count to
496 +     * avoid ABA effects. The bottom 16bits contains one plus the pool
497 +     * index of waiting worker thread.
498 +     */
499 +    private volatile int spareWaiters;
500 +
501 +    private static final int SPARE_COUNT_SHIFT = 16;
502 +    private static final int SPARE_ID_MASK     = (1 << 16) - 1;
503 +
504 +    /**
505       * Lifecycle control. The low word contains the number of workers
506       * that are (probably) executing tasks. This value is atomically
507       * incremented before a worker gets a task to run, and decremented
# Line 468 | Line 530 | public class ForkJoinPool extends Abstra
530       * making decisions about creating and suspending spare
531       * threads. Updated only by CAS. Note that adding a new worker
532       * requires incrementing both counts, since workers start off in
533 <     * running state.  This field is also used for memory-fencing
472 <     * configuration parameters.
533 >     * running state.
534       */
535      private volatile int workerCounts;
536  
# Line 501 | Line 562 | public class ForkJoinPool extends Abstra
562       */
563      private final int poolNumber;
564  
565 <    // utilities for updating fields
565 >
566 >    // Utilities for CASing fields. Note that several of these
567 >    // are manually inlined by callers
568  
569      /**
570 <     * Increments running count.  Also used by ForkJoinTask.
570 >     * Increments running count part of workerCounts
571       */
572      final void incrementRunningCount() {
573          int c;
574          do {} while (!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
575 <                                               c = workerCounts,
575 >                                               c = workerCounts,
576                                                 c + ONE_RUNNING));
577      }
578 <    
578 >
579      /**
580       * Tries to decrement running count unless already zero
581       */
# Line 525 | Line 588 | public class ForkJoinPool extends Abstra
588      }
589  
590      /**
591 +     * Forces decrement of encoded workerCounts, awaiting nonzero if
592 +     * (rarely) necessary when other count updates lag.
593 +     *
594 +     * @param dr -- either zero or ONE_RUNNING
595 +     * @param dt == either zero or ONE_TOTAL
596 +     */
597 +    private void decrementWorkerCounts(int dr, int dt) {
598 +        for (;;) {
599 +            int wc = workerCounts;
600 +            if (wc == 0 && (runState & TERMINATED) != 0)
601 +                return; // lagging termination on a backout
602 +            if ((wc & RUNNING_COUNT_MASK)  - dr < 0 ||
603 +                (wc >>> TOTAL_COUNT_SHIFT) - dt < 0)
604 +                Thread.yield();
605 +            if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
606 +                                         wc, wc - (dr + dt)))
607 +                return;
608 +        }
609 +    }
610 +
611 +    /**
612 +     * Increments event count
613 +     */
614 +    private void advanceEventCount() {
615 +        int c;
616 +        do {} while(!UNSAFE.compareAndSwapInt(this, eventCountOffset,
617 +                                              c = eventCount, c+1));
618 +    }
619 +
620 +    /**
621       * Tries incrementing active count; fails on contention.
622       * Called by workers before executing tasks.
623       *
# Line 572 | Line 665 | public class ForkJoinPool extends Abstra
665          lock.lock();
666          try {
667              ForkJoinWorkerThread[] ws = workers;
668 <            int nws = ws.length;
669 <            if (k < 0 || k >= nws || ws[k] != null) {
670 <                for (k = 0; k < nws && ws[k] != null; ++k)
668 >            int n = ws.length;
669 >            if (k < 0 || k >= n || ws[k] != null) {
670 >                for (k = 0; k < n && ws[k] != null; ++k)
671                      ;
672 <                if (k == nws)
673 <                    ws = Arrays.copyOf(ws, nws << 1);
672 >                if (k == n)
673 >                    ws = Arrays.copyOf(ws, n << 1);
674              }
675              ws[k] = w;
676              workers = ws; // volatile array write ensures slot visibility
# Line 610 | Line 703 | public class ForkJoinPool extends Abstra
703       * Tries to create and add new worker. Assumes that worker counts
704       * are already updated to accommodate the worker, so adjusts on
705       * failure.
613     *
614     * @return new worker or null if creation failed
706       */
707 <    private ForkJoinWorkerThread addWorker() {
707 >    private void addWorker() {
708          ForkJoinWorkerThread w = null;
709          try {
710              w = factory.newThread(this);
711          } finally { // Adjust on either null or exceptional factory return
712              if (w == null) {
713 <                onWorkerCreationFailure();
714 <                return null;
713 >                decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL);
714 >                tryTerminate(false); // in case of failure during shutdown
715              }
716          }
717 <        w.start(recordWorker(w), ueh);
718 <        return w;
628 <    }
629 <
630 <    /**
631 <     * Adjusts counts upon failure to create worker
632 <     */
633 <    private void onWorkerCreationFailure() {
634 <        for (;;) {
635 <            int wc = workerCounts;
636 <            if ((wc >>> TOTAL_COUNT_SHIFT) > 0 &&
637 <                UNSAFE.compareAndSwapInt(this, workerCountsOffset,
638 <                                         wc, wc - (ONE_RUNNING|ONE_TOTAL)))
639 <                break;
640 <        }
641 <        tryTerminate(false); // in case of failure during shutdown
642 <    }
643 <
644 <    /**
645 <     * Create enough total workers to establish target parallelism,
646 <     * giving up if terminating or addWorker fails
647 <     */
648 <    private void ensureEnoughTotalWorkers() {
649 <        int wc;
650 <        while (((wc = workerCounts) >>> TOTAL_COUNT_SHIFT) < parallelism &&
651 <               runState < TERMINATING) {
652 <            if ((UNSAFE.compareAndSwapInt(this, workerCountsOffset,
653 <                                          wc, wc + (ONE_RUNNING|ONE_TOTAL)) &&
654 <                 addWorker() == null))
655 <                break;
656 <        }
717 >        if (w != null)
718 >            w.start(recordWorker(w), ueh);
719      }
720  
721      /**
722       * Final callback from terminating worker.  Removes record of
723       * worker from array, and adjusts counts. If pool is shutting
724 <     * down, tries to complete terminatation, else possibly replaces
663 <     * the worker.
724 >     * down, tries to complete terminatation.
725       *
726       * @param w the worker
727       */
728      final void workerTerminated(ForkJoinWorkerThread w) {
668        if (w.active) { // force inactive
669            w.active = false;
670            do {} while (!tryDecrementActiveCount());
671        }
729          forgetWorker(w);
730 <
731 <        // Decrement total count, and if was running, running count
732 <        // Spin (waiting for other updates) if either would be negative
733 <        int nr = w.isTrimmed() ? 0 : ONE_RUNNING;
677 <        int unit = ONE_TOTAL + nr;
678 <        for (;;) {
679 <            int wc = workerCounts;
680 <            int rc = wc & RUNNING_COUNT_MASK;
681 <            if (rc - nr < 0 || (wc >>> TOTAL_COUNT_SHIFT) == 0)
682 <                Thread.yield(); // back off if waiting for other updates
683 <            else if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
684 <                                              wc, wc - unit))
685 <                break;
686 <        }
687 <
688 <        accumulateStealCount(w); // collect final count
689 <        if (!tryTerminate(false))
690 <            ensureEnoughTotalWorkers();
730 >        decrementWorkerCounts(w.isTrimmed()? 0 : ONE_RUNNING, ONE_TOTAL);
731 >        while (w.stealCount != 0) // collect final count
732 >            tryAccumulateStealCount(w);
733 >        tryTerminate(false);
734      }
735  
736      // Waiting for and signalling events
737  
738      /**
739       * Releases workers blocked on a count not equal to current count.
740 +     * Normally called after precheck that eventWaiters isn't zero to
741 +     * avoid wasted array checks.
742 +     *
743 +     * @param signalling true if caller is a signalling worker so can
744 +     * exit upon (conservatively) detected contention by other threads
745 +     * who will continue to release
746       */
747 <    private void releaseWaiters() {
748 <        long top;
749 <        int id;
750 <        while ((id = (int)((top = eventWaiters) & WAITER_INDEX_MASK)) > 0 &&
751 <               (int)(top >>> EVENT_COUNT_SHIFT) != eventCount) {
752 <            ForkJoinWorkerThread[] ws = workers;
753 <            ForkJoinWorkerThread w;
754 <            if (ws.length >= id && (w = ws[id - 1]) != null &&
755 <                UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
756 <                                          top, w.nextWaiter))
747 >    private void releaseEventWaiters(boolean signalling) {
748 >        ForkJoinWorkerThread[] ws = workers;
749 >        int n = ws.length;
750 >        long h; // head of stack
751 >        ForkJoinWorkerThread w; int id, ec;
752 >        while ((id = ((int)((h = eventWaiters) & WAITER_ID_MASK)) - 1) >= 0 &&
753 >               (int)(h >>> EVENT_COUNT_SHIFT) != (ec = eventCount) &&
754 >               id < n && (w = ws[id]) != null) {
755 >            if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
756 >                                          h, h = w.nextWaiter))
757                  LockSupport.unpark(w);
758 +            if (signalling && (eventCount != ec || eventWaiters != h))
759 +                break;
760          }
761      }
762  
763      /**
764 <     * Ensures eventCount on exit is different (mod 2^32) than on
765 <     * entry and wakes up all waiters
764 >     * Tries to advance eventCount and releases waiters. Called only
765 >     * from workers.
766       */
767 <    private void signalEvent() {
768 <        int c;
769 <        do {} while (!UNSAFE.compareAndSwapInt(this, eventCountOffset,
770 <                                               c = eventCount, c+1));
771 <        releaseWaiters();
767 >    final void signalWork() {
768 >        int c; // try to increment event count -- CAS failure OK
769 >        UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
770 >        if (eventWaiters != 0L)
771 >            releaseEventWaiters(true);
772      }
773  
774      /**
775 <     * Advances eventCount and releases waiters until interference by
776 <     * other releasing threads is detected.
775 >     * Blocks worker until terminating or event count
776 >     * advances from last value held by worker
777 >     *
778 >     * @param w the calling worker thread
779       */
780 <    final void signalWork() {
781 <        // EventCount CAS failures are OK -- any change in count suffices.
782 <        int ec;
783 <        UNSAFE.compareAndSwapInt(this, eventCountOffset, ec=eventCount, ec+1);
784 <        outer:for (;;) {
785 <            long top = eventWaiters;
786 <            ec = eventCount;
787 <            for (;;) {
788 <                ForkJoinWorkerThread[] ws; ForkJoinWorkerThread w;
789 <                int id = (int)(top & WAITER_INDEX_MASK);
790 <                if (id <= 0 || (int)(top >>> EVENT_COUNT_SHIFT) == ec)
791 <                    return;
792 <                if ((ws = workers).length < id || (w = ws[id - 1]) == null ||
793 <                    !UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
794 <                                               top, top = w.nextWaiter))
795 <                    continue outer;      // possibly stale; reread
796 <                LockSupport.unpark(w);
797 <                if (top != eventWaiters) // let someone else take over
798 <                    return;
780 >    private void eventSync(ForkJoinWorkerThread w) {
781 >        int wec = w.lastEventCount;
782 >        long nh = (((long)wec) << EVENT_COUNT_SHIFT) | ((long)(w.poolIndex+1));
783 >        long h;
784 >        while ((runState < SHUTDOWN || !tryTerminate(false)) &&
785 >               ((h = eventWaiters) == 0L ||
786 >                (int)(h >>> EVENT_COUNT_SHIFT) == wec) &&
787 >               eventCount == wec) {
788 >            if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
789 >                                          w.nextWaiter = h, nh)) {
790 >                while (runState < TERMINATING && eventCount == wec) {
791 >                    if (!tryAccumulateStealCount(w))  // transfer while idle
792 >                        continue;
793 >                    Thread.interrupted();             // clear/ignore interrupt
794 >                    if (eventCount != wec)
795 >                        break;
796 >                    LockSupport.park(w);
797 >                }
798 >                break;
799              }
800          }
801 +        w.lastEventCount = eventCount;
802      }
803  
804 +    // Maintaining spares
805 +
806      /**
807 <     * If worker is inactive, blocks until terminating or event count
752 <     * advances from last value held by worker; in any case helps
753 <     * release others.
754 <     *
755 <     * @param w the calling worker thread
807 >     * Pushes worker onto the spare stack
808       */
809 <    private void eventSync(ForkJoinWorkerThread w) {
810 <        if (!w.active) {
811 <            int prev = w.lastEventCount;
812 <            long nextTop = (((long)prev << EVENT_COUNT_SHIFT) |
813 <                            ((long)(w.poolIndex + 1)));
814 <            long top;
815 <            while ((runState < SHUTDOWN || !tryTerminate(false)) &&
816 <                   (((int)(top = eventWaiters) & WAITER_INDEX_MASK) == 0 ||
817 <                    (int)(top >>> EVENT_COUNT_SHIFT) == prev) &&
818 <                   eventCount == prev) {
819 <                if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
820 <                                              w.nextWaiter = top, nextTop)) {
821 <                    accumulateStealCount(w); // transfer steals while idle
822 <                    Thread.interrupted();    // clear/ignore interrupt
823 <                    while (eventCount == prev)
824 <                        w.doPark();
825 <                    break;
826 <                }
809 >    final void pushSpare(ForkJoinWorkerThread w) {
810 >        int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex+1);
811 >        do {} while (!UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
812 >                                               w.nextSpare = spareWaiters,ns));
813 >    }
814 >
815 >    /**
816 >     * Tries (once) to resume a spare if running count is less than
817 >     * target parallelism. Fails on contention or stale workers.
818 >     */
819 >    private void tryResumeSpare() {
820 >        int sw, id;
821 >        ForkJoinWorkerThread w;
822 >        ForkJoinWorkerThread[] ws;
823 >        if ((id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
824 >            id < (ws = workers).length && (w = ws[id]) != null &&
825 >            (workerCounts & RUNNING_COUNT_MASK) < parallelism &&
826 >            eventWaiters == 0L &&
827 >            spareWaiters == sw &&
828 >            UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
829 >                                     sw, w.nextSpare) &&
830 >            w.tryUnsuspend()) {
831 >            int c; // try increment; if contended, finish after unpark
832 >            boolean inc = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
833 >                                                   c = workerCounts,
834 >                                                   c + ONE_RUNNING);
835 >            LockSupport.unpark(w);
836 >            if (!inc) {
837 >                do {} while(!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
838 >                                                      c = workerCounts,
839 >                                                      c + ONE_RUNNING));
840              }
776            w.lastEventCount = eventCount;
841          }
842 <        releaseWaiters();
842 >    }
843 >
844 >    /**
845 >     * Callback from oldest spare occasionally waking up.  Tries
846 >     * (once) to shutdown a spare if more than 25% spare overage, or
847 >     * if UNUSED_SPARE_TRIM_RATE_NANOS have elapsed and there are at
848 >     * least #parallelism running threads. Note that we don't need CAS
849 >     * or locks here because the method is called only from the oldest
850 >     * suspended spare occasionally waking (and even misfires are OK).
851 >     *
852 >     * @param now the wake up nanoTime of caller
853 >     */
854 >    final void tryTrimSpare(long now) {
855 >        long lastTrim = trimTime;
856 >        trimTime = now;
857 >        helpMaintainParallelism(); // first, help wake up any needed spares
858 >        int sw, id;
859 >        ForkJoinWorkerThread w;
860 >        ForkJoinWorkerThread[] ws;
861 >        int pc = parallelism;
862 >        int wc = workerCounts;
863 >        if ((wc & RUNNING_COUNT_MASK) >= pc &&
864 >            (((wc >>> TOTAL_COUNT_SHIFT) - pc) > (pc >>> 2) + 1 ||// approx 25%
865 >             now - lastTrim >= UNUSED_SPARE_TRIM_RATE_NANOS) &&
866 >            (id = ((sw = spareWaiters) & SPARE_ID_MASK) - 1) >= 0 &&
867 >            id < (ws = workers).length && (w = ws[id]) != null &&
868 >            UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
869 >                                     sw, w.nextSpare))
870 >            w.shutdown(false);
871 >    }
872 >
873 >    /**
874 >     * Does at most one of:
875 >     *
876 >     * 1. Help wake up existing workers waiting for work via
877 >     *    releaseEventWaiters. (If any exist, then it probably doesn't
878 >     *    matter right now if under target parallelism level.)
879 >     *
880 >     * 2. If below parallelism level and a spare exists, try (once)
881 >     *    to resume it via tryResumeSpare.
882 >     *
883 >     * 3. If neither of the above, tries (once) to add a new
884 >     *    worker if either there are not enough total, or if all
885 >     *    existing workers are busy, there are either no running
886 >     *    workers or the deficit is at least twice the surplus.
887 >     */
888 >    private void helpMaintainParallelism() {
889 >        // uglified to work better when not compiled
890 >        int pc, wc, rc, tc, rs; long h;
891 >        if ((h = eventWaiters) != 0L) {
892 >            if ((int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
893 >                releaseEventWaiters(false); // avoid useless call
894 >        }
895 >        else if ((pc = parallelism) >
896 >                 (rc = ((wc = workerCounts) & RUNNING_COUNT_MASK))) {
897 >            if (spareWaiters != 0)
898 >                tryResumeSpare();
899 >            else if ((rs = runState) < TERMINATING &&
900 >                     ((tc = wc >>> TOTAL_COUNT_SHIFT) < pc ||
901 >                      (tc == (rs & ACTIVE_COUNT_MASK) && // all busy
902 >                       (rc == 0 ||                       // must add
903 >                        rc < pc - ((tc - pc) << 1)) &&   // within slack
904 >                       tc < MAX_WORKERS && runState == rs)) && // recheck busy
905 >                     workerCounts == wc &&
906 >                     UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
907 >                                              wc + (ONE_RUNNING|ONE_TOTAL)))
908 >                addWorker();
909 >        }
910      }
911  
912      /**
913       * Callback from workers invoked upon each top-level action (i.e.,
914       * stealing a task or taking a submission and running
915 <     * it). Performs one or both of the following:
915 >     * it). Performs one or more of the following:
916 >     *
917 >     * 1. If the worker cannot find work (misses > 0), updates its
918 >     *    active status to inactive and updates activeCount unless
919 >     *    this is the first miss and there is contention, in which
920 >     *    case it may try again (either in this or a subsequent
921 >     *    call).
922 >     *
923 >     * 2. If there are at least 2 misses, awaits the next task event
924 >     *    via eventSync
925 >     *
926 >     * 3. If there are too many running threads, suspends this worker
927 >     *    (first forcing inactivation if necessary).  If it is not
928 >     *    needed, it may be killed while suspended via
929 >     *    tryTrimSpare. Otherwise, upon resume it rechecks to make
930 >     *    sure that it is still needed.
931       *
932 <     * * If the worker cannot find work, updates its active status to
933 <     * inactive and updates activeCount unless there is contention, in
788 <     * which case it may try again (either in this or a subsequent
789 <     * call).  Additionally, awaits the next task event and/or helps
790 <     * wake up other releasable waiters.
791 <     *
792 <     * * If there are too many running threads, suspends this worker
793 <     * (first forcing inactivation if necessary).  If it is not
794 <     * resumed before a keepAlive elapses, the worker may be "trimmed"
795 <     * -- killed while suspended within suspendAsSpare. Otherwise,
796 <     * upon resume it rechecks to make sure that it is still needed.
932 >     * 4. Helps release and/or reactivate other workers via
933 >     *    helpMaintainParallelism
934       *
935       * @param w the worker
936 <     * @param worked false if the worker scanned for work but didn't
937 <     * find any (in which case it may block waiting for work).
936 >     * @param misses the number of scans by caller failing to find work
937 >     * (saturating at 2 just to avoid wraparound)
938       */
939 <    final void preStep(ForkJoinWorkerThread w, boolean worked) {
939 >    final void preStep(ForkJoinWorkerThread w, int misses) {
940          boolean active = w.active;
941 <        boolean inactivate = !worked & active;
941 >        int pc = parallelism;
942          for (;;) {
943 <            if (inactivate) {
944 <                int rs = runState;
943 >            int wc = workerCounts;
944 >            int rc = wc & RUNNING_COUNT_MASK;
945 >            if (active && (misses > 0 || rc > pc)) {
946 >                int rs;                      // try inactivate
947                  if (UNSAFE.compareAndSwapInt(this, runStateOffset,
948 <                                             rs, rs - ONE_ACTIVE))
949 <                    inactivate = active = w.active = false;
948 >                                             rs = runState, rs - ONE_ACTIVE))
949 >                    active = w.active = false;
950 >                else if (misses > 1 || rc > pc ||
951 >                         (rs & ACTIVE_COUNT_MASK) >= pc)
952 >                    continue;                // force inactivate
953              }
954 <            int wc = workerCounts;
955 <            if ((wc & RUNNING_COUNT_MASK) <= parallelism) {
956 <                if (!worked)
957 <                    eventSync(w);
958 <                return;
954 >            if (misses > 1) {
955 >                misses = 0;                  // don't re-sync
956 >                eventSync(w);                // continue loop to recheck rc
957 >            }
958 >            else if (rc > pc) {
959 >                if (workerCounts == wc &&   // try to suspend as spare
960 >                    UNSAFE.compareAndSwapInt(this, workerCountsOffset,
961 >                                             wc, wc - ONE_RUNNING) &&
962 >                    !w.suspendAsSpare())    // false if killed
963 >                    break;
964 >            }
965 >            else {
966 >                if (rc < pc || eventWaiters != 0L)
967 >                    helpMaintainParallelism();
968 >                break;
969              }
818            if (!(inactivate |= active) &&  // must inactivate to suspend
819                UNSAFE.compareAndSwapInt(this, workerCountsOffset,
820                                         wc, wc - ONE_RUNNING) &&
821                !w.suspendAsSpare())        // false if trimmed
822                return;
970          }
971      }
972  
973      /**
974 <     * Tries to decrement running count, and if so, possibly creates
975 <     * or resumes compensating threads before blocking on task joinMe.
976 <     * This code is sprawled out with manual inlining to evade some
977 <     * JIT oddities.
974 >     * Helps and/or blocks awaiting join of the given task.
975 >     * Alternates between helpJoinTask() and helpMaintainParallelism()
976 >     * as many times as there is a deficit in running count (or longer
977 >     * if running count would become zero), then blocks if task still
978 >     * not done.
979       *
980       * @param joinMe the task to join
833     * @return task status on exit
981       */
982 <    final int tryAwaitJoin(ForkJoinTask<?> joinMe) {
983 <        int cw = workerCounts; // read now to spoil CAS if counts change as ...
984 <        releaseWaiters();      // ... a byproduct of releaseWaiters
985 <        int stat = joinMe.status;
986 <        if (stat >= 0 && // inline variant of tryDecrementRunningCount
987 <            (cw & RUNNING_COUNT_MASK) > 0 &&
988 <            UNSAFE.compareAndSwapInt(this, workerCountsOffset,
989 <                                     cw, cw - ONE_RUNNING)) {
990 <            int pc = parallelism;
991 <            int scans = 0;  // to require confirming passes to add threads
992 <            outer: while ((workerCounts & RUNNING_COUNT_MASK) < pc) {
993 <                if ((stat = joinMe.status) < 0)
994 <                    break;
995 <                ForkJoinWorkerThread spare = null;
996 <                ForkJoinWorkerThread[] ws = workers;
997 <                int nws = ws.length;
998 <                for (int i = 0; i < nws; ++i) {
999 <                    ForkJoinWorkerThread w = ws[i];
1000 <                    if (w != null && w.isSuspended()) {
1001 <                        spare = w;
1002 <                        break;
1003 <                    }
1004 <                }
1005 <                if ((stat = joinMe.status) < 0) // recheck to narrow race
1006 <                    break;
860 <                int wc = workerCounts;
861 <                int rc = wc & RUNNING_COUNT_MASK;
862 <                if (rc >= pc)
863 <                    break;
864 <                if (spare != null) {
865 <                    if (spare.tryUnsuspend()) {
866 <                        int c; // inline incrementRunningCount
867 <                        do {} while (!UNSAFE.compareAndSwapInt
868 <                                     (this, workerCountsOffset,
869 <                                      c = workerCounts, c + ONE_RUNNING));
870 <                        LockSupport.unpark(spare);
871 <                        break;
872 <                    }
873 <                    continue;
874 <                }
875 <                int tc = wc >>> TOTAL_COUNT_SHIFT;
876 <                int sc = tc - pc;
877 <                if (rc > 0) {
878 <                    int p = pc;
879 <                    int s = sc;
880 <                    while (s-- >= 0) { // try keeping 3/4 live
881 <                        if (rc > (p -= (p >>> 2) + 1))
882 <                            break outer;
883 <                    }
884 <                }
885 <                if (scans++ > sc && tc < MAX_THREADS &&
886 <                    UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
887 <                                             wc + (ONE_RUNNING|ONE_TOTAL))) {
888 <                    addWorker();
889 <                    break;
890 <                }
982 >    final void awaitJoin(ForkJoinTask<?> joinMe, ForkJoinWorkerThread worker) {
983 >        int threshold = parallelism;         // descend blocking thresholds
984 >        while (joinMe.status >= 0) {
985 >            boolean block; int wc;
986 >            worker.helpJoinTask(joinMe);
987 >            if (joinMe.status < 0)
988 >                break;
989 >            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
990 >                if (threshold > 0)
991 >                    --threshold;
992 >                else
993 >                    advanceEventCount(); // force release
994 >                block = false;
995 >            }
996 >            else
997 >                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
998 >                                                 wc, wc - ONE_RUNNING);
999 >            helpMaintainParallelism();
1000 >            if (block) {
1001 >                int c;
1002 >                joinMe.internalAwaitDone();
1003 >                do {} while (!UNSAFE.compareAndSwapInt
1004 >                             (this, workerCountsOffset,
1005 >                              c = workerCounts, c + ONE_RUNNING));
1006 >                break;
1007              }
892            if (stat >= 0)
893                stat = joinMe.internalAwaitDone();
894            int c; // inline incrementRunningCount
895            do {} while (!UNSAFE.compareAndSwapInt
896                         (this, workerCountsOffset,
897                          c = workerCounts, c + ONE_RUNNING));
1008          }
899        return stat;
1009      }
1010  
1011      /**
1012 <     * Same idea as (and mostly pasted from) tryAwaitJoin, but
904 <     * self-contained
1012 >     * Same idea as awaitJoin, but no helping
1013       */
1014      final void awaitBlocker(ManagedBlocker blocker)
1015          throws InterruptedException {
1016 <        for (;;) {
1017 <            if (blocker.isReleasable())
1018 <                return;
1019 <            int cw = workerCounts;
1020 <            releaseWaiters();
1021 <            if ((cw & RUNNING_COUNT_MASK) > 0 &&
1022 <                UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1023 <                                         cw, cw - ONE_RUNNING))
1024 <                break;
917 <        }
918 <        boolean done = false;
919 <        int pc = parallelism;
920 <        int scans = 0;
921 <        outer: while ((workerCounts & RUNNING_COUNT_MASK) < pc) {
922 <            if (done = blocker.isReleasable())
923 <                break;
924 <            ForkJoinWorkerThread spare = null;
925 <            ForkJoinWorkerThread[] ws = workers;
926 <            int nws = ws.length;
927 <            for (int i = 0; i < nws; ++i) {
928 <                ForkJoinWorkerThread w = ws[i];
929 <                if (w != null && w.isSuspended()) {
930 <                    spare = w;
931 <                    break;
932 <                }
1016 >        int threshold = parallelism;
1017 >        while (!blocker.isReleasable()) {
1018 >            boolean block; int wc;
1019 >            if (((wc = workerCounts) & RUNNING_COUNT_MASK) <= threshold) {
1020 >                if (threshold > 0)
1021 >                    --threshold;
1022 >                else
1023 >                    advanceEventCount();
1024 >                block = false;
1025              }
1026 <            if (done = blocker.isReleasable())
1027 <                break;
1028 <            int wc = workerCounts;
1029 <            int rc = wc & RUNNING_COUNT_MASK;
1030 <            if (rc >= pc)
1031 <                break;
1032 <            if (spare != null) {
1033 <                if (spare.tryUnsuspend()) {
1026 >            else
1027 >                block = UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1028 >                                                 wc, wc - ONE_RUNNING);
1029 >            helpMaintainParallelism();
1030 >            if (block) {
1031 >                try {
1032 >                    do {} while (!blocker.isReleasable() && !blocker.block());
1033 >                } finally {
1034                      int c;
1035                      do {} while (!UNSAFE.compareAndSwapInt
1036                                   (this, workerCountsOffset,
1037                                    c = workerCounts, c + ONE_RUNNING));
946                    LockSupport.unpark(spare);
947                    break;
948                }
949                continue;
950            }
951            int tc = wc >>> TOTAL_COUNT_SHIFT;
952            int sc = tc - pc;
953            if (rc > 0) {
954                int p = pc;
955                int s = sc;
956                while (s-- >= 0) {
957                    if (rc > (p -= (p >>> 2) + 1))
958                        break outer;
1038                  }
960            }
961            if (scans++ > sc && tc < MAX_THREADS &&
962                UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
963                                         wc + (ONE_RUNNING|ONE_TOTAL))) {
964                addWorker();
1039                  break;
1040              }
1041          }
1042 <        try {
969 <            if (!done)
970 <                do {} while (!blocker.isReleasable() &&
971 <                             !blocker.block());
972 <        } finally {
973 <            int c;
974 <            do {} while (!UNSAFE.compareAndSwapInt
975 <                         (this, workerCountsOffset,
976 <                          c = workerCounts, c + ONE_RUNNING));
977 <        }
978 <    }  
1042 >    }
1043  
1044      /**
1045       * Possibly initiates and/or completes termination.
# Line 1005 | Line 1069 | public class ForkJoinPool extends Abstra
1069  
1070      /**
1071       * Actions on transition to TERMINATING
1072 +     *
1073 +     * Runs up to four passes through workers: (0) shutting down each
1074 +     * quietly (without waking up if parked) to quickly spread
1075 +     * notifications without unnecessary bouncing around event queues
1076 +     * etc (1) wake up and help cancel tasks (2) interrupt (3) mop up
1077 +     * races with interrupted workers
1078       */
1079      private void startTerminating() {
1080 <        for (int i = 0; i < 2; ++i) { // twice to mop up newly created workers
1081 <            cancelSubmissions();
1082 <            shutdownWorkers();
1083 <            cancelWorkerTasks();
1084 <            signalEvent();
1085 <            interruptWorkers();
1080 >        cancelSubmissions();
1081 >        for (int passes = 0; passes < 4 && workerCounts != 0; ++passes) {
1082 >            advanceEventCount();
1083 >            eventWaiters = 0L; // clobber lists
1084 >            spareWaiters = 0;
1085 >            ForkJoinWorkerThread[] ws = workers;
1086 >            int n = ws.length;
1087 >            for (int i = 0; i < n; ++i) {
1088 >                ForkJoinWorkerThread w = ws[i];
1089 >                if (w != null) {
1090 >                    w.shutdown(true);
1091 >                    if (passes > 0 && !w.isTerminated()) {
1092 >                        w.cancelTasks();
1093 >                        LockSupport.unpark(w);
1094 >                        if (passes > 1) {
1095 >                            try {
1096 >                                w.interrupt();
1097 >                            } catch (SecurityException ignore) {
1098 >                            }
1099 >                        }
1100 >                    }
1101 >                }
1102 >            }
1103          }
1104      }
1105  
# Line 1029 | Line 1116 | public class ForkJoinPool extends Abstra
1116          }
1117      }
1118  
1032    /**
1033     * Sets all worker run states to at least shutdown,
1034     * also resuming suspended workers
1035     */
1036    private void shutdownWorkers() {
1037        ForkJoinWorkerThread[] ws = workers;
1038        int nws = ws.length;
1039        for (int i = 0; i < nws; ++i) {
1040            ForkJoinWorkerThread w = ws[i];
1041            if (w != null)
1042                w.shutdown();
1043        }
1044    }
1045
1046    /**
1047     * Clears out and cancels all locally queued tasks
1048     */
1049    private void cancelWorkerTasks() {
1050        ForkJoinWorkerThread[] ws = workers;
1051        int nws = ws.length;
1052        for (int i = 0; i < nws; ++i) {
1053            ForkJoinWorkerThread w = ws[i];
1054            if (w != null)
1055                w.cancelTasks();
1056        }
1057    }
1058
1059    /**
1060     * Unsticks all workers blocked on joins etc
1061     */
1062    private void interruptWorkers() {
1063        ForkJoinWorkerThread[] ws = workers;
1064        int nws = ws.length;
1065        for (int i = 0; i < nws; ++i) {
1066            ForkJoinWorkerThread w = ws[i];
1067            if (w != null && !w.isTerminated()) {
1068                try {
1069                    w.interrupt();
1070                } catch (SecurityException ignore) {
1071                }
1072            }
1073        }
1074    }
1075
1119      // misc support for ForkJoinWorkerThread
1120  
1121      /**
# Line 1083 | Line 1126 | public class ForkJoinPool extends Abstra
1126      }
1127  
1128      /**
1129 <     * Accumulates steal count from a worker, clearing
1130 <     * the worker's value
1129 >     * Tries to accumulates steal count from a worker, clearing
1130 >     * the worker's value.
1131 >     *
1132 >     * @return true if worker steal count now zero
1133       */
1134 <    final void accumulateStealCount(ForkJoinWorkerThread w) {
1134 >    final boolean tryAccumulateStealCount(ForkJoinWorkerThread w) {
1135          int sc = w.stealCount;
1136 <        if (sc != 0) {
1137 <            long c;
1138 <            w.stealCount = 0;
1139 <            do {} while (!UNSAFE.compareAndSwapLong(this, stealCountOffset,
1140 <                                                    c = stealCount, c + sc));
1136 >        long c = stealCount;
1137 >        // CAS even if zero, for fence effects
1138 >        if (UNSAFE.compareAndSwapLong(this, stealCountOffset, c, c + sc)) {
1139 >            if (sc != 0)
1140 >                w.stealCount = 0;
1141 >            return true;
1142          }
1143 +        return sc == 0;
1144      }
1145  
1146      /**
# Line 1101 | Line 1148 | public class ForkJoinPool extends Abstra
1148       * active thread.
1149       */
1150      final int idlePerActive() {
1151 <        int pc = parallelism; // use targeted parallelism, not rc
1151 >        int pc = parallelism; // use parallelism, not rc
1152          int ac = runState;    // no mask -- artifically boosts during shutdown
1153          // Use exact results for small values, saturate past 4
1154          return pc <= ac? 0 : pc >>> 1 <= ac? 1 : pc >>> 2 <= ac? 3 : pc >>> 3;
# Line 1152 | Line 1199 | public class ForkJoinPool extends Abstra
1199       * use {@link java.lang.Runtime#availableProcessors}.
1200       * @param factory the factory for creating new threads. For default value,
1201       * use {@link #defaultForkJoinWorkerThreadFactory}.
1202 <     * @param handler the handler for internal worker threads that
1203 <     * terminate due to unrecoverable errors encountered while executing
1202 >     * @param handler the handler for internal worker threads that
1203 >     * terminate due to unrecoverable errors encountered while executing
1204       * tasks. For default value, use <code>null</code>.
1205 <     * @param asyncMode if true,
1205 >     * @param asyncMode if true,
1206       * establishes local first-in-first-out scheduling mode for forked
1207       * tasks that are never joined. This mode may be more appropriate
1208       * than default locally stack-based mode in applications in which
# Line 1169 | Line 1216 | public class ForkJoinPool extends Abstra
1216       *         because it does not hold {@link
1217       *         java.lang.RuntimePermission}{@code ("modifyThread")}
1218       */
1219 <    public ForkJoinPool(int parallelism,
1219 >    public ForkJoinPool(int parallelism,
1220                          ForkJoinWorkerThreadFactory factory,
1221                          Thread.UncaughtExceptionHandler handler,
1222                          boolean asyncMode) {
1223          checkPermission();
1224          if (factory == null)
1225              throw new NullPointerException();
1226 <        if (parallelism <= 0 || parallelism > MAX_THREADS)
1226 >        if (parallelism <= 0 || parallelism > MAX_WORKERS)
1227              throw new IllegalArgumentException();
1228          this.parallelism = parallelism;
1229          this.factory = factory;
# Line 1188 | Line 1235 | public class ForkJoinPool extends Abstra
1235          this.workerLock = new ReentrantLock();
1236          this.termination = new Phaser(1);
1237          this.poolNumber = poolNumberGenerator.incrementAndGet();
1238 +        this.trimTime = System.nanoTime();
1239      }
1240  
1241      /**
# Line 1195 | Line 1243 | public class ForkJoinPool extends Abstra
1243       * @param pc the initial parallelism level
1244       */
1245      private static int initialArraySizeFor(int pc) {
1246 <        // See Hackers Delight, sec 3.2. We know MAX_THREADS < (1 >>> 16)
1247 <        int size = pc < MAX_THREADS ? pc + 1 : MAX_THREADS;
1246 >        // See Hackers Delight, sec 3.2. We know MAX_WORKERS < (1 >>> 16)
1247 >        int size = pc < MAX_WORKERS ? pc + 1 : MAX_WORKERS;
1248          size |= size >>> 1;
1249          size |= size >>> 2;
1250          size |= size >>> 4;
# Line 1214 | Line 1262 | public class ForkJoinPool extends Abstra
1262              throw new NullPointerException();
1263          if (runState >= SHUTDOWN)
1264              throw new RejectedExecutionException();
1265 <        // Convert submissions to current pool into forks
1266 <        Thread t = Thread.currentThread();
1267 <        ForkJoinWorkerThread w;
1220 <        if ((t instanceof ForkJoinWorkerThread) &&
1221 <            (w = (ForkJoinWorkerThread) t).pool == this)
1222 <            w.pushTask(task);
1223 <        else {
1224 <            submissionQueue.offer(task);
1225 <            signalEvent();
1226 <            ensureEnoughTotalWorkers();
1227 <        }
1265 >        submissionQueue.offer(task);
1266 >        advanceEventCount();
1267 >        helpMaintainParallelism();         // start or wake up workers
1268      }
1269  
1270      /**
1271       * Performs the given task, returning its result upon completion.
1272       * If the caller is already engaged in a fork/join computation in
1273 <     * the current pool, this method is equivalent in effect to
1273 >     * the current pool, this method is equivalent in effect to
1274       * {@link ForkJoinTask#invoke}.
1275       *
1276       * @param task the task
# Line 1247 | Line 1287 | public class ForkJoinPool extends Abstra
1287      /**
1288       * Arranges for (asynchronous) execution of the given task.
1289       * If the caller is already engaged in a fork/join computation in
1290 <     * the current pool, this method is equivalent in effect to
1290 >     * the current pool, this method is equivalent in effect to
1291       * {@link ForkJoinTask#fork}.
1292       *
1293       * @param task the task
# Line 1278 | Line 1318 | public class ForkJoinPool extends Abstra
1318      /**
1319       * Submits a ForkJoinTask for execution.
1320       * If the caller is already engaged in a fork/join computation in
1321 <     * the current pool, this method is equivalent in effect to
1321 >     * the current pool, this method is equivalent in effect to
1322       * {@link ForkJoinTask#fork}.
1323       *
1324       * @param task the task to submit
# Line 1471 | Line 1511 | public class ForkJoinPool extends Abstra
1511      public long getQueuedTaskCount() {
1512          long count = 0;
1513          ForkJoinWorkerThread[] ws = workers;
1514 <        int nws = ws.length;
1515 <        for (int i = 0; i < nws; ++i) {
1514 >        int n = ws.length;
1515 >        for (int i = 0; i < n; ++i) {
1516              ForkJoinWorkerThread w = ws[i];
1517              if (w != null)
1518                  count += w.getQueueSize();
# Line 1530 | Line 1570 | public class ForkJoinPool extends Abstra
1570       * @return the number of elements transferred
1571       */
1572      protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
1573 <        int n = submissionQueue.drainTo(c);
1534 <        ForkJoinWorkerThread[] ws = workers;
1535 <        int nws = ws.length;
1536 <        for (int i = 0; i < nws; ++i) {
1537 <            ForkJoinWorkerThread w = ws[i];
1538 <            if (w != null)
1539 <                n += w.drainTasksTo(c);
1540 <        }
1541 <        return n;
1542 <    }
1543 <
1544 <    /**
1545 <     * Returns count of total parks by existing workers.
1546 <     * Used during development only since not meaningful to users.
1547 <     */
1548 <    private int collectParkCount() {
1549 <        int count = 0;
1573 >        int count = submissionQueue.drainTo(c);
1574          ForkJoinWorkerThread[] ws = workers;
1575 <        int nws = ws.length;
1576 <        for (int i = 0; i < nws; ++i) {
1575 >        int n = ws.length;
1576 >        for (int i = 0; i < n; ++i) {
1577              ForkJoinWorkerThread w = ws[i];
1578              if (w != null)
1579 <                count += w.parkCount;
1579 >                count += w.drainTasksTo(c);
1580          }
1581          return count;
1582      }
# Line 1574 | Line 1598 | public class ForkJoinPool extends Abstra
1598          int pc = parallelism;
1599          int rs = runState;
1600          int ac = rs & ACTIVE_COUNT_MASK;
1577        //        int pk = collectParkCount();
1601          return super.toString() +
1602              "[" + runLevelToString(rs) +
1603              ", parallelism = " + pc +
# Line 1584 | Line 1607 | public class ForkJoinPool extends Abstra
1607              ", steals = " + st +
1608              ", tasks = " + qt +
1609              ", submissions = " + qs +
1587            //            ", parks = " + pk +
1610              "]";
1611      }
1612  
# Line 1691 | Line 1713 | public class ForkJoinPool extends Abstra
1713       * Interface for extending managed parallelism for tasks running
1714       * in {@link ForkJoinPool}s.
1715       *
1716 <     * <p>A {@code ManagedBlocker} provides two methods.
1717 <     * Method {@code isReleasable} must return {@code true} if
1718 <     * blocking is not necessary. Method {@code block} blocks the
1719 <     * current thread if necessary (perhaps internally invoking
1720 <     * {@code isReleasable} before actually blocking).
1716 >     * <p>A {@code ManagedBlocker} provides two methods.  Method
1717 >     * {@code isReleasable} must return {@code true} if blocking is
1718 >     * not necessary. Method {@code block} blocks the current thread
1719 >     * if necessary (perhaps internally invoking {@code isReleasable}
1720 >     * before actually blocking). The unusual methods in this API
1721 >     * accommodate synchronizers that may, but don't usually, block
1722 >     * for long periods. Similarly, they allow more efficient internal
1723 >     * handling of cases in which additional workers may be, but
1724 >     * usually are not, needed to ensure sufficient parallelism.
1725 >     * Toward this end, implementations of method {@code isReleasable}
1726 >     * must be amenable to repeated invocation.
1727       *
1728       * <p>For example, here is a ManagedBlocker based on a
1729       * ReentrantLock:
# Line 1713 | Line 1741 | public class ForkJoinPool extends Abstra
1741       *     return hasLock || (hasLock = lock.tryLock());
1742       *   }
1743       * }}</pre>
1744 +     *
1745 +     * <p>Here is a class that possibly blocks waiting for an
1746 +     * item on a given queue:
1747 +     *  <pre> {@code
1748 +     * class QueueTaker<E> implements ManagedBlocker {
1749 +     *   final BlockingQueue<E> queue;
1750 +     *   volatile E item = null;
1751 +     *   QueueTaker(BlockingQueue<E> q) { this.queue = q; }
1752 +     *   public boolean block() throws InterruptedException {
1753 +     *     if (item == null)
1754 +     *       item = queue.take
1755 +     *     return true;
1756 +     *   }
1757 +     *   public boolean isReleasable() {
1758 +     *     return item != null || (item = queue.poll) != null;
1759 +     *   }
1760 +     *   public E getItem() { // call after pool.managedBlock completes
1761 +     *     return item;
1762 +     *   }
1763 +     * }}</pre>
1764       */
1765      public static interface ManagedBlocker {
1766          /**
# Line 1755 | Line 1803 | public class ForkJoinPool extends Abstra
1803      public static void managedBlock(ManagedBlocker blocker)
1804          throws InterruptedException {
1805          Thread t = Thread.currentThread();
1806 <        if (t instanceof ForkJoinWorkerThread)
1807 <            ((ForkJoinWorkerThread) t).pool.awaitBlocker(blocker);
1806 >        if (t instanceof ForkJoinWorkerThread) {
1807 >            ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
1808 >            w.pool.awaitBlocker(blocker);
1809 >        }
1810          else {
1811              do {} while (!blocker.isReleasable() && !blocker.block());
1812          }
# Line 1787 | Line 1837 | public class ForkJoinPool extends Abstra
1837          objectFieldOffset("eventWaiters",ForkJoinPool.class);
1838      private static final long stealCountOffset =
1839          objectFieldOffset("stealCount",ForkJoinPool.class);
1840 <
1840 >    private static final long spareWaitersOffset =
1841 >        objectFieldOffset("spareWaiters",ForkJoinPool.class);
1842  
1843      private static long objectFieldOffset(String field, Class<?> klazz) {
1844          try {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines