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.16 by jsr166, Sun Nov 18 18:03:10 2012 UTC vs.
Revision 1.22 by jsr166, Wed Nov 21 22:15:41 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 240 | 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 249 | 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 317 | 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 394 | 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 526 | 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 599 | 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 625 | 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
632 <        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 641 | 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
644        // Heuristic padding to ameliorate unfortunate memory placements
645        Object p00, p01, p02, p03, p04, p05, p06, p07;
646        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.
658 <         * Cases needing resizing or rejection are relayed to fullPush
659 <         * (that also handles shared queues).
660 <         *
661 <         * @param task the task. Caller must ensure non-null.
662 <         * @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
681 <         * specialization of a common fast path of this method is in
682 <         * ForkJoinPool.externalPush. When called from a FJWT queue,
683 <         * this can fail only if the pool has been shut down or
684 <         * 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 {
698 <                int s = top, oldLen, len;
699 <                if ((a = array) == null)
700 <                    a = array = new ForkJoinTask<?>[len=INITIAL_QUEUE_CAPACITY];
701 <                else if ((oldLen = a.length) > s + 1 - base)
702 <                    len = oldLen;
703 <                else if ((len = oldLen << 1) > MAXIMUM_QUEUE_CAPACITY)
704 <                    throw new RejectedExecutionException("Capacity exceeded");
705 <                else {
706 <                    int oldMask, b;
707 <                    ForkJoinTask<?>[] oldA = a;
708 <                    a = array = new ForkJoinTask<?>[len];
709 <                    if ((oldMask = oldLen - 1) >= 0 && s - (b = base) > 0) {
710 <                        int mask = len - 1;
711 <                        do {
712 <                            ForkJoinTask<?> x;
713 <                            int oldj = ((b & oldMask) << ASHIFT) + ABASE;
714 <                            int j    = ((b &    mask) << ASHIFT) + ABASE;
715 <                            x = (ForkJoinTask<?>)
716 <                                U.getObjectVolatile(oldA, oldj);
717 <                            if (x != null &&
718 <                                U.compareAndSwapObject(oldA, oldj, x, null))
719 <                                U.putObjectVolatile(a, j, x);
720 <                        } while (++b != s);
721 <                    }
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);
725 <                top = s + 1;
726 <            } finally {
727 <                if (!owned)
728 <                    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 853 | Line 874 | public class ForkJoinPool extends Abstra
874              return seed = r ^= r << 5;
875          }
876  
856        /**
857         * Provides a more accurate estimate of size than (top - base)
858         * by ordering reads and checking whether a near-empty queue
859         * has at least one unclaimed task.
860         */
861        final int queueSize() {
862            ForkJoinTask<?>[] a; int k, s, n;
863            return ((n = base - (s = top)) < 0 &&
864                    (n != -1 ||
865                     ((a = array) != null && (k = a.length) > 0 &&
866                      U.getObject
867                      (a, (long)((((k - 1) & (s - 1)) << ASHIFT) + ABASE)) != null))) ?
868                -n : 0;
869        }
870
877          // Specialized execution methods
878  
879          /**
# Line 981 | 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
985 <                    ForkJoinPool p;
986 <                    if ((p = pool) != null)
987 <                        p.collectStealCount(this);
988 <                }
989 <                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 1058 | Line 1061 | public class ForkJoinPool extends Abstra
1061          }
1062      }
1063  
1061    /**
1062     * Per-thread records for threads that submit to pools. Currently
1063     * holds only pseudo-random seed / index that is used to choose
1064     * submission queues in method externalPush. In the future, this may
1065     * also incorporate a means to implement different task rejection
1066     * and resubmission policies.
1067     *
1068     * Seeds for submitters and workers/workQueues work in basically
1069     * the same way but are initialized and updated using slightly
1070     * different mechanics. Both are initialized using the same
1071     * approach as in class ThreadLocal, where successive values are
1072     * unlikely to collide with previous values. Seeds are then
1073     * randomly modified upon collisions using xorshifts, which
1074     * requires a non-zero seed.
1075     */
1076    static final class Submitter {
1077        int seed;
1078        Submitter(int s) { seed = s; }
1079    }
1080
1081    /** Property prefix for constructing common pool */
1082    private static final String propPrefix =
1083        "java.util.concurrent.ForkJoinPool.common.";
1084
1064      // static fields (initialized in static initializer below)
1065  
1066      /**
# Line 1092 | 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 1106 | 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
1113 <     * 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 1249 | Line 1228 | public class ForkJoinPool extends Abstra
1228      static final int FIFO_QUEUE          =  1;
1229      static final int SHARED_QUEUE        = -1;
1230  
1231 +    // bounds for #steps in scan loop -- must be power 2 minus 1
1232 +    private static final int MIN_SCAN    = 0x1ff;   // cover estimation slop
1233 +    private static final int MAX_SCAN    = 0x1ffff; // 4 * max workers
1234 +
1235      // Instance fields
1236  
1237      /*
1238 <     * Field layout order in this class tends to matter more than one
1239 <     * would like. Runtime layout order is only loosely related to
1238 >     * Field layout of this class tends to matter more than one would
1239 >     * like. Runtime layout order is only loosely related to
1240       * declaration order and may differ across JVMs, but the following
1241       * empirically works OK on current JVMs.
1242       */
1243 +
1244 +    // Heuristic padding to ameliorate unfortunate memory placements
1245 +    volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06;
1246 +
1247      volatile long stealCount;                  // collects worker counts
1248      volatile long ctl;                         // main pool control
1262    final int parallelism;                     // parallelism level
1263    final int localMode;                       // per-worker scheduling mode
1264    volatile int indexSeed;                    // worker/submitter index seed
1249      volatile int plock;                        // shutdown status and seqLock
1250 +    volatile int indexSeed;                    // worker/submitter index seed
1251 +    final int config;                          // mode and parallelism level
1252      WorkQueue[] workQueues;                    // main registry
1253 <    final ForkJoinWorkerThreadFactory factory; // factory for new workers
1253 >    final ForkJoinWorkerThreadFactory factory;
1254      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1255      final String workerNamePrefix;             // to create worker name string
1256  
1257 +    volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17;
1258 +    volatile Object pad18, pad19, pad1a, pad1b;
1259 +
1260      /*
1261       * Acquires the plock lock to protect worker array and related
1262       * updates. This method is called only if an initial CAS on plock
# Line 1284 | Line 1273 | public class ForkJoinPool extends Abstra
1273              if (((ps = plock) & PL_LOCK) == 0 &&
1274                  U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK))
1275                  return nps;
1276 <            else if (r == 0)
1277 <                r = ThreadLocalRandom.current().nextInt(); // randomize spins
1276 >            else if (r == 0) { // randomize spins if possible
1277 >                Thread t = Thread.currentThread(); WorkQueue w; Submitter z;
1278 >                if ((t instanceof ForkJoinWorkerThread) &&
1279 >                    (w = ((ForkJoinWorkerThread)t).workQueue) != null)
1280 >                    r = w.seed;
1281 >                else if ((z = submitters.get()) != null)
1282 >                    r = z.seed;
1283 >                else
1284 >                    r = 1;
1285 >            }
1286              else if (spins >= 0) {
1287                  r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift
1288                  if (r >= 0)
# Line 1319 | Line 1316 | public class ForkJoinPool extends Abstra
1316          synchronized (this) { notifyAll(); }
1317      }
1318  
1319 +    /**
1320 +     * Performs secondary initialization, called when plock is zero.
1321 +     * Creates workQueue array and sets plock to a valid value.  The
1322 +     * lock body must be exception-free (so no try/finally) so we
1323 +     * optimistically allocate new array outside the lock and throw
1324 +     * away if (very rarely) not needed. (A similar tactic is used in
1325 +     * fullExternalPush.)  Because the plock seq value can eventually
1326 +     * wrap around zero, this method harmlessly fails to reinitialize
1327 +     * if workQueues exists, while still advancing plock.
1328 +     *
1329 +     * Additonally tries to create the first worker.
1330 +     */
1331 +    private void initWorkers() {
1332 +        WorkQueue[] ws, nws; int ps;
1333 +        int p = config & SMASK;        // find power of two table size
1334 +        int n = (p > 1) ? p - 1 : 1;   // ensure at least 2 slots
1335 +        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1336 +        n = (n + 1) << 1;
1337 +        if ((ws = workQueues) == null || ws.length == 0)
1338 +            nws = new WorkQueue[n];
1339 +        else
1340 +            nws = null;
1341 +        if (((ps = plock) & PL_LOCK) != 0 ||
1342 +            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1343 +            ps = acquirePlock();
1344 +        if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1345 +            workQueues = nws;
1346 +        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1347 +        if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1348 +            releasePlock(nps);
1349 +        tryAddWorker();
1350 +    }
1351 +
1352 +    /**
1353 +     * Tries to create and start one worker. Adjusts counts etc on
1354 +     * failure.
1355 +     */
1356 +    private void tryAddWorker() {
1357 +        long c; int u;
1358 +        while ((u = (int)((c = ctl) >>> 32)) < 0 &&
1359 +               (u & SHORT_SIGN) != 0 && (int)c == 0) {
1360 +            long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1361 +                             ((u + UAC_UNIT) & UAC_MASK)) << 32;
1362 +            if (U.compareAndSwapLong(this, CTL, c, nc)) {
1363 +                ForkJoinWorkerThreadFactory fac;
1364 +                Throwable ex = null;
1365 +                ForkJoinWorkerThread wt = null;
1366 +                try {
1367 +                    if ((fac = factory) != null &&
1368 +                        (wt = fac.newThread(this)) != null) {
1369 +                        wt.start();
1370 +                        break;
1371 +                    }
1372 +                } catch (Throwable e) {
1373 +                    ex = e;
1374 +                }
1375 +                deregisterWorker(wt, ex);
1376 +                break;
1377 +            }
1378 +        }
1379 +    }
1380 +
1381      //  Registering and deregistering workers
1382  
1383      /**
1384 <     * Callback from ForkJoinWorkerThread constructor to establish its
1385 <     * poolIndex and record its WorkQueue. To avoid scanning bias due
1386 <     * to packing entries in front of the workQueues array, we treat
1387 <     * the array as a simple power-of-two hash table using per-thread
1388 <     * seed as hash, expanding as needed.
1389 <     *
1390 <     * @param w the worker's queue
1391 <     */
1392 <    final void registerWorker(WorkQueue w) {
1393 <        int s, ps; // generate a rarely colliding candidate index seed
1394 <        do {} while (!U.compareAndSwapInt(this, INDEXSEED,
1395 <                                          s = indexSeed, s += SEED_INCREMENT) ||
1384 >     * Callback from ForkJoinWorkerThread to establish and record its
1385 >     * WorkQueue. To avoid scanning bias due to packing entries in
1386 >     * front of the workQueues array, we treat the array as a simple
1387 >     * power-of-two hash table using per-thread seed as hash,
1388 >     * expanding as needed.
1389 >     *
1390 >     * @param wt the worker thread
1391 >     * @return the worker's queue
1392 >     */
1393 >    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
1394 >        Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps;
1395 >        wt.setDaemon(true);
1396 >        if ((handler = ueh) != null)
1397 >            wt.setUncaughtExceptionHandler(handler);
1398 >        do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
1399 >                                          s += SEED_INCREMENT) ||
1400                       s == 0); // skip 0
1401 +        WorkQueue w = new WorkQueue(this, wt, config >>> 16, s);
1402          if (((ps = plock) & PL_LOCK) != 0 ||
1403              !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1404              ps = acquirePlock();
1405          int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1406          try {
1407 <            WorkQueue[] ws;
1344 <            if (w != null && (ws = workQueues) != null) {
1345 <                w.seed = s;
1407 >            if ((ws = workQueues) != null) {    // skip if shutting down
1408                  int n = ws.length, m = n - 1;
1409 <                int r = (s << 1) | 1;               // use odd-numbered indices
1410 <                if (ws[r &= m] != null) {           // collision
1411 <                    int probes = 0;                 // step by approx half size
1409 >                int r = (s << 1) | 1;           // use odd-numbered indices
1410 >                if (ws[r &= m] != null) {       // collision
1411 >                    int probes = 0;             // step by approx half size
1412                      int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
1413                      while (ws[r = (r + step) & m] != null) {
1414                          if (++probes >= n) {
# Line 1356 | Line 1418 | public class ForkJoinPool extends Abstra
1418                          }
1419                      }
1420                  }
1421 <                w.eventCount = w.poolIndex = r;     // establish before recording
1421 >                w.eventCount = w.poolIndex = r; // volatile write orders
1422                  ws[r] = w;
1423              }
1424          } finally {
1425              if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1426                  releasePlock(nps);
1427          }
1428 +        wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex)));
1429 +        return w;
1430      }
1431  
1432      /**
# Line 1378 | Line 1442 | public class ForkJoinPool extends Abstra
1442          WorkQueue w = null;
1443          if (wt != null && (w = wt.workQueue) != null) {
1444              int ps;
1381            collectStealCount(w);
1445              w.qlock = -1;                // ensure set
1446 +            long ns = w.nsteals, sc;     // collect steal count
1447 +            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1448 +                                               sc = stealCount, sc + ns));
1449              if (((ps = plock) & PL_LOCK) != 0 ||
1450                  !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1451                  ps = acquirePlock();
# Line 1404 | Line 1470 | public class ForkJoinPool extends Abstra
1470          if (!tryTerminate(false, false) && w != null) {
1471              w.cancelAll();                  // cancel remaining tasks
1472              if (w.array != null)            // suppress signal if never ran
1473 <                signalWork(null, 1);        // wake up or create replacement
1473 >                tryAddWorker();             // create replacement
1474              if (ex == null)                 // help clean refs on way out
1475                  ForkJoinTask.helpExpungeStaleExceptions();
1476          }
# Line 1413 | Line 1479 | public class ForkJoinPool extends Abstra
1479              ForkJoinTask.rethrow(ex);
1480      }
1481  
1416    /**
1417     * Collect worker steal count into total. Called on termination
1418     * and upon int overflow of local count. (There is a possible race
1419     * in the latter case vs any caller of getStealCount, which can
1420     * make its results less accurate than usual.)
1421     */
1422    final void collectStealCount(WorkQueue w) {
1423        if (w != null) {
1424            long sc;
1425            int ns = w.nsteals;
1426            w.nsteals = 0; // handle overflow
1427            long steals = (ns >= 0) ? ns : 1L + (long)(Integer.MAX_VALUE);
1428            do {} while (!U.compareAndSwapLong(this, STEALCOUNT,
1429                                               sc = stealCount, sc + steals));
1430        }
1431    }
1432
1482      // Submissions
1483  
1484      /**
# Line 1446 | Line 1495 | public class ForkJoinPool extends Abstra
1495              (ws = workQueues) != null && (m = (ws.length - 1)) >= 0 &&
1496              (q = ws[m & z.seed & SQMASK]) != null &&
1497              U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock
1498 <            int s = q.top, n;
1499 <            if ((a = q.array) != null && a.length > (n = s + 1 - q.base)) {
1500 <                U.putObject(a, (long)(((a.length - 1) & s) << ASHIFT) + ABASE,
1501 <                            task);
1498 >            int b = q.base, s = q.top, n, an;
1499 >            if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) {
1500 >                int j = (((an - 1) & s) << ASHIFT) + ABASE;
1501 >                U.putOrderedObject(a, j, task);
1502                  q.top = s + 1;                     // push on to deque
1503                  q.qlock = 0;
1504 <                if (n <= 1)
1505 <                    signalWork(q, 1);
1504 >                if (n <= 2)
1505 >                    signalWork(q);
1506                  return;
1507              }
1508              q.qlock = 0;
# Line 1464 | Line 1513 | public class ForkJoinPool extends Abstra
1513      /**
1514       * Full version of externalPush. This method is called, among
1515       * other times, upon the first submission of the first task to the
1516 <     * pool, so must perform secondary initialization: creating
1517 <     * workQueue array and setting plock to a valid value. It also
1518 <     * detects first submission by an external thread by looking up
1519 <     * its ThreadLocal, and creates a new shared queue if the one at
1520 <     * index if empty or contended. The lock bodies must be
1521 <     * exception-free (so no try/finally) so we optimistically
1522 <     * allocate new queues/arrays outside the locks and throw them
1474 <     * away if (very rarely) not needed. Note that the plock seq value
1475 <     * can eventually wrap around zero, but if so harmlessly fails to
1476 <     * reinitialize.
1516 >     * pool, so must perform secondary initialization (via
1517 >     * initWorkers). It also detects first submission by an external
1518 >     * thread by looking up its ThreadLocal, and creates a new shared
1519 >     * queue if the one at index if empty or contended. The plock lock
1520 >     * body must be exception-free (so no try/finally) so we
1521 >     * optimistically allocate new queues outside the lock and throw
1522 >     * them away if (very rarely) not needed.
1523       */
1524      private void fullExternalPush(ForkJoinTask<?> task) {
1525 <        for (Submitter z = null;;) {
1526 <            WorkQueue[] ws; WorkQueue q; int ps, m, r, s;
1527 <            if ((ps = plock) < 0)
1525 >        int r = 0; // random index seed
1526 >        for (Submitter z = submitters.get();;) {
1527 >            WorkQueue[] ws; WorkQueue q; int ps, m, k;
1528 >            if (z == null) {
1529 >                if (U.compareAndSwapInt(this, INDEXSEED, r = indexSeed,
1530 >                                        r += SEED_INCREMENT) && r != 0)
1531 >                    submitters.set(z = new Submitter(r));
1532 >            }
1533 >            else if (r == 0) {               // move to a different index
1534 >                r = z.seed;
1535 >                r ^= r << 13;                // same xorshift as WorkQueues
1536 >                r ^= r >>> 17;
1537 >                z.seed = r ^ (r << 5);
1538 >            }
1539 >            else if ((ps = plock) < 0)
1540                  throw new RejectedExecutionException();
1541 <            else if ((ws = workQueues) == null || (m = ws.length - 1) < 0) {
1542 <                int n = parallelism - 1; n |= n >>> 1; n |= n >>> 2;
1543 <                n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1544 <                WorkQueue[] nws = new WorkQueue[(n + 1) << 1]; // power of two
1545 <                if ((ps & PL_LOCK) != 0 ||
1541 >            else if (ps == 0 || (ws = workQueues) == null ||
1542 >                     (m = ws.length - 1) < 0)
1543 >                initWorkers();
1544 >            else if ((q = ws[k = r & m & SQMASK]) != null) {
1545 >                if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
1546 >                    ForkJoinTask<?>[] a = q.array;
1547 >                    int s = q.top;
1548 >                    boolean submitted = false;
1549 >                    try {                      // locked version of push
1550 >                        if ((a != null && a.length > s + 1 - q.base) ||
1551 >                            (a = q.growArray()) != null) {   // must presize
1552 >                            int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
1553 >                            U.putOrderedObject(a, j, task);
1554 >                            q.top = s + 1;
1555 >                            submitted = true;
1556 >                        }
1557 >                    } finally {
1558 >                        q.qlock = 0;  // unlock
1559 >                    }
1560 >                    if (submitted) {
1561 >                        signalWork(q);
1562 >                        return;
1563 >                    }
1564 >                }
1565 >                r = 0; // move on failure
1566 >            }
1567 >            else if (((ps = plock) & PL_LOCK) == 0) { // create new queue
1568 >                q = new WorkQueue(this, null, SHARED_QUEUE, r);
1569 >                if (((ps = plock) & PL_LOCK) != 0 ||
1570                      !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1571                      ps = acquirePlock();
1572 <                if ((ws = workQueues) == null)
1573 <                    workQueues = nws;
1572 >                if ((ws = workQueues) != null && k < ws.length && ws[k] == null)
1573 >                    ws[k] = q;
1574                  int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1575                  if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1576                      releasePlock(nps);
1577              }
1578 <            else if (z == null && (z = submitters.get()) == null) {
1579 <                if (U.compareAndSwapInt(this, INDEXSEED,
1498 <                                        s = indexSeed, s += SEED_INCREMENT) &&
1499 <                    s != 0) // skip 0
1500 <                    submitters.set(z = new Submitter(s));
1501 <            }
1502 <            else {
1503 <                int k = (r = z.seed) & m & SQMASK;
1504 <                if ((q = ws[k]) == null && (ps & PL_LOCK) == 0) {
1505 <                    (q = new WorkQueue(this, null, SHARED_QUEUE)).poolIndex = k;
1506 <                    if (((ps = plock) & PL_LOCK) != 0 ||
1507 <                        !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1508 <                        ps = acquirePlock();
1509 <                    WorkQueue w = null;
1510 <                    if ((ws = workQueues) != null && k < ws.length &&
1511 <                        (w = ws[k]) == null)
1512 <                        ws[k] = q;
1513 <                    else
1514 <                        q = w;
1515 <                    int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1516 <                    if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1517 <                        releasePlock(nps);
1518 <                }
1519 <                if (q != null && q.qlock == 0 && q.fullPush(task, false))
1520 <                    return;
1521 <                r ^= r << 13;                // same xorshift as WorkQueues
1522 <                r ^= r >>> 17;
1523 <                z.seed = r ^= r << 5;        // move to a different index
1524 <            }
1578 >            else
1579 >                r = 0; // try elsewhere while lock held
1580          }
1581      }
1582  
# Line 1536 | Line 1591 | public class ForkJoinPool extends Abstra
1591      }
1592  
1593      /**
1594 <     * Tries to create (at most one) or activate (possibly several)
1540 <     * workers if too few are active. On contention failure, continues
1541 <     * until at least one worker is signalled or the given queue is
1542 <     * empty or all workers are active.
1594 >     * Tries to create or activate a worker if too few are active.
1595       *
1596 <     * @param q if non-null, the queue holding tasks to be signalled
1545 <     * @param signals the target number of signals.
1596 >     * @param q the (non-null) queue holding tasks to be signalled
1597       */
1598 <    final void signalWork(WorkQueue q, int signals) {
1599 <        long c; int e, u, i; WorkQueue[] ws; WorkQueue w; Thread p;
1598 >    final void signalWork(WorkQueue q) {
1599 >        int hint = q.poolIndex;
1600 >        long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p;
1601          while ((u = (int)((c = ctl) >>> 32)) < 0) {
1602              if ((e = (int)c) > 0) {
1603                  if ((ws = workQueues) != null && ws.length > (i = e & SMASK) &&
# Line 1553 | Line 1605 | public class ForkJoinPool extends Abstra
1605                      long nc = (((long)(w.nextWait & E_MASK)) |
1606                                 ((long)(u + UAC_UNIT) << 32));
1607                      if (U.compareAndSwapLong(this, CTL, c, nc)) {
1608 +                        w.hint = hint;
1609                          w.eventCount = (e + E_SEQ) & E_MASK;
1610                          if ((p = w.parker) != null)
1611                              U.unpark(p);
1612 <                        if (--signals <= 0)
1560 <                            break;
1612 >                        break;
1613                      }
1614 <                    else
1563 <                        signals = 1;
1564 <                    if ((q != null && q.queueSize() == 0))
1614 >                    if (q.top - q.base <= 0)
1615                          break;
1616                  }
1617                  else
1618                      break;
1619              }
1620 <            else if (e == 0 && (u & SHORT_SIGN) != 0) {
1621 <                long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1622 <                                 ((u + UAC_UNIT) & UAC_MASK)) << 32;
1573 <                if (U.compareAndSwapLong(this, CTL, c, nc)) {
1574 <                    ForkJoinWorkerThread wt = null;
1575 <                    Throwable ex = null;
1576 <                    boolean started = false;
1577 <                    try {
1578 <                        ForkJoinWorkerThreadFactory fac;
1579 <                        if ((fac = factory) != null &&
1580 <                            (wt = fac.newThread(this)) != null) {
1581 <                            wt.start();
1582 <                            started = true;
1583 <                        }
1584 <                    } catch (Throwable rex) {
1585 <                        ex = rex;
1586 <                    }
1587 <                    if (!started)
1588 <                        deregisterWorker(wt, ex); // adjust counts on failure
1589 <                    break;
1590 <                }
1591 <            }
1592 <            else
1620 >            else {
1621 >                if ((short)u < 0)
1622 >                    tryAddWorker();
1623                  break;
1624 +            }
1625          }
1626      }
1627  
# Line 1600 | Line 1631 | public class ForkJoinPool extends Abstra
1631       * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1632       */
1633      final void runWorker(WorkQueue w) {
1634 <        // initialize queue array in this thread
1604 <        w.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY];
1634 >        w.growArray(); // allocate queue
1635          do { w.runTask(scan(w)); } while (w.qlock >= 0);
1636      }
1637  
# Line 1613 | Line 1643 | public class ForkJoinPool extends Abstra
1643       * contention, or state changes that indicate possible success on
1644       * re-invocation.
1645       *
1646 <     * The scan searches for tasks across a random permutation of
1647 <     * queues (starting at a random index and stepping by a random
1648 <     * relative prime, checking each at least once).  The scan
1649 <     * terminates upon either finding a non-empty queue, or completing
1650 <     * the sweep. If the worker is not inactivated, it takes and
1651 <     * returns a task from this queue. Otherwise, if not activated, it
1652 <     * signals workers (that may include itself) and returns so caller
1653 <     * can retry. Also returns for trtry if the worker array may have
1654 <     * changed during an empty scan.  On failure to find a task, we
1655 <     * take one of the following actions, after which the caller will
1656 <     * retry calling this method unless terminated.
1646 >     * The scan searches for tasks across queues (starting at a random
1647 >     * index, and relying on registerWorker to irregularly scatter
1648 >     * them within array to avoid bias), checking each at least twice.
1649 >     * The scan terminates upon either finding a non-empty queue, or
1650 >     * completing the sweep. If the worker is not inactivated, it
1651 >     * takes and returns a task from this queue. Otherwise, if not
1652 >     * activated, it signals workers (that may include itself) and
1653 >     * returns so caller can retry. Also returns for true if the
1654 >     * worker array may have changed during an empty scan.  On failure
1655 >     * to find a task, we take one of the following actions, after
1656 >     * which the caller will retry calling this method unless
1657 >     * terminated.
1658       *
1659       * * If pool is terminating, terminate the worker.
1660       *
# Line 1640 | Line 1671 | public class ForkJoinPool extends Abstra
1671       * @return a task or null if none found
1672       */
1673      private final ForkJoinTask<?> scan(WorkQueue w) {
1674 <        WorkQueue[] ws; WorkQueue q;           // first update random seed
1675 <        int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1676 <        int ps = plock, m;                     // volatile read order matters
1677 <        if ((ws = workQueues) != null && (m = ws.length - 1) > 0) {
1678 <            int ec = w.eventCount;             // ec is negative if inactive
1679 <            int step = (r >>> 16) | 1;         // relatively prime
1680 <            for (int j = (m + 1) << 2;  ; --j, r += step) {
1681 <                ForkJoinTask<?> t; ForkJoinTask<?>[] a; int b, n;
1682 <                if ((q = ws[r & m]) != null && (b = q.base) - q.top < 0 &&
1683 <                    (a = q.array) != null) {   // probably nonempty
1674 >        WorkQueue[] ws; int m;
1675 >        int ps = plock;                          // read plock before ws
1676 >        if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1677 >            int ec = w.eventCount;               // ec is negative if inactive
1678 >            int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1679 >            int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN;
1680 >            do {
1681 >                WorkQueue q; ForkJoinTask<?>[] a; int b;
1682 >                if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 &&
1683 >                    (a = q.array) != null) {     // probably nonempty
1684                      int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
1685 <                    t = (ForkJoinTask<?>)U.getObjectVolatile(a, i);
1685 >                    ForkJoinTask<?> t = (ForkJoinTask<?>)
1686 >                        U.getObjectVolatile(a, i);
1687                      if (q.base == b && ec >= 0 && t != null &&
1688                          U.compareAndSwapObject(a, i, t, null)) {
1689 <                        if ((n = q.top - (q.base = b + 1)) > 0)
1690 <                            signalWork(q, n);
1691 <                        return t;              // taken
1692 <                    }
1693 <                    if (j < m || (ec < 0 && (ec = w.eventCount) < 0)) {
1694 <                        if ((n = q.queueSize() - 1) > 0)
1695 <                            signalWork(q, n);
1696 <                        break;                 // let caller retry after signal
1697 <                    }
1698 <                }
1699 <                else if (j < 0) {              // end of scan
1700 <                    long c = ctl; int e;
1701 <                    if (plock != ps)           // incomplete sweep
1702 <                        break;
1703 <                    if ((e = (int)c) < 0)      // pool is terminating
1704 <                        w.qlock = -1;
1705 <                    else if (ec >= 0) {        // try to enqueue/inactivate
1706 <                        long nc = ((long)ec |
1707 <                                   ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1708 <                        w.nextWait = e;
1709 <                        w.eventCount = ec | INT_SIGN; // mark as inactive
1710 <                        if (ctl != c ||
1711 <                            !U.compareAndSwapLong(this, CTL, c, nc))
1712 <                            w.eventCount = ec; // unmark on CAS failure
1713 <                        else if ((int)(c >> AC_SHIFT) == 1 - parallelism)
1714 <                            idleAwaitWork(w, nc, c);  // quiescent
1715 <                    }
1716 <                    else if (w.seed >= 0 && w.eventCount < 0) {
1717 <                        Thread wt = Thread.currentThread();
1718 <                        Thread.interrupted();  // clear status
1719 <                        U.putObject(wt, PARKBLOCKER, this);
1720 <                        w.parker = wt;         // emulate LockSupport.park
1721 <                        if (w.eventCount < 0)  // recheck
1722 <                            U.park(false, 0L);
1723 <                        w.parker = null;
1724 <                        U.putObject(wt, PARKBLOCKER, null);
1725 <                    }
1726 <                    break;
1727 <                }
1689 >                        if ((q.base = b + 1) - q.top < 0)
1690 >                            signalWork(q);
1691 >                        return t;                // taken
1692 >                    }
1693 >                    else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) {
1694 >                        w.hint = (r + j) & m;    // help signal below
1695 >                        break;                   // cannot take
1696 >                    }
1697 >                }
1698 >            } while (--j >= 0);
1699 >
1700 >            long c, sc; int e, ns, h;
1701 >            if ((h = w.hint) < 0) {
1702 >                if ((ns = w.nsteals) != 0) {
1703 >                    if (U.compareAndSwapLong(this, STEALCOUNT,
1704 >                                             sc = stealCount, sc + ns))
1705 >                        w.nsteals = 0;           // collect steals
1706 >                }
1707 >                else if (plock != ps)            // consistency check
1708 >                    ;                            // skip
1709 >                else if ((e = (int)(c = ctl)) < 0)
1710 >                    w.qlock = -1;                // pool is terminating
1711 >                else if (ec >= 0) {              // try to enqueue/inactivate
1712 >                    long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1713 >                    w.nextWait = e;              // link and mark inactive
1714 >                    w.eventCount = ec | INT_SIGN;
1715 >                    if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1716 >                        w.eventCount = ec;       // unmark on CAS failure
1717 >                    else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1718 >                        idleAwaitWork(w, nc, c);
1719 >                }
1720 >                else if (w.eventCount < 0) {     // block
1721 >                    Thread wt = Thread.currentThread();
1722 >                    Thread.interrupted();        // clear status
1723 >                    U.putObject(wt, PARKBLOCKER, this);
1724 >                    w.parker = wt;               // emulate LockSupport.park
1725 >                    if (w.eventCount < 0)        // recheck
1726 >                        U.park(false, 0L);
1727 >                    w.parker = null;
1728 >                    U.putObject(wt, PARKBLOCKER, null);
1729 >                }
1730 >            }
1731 >            if (h >= 0 || (h = w.hint) >= 0) {   // signal others before retry
1732 >                w.hint = -1;                     // reset
1733 >                helpSignal(null, h, true);
1734              }
1735          }
1736          return null;
# Line 1711 | Line 1749 | public class ForkJoinPool extends Abstra
1749       * @param prevCtl the ctl value to restore if thread is terminated
1750       */
1751      private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) {
1752 <        if (w.eventCount < 0 &&
1753 <            (this == commonPool || !tryTerminate(false, false)) &&
1716 <            (int)prevCtl != 0) {
1752 >        if (w != null && w.eventCount < 0 &&
1753 >            !tryTerminate(false, false) && (int)prevCtl != 0) {
1754              int dc = -(short)(currentCtl >>> TC_SHIFT);
1755              long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1756              long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
# Line 1739 | Line 1776 | public class ForkJoinPool extends Abstra
1776      }
1777  
1778      /**
1779 <     * Scans through queues looking for work while joining a task;
1780 <     * if any are present, signals.
1779 >     * Scans through queues looking for work (optionally, while
1780 >     * joining a task); if any present, signals. May return early if
1781 >     * more signalling is detectably unneeded.
1782       *
1783 <     * @param task to return early if done
1783 >     * @param task if non-null, return early if done
1784       * @param origin an index to start scan
1785 +     * @param once if only the origin should be checked
1786       */
1787 <    final int helpSignal(ForkJoinTask<?> task, int origin) {
1788 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1789 <        if (task != null && (ws = workQueues) != null &&
1790 <            (m = ws.length - 1) >= 0) {
1791 <            for (int i = 0; i <= m; ++i) {
1792 <                if ((s = task.status) < 0)
1793 <                    return s;
1794 <                if ((q = ws[(i + origin) & m]) != null &&
1795 <                    (n = q.queueSize()) > 0) {
1796 <                    signalWork(q, n);
1797 <                    if ((int)(ctl >> AC_SHIFT) >= 0)
1787 >    private void helpSignal(ForkJoinTask<?> task, int origin, boolean once) {
1788 >        WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s;
1789 >        if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1790 >            (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1791 >            outer: for (int k = origin, j = once ? 0 : m; j >= 0; --j) {
1792 >                WorkQueue q = ws[k++ & m];
1793 >                for (int n = m;;) { // limit to at most m signals
1794 >                    if (task != null && task.status < 0)
1795 >                        break outer;
1796 >                    if (q == null ||
1797 >                        ((s = (task == null ? -1 : 0) - q.base + q.top) <= n &&
1798 >                         (n = s) <= 0))
1799                          break;
1800 +                    if ((u = (int)((c = ctl) >>> 32)) >= 0 ||
1801 +                        (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1802 +                        (w = ws[i]) == null)
1803 +                        break outer;
1804 +                    long nc = (((long)(w.nextWait & E_MASK)) |
1805 +                               ((long)(u + UAC_UNIT) << 32));
1806 +                    if (w.eventCount == (e | INT_SIGN) &&
1807 +                        U.compareAndSwapLong(this, CTL, c, nc)) {
1808 +                        w.eventCount = (e + E_SEQ) & E_MASK;
1809 +                        if ((p = w.parker) != null)
1810 +                            U.unpark(p);
1811 +                        if (--n <= 0)
1812 +                            break;
1813 +                    }
1814                  }
1815              }
1816          }
1763        return 0;
1817      }
1818  
1819      /**
# Line 1794 | Line 1847 | public class ForkJoinPool extends Abstra
1847                      }
1848                      if ((ws = workQueues) == null || (m = ws.length - 1) <= 0)
1849                          break restart;              // shutting down
1850 <                    if ((v = ws[h = (j.stealHint | 1) & m]) == null ||
1850 >                    if ((v = ws[h = (j.hint | 1) & m]) == null ||
1851                          v.currentSteal != subtask) {
1852                          for (int origin = h;;) {    // find stealer
1853                              if (((h = (h + 2) & m) & 15) == 1 &&
# Line 1802 | Line 1855 | public class ForkJoinPool extends Abstra
1855                                  continue restart;   // occasional staleness check
1856                              if ((v = ws[h]) != null &&
1857                                  v.currentSteal == subtask) {
1858 <                                j.stealHint = h;    // save hint
1858 >                                j.hint = h;        // save hint
1859                                  break;
1860                              }
1861                              if (h == origin)
# Line 1851 | Line 1904 | public class ForkJoinPool extends Abstra
1904  
1905      /**
1906       * Analog of tryHelpStealer for CountedCompleters. Tries to steal
1907 <     * and run tasks within the target's computation
1907 >     * and run tasks within the target's computation.
1908       *
1909       * @param task the task to join
1910       * @param mode if shared, exit upon completing any task
# Line 1859 | Line 1912 | public class ForkJoinPool extends Abstra
1912       *
1913       */
1914      private int helpComplete(ForkJoinTask<?> task, int mode) {
1915 <        WorkQueue[] ws; WorkQueue q; int m, n, s;
1915 >        WorkQueue[] ws; WorkQueue q; int m, n, s, u;
1916          if (task != null && (ws = workQueues) != null &&
1917              (m = ws.length - 1) >= 0) {
1918              for (int j = 1, origin = j;;) {
# Line 1867 | Line 1920 | public class ForkJoinPool extends Abstra
1920                      return s;
1921                  if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) {
1922                      origin = j;
1923 <                    if (mode == SHARED_QUEUE && (int)(ctl >> AC_SHIFT) >= 0)
1923 >                    if (mode == SHARED_QUEUE &&
1924 >                        ((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0))
1925                          break;
1926                  }
1927                  else if ((j = (j + 2) & m) == origin)
# Line 1885 | Line 1939 | public class ForkJoinPool extends Abstra
1939       * may become starved.
1940       */
1941      final boolean tryCompensate() {
1942 <        int pc = parallelism, e, u, i, tc; long c;
1942 >        int pc = config & SMASK, e, i, tc; long c;
1943          WorkQueue[] ws; WorkQueue w; Thread p;
1944 <        if ((e = (int)(c = ctl)) >= 0 && (ws = workQueues) != null) {
1944 >        if ((ws = workQueues) != null && (e = (int)(c = ctl)) >= 0) {
1945              if (e != 0 && (i = e & SMASK) < ws.length &&
1946                  (w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) {
1947                  long nc = ((long)(w.nextWait & E_MASK) |
# Line 1899 | Line 1953 | public class ForkJoinPool extends Abstra
1953                      return true;   // replace with idle worker
1954                  }
1955              }
1956 <            else if ((short)((u = (int)(c >>> 32)) >>> UTC_SHIFT) >= 0 &&
1957 <                     (u >> UAC_SHIFT) + pc > 1) {
1956 >            else if ((tc = (short)(c >>> TC_SHIFT)) >= 0 &&
1957 >                     (int)(c >> AC_SHIFT) + pc > 1) {
1958                  long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK);
1959                  if (U.compareAndSwapLong(this, CTL, c, nc))
1960 <                    return true;    // no compensation
1960 >                    return true;   // no compensation
1961              }
1962 <            else if ((tc = u + pc) < MAX_CAP) {
1962 >            else if (tc + pc < MAX_CAP) {
1963                  long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
1964                  if (U.compareAndSwapLong(this, CTL, c, nc)) {
1965 +                    ForkJoinWorkerThreadFactory fac;
1966                      Throwable ex = null;
1967                      ForkJoinWorkerThread wt = null;
1968                      try {
1914                        ForkJoinWorkerThreadFactory fac;
1969                          if ((fac = factory) != null &&
1970                              (wt = fac.newThread(this)) != null) {
1971                              wt.start();
# Line 1920 | Line 1974 | public class ForkJoinPool extends Abstra
1974                      } catch (Throwable rex) {
1975                          ex = rex;
1976                      }
1977 <                    deregisterWorker(wt, ex); // adjust counts etc
1977 >                    deregisterWorker(wt, ex); // clean up and return false
1978                  }
1979              }
1980          }
# Line 1939 | Line 1993 | public class ForkJoinPool extends Abstra
1993          if (joiner != null && task != null && (s = task.status) >= 0) {
1994              ForkJoinTask<?> prevJoin = joiner.currentJoin;
1995              joiner.currentJoin = task;
1996 <            do {} while ((s = task.status) >= 0 &&
1943 <                         joiner.queueSize() > 0 &&
1996 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
1997                           joiner.tryRemoveAndExec(task)); // process local tasks
1998 <            if (s >= 0 && (s = task.status) >= 0 &&
1999 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2000 <                (task instanceof CountedCompleter))
2001 <                s = helpComplete(task, LIFO_QUEUE);
1998 >            if (s >= 0 && (s = task.status) >= 0) {
1999 >                helpSignal(task, joiner.poolIndex, false);
2000 >                if ((s = task.status) >= 0 &&
2001 >                    (task instanceof CountedCompleter))
2002 >                    s = helpComplete(task, LIFO_QUEUE);
2003 >            }
2004              while (s >= 0 && (s = task.status) >= 0) {
2005 <                if ((joiner.queueSize() > 0 ||           // try helping
2005 >                if ((!joiner.isEmpty() ||           // try helping
2006                       (s = tryHelpStealer(joiner, task)) == 0) &&
2007 <                    (s = task.status) >= 0 && tryCompensate()) {
2008 <                    if (task.trySetSignal() && (s = task.status) >= 0) {
2009 <                        synchronized (task) {
2010 <                            if (task.status >= 0) {
2011 <                                try {                // see ForkJoinTask
2012 <                                    task.wait();     //  for explanation
2013 <                                } catch (InterruptedException ie) {
2007 >                    (s = task.status) >= 0) {
2008 >                    helpSignal(task, joiner.poolIndex, false);
2009 >                    if ((s = task.status) >= 0 && tryCompensate()) {
2010 >                        if (task.trySetSignal() && (s = task.status) >= 0) {
2011 >                            synchronized (task) {
2012 >                                if (task.status >= 0) {
2013 >                                    try {                // see ForkJoinTask
2014 >                                        task.wait();     //  for explanation
2015 >                                    } catch (InterruptedException ie) {
2016 >                                    }
2017                                  }
2018 +                                else
2019 +                                    task.notifyAll();
2020                              }
1961                            else
1962                                task.notifyAll();
2021                          }
2022 +                        long c;                          // re-activate
2023 +                        do {} while (!U.compareAndSwapLong
2024 +                                     (this, CTL, c = ctl, c + AC_UNIT));
2025                      }
1965                    long c;                          // re-activate
1966                    do {} while (!U.compareAndSwapLong
1967                                 (this, CTL, c = ctl, c + AC_UNIT));
2026                  }
2027              }
2028              joiner.currentJoin = prevJoin;
# Line 1985 | Line 2043 | public class ForkJoinPool extends Abstra
2043          if (joiner != null && task != null && (s = task.status) >= 0) {
2044              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2045              joiner.currentJoin = task;
2046 <            do {} while ((s = task.status) >= 0 &&
1989 <                         joiner.queueSize() > 0 &&
2046 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2047                           joiner.tryRemoveAndExec(task));
2048 <            if (s >= 0 && (s = task.status) >= 0 &&
2049 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2050 <                (task instanceof CountedCompleter))
2051 <                s = helpComplete(task, LIFO_QUEUE);
2052 <            if (s >= 0 && joiner.queueSize() == 0) {
2048 >            if (s >= 0 && (s = task.status) >= 0) {
2049 >                helpSignal(task, joiner.poolIndex, false);
2050 >                if ((s = task.status) >= 0 &&
2051 >                    (task instanceof CountedCompleter))
2052 >                    s = helpComplete(task, LIFO_QUEUE);
2053 >            }
2054 >            if (s >= 0 && joiner.isEmpty()) {
2055                  do {} while (task.status >= 0 &&
2056                               tryHelpStealer(joiner, task) > 0);
2057              }
# Line 2008 | Line 2067 | public class ForkJoinPool extends Abstra
2067       * @param r a (random) seed for scanning
2068       */
2069      private WorkQueue findNonEmptyStealQueue(int r) {
2011        int step = (r >>> 16) | 1;
2070          for (WorkQueue[] ws;;) {
2071 <            int ps = plock, m;
2071 >            int ps = plock, m, n;
2072              if ((ws = workQueues) == null || (m = ws.length - 1) < 1)
2073                  return null;
2074 <            for (int j = (m + 1) << 2; ; r += step) {
2075 <                WorkQueue q = ws[((r << 1) | 1) & m];
2076 <                if (q != null && q.queueSize() > 0)
2074 >            for (int j = (m + 1) << 2; ;) {
2075 >                WorkQueue q = ws[(((r + j) << 1) | 1) & m];
2076 >                if (q != null && (n = q.base - q.top) < 0) {
2077 >                    if (n < -1)
2078 >                        signalWork(q);
2079                      return q;
2080 +                }
2081                  else if (--j < 0) {
2082                      if (plock == ps)
2083                          return null;
# Line 2059 | Line 2120 | public class ForkJoinPool extends Abstra
2120                  }
2121                  else
2122                      c = ctl;        // re-increment on exit
2123 <                if ((int)(c >> AC_SHIFT) + parallelism == 0) {
2123 >                if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) {
2124                      do {} while (!U.compareAndSwapLong
2125                                   (this, CTL, c = ctl, c + AC_UNIT));
2126                      break;
# Line 2134 | Line 2195 | public class ForkJoinPool extends Abstra
2195      static int getSurplusQueuedTaskCount() {
2196          Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q;
2197          if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) {
2198 <            int b = (q = (wt = (ForkJoinWorkerThread)t).workQueue).base;
2199 <            int p = (pool = wt.pool).parallelism;
2198 >            int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).config & SMASK;
2199 >            int n = (q = wt.workQueue).top - q.base;
2200              int a = (int)(pool.ctl >> AC_SHIFT) + p;
2201 <            return q.top - b - (a > (p >>>= 1) ? 0 :
2202 <                                a > (p >>>= 1) ? 1 :
2203 <                                a > (p >>>= 1) ? 2 :
2204 <                                a > (p >>>= 1) ? 4 :
2205 <                                8);
2201 >            return n - (a > (p >>>= 1) ? 0 :
2202 >                        a > (p >>>= 1) ? 1 :
2203 >                        a > (p >>>= 1) ? 2 :
2204 >                        a > (p >>>= 1) ? 4 :
2205 >                        8);
2206          }
2207          return 0;
2208      }
# Line 2167 | Line 2228 | public class ForkJoinPool extends Abstra
2228              return false;
2229          for (long c;;) {
2230              if (((c = ctl) & STOP_BIT) != 0) {      // already terminating
2231 <                if ((short)(c >>> TC_SHIFT) == -parallelism) {
2231 >                if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) {
2232                      synchronized (this) {
2233                          notifyAll();                // signal when 0 workers
2234                      }
# Line 2186 | Line 2247 | public class ForkJoinPool extends Abstra
2247                      releasePlock(nps);
2248              }
2249              if (!now) {                             // check if idle & no tasks
2250 <                if ((int)(c >> AC_SHIFT) != -parallelism ||
2250 >                if ((int)(c >> AC_SHIFT) != -(config & SMASK) ||
2251                      hasQueuedSubmissions())
2252                      return false;
2253                  // Check for unqueued inactive workers. One pass suffices.
# Line 2256 | Line 2317 | public class ForkJoinPool extends Abstra
2317       */
2318      static boolean tryExternalUnpush(ForkJoinTask<?> t) {
2319          ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z;
2320 <        ForkJoinTask<?>[] a;  int m, s; long j;
2321 <        if ((z = submitters.get()) != null &&
2320 >        ForkJoinTask<?>[] a;  int m, s;
2321 >        if (t != null &&
2322 >            (z = submitters.get()) != null &&
2323              (p = commonPool) != null &&
2324              (ws = p.workQueues) != null &&
2325              (m = ws.length - 1) >= 0 &&
2326              (q = ws[m & z.seed & SQMASK]) != null &&
2327              (s = q.top) != q.base &&
2328 <            (a = q.array) != null &&
2329 <            U.getObjectVolatile
2330 <            (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2331 <            U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2332 <            if (q.array == a && q.top == s && // recheck
2333 <                U.compareAndSwapObject(a, j, t, null)) {
2334 <                q.top = s - 1;
2328 >            (a = q.array) != null) {
2329 >            long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
2330 >            if (U.getObject(a, j) == t &&
2331 >                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2332 >                if (q.array == a && q.top == s && // recheck
2333 >                    U.compareAndSwapObject(a, j, t, null)) {
2334 >                    q.top = s - 1;
2335 >                    q.qlock = 0;
2336 >                    return true;
2337 >                }
2338                  q.qlock = 0;
2274                return true;
2339              }
2276            q.qlock = 0;
2340          }
2341          return false;
2342      }
# Line 2288 | Line 2351 | public class ForkJoinPool extends Abstra
2351          if (q != null && (a = q.array) != null && (m = (a.length - 1)) >= 0 &&
2352              root != null && root.status >= 0) {
2353              for (;;) {
2354 <                int s; Object o; CountedCompleter<?> task = null;
2354 >                int s, u; Object o; CountedCompleter<?> task = null;
2355                  if ((s = q.top) - q.base > 0) {
2356                      long j = ((m & (s - 1)) << ASHIFT) + ABASE;
2357                      if ((o = U.getObject(a, j)) != null &&
# Line 2311 | Line 2374 | public class ForkJoinPool extends Abstra
2374                  }
2375                  if (task != null)
2376                      task.doExec();
2377 <                if (root.status < 0 || (int)(ctl >> AC_SHIFT) >= 0)
2377 >                if (root.status < 0 ||
2378 >                    (u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
2379                      break;
2380                  if (task == null) {
2381 <                    if (helpSignal(root, q.poolIndex) >= 0)
2381 >                    helpSignal(root, q.poolIndex, false);
2382 >                    if (root.status >= 0)
2383                          helpComplete(root, SHARED_QUEUE);
2384                      break;
2385                  }
# Line 2329 | Line 2394 | public class ForkJoinPool extends Abstra
2394      static void externalHelpJoin(ForkJoinTask<?> t) {
2395          // Some hard-to-avoid overlap with tryExternalUnpush
2396          ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z;
2397 <        ForkJoinTask<?>[] a;  int m, s, n; long j;
2398 <        if (t != null && t.status >= 0 &&
2397 >        ForkJoinTask<?>[] a;  int m, s, n;
2398 >        if (t != null &&
2399              (z = submitters.get()) != null &&
2400              (p = commonPool) != null &&
2401              (ws = p.workQueues) != null &&
2402              (m = ws.length - 1) >= 0 &&
2403              (q = ws[m & z.seed & SQMASK]) != null &&
2404              (a = q.array) != null) {
2405 <            if ((s = q.top) != q.base &&
2406 <                U.getObjectVolatile
2407 <                (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2408 <                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2409 <                if (q.array == a && q.top == s &&
2410 <                    U.compareAndSwapObject(a, j, t, null)) {
2411 <                    q.top = s - 1;
2412 <                    q.qlock = 0;
2413 <                    t.doExec();
2405 >            int am = a.length - 1;
2406 >            if ((s = q.top) != q.base) {
2407 >                long j = ((am & (s - 1)) << ASHIFT) + ABASE;
2408 >                if (U.getObject(a, j) == t &&
2409 >                    U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2410 >                    if (q.array == a && q.top == s &&
2411 >                        U.compareAndSwapObject(a, j, t, null)) {
2412 >                        q.top = s - 1;
2413 >                        q.qlock = 0;
2414 >                        t.doExec();
2415 >                    }
2416 >                    else
2417 >                        q.qlock = 0;
2418                  }
2350                else
2351                    q.qlock = 0;
2419              }
2420              if (t.status >= 0) {
2421                  if (t instanceof CountedCompleter)
2422                      p.externalHelpComplete(q, t);
2423                  else
2424 <                    p.helpSignal(t, q.poolIndex);
2424 >                    p.helpSignal(t, q.poolIndex, false);
2425              }
2426          }
2427      }
# Line 2364 | Line 2431 | public class ForkJoinPool extends Abstra
2431       */
2432      static void externalHelpQuiescePool() {
2433          ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b;
2367        int r = ThreadLocalRandom.current().nextInt();
2434          if ((p = commonPool) != null &&
2435 <            (q = p.findNonEmptyStealQueue(r)) != null &&
2435 >            (q = p.findNonEmptyStealQueue(1)) != null &&
2436              (b = q.base) - q.top < 0 &&
2437              (t = q.pollAt(b)) != null)
2438              t.doExec();
# Line 2443 | Line 2509 | public class ForkJoinPool extends Abstra
2509              throw new NullPointerException();
2510          if (parallelism <= 0 || parallelism > MAX_CAP)
2511              throw new IllegalArgumentException();
2446        this.parallelism = parallelism;
2512          this.factory = factory;
2513          this.ueh = handler;
2514 <        this.localMode = asyncMode ? FIFO_QUEUE : LIFO_QUEUE;
2514 >        this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0);
2515          long np = (long)(-parallelism); // offset ctl counts
2516          this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2517          int pn = nextPoolId();
# Line 2463 | Line 2528 | public class ForkJoinPool extends Abstra
2528      ForkJoinPool(int parallelism, long ctl,
2529                   ForkJoinWorkerThreadFactory factory,
2530                   Thread.UncaughtExceptionHandler handler) {
2531 <        this.parallelism = parallelism;
2531 >        this.config = parallelism;
2532          this.ctl = ctl;
2533          this.factory = factory;
2534          this.ueh = handler;
2470        this.localMode = LIFO_QUEUE;
2535          this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
2536      }
2537  
# Line 2477 | Line 2541 | public class ForkJoinPool extends Abstra
2541       * @return the common pool instance
2542       */
2543      public static ForkJoinPool commonPool() {
2544 <        return commonPool; // cannot be null (if so, a static init error)
2544 >        // assert commonPool != null : "static init error";
2545 >        return commonPool;
2546      }
2547  
2548      // Execution methods
# Line 2649 | Line 2714 | public class ForkJoinPool extends Abstra
2714       * @return the targeted parallelism level of this pool
2715       */
2716      public int getParallelism() {
2717 <        return parallelism;
2717 >        return config & SMASK;
2718      }
2719  
2720      /**
# Line 2670 | Line 2735 | public class ForkJoinPool extends Abstra
2735       * @return the number of worker threads
2736       */
2737      public int getPoolSize() {
2738 <        return parallelism + (short)(ctl >>> TC_SHIFT);
2738 >        return (config & SMASK) + (short)(ctl >>> TC_SHIFT);
2739      }
2740  
2741      /**
# Line 2680 | Line 2745 | public class ForkJoinPool extends Abstra
2745       * @return {@code true} if this pool uses async mode
2746       */
2747      public boolean getAsyncMode() {
2748 <        return localMode != 0;
2748 >        return (config >>> 16) == FIFO_QUEUE;
2749      }
2750  
2751      /**
# Line 2711 | Line 2776 | public class ForkJoinPool extends Abstra
2776       * @return the number of active threads
2777       */
2778      public int getActiveThreadCount() {
2779 <        int r = parallelism + (int)(ctl >> AC_SHIFT);
2779 >        int r = (config & SMASK) + (int)(ctl >> AC_SHIFT);
2780          return (r <= 0) ? 0 : r; // suppress momentarily negative values
2781      }
2782  
# Line 2727 | Line 2792 | public class ForkJoinPool extends Abstra
2792       * @return {@code true} if all threads are currently idle
2793       */
2794      public boolean isQuiescent() {
2795 <        return (int)(ctl >> AC_SHIFT) + parallelism == 0;
2795 >        return (int)(ctl >> AC_SHIFT) + (config & SMASK) == 0;
2796      }
2797  
2798      /**
# Line 2804 | Line 2869 | public class ForkJoinPool extends Abstra
2869          WorkQueue[] ws; WorkQueue w;
2870          if ((ws = workQueues) != null) {
2871              for (int i = 0; i < ws.length; i += 2) {
2872 <                if ((w = ws[i]) != null && w.queueSize() != 0)
2872 >                if ((w = ws[i]) != null && !w.isEmpty())
2873                      return true;
2874              }
2875          }
# Line 2890 | Line 2955 | public class ForkJoinPool extends Abstra
2955                  }
2956              }
2957          }
2958 <        int pc = parallelism;
2958 >        int pc = (config & SMASK);
2959          int tc = pc + (short)(c >>> TC_SHIFT);
2960          int ac = pc + (int)(c >> AC_SHIFT);
2961          if (ac < 0) // ignore transient negative
# Line 2963 | Line 3028 | public class ForkJoinPool extends Abstra
3028      public boolean isTerminated() {
3029          long c = ctl;
3030          return ((c & STOP_BIT) != 0L &&
3031 <                (short)(c >>> TC_SHIFT) == -parallelism);
3031 >                (short)(c >>> TC_SHIFT) == -(config & SMASK));
3032      }
3033  
3034      /**
# Line 2982 | Line 3047 | public class ForkJoinPool extends Abstra
3047      public boolean isTerminating() {
3048          long c = ctl;
3049          return ((c & STOP_BIT) != 0L &&
3050 <                (short)(c >>> TC_SHIFT) != -parallelism);
3050 >                (short)(c >>> TC_SHIFT) != -(config & SMASK));
3051      }
3052  
3053      /**
# Line 3126 | Line 3191 | public class ForkJoinPool extends Abstra
3191          if (t instanceof ForkJoinWorkerThread) {
3192              ForkJoinPool p = ((ForkJoinWorkerThread)t).pool;
3193              while (!blocker.isReleasable()) { // variant of helpSignal
3194 <                WorkQueue[] ws; WorkQueue q; int m, n;
3194 >                WorkQueue[] ws; WorkQueue q; int m, u;
3195                  if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) {
3196                      for (int i = 0; i <= m; ++i) {
3197                          if (blocker.isReleasable())
3198                              return;
3199 <                        if ((q = ws[i]) != null && (n = q.queueSize()) > 0) {
3200 <                            p.signalWork(q, n);
3201 <                            if ((int)(p.ctl >> AC_SHIFT) >= 0)
3199 >                        if ((q = ws[i]) != null && q.base - q.top < 0) {
3200 >                            p.signalWork(q);
3201 >                            if ((u = (int)(p.ctl >>> 32)) >= 0 ||
3202 >                                (u >> UAC_SHIFT) >= 0)
3203                                  break;
3204                          }
3205                      }
# Line 3179 | Line 3245 | public class ForkJoinPool extends Abstra
3245      private static final long QLOCK;
3246  
3247      static {
3182        // Establish common pool parameters
3183        // TBD: limit or report ignored exceptions?
3184
3185        int par = 0;
3186        ForkJoinWorkerThreadFactory fac = null;
3187        Thread.UncaughtExceptionHandler handler = null;
3188        try {
3189            String pp = System.getProperty(propPrefix + "parallelism");
3190            String hp = System.getProperty(propPrefix + "exceptionHandler");
3191            String fp = System.getProperty(propPrefix + "threadFactory");
3192            if (fp != null)
3193                fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
3194                       getSystemClassLoader().loadClass(fp).newInstance());
3195            if (hp != null)
3196                handler = ((Thread.UncaughtExceptionHandler)ClassLoader.
3197                           getSystemClassLoader().loadClass(hp).newInstance());
3198            if (pp != null)
3199                par = Integer.parseInt(pp);
3200        } catch (Exception ignore) {
3201        }
3202
3248          int s; // initialize field offsets for CAS etc
3249          try {
3250              U = getUnsafe();
# Line 3228 | Line 3273 | public class ForkJoinPool extends Abstra
3273          if ((s & (s-1)) != 0)
3274              throw new Error("data type scale not a power of two");
3275  
3276 +        submitters = new ThreadLocal<Submitter>();
3277 +        ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory =
3278 +            new DefaultForkJoinWorkerThreadFactory();
3279 +        modifyThreadPermission = new RuntimePermission("modifyThread");
3280 +
3281          /*
3282 <         * For extra caution, computations to set up pool state are
3283 <         * here; the constructor just assigns these values to fields.
3282 >         * Establish common pool parameters.  For extra caution,
3283 >         * computations to set up common pool state are here; the
3284 >         * constructor just assigns these values to fields.
3285           */
3286 <        ForkJoinWorkerThreadFactory defaultFac =
3287 <            defaultForkJoinWorkerThreadFactory =
3288 <            new DefaultForkJoinWorkerThreadFactory();
3289 <        if (fac == null)
3290 <            fac = defaultFac;
3286 >
3287 >        int par = 0;
3288 >        Thread.UncaughtExceptionHandler handler = null;
3289 >        try {  // TBD: limit or report ignored exceptions?
3290 >            String pp = System.getProperty
3291 >                ("java.util.concurrent.ForkJoinPool.common.parallelism");
3292 >            String hp = System.getProperty
3293 >                ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
3294 >            String fp = System.getProperty
3295 >                ("java.util.concurrent.ForkJoinPool.common.threadFactory");
3296 >            if (fp != null)
3297 >                fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
3298 >                       getSystemClassLoader().loadClass(fp).newInstance());
3299 >            if (hp != null)
3300 >                handler = ((Thread.UncaughtExceptionHandler)ClassLoader.
3301 >                           getSystemClassLoader().loadClass(hp).newInstance());
3302 >            if (pp != null)
3303 >                par = Integer.parseInt(pp);
3304 >        } catch (Exception ignore) {
3305 >        }
3306 >
3307          if (par <= 0)
3308              par = Runtime.getRuntime().availableProcessors();
3309          if (par > MAX_CAP)
3310              par = MAX_CAP;
3311 +        commonPoolParallelism = par;
3312          long np = (long)(-par); // precompute initial ctl value
3313          long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
3314  
3247        commonPoolParallelism = par;
3315          commonPool = new ForkJoinPool(par, ct, fac, handler);
3249        modifyThreadPermission = new RuntimePermission("modifyThread");
3250        submitters = new ThreadLocal<Submitter>();
3316      }
3317  
3318      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines