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

Comparing jsr166/src/jsr166e/ForkJoinPool.java (file contents):
Revision 1.14 by jsr166, Wed Nov 14 19:05:03 2012 UTC vs.
Revision 1.34 by dl, Mon Dec 17 16:31:08 2012 UTC

# Line 17 | Line 17 | import java.util.concurrent.ExecutorServ
17   import java.util.concurrent.Future;
18   import java.util.concurrent.RejectedExecutionException;
19   import java.util.concurrent.RunnableFuture;
20 import java.util.concurrent.ThreadLocalRandom;
20   import java.util.concurrent.TimeUnit;
21  
22   /**
# Line 38 | Line 37 | import java.util.concurrent.TimeUnit;
37   * ForkJoinPool}s may also be appropriate for use with event-style
38   * tasks that are never joined.
39   *
40 < * <p>A static {@link #commonPool} is available and appropriate for
40 > * <p>A static {@link #commonPool()} is available and appropriate for
41   * most applications. The common pool is used by any ForkJoinTask that
42   * is not explicitly submitted to a specified pool. Using the common
43   * pool normally reduces resource usage (its threads are slowly
# Line 52 | Line 51 | import java.util.concurrent.TimeUnit;
51   * dynamically adding, suspending, or resuming internal worker
52   * threads, even if some tasks are stalled waiting to join
53   * others. However, no such adjustments are guaranteed in the face of
54 < * blocked IO or other unmanaged synchronization. The nested {@link
54 > * blocked I/O or other unmanaged synchronization. The nested {@link
55   * ManagedBlocker} interface enables extension of the kinds of
56   * synchronization accommodated.
57   *
# Line 63 | Line 62 | import java.util.concurrent.TimeUnit;
62   * {@link #toString} returns indications of pool state in a
63   * convenient form for informal monitoring.
64   *
65 < * <p> As is the case with other ExecutorServices, there are three
65 > * <p>As is the case with other ExecutorServices, there are three
66   * main task execution methods summarized in the following table.
67   * These are designed to be used primarily by clients not already
68   * engaged in fork/join computations in the current pool.  The main
# Line 100 | Line 99 | import java.util.concurrent.TimeUnit;
99   *
100   * <p>The common pool is by default constructed with default
101   * parameters, but these may be controlled by setting three {@link
102 < * System#getProperty properties} with prefix {@code
102 > * System#getProperty system properties} with prefix {@code
103   * java.util.concurrent.ForkJoinPool.common}: {@code parallelism} --
104   * an integer greater than zero, {@code threadFactory} -- the class
105   * name of a {@link ForkJoinWorkerThreadFactory}, and {@code
106   * exceptionHandler} -- the class name of a {@link
107 + * java.lang.Thread.UncaughtExceptionHandler
108   * Thread.UncaughtExceptionHandler}. Upon any error in establishing
109   * these settings, default parameters are used.
110   *
# Line 239 | Line 239 | public class ForkJoinPool extends Abstra
239       * enable shutdown.  When used as a lock, it is normally only very
240       * briefly held, so is nearly always available after at most a
241       * brief spin, but we use a monitor-based backup strategy to
242 <     * blocking when needed.
242 >     * block when needed.
243       *
244       * Recording WorkQueues.  WorkQueues are recorded in the
245       * "workQueues" array that is created upon first use and expanded
# Line 248 | Line 248 | public class ForkJoinPool extends Abstra
248       * by a lock but the array is otherwise concurrently readable, and
249       * accessed directly.  To simplify index-based operations, the
250       * array size is always a power of two, and all readers must
251 <     * tolerate null slots. Worker queues are at odd indices Shared
251 >     * tolerate null slots. Worker queues are at odd indices. Shared
252       * (submission) queues are at even indices, up to a maximum of 64
253       * slots, to limit growth even if array needs to expand to add
254       * more workers. Grouping them together in this way simplifies and
# Line 316 | Line 316 | public class ForkJoinPool extends Abstra
316       * execute. However, many other threads may notice the same task
317       * and each signal to wake up a thread that might take it. So in
318       * general, pools will be over-signalled.  When a submission is
319 <     * added or another worker adds a task to a queue that is
320 <     * apparently empty, they signal waiting workers (or trigger
319 >     * added or another worker adds a task to a queue that has fewer
320 >     * than two tasks, they signal waiting workers (or trigger
321       * creation of new ones if fewer than the given parallelism level
322 <     * -- see signalWork).  These primary signals are buttressed by
323 <     * signals whenever other threads scan for work or do not have a
324 <     * task to process. On most platforms, signalling (unpark)
325 <     * overhead time is noticeably long, and the time between
326 <     * signalling a thread and it actually making progress can be very
327 <     * noticeably long, so it is worth offloading these delays from
328 <     * critical paths as much as possible.
322 >     * -- signalWork), and may leave a hint to the unparked worker to
323 >     * help signal others upon wakeup).  These primary signals are
324 >     * buttressed by others (see method helpSignal) whenever other
325 >     * threads scan for work or do not have a task to process.  On
326 >     * most platforms, signalling (unpark) overhead time is noticeably
327 >     * long, and the time between signalling a thread and it actually
328 >     * making progress can be very noticeably long, so it is worth
329 >     * offloading these delays from critical paths as much as
330 >     * possible.
331       *
332       * Trimming workers. To release resources after periods of lack of
333       * use, a worker starting to wait when the pool is quiescent will
# Line 393 | Line 395 | public class ForkJoinPool extends Abstra
395       * steals, rather than use per-task bookkeeping.  This sometimes
396       * requires a linear scan of workQueues array to locate stealers,
397       * but often doesn't because stealers leave hints (that may become
398 <     * stale/wrong) of where to locate them.  A stealHint is only a
399 <     * hint because a worker might have had multiple steals and the
400 <     * hint records only one of them (usually the most current).
401 <     * Hinting isolates cost to when it is needed, rather than adding
402 <     * to per-task overhead.  (2) It is "shallow", ignoring nesting
403 <     * and potentially cyclic mutual steals.  (3) It is intentionally
398 >     * stale/wrong) of where to locate them.  It is only a hint
399 >     * because a worker might have had multiple steals and the hint
400 >     * records only one of them (usually the most current).  Hinting
401 >     * isolates cost to when it is needed, rather than adding to
402 >     * per-task overhead.  (2) It is "shallow", ignoring nesting and
403 >     * potentially cyclic mutual steals.  (3) It is intentionally
404       * racy: field currentJoin is updated only while actively joining,
405       * which means that we miss links in the chain during long-lived
406       * tasks, GC stalls etc (which is OK since blocking in such cases
# Line 525 | Line 527 | public class ForkJoinPool extends Abstra
527       * Default ForkJoinWorkerThreadFactory implementation; creates a
528       * new ForkJoinWorkerThread.
529       */
530 <    static class DefaultForkJoinWorkerThreadFactory
530 >    static final class DefaultForkJoinWorkerThreadFactory
531          implements ForkJoinWorkerThreadFactory {
532 <        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
532 >        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
533              return new ForkJoinWorkerThread(pool);
534          }
535      }
536  
537      /**
538 +     * Per-thread records for threads that submit to pools. Currently
539 +     * holds only pseudo-random seed / index that is used to choose
540 +     * submission queues in method externalPush. In the future, this may
541 +     * also incorporate a means to implement different task rejection
542 +     * and resubmission policies.
543 +     *
544 +     * Seeds for submitters and workers/workQueues work in basically
545 +     * the same way but are initialized and updated using slightly
546 +     * different mechanics. Both are initialized using the same
547 +     * approach as in class ThreadLocal, where successive values are
548 +     * unlikely to collide with previous values. Seeds are then
549 +     * randomly modified upon collisions using xorshifts, which
550 +     * requires a non-zero seed.
551 +     */
552 +    static final class Submitter {
553 +        int seed;
554 +        Submitter(int s) { seed = s; }
555 +    }
556 +
557 +    /**
558       * Class for artificial tasks that are used to replace the target
559       * of local joins if they are removed from an interior queue slot
560       * in WorkQueue.tryRemoveAndExec. We don't need the proxy to
# Line 598 | Line 620 | public class ForkJoinPool extends Abstra
620       * trades off slightly slower average field access for the sake of
621       * avoiding really bad worst-case access. (Until better JVM
622       * support is in place, this padding is dependent on transient
623 <     * properties of JVM field layout rules.)  We also take care in
623 >     * properties of JVM field layout rules.) We also take care in
624       * allocating, sizing and resizing the array. Non-shared queue
625       * arrays are initialized by workers before use. Others are
626       * allocated on first use.
# Line 624 | Line 646 | public class ForkJoinPool extends Abstra
646           */
647          static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M
648  
649 +        // Heuristic padding to ameliorate unfortunate memory placements
650 +        volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06;
651 +
652          int seed;                  // for random scanning; initialize nonzero
653          volatile int eventCount;   // encoded inactivation count; < 0 if inactive
654          int nextWait;              // encoded record of next event waiter
655 <        final int mode;            // lifo, fifo, or shared
631 <        int nsteals;               // cumulative number of steals
655 >        int hint;                  // steal or signal hint (index)
656          int poolIndex;             // index of this queue in pool (or 0)
657 <        int stealHint;             // index of most recent known stealer
657 >        final int mode;            // 0: lifo, > 0: fifo, < 0: shared
658 >        int nsteals;               // number of steals
659          volatile int qlock;        // 1: locked, -1: terminate; else 0
660          volatile int base;         // index of next slot for poll
661          int top;                   // index of next slot for push
# Line 640 | Line 665 | public class ForkJoinPool extends Abstra
665          volatile Thread parker;    // == owner during call to park; else null
666          volatile ForkJoinTask<?> currentJoin;  // task being joined in awaitJoin
667          ForkJoinTask<?> currentSteal; // current non-local task being executed
643        // Heuristic padding to ameliorate unfortunate memory placements
644        Object p00, p01, p02, p03, p04, p05, p06, p07;
645        Object p08, p09, p0a, p0b, p0c, p0d, p0e;
668  
669 <        WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode) {
670 <            this.mode = mode;
669 >        volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17;
670 >        volatile Object pad18, pad19, pad1a, pad1b, pad1c, pad1d;
671 >
672 >        WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode,
673 >                  int seed) {
674              this.pool = pool;
675              this.owner = owner;
676 +            this.mode = mode;
677 +            this.seed = seed;
678              // Place indices in the center of array (that is not yet allocated)
679              base = top = INITIAL_QUEUE_CAPACITY >>> 1;
680          }
681  
682          /**
683 <         * Pushes a task. Call only by owner in unshared queues.
657 <         * Cases needing resizing or rejection are relayed to fullPush
658 <         * (that also handles shared queues).
659 <         *
660 <         * @param task the task. Caller must ensure non-null.
661 <         * @throw RejectedExecutionException if array cannot be resized
683 >         * Returns the approximate number of tasks in the queue.
684           */
685 <        final void push(ForkJoinTask<?> task) {
686 <            ForkJoinPool p; ForkJoinTask<?>[] a;
687 <            int s = top, n;
688 <            if ((a = array) != null && a.length > (n = s + 1 - base)) {
689 <                U.putOrderedObject
690 <                    (a, (((a.length - 1) & s) << ASHIFT) + ABASE, task);
691 <                top = s + 1;
692 <                if (n <= 1 && (p = pool) != null)
693 <                    p.signalWork(this, 1);
694 <            }
695 <            else
696 <                fullPush(task, true);
685 >        final int queueSize() {
686 >            int n = base - top;       // non-owner callers must read base first
687 >            return (n >= 0) ? 0 : -n; // ignore transient negative
688 >        }
689 >
690 >       /**
691 >         * Provides a more accurate estimate of whether this queue has
692 >         * any tasks than does queueSize, by checking whether a
693 >         * near-empty queue has at least one unclaimed task.
694 >         */
695 >        final boolean isEmpty() {
696 >            ForkJoinTask<?>[] a; int m, s;
697 >            int n = base - (s = top);
698 >            return (n >= 0 ||
699 >                    (n == -1 &&
700 >                     ((a = array) == null ||
701 >                      (m = a.length - 1) < 0 ||
702 >                      U.getObject
703 >                      (a, (long)((m & (s - 1)) << ASHIFT) + ABASE) == null)));
704          }
705  
706          /**
707 <         * Pushes a task if lock is free and array is either big
708 <         * enough or can be resized to be big enough. Note: a
680 <         * specialization of a common fast path of this method is in
681 <         * ForkJoinPool.externalPush. When called from a FJWT queue,
682 <         * this can fail only if the pool has been shut down or
683 <         * an out of memory error.
707 >         * Pushes a task. Call only by owner in unshared queues.  (The
708 >         * shared-queue version is embedded in method externalPush.)
709           *
710           * @param task the task. Caller must ensure non-null.
711 <         * @param owned if true, throw RJE on failure
711 >         * @throw RejectedExecutionException if array cannot be resized
712           */
713 <        final boolean fullPush(ForkJoinTask<?> task, boolean owned) {
714 <            ForkJoinPool p; ForkJoinTask<?>[] a;
715 <            if (owned) {
716 <                if (qlock < 0) // must be shutting down
717 <                    throw new RejectedExecutionException();
718 <            }
719 <            else if (!U.compareAndSwapInt(this, QLOCK, 0, 1))
720 <                return false;
721 <            try {
697 <                int s = top, oldLen, len;
698 <                if ((a = array) == null)
699 <                    a = array = new ForkJoinTask<?>[len=INITIAL_QUEUE_CAPACITY];
700 <                else if ((oldLen = a.length) > s + 1 - base)
701 <                    len = oldLen;
702 <                else if ((len = oldLen << 1) > MAXIMUM_QUEUE_CAPACITY)
703 <                    throw new RejectedExecutionException("Capacity exceeded");
704 <                else {
705 <                    int oldMask, b;
706 <                    ForkJoinTask<?>[] oldA = a;
707 <                    a = array = new ForkJoinTask<?>[len];
708 <                    if ((oldMask = oldLen - 1) >= 0 && s - (b = base) > 0) {
709 <                        int mask = len - 1;
710 <                        do {
711 <                            ForkJoinTask<?> x;
712 <                            int oldj = ((b & oldMask) << ASHIFT) + ABASE;
713 <                            int j    = ((b &    mask) << ASHIFT) + ABASE;
714 <                            x = (ForkJoinTask<?>)
715 <                                U.getObjectVolatile(oldA, oldj);
716 <                            if (x != null &&
717 <                                U.compareAndSwapObject(oldA, oldj, x, null))
718 <                                U.putObjectVolatile(a, j, x);
719 <                        } while (++b != s);
720 <                    }
713 >        final void push(ForkJoinTask<?> task) {
714 >            ForkJoinTask<?>[] a; ForkJoinPool p;
715 >            int s = top, m, n;
716 >            if ((a = array) != null) {    // ignore if queue removed
717 >                int j = (((m = a.length - 1) & s) << ASHIFT) + ABASE;
718 >                U.putOrderedObject(a, j, task);
719 >                if ((n = (top = s + 1) - base) <= 2) {
720 >                    if ((p = pool) != null)
721 >                        p.signalWork(this);
722                  }
723 <                U.putOrderedObject
724 <                    (a, (((len - 1) & s) << ASHIFT) + ABASE, task);
724 <                top = s + 1;
725 <            } finally {
726 <                if (!owned)
727 <                    qlock = 0;
723 >                else if (n >= m)
724 >                    growArray();
725              }
726 <            if ((p = pool) != null)
727 <                p.signalWork(this, 1);
728 <            return true;
726 >        }
727 >
728 >       /**
729 >         * Initializes or doubles the capacity of array. Call either
730 >         * by owner or with lock held -- it is OK for base, but not
731 >         * top, to move while resizings are in progress.
732 >         */
733 >        final ForkJoinTask<?>[] growArray() {
734 >            ForkJoinTask<?>[] oldA = array;
735 >            int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
736 >            if (size > MAXIMUM_QUEUE_CAPACITY)
737 >                throw new RejectedExecutionException("Queue capacity exceeded");
738 >            int oldMask, t, b;
739 >            ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
740 >            if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
741 >                (t = top) - (b = base) > 0) {
742 >                int mask = size - 1;
743 >                do {
744 >                    ForkJoinTask<?> x;
745 >                    int oldj = ((b & oldMask) << ASHIFT) + ABASE;
746 >                    int j    = ((b &    mask) << ASHIFT) + ABASE;
747 >                    x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
748 >                    if (x != null &&
749 >                        U.compareAndSwapObject(oldA, oldj, x, null))
750 >                        U.putObjectVolatile(a, j, x);
751 >                } while (++b != t);
752 >            }
753 >            return a;
754          }
755  
756          /**
# Line 852 | Line 874 | public class ForkJoinPool extends Abstra
874              return seed = r ^= r << 5;
875          }
876  
855        /**
856         * Provides a more accurate estimate of size than (top - base)
857         * by ordering reads and checking whether a near-empty queue
858         * has at least one unclaimed task.
859         */
860        final int queueSize() {
861            ForkJoinTask<?>[] a; int k, s, n;
862            return ((n = base - (s = top)) < 0 &&
863                    (n != -1 ||
864                     ((a = array) != null && (k = a.length) > 0 &&
865                      U.getObject
866                      (a, (long)((((k - 1) & (s - 1)) << ASHIFT) + ABASE)) != null))) ?
867                -n : 0;
868        }
869
877          // Specialized execution methods
878  
879          /**
# Line 980 | Line 987 | public class ForkJoinPool extends Abstra
987              if (t != null) {
988                  (currentSteal = t).doExec();
989                  currentSteal = null;
990 <                if (++nsteals < 0) {     // spill on overflow
991 <                    ForkJoinPool p;
985 <                    if ((p = pool) != null)
986 <                        p.collectStealCount(this);
987 <                }
988 <                if (top != base) {       // process remaining local tasks
990 >                ++nsteals;
991 >                if (base - top < 0) {       // process remaining local tasks
992                      if (mode == 0)
993                          popAndExecAll();
994                      else
# Line 1017 | Line 1020 | public class ForkJoinPool extends Abstra
1020                      s != Thread.State.TIMED_WAITING);
1021          }
1022  
1020        /**
1021         * If this owned and is not already interrupted, try to
1022         * interrupt and/or unpark, ignoring exceptions.
1023         */
1024        final void interruptOwner() {
1025            Thread wt, p;
1026            if ((wt = owner) != null && !wt.isInterrupted()) {
1027                try {
1028                    wt.interrupt();
1029                } catch (SecurityException ignore) {
1030                }
1031            }
1032            if ((p = parker) != null)
1033                U.unpark(p);
1034        }
1035
1023          // Unsafe mechanics
1024          private static final sun.misc.Unsafe U;
1025          private static final long QLOCK;
# Line 1057 | Line 1044 | public class ForkJoinPool extends Abstra
1044          }
1045      }
1046  
1060    /**
1061     * Per-thread records for threads that submit to pools. Currently
1062     * holds only pseudo-random seed / index that is used to choose
1063     * submission queues in method externalPush. In the future, this may
1064     * also incorporate a means to implement different task rejection
1065     * and resubmission policies.
1066     *
1067     * Seeds for submitters and workers/workQueues work in basically
1068     * the same way but are initialized and updated using slightly
1069     * different mechanics. Both are initialized using the same
1070     * approach as in class ThreadLocal, where successive values are
1071     * unlikely to collide with previous values. Seeds are then
1072     * randomly modified upon collisions using xorshifts, which
1073     * requires a non-zero seed.
1074     */
1075    static final class Submitter {
1076        int seed;
1077        Submitter(int s) { seed = s; }
1078    }
1079
1080    /** Property prefix for constructing common pool */
1081    private static final String propPrefix =
1082        "java.util.concurrent.ForkJoinPool.common.";
1083
1047      // static fields (initialized in static initializer below)
1048  
1049      /**
# Line 1091 | Line 1054 | public class ForkJoinPool extends Abstra
1054          defaultForkJoinWorkerThreadFactory;
1055  
1056      /**
1057 <     * Common (static) pool. Non-null for public use unless a static
1058 <     * construction exception, but internal usages null-check on use
1059 <     * to paranoically avoid potential initialization circularities
1060 <     * as well as to simplify generated code.
1057 >     * Per-thread submission bookkeeping. Shared across all pools
1058 >     * to reduce ThreadLocal pollution and because random motion
1059 >     * to avoid contention in one pool is likely to hold for others.
1060 >     * Lazily initialized on first submission (but null-checked
1061 >     * in other contexts to avoid unnecessary initialization).
1062       */
1063 <    static final ForkJoinPool commonPool;
1063 >    static final ThreadLocal<Submitter> submitters;
1064  
1065      /**
1066       * Permission required for callers of methods that may start or
# Line 1105 | Line 1069 | public class ForkJoinPool extends Abstra
1069      private static final RuntimePermission modifyThreadPermission;
1070  
1071      /**
1072 <     * Per-thread submission bookkeeping. Shared across all pools
1073 <     * to reduce ThreadLocal pollution and because random motion
1074 <     * to avoid contention in one pool is likely to hold for others.
1075 <     * Lazily initialized on first submission (but null-checked
1112 <     * in other contexts to avoid unnecessary initialization).
1072 >     * Common (static) pool. Non-null for public use unless a static
1073 >     * construction exception, but internal usages null-check on use
1074 >     * to paranoically avoid potential initialization circularities
1075 >     * as well as to simplify generated code.
1076       */
1077 <    static final ThreadLocal<Submitter> submitters;
1077 >    static final ForkJoinPool commonPool;
1078  
1079      /**
1080       * Common pool parallelism. Must equal commonPool.parallelism.
# Line 1149 | Line 1112 | public class ForkJoinPool extends Abstra
1112      private static final long FAST_IDLE_TIMEOUT =  200L * 1000L * 1000L;
1113  
1114      /**
1115 +     * Tolerance for idle timeouts, to cope with timer undershoots
1116 +     */
1117 +    private static final long TIMEOUT_SLOP = 2000000L;
1118 +
1119 +    /**
1120       * The maximum stolen->joining link depth allowed in method
1121       * tryHelpStealer.  Must be a power of two.  Depths for legitimate
1122       * chains are unbounded, but we use a fixed constant to avoid
# Line 1248 | Line 1216 | public class ForkJoinPool extends Abstra
1216      static final int FIFO_QUEUE          =  1;
1217      static final int SHARED_QUEUE        = -1;
1218  
1219 +    // bounds for #steps in scan loop -- must be power 2 minus 1
1220 +    private static final int MIN_SCAN    = 0x1ff;   // cover estimation slop
1221 +    private static final int MAX_SCAN    = 0x1ffff; // 4 * max workers
1222 +
1223      // Instance fields
1224  
1225      /*
1226 <     * Field layout order in this class tends to matter more than one
1227 <     * would like. Runtime layout order is only loosely related to
1226 >     * Field layout of this class tends to matter more than one would
1227 >     * like. Runtime layout order is only loosely related to
1228       * declaration order and may differ across JVMs, but the following
1229       * empirically works OK on current JVMs.
1230       */
1231 +
1232 +    // Heuristic padding to ameliorate unfortunate memory placements
1233 +    volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06;
1234 +
1235      volatile long stealCount;                  // collects worker counts
1236      volatile long ctl;                         // main pool control
1261    final int parallelism;                     // parallelism level
1262    final int localMode;                       // per-worker scheduling mode
1263    volatile int indexSeed;                    // worker/submitter index seed
1237      volatile int plock;                        // shutdown status and seqLock
1238 +    volatile int indexSeed;                    // worker/submitter index seed
1239 +    final int config;                          // mode and parallelism level
1240      WorkQueue[] workQueues;                    // main registry
1241 <    final ForkJoinWorkerThreadFactory factory; // factory for new workers
1241 >    final ForkJoinWorkerThreadFactory factory;
1242      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1243      final String workerNamePrefix;             // to create worker name string
1244  
1245 +    volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17;
1246 +    volatile Object pad18, pad19, pad1a, pad1b;
1247 +
1248      /*
1249       * Acquires the plock lock to protect worker array and related
1250       * updates. This method is called only if an initial CAS on plock
1251       * fails. This acts as a spinLock for normal cases, but falls back
1252       * to builtin monitor to block when (rarely) needed. This would be
1253       * a terrible idea for a highly contended lock, but works fine as
1254 <     * a more conservative alternative to a pure spinlock.  See
1277 <     * internal ConcurrentHashMap documentation for further
1278 <     * explanation of nearly the same construction.
1254 >     * a more conservative alternative to a pure spinlock.
1255       */
1256      private int acquirePlock() {
1257          int spins = PL_SPINS, r = 0, ps, nps;
# Line 1283 | Line 1259 | public class ForkJoinPool extends Abstra
1259              if (((ps = plock) & PL_LOCK) == 0 &&
1260                  U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK))
1261                  return nps;
1262 <            else if (r == 0)
1263 <                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1262 >            else if (r == 0) { // randomize spins if possible
1263 >                Thread t = Thread.currentThread(); WorkQueue w; Submitter z;
1264 >                if ((t instanceof ForkJoinWorkerThread) &&
1265 >                    (w = ((ForkJoinWorkerThread)t).workQueue) != null)
1266 >                    r = w.seed;
1267 >                else if ((z = submitters.get()) != null)
1268 >                    r = z.seed;
1269 >                else
1270 >                    r = 1;
1271 >            }
1272              else if (spins >= 0) {
1273                  r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1274                  if (r >= 0)
# Line 1318 | Line 1302 | public class ForkJoinPool extends Abstra
1302          synchronized (this) { notifyAll(); }
1303      }
1304  
1305 +    /**
1306 +     * Performs secondary initialization, called when plock is zero.
1307 +     * Creates workQueue array and sets plock to a valid value.  The
1308 +     * lock body must be exception-free (so no try/finally) so we
1309 +     * optimistically allocate new array outside the lock and throw
1310 +     * away if (very rarely) not needed. (A similar tactic is used in
1311 +     * fullExternalPush.)  Because the plock seq value can eventually
1312 +     * wrap around zero, this method harmlessly fails to reinitialize
1313 +     * if workQueues exists, while still advancing plock.
1314 +     *
1315 +     * Additionally tries to create the first worker.
1316 +     */
1317 +    private void initWorkers() {
1318 +        WorkQueue[] ws, nws; int ps;
1319 +        int p = config & SMASK;        // find power of two table size
1320 +        int n = (p > 1) ? p - 1 : 1;   // ensure at least 2 slots
1321 +        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1322 +        n = (n + 1) << 1;
1323 +        if ((ws = workQueues) == null || ws.length == 0)
1324 +            nws = new WorkQueue[n];
1325 +        else
1326 +            nws = null;
1327 +        if (((ps = plock) & PL_LOCK) != 0 ||
1328 +            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1329 +            ps = acquirePlock();
1330 +        if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1331 +            workQueues = nws;
1332 +        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1333 +        if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1334 +            releasePlock(nps);
1335 +        tryAddWorker();
1336 +    }
1337 +
1338 +    /**
1339 +     * Tries to create and start one worker if fewer than target
1340 +     * parallelism level exist. Adjusts counts etc on failure.
1341 +     */
1342 +    private void tryAddWorker() {
1343 +        long c; int u;
1344 +        while ((u = (int)((c = ctl) >>> 32)) < 0 &&
1345 +               (u & SHORT_SIGN) != 0 && (int)c == 0) {
1346 +            long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1347 +                             ((u + UAC_UNIT) & UAC_MASK)) << 32;
1348 +            if (U.compareAndSwapLong(this, CTL, c, nc)) {
1349 +                ForkJoinWorkerThreadFactory fac;
1350 +                Throwable ex = null;
1351 +                ForkJoinWorkerThread wt = null;
1352 +                try {
1353 +                    if ((fac = factory) != null &&
1354 +                        (wt = fac.newThread(this)) != null) {
1355 +                        wt.start();
1356 +                        break;
1357 +                    }
1358 +                } catch (Throwable e) {
1359 +                    ex = e;
1360 +                }
1361 +                deregisterWorker(wt, ex);
1362 +                break;
1363 +            }
1364 +        }
1365 +    }
1366 +
1367      //  Registering and deregistering workers
1368  
1369      /**
1370 <     * Callback from ForkJoinWorkerThread constructor to establish its
1371 <     * poolIndex and record its WorkQueue. To avoid scanning bias due
1372 <     * to packing entries in front of the workQueues array, we treat
1373 <     * the array as a simple power-of-two hash table using per-thread
1374 <     * seed as hash, expanding as needed.
1375 <     *
1376 <     * @param w the worker's queue
1377 <     */
1378 <    final void registerWorker(WorkQueue w) {
1379 <        int s, ps; // generate a rarely colliding candidate index seed
1380 <        do {} while (!U.compareAndSwapInt(this, INDEXSEED,
1381 <                                          s = indexSeed, s += SEED_INCREMENT) ||
1370 >     * Callback from ForkJoinWorkerThread to establish and record its
1371 >     * WorkQueue. To avoid scanning bias due to packing entries in
1372 >     * front of the workQueues array, we treat the array as a simple
1373 >     * power-of-two hash table using per-thread seed as hash,
1374 >     * expanding as needed.
1375 >     *
1376 >     * @param wt the worker thread
1377 >     * @return the worker's queue
1378 >     */
1379 >    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
1380 >        Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps;
1381 >        wt.setDaemon(true);
1382 >        if ((handler = ueh) != null)
1383 >            wt.setUncaughtExceptionHandler(handler);
1384 >        do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
1385 >                                          s += SEED_INCREMENT) ||
1386                       s == 0); // skip 0
1387 +        WorkQueue w = new WorkQueue(this, wt, config >>> 16, s);
1388          if (((ps = plock) & PL_LOCK) != 0 ||
1389              !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1390              ps = acquirePlock();
1391          int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1392          try {
1393 <            WorkQueue[] ws;
1343 <            if (w != null && (ws = workQueues) != null) {
1344 <                w.seed = s;
1393 >            if ((ws = workQueues) != null) {    // skip if shutting down
1394                  int n = ws.length, m = n - 1;
1395 <                int r = (s << 1) | 1;               // use odd-numbered indices
1396 <                if (ws[r &= m] != null) {           // collision
1397 <                    int probes = 0;                 // step by approx half size
1395 >                int r = (s << 1) | 1;           // use odd-numbered indices
1396 >                if (ws[r &= m] != null) {       // collision
1397 >                    int probes = 0;             // step by approx half size
1398                      int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
1399                      while (ws[r = (r + step) & m] != null) {
1400                          if (++probes >= n) {
# Line 1355 | Line 1404 | public class ForkJoinPool extends Abstra
1404                          }
1405                      }
1406                  }
1407 <                w.eventCount = w.poolIndex = r;     // establish before recording
1407 >                w.eventCount = w.poolIndex = r; // volatile write orders
1408                  ws[r] = w;
1409              }
1410          } finally {
1411              if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1412                  releasePlock(nps);
1413          }
1414 +        wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex)));
1415 +        return w;
1416      }
1417  
1418      /**
# Line 1377 | Line 1428 | public class ForkJoinPool extends Abstra
1428          WorkQueue w = null;
1429          if (wt != null && (w = wt.workQueue) != null) {
1430              int ps;
1380            collectStealCount(w);
1431              w.qlock = -1;                // ensure set
1432 +            long ns = w.nsteals, sc;     // collect steal count
1433 +            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1434 +                                               sc = stealCount, sc + ns));
1435              if (((ps = plock) & PL_LOCK) != 0 ||
1436                  !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1437                  ps = acquirePlock();
# Line 1394 | Line 1447 | public class ForkJoinPool extends Abstra
1447              }
1448          }
1449  
1450 <        long c;                             // adjust ctl counts
1450 >        long c;                          // adjust ctl counts
1451          do {} while (!U.compareAndSwapLong
1452                       (this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) |
1453                                             ((c - TC_UNIT) & TC_MASK) |
1454                                             (c & ~(AC_MASK|TC_MASK)))));
1455  
1456 <        if (!tryTerminate(false, false) && w != null) {
1457 <            w.cancelAll();                  // cancel remaining tasks
1458 <            if (w.array != null)            // suppress signal if never ran
1459 <                signalWork(null, 1);        // wake up or create replacement
1460 <            if (ex == null)                 // help clean refs on way out
1461 <                ForkJoinTask.helpExpungeStaleExceptions();
1456 >        if (!tryTerminate(false, false) && w != null && w.array != null) {
1457 >            w.cancelAll();               // cancel remaining tasks
1458 >            WorkQueue[] ws; WorkQueue v; Thread p; int u, i, e;
1459 >            while ((u = (int)((c = ctl) >>> 32)) < 0 && (e = (int)c) >= 0) {
1460 >                if (e > 0) {             // activate or create replacement
1461 >                    if ((ws = workQueues) == null ||
1462 >                        (i = e & SMASK) >= ws.length ||
1463 >                        (v = ws[i]) != null)
1464 >                        break;
1465 >                    long nc = (((long)(v.nextWait & E_MASK)) |
1466 >                               ((long)(u + UAC_UNIT) << 32));
1467 >                    if (v.eventCount != (e | INT_SIGN))
1468 >                        break;
1469 >                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1470 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1471 >                        if ((p = v.parker) != null)
1472 >                            U.unpark(p);
1473 >                        break;
1474 >                    }
1475 >                }
1476 >                else {
1477 >                    if ((short)u < 0)
1478 >                        tryAddWorker();
1479 >                    break;
1480 >                }
1481 >            }
1482          }
1483 <
1484 <        if (ex != null)                     // rethrow
1483 >        if (ex == null)                     // help clean refs on way out
1484 >            ForkJoinTask.helpExpungeStaleExceptions();
1485 >        else                                // rethrow
1486              ForkJoinTask.rethrow(ex);
1487      }
1488  
1415    /**
1416     * Collect worker steal count into total. Called on termination
1417     * and upon int overflow of local count. (There is a possible race
1418     * in the latter case vs any caller of getStealCount, which can
1419     * make its results less accurate than usual.)
1420     */
1421    final void collectStealCount(WorkQueue w) {
1422        if (w != null) {
1423            long sc;
1424            int ns = w.nsteals;
1425            w.nsteals = 0; // handle overflow
1426            long steals = (ns >= 0) ? ns : 1L + (long)(Integer.MAX_VALUE);
1427            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1428                                               sc = stealCount, sc + steals));
1429        }
1430    }
1431
1489      // Submissions
1490  
1491      /**
# Line 1445 | Line 1502 | public class ForkJoinPool extends Abstra
1502              (ws = workQueues) != null && (m = (ws.length - 1)) >= 0 &&
1503              (q = ws[m & z.seed & SQMASK]) != null &&
1504              U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock
1505 <            int s = q.top, n;
1506 <            if ((a = q.array) != null && a.length > (n = s + 1 - q.base)) {
1507 <                U.putObject(a, (long)(((a.length - 1) & s) << ASHIFT) + ABASE,
1508 <                            task);
1505 >            int b = q.base, s = q.top, n, an;
1506 >            if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) {
1507 >                int j = (((an - 1) & s) << ASHIFT) + ABASE;
1508 >                U.putOrderedObject(a, j, task);
1509                  q.top = s + 1;                     // push on to deque
1510                  q.qlock = 0;
1511 <                if (n <= 1)
1512 <                    signalWork(q, 1);
1511 >                if (n <= 2)
1512 >                    signalWork(q);
1513                  return;
1514              }
1515              q.qlock = 0;
# Line 1463 | Line 1520 | public class ForkJoinPool extends Abstra
1520      /**
1521       * Full version of externalPush. This method is called, among
1522       * other times, upon the first submission of the first task to the
1523 <     * pool, so must perform secondary initialization: creating
1524 <     * workQueue array and setting plock to a valid value. It also
1525 <     * detects first submission by an external thread by looking up
1526 <     * its ThreadLocal, and creates a new shared queue if the one at
1527 <     * index if empty or contended. The lock bodies must be
1528 <     * exception-free (so no try/finally) so we optimistically
1529 <     * allocate new queues/arrays outside the locks and throw them
1473 <     * away if (very rarely) not needed. Note that the plock seq value
1474 <     * can eventually wrap around zero, but if so harmlessly fails to
1475 <     * reinitialize.
1523 >     * pool, so must perform secondary initialization (via
1524 >     * initWorkers). It also detects first submission by an external
1525 >     * thread by looking up its ThreadLocal, and creates a new shared
1526 >     * queue if the one at index if empty or contended. The plock lock
1527 >     * body must be exception-free (so no try/finally) so we
1528 >     * optimistically allocate new queues outside the lock and throw
1529 >     * them away if (very rarely) not needed.
1530       */
1531      private void fullExternalPush(ForkJoinTask<?> task) {
1532 <        for (Submitter z = null;;) {
1533 <            WorkQueue[] ws; WorkQueue q; int ps, m, r, s;
1534 <            if ((ps = plock) < 0)
1532 >        int r = 0; // random index seed
1533 >        for (Submitter z = submitters.get();;) {
1534 >            WorkQueue[] ws; WorkQueue q; int ps, m, k;
1535 >            if (z == null) {
1536 >                if (U.compareAndSwapInt(this, INDEXSEED, r = indexSeed,
1537 >                                        r += SEED_INCREMENT) && r != 0)
1538 >                    submitters.set(z = new Submitter(r));
1539 >            }
1540 >            else if (r == 0) {               // move to a different index
1541 >                r = z.seed;
1542 >                r ^= r << 13;                // same xorshift as WorkQueues
1543 >                r ^= r >>> 17;
1544 >                z.seed = r ^ (r << 5);
1545 >            }
1546 >            else if ((ps = plock) < 0)
1547                  throw new RejectedExecutionException();
1548 <            else if ((ws = workQueues) == null || (m = ws.length - 1) < 0) {
1549 <                int n = parallelism - 1; n |= n >>> 1; n |= n >>> 2;
1550 <                n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1551 <                WorkQueue[] nws = new WorkQueue[(n + 1) << 1]; // power of two
1552 <                if ((ps & PL_LOCK) != 0 ||
1548 >            else if (ps == 0 || (ws = workQueues) == null ||
1549 >                     (m = ws.length - 1) < 0)
1550 >                initWorkers();
1551 >            else if ((q = ws[k = r & m & SQMASK]) != null) {
1552 >                if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
1553 >                    ForkJoinTask<?>[] a = q.array;
1554 >                    int s = q.top;
1555 >                    boolean submitted = false;
1556 >                    try {                      // locked version of push
1557 >                        if ((a != null && a.length > s + 1 - q.base) ||
1558 >                            (a = q.growArray()) != null) {   // must presize
1559 >                            int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
1560 >                            U.putOrderedObject(a, j, task);
1561 >                            q.top = s + 1;
1562 >                            submitted = true;
1563 >                        }
1564 >                    } finally {
1565 >                        q.qlock = 0;  // unlock
1566 >                    }
1567 >                    if (submitted) {
1568 >                        signalWork(q);
1569 >                        return;
1570 >                    }
1571 >                }
1572 >                r = 0; // move on failure
1573 >            }
1574 >            else if (((ps = plock) & PL_LOCK) == 0) { // create new queue
1575 >                q = new WorkQueue(this, null, SHARED_QUEUE, r);
1576 >                if (((ps = plock) & PL_LOCK) != 0 ||
1577                      !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1578                      ps = acquirePlock();
1579 <                if ((ws = workQueues) == null)
1580 <                    workQueues = nws;
1579 >                if ((ws = workQueues) != null && k < ws.length && ws[k] == null)
1580 >                    ws[k] = q;
1581                  int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1582                  if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1583                      releasePlock(nps);
1584              }
1585 <            else if (z == null && (z = submitters.get()) == null) {
1586 <                if (U.compareAndSwapInt(this, INDEXSEED,
1497 <                                        s = indexSeed, s += SEED_INCREMENT) &&
1498 <                    s != 0) // skip 0
1499 <                    submitters.set(z = new Submitter(s));
1500 <            }
1501 <            else {
1502 <                int k = (r = z.seed) & m & SQMASK;
1503 <                if ((q = ws[k]) == null && (ps & PL_LOCK) == 0) {
1504 <                    (q = new WorkQueue(this, null, SHARED_QUEUE)).poolIndex = k;
1505 <                    if (((ps = plock) & PL_LOCK) != 0 ||
1506 <                        !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1507 <                        ps = acquirePlock();
1508 <                    WorkQueue w = null;
1509 <                    if ((ws = workQueues) != null && k < ws.length &&
1510 <                        (w = ws[k]) == null)
1511 <                        ws[k] = q;
1512 <                    else
1513 <                        q = w;
1514 <                    int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1515 <                    if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1516 <                        releasePlock(nps);
1517 <                }
1518 <                if (q != null && q.qlock == 0 && q.fullPush(task, false))
1519 <                    return;
1520 <                r ^= r << 13;                // same xorshift as WorkQueues
1521 <                r ^= r >>> 17;
1522 <                z.seed = r ^= r << 5;        // move to a different index
1523 <            }
1585 >            else
1586 >                r = 0; // try elsewhere while lock held
1587          }
1588      }
1589  
# Line 1535 | Line 1598 | public class ForkJoinPool extends Abstra
1598      }
1599  
1600      /**
1601 <     * Tries to create (at most one) or activate (possibly several)
1539 <     * workers if too few are active. On contention failure, continues
1540 <     * until at least one worker is signalled or the given queue is
1541 <     * empty or all workers are active.
1601 >     * Tries to create or activate a worker if too few are active.
1602       *
1603 <     * @param q if non-null, the queue holding tasks to be signalled
1544 <     * @param signals the target number of signals.
1603 >     * @param q the (non-null) queue holding tasks to be signalled
1604       */
1605 <    final void signalWork(WorkQueue q, int signals) {
1606 <        long c; int e, u, i; WorkQueue[] ws; WorkQueue w; Thread p;
1605 >    final void signalWork(WorkQueue q) {
1606 >        int hint = q.poolIndex;
1607 >        long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p;
1608          while ((u = (int)((c = ctl) >>> 32)) < 0) {
1609              if ((e = (int)c) > 0) {
1610                  if ((ws = workQueues) != null && ws.length > (i = e & SMASK) &&
# Line 1552 | Line 1612 | public class ForkJoinPool extends Abstra
1612                      long nc = (((long)(w.nextWait & E_MASK)) |
1613                                 ((long)(u + UAC_UNIT) << 32));
1614                      if (U.compareAndSwapLong(this, CTL, c, nc)) {
1615 +                        w.hint = hint;
1616                          w.eventCount = (e + E_SEQ) & E_MASK;
1617                          if ((p = w.parker) != null)
1618                              U.unpark(p);
1619 <                        if (--signals <= 0)
1559 <                            break;
1619 >                        break;
1620                      }
1621 <                    else
1562 <                        signals = 1;
1563 <                    if ((q != null && q.queueSize() == 0))
1621 >                    if (q.top - q.base <= 0)
1622                          break;
1623                  }
1624                  else
1625                      break;
1626              }
1627 <            else if (e == 0 && (u & SHORT_SIGN) != 0) {
1628 <                long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1629 <                                 ((u + UAC_UNIT) & UAC_MASK)) << 32;
1572 <                if (U.compareAndSwapLong(this, CTL, c, nc)) {
1573 <                    ForkJoinWorkerThread wt = null;
1574 <                    Throwable ex = null;
1575 <                    boolean started = false;
1576 <                    try {
1577 <                        ForkJoinWorkerThreadFactory fac;
1578 <                        if ((fac = factory) != null &&
1579 <                            (wt = fac.newThread(this)) != null) {
1580 <                            wt.start();
1581 <                            started = true;
1582 <                        }
1583 <                    } catch (Throwable rex) {
1584 <                        ex = rex;
1585 <                    }
1586 <                    if (!started)
1587 <                        deregisterWorker(wt, ex); // adjust counts on failure
1588 <                    break;
1589 <                }
1590 <            }
1591 <            else
1627 >            else {
1628 >                if ((short)u < 0)
1629 >                    tryAddWorker();
1630                  break;
1631 +            }
1632          }
1633      }
1634  
# Line 1599 | Line 1638 | public class ForkJoinPool extends Abstra
1638       * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1639       */
1640      final void runWorker(WorkQueue w) {
1641 <        // initialize queue array in this thread
1603 <        w.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY];
1641 >        w.growArray(); // allocate queue
1642          do { w.runTask(scan(w)); } while (w.qlock >= 0);
1643      }
1644  
# Line 1612 | Line 1650 | public class ForkJoinPool extends Abstra
1650       * contention, or state changes that indicate possible success on
1651       * re-invocation.
1652       *
1653 <     * The scan searches for tasks across a random permutation of
1654 <     * queues (starting at a random index and stepping by a random
1655 <     * relative prime, checking each at least once).  The scan
1656 <     * terminates upon either finding a non-empty queue, or completing
1657 <     * the sweep. If the worker is not inactivated, it takes and
1658 <     * returns a task from this queue. Otherwise, if not activated, it
1659 <     * signals workers (that may include itself) and returns so caller
1660 <     * can retry. Also returns for trtry if the worker array may have
1661 <     * changed during an empty scan.  On failure to find a task, we
1662 <     * take one of the following actions, after which the caller will
1663 <     * retry calling this method unless terminated.
1653 >     * The scan searches for tasks across queues (starting at a random
1654 >     * index, and relying on registerWorker to irregularly scatter
1655 >     * them within array to avoid bias), checking each at least twice.
1656 >     * The scan terminates upon either finding a non-empty queue, or
1657 >     * completing the sweep. If the worker is not inactivated, it
1658 >     * takes and returns a task from this queue. Otherwise, if not
1659 >     * activated, it signals workers (that may include itself) and
1660 >     * returns so caller can retry. Also returns for true if the
1661 >     * worker array may have changed during an empty scan.  On failure
1662 >     * to find a task, we take one of the following actions, after
1663 >     * which the caller will retry calling this method unless
1664 >     * terminated.
1665       *
1666       * * If pool is terminating, terminate the worker.
1667       *
1668       * * If not already enqueued, try to inactivate and enqueue the
1669       * worker on wait queue. Or, if inactivating has caused the pool
1670 <     * to be quiescent, relay to idleAwaitWork to check for
1671 <     * termination and possibly shrink pool.
1670 >     * to be quiescent, relay to idleAwaitWork to possibly shrink
1671 >     * pool.
1672       *
1673       * * If already enqueued and none of the above apply, possibly
1674 <     * (with 1/2 probability) park awaiting signal, else lingering to
1675 <     * help scan and signal.
1674 >     * park awaiting signal, else lingering to help scan and signal.
1675 >     *
1676 >     * * If a non-empty queue discovered or left as a hint,
1677 >     * help wake up other workers before return
1678       *
1679       * @param w the worker (via its WorkQueue)
1680       * @return a task or null if none found
1681       */
1682      private final ForkJoinTask<?> scan(WorkQueue w) {
1683 <        WorkQueue[] ws; WorkQueue q;           // first update random seed
1684 <        int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1685 <        int ps = plock, m;                     // volatile read order matters
1686 <        if ((ws = workQueues) != null && (m = ws.length - 1) > 0) {
1687 <            int ec = w.eventCount;             // ec is negative if inactive
1688 <            int step = (r >>> 16) | 1;         // relatively prime
1689 <            for (int j = (m + 1) << 2;  ; --j, r += step) {
1690 <                ForkJoinTask<?> t; ForkJoinTask<?>[] a; int b, n;
1691 <                if ((q = ws[r & m]) != null && (b = q.base) - q.top < 0 &&
1692 <                    (a = q.array) != null) {   // probably nonempty
1683 >        WorkQueue[] ws; int m;
1684 >        int ps = plock;                          // read plock before ws
1685 >        if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1686 >            int ec = w.eventCount;               // ec is negative if inactive
1687 >            int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1688 >            w.hint = -1;                         // update seed and clear hint
1689 >            int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN;
1690 >            do {
1691 >                WorkQueue q; ForkJoinTask<?>[] a; int b;
1692 >                if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 &&
1693 >                    (a = q.array) != null) {     // probably nonempty
1694                      int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
1695 <                    t = (ForkJoinTask<?>)U.getObjectVolatile(a, i);
1695 >                    ForkJoinTask<?> t = (ForkJoinTask<?>)
1696 >                        U.getObjectVolatile(a, i);
1697                      if (q.base == b && ec >= 0 && t != null &&
1698                          U.compareAndSwapObject(a, i, t, null)) {
1699 <                        if ((n = q.top - (q.base = b + 1)) > 0)
1700 <                            signalWork(q, n);
1701 <                        return t;              // taken
1702 <                    }
1703 <                    if (j < m || (ec < 0 && (ec = w.eventCount) < 0)) {
1704 <                        if ((n = q.queueSize() - 1) > 0)
1705 <                            signalWork(q, n);
1706 <                        break;                 // let caller retry after signal
1707 <                    }
1708 <                }
1709 <                else if (j < 0) {              // end of scan
1710 <                    long c = ctl; int e;
1711 <                    if (plock != ps)           // incomplete sweep
1712 <                        break;
1713 <                    if ((e = (int)c) < 0)      // pool is terminating
1714 <                        w.qlock = -1;
1715 <                    else if (ec >= 0) {        // try to enqueue/inactivate
1716 <                        long nc = ((long)ec |
1717 <                                   ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1718 <                        w.nextWait = e;
1719 <                        w.eventCount = ec | INT_SIGN; // mark as inactive
1720 <                        if (ctl != c ||
1721 <                            !U.compareAndSwapLong(this, CTL, c, nc))
1722 <                            w.eventCount = ec; // unmark on CAS failure
1723 <                        else if ((int)(c >> AC_SHIFT) == 1 - parallelism)
1724 <                            idleAwaitWork(w, nc, c);  // quiescent
1699 >                        if ((q.base = b + 1) - q.top < 0)
1700 >                            signalWork(q);
1701 >                        return t;                // taken
1702 >                    }
1703 >                    else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) {
1704 >                        w.hint = (r + j) & m;    // help signal below
1705 >                        break;                   // cannot take
1706 >                    }
1707 >                }
1708 >            } while (--j >= 0);
1709 >
1710 >            int h, e, ns; long c, sc; WorkQueue q;
1711 >            if ((ns = w.nsteals) != 0) {
1712 >                if (U.compareAndSwapLong(this, STEALCOUNT,
1713 >                                         sc = stealCount, sc + ns))
1714 >                    w.nsteals = 0;               // collect steals and rescan
1715 >            }
1716 >            else if (plock != ps)                // consistency check
1717 >                ;                                // skip
1718 >            else if ((e = (int)(c = ctl)) < 0)
1719 >                w.qlock = -1;                    // pool is terminating
1720 >            else {
1721 >                if ((h = w.hint) < 0) {
1722 >                    if (ec >= 0) {               // try to enqueue/inactivate
1723 >                        long nc = (((long)ec |
1724 >                                    ((c - AC_UNIT) & (AC_MASK|TC_MASK))));
1725 >                        w.nextWait = e;          // link and mark inactive
1726 >                        w.eventCount = ec | INT_SIGN;
1727 >                        if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1728 >                            w.eventCount = ec;   // unmark on CAS failure
1729 >                        else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1730 >                            idleAwaitWork(w, nc, c);
1731                      }
1732 <                    else if (w.seed >= 0 && w.eventCount < 0) {
1732 >                    else if (w.eventCount < 0 && !tryTerminate(false, false) &&
1733 >                             ctl == c) {         // block
1734                          Thread wt = Thread.currentThread();
1735 <                        Thread.interrupted();  // clear status
1735 >                        Thread.interrupted();    // clear status
1736                          U.putObject(wt, PARKBLOCKER, this);
1737 <                        w.parker = wt;         // emulate LockSupport.park
1738 <                        if (w.eventCount < 0)  // recheck
1737 >                        w.parker = wt;           // emulate LockSupport.park
1738 >                        if (w.eventCount < 0)    // recheck
1739                              U.park(false, 0L);
1740                          w.parker = null;
1741                          U.putObject(wt, PARKBLOCKER, null);
1742                      }
1743 <                    break;
1743 >                }
1744 >                if ((h >= 0 || (h = w.hint) >= 0) &&
1745 >                    (ws = workQueues) != null && h < ws.length &&
1746 >                    (q = ws[h]) != null) {      // signal others before retry
1747 >                    WorkQueue v; Thread p; int u, i, s;
1748 >                    for (int n = (config & SMASK) >>> 1;;) {
1749 >                        int idleCount = (w.eventCount < 0) ? 0 : -1;
1750 >                        if (((s = idleCount - q.base + q.top) <= n &&
1751 >                             (n = s) <= 0) ||
1752 >                            (u = (int)((c = ctl) >>> 32)) >= 0 ||
1753 >                            (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1754 >                            (v = ws[i]) == null)
1755 >                            break;
1756 >                        long nc = (((long)(v.nextWait & E_MASK)) |
1757 >                                   ((long)(u + UAC_UNIT) << 32));
1758 >                        if (v.eventCount != (e | INT_SIGN) ||
1759 >                            !U.compareAndSwapLong(this, CTL, c, nc))
1760 >                            break;
1761 >                        v.hint = h;
1762 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1763 >                        if ((p = v.parker) != null)
1764 >                            U.unpark(p);
1765 >                        if (--n <= 0)
1766 >                            break;
1767 >                    }
1768                  }
1769              }
1770          }
# Line 1710 | Line 1784 | public class ForkJoinPool extends Abstra
1784       * @param prevCtl the ctl value to restore if thread is terminated
1785       */
1786      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1787 <        if (w.eventCount < 0 &&
1788 <            (this == commonPool || !tryTerminate(false, false)) &&
1715 <            (int)prevCtl != 0) {
1787 >        if (w != null && w.eventCount < 0 &&
1788 >            !tryTerminate(false, false) && (int)prevCtl != 0) {
1789              int dc = -(short)(currentCtl >>> TC_SHIFT);
1790              long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1791 <            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1791 >            long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP;
1792              Thread wt = Thread.currentThread();
1793              while (ctl == currentCtl) {
1794                  Thread.interrupted();  // timed variant of version in scan()
# Line 1738 | Line 1811 | public class ForkJoinPool extends Abstra
1811      }
1812  
1813      /**
1814 <     * Scans through queues looking for work while joining a task;
1815 <     * if any are present, signals.
1814 >     * Scans through queues looking for work while joining a task; if
1815 >     * any present, signals. May return early if more signalling is
1816 >     * detectably unneeded.
1817       *
1818 <     * @param task to return early if done
1818 >     * @param task return early if done
1819       * @param origin an index to start scan
1820       */
1821 <    final int helpSignal(ForkJoinTask<?> task, int origin) {
1822 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1823 <        if (task != null && (ws = workQueues) != null &&
1824 <            (m = ws.length - 1) >= 0) {
1825 <            for (int i = 0; i <= m; ++i) {
1826 <                if ((s = task.status) < 0)
1827 <                    return s;
1828 <                if ((q = ws[(i + origin) & m]) != null &&
1829 <                    (n = q.queueSize()) > 0) {
1830 <                    signalWork(q, n);
1831 <                    if ((int)(ctl >> AC_SHIFT) >= 0)
1821 >    private void helpSignal(ForkJoinTask<?> task, int origin) {
1822 >        WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s;
1823 >        if (task != null && task.status >= 0 &&
1824 >            (u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1825 >            (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1826 >            outer: for (int k = origin, j = m; j >= 0; --j) {
1827 >                WorkQueue q = ws[k++ & m];
1828 >                for (int n = m;;) { // limit to at most m signals
1829 >                    if (task.status < 0)
1830 >                        break outer;
1831 >                    if (q == null ||
1832 >                        ((s = -q.base + q.top) <= n && (n = s) <= 0))
1833                          break;
1834 +                    if ((u = (int)((c = ctl) >>> 32)) >= 0 ||
1835 +                        (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1836 +                        (w = ws[i]) == null)
1837 +                        break outer;
1838 +                    long nc = (((long)(w.nextWait & E_MASK)) |
1839 +                               ((long)(u + UAC_UNIT) << 32));
1840 +                    if (w.eventCount != (e | INT_SIGN))
1841 +                        break outer;
1842 +                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1843 +                        w.eventCount = (e + E_SEQ) & E_MASK;
1844 +                        if ((p = w.parker) != null)
1845 +                            U.unpark(p);
1846 +                        if (--n <= 0)
1847 +                            break;
1848 +                    }
1849                  }
1850              }
1851          }
1762        return 0;
1852      }
1853  
1854      /**
# Line 1793 | Line 1882 | public class ForkJoinPool extends Abstra
1882                      }
1883                      if ((ws = workQueues) == null || (m = ws.length - 1) <= 0)
1884                          break restart;              // shutting down
1885 <                    if ((v = ws[h = (j.stealHint | 1) & m]) == null ||
1885 >                    if ((v = ws[h = (j.hint | 1) & m]) == null ||
1886                          v.currentSteal != subtask) {
1887                          for (int origin = h;;) {    // find stealer
1888                              if (((h = (h + 2) & m) & 15) == 1 &&
# Line 1801 | Line 1890 | public class ForkJoinPool extends Abstra
1890                                  continue restart;   // occasional staleness check
1891                              if ((v = ws[h]) != null &&
1892                                  v.currentSteal == subtask) {
1893 <                                j.stealHint = h;    // save hint
1893 >                                j.hint = h;        // save hint
1894                                  break;
1895                              }
1896                              if (h == origin)
# Line 1850 | Line 1939 | public class ForkJoinPool extends Abstra
1939  
1940      /**
1941       * Analog of tryHelpStealer for CountedCompleters. Tries to steal
1942 <     * and run tasks within the target's computation
1942 >     * and run tasks within the target's computation.
1943       *
1944       * @param task the task to join
1945       * @param mode if shared, exit upon completing any task
# Line 1858 | Line 1947 | public class ForkJoinPool extends Abstra
1947       *
1948       */
1949      private int helpComplete(ForkJoinTask<?> task, int mode) {
1950 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1950 >        WorkQueue[] ws; WorkQueue q; int m, n, s, u;
1951          if (task != null && (ws = workQueues) != null &&
1952              (m = ws.length - 1) >= 0) {
1953              for (int j = 1, origin = j;;) {
# Line 1866 | Line 1955 | public class ForkJoinPool extends Abstra
1955                      return s;
1956                  if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) {
1957                      origin = j;
1958 <                    if (mode == SHARED_QUEUE && (int)(ctl >> AC_SHIFT) >= 0)
1958 >                    if (mode == SHARED_QUEUE &&
1959 >                        ((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0))
1960                          break;
1961                  }
1962                  else if ((j = (j + 2) & m) == origin)
# Line 1884 | Line 1974 | public class ForkJoinPool extends Abstra
1974       * may become starved.
1975       */
1976      final boolean tryCompensate() {
1977 <        int pc = parallelism, e, u, i, tc; long c;
1977 >        int pc = config & SMASK, e, i, tc; long c;
1978          WorkQueue[] ws; WorkQueue w; Thread p;
1979 <        if ((e = (int)(c = ctl)) >= 0 && (ws = workQueues) != null) {
1979 >        if ((ws = workQueues) != null && (e = (int)(c = ctl)) >= 0) {
1980              if (e != 0 && (i = e & SMASK) < ws.length &&
1981                  (w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) {
1982                  long nc = ((long)(w.nextWait & E_MASK) |
# Line 1898 | Line 1988 | public class ForkJoinPool extends Abstra
1988                      return true;   // replace with idle worker
1989                  }
1990              }
1991 <            else if ((short)((u = (int)(c >>> 32)) >>> UTC_SHIFT) >= 0 &&
1992 <                     (u >> UAC_SHIFT) + pc > 1) {
1991 >            else if ((tc = (short)(c >>> TC_SHIFT)) >= 0 &&
1992 >                     (int)(c >> AC_SHIFT) + pc > 1) {
1993                  long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK);
1994                  if (U.compareAndSwapLong(this, CTL, c, nc))
1995 <                    return true;    // no compensation
1995 >                    return true;   // no compensation
1996              }
1997 <            else if ((tc = u + pc) < MAX_CAP) {
1997 >            else if (tc + pc < MAX_CAP) {
1998                  long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
1999                  if (U.compareAndSwapLong(this, CTL, c, nc)) {
2000 +                    ForkJoinWorkerThreadFactory fac;
2001                      Throwable ex = null;
2002                      ForkJoinWorkerThread wt = null;
2003                      try {
1913                        ForkJoinWorkerThreadFactory fac;
2004                          if ((fac = factory) != null &&
2005                              (wt = fac.newThread(this)) != null) {
2006                              wt.start();
# Line 1919 | Line 2009 | public class ForkJoinPool extends Abstra
2009                      } catch (Throwable rex) {
2010                          ex = rex;
2011                      }
2012 <                    deregisterWorker(wt, ex); // adjust counts etc
2012 >                    deregisterWorker(wt, ex); // clean up and return false
2013                  }
2014              }
2015          }
# Line 1938 | Line 2028 | public class ForkJoinPool extends Abstra
2028          if (joiner != null && task != null && (s = task.status) >= 0) {
2029              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2030              joiner.currentJoin = task;
2031 <            do {} while ((s = task.status) >= 0 &&
1942 <                         joiner.queueSize() > 0 &&
2031 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2032                           joiner.tryRemoveAndExec(task)); // process local tasks
2033 <            if (s >= 0 && (s = task.status) >= 0 &&
2034 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2035 <                (task instanceof CountedCompleter))
2036 <                s = helpComplete(task, LIFO_QUEUE);
2033 >            if (s >= 0 && (s = task.status) >= 0) {
2034 >                helpSignal(task, joiner.poolIndex);
2035 >                if ((s = task.status) >= 0 &&
2036 >                    (task instanceof CountedCompleter))
2037 >                    s = helpComplete(task, LIFO_QUEUE);
2038 >            }
2039              while (s >= 0 && (s = task.status) >= 0) {
2040 <                if ((joiner.queueSize() > 0 ||           // try helping
2040 >                if ((!joiner.isEmpty() ||           // try helping
2041                       (s = tryHelpStealer(joiner, task)) == 0) &&
2042 <                    (s = task.status) >= 0 && tryCompensate()) {
2043 <                    if (task.trySetSignal() && (s = task.status) >= 0) {
2044 <                        synchronized (task) {
2045 <                            if (task.status >= 0) {
2046 <                                try {                // see ForkJoinTask
2047 <                                    task.wait();     //  for explanation
2048 <                                } catch (InterruptedException ie) {
2042 >                    (s = task.status) >= 0) {
2043 >                    helpSignal(task, joiner.poolIndex);
2044 >                    if ((s = task.status) >= 0 && tryCompensate()) {
2045 >                        if (task.trySetSignal() && (s = task.status) >= 0) {
2046 >                            synchronized (task) {
2047 >                                if (task.status >= 0) {
2048 >                                    try {                // see ForkJoinTask
2049 >                                        task.wait();     //  for explanation
2050 >                                    } catch (InterruptedException ie) {
2051 >                                    }
2052                                  }
2053 +                                else
2054 +                                    task.notifyAll();
2055                              }
1960                            else
1961                                task.notifyAll();
2056                          }
2057 +                        long c;                          // re-activate
2058 +                        do {} while (!U.compareAndSwapLong
2059 +                                     (this, CTL, c = ctl, c + AC_UNIT));
2060                      }
1964                    long c;                          // re-activate
1965                    do {} while (!U.compareAndSwapLong
1966                                 (this, CTL, c = ctl, c + AC_UNIT));
2061                  }
2062              }
2063              joiner.currentJoin = prevJoin;
# Line 1984 | Line 2078 | public class ForkJoinPool extends Abstra
2078          if (joiner != null && task != null && (s = task.status) >= 0) {
2079              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2080              joiner.currentJoin = task;
2081 <            do {} while ((s = task.status) >= 0 &&
1988 <                         joiner.queueSize() > 0 &&
2081 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2082                           joiner.tryRemoveAndExec(task));
2083 <            if (s >= 0 && (s = task.status) >= 0 &&
2084 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2085 <                (task instanceof CountedCompleter))
2086 <                s = helpComplete(task, LIFO_QUEUE);
2087 <            if (s >= 0 && joiner.queueSize() == 0) {
2083 >            if (s >= 0 && (s = task.status) >= 0) {
2084 >                helpSignal(task, joiner.poolIndex);
2085 >                if ((s = task.status) >= 0 &&
2086 >                    (task instanceof CountedCompleter))
2087 >                    s = helpComplete(task, LIFO_QUEUE);
2088 >            }
2089 >            if (s >= 0 && joiner.isEmpty()) {
2090                  do {} while (task.status >= 0 &&
2091                               tryHelpStealer(joiner, task) > 0);
2092              }
# Line 2007 | Line 2102 | public class ForkJoinPool extends Abstra
2102       * @param r a (random) seed for scanning
2103       */
2104      private WorkQueue findNonEmptyStealQueue(int r) {
2010        int step = (r >>> 16) | 1;
2105          for (WorkQueue[] ws;;) {
2106 <            int ps = plock, m;
2106 >            int ps = plock, m, n;
2107              if ((ws = workQueues) == null || (m = ws.length - 1) < 1)
2108                  return null;
2109 <            for (int j = (m + 1) << 2; ; r += step) {
2110 <                WorkQueue q = ws[((r << 1) | 1) & m];
2111 <                if (q != null && q.queueSize() > 0)
2109 >            for (int j = (m + 1) << 2; ;) {
2110 >                WorkQueue q = ws[(((r + j) << 1) | 1) & m];
2111 >                if (q != null && (n = q.base - q.top) < 0) {
2112 >                    if (n < -1)
2113 >                        signalWork(q);
2114                      return q;
2115 +                }
2116                  else if (--j < 0) {
2117                      if (plock == ps)
2118                          return null;
# Line 2058 | Line 2155 | public class ForkJoinPool extends Abstra
2155                  }
2156                  else
2157                      c = ctl;        // re-increment on exit
2158 <                if ((int)(c >> AC_SHIFT) + parallelism == 0) {
2158 >                if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) {
2159                      do {} while (!U.compareAndSwapLong
2160                                   (this, CTL, c = ctl, c + AC_UNIT));
2161                      break;
# Line 2133 | Line 2230 | public class ForkJoinPool extends Abstra
2230      static int getSurplusQueuedTaskCount() {
2231          Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q;
2232          if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) {
2233 <            int b = (q = (wt = (ForkJoinWorkerThread)t).workQueue).base;
2234 <            int p = (pool = wt.pool).parallelism;
2233 >            int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).config & SMASK;
2234 >            int n = (q = wt.workQueue).top - q.base;
2235              int a = (int)(pool.ctl >> AC_SHIFT) + p;
2236 <            return q.top - b - (a > (p >>>= 1) ? 0 :
2237 <                                a > (p >>>= 1) ? 1 :
2238 <                                a > (p >>>= 1) ? 2 :
2239 <                                a > (p >>>= 1) ? 4 :
2240 <                                8);
2236 >            return n - (a > (p >>>= 1) ? 0 :
2237 >                        a > (p >>>= 1) ? 1 :
2238 >                        a > (p >>>= 1) ? 2 :
2239 >                        a > (p >>>= 1) ? 4 :
2240 >                        8);
2241          }
2242          return 0;
2243      }
# Line 2166 | Line 2263 | public class ForkJoinPool extends Abstra
2263              return false;
2264          for (long c;;) {
2265              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2266 <                if ((short)(c >>> TC_SHIFT) == -parallelism) {
2266 >                if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) {
2267                      synchronized (this) {
2268                          notifyAll();                // signal when 0 workers
2269                      }
# Line 2180 | Line 2277 | public class ForkJoinPool extends Abstra
2277                  if (((ps = plock) & PL_LOCK) != 0 ||
2278                      !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
2279                      ps = acquirePlock();
2280 <                int nps = SHUTDOWN;
2281 <                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
2185 <                    releasePlock(nps);
2280 >                if (!U.compareAndSwapInt(this, PLOCK, ps, SHUTDOWN))
2281 >                    releasePlock(SHUTDOWN);
2282              }
2283              if (!now) {                             // check if idle & no tasks
2284 <                if ((int)(c >> AC_SHIFT) != -parallelism ||
2284 >                if ((int)(c >> AC_SHIFT) != -(config & SMASK) ||
2285                      hasQueuedSubmissions())
2286                      return false;
2287                  // Check for unqueued inactive workers. One pass suffices.
# Line 2201 | Line 2297 | public class ForkJoinPool extends Abstra
2297                  for (int pass = 0; pass < 3; ++pass) {
2298                      WorkQueue[] ws = workQueues;
2299                      if (ws != null) {
2300 <                        WorkQueue w;
2300 >                        WorkQueue w; Thread wt;
2301                          int n = ws.length;
2302                          for (int i = 0; i < n; ++i) {
2303                              if ((w = ws[i]) != null) {
2304                                  w.qlock = -1;
2305                                  if (pass > 0) {
2306                                      w.cancelAll();
2307 <                                    if (pass > 1)
2308 <                                        w.interruptOwner();
2307 >                                    if (pass > 1 && (wt = w.owner) != null) {
2308 >                                        if (!wt.isInterrupted()) {
2309 >                                            try {
2310 >                                                wt.interrupt();
2311 >                                            } catch (SecurityException ignore) {
2312 >                                            }
2313 >                                        }
2314 >                                        U.unpark(wt);
2315 >                                    }
2316                                  }
2317                              }
2318                          }
# Line 2255 | Line 2358 | public class ForkJoinPool extends Abstra
2358       */
2359      static boolean tryExternalUnpush(ForkJoinTask<?> t) {
2360          ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z;
2361 <        ForkJoinTask<?>[] a;  int m, s; long j;
2362 <        if ((z = submitters.get()) != null &&
2361 >        ForkJoinTask<?>[] a;  int m, s;
2362 >        if (t != null &&
2363 >            (z = submitters.get()) != null &&
2364              (p = commonPool) != null &&
2365              (ws = p.workQueues) != null &&
2366              (m = ws.length - 1) >= 0 &&
2367              (q = ws[m & z.seed & SQMASK]) != null &&
2368              (s = q.top) != q.base &&
2369 <            (a = q.array) != null &&
2370 <            U.getObjectVolatile
2371 <            (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2372 <            U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2373 <            if (q.array == a && q.top == s && // recheck
2374 <                U.compareAndSwapObject(a, j, t, null)) {
2375 <                q.top = s - 1;
2369 >            (a = q.array) != null) {
2370 >            long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
2371 >            if (U.getObject(a, j) == t &&
2372 >                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2373 >                if (q.array == a && q.top == s && // recheck
2374 >                    U.compareAndSwapObject(a, j, t, null)) {
2375 >                    q.top = s - 1;
2376 >                    q.qlock = 0;
2377 >                    return true;
2378 >                }
2379                  q.qlock = 0;
2273                return true;
2380              }
2275            q.qlock = 0;
2381          }
2382          return false;
2383      }
# Line 2287 | Line 2392 | public class ForkJoinPool extends Abstra
2392          if (q != null && (a = q.array) != null && (m = (a.length - 1)) >= 0 &&
2393              root != null && root.status >= 0) {
2394              for (;;) {
2395 <                int s; Object o; CountedCompleter<?> task = null;
2395 >                int s, u; Object o; CountedCompleter<?> task = null;
2396                  if ((s = q.top) - q.base > 0) {
2397                      long j = ((m & (s - 1)) << ASHIFT) + ABASE;
2398                      if ((o = U.getObject(a, j)) != null &&
# Line 2310 | Line 2415 | public class ForkJoinPool extends Abstra
2415                  }
2416                  if (task != null)
2417                      task.doExec();
2418 <                if (root.status < 0 || (int)(ctl >> AC_SHIFT) >= 0)
2418 >                if (root.status < 0 ||
2419 >                    (u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
2420                      break;
2421                  if (task == null) {
2422 <                    if (helpSignal(root, q.poolIndex) >= 0)
2422 >                    helpSignal(root, q.poolIndex);
2423 >                    if (root.status >= 0)
2424                          helpComplete(root, SHARED_QUEUE);
2425                      break;
2426                  }
# Line 2328 | Line 2435 | public class ForkJoinPool extends Abstra
2435      static void externalHelpJoin(ForkJoinTask<?> t) {
2436          // Some hard-to-avoid overlap with tryExternalUnpush
2437          ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z;
2438 <        ForkJoinTask<?>[] a;  int m, s, n; long j;
2439 <        if (t != null && t.status >= 0 &&
2438 >        ForkJoinTask<?>[] a;  int m, s, n;
2439 >        if (t != null &&
2440              (z = submitters.get()) != null &&
2441              (p = commonPool) != null &&
2442              (ws = p.workQueues) != null &&
2443              (m = ws.length - 1) >= 0 &&
2444              (q = ws[m & z.seed & SQMASK]) != null &&
2445              (a = q.array) != null) {
2446 <            if ((s = q.top) != q.base &&
2447 <                U.getObjectVolatile
2448 <                (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2449 <                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2450 <                if (q.array == a && q.top == s &&
2451 <                    U.compareAndSwapObject(a, j, t, null)) {
2452 <                    q.top = s - 1;
2453 <                    q.qlock = 0;
2454 <                    t.doExec();
2446 >            int am = a.length - 1;
2447 >            if ((s = q.top) != q.base) {
2448 >                long j = ((am & (s - 1)) << ASHIFT) + ABASE;
2449 >                if (U.getObject(a, j) == t &&
2450 >                    U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2451 >                    if (q.array == a && q.top == s &&
2452 >                        U.compareAndSwapObject(a, j, t, null)) {
2453 >                        q.top = s - 1;
2454 >                        q.qlock = 0;
2455 >                        t.doExec();
2456 >                    }
2457 >                    else
2458 >                        q.qlock = 0;
2459                  }
2349                else
2350                    q.qlock = 0;
2460              }
2461              if (t.status >= 0) {
2462                  if (t instanceof CountedCompleter)
# Line 2363 | Line 2472 | public class ForkJoinPool extends Abstra
2472       */
2473      static void externalHelpQuiescePool() {
2474          ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b;
2366        int r = ThreadLocalRandom.current().nextInt();
2475          if ((p = commonPool) != null &&
2476 <            (q = p.findNonEmptyStealQueue(r)) != null &&
2476 >            (q = p.findNonEmptyStealQueue(1)) != null &&
2477              (b = q.base) - q.top < 0 &&
2478              (t = q.pollAt(b)) != null)
2479              t.doExec();
# Line 2442 | Line 2550 | public class ForkJoinPool extends Abstra
2550              throw new NullPointerException();
2551          if (parallelism <= 0 || parallelism > MAX_CAP)
2552              throw new IllegalArgumentException();
2445        this.parallelism = parallelism;
2553          this.factory = factory;
2554          this.ueh = handler;
2555 <        this.localMode = asyncMode ? FIFO_QUEUE : LIFO_QUEUE;
2555 >        this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0);
2556          long np = (long)(-parallelism); // offset ctl counts
2557          this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2558          int pn = nextPoolId();
# Line 2462 | Line 2569 | public class ForkJoinPool extends Abstra
2569      ForkJoinPool(int parallelism, long ctl,
2570                   ForkJoinWorkerThreadFactory factory,
2571                   Thread.UncaughtExceptionHandler handler) {
2572 <        this.parallelism = parallelism;
2572 >        this.config = parallelism;
2573          this.ctl = ctl;
2574          this.factory = factory;
2575          this.ueh = handler;
2469        this.localMode = LIFO_QUEUE;
2576          this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2577      }
2578  
2579      /**
2580 <     * Returns the common pool instance.
2580 >     * Returns the common pool instance. This pool is statically
2581 >     * constructed; its run state is unaffected by attempts to
2582 >     * {@link @shutdown} or {@link #shutdownNow}.
2583       *
2584       * @return the common pool instance
2585       */
2586      public static ForkJoinPool commonPool() {
2587 <        return commonPool; // cannot be null (if so, a static init error)
2587 >        // assert commonPool != null : "static init error";
2588 >        return commonPool;
2589      }
2590  
2591      // Execution methods
# Line 2648 | Line 2757 | public class ForkJoinPool extends Abstra
2757       * @return the targeted parallelism level of this pool
2758       */
2759      public int getParallelism() {
2760 <        return parallelism;
2760 >        return config & SMASK;
2761      }
2762  
2763      /**
# Line 2669 | Line 2778 | public class ForkJoinPool extends Abstra
2778       * @return the number of worker threads
2779       */
2780      public int getPoolSize() {
2781 <        return parallelism + (short)(ctl >>> TC_SHIFT);
2781 >        return (config & SMASK) + (short)(ctl >>> TC_SHIFT);
2782      }
2783  
2784      /**
# Line 2679 | Line 2788 | public class ForkJoinPool extends Abstra
2788       * @return {@code true} if this pool uses async mode
2789       */
2790      public boolean getAsyncMode() {
2791 <        return localMode != 0;
2791 >        return (config >>> 16) == FIFO_QUEUE;
2792      }
2793  
2794      /**
# Line 2710 | Line 2819 | public class ForkJoinPool extends Abstra
2819       * @return the number of active threads
2820       */
2821      public int getActiveThreadCount() {
2822 <        int r = parallelism + (int)(ctl >> AC_SHIFT);
2822 >        int r = (config & SMASK) + (int)(ctl >> AC_SHIFT);
2823          return (r <= 0) ? 0 : r; // suppress momentarily negative values
2824      }
2825  
# Line 2726 | Line 2835 | public class ForkJoinPool extends Abstra
2835       * @return {@code true} if all threads are currently idle
2836       */
2837      public boolean isQuiescent() {
2838 <        return (int)(ctl >> AC_SHIFT) + parallelism == 0;
2838 >        return (int)(ctl >> AC_SHIFT) + (config & SMASK) == 0;
2839      }
2840  
2841      /**
# Line 2803 | Line 2912 | public class ForkJoinPool extends Abstra
2912          WorkQueue[] ws; WorkQueue w;
2913          if ((ws = workQueues) != null) {
2914              for (int i = 0; i < ws.length; i += 2) {
2915 <                if ((w = ws[i]) != null && w.queueSize() != 0)
2915 >                if ((w = ws[i]) != null && !w.isEmpty())
2916                      return true;
2917              }
2918          }
# Line 2889 | Line 2998 | public class ForkJoinPool extends Abstra
2998                  }
2999              }
3000          }
3001 <        int pc = parallelism;
3001 >        int pc = (config & SMASK);
3002          int tc = pc + (short)(c >>> TC_SHIFT);
3003          int ac = pc + (int)(c >> AC_SHIFT);
3004          if (ac < 0) // ignore transient negative
# Line 2962 | Line 3071 | public class ForkJoinPool extends Abstra
3071      public boolean isTerminated() {
3072          long c = ctl;
3073          return ((c & STOP_BIT) != 0L &&
3074 <                (short)(c >>> TC_SHIFT) == -parallelism);
3074 >                (short)(c >>> TC_SHIFT) == -(config & SMASK));
3075      }
3076  
3077      /**
# Line 2970 | Line 3079 | public class ForkJoinPool extends Abstra
3079       * commenced but not yet completed.  This method may be useful for
3080       * debugging. A return of {@code true} reported a sufficient
3081       * period after shutdown may indicate that submitted tasks have
3082 <     * ignored or suppressed interruption, or are waiting for IO,
3082 >     * ignored or suppressed interruption, or are waiting for I/O,
3083       * causing this executor not to properly terminate. (See the
3084       * advisory notes for class {@link ForkJoinTask} stating that
3085       * tasks should not normally entail blocking operations.  But if
# Line 2981 | Line 3090 | public class ForkJoinPool extends Abstra
3090      public boolean isTerminating() {
3091          long c = ctl;
3092          return ((c & STOP_BIT) != 0L &&
3093 <                (short)(c >>> TC_SHIFT) != -parallelism);
3093 >                (short)(c >>> TC_SHIFT) != -(config & SMASK));
3094      }
3095  
3096      /**
# Line 3125 | Line 3234 | public class ForkJoinPool extends Abstra
3234          if (t instanceof ForkJoinWorkerThread) {
3235              ForkJoinPool p = ((ForkJoinWorkerThread)t).pool;
3236              while (!blocker.isReleasable()) { // variant of helpSignal
3237 <                WorkQueue[] ws; WorkQueue q; int m, n;
3237 >                WorkQueue[] ws; WorkQueue q; int m, u;
3238                  if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) {
3239                      for (int i = 0; i <= m; ++i) {
3240                          if (blocker.isReleasable())
3241                              return;
3242 <                        if ((q = ws[i]) != null && (n = q.queueSize()) > 0) {
3243 <                            p.signalWork(q, n);
3244 <                            if ((int)(p.ctl >> AC_SHIFT) >= 0)
3242 >                        if ((q = ws[i]) != null && q.base - q.top < 0) {
3243 >                            p.signalWork(q);
3244 >                            if ((u = (int)(p.ctl >>> 32)) >= 0 ||
3245 >                                (u >> UAC_SHIFT) >= 0)
3246                                  break;
3247                          }
3248                      }
# Line 3178 | Line 3288 | public class ForkJoinPool extends Abstra
3288      private static final long QLOCK;
3289  
3290      static {
3181        // Establish common pool parameters
3182        // TBD: limit or report ignored exceptions?
3183
3184        int par = 0;
3185        ForkJoinWorkerThreadFactory fac = null;
3186        Thread.UncaughtExceptionHandler handler = null;
3187        try {
3188            String pp = System.getProperty(propPrefix + "parallelism");
3189            String hp = System.getProperty(propPrefix + "exceptionHandler");
3190            String fp = System.getProperty(propPrefix + "threadFactory");
3191            if (fp != null)
3192                fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
3193                       getSystemClassLoader().loadClass(fp).newInstance());
3194            if (hp != null)
3195                handler = ((Thread.UncaughtExceptionHandler)ClassLoader.
3196                           getSystemClassLoader().loadClass(hp).newInstance());
3197            if (pp != null)
3198                par = Integer.parseInt(pp);
3199        } catch (Exception ignore) {
3200        }
3201
3291          int s; // initialize field offsets for CAS etc
3292          try {
3293              U = getUnsafe();
# Line 3227 | Line 3316 | public class ForkJoinPool extends Abstra
3316          if ((s & (s-1)) != 0)
3317              throw new Error("data type scale not a power of two");
3318  
3319 +        submitters = new ThreadLocal<Submitter>();
3320 +        ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory =
3321 +            new DefaultForkJoinWorkerThreadFactory();
3322 +        modifyThreadPermission = new RuntimePermission("modifyThread");
3323 +
3324          /*
3325 <         * For extra caution, computations to set up pool state are
3326 <         * here; the constructor just assigns these values to fields.
3325 >         * Establish common pool parameters.  For extra caution,
3326 >         * computations to set up common pool state are here; the
3327 >         * constructor just assigns these values to fields.
3328           */
3329 <        ForkJoinWorkerThreadFactory defaultFac =
3330 <            defaultForkJoinWorkerThreadFactory =
3331 <            new DefaultForkJoinWorkerThreadFactory();
3332 <        if (fac == null)
3333 <            fac = defaultFac;
3329 >
3330 >        int par = 0;
3331 >        Thread.UncaughtExceptionHandler handler = null;
3332 >        try {  // TBD: limit or report ignored exceptions?
3333 >            String pp = System.getProperty
3334 >                ("java.util.concurrent.ForkJoinPool.common.parallelism");
3335 >            String hp = System.getProperty
3336 >                ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
3337 >            String fp = System.getProperty
3338 >                ("java.util.concurrent.ForkJoinPool.common.threadFactory");
3339 >            if (fp != null)
3340 >                fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
3341 >                       getSystemClassLoader().loadClass(fp).newInstance());
3342 >            if (hp != null)
3343 >                handler = ((Thread.UncaughtExceptionHandler)ClassLoader.
3344 >                           getSystemClassLoader().loadClass(hp).newInstance());
3345 >            if (pp != null)
3346 >                par = Integer.parseInt(pp);
3347 >        } catch (Exception ignore) {
3348 >        }
3349 >
3350          if (par <= 0)
3351              par = Runtime.getRuntime().availableProcessors();
3352          if (par > MAX_CAP)
3353              par = MAX_CAP;
3354 +        commonPoolParallelism = par;
3355          long np = (long)(-par); // precompute initial ctl value
3356          long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
3357  
3246        commonPoolParallelism = par;
3358          commonPool = new ForkJoinPool(par, ct, fac, handler);
3248        modifyThreadPermission = new RuntimePermission("modifyThread");
3249        submitters = new ThreadLocal<Submitter>();
3359      }
3360  
3361      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines