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.354 by dl, Sat May 11 11:02:57 2019 UTC vs.
Revision 1.355 by dl, Fri Jan 17 18:12:07 2020 UTC

# Line 16 | Line 16 | import java.security.Permissions;
16   import java.security.PrivilegedAction;
17   import java.security.ProtectionDomain;
18   import java.util.ArrayList;
19 + import java.util.Arrays;
20 + import java.util.Iterator;
21   import java.util.Collection;
22   import java.util.Collections;
23   import java.util.List;
24   import java.util.function.Predicate;
25   import java.util.concurrent.locks.LockSupport;
26 + import java.util.concurrent.locks.ReentrantLock;
27 + import java.util.concurrent.locks.Condition;
28  
29   /**
30   * An {@link ExecutorService} for running {@link ForkJoinTask}s.
# Line 201 | Line 205 | public class ForkJoinPool extends Abstra
205       * in a circular buffer:
206       *    q.array[q.top++ % length] = task;
207       *
208 <     * (The actual code needs to null-check and size-check the array,
208 >     * The actual code needs to null-check and size-check the array,
209       * uses masking, not mod, for indexing a power-of-two-sized array,
210 <     * adds a release fence for publication, and possibly signals
211 <     * waiting workers to start scanning -- see below.)  Both a
212 <     * successful pop and poll mainly entail a CAS of a slot from
213 <     * non-null to null.
214 <     *
215 <     * The pop operation (always performed by owner) is:
216 <     *   if ((the task at top slot is not null) and
217 <     *        (CAS slot to null))
218 <     *           decrement top and return task;
219 <     *
220 <     * And the poll operation (usually by a stealer) is
221 <     *    if ((the task at base slot is not null) and
222 <     *        (CAS slot to null))
223 <     *           increment base and return task;
224 <     *
225 <     * There are several variants of each of these. Most uses occur
226 <     * within operations that also interleave contention or emptiness
227 <     * tracking or inspection of elements before extracting them, so
228 <     * must interleave these with the above code. When performed by
229 <     * owner, getAndSet is used instead of CAS (see for example method
230 <     * nextLocalTask) which is usually more efficient, and possible
231 <     * because the top index cannot independently change during the
232 <     * operation.
210 >     * enforces memory ordering, supports resizing, and possibly
211 >     * signals waiting workers to start scanning -- see below.
212 >     *
213 >     * The pop operation (always performed by owner) is of the form:
214 >     *   if ((task = getAndSet(q.array, (q.top-1) % length, null)) != null)
215 >     *        decrement top and return task;
216 >     * If this fails, the queue is empty.
217 >     *
218 >     * The poll operation by another stealer thread is, basically:
219 >     *   if (CAS nonnull task at q.array[q.base % length] to null)
220 >     *       increment base and return task;
221 >     *
222 >     * This may fail due to contention, and may be retried.
223 >     * Implementations must ensure a consistent snapshot of the base
224 >     * index and the task (by looping or trying elsewhere) before
225 >     * trying CAS.  There isn't actually a method of this form,
226 >     * because failure due to inconsistency or contention is handled
227 >     * in different ways in different contexts, normally by first
228 >     * trying other queues. (For the most straightforward example, see
229 >     * method pollScan.) There are further variants for cases
230 >     * requiring inspection of elements before extracting them, so
231 >     * must interleave these with variants of this code.  Also, a more
232 >     * efficient version (nextLocalTask) is used for polls by owners.
233 >     * It avoids some overhead because the queue cannot be growing
234 >     * during call.
235       *
236       * Memory ordering.  See "Correct and Efficient Work-Stealing for
237       * Weak Memory Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013
238       * (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an
239       * analysis of memory ordering requirements in work-stealing
240 <     * algorithms similar to (but different than) the one used here.
241 <     * Extracting tasks in array slots via (fully fenced) CAS provides
242 <     * primary synchronization. The base and top indices imprecisely
243 <     * guide where to extract from. We do not usually require strict
244 <     * orderings of array and index updates. Many index accesses use
245 <     * plain mode, with ordering constrained by surrounding context
246 <     * (usually with respect to element CASes or the two WorkQueue
247 <     * volatile fields source and phase). When not otherwise already
248 <     * constrained, reads of "base" by queue owners use acquire-mode,
249 <     * and some externally callable methods preface accesses with
250 <     * acquire fences.  Additionally, to ensure that index update
251 <     * writes are not coalesced or postponed in loops etc, "opaque"
252 <     * mode is used in a few cases where timely writes are not
253 <     * otherwise ensured. The "locked" versions of push- and pop-
254 <     * based methods for shared queues differ from owned versions
255 <     * because locking already forces some of the ordering.
240 >     * algorithms similar to the one used here.  Inserting and
241 >     * extracting tasks in array slots via volatile or atomic accesses
242 >     * or explicit fences provides primary synchronization.
243 >     *
244 >     * Operations on deque elements require reads and writes of both
245 >     * indices and slots. When possible, we allow these to occur in
246 >     * any order.  Because the base and top indices (along with other
247 >     * pool or array fields accessed in many methods) only imprecisely
248 >     * guide where to extract from, we let accesses other than the
249 >     * element getAndSet/CAS/setVolatile appear in any order, using
250 >     * plain mode. But we must still preface some methods (mainly
251 >     * those that may be accessed externally) with an acquireFence to
252 >     * avoid unbounded staleness. We use explicit acquiring reads
253 >     * (getSlot) rather than plain array access when acquire mode is
254 >     * required but not otherwise ensured by context. To reduce stalls
255 >     * by other stealers, we encourage timely writes to the base index
256 >     * by immediately following updates with a write of a volatile
257 >     * field that must be updated anyway, or an Opaque-mode write if
258 >     * there is no such opportunity.
259       *
260       * Because indices and slot contents cannot always be consistent,
261 <     * a check that base == top indicates (momentary) emptiness, but
262 <     * otherwise may err on the side of possibly making the queue
263 <     * appear nonempty when a push, pop, or poll have not fully
264 <     * committed, or making it appear empty when an update of top has
265 <     * not yet been visibly written.  (Method isEmpty() checks the
266 <     * case of a partially completed removal of the last element.)
267 <     * Because of this, the poll operation, considered individually,
268 <     * is not wait-free. One thief cannot successfully continue until
269 <     * another in-progress one (or, if previously empty, a push)
270 <     * visibly completes.  This can stall threads when required to
271 <     * consume from a given queue (see method poll()).  However, in
272 <     * the aggregate, we ensure at least probabilistic
273 <     * non-blockingness.  If an attempted steal fails, a scanning
274 <     * thief chooses a different random victim target to try next. So,
275 <     * in order for one thief to progress, it suffices for any
276 <     * in-progress poll or new push on any empty queue to complete.
261 >     * the emptiness check base == top is only quiescently accurate
262 >     * (and so used where this suffices). Otherwise, it may err on the
263 >     * side of possibly making the queue appear nonempty when a push,
264 >     * pop, or poll have not fully committed, or making it appear
265 >     * empty when an update of top or base has not yet been seen.
266 >     * Method isEmpty() provides a more accurate test by checking both
267 >     * indices and slots.  Similarly, the check in push for the queue
268 >     * array being full may trigger when not completely full, causing
269 >     * a resize earlier than required.
270 >     *
271 >     * Mainly because of these potential inconsistencies among slots
272 >     * vs indices, the poll operation, considered individually, is not
273 >     * wait-free. One thief cannot successfully continue until another
274 >     * in-progress one (or, if previously empty, a push) visibly
275 >     * completes.  This can stall threads when required to consume
276 >     * from a given queue (which may spin).  However, in the
277 >     * aggregate, we ensure probabilistic non-blockingness at least
278 >     * until checking quiescence (which is is intrinsically blocking):
279 >     * If an attempted steal fails, a scanning thief chooses a
280 >     * different victim target to try next. So, in order for one thief
281 >     * to progress, it suffices for any in-progress poll or new push
282 >     * on any empty queue to complete. The worst cases occur when many
283 >     * threads are looking for tasks being produced by a stalled
284 >     * producer.
285       *
286       * This approach also enables support of a user mode in which
287       * local task processing is in FIFO, not LIFO order, simply by
288       * using poll rather than pop.  This can be useful in
289 <     * message-passing frameworks in which tasks are never joined.
289 >     * message-passing frameworks in which tasks are never joined,
290 >     * although with increased contention among task produces and
291 >     * consumers.
292       *
293       * WorkQueues are also used in a similar way for tasks submitted
294       * to the pool. We cannot mix these tasks in the same queues used
# Line 279 | Line 298 | public class ForkJoinPool extends Abstra
298       * choosing existing queues, and may be randomly repositioned upon
299       * contention with other submitters.  In essence, submitters act
300       * like workers except that they are restricted to executing local
301 <     * tasks that they submitted.  Insertion of tasks in shared mode
302 <     * requires a lock but we use only a simple spinlock (using field
303 <     * phase), because submitters encountering a busy queue move to a
304 <     * different position to use or create other queues -- they block
305 <     * only when creating and registering new queues. Because it is
306 <     * used only as a spinlock, unlocking requires only a "releasing"
288 <     * store (using setRelease) unless otherwise signalling.
301 >     * tasks that they submitted (or when known, subtasks thereof).
302 >     * Insertion of tasks in shared mode requires a lock. We use only
303 >     * a simple spinlock (using field "source"), because submitters
304 >     * encountering a busy queue move to a different position to use
305 >     * or create other queues. They block only when registering new
306 >     * queues.
307       *
308       * Management
309       * ==========
# Line 293 | Line 311 | public class ForkJoinPool extends Abstra
311       * The main throughput advantages of work-stealing stem from
312       * decentralized control -- workers mostly take tasks from
313       * themselves or each other, at rates that can exceed a billion
314 <     * per second.  The pool itself creates, activates (enables
315 <     * scanning for and running tasks), deactivates, blocks, and
316 <     * terminates threads, all with minimal central information.
317 <     * There are only a few properties that we can globally track or
318 <     * maintain, so we pack them into a small number of variables,
319 <     * often maintaining atomicity without blocking or locking.
320 <     * Nearly all essentially atomic control state is held in a few
321 <     * volatile variables that are by far most often read (not
322 <     * written) as status and consistency checks. We pack as much
323 <     * information into them as we can.
314 >     * per second.  Most non-atomic control is performed by some form
315 >     * of scanning across or within queues.  The pool itself creates,
316 >     * activates (enables scanning for and running tasks),
317 >     * deactivates, blocks, and terminates threads, all with minimal
318 >     * central information.  There are only a few properties that we
319 >     * can globally track or maintain, so we pack them into a small
320 >     * number of variables, often maintaining atomicity without
321 >     * blocking or locking.  Nearly all essentially atomic control
322 >     * state is held in a few volatile variables that are by far most
323 >     * often read (not written) as status and consistency checks. We
324 >     * pack as much information into them as we can.
325       *
326       * Field "ctl" contains 64 bits holding information needed to
327       * atomically decide to add, enqueue (on an event queue), and
# Line 314 | Line 333 | public class ForkJoinPool extends Abstra
333       *
334       * Field "mode" holds configuration parameters as well as lifetime
335       * status, atomically and monotonically setting SHUTDOWN, STOP,
336 <     * and finally TERMINATED bits.
336 >     * and finally TERMINATED bits. It is updated only via bitwise
337 >     * atomics (getAndBitwiseOr).
338       *
339 <     * Field "workQueues" holds references to WorkQueues.  It is
340 <     * updated (only during worker creation and termination) under
341 <     * lock (using field workerNamePrefix as lock), but is otherwise
342 <     * concurrently readable, and accessed directly. We also ensure
343 <     * that uses of the array reference itself never become too stale
344 <     * in case of resizing, by arranging that (re-)reads are separated
345 <     * by at least one acquiring read access.  To simplify index-based
346 <     * operations, the array size is always a power of two, and all
347 <     * readers must tolerate null slots. Worker queues are at odd
348 <     * indices. Shared (submission) queues are at even indices, up to
329 <     * a maximum of 64 slots, to limit growth even if the array needs
330 <     * to expand to add more workers. Grouping them together in this
331 <     * way simplifies and speeds up task scanning.
339 >     * Array "queues" holds references to WorkQueues.  It is updated
340 >     * (only during worker creation and termination) under the
341 >     * registrationLock, but is otherwise concurrently readable, and
342 >     * accessed directly (although always prefaced by acquireFences or
343 >     * other acquiring reads). To simplify index-based operations, the
344 >     * array size is always a power of two, and all readers must
345 >     * tolerate null slots.  Worker queues are at odd indices. Worker
346 >     * ids masked with SMASK match their index. Shared (submission)
347 >     * queues are at even indices. Grouping them together in this way
348 >     * simplifies and speeds up task scanning.
349       *
350       * All worker thread creation is on-demand, triggered by task
351       * submissions, replacement of terminated workers, and/or
352       * compensation for blocked workers. However, all other support
353       * code is set up to work with other policies.  To ensure that we
354 <     * do not hold on to worker references that would prevent GC, all
355 <     * accesses to workQueues are via indices into the workQueues
356 <     * array (which is one source of some of the messy code
357 <     * constructions here). In essence, the workQueues array serves as
354 >     * do not hold on to worker or task references that would prevent
355 >     * GC, all accesses to workQueues are via indices into the
356 >     * queues array (which is one source of some of the messy code
357 >     * constructions here). In essence, the queues array serves as
358       * a weak reference mechanism. Thus for example the stack top
359       * subfield of ctl stores indices, not references.
360       *
# Line 346 | Line 363 | public class ForkJoinPool extends Abstra
363       * none can be found immediately, and we cannot start/resume
364       * workers unless there appear to be tasks available.  On the
365       * other hand, we must quickly prod them into action when new
366 <     * tasks are submitted or generated. In many usages, ramp-up time
366 >     * tasks are submitted or generated. These latencies are mainly a
367 >     * function of JVM park/unpark (and underlying OS) performance,
368 >     * which can be slow and variable.  In many usages, ramp-up time
369       * is the main limiting factor in overall performance, which is
370       * compounded at program start-up by JIT compilation and
371 <     * allocation. So we streamline this as much as possible.
371 >     * allocation. On the other hand, throughput degrades when too
372 >     * many threads poll for too few tasks.
373       *
374 <     * The "ctl" field atomically maintains total worker and
375 <     * "released" worker counts, plus the head of the available worker
376 <     * queue (actually stack, represented by the lower 32bit subfield
377 <     * of ctl).  Released workers are those known to be scanning for
374 >     * The "ctl" field atomically maintains total and "released"
375 >     * worker counts, plus the head of the available worker queue
376 >     * (actually stack, represented by the lower 32bit subfield of
377 >     * ctl).  Released workers are those known to be scanning for
378       * and/or running tasks. Unreleased ("available") workers are
379       * recorded in the ctl stack. These workers are made available for
380 <     * signalling by enqueuing in ctl (see method runWorker).  The
380 >     * signalling by enqueuing in ctl (see method awaitWork).  The
381       * "queue" is a form of Treiber stack. This is ideal for
382       * activating threads in most-recently used order, and improves
383       * performance and locality, outweighing the disadvantages of
384       * being prone to contention and inability to release a worker
385 <     * unless it is topmost on stack.  To avoid missed signal problems
366 <     * inherent in any wait/signal design, available workers rescan
367 <     * for (and if found run) tasks after enqueuing.  Normally their
368 <     * release status will be updated while doing so, but the released
369 <     * worker ctl count may underestimate the number of active
370 <     * threads. (However, it is still possible to determine quiescence
371 <     * via a validation traversal -- see isQuiescent).  After an
372 <     * unsuccessful rescan, available workers are blocked until
373 <     * signalled (see signalWork).  The top stack state holds the
385 >     * unless it is topmost on stack. The top stack state holds the
386       * value of the "phase" field of the worker: its index and status,
387       * plus a version counter that, in addition to the count subfields
388       * (also serving as version stamps) provide protection against
# Line 378 | Line 390 | public class ForkJoinPool extends Abstra
390       *
391       * Creating workers. To create a worker, we pre-increment counts
392       * (serving as a reservation), and attempt to construct a
393 <     * ForkJoinWorkerThread via its factory. Upon construction, the
394 <     * new thread invokes registerWorker, where it constructs a
395 <     * WorkQueue and is assigned an index in the workQueues array
396 <     * (expanding the array if necessary). The thread is then started.
397 <     * Upon any exception across these steps, or null return from
398 <     * factory, deregisterWorker adjusts counts and records
399 <     * accordingly.  If a null return, the pool continues running with
400 <     * fewer than the target number workers. If exceptional, the
401 <     * exception is propagated, generally to some external caller.
390 <     * Worker index assignment avoids the bias in scanning that would
391 <     * occur if entries were sequentially packed starting at the front
392 <     * of the workQueues array. We treat the array as a simple
393 <     * power-of-two hash table, expanding as needed. The seedIndex
394 <     * increment ensures no collisions until a resize is needed or a
395 <     * worker is deregistered and replaced, and thereafter keeps
396 <     * probability of collision low. We cannot use
397 <     * ThreadLocalRandom.getProbe() for similar purposes here because
398 <     * the thread has not started yet, but do so for creating
399 <     * submission queues for existing external threads (see
400 <     * externalPush).
393 >     * ForkJoinWorkerThread via its factory. On starting, the new
394 >     * thread first invokes registerWorker, where it constructs a
395 >     * WorkQueue and is assigned an index in the queues array
396 >     * (expanding the array if necessary).  Upon any exception across
397 >     * these steps, or null return from factory, deregisterWorker
398 >     * adjusts counts and records accordingly.  If a null return, the
399 >     * pool continues running with fewer than the target number
400 >     * workers. If exceptional, the exception is propagated, generally
401 >     * to some external caller.
402       *
403       * WorkQueue field "phase" is used by both workers and the pool to
404       * manage and track whether a worker is UNSIGNALLED (possibly
405       * blocked waiting for a signal).  When a worker is enqueued its
406 <     * phase field is set. Note that phase field updates lag queue CAS
407 <     * releases so usage requires care -- seeing a negative phase does
408 <     * not guarantee that the worker is available. When queued, the
409 <     * lower 16 bits of scanState must hold its pool index. So we
410 <     * place the index there upon initialization and otherwise keep it
410 <     * there or restore it when necessary.
406 >     * phase field is set negative. Note that phase field updates lag
407 >     * queue CAS releases; seeing a negative phase does not guarantee
408 >     * that the worker is available. When queued, the lower 16 bits of
409 >     * its phase must hold its pool index. So we place the index there
410 >     * upon initialization and never modify these bits.
411       *
412       * The ctl field also serves as the basis for memory
413       * synchronization surrounding activation. This uses a more
414       * efficient version of a Dekker-like rule that task producers and
415       * consumers sync with each other by both writing/CASing ctl (even
416 <     * if to its current value).  This would be extremely costly. So
417 <     * we relax it in several ways: (1) Producers only signal when
418 <     * their queue is possibly empty at some point during a push
419 <     * operation. (2) Other workers propagate this signal
420 <     * when they find tasks in a queue with size greater than one. (3)
421 <     * Workers only enqueue after scanning (see below) and not finding
422 <     * any tasks.  (4) Rather than CASing ctl to its current value in
423 <     * the common case where no action is required, we reduce write
424 <     * contention by equivalently prefacing signalWork when called by
425 <     * an external task producer using a memory access with
426 <     * full-volatile semantics or a "fullFence".
427 <     *
428 <     * Almost always, too many signals are issued, in part because a
429 <     * task producer cannot tell if some existing worker is in the
430 <     * midst of finishing one task (or already scanning) and ready to
431 <     * take another without being signalled. So the producer might
432 <     * instead activate a different worker that does not find any
433 <     * work, and then inactivates. This scarcely matters in
434 <     * steady-state computations involving all workers, but can create
435 <     * contention and bookkeeping bottlenecks during ramp-up,
416 >     * if to its current value).  However, rather than CASing ctl to
417 >     * its current value in the common case where no action is
418 >     * required, we reduce write contention by ensuring that
419 >     * signalWork invocations are prefaced with a full-volatile memory
420 >     * access (which is usually needed anyway).
421 >     *
422 >     * Signalling. Signals (in signalWork) cause new or reactivated
423 >     * workers to scan for tasks.  Method signalWork and its callers
424 >     * try to approximate the unattainable goal of having the right
425 >     * number of workers activated for the tasks at hand, but must err
426 >     * on the side of too many workers vs too few to avoid stalls.  If
427 >     * computations are purely tree structured, it suffices for every
428 >     * worker to activate another when it pushes a task into an empty
429 >     * queue, resulting in O(log(#threads)) steps to full activation.
430 >     * If instead, tasks come in serially from only a single producer,
431 >     * each worker taking its first (since the last quiescence) task
432 >     * from a queue should signal another if there are more tasks in
433 >     * that queue. This is equivalent to, but generally faster than,
434 >     * arranging the stealer take two tasks, re-pushing one on its own
435 >     * queue, and signalling (because its queue is empty), also
436 >     * resulting in logarithmic full activation time. Because we don't
437 >     * know about usage patterns (or most commonly, mixtures), we use
438 >     * both approaches.  We approximate the second rule by arranging
439 >     * that workers in scan() do not repeat signals when repeatedly
440 >     * taking tasks from any given queue, by remembering the previous
441 >     * one. There are narrow windows in which both rules may apply,
442 >     * leading to duplicate or unnecessary signals. Despite such
443 >     * limitations, these rules usually avoid slowdowns that otherwise
444 >     * occur when too many workers contend to take too few tasks, or
445 >     * when producers waste most of their time resignalling.  However,
446 >     * contention and overhead effects may still occur during ramp-up,
447       * ramp-down, and small computations involving only a few workers.
448       *
449 <     * Scanning. Method scan (from runWorker) performs top-level
450 <     * scanning for tasks. (Similar scans appear in helpQuiesce and
451 <     * pollScan.)  Each scan traverses and tries to poll from each
452 <     * queue starting at a random index. Scans are not performed in
453 <     * ideal random permutation order, to reduce cacheline
454 <     * contention. The pseudorandom generator need not have
455 <     * high-quality statistical properties in the long term, but just
456 <     * within computations; We use Marsaglia XorShifts (often via
457 <     * ThreadLocalRandom.nextSecondarySeed), which are cheap and
458 <     * suffice. Scanning also includes contention reduction: When
459 <     * scanning workers fail to extract an apparently existing task,
460 <     * they soon restart at a different pseudorandom index.  This form
461 <     * of backoff improves throughput when many threads are trying to
462 <     * take tasks from few queues, which can be common in some usages.
452 <     * Scans do not otherwise explicitly take into account core
453 <     * affinities, loads, cache localities, etc, However, they do
449 >     * Scanning. Method scan performs top-level scanning for (and
450 >     * execution of) tasks.  Scans by different workers and/or at
451 >     * different times are unlikely to poll queues in the same
452 >     * order. Each scan traverses and tries to poll from each queue in
453 >     * a pseudorandom permutation order by starting at a random index,
454 >     * and using a constant cyclically exhaustive stride; restarting
455 >     * upon contention.  (Non-top-level scans; for example in
456 >     * helpJoin, use simpler linear probes because they do not
457 >     * systematically contend with top-level scans.)  The pseudorandom
458 >     * generator need not have high-quality statistical properties in
459 >     * the long term. We use Marsaglia XorShifts, seeded with the Weyl
460 >     * sequence from ThreadLocalRandom probes, which are cheap and
461 >     * suffice. Scans do not otherwise explicitly take into account
462 >     * core affinities, loads, cache localities, etc, However, they do
463       * exploit temporal locality (which usually approximates these) by
464       * preferring to re-poll from the same queue after a successful
465 <     * poll before trying others (see method topLevelExec). However
466 <     * this preference is bounded (see TOP_BOUND_SHIFT) as a safeguard
467 <     * against infinitely unfair looping under unbounded user task
468 <     * recursion, and also to reduce long-term contention when many
469 <     * threads poll few queues holding many small tasks. The bound is
470 <     * high enough to avoid much impact on locality and scheduling
471 <     * overhead.
465 >     * poll before trying others (see method topLevelExec).  This
466 >     * reduces fairness, which is partially counteracted by using a
467 >     * one-shot form of poll (tryPoll) that may lose to other workers.
468 >     *
469 >     * Deactivation. Method scan returns a sentinel when no tasks are
470 >     * found, leading to deactivation (see awaitWork). The count
471 >     * fields in ctl allow accurate discovery of quiescent states
472 >     * (i.e., when all workers are idle) after deactivation. However,
473 >     * this may also race with new (external) submissions, so a
474 >     * recheck is also needed to determine quiescence. Upon apparently
475 >     * triggering quiescence, awaitWork re-scans and self-signals if
476 >     * it may have missed a signal. In other cases, a missed signal
477 >     * may transiently lower parallelism because deactivation does not
478 >     * necessarily mean that there is no more work, only that that
479 >     * there were no tasks not taken by other workers.  But more
480 >     * signals are generated (see above) to eventually reactivate if
481 >     * needed.
482       *
483       * Trimming workers. To release resources after periods of lack of
484       * use, a worker starting to wait when the pool is quiescent will
485 <     * time out and terminate (see method runWorker) if the pool has
486 <     * remained quiescent for period given by field keepAlive.
485 >     * time out and terminate if the pool has remained quiescent for
486 >     * period given by field keepAlive.
487       *
488       * Shutdown and Termination. A call to shutdownNow invokes
489 <     * tryTerminate to atomically set a runState bit. The calling
490 <     * thread, as well as every other worker thereafter terminating,
491 <     * helps terminate others by cancelling their unprocessed tasks,
492 <     * and waking them up, doing so repeatedly until stable. Calls to
493 <     * non-abrupt shutdown() preface this by checking whether
475 <     * termination should commence by sweeping through queues (until
476 <     * stable) to ensure lack of in-flight submissions and workers
477 <     * about to process them before triggering the "STOP" phase of
489 >     * tryTerminate to atomically set a mode bit. The calling thread,
490 >     * as well as every other worker thereafter terminating, helps
491 >     * terminate others by cancelling their unprocessed tasks, and
492 >     * waking them up. Calls to non-abrupt shutdown() preface this by
493 >     * checking isQuiescent before triggering the "STOP" phase of
494       * termination.
495       *
496       * Joining Tasks
497       * =============
498       *
499 <     * Any of several actions may be taken when one worker is waiting
499 >     * Normally, the first option when joining a task that is not done
500 >     * is to try to unfork it from local queue and run it.  Otherwise,
501 >     * any of several actions may be taken when one worker is waiting
502       * to join a task stolen (or always held) by another.  Because we
503       * are multiplexing many tasks on to a pool of workers, we can't
504       * always just let them block (as in Thread.join).  We also cannot
# Line 491 | Line 509 | public class ForkJoinPool extends Abstra
509       * Instead we combine two tactics:
510       *
511       *   Helping: Arranging for the joiner to execute some task that it
512 <     *      would be running if the steal had not occurred.
512 >     *      could be running if the steal had not occurred.
513       *
514       *   Compensating: Unless there are already enough live threads,
515       *      method tryCompensate() may create or re-activate a spare
516       *      thread to compensate for blocked joiners until they unblock.
517       *
518 <     * A third form (implemented in tryRemoveAndExec) amounts to
519 <     * helping a hypothetical compensator: If we can readily tell that
520 <     * a possible action of a compensator is to steal and execute the
518 >     * A third form (implemented via tryRemove) amounts to helping a
519 >     * hypothetical compensator: If we can readily tell that a
520 >     * possible action of a compensator is to steal and execute the
521       * task being joined, the joining thread can do so directly,
522 <     * without the need for a compensation thread.
522 >     * without the need for a compensation thread; although with a
523 >     * (rare) possibility of reduced parallelism because of a
524 >     * transient gap in the queue array.
525 >     *
526 >     * Other intermediate forms available for specific task types (for
527 >     * example helpAsyncBlocker) often avoid or postpone the need for
528 >     * blocking or compensation.
529       *
530       * The ManagedBlocker extension API can't use helping so relies
531       * only on compensation in method awaitBlocker.
532       *
533 <     * The algorithm in awaitJoin entails a form of "linear helping".
534 <     * Each worker records (in field source) the id of the queue from
535 <     * which it last stole a task.  The scan in method awaitJoin uses
536 <     * these markers to try to find a worker to help (i.e., steal back
537 <     * a task from and execute it) that could hasten completion of the
538 <     * actively joined task.  Thus, the joiner executes a task that
539 <     * would be on its own local deque if the to-be-joined task had
540 <     * not been stolen. This is a conservative variant of the approach
541 <     * described in Wagner & Calder "Leapfrogging: a portable
533 >     * The algorithm in helpJoin entails a form of "linear helping".
534 >     * Each worker records (in field "source") the id of the queue
535 >     * from which it last stole a task.  The scan in method helpJoin
536 >     * uses these markers to try to find a worker to help (i.e., steal
537 >     * back a task from and execute it) that could hasten completion
538 >     * of the actively joined task.  Thus, the joiner executes a task
539 >     * that would be on its own local deque if the to-be-joined task
540 >     * had not been stolen. This is a conservative variant of the
541 >     * approach described in Wagner & Calder "Leapfrogging: a portable
542       * technique for implementing efficient futures" SIGPLAN Notices,
543       * 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs
544       * mainly in that we only record queue ids, not full dependency
545 <     * links.  This requires a linear scan of the workQueues array to
545 >     * links.  This requires a linear scan of the queues array to
546       * locate stealers, but isolates cost to when it is needed, rather
547 <     * than adding to per-task overhead. Searches can fail to locate
548 <     * stealers GC stalls and the like delay recording sources.
549 <     * Further, even when accurately identified, stealers might not
550 <     * ever produce a task that the joiner can in turn help with. So,
551 <     * compensation is tried upon failure to find tasks to run.
547 >     * than adding to per-task overhead. Also, searches are limited to
548 >     * direct and at most two levels of indirect stealers, after which
549 >     * there are rapidly diminishing returns on increased overhead.
550 >     * Searches can fail to locate stealers when stalls delay
551 >     * recording sources.  Further, even when accurately identified,
552 >     * stealers might not ever produce a task that the joiner can in
553 >     * turn help with. So, compensation is tried upon failure to find
554 >     * tasks to run.
555 >     *
556 >     * Joining CountedCompleters (see helpComplete) differs from (and
557 >     * is generally more efficient than) other cases because task
558 >     * eligibility is determined by checking completion chains rather
559 >     * than tracking stealers.
560       *
561       * Compensation does not by default aim to keep exactly the target
562       * parallelism number of unblocked threads running at any given
# Line 549 | Line 581 | public class ForkJoinPool extends Abstra
581       * footprint to the setup of about a dozen fields.
582       *
583       * When external threads submit to the common pool, they can
584 <     * perform subtask processing (see externalHelpComplete and
585 <     * related methods) upon joins.  This caller-helps policy makes it
584 >     * perform subtask processing (see helpComplete and related
585 >     * methods) upon joins.  This caller-helps policy makes it
586       * sensible to set common pool parallelism level to one (or more)
587       * less than the total number of available cores, or even zero for
588       * pure caller-runs.  We do not need to record whether external
# Line 566 | Line 598 | public class ForkJoinPool extends Abstra
598       * InnocuousForkJoinWorkerThread when there is a SecurityManager
599       * present. These workers have no permissions set, do not belong
600       * to any user-defined ThreadGroup, and erase all ThreadLocals
601 <     * after executing any top-level task (see
602 <     * WorkQueue.afterTopLevelExec).  The associated mechanics (mainly
603 <     * in ForkJoinWorkerThread) may be JVM-dependent and must access
572 <     * particular Thread class fields to achieve this effect.
601 >     * after executing any top-level task.  The associated mechanics
602 >     * (mainly in ForkJoinWorkerThread) may be JVM-dependent and must
603 >     * access particular Thread class fields to achieve this effect.
604       *
605       * Memory placement
606       * ================
607       *
608       * Performance can be very sensitive to placement of instances of
609       * ForkJoinPool and WorkQueues and their queue arrays. To reduce
610 <     * false-sharing impact, the @Contended annotation isolates
611 <     * adjacent WorkQueue instances, as well as the ForkJoinPool.ctl
612 <     * field. WorkQueue arrays are allocated (by their threads) with
613 <     * larger initial sizes than most ever need, mostly to reduce
614 <     * false sharing with current garbage collectors that use cardmark
615 <     * tables.
610 >     * false-sharing impact, the @Contended annotation isolates the
611 >     * ForkJoinPool.ctl field as well as the most heavily written
612 >     * WorkQueue fields. These mainy reduce cache traffic by scanners.
613 >     * WorkQueue arrays are presized large enough to avoid resizing
614 >     * (which transiently reduces throughput) in most tree-like
615 >     * computations, although not in some streaming usages. Initial
616 >     * sizes are not large enough to avoid secondary contention
617 >     * effects (especially for GC cardmarks) when queues are placed
618 >     * near each other in memory. This is common, but has different
619 >     * impact in different collectors and remains incompletely
620 >     * addressed.
621       *
622       * Style notes
623       * ===========
624       *
625 <     * Memory ordering relies mainly on VarHandles.  This can be
625 >     * Memory ordering relies mainly on atomic operations (CAS,
626 >     * getAndSet, getAndAdd) along with explicit fences.  This can be
627       * awkward and ugly, but also reflects the need to control
628       * outcomes across the unusual cases that arise in very racy code
629       * with very few invariants. All fields are read into locals
630 <     * before use, and null-checked if they are references.  Array
631 <     * accesses using masked indices include checks (that are always
632 <     * true) that the array length is non-zero to avoid compilers
633 <     * inserting more expensive traps.  This is usually done in a
634 <     * "C"-like style of listing declarations at the heads of methods
635 <     * or blocks, and using inline assignments on first encounter.
636 <     * Nearly all explicit checks lead to bypass/return, not exception
637 <     * throws, because they may legitimately arise due to
638 <     * cancellation/revocation during shutdown.
630 >     * before use, and null-checked if they are references, even if
631 >     * they can never be null under current usages.  Array accesses
632 >     * using masked indices include checks (that are always true) that
633 >     * the array length is non-zero to avoid compilers inserting more
634 >     * expensive traps.  This is usually done in a "C"-like style of
635 >     * listing declarations at the heads of methods or blocks, and
636 >     * using inline assignments on first encounter.  Nearly all
637 >     * explicit checks lead to bypass/return, not exception throws,
638 >     * because they may legitimately arise during shutdown.
639       *
640       * There is a lot of representation-level coupling among classes
641       * ForkJoinPool, ForkJoinWorkerThread, and ForkJoinTask.  The
# Line 623 | Line 660 | public class ForkJoinPool extends Abstra
660       * (6) Callbacks and other support for ForkJoinTask methods
661       * (7) Exported methods
662       * (8) Static block initializing statics in minimally dependent order
663 +     *
664 +     * Revision notes
665 +     * ==============
666 +     *
667 +     * The main sources of differences of January 2020 ForkJoin
668 +     * classes from previous version are:
669 +     *
670 +     * * ForkJoinTask now uses field "aux" to support blocking joins
671 +     *   and/or record exceptions, replacing reliance on builtin
672 +     *   monitors and side tables.
673 +     * * Scans probe slots (vs compare indices), alonmg with related
674 +     *   changes that reduce performance differences across most
675 +     *   garbage collectors, and reduces contention.
676 +     * * Refactoring for better integration of special task types and
677 +     *   other capabilities that had been incrementally tacked on. Plus
678 +     *   many minor reworkings to improve consistency.
679       */
680  
681      // Static utilities
# Line 637 | Line 690 | public class ForkJoinPool extends Abstra
690              security.checkPermission(modifyThreadPermission);
691      }
692  
693 +    static AccessControlContext contextWithPermissions(Permission ... perms) {
694 +        Permissions permissions = new Permissions();
695 +        for (Permission perm : perms)
696 +            permissions.add(perm);
697 +        return new AccessControlContext(
698 +            new ProtectionDomain[] { new ProtectionDomain(null, permissions) });
699 +    }
700 +
701      // Nested classes
702  
703      /**
# Line 664 | Line 725 | public class ForkJoinPool extends Abstra
725          public ForkJoinWorkerThread newThread(ForkJoinPool pool);
726      }
727  
667    static AccessControlContext contextWithPermissions(Permission ... perms) {
668        Permissions permissions = new Permissions();
669        for (Permission perm : perms)
670            permissions.add(perm);
671        return new AccessControlContext(
672            new ProtectionDomain[] { new ProtectionDomain(null, permissions) });
673    }
674
728      /**
729       * Default ForkJoinWorkerThreadFactory implementation; creates a
730       * new ForkJoinWorkerThread using the system class loader as the
731       * thread context class loader.
732       */
733 <    private static final class DefaultForkJoinWorkerThreadFactory
733 >    static final class DefaultForkJoinWorkerThreadFactory
734 >        implements ForkJoinWorkerThreadFactory {
735 >        // ACC for access to the factory
736 >        private static final AccessControlContext ACC = contextWithPermissions(
737 >            new RuntimePermission("getClassLoader"),
738 >            new RuntimePermission("setContextClassLoader"));
739 >
740 >        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
741 >            return AccessController.doPrivileged(
742 >                new PrivilegedAction<>() {
743 >                    public ForkJoinWorkerThread run() {
744 >                        return new ForkJoinWorkerThread(null, pool, true, false);
745 >                    }},
746 >                ACC);
747 >        }
748 >    }
749 >
750 >    /**
751 >     * Factory for InnocuousForkJoinWorkerThread. Support requires
752 >     * that we break quite a lot of encapsulation (some via helper
753 >     * methods in ThreadLocalRandom) to access and set Thread fields.
754 >     */
755 >    static final class InnocuousForkJoinWorkerThreadFactory
756          implements ForkJoinWorkerThreadFactory {
757 +        // ACC for access to the factory
758          private static final AccessControlContext ACC = contextWithPermissions(
759 +            modifyThreadPermission,
760 +            new RuntimePermission("enableContextClassLoaderOverride"),
761 +            new RuntimePermission("modifyThreadGroup"),
762              new RuntimePermission("getClassLoader"),
763              new RuntimePermission("setContextClassLoader"));
764  
# Line 687 | Line 766 | public class ForkJoinPool extends Abstra
766              return AccessController.doPrivileged(
767                  new PrivilegedAction<>() {
768                      public ForkJoinWorkerThread run() {
769 <                        return new ForkJoinWorkerThread(
770 <                            pool, ClassLoader.getSystemClassLoader()); }},
769 >                        return new ForkJoinWorkerThread.
770 >                            InnocuousForkJoinWorkerThread(pool); }},
771                  ACC);
772          }
773      }
# Line 699 | Line 778 | public class ForkJoinPool extends Abstra
778      static final int SWIDTH       = 16;            // width of short
779      static final int SMASK        = 0xffff;        // short bits == max index
780      static final int MAX_CAP      = 0x7fff;        // max #workers - 1
702    static final int SQMASK       = 0x007e;        // max 64 (even) slots
781  
782      // Masks and units for WorkQueue.phase and ctl sp subfield
783      static final int UNSIGNALLED  = 1 << 31;       // must be negative
784      static final int SS_SEQ       = 1 << 16;       // version count
707    static final int QLOCK        = 1;             // must be 1
785  
786 <    // Mode bits and sentinels, some also used in WorkQueue id and.source fields
710 <    static final int OWNED        = 1;             // queue has owner thread
786 >    // Mode bits and sentinels, some also used in WorkQueue fields
787      static final int FIFO         = 1 << 16;       // fifo queue or access mode
788 <    static final int SHUTDOWN     = 1 << 18;
789 <    static final int TERMINATED   = 1 << 19;
788 >    static final int SRC          = 1 << 17;       // set for valid queue ids
789 >    static final int INNOCUOUS    = 1 << 18;       // set for Innocuous workers
790 >    static final int QUIET        = 1 << 19;       // quiescing phase or source
791 >    static final int SHUTDOWN     = 1 << 24;
792 >    static final int TERMINATED   = 1 << 25;
793      static final int STOP         = 1 << 31;       // must be negative
794 <    static final int QUIET        = 1 << 30;       // not scanning or working
716 <    static final int DORMANT      = QUIET | UNSIGNALLED;
794 >    static final int ADJUST       = 1 << 16;       // tryCompensate return
795  
796      /**
797 <     * Initial capacity of work-stealing queue array.
798 <     * Must be a power of two, at least 2.
797 >     * Initial capacity of work-stealing queue array.  Must be a power
798 >     * of two, at least 2. See above.
799       */
800 <    static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
723 <
724 <    /**
725 <     * Maximum capacity for queue arrays. Must be a power of two less
726 <     * than or equal to 1 << (31 - width of array entry) to ensure
727 <     * lack of wraparound of index calculations, but defined to a
728 <     * value a bit less than this to help users trap runaway programs
729 <     * before saturating systems.
730 <     */
731 <    static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M
732 <
733 <    /**
734 <     * The maximum number of top-level polls per worker before
735 <     * checking other queues, expressed as a bit shift.  See above for
736 <     * rationale.
737 <     */
738 <    static final int TOP_BOUND_SHIFT = 10;
800 >    static final int INITIAL_QUEUE_CAPACITY = 1 << 8;
801  
802      /**
803       * Queues supporting work-stealing as well as external task
804       * submission. See above for descriptions and algorithms.
805       */
744    @jdk.internal.vm.annotation.Contended
806      static final class WorkQueue {
807 <        volatile int source;       // source queue id, or sentinel
747 <        int id;                    // pool index, mode, tag
748 <        int base;                  // index of next slot for poll
749 <        int top;                   // index of next slot for push
750 <        volatile int phase;        // versioned, negative: queued, 1: locked
807 >        volatile int phase;        // versioned, negative if inactive
808          int stackPred;             // pool stack (ctl) predecessor link
809 <        int nsteals;               // number of steals
809 >        int config;                // index, mode, ORed with SRC after init
810 >        int base;                  // index of next slot for poll
811          ForkJoinTask<?>[] array;   // the queued tasks; power of 2 size
754        final ForkJoinPool pool;   // the containing pool (may be null)
812          final ForkJoinWorkerThread owner; // owning thread or null if shared
813  
814 <        WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) {
815 <            this.pool = pool;
816 <            this.owner = owner;
817 <            // Place indices in the center of array (that is not yet allocated)
818 <            base = top = INITIAL_QUEUE_CAPACITY >>> 1;
814 >        // segregate fields frequently updated but not read by scans or steals
815 >        @jdk.internal.vm.annotation.Contended("w")
816 >        int top;                   // index of next slot for push
817 >        @jdk.internal.vm.annotation.Contended("w")
818 >        volatile int source;       // source queue id, lock, or sentinel
819 >        @jdk.internal.vm.annotation.Contended("w")
820 >        int nsteals;               // number of steals from other queues
821 >
822 >        // Support for atomic operations
823 >        private static final VarHandle QA; // for array slots
824 >        private static final VarHandle SOURCE;
825 >        private static final VarHandle BASE;
826 >        static final ForkJoinTask<?> getSlot(ForkJoinTask<?>[] a, int i) {
827 >            return (ForkJoinTask<?>)QA.getAcquire(a, i);
828 >        }
829 >        static final ForkJoinTask<?> getAndClearSlot(ForkJoinTask<?>[] a,
830 >                                                     int i) {
831 >            return (ForkJoinTask<?>)QA.getAndSet(a, i, null);
832 >        }
833 >        static final void setSlotVolatile(ForkJoinTask<?>[] a, int i,
834 >                                          ForkJoinTask<?> v) {
835 >            QA.setVolatile(a, i, v);
836 >        }
837 >        static final boolean casSlotToNull(ForkJoinTask<?>[] a, int i,
838 >                                          ForkJoinTask<?> c) {
839 >            return QA.weakCompareAndSet(a, i, c, null);
840 >        }
841 >        final boolean tryLock() {
842 >            return SOURCE.compareAndSet(this, 0, 1);
843 >        }
844 >        final void setBaseOpaque(int b) {
845 >            BASE.setOpaque(this, b);
846          }
847  
848          /**
849 <         * Tries to lock shared queue by CASing phase field.
849 >         * Constructor used by ForkJoinWorkerThreads. Most fields
850 >         * are initialized upon thread start, in pool.registerWorker.
851           */
852 <        final boolean tryLockPhase() {
853 <            return PHASE.compareAndSet(this, 0, 1);
852 >        WorkQueue(ForkJoinWorkerThread owner, boolean isInnocuous) {
853 >            this.config = (isInnocuous) ? INNOCUOUS : 0;
854 >            this.owner = owner;
855          }
856  
857 <        final void releasePhaseLock() {
858 <            PHASE.setRelease(this, 0);
857 >        /**
858 >         * Constructor used for external queues.
859 >         */
860 >        WorkQueue(int config) {
861 >            array = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
862 >            this.config = config;
863 >            owner = null;
864 >            phase = -1;
865          }
866  
867          /**
868           * Returns an exportable index (used by ForkJoinWorkerThread).
869           */
870          final int getPoolIndex() {
871 <            return (id & 0xffff) >>> 1; // ignore odd/even tag bit
871 >            return (config & 0xffff) >>> 1; // ignore odd/even tag bit
872          }
873  
874          /**
875           * Returns the approximate number of tasks in the queue.
876           */
877          final int queueSize() {
878 <            int n = (int)BASE.getAcquire(this) - top;
879 <            return (n >= 0) ? 0 : -n; // ignore transient negative
878 >            VarHandle.acquireFence(); // ensure fresh reads by external callers
879 >            int n = top - base;
880 >            return (n < 0) ? 0 : n;   // ignore transient negative
881          }
882  
883          /**
884           * Provides a more accurate estimate of whether this queue has
885 <         * any tasks than does queueSize, by checking whether a
886 <         * near-empty queue has at least one unclaimed task.
885 >         * any tasks than does queueSize, by checking whether an
886 >         * apparently near-empty queue has at least one unclaimed
887 >         * task.
888           */
889          final boolean isEmpty() {
890 <            ForkJoinTask<?>[] a; int n, cap, b;
891 <            VarHandle.acquireFence(); // needed by external callers
892 <            return ((n = (b = base) - top) >= 0 || // possibly one task
893 <                    (n == -1 && ((a = array) == null ||
894 <                                 (cap = a.length) == 0 ||
895 <                                 a[(cap - 1) & b] == null)));
890 >            VarHandle.acquireFence();
891 >            int s = top, b = base, cap;
892 >            ForkJoinTask<?>[] a = array;
893 >            return s - b <= 1 && (a == null || (cap = a.length) == 0 ||
894 >                                  (a[(cap - 1) & b] == null &&
895 >                                   a[(cap - 1) & (s - 1)] == null));
896          }
897  
898          /**
899           * Pushes a task. Call only by owner in unshared queues.
900           *
901           * @param task the task. Caller must ensure non-null.
902 +         * @param pool (no-op if null)
903           * @throws RejectedExecutionException if array cannot be resized
904           */
905 <        final void push(ForkJoinTask<?> task) {
906 <            ForkJoinTask<?>[] a;
907 <            int s = top, d = s - base, cap, m;
908 <            ForkJoinPool p = pool;
909 <            if ((a = array) != null && (cap = a.length) > 0) {
815 <                QA.setRelease(a, (m = cap - 1) & s, task);
816 <                top = s + 1;
905 >        final void push(ForkJoinTask<?> task, ForkJoinPool pool) {
906 >            ForkJoinTask<?>[] a = array;
907 >            int s = top++, d = s - base, cap, m; // skip insert if disabled
908 >            if (a != null && pool != null && (cap = a.length) > 0) {
909 >                setSlotVolatile(a, (m = cap - 1) & s, task);
910                  if (d == m)
911 <                    growArray(false);
912 <                else if (QA.getAcquire(a, m & (s - 1)) == null && p != null) {
913 <                    VarHandle.fullFence();  // was empty
821 <                    p.signalWork(null);
822 <                }
911 >                    growArray();
912 >                if (d == m || a[m & (s - 1)] == null)
913 >                    pool.signalWork(); // signal if was empty or resized
914              }
915          }
916  
917          /**
918 <         * Version of push for shared queues. Call only with phase lock held.
919 <         * @return true if should signal work
918 >         * Pushes task to a shared queue with lock already held, and unlocks.
919 >         *
920 >         * @return true if caller should signal work
921           */
922          final boolean lockedPush(ForkJoinTask<?> task) {
923 <            ForkJoinTask<?>[] a;
924 <            boolean signal = false;
925 <            int s = top, d = s - base, cap, m;
926 <            if ((a = array) != null && (cap = a.length) > 0) {
835 <                a[(m = (cap - 1)) & s] = task;
836 <                top = s + 1;
923 >            ForkJoinTask<?>[] a = array;
924 >            int s = top++, d = s - base, cap, m;
925 >            if (a != null && (cap = a.length) > 0) {
926 >                a[(m = cap - 1) & s] = task;
927                  if (d == m)
928 <                    growArray(true);
929 <                else {
930 <                    phase = 0; // full volatile unlock
931 <                    if (((s - base) & ~1) == 0) // size 0 or 1
842 <                        signal = true;
843 <                }
928 >                    growArray();
929 >                source = 0; // unlock
930 >                if (d == m || a[m & (s - 1)] == null)
931 >                    return true;
932              }
933 <            return signal;
933 >            return false;
934          }
935  
936          /**
937 <         * Doubles the capacity of array. Call either by owner or with
938 <         * lock held -- it is OK for base, but not top, to move while
939 <         * resizings are in progress.
940 <         */
941 <        final void growArray(boolean locked) {
942 <            ForkJoinTask<?>[] newA = null;
943 <            try {
944 <                ForkJoinTask<?>[] oldA; int oldSize, newSize;
945 <                if ((oldA = array) != null && (oldSize = oldA.length) > 0 &&
946 <                    (newSize = oldSize << 1) <= MAXIMUM_QUEUE_CAPACITY &&
947 <                    newSize > 0) {
948 <                    try {
949 <                        newA = new ForkJoinTask<?>[newSize];
950 <                    } catch (OutOfMemoryError ex) {
951 <                    }
952 <                    if (newA != null) { // poll from old array, push to new
953 <                        int oldMask = oldSize - 1, newMask = newSize - 1;
866 <                        for (int s = top - 1, k = oldMask; k >= 0; --k) {
867 <                            ForkJoinTask<?> x = (ForkJoinTask<?>)
868 <                                QA.getAndSet(oldA, s & oldMask, null);
869 <                            if (x != null)
870 <                                newA[s-- & newMask] = x;
871 <                            else
872 <                                break;
873 <                        }
874 <                        array = newA;
875 <                        VarHandle.releaseFence();
876 <                    }
937 >         * Doubles the capacity of array. Called by owner or with lock
938 >         * held after pre-incrementing top, which is reverted on
939 >         * allocation failure.
940 >         */
941 >        final void growArray() {
942 >            ForkJoinTask<?>[] oldArray = array, newArray;
943 >            int s = top - 1, oldCap, newCap;
944 >            if (oldArray != null && (oldCap = oldArray.length) > 0 &&
945 >                (newCap = oldCap << 1) > 0) { // skip if disabled
946 >                try {
947 >                    newArray = new ForkJoinTask<?>[newCap];
948 >                } catch (Throwable ex) {
949 >                    top = s;
950 >                    if (owner == null)
951 >                        source = 0; // unlock
952 >                    throw new RejectedExecutionException(
953 >                        "Queue capacity exceeded");
954                  }
955 <            } finally {
956 <                if (locked)
957 <                    phase = 0;
958 <            }
959 <            if (newA == null)
960 <                throw new RejectedExecutionException("Queue capacity exceeded");
884 <        }
885 <
886 <        /**
887 <         * Takes next task, if one exists, in FIFO order.
888 <         */
889 <        final ForkJoinTask<?> poll() {
890 <            int b, k, cap; ForkJoinTask<?>[] a;
891 <            while ((a = array) != null && (cap = a.length) > 0 &&
892 <                   top - (b = base) > 0) {
893 <                ForkJoinTask<?> t = (ForkJoinTask<?>)
894 <                    QA.getAcquire(a, k = (cap - 1) & b);
895 <                if (base == b++) {
896 <                    if (t == null)
897 <                        Thread.yield(); // await index advance
898 <                    else if (QA.compareAndSet(a, k, t, null)) {
899 <                        BASE.setOpaque(this, b);
900 <                        return t;
901 <                    }
955 >                int newMask = newCap - 1, oldMask = oldCap - 1;
956 >                for (int k = oldCap; k > 0; --k, --s) {
957 >                    ForkJoinTask<?> x;        // poll old, push to new
958 >                    if ((x = getAndClearSlot(oldArray, s & oldMask)) == null)
959 >                        break;                // others already taken
960 >                    newArray[s & newMask] = x;
961                  }
962 +                VarHandle.releaseFence();     // fill before publish
963 +                array = newArray;
964              }
904            return null;
965          }
966  
967 +        // Variants of pop
968 +
969          /**
970 <         * Takes next task, if one exists, in order specified by mode.
970 >         * Pops and returns task, or null if empty. Called only by owner.
971           */
972 <        final ForkJoinTask<?> nextLocalTask() {
972 >        private ForkJoinTask<?> pop() {
973              ForkJoinTask<?> t = null;
974 <            int md = id, b, s, d, cap; ForkJoinTask<?>[] a;
975 <            if ((a = array) != null && (cap = a.length) > 0 &&
976 <                (d = (s = top) - (b = base)) > 0) {
977 <                if ((md & FIFO) == 0 || d == 1) {
916 <                    if ((t = (ForkJoinTask<?>)
917 <                         QA.getAndSet(a, (cap - 1) & --s, null)) != null)
918 <                        TOP.setOpaque(this, s);
919 <                }
920 <                else if ((t = (ForkJoinTask<?>)
921 <                          QA.getAndSet(a, (cap - 1) & b++, null)) != null) {
922 <                    BASE.setOpaque(this, b);
923 <                }
924 <                else // on contention in FIFO mode, use regular poll
925 <                    t = poll();
926 <            }
974 >            int s = top, cap; ForkJoinTask<?>[] a;
975 >            if ((a = array) != null && (cap = a.length) > 0 && base != s-- &&
976 >                (t = getAndClearSlot(a, (cap - 1) & s)) != null)
977 >                top = s;
978              return t;
979          }
980  
981          /**
982 <         * Returns next task, if one exists, in order specified by mode.
982 >         * Pops the given task for owner only if it is at the current top.
983           */
984 <        final ForkJoinTask<?> peek() {
985 <            int cap; ForkJoinTask<?>[] a;
986 <            return ((a = array) != null && (cap = a.length) > 0) ?
987 <                a[(cap - 1) & ((id & FIFO) != 0 ? base : top - 1)] : null;
984 >        final boolean tryUnpush(ForkJoinTask<?> task) {
985 >            int s = top - 1, cap, k; ForkJoinTask<?>[] a;
986 >            if ((a = array) != null && task != null && (cap = a.length) > 0 &&
987 >                a[k = (cap - 1) & s] == task && casSlotToNull(a, k, task)) {
988 >                top = s;
989 >                return true;
990 >            }
991 >            return false;
992          }
993  
994          /**
995 <         * Pops the given task only if it is at the current top.
995 >         * Locking version of tryUnpush.
996           */
997 <        final boolean tryUnpush(ForkJoinTask<?> task) {
998 <            boolean popped = false;
999 <            int s, cap; ForkJoinTask<?>[] a;
1000 <            if ((a = array) != null && (cap = a.length) > 0 &&
1001 <                (s = top) != base &&
1002 <                (popped = QA.compareAndSet(a, (cap - 1) & --s, task, null)))
1003 <                TOP.setOpaque(this, s);
1004 <            return popped;
997 >        final boolean externalTryUnpush(ForkJoinTask<?> task) {
998 >            boolean taken = false;
999 >            int s = top, cap, k; ForkJoinTask<?>[] a;
1000 >            if ((a = array) != null && task != null && (cap = a.length) > 0 &&
1001 >                a[k = (cap - 1) & (s - 1)] == task && tryLock()) {
1002 >                if (top == s && array == a &&
1003 >                    (taken = casSlotToNull(a, k, task)))
1004 >                    top = s - 1;
1005 >                source = 0; // release lock
1006 >            }
1007 >            return taken;
1008          }
1009  
1010          /**
1011 <         * Shared version of tryUnpush.
1012 <         */
1013 <        final boolean tryLockedUnpush(ForkJoinTask<?> task) {
1014 <            boolean popped = false;
1015 <            int s = top - 1, k, cap; ForkJoinTask<?>[] a;
1016 <            if ((a = array) != null && (cap = a.length) > 0 &&
1017 <                a[k = (cap - 1) & s] == task && tryLockPhase()) {
1018 <                if (top == s + 1 && array == a &&
1019 <                    (popped = QA.compareAndSet(a, k, task, null)))
1020 <                    top = s;
1021 <                releasePhaseLock();
1011 >         * Deep form of pop: Traverses from top and removes task if
1012 >         * present, shifting others to fill gap.
1013 >         */
1014 >        final boolean tryRemove(ForkJoinTask<?> task) {
1015 >            int s = top, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
1016 >            if ((a = array) != null && task != null && (cap = a.length) > 0) {
1017 >                for (int m = cap - 1, d = s - base, i = --s, k; d > 0; --i,--d) {
1018 >                    if ((t = a[k = i & m]) == task) {
1019 >                        if (!casSlotToNull(a, k, t))
1020 >                            break;
1021 >                        for (int j = i; j != s; ) // shift down
1022 >                            a[j & m] = getAndClearSlot(a, ++j & m);
1023 >                        top = s;
1024 >                        return true;
1025 >                    }
1026 >                }
1027              }
1028 <            return popped;
1028 >            return false;
1029          }
1030  
1031 +        // variants of poll
1032 +
1033          /**
1034 <         * Removes and cancels all known tasks, ignoring any exceptions.
1034 >         * Tries once to poll next task in FIFO order, failing on
1035 >         * inconsistency or contention.
1036           */
1037 <        final void cancelAll() {
1038 <            for (ForkJoinTask<?> t; (t = poll()) != null; )
1039 <                ForkJoinTask.cancelIgnoringExceptions(t);
1037 >        final ForkJoinTask<?> tryPoll() {
1038 >            int cap, b, k; ForkJoinTask<?>[] a;
1039 >            if ((a = array) != null && (cap = a.length) > 0) {
1040 >                ForkJoinTask<?> t = getSlot(a, k = (cap - 1) & (b = base));
1041 >                if (base == b++ && t != null && casSlotToNull(a, k, t)) {
1042 >                    setBaseOpaque(b);
1043 >                    return t;
1044 >                }
1045 >            }
1046 >            return null;
1047          }
1048  
976        // Specialized execution methods
977
1049          /**
1050 <         * Runs the given (stolen) task if nonnull, as well as
980 <         * remaining local tasks and others available from the given
981 <         * queue, up to bound n (to avoid infinite unfairness).
1050 >         * Takes next task, if one exists, in order specified by mode.
1051           */
1052 <        final void topLevelExec(ForkJoinTask<?> t, WorkQueue q, int n) {
1053 <            int nstolen = 1;
1054 <            for (int j = 0;;) {
1055 <                if (t != null)
1056 <                    t.doExec();
1057 <                if (j++ <= n)
1058 <                    t = nextLocalTask();
1059 <                else {
1060 <                    j = 0;
1061 <                    t = null;
1062 <                }
994 <                if (t == null) {
995 <                    if (q != null && (t = q.poll()) != null) {
996 <                        ++nstolen;
997 <                        j = 0;
1052 >        final ForkJoinTask<?> nextLocalTask(int cfg) {
1053 >            ForkJoinTask<?> t = null;
1054 >            int s = top, cap; ForkJoinTask<?>[] a;
1055 >            if ((a = array) != null && (cap = a.length) > 0) {
1056 >                for (int b, d;;) {
1057 >                    if ((d = s - (b = base)) <= 0)
1058 >                        break;
1059 >                    if (d == 1 || (cfg & FIFO) == 0) {
1060 >                        if ((t = getAndClearSlot(a, --s & (cap - 1))) != null)
1061 >                            top = s;
1062 >                        break;
1063                      }
1064 <                    else if (j != 0)
1064 >                    if ((t = getAndClearSlot(a, b++ & (cap - 1))) != null) {
1065 >                        setBaseOpaque(b);
1066                          break;
1067 +                    }
1068                  }
1069              }
1070 <            ForkJoinWorkerThread thread = owner;
1004 <            nsteals += nstolen;
1005 <            source = 0;
1006 <            if (thread != null)
1007 <                thread.afterTopLevelExec();
1070 >            return t;
1071          }
1072  
1073          /**
1074 <         * If present, removes task from queue and executes it.
1074 >         * Takes next task, if one exists, using configured mode.
1075           */
1076 <        final void tryRemoveAndExec(ForkJoinTask<?> task) {
1077 <            ForkJoinTask<?>[] a; int s, cap;
1078 <            if ((a = array) != null && (cap = a.length) > 0 &&
1079 <                (s = top) - base > 0) { // traverse from top
1080 <                for (int m = cap - 1, ns = s - 1, i = ns; ; --i) {
1081 <                    int index = i & m;
1082 <                    ForkJoinTask<?> t = (ForkJoinTask<?>)QA.get(a, index);
1083 <                    if (t == null)
1084 <                        break;
1085 <                    else if (t == task) {
1086 <                        if (QA.compareAndSet(a, index, t, null)) {
1087 <                            top = ns;   // safely shift down
1088 <                            for (int j = i; j != ns; ++j) {
1089 <                                ForkJoinTask<?> f;
1090 <                                int pindex = (j + 1) & m;
1091 <                                f = (ForkJoinTask<?>)QA.get(a, pindex);
1092 <                                QA.setVolatile(a, pindex, null);
1093 <                                int jindex = j & m;
1094 <                                QA.setRelease(a, jindex, f);
1095 <                            }
1096 <                            VarHandle.releaseFence();
1097 <                            t.doExec();
1098 <                        }
1099 <                        break;
1100 <                    }
1101 <                }
1076 >        final ForkJoinTask<?> nextLocalTask() {
1077 >            return nextLocalTask(config);
1078 >        }
1079 >
1080 >        /**
1081 >         * Returns next task, if one exists, in order specified by mode.
1082 >         */
1083 >        final ForkJoinTask<?> peek() {
1084 >            VarHandle.acquireFence();
1085 >            int cap; ForkJoinTask<?>[] a;
1086 >            return ((a = array) != null && (cap = a.length) > 0) ?
1087 >                a[(cap - 1) & ((config & FIFO) != 0 ? base : top - 1)] : null;
1088 >        }
1089 >
1090 >        // specialized execution methods
1091 >
1092 >        /**
1093 >         * Runs the given (stolen) task if nonnull, as well as
1094 >         * remaining local tasks and/or others available from the
1095 >         * given queue.
1096 >         */
1097 >        final void topLevelExec(ForkJoinTask<?> task, WorkQueue q) {
1098 >            int cfg = config, nstolen = 1;
1099 >            while (task != null) {
1100 >                task.doExec();
1101 >                if ((task = nextLocalTask(cfg)) == null &&
1102 >                    q != null && (task = q.tryPoll()) != null)
1103 >                    ++nstolen;
1104              }
1105 +            nsteals += nstolen;
1106 +            source = 0;
1107 +            if ((cfg & INNOCUOUS) != 0)
1108 +                ThreadLocalRandom.eraseThreadLocals(Thread.currentThread());
1109          }
1110  
1111          /**
# Line 1044 | Line 1113 | public class ForkJoinPool extends Abstra
1113           * until done, not found, or limit exceeded.
1114           *
1115           * @param task root of CountedCompleter computation
1116 +         * @param owned true if owned by a ForkJoinWorkerThread
1117           * @param limit max runs, or zero for no limit
1118 <         * @param shared true if must lock to extract task
1049 <         * @return task status on exit
1118 >         * @return ask status on exit
1119           */
1120 <        final int helpCC(CountedCompleter<?> task, int limit, boolean shared) {
1121 <            int status = 0;
1122 <            if (task != null && (status = task.status) >= 0) {
1123 <                int s, k, cap; ForkJoinTask<?>[] a;
1124 <                while ((a = array) != null && (cap = a.length) > 0 &&
1125 <                       (s = top) - base > 0) {
1126 <                    CountedCompleter<?> v = null;
1127 <                    ForkJoinTask<?> o = a[k = (cap - 1) & (s - 1)];
1128 <                    if (o instanceof CountedCompleter) {
1129 <                        CountedCompleter<?> t = (CountedCompleter<?>)o;
1130 <                        for (CountedCompleter<?> f = t;;) {
1131 <                            if (f != task) {
1132 <                                if ((f = f.completer) == null)
1133 <                                    break;
1065 <                            }
1066 <                            else if (shared) {
1067 <                                if (tryLockPhase()) {
1068 <                                    if (top == s && array == a &&
1069 <                                        QA.compareAndSet(a, k, t, null)) {
1070 <                                        top = s - 1;
1071 <                                        v = t;
1072 <                                    }
1073 <                                    releasePhaseLock();
1074 <                                }
1075 <                                break;
1076 <                            }
1077 <                            else {
1078 <                                if (QA.compareAndSet(a, k, t, null)) {
1079 <                                    top = s - 1;
1080 <                                    v = t;
1081 <                                }
1082 <                                break;
1083 <                            }
1120 >        final int helpComplete(CountedCompleter<?> task, boolean owned,
1121 >                               int limit) {
1122 >            int status = 0, cap, k, p, s; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
1123 >            while (task != null && (status = task.status) >= 0 &&
1124 >                   (a = array) != null && (cap = a.length) > 0 &&
1125 >                   (t = a[k = (cap - 1) & (s = (p = top) - 1)])
1126 >                   instanceof CountedCompleter) {
1127 >                CountedCompleter<?> f = (CountedCompleter<?>)t;
1128 >                boolean taken = false;
1129 >                for (;;) {     // exec if root task is a completer of t
1130 >                    if (f == task) {
1131 >                        if (owned) {
1132 >                            if ((taken = casSlotToNull(a, k, t)))
1133 >                                top = s;
1134                          }
1135 +                        else if (tryLock()) {
1136 +                            if (top == p && array == a &&
1137 +                                (taken = casSlotToNull(a, k, t)))
1138 +                                top = s;
1139 +                            source = 0;
1140 +                        }
1141 +                        break;
1142                      }
1143 <                    if (v != null)
1087 <                        v.doExec();
1088 <                    if ((status = task.status) < 0 || v == null ||
1089 <                        (limit != 0 && --limit == 0))
1143 >                    else if ((f = f.completer) == null)
1144                          break;
1145                  }
1146 +                if (!taken)
1147 +                    break;
1148 +                t.doExec();
1149 +                if (limit != 0 && --limit == 0)
1150 +                    break;
1151              }
1152              return status;
1153          }
1154  
1155          /**
1156           * Tries to poll and run AsynchronousCompletionTasks until
1157 <         * none found or blocker is released
1157 >         * none found or blocker is released.
1158           *
1159           * @param blocker the blocker
1160           */
1161          final void helpAsyncBlocker(ManagedBlocker blocker) {
1162 <            if (blocker != null) {
1163 <                int b, k, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
1164 <                while ((a = array) != null && (cap = a.length) > 0 &&
1165 <                       top - (b = base) > 0) {
1166 <                    t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b);
1167 <                    if (blocker.isReleasable())
1168 <                        break;
1169 <                    else if (base == b++ && t != null) {
1170 <                        if (!(t instanceof CompletableFuture.
1171 <                              AsynchronousCompletionTask))
1113 <                            break;
1114 <                        else if (QA.compareAndSet(a, k, t, null)) {
1115 <                            BASE.setOpaque(this, b);
1116 <                            t.doExec();
1117 <                        }
1118 <                    }
1162 >            int cap, b, d, k; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
1163 >            while (blocker != null && (d = top - (b = base)) > 0 &&
1164 >                   (a = array) != null && (cap = a.length) > 0 &&
1165 >                   (((t = getSlot(a, k = (cap - 1) & b)) == null && d > 1) ||
1166 >                    t instanceof
1167 >                    CompletableFuture.AsynchronousCompletionTask) &&
1168 >                   !blocker.isReleasable()) {
1169 >                if (t != null && base == b++ && casSlotToNull(a, k, t)) {
1170 >                    setBaseOpaque(b);
1171 >                    t.doExec();
1172                  }
1173              }
1174          }
1175  
1176 +        // misc
1177 +
1178 +        /** AccessControlContext for innocuous workers, created on 1st use. */
1179 +        private static AccessControlContext INNOCUOUS_ACC;
1180 +
1181 +        /**
1182 +         * Initializes (upon registration) InnocuousForkJoinWorkerThreads.
1183 +         */
1184 +        final void initializeInnocuousWorker() {
1185 +            AccessControlContext acc; // racy construction OK
1186 +            if ((acc = INNOCUOUS_ACC) == null)
1187 +                INNOCUOUS_ACC = acc = new AccessControlContext(
1188 +                    new ProtectionDomain[] { new ProtectionDomain(null, null) });
1189 +            Thread t = Thread.currentThread();
1190 +            ThreadLocalRandom.setInheritedAccessControlContext(t, acc);
1191 +            ThreadLocalRandom.eraseThreadLocals(t);
1192 +        }
1193 +
1194          /**
1195           * Returns true if owned and not known to be blocked.
1196           */
# Line 1131 | Line 1202 | public class ForkJoinPool extends Abstra
1202                      s != Thread.State.TIMED_WAITING);
1203          }
1204  
1134        // VarHandle mechanics.
1135        static final VarHandle PHASE;
1136        static final VarHandle BASE;
1137        static final VarHandle TOP;
1205          static {
1206              try {
1207 +                QA = MethodHandles.arrayElementVarHandle(ForkJoinTask[].class);
1208                  MethodHandles.Lookup l = MethodHandles.lookup();
1209 <                PHASE = l.findVarHandle(WorkQueue.class, "phase", int.class);
1209 >                SOURCE = l.findVarHandle(WorkQueue.class, "source", int.class);
1210                  BASE = l.findVarHandle(WorkQueue.class, "base", int.class);
1143                TOP = l.findVarHandle(WorkQueue.class, "top", int.class);
1211              } catch (ReflectiveOperationException e) {
1212                  throw new ExceptionInInitializerError(e);
1213              }
# Line 1184 | Line 1251 | public class ForkJoinPool extends Abstra
1251      private static final int COMMON_MAX_SPARES;
1252  
1253      /**
1254 <     * Sequence number for creating workerNamePrefix.
1188 <     */
1189 <    private static int poolNumberSequence;
1190 <
1191 <    /**
1192 <     * Returns the next sequence number. We don't expect this to
1193 <     * ever contend, so use simple builtin sync.
1254 >     * Sequence number for creating worker names
1255       */
1256 <    private static final synchronized int nextPoolId() {
1196 <        return ++poolNumberSequence;
1197 <    }
1256 >    private static volatile int poolIds;
1257  
1258      // static configuration constants
1259  
# Line 1219 | Line 1278 | public class ForkJoinPool extends Abstra
1278       */
1279      private static final int DEFAULT_COMMON_MAX_SPARES = 256;
1280  
1222    /**
1223     * Increment for seed generators. See class ThreadLocal for
1224     * explanation.
1225     */
1226    private static final int SEED_INCREMENT = 0x9e3779b9;
1227
1281      /*
1282       * Bits and masks for field ctl, packed with 4 16 bit subfields:
1283       * RC: Number of released (unqueued) workers minus target parallelism
# Line 1242 | Line 1295 | public class ForkJoinPool extends Abstra
1295       * deal with possibly negative fields, we use casts in and out of
1296       * "short" and/or signed shifts to maintain signedness.
1297       *
1298 <     * Because it occupies uppermost bits, we can add one release count
1299 <     * using getAndAddLong of RC_UNIT, rather than CAS, when returning
1300 <     * from a blocked join.  Other updates entail multiple subfields
1301 <     * and masking, requiring CAS.
1298 >     * Because it occupies uppermost bits, we can add one release
1299 >     * count using getAndAdd of RC_UNIT, rather than CAS, when
1300 >     * returning from a blocked join.  Other updates entail multiple
1301 >     * subfields and masking, requiring CAS.
1302       *
1303       * The limits packed in field "bounds" are also offset by the
1304       * parallelism level to make them comparable to the ctl rc and tc
# Line 1269 | Line 1322 | public class ForkJoinPool extends Abstra
1322  
1323      // Instance fields
1324  
1272    volatile long stealCount;            // collects worker nsteals
1325      final long keepAlive;                // milliseconds before dropping if idle
1326 <    int indexSeed;                       // next worker index
1326 >    volatile long stealCount;            // collects worker nsteals
1327 >    int scanRover;                       // advances across pollScan calls
1328 >    volatile int threadIds;              // for worker thread names
1329      final int bounds;                    // min, max threads packed as shorts
1330      volatile int mode;                   // parallelism, runstate, queue mode
1331 <    WorkQueue[] workQueues;              // main registry
1332 <    final String workerNamePrefix;       // for worker thread string; sync lock
1331 >    WorkQueue[] queues;                  // main registry
1332 >    final ReentrantLock registrationLock;
1333 >    Condition termination;               // lazily constructed
1334 >    final String workerNamePrefix;       // null for common pool
1335      final ForkJoinWorkerThreadFactory factory;
1336      final UncaughtExceptionHandler ueh;  // per-worker UEH
1337      final Predicate<? super ForkJoinPool> saturate;
# Line 1283 | Line 1339 | public class ForkJoinPool extends Abstra
1339      @jdk.internal.vm.annotation.Contended("fjpctl") // segregate
1340      volatile long ctl;                   // main pool control
1341  
1342 +    // Support for atomic operations
1343 +    private static final VarHandle CTL;
1344 +    private static final VarHandle MODE;
1345 +    private static final VarHandle THREADIDS;
1346 +    private static final VarHandle POOLIDS;
1347 +    private boolean compareAndSetCtl(long c, long v) {
1348 +        return CTL.compareAndSet(this, c, v);
1349 +    }
1350 +    private long compareAndExchangeCtl(long c, long v) {
1351 +        return (long)CTL.compareAndExchange(this, c, v);
1352 +    }
1353 +    private long getAndAddCtl(long v) {
1354 +        return (long)CTL.getAndAdd(this, v);
1355 +    }
1356 +    private int getAndBitwiseOrMode(int v) {
1357 +        return (int)MODE.getAndBitwiseOr(this, v);
1358 +    }
1359 +    private int getAndAddThreadIds(int x) {
1360 +        return (int)THREADIDS.getAndAdd(this, x);
1361 +    }
1362 +    private static int getAndAddPoolIds(int x) {
1363 +        return (int)POOLIDS.getAndAdd(x);
1364 +    }
1365 +
1366      // Creating, registering and deregistering workers
1367  
1368      /**
# Line 1309 | Line 1389 | public class ForkJoinPool extends Abstra
1389      }
1390  
1391      /**
1392 <     * Tries to add one worker, incrementing ctl counts before doing
1313 <     * so, relying on createWorker to back out on failure.
1314 <     *
1315 <     * @param c incoming ctl value, with total count negative and no
1316 <     * idle workers.  On CAS failure, c is refreshed and retried if
1317 <     * this holds (otherwise, a new worker is not needed).
1392 >     * Provides a name for ForkJoinWorkerThread constructor
1393       */
1394 <    private void tryAddWorker(long c) {
1395 <        do {
1396 <            long nc = ((RC_MASK & (c + RC_UNIT)) |
1397 <                       (TC_MASK & (c + TC_UNIT)));
1398 <            if (ctl == c && CTL.compareAndSet(this, c, nc)) {
1399 <                createWorker();
1325 <                break;
1326 <            }
1327 <        } while (((c = ctl) & ADD_WORKER) != 0L && (int)c == 0);
1394 >    final String nextWorkerThreadName() {
1395 >        String prefix = workerNamePrefix;
1396 >        int tid = getAndAddThreadIds(1) + 1;
1397 >        if (prefix == null) // commonPool has no prefix
1398 >            prefix = "ForkJoinPool.commonPool-worker-";
1399 >        return prefix.concat(Integer.toString(tid));
1400      }
1401  
1402      /**
1403 <     * Callback from ForkJoinWorkerThread constructor to establish and
1404 <     * record its WorkQueue.
1405 <     *
1406 <     * @param wt the worker thread
1407 <     * @return the worker's queue
1408 <     */
1409 <    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
1410 <        UncaughtExceptionHandler handler;
1411 <        wt.setDaemon(true);                             // configure thread
1412 <        if ((handler = ueh) != null)
1413 <            wt.setUncaughtExceptionHandler(handler);
1414 <        int tid = 0;                                    // for thread name
1415 <        int idbits = mode & FIFO;
1416 <        String prefix = workerNamePrefix;
1417 <        WorkQueue w = new WorkQueue(this, wt);
1418 <        if (prefix != null) {
1419 <            synchronized (prefix) {
1420 <                WorkQueue[] ws = workQueues; int n;
1421 <                int s = indexSeed += SEED_INCREMENT;
1422 <                idbits |= (s & ~(SMASK | FIFO | DORMANT));
1423 <                if (ws != null && (n = ws.length) > 1) {
1424 <                    int m = n - 1;
1425 <                    tid = m & ((s << 1) | 1);           // odd-numbered indices
1426 <                    for (int probes = n >>> 1;;) {      // find empty slot
1355 <                        WorkQueue q;
1356 <                        if ((q = ws[tid]) == null || q.phase == QUIET)
1357 <                            break;
1358 <                        else if (--probes == 0) {
1359 <                            tid = n | 1;                // resize below
1360 <                            break;
1361 <                        }
1362 <                        else
1363 <                            tid = (tid + 2) & m;
1364 <                    }
1365 <                    w.phase = w.id = tid | idbits;      // now publishable
1403 >     * Finishes initializing and records owned queue.
1404 >     *
1405 >     * @param w caller's WorkQueue
1406 >     */
1407 >    final void registerWorker(WorkQueue w) {
1408 >        ReentrantLock lock = registrationLock;
1409 >        ThreadLocalRandom.localInit();
1410 >        int seed = ThreadLocalRandom.getProbe();
1411 >        if (w != null && lock != null) {
1412 >            int modebits = (mode & FIFO) | w.config;
1413 >            w.array = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
1414 >            w.stackPred = seed;                         // stash for runWorker
1415 >            if ((modebits & INNOCUOUS) != 0)
1416 >                w.initializeInnocuousWorker();
1417 >            int id = (seed << 1) | 1;                   // initial index guess
1418 >            lock.lock();
1419 >            try {
1420 >                WorkQueue[] qs; int n;                  // find queue index
1421 >                if ((qs = queues) != null && (n = qs.length) > 0) {
1422 >                    int k = n, m = n - 1;
1423 >                    for (; qs[id &= m] != null && k > 0; id -= 2, k -= 2);
1424 >                    if (k == 0)
1425 >                        id = n | 1;                     // resize below
1426 >                    w.phase = w.config = id | modebits; // now publishable
1427  
1428 <                    if (tid < n)
1429 <                        ws[tid] = w;
1428 >                    if (id < n)
1429 >                        qs[id] = w;
1430                      else {                              // expand array
1431 <                        int an = n << 1;
1431 >                        int an = n << 1, am = an - 1;
1432                          WorkQueue[] as = new WorkQueue[an];
1433 <                        as[tid] = w;
1434 <                        int am = an - 1;
1435 <                        for (int j = 0; j < n; ++j) {
1436 <                            WorkQueue v;                // copy external queue
1437 <                            if ((v = ws[j]) != null)    // position may change
1438 <                                as[v.id & am & SQMASK] = v;
1439 <                            if (++j >= n)
1379 <                                break;
1380 <                            as[j] = ws[j];              // copy worker
1433 >                        as[id & am] = w;
1434 >                        for (int j = 1; j < n; j += 2)
1435 >                            as[j] = qs[j];
1436 >                        for (int j = 0; j < n; j += 2) {
1437 >                            WorkQueue q;
1438 >                            if ((q = qs[j]) != null)    // shared queues may move
1439 >                                as[q.config & am] = q;
1440                          }
1441 <                        workQueues = as;
1441 >                        VarHandle.releaseFence();       // fill before publish
1442 >                        queues = as;
1443                      }
1444                  }
1445 +            } finally {
1446 +                lock.unlock();
1447              }
1386            wt.setName(prefix.concat(Integer.toString(tid)));
1448          }
1388        return w;
1449      }
1450  
1451      /**
# Line 1398 | Line 1458 | public class ForkJoinPool extends Abstra
1458       * @param ex the exception causing failure, or null if none
1459       */
1460      final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
1461 +        ReentrantLock lock = registrationLock;
1462          WorkQueue w = null;
1463 <        int phase = 0;
1464 <        if (wt != null && (w = wt.workQueue) != null) {
1465 <            Object lock = workerNamePrefix;
1466 <            int wid = w.id;
1467 <            long ns = (long)w.nsteals & 0xffffffffL;
1468 <            if (lock != null) {
1469 <                synchronized (lock) {
1470 <                    WorkQueue[] ws; int n, i;         // remove index from array
1471 <                    if ((ws = workQueues) != null && (n = ws.length) > 0 &&
1472 <                        ws[i = wid & (n - 1)] == w)
1473 <                        ws[i] = null;
1474 <                    stealCount += ns;
1475 <                }
1476 <            }
1477 <            phase = w.phase;
1478 <        }
1479 <        if (phase != QUIET) {                         // else pre-adjusted
1480 <            long c;                                   // decrement counts
1481 <            do {} while (!CTL.weakCompareAndSet
1482 <                         (this, c = ctl, ((RC_MASK & (c - RC_UNIT)) |
1483 <                                          (TC_MASK & (c - TC_UNIT)) |
1423 <                                          (SP_MASK & c))));
1463 >        int cfg = 0;
1464 >        if (wt != null && (w = wt.workQueue) != null && lock != null) {
1465 >            WorkQueue[] qs; int n, i;
1466 >            cfg = w.config;
1467 >            long ns = w.nsteals & 0xffffffffL;
1468 >            lock.lock();                             // remove index from array
1469 >            if ((qs = queues) != null && (n = qs.length) > 0 &&
1470 >                qs[i = cfg & (n - 1)] == w)
1471 >                qs[i] = null;
1472 >            stealCount += ns;                        // accumulate steals
1473 >            lock.unlock();
1474 >            long c = ctl;
1475 >            if (w.phase != QUIET)                    // decrement counts
1476 >                do {} while (c != (c = compareAndExchangeCtl(
1477 >                                       c, ((RC_MASK & (c - RC_UNIT)) |
1478 >                                           (TC_MASK & (c - TC_UNIT)) |
1479 >                                           (SP_MASK & c)))));
1480 >            else if ((int)c == 0)                    // was dropped on timeout
1481 >                cfg = 0;                             // suppress signal if last
1482 >            for (ForkJoinTask<?> t; (t = w.pop()) != null; )
1483 >                ForkJoinTask.cancelIgnoringExceptions(t); // cancel tasks
1484          }
1425        if (w != null)
1426            w.cancelAll();                            // cancel remaining tasks
1485  
1486 <        if (!tryTerminate(false, false) &&            // possibly replace worker
1487 <            w != null && w.array != null)             // avoid repeated failures
1488 <            signalWork(null);
1431 <
1432 <        if (ex == null)                               // help clean on way out
1433 <            ForkJoinTask.helpExpungeStaleExceptions();
1434 <        else                                          // rethrow
1486 >        if (!tryTerminate(false, false) && w != null && (cfg & SRC) != 0)
1487 >            signalWork();                            // possibly replace worker
1488 >        if (ex != null)
1489              ForkJoinTask.rethrow(ex);
1490      }
1491  
1492 <    /**
1492 >    /*
1493       * Tries to create or release a worker if too few are running.
1440     * @param q if non-null recheck if empty on CAS failure
1494       */
1495 <    final void signalWork(WorkQueue q) {
1496 <        for (;;) {
1497 <            long c; int sp; WorkQueue[] ws; int i; WorkQueue v;
1498 <            if ((c = ctl) >= 0L)                      // enough workers
1499 <                break;
1500 <            else if ((sp = (int)c) == 0) {            // no idle workers
1501 <                if ((c & ADD_WORKER) != 0L)           // too few workers
1502 <                    tryAddWorker(c);
1503 <                break;
1495 >    final void signalWork() {
1496 >        for (long c = ctl; c < 0L;) {
1497 >            int sp, i; WorkQueue[] qs; WorkQueue v;
1498 >            if ((sp = (int)c & ~UNSIGNALLED) == 0) {  // no idle workers
1499 >                if ((c & ADD_WORKER) == 0L)           // enough total workers
1500 >                    break;
1501 >                if (c == (c = compareAndExchangeCtl(
1502 >                              c, ((RC_MASK & (c + RC_UNIT)) |
1503 >                                  (TC_MASK & (c + TC_UNIT)))))) {
1504 >                    createWorker();
1505 >                    break;
1506 >                }
1507              }
1508 <            else if ((ws = workQueues) == null)
1508 >            else if ((qs = queues) == null)
1509                  break;                                // unstarted/terminated
1510 <            else if (ws.length <= (i = sp & SMASK))
1510 >            else if (qs.length <= (i = sp & SMASK))
1511                  break;                                // terminated
1512 <            else if ((v = ws[i]) == null)
1512 >            else if ((v = qs[i]) == null)
1513                  break;                                // terminating
1514              else {
1459                int np = sp & ~UNSIGNALLED;
1460                int vp = v.phase;
1515                  long nc = (v.stackPred & SP_MASK) | (UC_MASK & (c + RC_UNIT));
1516                  Thread vt = v.owner;
1517 <                if (sp == vp && CTL.compareAndSet(this, c, nc)) {
1518 <                    v.phase = np;
1519 <                    if (vt != null && v.source < 0)
1466 <                        LockSupport.unpark(vt);
1517 >                if (c == (c = compareAndExchangeCtl(c, nc))) {
1518 >                    v.phase = sp;
1519 >                    LockSupport.unpark(vt);           // release idle worker
1520                      break;
1521                  }
1469                else if (q != null && q.isEmpty())     // no need to retry
1470                    break;
1522              }
1523          }
1524      }
1525  
1526      /**
1527 <     * Tries to decrement counts (sometimes implicitly) and possibly
1528 <     * arrange for a compensating worker in preparation for blocking:
1478 <     * If not all core workers yet exist, creates one, else if any are
1479 <     * unreleased (possibly including caller) releases one, else if
1480 <     * fewer than the minimum allowed number of workers running,
1481 <     * checks to see that they are all active, and if so creates an
1482 <     * extra worker unless over maximum limit and policy is to
1483 <     * saturate.  Most of these steps can fail due to interference, in
1484 <     * which case 0 is returned so caller will retry. A negative
1485 <     * return value indicates that the caller doesn't need to
1486 <     * re-adjust counts when later unblocked.
1527 >     * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1528 >     * See above for explanation.
1529       *
1530 <     * @return 1: block then adjust, -1: block without adjust, 0 : retry
1530 >     * @param w caller's WorkQueue (may be null on failed initialization)
1531       */
1532 <    private int tryCompensate(WorkQueue w) {
1533 <        int t, n, sp;
1534 <        long c = ctl;
1535 <        WorkQueue[] ws = workQueues;
1536 <        if ((t = (short)(c >>> TC_SHIFT)) >= 0) {
1537 <            if (ws == null || (n = ws.length) <= 0 || w == null)
1538 <                return 0;                        // disabled
1539 <            else if ((sp = (int)c) != 0) {       // replace or release
1540 <                WorkQueue v = ws[sp & (n - 1)];
1541 <                int wp = w.phase;
1542 <                long uc = UC_MASK & ((wp < 0) ? c + RC_UNIT : c);
1543 <                int np = sp & ~UNSIGNALLED;
1544 <                if (v != null) {
1545 <                    int vp = v.phase;
1546 <                    Thread vt = v.owner;
1547 <                    long nc = ((long)v.stackPred & SP_MASK) | uc;
1548 <                    if (vp == sp && CTL.compareAndSet(this, c, nc)) {
1549 <                        v.phase = np;
1550 <                        if (vt != null && v.source < 0)
1551 <                            LockSupport.unpark(vt);
1552 <                        return (wp < 0) ? -1 : 1;
1553 <                    }
1532 >    final void runWorker(WorkQueue w) {
1533 >        if (w != null) {                        // skip on failed init
1534 >            w.config |= SRC;                    // mark as valid source
1535 >            int r = w.stackPred, src = 0;       // use seed from registerWorker
1536 >            do {
1537 >                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift
1538 >            } while ((src = scan(w, src, r)) >= 0 ||
1539 >                     (src = awaitWork(w)) == 0);
1540 >        }
1541 >    }
1542 >
1543 >    /**
1544 >     * Scans for and if found executes top-level tasks: Tries to poll
1545 >     * each queue starting at a random index with random stride,
1546 >     * returning source id or retry indicator if contended or
1547 >     * inconsistent.
1548 >     *
1549 >     * @param w caller's WorkQueue
1550 >     * @param prevSrc the previous queue stolen from in current phase, or 0
1551 >     * @param r random seed
1552 >     * @return id of queue if taken, negative if none found, prevSrc for retry
1553 >     */
1554 >    private int scan(WorkQueue w, int prevSrc, int r) {
1555 >        WorkQueue[] qs = queues;
1556 >        int n = (w == null || qs == null) ? 0 : qs.length;
1557 >        for (int step = (r >>> 16) | 1, i = n; i > 0; --i, r += step) {
1558 >            int j, cap, b; WorkQueue q; ForkJoinTask<?>[] a;
1559 >            if ((q = qs[j = r & (n - 1)]) != null && // poll at qs[j].array[k]
1560 >                (a = q.array) != null && (cap = a.length) > 0) {
1561 >                int k = (cap - 1) & (b = q.base), nextBase = b + 1;
1562 >                int nextIndex = (cap - 1) & nextBase, src = j | SRC;
1563 >                ForkJoinTask<?> t = WorkQueue.getSlot(a, k);
1564 >                if (q.base != b)                // inconsistent
1565 >                    return prevSrc;
1566 >                else if (t != null && WorkQueue.casSlotToNull(a, k, t)) {
1567 >                    q.base = nextBase;
1568 >                    ForkJoinTask<?> next = a[nextIndex];
1569 >                    if ((w.source = src) != prevSrc && next != null)
1570 >                        signalWork();           // propagate
1571 >                    w.topLevelExec(t, q);
1572 >                    return src;
1573 >                }
1574 >                else if (a[nextIndex] != null)  // revisit
1575 >                    return prevSrc;
1576 >            }
1577 >        }
1578 >        return (queues != qs) ? prevSrc: -1;    // possibly resized
1579 >    }
1580 >
1581 >    /**
1582 >     * Advances worker phase, pushes onto ctl stack, and awaits signal
1583 >     * or reports termination.
1584 >     *
1585 >     * @return negative if terminated, else 0
1586 >     */
1587 >    private int awaitWork(WorkQueue w) {
1588 >        if (w == null)
1589 >            return -1;                       // already terminated
1590 >        int phase, ac;                       // advance phase
1591 >        w.phase = (phase = w.phase + SS_SEQ) | UNSIGNALLED;
1592 >        long prevCtl = ctl, c;               // enqueue
1593 >        do {
1594 >            w.stackPred = (int)prevCtl;
1595 >            c = ((prevCtl - RC_UNIT) & UC_MASK) | (phase & SP_MASK);
1596 >        } while (prevCtl != (prevCtl = compareAndExchangeCtl(prevCtl, c)));
1597 >
1598 >        LockSupport.setCurrentBlocker(this); // prepare to block (exit also OK)
1599 >        long deadline = 0L;                  // use timed wait if nonzero
1600 >        if ((ac = (int)(c >> RC_SHIFT)) + (mode & SMASK) <= 0) { // quiescent
1601 >            if ((deadline = System.currentTimeMillis() + keepAlive) == 0L)
1602 >                deadline = 1L;               // avoid zero
1603 >            WorkQueue[] qs = queues;         // check for racing submission
1604 >            int n = (qs == null || ctl != c) ? 0 : qs.length;
1605 >            for (int i = 0; i < n; i += 2) {
1606 >                WorkQueue q; ForkJoinTask<?>[] a; int cap;
1607 >                if ((q = qs[i]) != null && (a = q.array) != null &&
1608 >                    (cap = a.length) > 0 && a[(cap - 1) & q.base] != null) {
1609 >                    if (ctl == c && compareAndSetCtl(c, prevCtl))
1610 >                        w.phase = phase;     // self-signal
1611 >                    break;                   // else lost race
1612                  }
1513                return 0;
1613              }
1614 <            else if ((int)(c >> RC_SHIFT) -      // reduce parallelism
1615 <                     (short)(bounds & SMASK) > 0) {
1616 <                long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c));
1617 <                return CTL.compareAndSet(this, c, nc) ? 1 : 0;
1618 <            }
1619 <            else {                               // validate
1620 <                int md = mode, pc = md & SMASK, tc = pc + t, bc = 0;
1621 <                boolean unstable = false;
1622 <                for (int i = 1; i < n; i += 2) {
1623 <                    WorkQueue q; Thread wt; Thread.State ts;
1624 <                    if ((q = ws[i]) != null) {
1625 <                        if (q.source == 0) {
1626 <                            unstable = true;
1627 <                            break;
1628 <                        }
1629 <                        else {
1630 <                            --tc;
1631 <                            if ((wt = q.owner) != null &&
1632 <                                ((ts = wt.getState()) == Thread.State.BLOCKED ||
1633 <                                 ts == Thread.State.WAITING))
1535 <                                ++bc;            // worker is blocking
1536 <                        }
1537 <                    }
1538 <                }
1539 <                if (unstable || tc != 0 || ctl != c)
1540 <                    return 0;                    // inconsistent
1541 <                else if (t + pc >= MAX_CAP || t >= (bounds >>> SWIDTH)) {
1542 <                    Predicate<? super ForkJoinPool> sat;
1543 <                    if ((sat = saturate) != null && sat.test(this))
1544 <                        return -1;
1545 <                    else if (bc < pc) {          // lagging
1546 <                        Thread.yield();          // for retry spins
1547 <                        return 0;
1548 <                    }
1549 <                    else
1550 <                        throw new RejectedExecutionException(
1551 <                            "Thread limit exceeded replacing blocked worker");
1552 <                }
1614 >        }
1615 >        for (;;) {                           // await activation or termination
1616 >            if (w.phase >= 0)
1617 >                break;
1618 >            else if (tryTerminate(false, false))
1619 >                return -1;
1620 >            else if ((int)(ctl >> RC_SHIFT) > ac)
1621 >                Thread.onSpinWait();         // signal in progress
1622 >            else if (deadline != 0L)
1623 >                LockSupport.parkUntil(deadline);
1624 >            else
1625 >                LockSupport.park();
1626 >            if (w.phase >= 0)
1627 >                break;
1628 >            else if (deadline != 0L &&
1629 >                     deadline - System.currentTimeMillis() <= TIMEOUT_SLOP &&
1630 >                     compareAndSetCtl(c, ((UC_MASK & (c - TC_UNIT)) |
1631 >                                          (w.stackPred & SP_MASK)))) {
1632 >                w.phase = QUIET;
1633 >                return -1;                   // drop on timeout
1634              }
1635 +            else
1636 +                Thread.interrupted();        // clear status before repark
1637          }
1638 +        LockSupport.setCurrentBlocker(null);
1639 +        return 0;
1640 +    }
1641  
1642 <        long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); // expand pool
1643 <        return CTL.compareAndSet(this, c, nc) && createWorker() ? 1 : 0;
1642 >    /**
1643 >     * Tries to decrement counts (sometimes implicitly) and possibly
1644 >     * arrange for a compensating worker in preparation for
1645 >     * blocking. May fail due to interference, in which case -1 is
1646 >     * returned so caller may retry. A zero return value indicates
1647 >     * that the caller doesn't need to re-adjust counts when later
1648 >     * unblocked.
1649 >     *
1650 >     * @param c incoming ctl value
1651 >     * @return ADJUST: block then adjust, 0: block without adjust, -1 : retry
1652 >     */
1653 >    private int tryCompensate(long c) {
1654 >        Predicate<? super ForkJoinPool> sat;
1655 >        int b = bounds; // counts are signed; centered at parallelism level == 0
1656 >        int minActive = (short)(b & SMASK),
1657 >            maxTotal  = b >>> SWIDTH,
1658 >            active    = (int)(c >> RC_SHIFT),
1659 >            total     = (short)(c >>> TC_SHIFT), sp;
1660 >        if ((sp = (int)c & ~UNSIGNALLED) != 0) {   // activate idle worker
1661 >            WorkQueue[] qs; int n; WorkQueue v;
1662 >            if ((qs = queues) != null && (n = qs.length) > 0 &&
1663 >                (v = qs[sp & (n - 1)]) != null) {
1664 >                Thread vt = v.owner;
1665 >                long nc = ((long)v.stackPred & SP_MASK) | (UC_MASK & c);
1666 >                if (compareAndSetCtl(c, nc)) {
1667 >                    v.phase = sp;
1668 >                    LockSupport.unpark(vt);
1669 >                    return ADJUST;
1670 >                }
1671 >            }
1672 >            return -1;                               // retry
1673 >        }
1674 >        else if (total >= 0 && active > minActive) { // reduce parallelism
1675 >            long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c));
1676 >            return compareAndSetCtl(c, nc) ? ADJUST : -1;
1677 >        }
1678 >        else if (total < maxTotal) {                 // expand pool
1679 >            long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
1680 >            return !compareAndSetCtl(c, nc) ? -1 : !createWorker() ? 0 : ADJUST;
1681 >        }
1682 >        else if (!compareAndSetCtl(c, c))            // validate
1683 >            return -1;
1684 >        else if ((sat = saturate) != null && sat.test(this))
1685 >            return 0;
1686 >        else
1687 >            throw new RejectedExecutionException(
1688 >                "Thread limit exceeded replacing blocked worker");
1689      }
1690  
1691      /**
1692 <     * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1562 <     * See above for explanation.
1692 >     * Readjusts RC count; called from ForkJoinTask after blocking.
1693       */
1694 <    final void runWorker(WorkQueue w) {
1695 <        int r = (w.id ^ ThreadLocalRandom.nextSecondarySeed()) | FIFO; // rng
1696 <        w.array = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; // initialize
1697 <        for (;;) {
1698 <            int phase;
1699 <            if (scan(w, r)) {                     // scan until apparently empty
1700 <                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // move (xorshift)
1701 <            }
1702 <            else if ((phase = w.phase) >= 0) {    // enqueue, then rescan
1703 <                long np = (w.phase = (phase + SS_SEQ) | UNSIGNALLED) & SP_MASK;
1704 <                long c, nc;
1705 <                do {
1706 <                    w.stackPred = (int)(c = ctl);
1577 <                    nc = ((c - RC_UNIT) & UC_MASK) | np;
1578 <                } while (!CTL.weakCompareAndSet(this, c, nc));
1579 <            }
1580 <            else {                                // already queued
1581 <                int pred = w.stackPred;
1582 <                Thread.interrupted();             // clear before park
1583 <                w.source = DORMANT;               // enable signal
1584 <                long c = ctl;
1585 <                int md = mode, rc = (md & SMASK) + (int)(c >> RC_SHIFT);
1586 <                if (md < 0)                       // terminating
1587 <                    break;
1588 <                else if (rc <= 0 && (md & SHUTDOWN) != 0 &&
1589 <                         tryTerminate(false, false))
1590 <                    break;                        // quiescent shutdown
1591 <                else if (w.phase < 0) {
1592 <                    if (rc <= 0 && pred != 0 && phase == (int)c) {
1593 <                        long nc = (UC_MASK & (c - TC_UNIT)) | (SP_MASK & pred);
1594 <                        long d = keepAlive + System.currentTimeMillis();
1595 <                        LockSupport.parkUntil(this, d);
1596 <                        if (ctl == c &&           // drop on timeout if all idle
1597 <                            d - System.currentTimeMillis() <= TIMEOUT_SLOP &&
1598 <                            CTL.compareAndSet(this, c, nc)) {
1599 <                            w.phase = QUIET;
1600 <                            break;
1601 <                        }
1602 <                    }
1603 <                    else {
1604 <                        LockSupport.park(this);
1605 <                        if (w.phase < 0)          // one spurious wakeup check
1606 <                            LockSupport.park(this);
1607 <                    }
1608 <                }
1609 <                w.source = 0;                     // disable signal
1610 <            }
1611 <        }
1694 >    final void uncompensate() {
1695 >        getAndAddCtl(RC_UNIT);
1696 >    }
1697 >
1698 >    /**
1699 >     * Calls tryCompensate until success; needed for ForkJoinTask timed waits.
1700 >     *
1701 >     * @return 0 for no compensation, else ADJUST
1702 >     */
1703 >    final int preCompensate() {
1704 >        int comp;
1705 >        do {} while ((comp = tryCompensate(ctl)) < 0);
1706 >        return comp;
1707      }
1708  
1709      /**
1710 <     * Scans for and if found executes one or more top-level tasks from a queue.
1710 >     * Helps if possible until the given task is done.  Scans other
1711 >     * queues for a task produced by one of w's stealers; returning
1712 >     * compensated blocking sentinel if none are found.
1713       *
1714 <     * @return true if found an apparently non-empty queue, and
1715 <     * possibly ran task(s).
1716 <     */
1717 <    private boolean scan(WorkQueue w, int r) {
1718 <        WorkQueue[] ws; int n;
1719 <        if ((ws = workQueues) != null && (n = ws.length) > 0 && w != null) {
1720 <            for (int m = n - 1, j = r & m;;) {
1721 <                WorkQueue q; int b;
1722 <                if ((q = ws[j]) != null && q.top != (b = q.base)) {
1723 <                    int qid = q.id;
1724 <                    ForkJoinTask<?>[] a; int cap, k; ForkJoinTask<?> t;
1725 <                    if ((a = q.array) != null && (cap = a.length) > 0) {
1726 <                        t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b);
1727 <                        if (q.base == b++ && t != null &&
1728 <                            QA.compareAndSet(a, k, t, null)) {
1729 <                            q.base = b;
1730 <                            w.source = qid;
1731 <                            if (a[(cap - 1) & b] != null)
1732 <                                signalWork(q);    // help signal if more tasks
1733 <                            w.topLevelExec(t, q,  // random fairness bound
1734 <                                           (r | (1 << TOP_BOUND_SHIFT)) & SMASK);
1714 >     * @param task the task
1715 >     * @param w caller's WorkQueue
1716 >     * @return task status on exit, or ADJUST for compensated blocking
1717 >     */
1718 >    final int helpJoin(ForkJoinTask<?> task, WorkQueue w) {
1719 >        int s = 0;
1720 >        if (task != null && w != null) {
1721 >            int wsrc = w.source, wid = w.config & SMASK, r = wid + 2;
1722 >            boolean scan = true;
1723 >            long c = 0L;                          // track ctl stability
1724 >            outer: for (;;) {
1725 >                if ((s = task.status) < 0)
1726 >                    break;
1727 >                else if (!scan && c == (c = ctl)) {
1728 >                    if (mode < 0)
1729 >                        ForkJoinTask.cancelIgnoringExceptions(task);
1730 >                    else if ((s = tryCompensate(c)) >= 0)
1731 >                        break;                    // block
1732 >                }
1733 >                else {                            // scan for subtasks
1734 >                    scan = false;
1735 >                    WorkQueue[] qs = queues;
1736 >                    int n = (qs == null) ? 0 : qs.length, m = n - 1;
1737 >                    for (int i = n; i > 0; i -= 2, r += 2) {
1738 >                        int j; WorkQueue q, x, y; ForkJoinTask<?>[] a;
1739 >                        if ((q = qs[j = r & m]) != null) {
1740 >                            int sq = q.source & SMASK, cap, b;
1741 >                            if ((a = q.array) != null && (cap = a.length) > 0) {
1742 >                                int k = (cap - 1) & (b = q.base);
1743 >                                int nextBase = b + 1, src = j | SRC, sx;
1744 >                                ForkJoinTask<?> t = WorkQueue.getSlot(a, k);
1745 >                                boolean eligible = sq == wid ||
1746 >                                    ((x = qs[sq & m]) != null &&   // indirect
1747 >                                     ((sx = (x.source & SMASK)) == wid ||
1748 >                                      ((y = qs[sx & m]) != null && // 2-indirect
1749 >                                       (y.source & SMASK) == wid)));
1750 >                                if ((s = task.status) < 0)
1751 >                                    break outer;
1752 >                                else if ((q.source & SMASK) != sq ||
1753 >                                         q.base != b)
1754 >                                    scan = true;          // inconsistent
1755 >                                else if (t == null)
1756 >                                    scan |= (a[nextBase & (cap - 1)] != null ||
1757 >                                             q.top != b); // lagging
1758 >                                else if (eligible) {
1759 >                                    if (WorkQueue.casSlotToNull(a, k, t)) {
1760 >                                        q.base = nextBase;
1761 >                                        w.source = src;
1762 >                                        t.doExec();
1763 >                                        w.source = wsrc;
1764 >                                    }
1765 >                                    scan = true;
1766 >                                    break;
1767 >                                }
1768 >                            }
1769                          }
1770                      }
1640                    return true;
1771                  }
1642                else if (--n > 0)
1643                    j = (j + 1) & m;
1644                else
1645                    break;
1772              }
1773          }
1774 <        return false;
1774 >        return s;
1775      }
1776  
1777      /**
1778 <     * Helps and/or blocks until the given task is done or timeout.
1779 <     * First tries locally helping, then scans other queues for a task
1780 <     * produced by one of w's stealers; compensating and blocking if
1781 <     * none are found (rescanning if tryCompensate fails).
1782 <     *
1783 <     * @param w caller
1784 <     * @param task the task
1659 <     * @param deadline for timed waits, if nonzero
1660 <     * @return task status on exit
1778 >     * Version of helpJoin for CountedCompleters, also usable with
1779 >     * external submitter threads. Scans for and runs subtasks of the
1780 >     * given root task, compensating and blocking if none are found.
1781 >
1782 >     * @param task root of CountedCompleter computation
1783 >     * @param w caller's WorkQueue
1784 >     * @return task status on exit, or ADJUST for compensated blocking
1785       */
1786 <    final int awaitJoin(WorkQueue w, ForkJoinTask<?> task, long deadline) {
1786 >    final int helpComplete(CountedCompleter<?> task, WorkQueue w) {
1787          int s = 0;
1788 <        int seed = ThreadLocalRandom.nextSecondarySeed();
1789 <        if (w != null && task != null &&
1790 <            (!(task instanceof CountedCompleter) ||
1791 <             (s = w.helpCC((CountedCompleter<?>)task, 0, false)) >= 0)) {
1792 <            w.tryRemoveAndExec(task);
1793 <            int src = w.source, id = w.id;
1794 <            int r = (seed >>> 16) | 1, step = (seed & ~1) | 2;
1671 <            s = task.status;
1672 <            while (s >= 0) {
1673 <                WorkQueue[] ws;
1674 <                int n = (ws = workQueues) == null ? 0 : ws.length, m = n - 1;
1675 <                while (n > 0) {
1676 <                    WorkQueue q; int b;
1677 <                    if ((q = ws[r & m]) != null && q.source == id &&
1678 <                        q.top != (b = q.base)) {
1679 <                        ForkJoinTask<?>[] a; int cap, k;
1680 <                        int qid = q.id;
1681 <                        if ((a = q.array) != null && (cap = a.length) > 0) {
1682 <                            ForkJoinTask<?> t = (ForkJoinTask<?>)
1683 <                                QA.getAcquire(a, k = (cap - 1) & b);
1684 <                            if (q.source == id && q.base == b++ &&
1685 <                                t != null && QA.compareAndSet(a, k, t, null)) {
1686 <                                q.base = b;
1687 <                                w.source = qid;
1688 <                                t.doExec();
1689 <                                w.source = src;
1690 <                            }
1691 <                        }
1788 >        if (task != null && w != null) {
1789 >            int r = w.config;
1790 >            boolean owned = (r & 1) != 0, scan = true, locals = true;
1791 >            long c = 0L;
1792 >            outer: for (;;) {
1793 >                if (locals) {                     // try locals before scanning
1794 >                    if ((s = w.helpComplete(task, owned, 0)) < 0)
1795                          break;
1796 <                    }
1694 <                    else {
1695 <                        r += step;
1696 <                        --n;
1697 <                    }
1796 >                    locals = false;
1797                  }
1798 <                if ((s = task.status) < 0)
1798 >                else if ((s = task.status) < 0)
1799                      break;
1800 <                else if (n == 0) { // empty scan
1801 <                    long ms, ns; int block;
1802 <                    if (deadline == 0L)
1803 <                        ms = 0L;                       // untimed
1804 <                    else if ((ns = deadline - System.nanoTime()) <= 0L)
1805 <                        break;                         // timeout
1806 <                    else if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) <= 0L)
1807 <                        ms = 1L;                       // avoid 0 for timed wait
1808 <                    if ((block = tryCompensate(w)) != 0) {
1809 <                        task.internalWait(ms);
1810 <                        CTL.getAndAdd(this, (block > 0) ? RC_UNIT : 0L);
1800 >                else if (!scan && c == (c = ctl)) {
1801 >                    if (mode < 0)
1802 >                        ForkJoinTask.cancelIgnoringExceptions(task);
1803 >                    else if (!owned || (s = tryCompensate(c)) >= 0)
1804 >                        break;                    // block
1805 >                }
1806 >                else {                            // scan for subtasks
1807 >                    scan = false;
1808 >                    WorkQueue[] qs = queues;
1809 >                    int n = (qs == null) ? 0 : qs.length;
1810 >                    for (int i = n; i > 0; --i, ++r) {
1811 >                        int j, cap, b; WorkQueue q; ForkJoinTask<?>[] a;
1812 >                        boolean eligible = false;
1813 >                        if ((q = qs[j = r & (n - 1)]) != null &&
1814 >                            (a = q.array) != null && (cap = a.length) > 0) {
1815 >                            int k = (cap - 1) & (b = q.base), nextBase = b + 1;
1816 >                            ForkJoinTask<?> t = WorkQueue.getSlot(a, k);
1817 >                            if (t instanceof CountedCompleter) {
1818 >                                CountedCompleter<?> f = (CountedCompleter<?>)t;
1819 >                                do {} while (!(eligible = (f == task)) &&
1820 >                                             (f = f.completer) != null);
1821 >                            }
1822 >                            if ((s = task.status) < 0)
1823 >                                break outer;
1824 >                            else if (q.base != b)
1825 >                                scan = true;       // inconsistent
1826 >                            else if (t == null)
1827 >                                scan |= (a[nextBase & (cap - 1)] != null ||
1828 >                                         q.top != b);
1829 >                            else if (eligible) {
1830 >                                if (WorkQueue.casSlotToNull(a, k, t)) {
1831 >                                    q.setBaseOpaque(nextBase);
1832 >                                    t.doExec();
1833 >                                    locals = true;
1834 >                                }
1835 >                                scan = true;
1836 >                                break;
1837 >                            }
1838 >                        }
1839                      }
1713                    s = task.status;
1840                  }
1841              }
1842          }
# Line 1723 | Line 1849 | public class ForkJoinPool extends Abstra
1849       * find tasks either.
1850       */
1851      final void helpQuiescePool(WorkQueue w) {
1852 <        int prevSrc = w.source;
1853 <        int seed = ThreadLocalRandom.nextSecondarySeed();
1854 <        int r = seed >>> 16, step = r | 1;
1855 <        for (int source = prevSrc, released = -1;;) { // -1 until known
1856 <            ForkJoinTask<?> localTask; WorkQueue[] ws;
1857 <            while ((localTask = w.nextLocalTask()) != null)
1858 <                localTask.doExec();
1859 <            if (w.phase >= 0 && released == -1)
1860 <                released = 1;
1861 <            boolean quiet = true, empty = true;
1862 <            int n = (ws = workQueues) == null ? 0 : ws.length;
1863 <            for (int m = n - 1; n > 0; r += step, --n) {
1864 <                WorkQueue q; int b;
1865 <                if ((q = ws[r & m]) != null) {
1866 <                    int qs = q.source;
1867 <                    if (q.top != (b = q.base)) {
1868 <                        quiet = empty = false;
1869 <                        ForkJoinTask<?>[] a; int cap, k;
1870 <                        int qid = q.id;
1871 <                        if ((a = q.array) != null && (cap = a.length) > 0) {
1872 <                            if (released == 0) {    // increment
1873 <                                released = 1;
1874 <                                CTL.getAndAdd(this, RC_UNIT);
1852 >        if (w != null) {
1853 >            int prevSrc = w.source, wsrc = prevSrc, cfg = w.config, r = cfg + 1;
1854 >            for (boolean active = true, locals = true;;) {
1855 >                boolean busy = false, scan = false;
1856 >                if (locals) {  // run local tasks before (re)polling
1857 >                    locals = false;
1858 >                    for (ForkJoinTask<?> u; (u = w.nextLocalTask(cfg)) != null;)
1859 >                        u.doExec();
1860 >                }
1861 >                WorkQueue[] qs = queues;
1862 >                int n = (qs == null) ? 0 : qs.length;
1863 >                for (int i = n; i > 0; --i, ++r) {
1864 >                    int j, b, cap; WorkQueue q; ForkJoinTask<?>[] a;
1865 >                    if ((q = qs[j = (n - 1) & r]) != null && q != w &&
1866 >                        (a = q.array) != null && (cap = a.length) > 0) {
1867 >                        int k = (cap - 1) & (b = q.base);
1868 >                        int nextBase = b + 1, src = j | SRC;
1869 >                        ForkJoinTask<?> t = WorkQueue.getSlot(a, k);
1870 >                        if (q.base != b)
1871 >                            busy = scan = true;
1872 >                        else if (t != null) {
1873 >                            busy = scan = true;
1874 >                            if (!active) {    // increment before taking
1875 >                                active = true;
1876 >                                getAndAddCtl(RC_UNIT);
1877                              }
1878 <                            ForkJoinTask<?> t = (ForkJoinTask<?>)
1879 <                                QA.getAcquire(a, k = (cap - 1) & b);
1880 <                            if (q.base == b++ && t != null &&
1753 <                                QA.compareAndSet(a, k, t, null)) {
1754 <                                q.base = b;
1755 <                                w.source = qid;
1878 >                            if (WorkQueue.casSlotToNull(a, k, t)) {
1879 >                                q.base = nextBase;
1880 >                                w.source = src;
1881                                  t.doExec();
1882 <                                w.source = source = prevSrc;
1882 >                                w.source = wsrc = prevSrc;
1883 >                                locals = true;
1884                              }
1885 +                            break;
1886 +                        }
1887 +                        else if (!busy) {
1888 +                            if (q.top != b || a[nextBase & (cap - 1)] != null)
1889 +                                busy = scan = true;
1890 +                            else if (q.source != QUIET && q.phase >= 0)
1891 +                                busy = true;
1892                          }
1760                        break;
1893                      }
1762                    else if ((qs & QUIET) == 0)
1763                        quiet = false;
1894                  }
1895 <            }
1896 <            if (quiet) {
1897 <                if (released == 0)
1898 <                    CTL.getAndAdd(this, RC_UNIT);
1899 <                w.source = prevSrc;
1900 <                break;
1901 <            }
1902 <            else if (empty) {
1903 <                if (source != QUIET)
1904 <                    w.source = source = QUIET;
1905 <                if (released == 1) {                 // decrement
1906 <                    released = 0;
1907 <                    CTL.getAndAdd(this, RC_MASK & -RC_UNIT);
1895 >                VarHandle.acquireFence();
1896 >                if (!scan && queues == qs) {
1897 >                    if (!busy) {
1898 >                        w.source = prevSrc;
1899 >                        if (!active)
1900 >                            getAndAddCtl(RC_UNIT);
1901 >                        break;
1902 >                    }
1903 >                    if (wsrc != QUIET)
1904 >                        w.source = wsrc = QUIET;
1905 >                    if (active) {                 // decrement
1906 >                        active = false;
1907 >                        getAndAddCtl(RC_MASK & -RC_UNIT);
1908 >                    }
1909 >                    else
1910 >                        Thread.yield();           // no tasks but others busy
1911                  }
1912              }
1913          }
1914      }
1915  
1916      /**
1917 <     * Scans for and returns a polled task, if available.
1918 <     * Used only for untracked polls.
1917 >     * Scans for and returns a polled task, if available.  Used only
1918 >     * for untracked polls. Begins scan at an index (scanRover)
1919 >     * advanced on each call, to avoid systematic unfairness.
1920       *
1921       * @param submissionsOnly if true, only scan submission queues
1922       */
1923      private ForkJoinTask<?> pollScan(boolean submissionsOnly) {
1924 <        WorkQueue[] ws; int n;
1925 <        rescan: while ((mode & STOP) == 0 && (ws = workQueues) != null &&
1926 <                      (n = ws.length) > 0) {
1927 <            int m = n - 1;
1928 <            int r = ThreadLocalRandom.nextSecondarySeed();
1929 <            int h = r >>> 16;
1930 <            int origin, step;
1931 <            if (submissionsOnly) {
1932 <                origin = (r & ~1) & m;         // even indices and steps
1933 <                step = (h & ~1) | 2;
1934 <            }
1935 <            else {
1936 <                origin = r & m;
1937 <                step = h | 1;
1938 <            }
1939 <            boolean nonempty = false;
1940 <            for (int i = origin, oldSum = 0, checkSum = 0;;) {
1941 <                WorkQueue q;
1942 <                if ((q = ws[i]) != null) {
1943 <                    int b; ForkJoinTask<?> t;
1944 <                    if (q.top - (b = q.base) > 0) {
1945 <                        nonempty = true;
1946 <                        if ((t = q.poll()) != null)
1813 <                            return t;
1924 >        VarHandle.acquireFence();
1925 >        int r = scanRover += 0x61c88647; // Weyl increment; raciness OK
1926 >        if (submissionsOnly)             // even indices only
1927 >            r &= ~1;
1928 >        int step = (submissionsOnly) ? 2 : 1;
1929 >        WorkQueue[] qs; int n;
1930 >        while ((qs = queues) != null && (n = qs.length) > 0) {
1931 >            boolean scan = false;
1932 >            for (int i = 0; i < n; i += step) {
1933 >                int j, cap, b; WorkQueue q; ForkJoinTask<?>[] a;
1934 >                if ((q = qs[j = (n - 1) & (r + i)]) != null &&
1935 >                    (a = q.array) != null && (cap = a.length) > 0) {
1936 >                    int k = (cap - 1) & (b = q.base), nextBase = b + 1;
1937 >                    ForkJoinTask<?> t = WorkQueue.getSlot(a, k);
1938 >                    if (q.base != b)
1939 >                        scan = true;
1940 >                    else if (t == null)
1941 >                        scan |= (q.top != b || a[nextBase & (cap - 1)] != null);
1942 >                    else if (!WorkQueue.casSlotToNull(a, k, t))
1943 >                        scan = true;
1944 >                    else {
1945 >                        q.setBaseOpaque(nextBase);
1946 >                        return t;
1947                      }
1815                    else
1816                        checkSum += b + q.id;
1817                }
1818                if ((i = (i + step) & m) == origin) {
1819                    if (!nonempty && oldSum == (oldSum = checkSum))
1820                        break rescan;
1821                    checkSum = 0;
1822                    nonempty = false;
1948                  }
1949              }
1950 +            if (!scan && queues == qs)
1951 +                break;
1952          }
1953          return null;
1954      }
# Line 1833 | Line 1960 | public class ForkJoinPool extends Abstra
1960       */
1961      final ForkJoinTask<?> nextTaskFor(WorkQueue w) {
1962          ForkJoinTask<?> t;
1963 <        if (w == null || (t = w.nextLocalTask()) == null)
1963 >        if (w == null || (t = w.nextLocalTask(w.config)) == null)
1964              t = pollScan(false);
1965          return t;
1966      }
# Line 1841 | Line 1968 | public class ForkJoinPool extends Abstra
1968      // External operations
1969  
1970      /**
1971 <     * Adds the given task to a submission queue at submitter's
1972 <     * current queue, creating one if null or contended.
1846 <     *
1847 <     * @param task the task. Caller must ensure non-null.
1971 >     * Finds and locks a WorkQueue for an external submitter, or
1972 >     * returns null if shutdown or terminating.
1973       */
1974 <    final void externalPush(ForkJoinTask<?> task) {
1975 <        int r;                                // initialize caller's probe
1974 >    final WorkQueue submissionQueue() {
1975 >        int r;
1976          if ((r = ThreadLocalRandom.getProbe()) == 0) {
1977 <            ThreadLocalRandom.localInit();
1977 >            ThreadLocalRandom.localInit();           // initialize caller's probe
1978              r = ThreadLocalRandom.getProbe();
1979          }
1980 <        for (;;) {
1981 <            WorkQueue q;
1982 <            int md = mode, n;
1983 <            WorkQueue[] ws = workQueues;
1984 <            if ((md & SHUTDOWN) != 0 || ws == null || (n = ws.length) <= 0)
1985 <                throw new RejectedExecutionException();
1986 <            else if ((q = ws[(n - 1) & r & SQMASK]) == null) { // add queue
1987 <                int qid = (r | QUIET) & ~(FIFO | OWNED);
1988 <                Object lock = workerNamePrefix;
1989 <                ForkJoinTask<?>[] qa =
1990 <                    new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
1991 <                q = new WorkQueue(this, null);
1867 <                q.array = qa;
1868 <                q.id = qid;
1869 <                q.source = QUIET;
1870 <                if (lock != null) {     // unless disabled, lock pool to install
1871 <                    synchronized (lock) {
1872 <                        WorkQueue[] vs; int i, vn;
1873 <                        if ((vs = workQueues) != null && (vn = vs.length) > 0 &&
1874 <                            vs[i = qid & (vn - 1) & SQMASK] == null)
1875 <                            vs[i] = q;  // else another thread already installed
1876 <                    }
1980 >        for (int id = r << 1;;) {                    // even indices only
1981 >            int md = mode, n, i; WorkQueue q; ReentrantLock lock;
1982 >            WorkQueue[] qs = queues;
1983 >            if ((md & SHUTDOWN) != 0 || qs == null || (n = qs.length) <= 0)
1984 >                return null;
1985 >            else if ((q = qs[i = (n - 1) & id]) == null) {
1986 >                if ((lock = registrationLock) != null) {
1987 >                    WorkQueue w = new WorkQueue(id | SRC);
1988 >                    lock.lock();                    // install under lock
1989 >                    if (qs[i] == null)
1990 >                        qs[i] = w;                  // else lost race; discard
1991 >                    lock.unlock();
1992                  }
1993              }
1994 <            else if (!q.tryLockPhase()) // move if busy
1995 <                r = ThreadLocalRandom.advanceProbe(r);
1996 <            else {
1997 <                if (q.lockedPush(task))
1883 <                    signalWork(null);
1884 <                return;
1885 <            }
1994 >            else if (!q.tryLock())                  // move and restart
1995 >                id = (r = ThreadLocalRandom.advanceProbe(r)) << 1;
1996 >            else
1997 >                return q;
1998          }
1999      }
2000  
2001      /**
2002 +     * Adds the given task to an external submission queue, or throws
2003 +     * exception if shutdown or terminating
2004 +     *
2005 +     * @param task the task. Caller must ensure non-null.
2006 +     */
2007 +    final void externalPush(ForkJoinTask<?> task) {
2008 +        WorkQueue q;
2009 +        if ((q = submissionQueue()) == null)
2010 +            throw new RejectedExecutionException(); // shutdown or disabled
2011 +        else if (q.lockedPush(task))
2012 +            signalWork();
2013 +    }
2014 +
2015 +    /**
2016       * Pushes a possibly-external submission.
2017       */
2018      private <T> ForkJoinTask<T> externalSubmit(ForkJoinTask<T> task) {
2019 <        Thread t; ForkJoinWorkerThread w; WorkQueue q;
2019 >        Thread t; ForkJoinWorkerThread wt; WorkQueue q;
2020          if (task == null)
2021              throw new NullPointerException();
2022          if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) &&
2023 <            (w = (ForkJoinWorkerThread)t).pool == this &&
2024 <            (q = w.workQueue) != null)
2025 <            q.push(task);
2023 >            (q = (wt = (ForkJoinWorkerThread)t).workQueue) != null &&
2024 >            wt.pool == this)
2025 >            q.push(task, this);
2026          else
2027              externalPush(task);
2028          return task;
2029      }
2030  
2031      /**
2032 <     * Returns common pool queue for an external thread.
2033 <     */
2034 <    static WorkQueue commonSubmitterQueue() {
2035 <        ForkJoinPool p = common;
2036 <        int r = ThreadLocalRandom.getProbe();
2037 <        WorkQueue[] ws; int n;
2038 <        return (p != null && (ws = p.workQueues) != null &&
2039 <                (n = ws.length) > 0) ?
2040 <            ws[(n - 1) & r & SQMASK] : null;
2041 <    }
1916 <
1917 <    /**
1918 <     * Performs tryUnpush for an external submitter.
1919 <     */
1920 <    final boolean tryExternalUnpush(ForkJoinTask<?> task) {
1921 <        int r = ThreadLocalRandom.getProbe();
1922 <        WorkQueue[] ws; WorkQueue w; int n;
1923 <        return ((ws = workQueues) != null &&
1924 <                (n = ws.length) > 0 &&
1925 <                (w = ws[(n - 1) & r & SQMASK]) != null &&
1926 <                w.tryLockedUnpush(task));
1927 <    }
1928 <
1929 <    /**
1930 <     * Performs helpComplete for an external submitter.
1931 <     */
1932 <    final int externalHelpComplete(CountedCompleter<?> task, int maxTasks) {
1933 <        int r = ThreadLocalRandom.getProbe();
1934 <        WorkQueue[] ws; WorkQueue w; int n;
1935 <        return ((ws = workQueues) != null && (n = ws.length) > 0 &&
1936 <                (w = ws[(n - 1) & r & SQMASK]) != null) ?
1937 <            w.helpCC(task, maxTasks, true) : 0;
2032 >     * Returns common pool queue for an external thread that has
2033 >     * possibly ever submitted a common pool task (nonzero probe), or
2034 >     * null if none.
2035 >     */
2036 >    static WorkQueue commonQueue() {
2037 >        ForkJoinPool p; WorkQueue[] qs;
2038 >        int r = ThreadLocalRandom.getProbe(), n;
2039 >        return ((p = common) != null && (qs = p.queues) != null &&
2040 >                (n = qs.length) > 0 && r != 0) ?
2041 >            qs[(n - 1) & (r << 1)] : null;
2042      }
2043  
2044      /**
2045 <     * Tries to steal and run tasks within the target's computation.
2046 <     * The maxTasks argument supports external usages; internal calls
2047 <     * use zero, allowing unbounded steps (external calls trap
1944 <     * non-positive values).
1945 <     *
1946 <     * @param w caller
1947 <     * @param maxTasks if non-zero, the maximum number of other tasks to run
1948 <     * @return task status on exit
2045 >     * If the given executor is a ForkJoinPool, poll and execute
2046 >     * AsynchronousCompletionTasks from worker's queue until none are
2047 >     * available or blocker is released.
2048       */
2049 <    final int helpComplete(WorkQueue w, CountedCompleter<?> task,
2050 <                           int maxTasks) {
2051 <        return (w == null) ? 0 : w.helpCC(task, maxTasks, false);
2049 >    static void helpAsyncBlocker(Executor e, ManagedBlocker blocker) {
2050 >        WorkQueue w = null; Thread t; ForkJoinWorkerThread wt;
2051 >        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
2052 >            if ((wt = (ForkJoinWorkerThread)t).pool == e)
2053 >                w = wt.workQueue;
2054 >        }
2055 >        else if (e == common)
2056 >            w = commonQueue();
2057 >        if (w != null)
2058 >            w.helpAsyncBlocker(blocker);
2059      }
2060  
2061      /**
# Line 2022 | Line 2128 | public class ForkJoinPool extends Abstra
2128       * @return true if terminating or terminated
2129       */
2130      private boolean tryTerminate(boolean now, boolean enable) {
2131 <        int md; // 3 phases: try to set SHUTDOWN, then STOP, then TERMINATED
2132 <
2133 <        while (((md = mode) & SHUTDOWN) == 0) {
2028 <            if (!enable || this == common)        // cannot shutdown
2131 >        int md; // try to set SHUTDOWN, then STOP, then help terminate
2132 >        if (((md = mode) & SHUTDOWN) == 0) {
2133 >            if (!enable)
2134                  return false;
2135 <            else
2031 <                MODE.compareAndSet(this, md, md | SHUTDOWN);
2135 >            md = getAndBitwiseOrMode(SHUTDOWN);
2136          }
2137 <
2138 <        while (((md = mode) & STOP) == 0) {       // try to initiate termination
2139 <            if (!now) {                           // check if quiescent & empty
2140 <                for (long oldSum = 0L;;) {        // repeat until stable
2037 <                    boolean running = false;
2038 <                    long checkSum = ctl;
2039 <                    WorkQueue[] ws = workQueues;
2040 <                    if ((md & SMASK) + (int)(checkSum >> RC_SHIFT) > 0)
2041 <                        running = true;
2042 <                    else if (ws != null) {
2043 <                        WorkQueue w;
2044 <                        for (int i = 0; i < ws.length; ++i) {
2045 <                            if ((w = ws[i]) != null) {
2046 <                                int s = w.source, p = w.phase;
2047 <                                int d = w.id, b = w.base;
2048 <                                if (b != w.top ||
2049 <                                    ((d & 1) == 1 && (s >= 0 || p >= 0))) {
2050 <                                    running = true;
2051 <                                    break;     // working, scanning, or have work
2052 <                                }
2053 <                                checkSum += (((long)s << 48) + ((long)p << 32) +
2054 <                                             ((long)b << 16) + (long)d);
2055 <                            }
2056 <                        }
2057 <                    }
2058 <                    if (((md = mode) & STOP) != 0)
2059 <                        break;                 // already triggered
2060 <                    else if (running)
2061 <                        return false;
2062 <                    else if (workQueues == ws && oldSum == (oldSum = checkSum))
2063 <                        break;
2064 <                }
2065 <            }
2066 <            if ((md & STOP) == 0)
2067 <                MODE.compareAndSet(this, md, md | STOP);
2137 >        if ((md & STOP) == 0) {
2138 >            if (!now && !isQuiescent())
2139 >                return false;
2140 >            md = getAndBitwiseOrMode(STOP);
2141          }
2142 <
2143 <        while (((md = mode) & TERMINATED) == 0) { // help terminate others
2144 <            for (long oldSum = 0L;;) {            // repeat until stable
2145 <                WorkQueue[] ws; WorkQueue w;
2146 <                long checkSum = ctl;
2147 <                if ((ws = workQueues) != null) {
2148 <                    for (int i = 0; i < ws.length; ++i) {
2149 <                        if ((w = ws[i]) != null) {
2150 <                            ForkJoinWorkerThread wt = w.owner;
2151 <                            w.cancelAll();        // clear queues
2152 <                            if (wt != null) {
2080 <                                try {             // unblock join or park
2081 <                                    wt.interrupt();
2082 <                                } catch (Throwable ignore) {
2083 <                                }
2084 <                            }
2085 <                            checkSum += ((long)w.phase << 32) + w.base;
2086 <                        }
2142 >        if ((md & TERMINATED) == 0) {
2143 >            for (ForkJoinTask<?> t; (t = pollScan(false)) != null; )
2144 >                ForkJoinTask.cancelIgnoringExceptions(t); // cancel tasks
2145 >            WorkQueue[] qs; WorkQueue q; Thread t;
2146 >            int n = ((qs = queues) == null) ? 0 : qs.length;
2147 >            for (int i = 1; i < n; i += 2) { // unblock parked workers
2148 >                if ((q = qs[i]) != null && (t = q.owner) != null &&
2149 >                    !t.isInterrupted()) {
2150 >                    try {
2151 >                        t.interrupt();
2152 >                    } catch (Throwable ignore) {
2153                      }
2154                  }
2089                if (((md = mode) & TERMINATED) != 0 ||
2090                    (workQueues == ws && oldSum == (oldSum = checkSum)))
2091                    break;
2155              }
2156 <            if ((md & TERMINATED) != 0)
2157 <                break;
2158 <            else if ((md & SMASK) + (short)(ctl >>> TC_SHIFT) > 0)
2159 <                break;
2160 <            else if (MODE.compareAndSet(this, md, md | TERMINATED)) {
2161 <                synchronized (this) {
2162 <                    notifyAll();                  // for awaitTermination
2163 <                }
2164 <                break;
2156 >
2157 >            ReentrantLock lock; Condition cond; // finish if no workers
2158 >            if ((md & SMASK) + (short)(ctl >>> TC_SHIFT) <= 0 &&
2159 >                (getAndBitwiseOrMode(TERMINATED) & TERMINATED) == 0 &&
2160 >                (lock = registrationLock) != null) {
2161 >                lock.lock();
2162 >                if ((cond = termination) != null)
2163 >                    cond.signalAll();
2164 >                lock.unlock();
2165              }
2166          }
2167          return true;
# Line 2269 | Line 2332 | public class ForkJoinPool extends Abstra
2332                          Predicate<? super ForkJoinPool> saturate,
2333                          long keepAliveTime,
2334                          TimeUnit unit) {
2335 <        // check, encode, pack parameters
2336 <        if (parallelism <= 0 || parallelism > MAX_CAP ||
2337 <            maximumPoolSize < parallelism || keepAliveTime <= 0L)
2335 >        checkPermission();
2336 >        int p = parallelism;
2337 >        if (p <= 0 || p > MAX_CAP || p > maximumPoolSize || keepAliveTime <= 0L)
2338              throw new IllegalArgumentException();
2339 <        if (factory == null)
2339 >        if (factory == null || unit == null)
2340              throw new NullPointerException();
2278        long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
2279
2280        int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP);
2281        long c = ((((long)(-corep)       << TC_SHIFT) & TC_MASK) |
2282                  (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
2283        int m = parallelism | (asyncMode ? FIFO : 0);
2284        int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism;
2285        int minAvail = Math.min(Math.max(minimumRunnable, 0), MAX_CAP);
2286        int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH);
2287        int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots
2288        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
2289        n = (n + 1) << 1; // power of two, including space for submission queues
2290
2291        this.workerNamePrefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
2292        this.workQueues = new WorkQueue[n];
2341          this.factory = factory;
2342          this.ueh = handler;
2343          this.saturate = saturate;
2344 <        this.keepAlive = ms;
2345 <        this.bounds = b;
2346 <        this.mode = m;
2347 <        this.ctl = c;
2348 <        checkPermission();
2344 >        this.keepAlive = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
2345 >        int size = 1 << (33 - Integer.numberOfLeadingZeros(p - 1));
2346 >        int corep = Math.min(Math.max(corePoolSize, p), MAX_CAP);
2347 >        int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - p;
2348 >        int minAvail = Math.min(Math.max(minimumRunnable, 0), MAX_CAP);
2349 >        this.bounds = ((minAvail - p) & SMASK) | (maxSpares << SWIDTH);
2350 >        this.mode = p | (asyncMode ? FIFO : 0);
2351 >        this.ctl = ((((long)(-corep) << TC_SHIFT) & TC_MASK) |
2352 >                    (((long)(-p)     << RC_SHIFT) & RC_MASK));
2353 >        this.registrationLock = new ReentrantLock();
2354 >        this.queues = new WorkQueue[size];
2355 >        String pid = Integer.toString(getAndAddPoolIds(1) + 1);
2356 >        this.workerNamePrefix = "ForkJoinPool-" + pid + "-worker-";
2357      }
2358  
2359 +    // helper method for commonPool constructor
2360      private static Object newInstanceFromSystemProperty(String property)
2361          throws ReflectiveOperationException {
2362          String className = System.getProperty(property);
# Line 2314 | Line 2371 | public class ForkJoinPool extends Abstra
2371       * overridden by system properties
2372       */
2373      private ForkJoinPool(byte forCommonPoolOnly) {
2374 <        int parallelism = -1;
2374 >        int parallelism = Runtime.getRuntime().availableProcessors() - 1;
2375          ForkJoinWorkerThreadFactory fac = null;
2376          UncaughtExceptionHandler handler = null;
2377          try {  // ignore exceptions in accessing/parsing properties
2321            String pp = System.getProperty
2322                ("java.util.concurrent.ForkJoinPool.common.parallelism");
2323            if (pp != null)
2324                parallelism = Integer.parseInt(pp);
2378              fac = (ForkJoinWorkerThreadFactory) newInstanceFromSystemProperty(
2379                  "java.util.concurrent.ForkJoinPool.common.threadFactory");
2380              handler = (UncaughtExceptionHandler) newInstanceFromSystemProperty(
2381                  "java.util.concurrent.ForkJoinPool.common.exceptionHandler");
2382 +            String pp = System.getProperty
2383 +                ("java.util.concurrent.ForkJoinPool.common.parallelism");
2384 +            if (pp != null)
2385 +                parallelism = Integer.parseInt(pp);
2386          } catch (Exception ignore) {
2387          }
2388 <
2389 <        if (fac == null) {
2390 <            if (System.getSecurityManager() == null)
2391 <                fac = defaultForkJoinWorkerThreadFactory;
2392 <            else // use security-managed default
2393 <                fac = new InnocuousForkJoinWorkerThreadFactory();
2337 <        }
2338 <        if (parallelism < 0 && // default 1 less than #cores
2339 <            (parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0)
2340 <            parallelism = 1;
2341 <        if (parallelism > MAX_CAP)
2342 <            parallelism = MAX_CAP;
2343 <
2344 <        long c = ((((long)(-parallelism) << TC_SHIFT) & TC_MASK) |
2345 <                  (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
2346 <        int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH);
2347 <        int n = (parallelism > 1) ? parallelism - 1 : 1;
2348 <        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
2349 <        n = (n + 1) << 1;
2350 <
2351 <        this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2352 <        this.workQueues = new WorkQueue[n];
2353 <        this.factory = fac;
2388 >        int p = this.mode = Math.min(Math.max(parallelism, 0), MAX_CAP);
2389 >        int size = 1 << (33 - Integer.numberOfLeadingZeros(p > 0 ? p - 1 : 1));
2390 >        this.factory = (fac != null) ? fac :
2391 >            (System.getSecurityManager() == null ?
2392 >             defaultForkJoinWorkerThreadFactory :
2393 >             new InnocuousForkJoinWorkerThreadFactory());
2394          this.ueh = handler;
2355        this.saturate = null;
2395          this.keepAlive = DEFAULT_KEEPALIVE;
2396 <        this.bounds = b;
2397 <        this.mode = parallelism;
2398 <        this.ctl = c;
2396 >        this.saturate = null;
2397 >        this.workerNamePrefix = null;
2398 >        this.bounds = ((1 - p) & SMASK) | (COMMON_MAX_SPARES << SWIDTH);
2399 >        this.ctl = ((((long)(-p) << TC_SHIFT) & TC_MASK) |
2400 >                    (((long)(-p) << RC_SHIFT) & RC_MASK));
2401 >        this.queues = new WorkQueue[size];
2402 >        this.registrationLock = new ReentrantLock();
2403      }
2404  
2405      /**
# Line 2397 | Line 2440 | public class ForkJoinPool extends Abstra
2440       *         scheduled for execution
2441       */
2442      public <T> T invoke(ForkJoinTask<T> task) {
2400        if (task == null)
2401            throw new NullPointerException();
2443          externalSubmit(task);
2444          return task.join();
2445      }
# Line 2422 | Line 2463 | public class ForkJoinPool extends Abstra
2463       * @throws RejectedExecutionException if the task cannot be
2464       *         scheduled for execution
2465       */
2466 +    @Override
2467 +    @SuppressWarnings("unchecked")
2468      public void execute(Runnable task) {
2469 <        if (task == null)
2470 <            throw new NullPointerException();
2471 <        ForkJoinTask<?> job;
2429 <        if (task instanceof ForkJoinTask<?>) // avoid re-wrap
2430 <            job = (ForkJoinTask<?>) task;
2431 <        else
2432 <            job = new ForkJoinTask.RunnableExecuteAction(task);
2433 <        externalSubmit(job);
2469 >        externalSubmit((task instanceof ForkJoinTask<?>)
2470 >                       ? (ForkJoinTask<Void>) task // avoid re-wrap
2471 >                       : new ForkJoinTask.RunnableExecuteAction(task));
2472      }
2473  
2474      /**
# Line 2452 | Line 2490 | public class ForkJoinPool extends Abstra
2490       * @throws RejectedExecutionException if the task cannot be
2491       *         scheduled for execution
2492       */
2493 +    @Override
2494      public <T> ForkJoinTask<T> submit(Callable<T> task) {
2495          return externalSubmit(new ForkJoinTask.AdaptedCallable<T>(task));
2496      }
# Line 2461 | Line 2500 | public class ForkJoinPool extends Abstra
2500       * @throws RejectedExecutionException if the task cannot be
2501       *         scheduled for execution
2502       */
2503 +    @Override
2504      public <T> ForkJoinTask<T> submit(Runnable task, T result) {
2505          return externalSubmit(new ForkJoinTask.AdaptedRunnable<T>(task, result));
2506      }
# Line 2470 | Line 2510 | public class ForkJoinPool extends Abstra
2510       * @throws RejectedExecutionException if the task cannot be
2511       *         scheduled for execution
2512       */
2513 +    @Override
2514      @SuppressWarnings("unchecked")
2515      public ForkJoinTask<?> submit(Runnable task) {
2475        if (task == null)
2476            throw new NullPointerException();
2516          return externalSubmit((task instanceof ForkJoinTask<?>)
2517              ? (ForkJoinTask<Void>) task // avoid re-wrap
2518              : new ForkJoinTask.AdaptedRunnableAction(task));
2519      }
2520  
2521      /**
2522 +     * Task class, plus some helper methods, for invokeAll and invokeAny.
2523 +     */
2524 +    static final class BulkTask<E> extends CountedCompleter<E> {
2525 +        private static final long serialVersionUID = 2838392045355241008L;
2526 +        @SuppressWarnings("serial") // Conditionally serializable
2527 +        final Callable<E> callable;
2528 +        @SuppressWarnings("serial") // Conditionally serializable
2529 +        E result;
2530 +        final boolean invokeAny;    // false if performing invokeAll
2531 +        BulkTask(BulkTask<E> parent, Callable<E> callable, boolean invokeAny) {
2532 +            super(parent);
2533 +            this.callable = callable;
2534 +            this.invokeAny = invokeAny;
2535 +        }
2536 +        public E getRawResult() { return result; }
2537 +        public void setRawResult(E r) { result = r; }
2538 +
2539 +        public void compute() {
2540 +            try {
2541 +                E r = callable.call();
2542 +                @SuppressWarnings("unchecked") CountedCompleter<E> p =
2543 +                    invokeAny ? (CountedCompleter<E>)getCompleter() : null;
2544 +                if (p != null)
2545 +                    p.complete(r);
2546 +                else
2547 +                    complete(r);
2548 +            } catch(Throwable ex) {
2549 +                completeExceptionally(ex);
2550 +            }
2551 +        }
2552 +
2553 +        static void cancelAll(BulkTask<?>[] fs) { // cancel all nonnull tasks
2554 +            if (fs != null) {
2555 +                for (BulkTask<?> f: fs) {
2556 +                    if (f != null)
2557 +                        f.cancel(false);
2558 +                }
2559 +            }
2560 +        }
2561 +
2562 +        /**
2563 +         * Creates, records, and forks a BulkTask for each Callable;
2564 +         * returns the array, with first element root task (if noneepty)
2565 +         */
2566 +        static <T> BulkTask<T>[] forkAll(Collection<? extends Callable<T>> cs,
2567 +                                         boolean invokeAny) {
2568 +            int n = cs.size();
2569 +            @SuppressWarnings("unchecked")
2570 +            BulkTask<T>[] fs = (BulkTask<T>[])new BulkTask<?>[n];
2571 +            BulkTask<T> root = null; // parent completer for all others
2572 +            Iterator<? extends Callable<T>> it = cs.iterator();
2573 +            int i = 0; // ignores extra elements if cs.size() inconsistent
2574 +            while (i < n && it.hasNext()) {
2575 +                Callable<T> c; BulkTask<T> f;
2576 +                if ((c = it.next()) == null) {
2577 +                    cancelAll(fs);
2578 +                    throw new NullPointerException();
2579 +                }
2580 +                fs[i++] = f = new BulkTask<T>(root, c, invokeAny);
2581 +                if (root == null)
2582 +                    (root = f).setPendingCount(n);
2583 +                f.fork();
2584 +            }
2585 +            return fs;
2586 +        }
2587 +
2588 +        /**
2589 +         * If completed abnormally, throws any exception encountered
2590 +         * by any task in array, or a CancellationException if none,
2591 +         * wrapped in ExecutionException. Else returns result.
2592 +         */
2593 +        E reportInvokeAnyResult(BulkTask<?>[] fs) throws ExecutionException {
2594 +            E r = getRawResult();
2595 +            if (r == null && isCompletedAbnormally()) {
2596 +                Throwable ex = null;
2597 +                if (fs != null) {
2598 +                    for (BulkTask<?> f: fs) {
2599 +                        if (f != null && (ex = f.getException()) != null)
2600 +                            break;
2601 +                    }
2602 +                }
2603 +                if (ex == null)
2604 +                    ex = new CancellationException();
2605 +                throw new ExecutionException(ex);
2606 +            }
2607 +            return r;
2608 +        }
2609 +    }
2610 +
2611 +    /**
2612       * @throws NullPointerException       {@inheritDoc}
2613       * @throws RejectedExecutionException {@inheritDoc}
2614       */
2615 +    @Override
2616      public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
2617 <        // In previous versions of this class, this method constructed
2618 <        // a task to run ForkJoinTask.invokeAll, but now external
2619 <        // invocation of multiple tasks is at least as efficient.
2620 <        ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
2617 >        BulkTask<T>[] fs; BulkTask<T> root;
2618 >        if ((fs = BulkTask.forkAll(tasks, false)) != null && fs.length > 0 &&
2619 >            (root = fs[0]) != null)
2620 >            root.quietlyJoin();
2621 >        return Arrays.asList(fs);
2622 >    }
2623  
2624 <        try {
2625 <            for (Callable<T> t : tasks) {
2626 <                ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t);
2627 <                futures.add(f);
2628 <                externalSubmit(f);
2629 <            }
2630 <            for (int i = 0, size = futures.size(); i < size; i++)
2631 <                ((ForkJoinTask<?>)futures.get(i)).quietlyJoin();
2632 <            return futures;
2633 <        } catch (Throwable t) {
2634 <            for (int i = 0, size = futures.size(); i < size; i++)
2635 <                futures.get(i).cancel(false);
2636 <            throw t;
2624 >    @Override
2625 >    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
2626 >                                         long timeout, TimeUnit unit)
2627 >        throws InterruptedException {
2628 >        BulkTask<T>[] fs; BulkTask<T> root;
2629 >        long deadline = unit.toNanos(timeout) + System.nanoTime();
2630 >        if ((fs = BulkTask.forkAll(tasks, false)) != null && fs.length > 0 &&
2631 >            (root = fs[0]) != null) {
2632 >            try {
2633 >                root.get(deadline, TimeUnit.NANOSECONDS);
2634 >            } catch (Exception ex) {
2635 >                BulkTask.cancelAll(fs);
2636 >            }
2637 >        }
2638 >        return Arrays.asList(fs);
2639 >    }
2640 >
2641 >    @Override
2642 >    public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
2643 >        throws InterruptedException, ExecutionException {
2644 >        BulkTask<T>[] fs; BulkTask<T> root;
2645 >        if ((fs = BulkTask.forkAll(tasks, true)) != null && fs.length > 0 &&
2646 >            (root = fs[0]) != null) {
2647 >            root.quietlyJoin();
2648 >            BulkTask.cancelAll(fs);
2649 >            return root.reportInvokeAnyResult(fs);
2650          }
2651 +        else
2652 +            throw new IllegalArgumentException(); // no tasks
2653 +    }
2654 +
2655 +    @Override
2656 +    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
2657 +                           long timeout, TimeUnit unit)
2658 +        throws InterruptedException, ExecutionException, TimeoutException {
2659 +        BulkTask<T>[] fs; BulkTask<T> root;
2660 +        long deadline = unit.toNanos(timeout) + System.nanoTime();
2661 +        if ((fs = BulkTask.forkAll(tasks, true)) != null && fs.length > 0 &&
2662 +            (root = fs[0]) != null) {
2663 +            TimeoutException tex = null;
2664 +            try {
2665 +                root.get(deadline, TimeUnit.NANOSECONDS);
2666 +            } catch (TimeoutException tx) {
2667 +                tex = tx;
2668 +            } catch (Throwable ignore) {
2669 +            }
2670 +            BulkTask.cancelAll(fs);
2671 +            if (tex != null)
2672 +                throw tex;
2673 +            return root.reportInvokeAnyResult(fs);
2674 +        }
2675 +        else
2676 +            throw new IllegalArgumentException();
2677      }
2678  
2679      /**
# Line 2575 | Line 2746 | public class ForkJoinPool extends Abstra
2746       * @return the number of worker threads
2747       */
2748      public int getRunningThreadCount() {
2578        WorkQueue[] ws; WorkQueue w;
2749          VarHandle.acquireFence();
2750 +        WorkQueue[] qs; WorkQueue q;
2751          int rc = 0;
2752 <        if ((ws = workQueues) != null) {
2753 <            for (int i = 1; i < ws.length; i += 2) {
2754 <                if ((w = ws[i]) != null && w.isApparentlyUnblocked())
2752 >        if ((qs = queues) != null) {
2753 >            for (int i = 1; i < qs.length; i += 2) {
2754 >                if ((q = qs[i]) != null && q.isApparentlyUnblocked())
2755                      ++rc;
2756              }
2757          }
# Line 2611 | Line 2782 | public class ForkJoinPool extends Abstra
2782       * @return {@code true} if all threads are currently idle
2783       */
2784      public boolean isQuiescent() {
2785 <        for (;;) {
2786 <            long c = ctl;
2787 <            int md = mode, pc = md & SMASK;
2788 <            int tc = pc + (short)(c >>> TC_SHIFT);
2789 <            int rc = pc + (int)(c >> RC_SHIFT);
2619 <            if ((md & (STOP | TERMINATED)) != 0)
2620 <                return true;
2621 <            else if (rc > 0)
2622 <                return false;
2623 <            else {
2624 <                WorkQueue[] ws; WorkQueue v;
2625 <                if ((ws = workQueues) != null) {
2626 <                    for (int i = 1; i < ws.length; i += 2) {
2627 <                        if ((v = ws[i]) != null) {
2628 <                            if (v.source > 0)
2629 <                                return false;
2630 <                            --tc;
2631 <                        }
2632 <                    }
2633 <                }
2634 <                if (tc == 0 && ctl == c)
2635 <                    return true;
2636 <            }
2637 <        }
2785 >        int m, p;
2786 >        return ((((m = mode) & STOP) != 0) ||
2787 >                ((p = m & SMASK) + (int)(ctl >> RC_SHIFT) <= 0 &&
2788 >                 !hasQueuedSubmissions() && p + (int)(ctl >> RC_SHIFT) <= 0) ||
2789 >                (mode & STOP) != 0); // recheck
2790      }
2791  
2792      /**
# Line 2650 | Line 2802 | public class ForkJoinPool extends Abstra
2802       */
2803      public long getStealCount() {
2804          long count = stealCount;
2805 <        WorkQueue[] ws; WorkQueue w;
2806 <        if ((ws = workQueues) != null) {
2807 <            for (int i = 1; i < ws.length; i += 2) {
2808 <                if ((w = ws[i]) != null)
2809 <                    count += (long)w.nsteals & 0xffffffffL;
2805 >        WorkQueue[] qs; WorkQueue q;
2806 >        if ((qs = queues) != null) {
2807 >            for (int i = 1; i < qs.length; i += 2) {
2808 >                if ((q = qs[i]) != null)
2809 >                    count += (long)q.nsteals & 0xffffffffL;
2810              }
2811          }
2812          return count;
# Line 2671 | Line 2823 | public class ForkJoinPool extends Abstra
2823       * @return the number of queued tasks
2824       */
2825      public long getQueuedTaskCount() {
2674        WorkQueue[] ws; WorkQueue w;
2826          VarHandle.acquireFence();
2827 +        WorkQueue[] qs; WorkQueue q;
2828          int count = 0;
2829 <        if ((ws = workQueues) != null) {
2830 <            for (int i = 1; i < ws.length; i += 2) {
2831 <                if ((w = ws[i]) != null)
2832 <                    count += w.queueSize();
2829 >        if ((qs = queues) != null) {
2830 >            for (int i = 1; i < qs.length; i += 2) {
2831 >                if ((q = qs[i]) != null)
2832 >                    count += q.queueSize();
2833              }
2834          }
2835          return count;
# Line 2691 | Line 2843 | public class ForkJoinPool extends Abstra
2843       * @return the number of queued submissions
2844       */
2845      public int getQueuedSubmissionCount() {
2694        WorkQueue[] ws; WorkQueue w;
2846          VarHandle.acquireFence();
2847 +        WorkQueue[] qs; WorkQueue q;
2848          int count = 0;
2849 <        if ((ws = workQueues) != null) {
2850 <            for (int i = 0; i < ws.length; i += 2) {
2851 <                if ((w = ws[i]) != null)
2852 <                    count += w.queueSize();
2849 >        if ((qs = queues) != null) {
2850 >            for (int i = 0; i < qs.length; i += 2) {
2851 >                if ((q = qs[i]) != null)
2852 >                    count += q.queueSize();
2853              }
2854          }
2855          return count;
# Line 2710 | Line 2862 | public class ForkJoinPool extends Abstra
2862       * @return {@code true} if there are any queued submissions
2863       */
2864      public boolean hasQueuedSubmissions() {
2713        WorkQueue[] ws; WorkQueue w;
2865          VarHandle.acquireFence();
2866 <        if ((ws = workQueues) != null) {
2867 <            for (int i = 0; i < ws.length; i += 2) {
2868 <                if ((w = ws[i]) != null && !w.isEmpty())
2866 >        WorkQueue[] qs; WorkQueue q;
2867 >        if ((qs = queues) != null) {
2868 >            for (int i = 0; i < qs.length; i += 2) {
2869 >                if ((q = qs[i]) != null && !q.isEmpty())
2870                      return true;
2871              }
2872          }
# Line 2750 | Line 2902 | public class ForkJoinPool extends Abstra
2902       * @return the number of elements transferred
2903       */
2904      protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
2753        WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t;
2754        VarHandle.acquireFence();
2905          int count = 0;
2906 <        if ((ws = workQueues) != null) {
2907 <            for (int i = 0; i < ws.length; ++i) {
2908 <                if ((w = ws[i]) != null) {
2759 <                    while ((t = w.poll()) != null) {
2760 <                        c.add(t);
2761 <                        ++count;
2762 <                    }
2763 <                }
2764 <            }
2906 >        for (ForkJoinTask<?> t; (t = pollScan(false)) != null; ) {
2907 >            c.add(t);
2908 >            ++count;
2909          }
2910          return count;
2911      }
# Line 2774 | Line 2918 | public class ForkJoinPool extends Abstra
2918       * @return a string identifying this pool, as well as its state
2919       */
2920      public String toString() {
2921 <        // Use a single pass through workQueues to collect counts
2921 >        // Use a single pass through queues to collect counts
2922          int md = mode; // read volatile fields first
2923          long c = ctl;
2924          long st = stealCount;
2925 <        long qt = 0L, qs = 0L; int rc = 0;
2926 <        WorkQueue[] ws; WorkQueue w;
2927 <        if ((ws = workQueues) != null) {
2928 <            for (int i = 0; i < ws.length; ++i) {
2929 <                if ((w = ws[i]) != null) {
2930 <                    int size = w.queueSize();
2925 >        long qt = 0L, ss = 0L; int rc = 0;
2926 >        WorkQueue[] qs; WorkQueue q;
2927 >        if ((qs = queues) != null) {
2928 >            for (int i = 0; i < qs.length; ++i) {
2929 >                if ((q = qs[i]) != null) {
2930 >                    int size = q.queueSize();
2931                      if ((i & 1) == 0)
2932 <                        qs += size;
2932 >                        ss += size;
2933                      else {
2934                          qt += size;
2935 <                        st += (long)w.nsteals & 0xffffffffL;
2936 <                        if (w.isApparentlyUnblocked())
2935 >                        st += (long)q.nsteals & 0xffffffffL;
2936 >                        if (q.isApparentlyUnblocked())
2937                              ++rc;
2938                      }
2939                  }
# Line 2813 | Line 2957 | public class ForkJoinPool extends Abstra
2957              ", running = " + rc +
2958              ", steals = " + st +
2959              ", tasks = " + qt +
2960 <            ", submissions = " + qs +
2960 >            ", submissions = " + ss +
2961              "]";
2962      }
2963  
# Line 2833 | Line 2977 | public class ForkJoinPool extends Abstra
2977       */
2978      public void shutdown() {
2979          checkPermission();
2980 <        tryTerminate(false, true);
2980 >        if (this != common)
2981 >            tryTerminate(false, true);
2982      }
2983  
2984      /**
# Line 2856 | Line 3001 | public class ForkJoinPool extends Abstra
3001       */
3002      public List<Runnable> shutdownNow() {
3003          checkPermission();
3004 <        tryTerminate(true, true);
3004 >        if (this != common)
3005 >            tryTerminate(true, true);
3006          return Collections.emptyList();
3007      }
3008  
# Line 2883 | Line 3029 | public class ForkJoinPool extends Abstra
3029       * @return {@code true} if terminating but not yet terminated
3030       */
3031      public boolean isTerminating() {
3032 <        int md = mode;
2887 <        return (md & STOP) != 0 && (md & TERMINATED) == 0;
3032 >        return (mode & (STOP | TERMINATED)) == STOP;
3033      }
3034  
3035      /**
# Line 2912 | Line 3057 | public class ForkJoinPool extends Abstra
3057       */
3058      public boolean awaitTermination(long timeout, TimeUnit unit)
3059          throws InterruptedException {
3060 +        long nanos = unit.toNanos(timeout);
3061 +        ReentrantLock lock; Condition cond; // construct only if waiting
3062          if (Thread.interrupted())
3063              throw new InterruptedException();
3064          if (this == common) {
3065              awaitQuiescence(timeout, unit);
3066              return false;
3067          }
3068 <        long nanos = unit.toNanos(timeout);
2922 <        if (isTerminated())
3068 >        if (isTerminated() || (lock = registrationLock) == null)
3069              return true;
3070 <        if (nanos <= 0L)
3071 <            return false;
3072 <        long deadline = System.nanoTime() + nanos;
3073 <        synchronized (this) {
3074 <            for (;;) {
3075 <                if (isTerminated())
3076 <                    return true;
3077 <                if (nanos <= 0L)
2932 <                    return false;
2933 <                long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
2934 <                wait(millis > 0L ? millis : 1L);
2935 <                nanos = deadline - System.nanoTime();
2936 <            }
3070 >        lock.lock();
3071 >        try {
3072 >            if ((cond = termination) == null)
3073 >                termination = cond = lock.newCondition();
3074 >            while (!isTerminated() && nanos > 0L)
3075 >                nanos = cond.awaitNanos(nanos);
3076 >        } finally {
3077 >            lock.unlock();
3078          }
3079 +        return isTerminated();
3080      }
3081  
3082      /**
# Line 2949 | Line 3091 | public class ForkJoinPool extends Abstra
3091       * timeout elapsed.
3092       */
3093      public boolean awaitQuiescence(long timeout, TimeUnit unit) {
3094 +        Thread thread; ForkJoinWorkerThread wt;
3095          long nanos = unit.toNanos(timeout);
3096 <        ForkJoinWorkerThread wt;
2954 <        Thread thread = Thread.currentThread();
2955 <        if ((thread instanceof ForkJoinWorkerThread) &&
3096 >        if ((thread = Thread.currentThread()) instanceof ForkJoinWorkerThread &&
3097              (wt = (ForkJoinWorkerThread)thread).pool == this) {
3098              helpQuiescePool(wt.workQueue);
3099              return true;
3100          }
3101 <        else {
3102 <            for (long startTime = System.nanoTime();;) {
3103 <                ForkJoinTask<?> t;
3104 <                if ((t = pollScan(false)) != null)
3105 <                    t.doExec();
3106 <                else if (isQuiescent())
3107 <                    return true;
3108 <                else if ((System.nanoTime() - startTime) > nanos)
3109 <                    return false;
3110 <                else
3111 <                    Thread.yield(); // cannot block
3101 >        // else cannot block, so use exponential sleeps
3102 >        boolean quiesced = false, interrupted = false;
3103 >        for (long startTime = System.nanoTime(), parkTime = 0L;;) {
3104 >            ForkJoinTask<?> t;
3105 >            if ((t = pollScan(false)) != null) {
3106 >                t.doExec();
3107 >                parkTime = 0L;
3108 >            }
3109 >            else if (quiesced = isQuiescent())
3110 >                break;
3111 >            else if ((System.nanoTime() - startTime) > nanos)
3112 >                break;
3113 >            else if (parkTime == 0L) {
3114 >                parkTime = 1L << 10; // initially about 1 usec
3115 >                Thread.yield();
3116 >            }
3117 >            else if (Thread.interrupted())
3118 >                interrupted = true;
3119 >            else {
3120 >                LockSupport.parkNanos(this, parkTime);
3121 >                if (parkTime < nanos >>> 8) // max sleep approx 1% nanos
3122 >                    parkTime <<= 1;
3123              }
3124          }
3125 +        if (interrupted)
3126 +            Thread.currentThread().interrupt();
3127 +        return quiesced;
3128      }
3129  
3130      /**
# Line 2989 | Line 3144 | public class ForkJoinPool extends Abstra
3144       * not necessary. Method {@link #block} blocks the current thread
3145       * if necessary (perhaps internally invoking {@code isReleasable}
3146       * before actually blocking). These actions are performed by any
3147 <     * thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}.
3148 <     * The unusual methods in this API accommodate synchronizers that
3149 <     * may, but don't usually, block for long periods. Similarly, they
3150 <     * allow more efficient internal handling of cases in which
3151 <     * additional workers may be, but usually are not, needed to
3152 <     * ensure sufficient parallelism.  Toward this end,
3153 <     * implementations of method {@code isReleasable} must be amenable
3154 <     * to repeated invocation.
3147 >     * thread invoking {@link
3148 >     * ForkJoinPool#managedBlock(ManagedBlocker)}.  The unusual
3149 >     * methods in this API accommodate synchronizers that may, but
3150 >     * don't usually, block for long periods. Similarly, they allow
3151 >     * more efficient internal handling of cases in which additional
3152 >     * workers may be, but usually are not, needed to ensure
3153 >     * sufficient parallelism.  Toward this end, implementations of
3154 >     * method {@code isReleasable} must be amenable to repeated
3155 >     * invocation. Neither method is invoked after a prior invocation
3156 >     * of {@code isReleasable} or {@code block} returns {@code true}.
3157       *
3158       * <p>For example, here is a ManagedBlocker based on a
3159       * ReentrantLock:
# Line 3082 | Line 3239 | public class ForkJoinPool extends Abstra
3239       */
3240      public static void managedBlock(ManagedBlocker blocker)
3241          throws InterruptedException {
3242 +        Thread t; ForkJoinPool p;
3243 +        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread &&
3244 +            (p = ((ForkJoinWorkerThread)t).pool) != null)
3245 +            p.compensatedBlock(blocker);
3246 +        else
3247 +            unmanagedBlock(blocker);
3248 +    }
3249 +
3250 +    /** ManagedBlock for ForkJoinWorkerThreads */
3251 +    private void compensatedBlock(ManagedBlocker blocker)
3252 +        throws InterruptedException {
3253          if (blocker == null) throw new NullPointerException();
3254 <        ForkJoinPool p;
3255 <        ForkJoinWorkerThread wt;
3256 <        WorkQueue w;
3257 <        Thread t = Thread.currentThread();
3258 <        if ((t instanceof ForkJoinWorkerThread) &&
3259 <            (p = (wt = (ForkJoinWorkerThread)t).pool) != null &&
3260 <            (w = wt.workQueue) != null) {
3261 <            int block;
3262 <            while (!blocker.isReleasable()) {
3263 <                if ((block = p.tryCompensate(w)) != 0) {
3264 <                    try {
3097 <                        do {} while (!blocker.isReleasable() &&
3098 <                                     !blocker.block());
3099 <                    } finally {
3100 <                        CTL.getAndAdd(p, (block > 0) ? RC_UNIT : 0L);
3101 <                    }
3102 <                    break;
3254 >        for (;;) {
3255 >            int comp; boolean done;
3256 >            long c = ctl;
3257 >            if (blocker.isReleasable())
3258 >                break;
3259 >            if ((comp = tryCompensate(c)) >= 0) {
3260 >                long post = (comp == 0) ? 0L : RC_UNIT;
3261 >                try {
3262 >                    done = blocker.block();
3263 >                } finally {
3264 >                    getAndAddCtl(post);
3265                  }
3266 +                if (done)
3267 +                    break;
3268              }
3269          }
3106        else {
3107            do {} while (!blocker.isReleasable() &&
3108                         !blocker.block());
3109        }
3270      }
3271  
3272 <    /**
3273 <     * If the given executor is a ForkJoinPool, poll and execute
3274 <     * AsynchronousCompletionTasks from worker's queue until none are
3275 <     * available or blocker is released.
3276 <     */
3117 <    static void helpAsyncBlocker(Executor e, ManagedBlocker blocker) {
3118 <        if (e instanceof ForkJoinPool) {
3119 <            WorkQueue w; ForkJoinWorkerThread wt; WorkQueue[] ws; int r, n;
3120 <            ForkJoinPool p = (ForkJoinPool)e;
3121 <            Thread thread = Thread.currentThread();
3122 <            if (thread instanceof ForkJoinWorkerThread &&
3123 <                (wt = (ForkJoinWorkerThread)thread).pool == p)
3124 <                w = wt.workQueue;
3125 <            else if ((r = ThreadLocalRandom.getProbe()) != 0 &&
3126 <                     (ws = p.workQueues) != null && (n = ws.length) > 0)
3127 <                w = ws[(n - 1) & r & SQMASK];
3128 <            else
3129 <                w = null;
3130 <            if (w != null)
3131 <                w.helpAsyncBlocker(blocker);
3132 <        }
3272 >    /** ManagedBlock for external threads */
3273 >    private static void unmanagedBlock(ManagedBlocker blocker)
3274 >        throws InterruptedException {
3275 >        if (blocker == null) throw new NullPointerException();
3276 >        do {} while (!blocker.isReleasable() && !blocker.block());
3277      }
3278  
3279 <    // AbstractExecutorService overrides.  These rely on undocumented
3280 <    // fact that ForkJoinTask.adapt returns ForkJoinTasks that also
3281 <    // implement RunnableFuture.
3279 >    // AbstractExecutorService.newTaskFor overrides rely on
3280 >    // undocumented fact that ForkJoinTask.adapt returns ForkJoinTasks
3281 >    // that also implement RunnableFuture.
3282  
3283 +    @Override
3284      protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
3285          return new ForkJoinTask.AdaptedRunnable<T>(runnable, value);
3286      }
3287  
3288 +    @Override
3289      protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
3290          return new ForkJoinTask.AdaptedCallable<T>(callable);
3291      }
3292  
3147    // VarHandle mechanics
3148    private static final VarHandle CTL;
3149    private static final VarHandle MODE;
3150    static final VarHandle QA;
3151
3293      static {
3294          try {
3295              MethodHandles.Lookup l = MethodHandles.lookup();
3296              CTL = l.findVarHandle(ForkJoinPool.class, "ctl", long.class);
3297              MODE = l.findVarHandle(ForkJoinPool.class, "mode", int.class);
3298 <            QA = MethodHandles.arrayElementVarHandle(ForkJoinTask[].class);
3298 >            THREADIDS = l.findVarHandle(ForkJoinPool.class, "threadIds", int.class);
3299 >            POOLIDS = l.findStaticVarHandle(ForkJoinPool.class, "poolIds", int.class);
3300          } catch (ReflectiveOperationException e) {
3301              throw new ExceptionInInitializerError(e);
3302          }
# Line 3175 | Line 3317 | public class ForkJoinPool extends Abstra
3317          defaultForkJoinWorkerThreadFactory =
3318              new DefaultForkJoinWorkerThreadFactory();
3319          modifyThreadPermission = new RuntimePermission("modifyThread");
3178
3320          common = AccessController.doPrivileged(new PrivilegedAction<>() {
3321              public ForkJoinPool run() {
3322                  return new ForkJoinPool((byte)0); }});
3323  
3324          COMMON_PARALLELISM = Math.max(common.mode & SMASK, 1);
3325      }
3185
3186    /**
3187     * Factory for innocuous worker threads.
3188     */
3189    private static final class InnocuousForkJoinWorkerThreadFactory
3190        implements ForkJoinWorkerThreadFactory {
3191
3192        /**
3193         * An ACC to restrict permissions for the factory itself.
3194         * The constructed workers have no permissions set.
3195         */
3196        private static final AccessControlContext ACC = contextWithPermissions(
3197            modifyThreadPermission,
3198            new RuntimePermission("enableContextClassLoaderOverride"),
3199            new RuntimePermission("modifyThreadGroup"),
3200            new RuntimePermission("getClassLoader"),
3201            new RuntimePermission("setContextClassLoader"));
3202
3203        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
3204            return AccessController.doPrivileged(
3205                new PrivilegedAction<>() {
3206                    public ForkJoinWorkerThread run() {
3207                        return new ForkJoinWorkerThread.
3208                            InnocuousForkJoinWorkerThread(pool); }},
3209                ACC);
3210        }
3211    }
3326   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines