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.12 by dl, Wed Nov 14 17:20:29 2012 UTC vs.
Revision 1.29 by jsr166, Fri Dec 14 16:33:42 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 449 | Line 451 | public class ForkJoinPool extends Abstra
451       * perform some subtask processing (see externalHelpJoin and
452       * related methods).  We do not need to record whether these
453       * submissions are to the common pool -- if not, externalHelpJoin
454 <     * returns quicky (at the most helping to signal some common pool
454 >     * returns quickly (at the most helping to signal some common pool
455       * workers). These submitters would otherwise be blocked waiting
456       * for completion, so the extra effort (with liberally sprinkled
457       * task status checks) in inapplicable cases amounts to an odd
# 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 relyaed 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
984 <                    ForkJoinPool p;
985 <                    if ((p = pool) != null)
986 <                        p.collectStealCount(this);
987 <                }
988 <                if (top != base) {       // process remaining local tasks
990 >                if (base - top < 0) {       // process remaining local tasks
991                      if (mode == 0)
992                          popAndExecAll();
993                      else
994                          pollAndExecAll();
995                  }
996 +                ++nsteals;
997 +                hint = -1;
998              }
999          }
1000  
# Line 1057 | Line 1061 | public class ForkJoinPool extends Abstra
1061          }
1062      }
1063  
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
1064      // static fields (initialized in static initializer below)
1065  
1066      /**
# Line 1091 | Line 1071 | public class ForkJoinPool extends Abstra
1071          defaultForkJoinWorkerThreadFactory;
1072  
1073      /**
1074 <     * Common (static) pool. Non-null for public use unless a static
1075 <     * construction exception, but internal usages null-check on use
1076 <     * to paranoically avoid potential initialization circularities
1077 <     * as well as to simplify generated code.
1074 >     * Per-thread submission bookkeeping. Shared across all pools
1075 >     * to reduce ThreadLocal pollution and because random motion
1076 >     * to avoid contention in one pool is likely to hold for others.
1077 >     * Lazily initialized on first submission (but null-checked
1078 >     * in other contexts to avoid unnecessary initialization).
1079       */
1080 <    static final ForkJoinPool commonPool;
1080 >    static final ThreadLocal<Submitter> submitters;
1081  
1082      /**
1083       * Permission required for callers of methods that may start or
# Line 1105 | Line 1086 | public class ForkJoinPool extends Abstra
1086      private static final RuntimePermission modifyThreadPermission;
1087  
1088      /**
1089 <     * Per-thread submission bookkeeping. Shared across all pools
1090 <     * to reduce ThreadLocal pollution and because random motion
1091 <     * to avoid contention in one pool is likely to hold for others.
1092 <     * Lazily initialized on first submission (but null-checked
1112 <     * in other contexts to avoid unnecessary initialization).
1089 >     * Common (static) pool. Non-null for public use unless a static
1090 >     * construction exception, but internal usages null-check on use
1091 >     * to paranoically avoid potential initialization circularities
1092 >     * as well as to simplify generated code.
1093       */
1094 <    static final ThreadLocal<Submitter> submitters;
1094 >    static final ForkJoinPool commonPool;
1095  
1096      /**
1097       * Common pool parallelism. Must equal commonPool.parallelism.
# Line 1149 | Line 1129 | public class ForkJoinPool extends Abstra
1129      private static final long FAST_IDLE_TIMEOUT =  200L * 1000L * 1000L;
1130  
1131      /**
1132 +     * Tolerance for idle timeouts, to cope with timer undershoots
1133 +     */
1134 +    private static final long TIMEOUT_SLOP = 2000000L; // 20ms
1135 +
1136 +    /**
1137       * The maximum stolen->joining link depth allowed in method
1138       * tryHelpStealer.  Must be a power of two.  Depths for legitimate
1139       * chains are unbounded, but we use a fixed constant to avoid
# Line 1248 | Line 1233 | public class ForkJoinPool extends Abstra
1233      static final int FIFO_QUEUE          =  1;
1234      static final int SHARED_QUEUE        = -1;
1235  
1236 +    // bounds for #steps in scan loop -- must be power 2 minus 1
1237 +    private static final int MIN_SCAN    = 0x1ff;   // cover estimation slop
1238 +    private static final int MAX_SCAN    = 0x1ffff; // 4 * max workers
1239 +
1240      // Instance fields
1241  
1242      /*
1243 <     * Field layout order in this class tends to matter more than one
1244 <     * would like. Runtime layout order is only loosely related to
1243 >     * Field layout of this class tends to matter more than one would
1244 >     * like. Runtime layout order is only loosely related to
1245       * declaration order and may differ across JVMs, but the following
1246       * empirically works OK on current JVMs.
1247       */
1248 +
1249 +    // Heuristic padding to ameliorate unfortunate memory placements
1250 +    volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06;
1251 +
1252      volatile long stealCount;                  // collects worker counts
1253      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
1254      volatile int plock;                        // shutdown status and seqLock
1255 +    volatile int indexSeed;                    // worker/submitter index seed
1256 +    final int config;                          // mode and parallelism level
1257      WorkQueue[] workQueues;                    // main registry
1258 <    final ForkJoinWorkerThreadFactory factory; // factory for new workers
1258 >    final ForkJoinWorkerThreadFactory factory;
1259      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1260      final String workerNamePrefix;             // to create worker name string
1261  
1262 +    volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17;
1263 +    volatile Object pad18, pad19, pad1a, pad1b;
1264 +
1265      /*
1266       * Acquires the plock lock to protect worker array and related
1267       * updates. This method is called only if an initial CAS on plock
# Line 1283 | Line 1278 | public class ForkJoinPool extends Abstra
1278              if (((ps = plock) & PL_LOCK) == 0 &&
1279                  U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK))
1280                  return nps;
1281 <            else if (r == 0)
1282 <                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1281 >            else if (r == 0) { // randomize spins if possible
1282 >                Thread t = Thread.currentThread(); WorkQueue w; Submitter z;
1283 >                if ((t instanceof ForkJoinWorkerThread) &&
1284 >                    (w = ((ForkJoinWorkerThread)t).workQueue) != null)
1285 >                    r = w.seed;
1286 >                else if ((z = submitters.get()) != null)
1287 >                    r = z.seed;
1288 >                else
1289 >                    r = 1;
1290 >            }
1291              else if (spins >= 0) {
1292                  r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1293                  if (r >= 0)
1294                      --spins;
1295              }
1296              else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) {
1297 <                synchronized(this) {
1297 >                synchronized (this) {
1298                      if ((plock & PL_SIGNAL) != 0) {
1299                          try {
1300                              wait();
# Line 1315 | Line 1318 | public class ForkJoinPool extends Abstra
1318       */
1319      private void releasePlock(int ps) {
1320          plock = ps;
1321 <        synchronized(this) { notifyAll(); }
1321 >        synchronized (this) { notifyAll(); }
1322 >    }
1323 >
1324 >    /**
1325 >     * Performs secondary initialization, called when plock is zero.
1326 >     * Creates workQueue array and sets plock to a valid value.  The
1327 >     * lock body must be exception-free (so no try/finally) so we
1328 >     * optimistically allocate new array outside the lock and throw
1329 >     * away if (very rarely) not needed. (A similar tactic is used in
1330 >     * fullExternalPush.)  Because the plock seq value can eventually
1331 >     * wrap around zero, this method harmlessly fails to reinitialize
1332 >     * if workQueues exists, while still advancing plock.
1333 >     *
1334 >     * Additionally tries to create the first worker.
1335 >     */
1336 >    private void initWorkers() {
1337 >        WorkQueue[] ws, nws; int ps;
1338 >        int p = config & SMASK;        // find power of two table size
1339 >        int n = (p > 1) ? p - 1 : 1;   // ensure at least 2 slots
1340 >        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1341 >        n = (n + 1) << 1;
1342 >        if ((ws = workQueues) == null || ws.length == 0)
1343 >            nws = new WorkQueue[n];
1344 >        else
1345 >            nws = null;
1346 >        if (((ps = plock) & PL_LOCK) != 0 ||
1347 >            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1348 >            ps = acquirePlock();
1349 >        if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1350 >            workQueues = nws;
1351 >        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1352 >        if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1353 >            releasePlock(nps);
1354 >        tryAddWorker();
1355 >    }
1356 >
1357 >    /**
1358 >     * Tries to create and start one worker if fewer than target
1359 >     * parallelism level exist. Adjusts counts etc on failure.
1360 >     */
1361 >    private void tryAddWorker() {
1362 >        long c; int u;
1363 >        while ((u = (int)((c = ctl) >>> 32)) < 0 &&
1364 >               (u & SHORT_SIGN) != 0 && (int)c == 0) {
1365 >            long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1366 >                             ((u + UAC_UNIT) & UAC_MASK)) << 32;
1367 >            if (U.compareAndSwapLong(this, CTL, c, nc)) {
1368 >                ForkJoinWorkerThreadFactory fac;
1369 >                Throwable ex = null;
1370 >                ForkJoinWorkerThread wt = null;
1371 >                try {
1372 >                    if ((fac = factory) != null &&
1373 >                        (wt = fac.newThread(this)) != null) {
1374 >                        wt.start();
1375 >                        break;
1376 >                    }
1377 >                } catch (Throwable e) {
1378 >                    ex = e;
1379 >                }
1380 >                deregisterWorker(wt, ex);
1381 >                break;
1382 >            }
1383 >        }
1384      }
1385  
1386      //  Registering and deregistering workers
1387  
1388      /**
1389 <     * Callback from ForkJoinWorkerThread constructor to establish its
1390 <     * poolIndex and record its WorkQueue. To avoid scanning bias due
1391 <     * to packing entries in front of the workQueues array, we treat
1392 <     * the array as a simple power-of-two hash table using per-thread
1393 <     * seed as hash, expanding as needed.
1394 <     *
1395 <     * @param w the worker's queue
1396 <     */
1397 <    final void registerWorker(WorkQueue w) {
1398 <        int s, ps; // generate a rarely colliding candidate index seed
1399 <        do {} while (!U.compareAndSwapInt(this, INDEXSEED,
1400 <                                          s = indexSeed, s += SEED_INCREMENT) ||
1389 >     * Callback from ForkJoinWorkerThread to establish and record its
1390 >     * WorkQueue. To avoid scanning bias due to packing entries in
1391 >     * front of the workQueues array, we treat the array as a simple
1392 >     * power-of-two hash table using per-thread seed as hash,
1393 >     * expanding as needed.
1394 >     *
1395 >     * @param wt the worker thread
1396 >     * @return the worker's queue
1397 >     */
1398 >    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
1399 >        Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps;
1400 >        wt.setDaemon(true);
1401 >        if ((handler = ueh) != null)
1402 >            wt.setUncaughtExceptionHandler(handler);
1403 >        do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
1404 >                                          s += SEED_INCREMENT) ||
1405                       s == 0); // skip 0
1406 +        WorkQueue w = new WorkQueue(this, wt, config >>> 16, s);
1407          if (((ps = plock) & PL_LOCK) != 0 ||
1408              !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1409              ps = acquirePlock();
1410          int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1411          try {
1412 <            WorkQueue[] ws;
1343 <            if (w != null && (ws = workQueues) != null) {
1344 <                w.seed = s;
1412 >            if ((ws = workQueues) != null) {    // skip if shutting down
1413                  int n = ws.length, m = n - 1;
1414 <                int r = (s << 1) | 1;               // use odd-numbered indices
1415 <                if (ws[r &= m] != null) {           // collision
1416 <                    int probes = 0;                 // step by approx half size
1414 >                int r = (s << 1) | 1;           // use odd-numbered indices
1415 >                if (ws[r &= m] != null) {       // collision
1416 >                    int probes = 0;             // step by approx half size
1417                      int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
1418                      while (ws[r = (r + step) & m] != null) {
1419                          if (++probes >= n) {
# Line 1355 | Line 1423 | public class ForkJoinPool extends Abstra
1423                          }
1424                      }
1425                  }
1426 <                w.eventCount = w.poolIndex = r;     // establish before recording
1426 >                w.eventCount = w.poolIndex = r; // volatile write orders
1427                  ws[r] = w;
1428              }
1429          } finally {
1430              if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1431                  releasePlock(nps);
1432          }
1433 +        wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex)));
1434 +        return w;
1435      }
1436  
1437      /**
# Line 1377 | Line 1447 | public class ForkJoinPool extends Abstra
1447          WorkQueue w = null;
1448          if (wt != null && (w = wt.workQueue) != null) {
1449              int ps;
1380            collectStealCount(w);
1450              w.qlock = -1;                // ensure set
1451 +            long ns = w.nsteals, sc;     // collect steal count
1452 +            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1453 +                                               sc = stealCount, sc + ns));
1454              if (((ps = plock) & PL_LOCK) != 0 ||
1455                  !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1456                  ps = acquirePlock();
# Line 1400 | Line 1472 | public class ForkJoinPool extends Abstra
1472                                             ((c - TC_UNIT) & TC_MASK) |
1473                                             (c & ~(AC_MASK|TC_MASK)))));
1474  
1475 <        if (!tryTerminate(false, false) && w != null) {
1475 >        if (!tryTerminate(false, false) && w != null && w.array != null) {
1476              w.cancelAll();                  // cancel remaining tasks
1477 <            if (w.array != null)            // suppress signal if never ran
1478 <                signalWork(null, 1);        // wake up or create replacement
1479 <            if (ex == null)                 // help clean refs on way out
1480 <                ForkJoinTask.helpExpungeStaleExceptions();
1477 >            int e, u, i, n; WorkQueue[] ws; WorkQueue v; Thread p;
1478 >            while ((u = (int)((c = ctl) >>> 32)) < 0) {
1479 >                if ((e = (int)c) > 0) {     // activate or create replacement
1480 >                    if ((ws = workQueues) != null &&
1481 >                        ws.length > (i = e & SMASK) &&
1482 >                        (v = ws[i]) != null && v.eventCount == (e | INT_SIGN)) {
1483 >                        long nc = (((long)(v.nextWait & E_MASK)) |
1484 >                                   ((long)(u + UAC_UNIT) << 32));
1485 >                        if (U.compareAndSwapLong(this, CTL, c, nc)) {
1486 >                            v.eventCount = (e + E_SEQ) & E_MASK;
1487 >                            if ((p = v.parker) != null)
1488 >                                U.unpark(p);
1489 >                            break;
1490 >                        }
1491 >                    }
1492 >                    else
1493 >                        break;
1494 >                }
1495 >                else {
1496 >                    if ((short)u < 0)
1497 >                        tryAddWorker();
1498 >                    break;
1499 >                }
1500 >            }
1501          }
1502 <
1503 <        if (ex != null)                     // rethrow
1502 >        if (ex == null)                     // help clean refs on way out
1503 >            ForkJoinTask.helpExpungeStaleExceptions();
1504 >        else                                // rethrow
1505              ForkJoinTask.rethrow(ex);
1506      }
1507  
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
1508      // Submissions
1509  
1510      /**
# Line 1445 | Line 1521 | public class ForkJoinPool extends Abstra
1521              (ws = workQueues) != null && (m = (ws.length - 1)) >= 0 &&
1522              (q = ws[m & z.seed & SQMASK]) != null &&
1523              U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock
1524 <            int s = q.top, n;
1525 <            if ((a = q.array) != null && a.length > (n = s + 1 - q.base)) {
1526 <                U.putObject(a, (long)(((a.length - 1) & s) << ASHIFT) + ABASE,
1527 <                            task);
1524 >            int b = q.base, s = q.top, n, an;
1525 >            if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) {
1526 >                int j = (((an - 1) & s) << ASHIFT) + ABASE;
1527 >                U.putOrderedObject(a, j, task);
1528                  q.top = s + 1;                     // push on to deque
1529                  q.qlock = 0;
1530 <                if (n <= 1)
1531 <                    signalWork(q, 1);
1530 >                if (n <= 2)
1531 >                    signalWork(q);
1532                  return;
1533              }
1534              q.qlock = 0;
# Line 1463 | Line 1539 | public class ForkJoinPool extends Abstra
1539      /**
1540       * Full version of externalPush. This method is called, among
1541       * other times, upon the first submission of the first task to the
1542 <     * pool, so must perform secondary initialization: creating
1543 <     * workQueue array and setting plock to a valid value. It also
1544 <     * detects first submission by an external thread by looking up
1545 <     * its ThreadLocal, and creates a new shared queue if the one at
1546 <     * index if empty or contended. The lock bodies must be
1547 <     * exception-free (so no try/finally) so we optimistically
1548 <     * 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.
1542 >     * pool, so must perform secondary initialization (via
1543 >     * initWorkers). It also detects first submission by an external
1544 >     * thread by looking up its ThreadLocal, and creates a new shared
1545 >     * queue if the one at index if empty or contended. The plock lock
1546 >     * body must be exception-free (so no try/finally) so we
1547 >     * optimistically allocate new queues outside the lock and throw
1548 >     * them away if (very rarely) not needed.
1549       */
1550      private void fullExternalPush(ForkJoinTask<?> task) {
1551 <        for (Submitter z = null;;) {
1552 <            WorkQueue[] ws; WorkQueue q; int ps, m, r, s;
1553 <            if ((ps = plock) < 0)
1551 >        int r = 0; // random index seed
1552 >        for (Submitter z = submitters.get();;) {
1553 >            WorkQueue[] ws; WorkQueue q; int ps, m, k;
1554 >            if (z == null) {
1555 >                if (U.compareAndSwapInt(this, INDEXSEED, r = indexSeed,
1556 >                                        r += SEED_INCREMENT) && r != 0)
1557 >                    submitters.set(z = new Submitter(r));
1558 >            }
1559 >            else if (r == 0) {               // move to a different index
1560 >                r = z.seed;
1561 >                r ^= r << 13;                // same xorshift as WorkQueues
1562 >                r ^= r >>> 17;
1563 >                z.seed = r ^ (r << 5);
1564 >            }
1565 >            else if ((ps = plock) < 0)
1566                  throw new RejectedExecutionException();
1567 <            else if ((ws = workQueues) == null || (m = ws.length - 1) < 0) {
1568 <                int n = parallelism - 1; n |= n >>> 1; n |= n >>> 2;
1569 <                n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1570 <                WorkQueue[] nws = new WorkQueue[(n + 1) << 1]; // power of two
1571 <                if ((ps & PL_LOCK) != 0 ||
1567 >            else if (ps == 0 || (ws = workQueues) == null ||
1568 >                     (m = ws.length - 1) < 0)
1569 >                initWorkers();
1570 >            else if ((q = ws[k = r & m & SQMASK]) != null) {
1571 >                if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
1572 >                    ForkJoinTask<?>[] a = q.array;
1573 >                    int s = q.top;
1574 >                    boolean submitted = false;
1575 >                    try {                      // locked version of push
1576 >                        if ((a != null && a.length > s + 1 - q.base) ||
1577 >                            (a = q.growArray()) != null) {   // must presize
1578 >                            int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
1579 >                            U.putOrderedObject(a, j, task);
1580 >                            q.top = s + 1;
1581 >                            submitted = true;
1582 >                        }
1583 >                    } finally {
1584 >                        q.qlock = 0;  // unlock
1585 >                    }
1586 >                    if (submitted) {
1587 >                        signalWork(q);
1588 >                        return;
1589 >                    }
1590 >                }
1591 >                r = 0; // move on failure
1592 >            }
1593 >            else if (((ps = plock) & PL_LOCK) == 0) { // create new queue
1594 >                q = new WorkQueue(this, null, SHARED_QUEUE, r);
1595 >                if (((ps = plock) & PL_LOCK) != 0 ||
1596                      !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1597                      ps = acquirePlock();
1598 <                if ((ws = workQueues) == null)
1599 <                    workQueues = nws;
1598 >                if ((ws = workQueues) != null && k < ws.length && ws[k] == null)
1599 >                    ws[k] = q;
1600                  int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1601                  if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1602                      releasePlock(nps);
1603              }
1604 <            else if (z == null && (z = submitters.get()) == null) {
1605 <                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 <            }
1604 >            else
1605 >                r = 0; // try elsewhere while lock held
1606          }
1607      }
1608  
# Line 1535 | Line 1617 | public class ForkJoinPool extends Abstra
1617      }
1618  
1619      /**
1620 <     * 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.
1620 >     * Tries to create or activate a worker if too few are active.
1621       *
1622 <     * @param q if non-null, the queue holding tasks to be signalled
1544 <     * @param signals the target number of signals.
1622 >     * @param q the (non-null) queue holding tasks to be signalled
1623       */
1624 <    final void signalWork(WorkQueue q, int signals) {
1625 <        long c; int e, u, i; WorkQueue[] ws; WorkQueue w; Thread p;
1624 >    final void signalWork(WorkQueue q) {
1625 >        int hint = q.poolIndex;
1626 >        long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p;
1627          while ((u = (int)((c = ctl) >>> 32)) < 0) {
1628              if ((e = (int)c) > 0) {
1629                  if ((ws = workQueues) != null && ws.length > (i = e & SMASK) &&
# Line 1552 | Line 1631 | public class ForkJoinPool extends Abstra
1631                      long nc = (((long)(w.nextWait & E_MASK)) |
1632                                 ((long)(u + UAC_UNIT) << 32));
1633                      if (U.compareAndSwapLong(this, CTL, c, nc)) {
1634 +                        w.hint = hint;
1635                          w.eventCount = (e + E_SEQ) & E_MASK;
1636                          if ((p = w.parker) != null)
1637                              U.unpark(p);
1638 <                        if (--signals <= 0)
1559 <                            break;
1638 >                        break;
1639                      }
1640 <                    else
1562 <                        signals = 1;
1563 <                    if ((q != null && q.queueSize() == 0))
1640 >                    if (q.top - q.base <= 0)
1641                          break;
1642                  }
1643                  else
1644                      break;
1645              }
1646 <            else if (e == 0 && (u & SHORT_SIGN) != 0) {
1647 <                long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1648 <                                 ((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
1646 >            else {
1647 >                if ((short)u < 0)
1648 >                    tryAddWorker();
1649                  break;
1650 +            }
1651          }
1652      }
1653  
# Line 1599 | Line 1657 | public class ForkJoinPool extends Abstra
1657       * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1658       */
1659      final void runWorker(WorkQueue w) {
1660 <        // initialize queue array in this thread
1603 <        w.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY];
1660 >        w.growArray(); // allocate queue
1661          do { w.runTask(scan(w)); } while (w.qlock >= 0);
1662      }
1663  
# Line 1612 | Line 1669 | public class ForkJoinPool extends Abstra
1669       * contention, or state changes that indicate possible success on
1670       * re-invocation.
1671       *
1672 <     * The scan searches for tasks across a random permutation of
1673 <     * queues (starting at a random index and stepping by a random
1674 <     * relative prime, checking each at least once).  The scan
1675 <     * terminates upon either finding a non-empty queue, or completing
1676 <     * the sweep. If the worker is not inactivated, it takes and
1677 <     * returns a task from this queue. Otherwise, if not activated, it
1678 <     * signals workers (that may include itself) and returns so caller
1679 <     * can retry. Also returns for trtry if the worker array may have
1680 <     * changed during an empty scan.  On failure to find a task, we
1681 <     * take one of the following actions, after which the caller will
1682 <     * retry calling this method unless terminated.
1672 >     * The scan searches for tasks across queues (starting at a random
1673 >     * index, and relying on registerWorker to irregularly scatter
1674 >     * them within array to avoid bias), checking each at least twice.
1675 >     * The scan terminates upon either finding a non-empty queue, or
1676 >     * completing the sweep. If the worker is not inactivated, it
1677 >     * takes and returns a task from this queue. Otherwise, if not
1678 >     * activated, it signals workers (that may include itself) and
1679 >     * returns so caller can retry. Also returns for true if the
1680 >     * worker array may have changed during an empty scan.  On failure
1681 >     * to find a task, we take one of the following actions, after
1682 >     * which the caller will retry calling this method unless
1683 >     * terminated.
1684       *
1685       * * If pool is terminating, terminate the worker.
1686       *
# Line 1632 | Line 1690 | public class ForkJoinPool extends Abstra
1690       * termination and possibly shrink pool.
1691       *
1692       * * If already enqueued and none of the above apply, possibly
1693 <     * (with 1/2 probablility) park awaiting signal, else lingering to
1693 >     * (with 1/2 probability) park awaiting signal, else lingering to
1694       * help scan and signal.
1695       *
1696       * @param w the worker (via its WorkQueue)
1697       * @return a task or null if none found
1698       */
1699      private final ForkJoinTask<?> scan(WorkQueue w) {
1700 <        WorkQueue[] ws; WorkQueue q;           // first update random seed
1701 <        int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1702 <        int ps = plock, m;                     // volatile read order matters
1703 <        if ((ws = workQueues) != null && (m = ws.length - 1) > 0) {
1704 <            int ec = w.eventCount;             // ec is negative if inactive
1705 <            int step = (r >>> 16) | 1;         // relatively prime
1706 <            for (int j = (m + 1) << 2;  ; --j, r += step) {
1707 <                ForkJoinTask<?> t; ForkJoinTask<?>[] a; int b, n;
1708 <                if ((q = ws[r & m]) != null && (b = q.base) - q.top < 0 &&
1709 <                    (a = q.array) != null) {   // probably nonempty
1700 >        WorkQueue[] ws; int m;
1701 >        int ps = plock;                          // read plock before ws
1702 >        if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1703 >            int ec = w.eventCount;               // ec is negative if inactive
1704 >            int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1705 >            int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN;
1706 >            do {
1707 >                WorkQueue q; ForkJoinTask<?>[] a; int b;
1708 >                if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 &&
1709 >                    (a = q.array) != null) {     // probably nonempty
1710                      int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
1711 <                    t = (ForkJoinTask<?>)U.getObjectVolatile(a, i);
1711 >                    ForkJoinTask<?> t = (ForkJoinTask<?>)
1712 >                        U.getObjectVolatile(a, i);
1713                      if (q.base == b && ec >= 0 && t != null &&
1714                          U.compareAndSwapObject(a, i, t, null)) {
1715 <                        if ((n = q.top - (q.base = b + 1)) > 0)
1716 <                            signalWork(q, n);
1717 <                        return t;              // taken
1718 <                    }
1719 <                    if (j < m || (ec < 0 && (ec = w.eventCount) < 0)) {
1720 <                        if ((n = q.queueSize() - 1) > 0)
1721 <                            signalWork(q, n);
1722 <                        break;                 // let caller retry after signal
1723 <                    }
1724 <                }
1725 <                else if (j < 0) {              // end of scan
1726 <                    long c = ctl; int e;
1727 <                    if (plock != ps)           // incomplete sweep
1728 <                        break;
1729 <                    if ((e = (int)c) < 0)      // pool is terminating
1730 <                        w.qlock = -1;
1731 <                    else if (ec >= 0) {        // try to enqueue/inactivate
1732 <                        long nc = ((long)ec |
1733 <                                   ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1734 <                        w.nextWait = e;
1735 <                        w.eventCount = ec | INT_SIGN; // mark as inactive
1736 <                        if (ctl != c ||
1737 <                            !U.compareAndSwapLong(this, CTL, c, nc))
1738 <                            w.eventCount = ec; // unmark on CAS failure
1739 <                        else if ((int)(c >> AC_SHIFT) == 1 - parallelism)
1740 <                            idleAwaitWork(w, nc, c);  // quiescent
1741 <                    }
1742 <                    else if (w.seed >= 0 && w.eventCount < 0) {
1743 <                        Thread wt = Thread.currentThread();
1744 <                        Thread.interrupted();  // clear status
1745 <                        U.putObject(wt, PARKBLOCKER, this);
1746 <                        w.parker = wt;         // emulate LockSupport.park
1747 <                        if (w.eventCount < 0)  // recheck
1748 <                            U.park(false, 0L);
1749 <                        w.parker = null;
1750 <                        U.putObject(wt, PARKBLOCKER, null);
1751 <                    }
1752 <                    break;
1715 >                        if ((q.base = b + 1) - q.top < 0)
1716 >                            signalWork(q);
1717 >                        return t;                // taken
1718 >                    }
1719 >                    else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) {
1720 >                        w.hint = (r + j) & m;    // help signal below
1721 >                        break;                   // cannot take
1722 >                    }
1723 >                }
1724 >            } while (--j >= 0);
1725 >
1726 >            long c, sc; int e, ns, h;
1727 >            if ((h = w.hint) < 0) {
1728 >                if ((ns = w.nsteals) != 0) {
1729 >                    if (U.compareAndSwapLong(this, STEALCOUNT,
1730 >                                             sc = stealCount, sc + ns))
1731 >                        w.nsteals = 0;           // collect steals
1732 >                }
1733 >                else if (plock != ps)            // consistency check
1734 >                    ;                            // skip
1735 >                else if ((e = (int)(c = ctl)) < 0)
1736 >                    w.qlock = -1;                // pool is terminating
1737 >                else if (ec >= 0) {              // try to enqueue/inactivate
1738 >                    long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1739 >                    w.nextWait = e;              // link and mark inactive
1740 >                    w.eventCount = ec | INT_SIGN;
1741 >                    if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1742 >                        w.eventCount = ec;       // unmark on CAS failure
1743 >                    else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1744 >                        idleAwaitWork(w, nc, c);
1745 >                }
1746 >                else if (w.eventCount < 0) {     // block
1747 >                    Thread wt = Thread.currentThread();
1748 >                    Thread.interrupted();        // clear status
1749 >                    U.putObject(wt, PARKBLOCKER, this);
1750 >                    w.parker = wt;               // emulate LockSupport.park
1751 >                    if (w.eventCount < 0)        // recheck
1752 >                        U.park(false, 0L);
1753 >                    w.parker = null;
1754 >                    U.putObject(wt, PARKBLOCKER, null);
1755                  }
1756              }
1757 +            if (h >= 0 || w.hint >= 0)           // signal others before retry
1758 +                helpSignalHint(w);
1759          }
1760          return null;
1761      }
# Line 1710 | Line 1773 | public class ForkJoinPool extends Abstra
1773       * @param prevCtl the ctl value to restore if thread is terminated
1774       */
1775      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1776 <        if (w.eventCount < 0 &&
1777 <            (this == commonPool || !tryTerminate(false, false)) &&
1715 <            (int)prevCtl != 0) {
1776 >        if (w != null && w.eventCount < 0 &&
1777 >            !tryTerminate(false, false) && (int)prevCtl != 0) {
1778              int dc = -(short)(currentCtl >>> TC_SHIFT);
1779              long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1780 <            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1780 >            long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP;
1781              Thread wt = Thread.currentThread();
1782              while (ctl == currentCtl) {
1783                  Thread.interrupted();  // timed variant of version in scan()
# Line 1738 | Line 1800 | public class ForkJoinPool extends Abstra
1800      }
1801  
1802      /**
1803 <     * Scans through queues looking for work while joining a task;
1804 <     * if any are present, signals.
1803 >     * Scans through queues looking for work while joining a task; if
1804 >     * any present, signals. May return early if more signalling is
1805 >     * detectably unneeded.
1806       *
1807 <     * @param task to return early if done
1807 >     * @param task return early if done
1808       * @param origin an index to start scan
1809       */
1810 <    final int helpSignal(ForkJoinTask<?> task, int origin) {
1811 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1812 <        if (task != null && (ws = workQueues) != null &&
1813 <            (m = ws.length - 1) >= 0) {
1814 <            for (int i = 0; i <= m; ++i) {
1815 <                if ((s = task.status) < 0)
1816 <                    return s;
1817 <                if ((q = ws[(i + origin) & m]) != null &&
1818 <                    (n = q.queueSize()) > 0) {
1819 <                    signalWork(q, n);
1820 <                    if ((int)(ctl >> AC_SHIFT) >= 0)
1810 >    private void helpSignal(ForkJoinTask<?> task, int origin) {
1811 >        WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s;
1812 >        if (task != null && task.status >= 0 &&
1813 >            (u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1814 >            (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1815 >            outer: for (int k = origin, j = m; j >= 0; --j) {
1816 >                WorkQueue q = ws[k++ & m];
1817 >                for (int n = m;;) { // limit to at most m signals
1818 >                    if (task.status < 0)
1819 >                        break outer;
1820 >                    if (q == null ||
1821 >                        ((s = -q.base + q.top) <= n && (n = s) <= 0))
1822                          break;
1823 +                    if ((u = (int)((c = ctl) >>> 32)) >= 0 ||
1824 +                        (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1825 +                        (w = ws[i]) == null)
1826 +                        break outer;
1827 +                    long nc = (((long)(w.nextWait & E_MASK)) |
1828 +                               ((long)(u + UAC_UNIT) << 32));
1829 +                    if (w.eventCount == (e | INT_SIGN) &&
1830 +                        U.compareAndSwapLong(this, CTL, c, nc)) {
1831 +                        w.eventCount = (e + E_SEQ) & E_MASK;
1832 +                        if ((p = w.parker) != null)
1833 +                            U.unpark(p);
1834 +                        if (--n <= 0)
1835 +                            break;
1836 +                    }
1837 +                }
1838 +            }
1839 +        }
1840 +    }
1841 +
1842 +    /**
1843 +     * Signals other workers if tasks are present in hinted queue.
1844 +     *
1845 +     * @param caller the worker with the hint
1846 +     */
1847 +    private void helpSignalHint(WorkQueue caller) {
1848 +        WorkQueue[] ws; WorkQueue q, w; Thread p; long c; int h, m, u, e, i, s;
1849 +        if (caller != null && (h = caller.hint) >= 0) {
1850 +            caller.hint = -1;
1851 +            if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1852 +                (ws = workQueues) != null && (m = ws.length - 1) >= 0 &&
1853 +                (q = ws[h & m]) != null) {
1854 +                for (int n = 2;;) { // limit to at most 2 signals
1855 +                    int idleCount = (caller.eventCount < 0) ? 0 : -1;
1856 +                    if (((s = idleCount - q.base + q.top) <= n &&
1857 +                         (n = s) <= 0) ||
1858 +                        (u = (int)((c = ctl) >>> 32)) >= 0 ||
1859 +                        (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1860 +                        (w = ws[i]) == null)
1861 +                        break;
1862 +                    long nc = (((long)(w.nextWait & E_MASK)) |
1863 +                               ((long)(u + UAC_UNIT) << 32));
1864 +                    if (w.eventCount == (e | INT_SIGN) &&
1865 +                        U.compareAndSwapLong(this, CTL, c, nc)) {
1866 +                        w.hint = h;
1867 +                        w.eventCount = (e + E_SEQ) & E_MASK;
1868 +                        if ((p = w.parker) != null)
1869 +                            U.unpark(p);
1870 +                        if (--n <= 0)
1871 +                            break;
1872 +                    }
1873                  }
1874              }
1875          }
1762        return 0;
1876      }
1877  
1878      /**
# Line 1793 | Line 1906 | public class ForkJoinPool extends Abstra
1906                      }
1907                      if ((ws = workQueues) == null || (m = ws.length - 1) <= 0)
1908                          break restart;              // shutting down
1909 <                    if ((v = ws[h = (j.stealHint | 1) & m]) == null ||
1909 >                    if ((v = ws[h = (j.hint | 1) & m]) == null ||
1910                          v.currentSteal != subtask) {
1911                          for (int origin = h;;) {    // find stealer
1912                              if (((h = (h + 2) & m) & 15) == 1 &&
# Line 1801 | Line 1914 | public class ForkJoinPool extends Abstra
1914                                  continue restart;   // occasional staleness check
1915                              if ((v = ws[h]) != null &&
1916                                  v.currentSteal == subtask) {
1917 <                                j.stealHint = h;    // save hint
1917 >                                j.hint = h;        // save hint
1918                                  break;
1919                              }
1920                              if (h == origin)
# Line 1850 | Line 1963 | public class ForkJoinPool extends Abstra
1963  
1964      /**
1965       * Analog of tryHelpStealer for CountedCompleters. Tries to steal
1966 <     * and run tasks within the target's computation
1966 >     * and run tasks within the target's computation.
1967       *
1968       * @param task the task to join
1969       * @param mode if shared, exit upon completing any task
# Line 1858 | Line 1971 | public class ForkJoinPool extends Abstra
1971       *
1972       */
1973      private int helpComplete(ForkJoinTask<?> task, int mode) {
1974 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1974 >        WorkQueue[] ws; WorkQueue q; int m, n, s, u;
1975          if (task != null && (ws = workQueues) != null &&
1976              (m = ws.length - 1) >= 0) {
1977              for (int j = 1, origin = j;;) {
# Line 1866 | Line 1979 | public class ForkJoinPool extends Abstra
1979                      return s;
1980                  if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) {
1981                      origin = j;
1982 <                    if (mode == SHARED_QUEUE && (int)(ctl >> AC_SHIFT) >= 0)
1982 >                    if (mode == SHARED_QUEUE &&
1983 >                        ((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0))
1984                          break;
1985                  }
1986                  else if ((j = (j + 2) & m) == origin)
# Line 1884 | Line 1998 | public class ForkJoinPool extends Abstra
1998       * may become starved.
1999       */
2000      final boolean tryCompensate() {
2001 <        int pc = parallelism, e, u, i, tc; long c;
2001 >        int pc = config & SMASK, e, i, tc; long c;
2002          WorkQueue[] ws; WorkQueue w; Thread p;
2003 <        if ((e = (int)(c = ctl)) >= 0 && (ws = workQueues) != null) {
2003 >        if ((ws = workQueues) != null && (e = (int)(c = ctl)) >= 0) {
2004              if (e != 0 && (i = e & SMASK) < ws.length &&
2005                  (w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) {
2006                  long nc = ((long)(w.nextWait & E_MASK) |
# Line 1898 | Line 2012 | public class ForkJoinPool extends Abstra
2012                      return true;   // replace with idle worker
2013                  }
2014              }
2015 <            else if ((short)((u = (int)(c >>> 32)) >>> UTC_SHIFT) >= 0 &&
2016 <                     (u >> UAC_SHIFT) + pc > 1) {
2015 >            else if ((tc = (short)(c >>> TC_SHIFT)) >= 0 &&
2016 >                     (int)(c >> AC_SHIFT) + pc > 1) {
2017                  long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK);
2018                  if (U.compareAndSwapLong(this, CTL, c, nc))
2019 <                    return true;    // no compensation
2019 >                    return true;   // no compensation
2020              }
2021 <            else if ((tc = u + pc) < MAX_CAP) {
2021 >            else if (tc + pc < MAX_CAP) {
2022                  long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
2023                  if (U.compareAndSwapLong(this, CTL, c, nc)) {
2024 +                    ForkJoinWorkerThreadFactory fac;
2025                      Throwable ex = null;
2026                      ForkJoinWorkerThread wt = null;
2027                      try {
1913                        ForkJoinWorkerThreadFactory fac;
2028                          if ((fac = factory) != null &&
2029                              (wt = fac.newThread(this)) != null) {
2030                              wt.start();
# Line 1919 | Line 2033 | public class ForkJoinPool extends Abstra
2033                      } catch (Throwable rex) {
2034                          ex = rex;
2035                      }
2036 <                    deregisterWorker(wt, ex); // adjust counts etc
2036 >                    deregisterWorker(wt, ex); // clean up and return false
2037                  }
2038              }
2039          }
# Line 1938 | Line 2052 | public class ForkJoinPool extends Abstra
2052          if (joiner != null && task != null && (s = task.status) >= 0) {
2053              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2054              joiner.currentJoin = task;
2055 <            do {} while ((s = task.status) >= 0 &&
1942 <                         joiner.queueSize() > 0 &&
2055 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2056                           joiner.tryRemoveAndExec(task)); // process local tasks
2057 <            if (s >= 0 && (s = task.status) >= 0 &&
2058 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2059 <                (task instanceof CountedCompleter))
2060 <                s = helpComplete(task, LIFO_QUEUE);
2057 >            if (s >= 0 && (s = task.status) >= 0) {
2058 >                helpSignal(task, joiner.poolIndex);
2059 >                if ((s = task.status) >= 0 &&
2060 >                    (task instanceof CountedCompleter))
2061 >                    s = helpComplete(task, LIFO_QUEUE);
2062 >            }
2063              while (s >= 0 && (s = task.status) >= 0) {
2064 <                if ((joiner.queueSize() > 0 ||           // try helping
2064 >                if ((!joiner.isEmpty() ||           // try helping
2065                       (s = tryHelpStealer(joiner, task)) == 0) &&
2066 <                    (s = task.status) >= 0 && tryCompensate()) {
2067 <                    if (task.trySetSignal() && (s = task.status) >= 0) {
2068 <                        synchronized (task) {
2069 <                            if (task.status >= 0) {
2070 <                                try {                // see ForkJoinTask
2071 <                                    task.wait();     //  for explanation
2072 <                                } catch (InterruptedException ie) {
2066 >                    (s = task.status) >= 0) {
2067 >                    helpSignal(task, joiner.poolIndex);
2068 >                    if ((s = task.status) >= 0 && tryCompensate()) {
2069 >                        if (task.trySetSignal() && (s = task.status) >= 0) {
2070 >                            synchronized (task) {
2071 >                                if (task.status >= 0) {
2072 >                                    try {                // see ForkJoinTask
2073 >                                        task.wait();     //  for explanation
2074 >                                    } catch (InterruptedException ie) {
2075 >                                    }
2076                                  }
2077 +                                else
2078 +                                    task.notifyAll();
2079                              }
1960                            else
1961                                task.notifyAll();
2080                          }
2081 +                        long c;                          // re-activate
2082 +                        do {} while (!U.compareAndSwapLong
2083 +                                     (this, CTL, c = ctl, c + AC_UNIT));
2084                      }
1964                    long c;                          // re-activate
1965                    do {} while (!U.compareAndSwapLong
1966                                 (this, CTL, c = ctl, c + AC_UNIT));
2085                  }
2086              }
2087              joiner.currentJoin = prevJoin;
# Line 1984 | Line 2102 | public class ForkJoinPool extends Abstra
2102          if (joiner != null && task != null && (s = task.status) >= 0) {
2103              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2104              joiner.currentJoin = task;
2105 <            do {} while ((s = task.status) >= 0 &&
1988 <                         joiner.queueSize() > 0 &&
2105 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2106                           joiner.tryRemoveAndExec(task));
2107 <            if (s >= 0 && (s = task.status) >= 0 &&
2108 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2109 <                (task instanceof CountedCompleter))
2110 <                s = helpComplete(task, LIFO_QUEUE);
2111 <            if (s >= 0 && joiner.queueSize() == 0) {
2107 >            if (s >= 0 && (s = task.status) >= 0) {
2108 >                helpSignal(task, joiner.poolIndex);
2109 >                if ((s = task.status) >= 0 &&
2110 >                    (task instanceof CountedCompleter))
2111 >                    s = helpComplete(task, LIFO_QUEUE);
2112 >            }
2113 >            if (s >= 0 && joiner.isEmpty()) {
2114                  do {} while (task.status >= 0 &&
2115                               tryHelpStealer(joiner, task) > 0);
2116              }
# Line 2007 | Line 2126 | public class ForkJoinPool extends Abstra
2126       * @param r a (random) seed for scanning
2127       */
2128      private WorkQueue findNonEmptyStealQueue(int r) {
2010        int step = (r >>> 16) | 1;
2129          for (WorkQueue[] ws;;) {
2130 <            int ps = plock, m;
2130 >            int ps = plock, m, n;
2131              if ((ws = workQueues) == null || (m = ws.length - 1) < 1)
2132                  return null;
2133 <            for (int j = (m + 1) << 2; ; r += step) {
2134 <                WorkQueue q = ws[((r << 1) | 1) & m];
2135 <                if (q != null && q.queueSize() > 0)
2133 >            for (int j = (m + 1) << 2; ;) {
2134 >                WorkQueue q = ws[(((r + j) << 1) | 1) & m];
2135 >                if (q != null && (n = q.base - q.top) < 0) {
2136 >                    if (n < -1)
2137 >                        signalWork(q);
2138                      return q;
2139 +                }
2140                  else if (--j < 0) {
2141                      if (plock == ps)
2142                          return null;
# Line 2058 | Line 2179 | public class ForkJoinPool extends Abstra
2179                  }
2180                  else
2181                      c = ctl;        // re-increment on exit
2182 <                if ((int)(c >> AC_SHIFT) + parallelism == 0) {
2182 >                if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) {
2183                      do {} while (!U.compareAndSwapLong
2184                                   (this, CTL, c = ctl, c + AC_UNIT));
2185                      break;
# Line 2133 | Line 2254 | public class ForkJoinPool extends Abstra
2254      static int getSurplusQueuedTaskCount() {
2255          Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q;
2256          if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) {
2257 <            int b = (q = (wt = (ForkJoinWorkerThread)t).workQueue).base;
2258 <            int p = (pool = wt.pool).parallelism;
2257 >            int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).config & SMASK;
2258 >            int n = (q = wt.workQueue).top - q.base;
2259              int a = (int)(pool.ctl >> AC_SHIFT) + p;
2260 <            return q.top - b - (a > (p >>>= 1) ? 0 :
2261 <                                a > (p >>>= 1) ? 1 :
2262 <                                a > (p >>>= 1) ? 2 :
2263 <                                a > (p >>>= 1) ? 4 :
2264 <                                8);
2260 >            return n - (a > (p >>>= 1) ? 0 :
2261 >                        a > (p >>>= 1) ? 1 :
2262 >                        a > (p >>>= 1) ? 2 :
2263 >                        a > (p >>>= 1) ? 4 :
2264 >                        8);
2265          }
2266          return 0;
2267      }
# Line 2166 | Line 2287 | public class ForkJoinPool extends Abstra
2287              return false;
2288          for (long c;;) {
2289              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2290 <                if ((short)(c >>> TC_SHIFT) == -parallelism) {
2290 >                if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) {
2291                      synchronized (this) {
2292                          notifyAll();                // signal when 0 workers
2293                      }
# Line 2185 | Line 2306 | public class ForkJoinPool extends Abstra
2306                      releasePlock(nps);
2307              }
2308              if (!now) {                             // check if idle & no tasks
2309 <                if ((int)(c >> AC_SHIFT) != -parallelism ||
2309 >                if ((int)(c >> AC_SHIFT) != -(config & SMASK) ||
2310                      hasQueuedSubmissions())
2311                      return false;
2312                  // Check for unqueued inactive workers. One pass suffices.
# Line 2255 | Line 2376 | public class ForkJoinPool extends Abstra
2376       */
2377      static boolean tryExternalUnpush(ForkJoinTask<?> t) {
2378          ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z;
2379 <        ForkJoinTask<?>[] a;  int m, s; long j;
2380 <        if ((z = submitters.get()) != null &&
2379 >        ForkJoinTask<?>[] a;  int m, s;
2380 >        if (t != null &&
2381 >            (z = submitters.get()) != null &&
2382              (p = commonPool) != null &&
2383              (ws = p.workQueues) != null &&
2384              (m = ws.length - 1) >= 0 &&
2385              (q = ws[m & z.seed & SQMASK]) != null &&
2386              (s = q.top) != q.base &&
2387 <            (a = q.array) != null &&
2388 <            U.getObjectVolatile
2389 <            (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2390 <            U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2391 <            if (q.array == a && q.top == s && // recheck
2392 <                U.compareAndSwapObject(a, j, t, null)) {
2393 <                q.top = s - 1;
2387 >            (a = q.array) != null) {
2388 >            long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
2389 >            if (U.getObject(a, j) == t &&
2390 >                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2391 >                if (q.array == a && q.top == s && // recheck
2392 >                    U.compareAndSwapObject(a, j, t, null)) {
2393 >                    q.top = s - 1;
2394 >                    q.qlock = 0;
2395 >                    return true;
2396 >                }
2397                  q.qlock = 0;
2273                return true;
2398              }
2275            q.qlock = 0;
2399          }
2400          return false;
2401      }
# Line 2287 | Line 2410 | public class ForkJoinPool extends Abstra
2410          if (q != null && (a = q.array) != null && (m = (a.length - 1)) >= 0 &&
2411              root != null && root.status >= 0) {
2412              for (;;) {
2413 <                int s; Object o; CountedCompleter<?> task = null;
2413 >                int s, u; Object o; CountedCompleter<?> task = null;
2414                  if ((s = q.top) - q.base > 0) {
2415                      long j = ((m & (s - 1)) << ASHIFT) + ABASE;
2416                      if ((o = U.getObject(a, j)) != null &&
# Line 2305 | Line 2428 | public class ForkJoinPool extends Abstra
2428                                  }
2429                                  break;
2430                              }
2431 <                        } while((r = r.completer) != null);
2431 >                        } while ((r = r.completer) != null);
2432                      }
2433                  }
2434                  if (task != null)
2435                      task.doExec();
2436 <                if (root.status < 0 || (int)(ctl >> AC_SHIFT) >= 0)
2436 >                if (root.status < 0 ||
2437 >                    (u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
2438                      break;
2439                  if (task == null) {
2440 <                    if (helpSignal(root, q.poolIndex) >= 0)
2440 >                    helpSignal(root, q.poolIndex);
2441 >                    if (root.status >= 0)
2442                          helpComplete(root, SHARED_QUEUE);
2443                      break;
2444                  }
# Line 2328 | Line 2453 | public class ForkJoinPool extends Abstra
2453      static void externalHelpJoin(ForkJoinTask<?> t) {
2454          // Some hard-to-avoid overlap with tryExternalUnpush
2455          ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z;
2456 <        ForkJoinTask<?>[] a;  int m, s, n; long j;
2457 <        if (t != null && t.status >= 0 &&
2456 >        ForkJoinTask<?>[] a;  int m, s, n;
2457 >        if (t != null &&
2458              (z = submitters.get()) != null &&
2459              (p = commonPool) != null &&
2460              (ws = p.workQueues) != null &&
2461              (m = ws.length - 1) >= 0 &&
2462              (q = ws[m & z.seed & SQMASK]) != null &&
2463              (a = q.array) != null) {
2464 <            if ((s = q.top) != q.base &&
2465 <                U.getObjectVolatile
2466 <                (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2467 <                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2468 <                if (q.array == a && q.top == s &&
2469 <                    U.compareAndSwapObject(a, j, t, null)) {
2470 <                    q.top = s - 1;
2471 <                    q.qlock = 0;
2472 <                    t.doExec();
2464 >            int am = a.length - 1;
2465 >            if ((s = q.top) != q.base) {
2466 >                long j = ((am & (s - 1)) << ASHIFT) + ABASE;
2467 >                if (U.getObject(a, j) == t &&
2468 >                    U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2469 >                    if (q.array == a && q.top == s &&
2470 >                        U.compareAndSwapObject(a, j, t, null)) {
2471 >                        q.top = s - 1;
2472 >                        q.qlock = 0;
2473 >                        t.doExec();
2474 >                    }
2475 >                    else
2476 >                        q.qlock = 0;
2477                  }
2349                else
2350                    q.qlock = 0;
2478              }
2479              if (t.status >= 0) {
2480                  if (t instanceof CountedCompleter)
# Line 2363 | Line 2490 | public class ForkJoinPool extends Abstra
2490       */
2491      static void externalHelpQuiescePool() {
2492          ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b;
2366        int r = ThreadLocalRandom.current().nextInt();
2493          if ((p = commonPool) != null &&
2494 <            (q = p.findNonEmptyStealQueue(r)) != null &&
2494 >            (q = p.findNonEmptyStealQueue(1)) != null &&
2495              (b = q.base) - q.top < 0 &&
2496              (t = q.pollAt(b)) != null)
2497              t.doExec();
# Line 2442 | Line 2568 | public class ForkJoinPool extends Abstra
2568              throw new NullPointerException();
2569          if (parallelism <= 0 || parallelism > MAX_CAP)
2570              throw new IllegalArgumentException();
2445        this.parallelism = parallelism;
2571          this.factory = factory;
2572          this.ueh = handler;
2573 <        this.localMode = asyncMode ? FIFO_QUEUE : LIFO_QUEUE;
2573 >        this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0);
2574          long np = (long)(-parallelism); // offset ctl counts
2575          this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2576          int pn = nextPoolId();
# Line 2462 | Line 2587 | public class ForkJoinPool extends Abstra
2587      ForkJoinPool(int parallelism, long ctl,
2588                   ForkJoinWorkerThreadFactory factory,
2589                   Thread.UncaughtExceptionHandler handler) {
2590 <        this.parallelism = parallelism;
2590 >        this.config = parallelism;
2591          this.ctl = ctl;
2592          this.factory = factory;
2593          this.ueh = handler;
2469        this.localMode = LIFO_QUEUE;
2594          this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2595      }
2596  
# Line 2476 | Line 2600 | public class ForkJoinPool extends Abstra
2600       * @return the common pool instance
2601       */
2602      public static ForkJoinPool commonPool() {
2603 <        return commonPool; // cannot be null (if so, a static init error)
2603 >        // assert commonPool != null : "static init error";
2604 >        return commonPool;
2605      }
2606  
2607      // Execution methods
# Line 2648 | Line 2773 | public class ForkJoinPool extends Abstra
2773       * @return the targeted parallelism level of this pool
2774       */
2775      public int getParallelism() {
2776 <        return parallelism;
2776 >        return config & SMASK;
2777      }
2778  
2779      /**
# Line 2669 | Line 2794 | public class ForkJoinPool extends Abstra
2794       * @return the number of worker threads
2795       */
2796      public int getPoolSize() {
2797 <        return parallelism + (short)(ctl >>> TC_SHIFT);
2797 >        return (config & SMASK) + (short)(ctl >>> TC_SHIFT);
2798      }
2799  
2800      /**
# Line 2679 | Line 2804 | public class ForkJoinPool extends Abstra
2804       * @return {@code true} if this pool uses async mode
2805       */
2806      public boolean getAsyncMode() {
2807 <        return localMode != 0;
2807 >        return (config >>> 16) == FIFO_QUEUE;
2808      }
2809  
2810      /**
# Line 2710 | Line 2835 | public class ForkJoinPool extends Abstra
2835       * @return the number of active threads
2836       */
2837      public int getActiveThreadCount() {
2838 <        int r = parallelism + (int)(ctl >> AC_SHIFT);
2838 >        int r = (config & SMASK) + (int)(ctl >> AC_SHIFT);
2839          return (r <= 0) ? 0 : r; // suppress momentarily negative values
2840      }
2841  
# Line 2726 | Line 2851 | public class ForkJoinPool extends Abstra
2851       * @return {@code true} if all threads are currently idle
2852       */
2853      public boolean isQuiescent() {
2854 <        return (int)(ctl >> AC_SHIFT) + parallelism == 0;
2854 >        return (int)(ctl >> AC_SHIFT) + (config & SMASK) == 0;
2855      }
2856  
2857      /**
# Line 2803 | Line 2928 | public class ForkJoinPool extends Abstra
2928          WorkQueue[] ws; WorkQueue w;
2929          if ((ws = workQueues) != null) {
2930              for (int i = 0; i < ws.length; i += 2) {
2931 <                if ((w = ws[i]) != null && w.queueSize() != 0)
2931 >                if ((w = ws[i]) != null && !w.isEmpty())
2932                      return true;
2933              }
2934          }
# Line 2889 | Line 3014 | public class ForkJoinPool extends Abstra
3014                  }
3015              }
3016          }
3017 <        int pc = parallelism;
3017 >        int pc = (config & SMASK);
3018          int tc = pc + (short)(c >>> TC_SHIFT);
3019          int ac = pc + (int)(c >> AC_SHIFT);
3020          if (ac < 0) // ignore transient negative
# Line 2962 | Line 3087 | public class ForkJoinPool extends Abstra
3087      public boolean isTerminated() {
3088          long c = ctl;
3089          return ((c & STOP_BIT) != 0L &&
3090 <                (short)(c >>> TC_SHIFT) == -parallelism);
3090 >                (short)(c >>> TC_SHIFT) == -(config & SMASK));
3091      }
3092  
3093      /**
# Line 2970 | Line 3095 | public class ForkJoinPool extends Abstra
3095       * commenced but not yet completed.  This method may be useful for
3096       * debugging. A return of {@code true} reported a sufficient
3097       * period after shutdown may indicate that submitted tasks have
3098 <     * ignored or suppressed interruption, or are waiting for IO,
3098 >     * ignored or suppressed interruption, or are waiting for I/O,
3099       * causing this executor not to properly terminate. (See the
3100       * advisory notes for class {@link ForkJoinTask} stating that
3101       * tasks should not normally entail blocking operations.  But if
# Line 2981 | Line 3106 | public class ForkJoinPool extends Abstra
3106      public boolean isTerminating() {
3107          long c = ctl;
3108          return ((c & STOP_BIT) != 0L &&
3109 <                (short)(c >>> TC_SHIFT) != -parallelism);
3109 >                (short)(c >>> TC_SHIFT) != -(config & SMASK));
3110      }
3111  
3112      /**
# Line 3125 | Line 3250 | public class ForkJoinPool extends Abstra
3250          if (t instanceof ForkJoinWorkerThread) {
3251              ForkJoinPool p = ((ForkJoinWorkerThread)t).pool;
3252              while (!blocker.isReleasable()) { // variant of helpSignal
3253 <                WorkQueue[] ws; WorkQueue q; int m, n;
3253 >                WorkQueue[] ws; WorkQueue q; int m, u;
3254                  if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) {
3255                      for (int i = 0; i <= m; ++i) {
3256                          if (blocker.isReleasable())
3257                              return;
3258 <                        if ((q = ws[i]) != null && (n = q.queueSize()) > 0) {
3259 <                            p.signalWork(q, n);
3260 <                            if ((int)(p.ctl >> AC_SHIFT) >= 0)
3258 >                        if ((q = ws[i]) != null && q.base - q.top < 0) {
3259 >                            p.signalWork(q);
3260 >                            if ((u = (int)(p.ctl >>> 32)) >= 0 ||
3261 >                                (u >> UAC_SHIFT) >= 0)
3262                                  break;
3263                          }
3264                      }
# Line 3178 | Line 3304 | public class ForkJoinPool extends Abstra
3304      private static final long QLOCK;
3305  
3306      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
3307          int s; // initialize field offsets for CAS etc
3308          try {
3309              U = getUnsafe();
# Line 3227 | Line 3332 | public class ForkJoinPool extends Abstra
3332          if ((s & (s-1)) != 0)
3333              throw new Error("data type scale not a power of two");
3334  
3335 +        submitters = new ThreadLocal<Submitter>();
3336 +        ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory =
3337 +            new DefaultForkJoinWorkerThreadFactory();
3338 +        modifyThreadPermission = new RuntimePermission("modifyThread");
3339 +
3340          /*
3341 <         * For extra caution, computations to set up pool state are
3342 <         * here; the constructor just assigns these values to fields.
3341 >         * Establish common pool parameters.  For extra caution,
3342 >         * computations to set up common pool state are here; the
3343 >         * constructor just assigns these values to fields.
3344           */
3345 <        ForkJoinWorkerThreadFactory defaultFac =
3346 <            defaultForkJoinWorkerThreadFactory =
3347 <            new DefaultForkJoinWorkerThreadFactory();
3348 <        if (fac == null)
3349 <            fac = defaultFac;
3345 >
3346 >        int par = 0;
3347 >        Thread.UncaughtExceptionHandler handler = null;
3348 >        try {  // TBD: limit or report ignored exceptions?
3349 >            String pp = System.getProperty
3350 >                ("java.util.concurrent.ForkJoinPool.common.parallelism");
3351 >            String hp = System.getProperty
3352 >                ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
3353 >            String fp = System.getProperty
3354 >                ("java.util.concurrent.ForkJoinPool.common.threadFactory");
3355 >            if (fp != null)
3356 >                fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
3357 >                       getSystemClassLoader().loadClass(fp).newInstance());
3358 >            if (hp != null)
3359 >                handler = ((Thread.UncaughtExceptionHandler)ClassLoader.
3360 >                           getSystemClassLoader().loadClass(hp).newInstance());
3361 >            if (pp != null)
3362 >                par = Integer.parseInt(pp);
3363 >        } catch (Exception ignore) {
3364 >        }
3365 >
3366          if (par <= 0)
3367              par = Runtime.getRuntime().availableProcessors();
3368          if (par > MAX_CAP)
3369              par = MAX_CAP;
3370 +        commonPoolParallelism = par;
3371          long np = (long)(-par); // precompute initial ctl value
3372          long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
3373  
3246        commonPoolParallelism = par;
3374          commonPool = new ForkJoinPool(par, ct, fac, handler);
3248        modifyThreadPermission = new RuntimePermission("modifyThread");
3249        submitters = new ThreadLocal<Submitter>();
3375      }
3376  
3377      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines