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

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.146 by dl, Mon Nov 19 18:12:42 2012 UTC vs.
Revision 1.164 by dl, Tue Dec 18 21:46:16 2012 UTC

# Line 51 | 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 99 | 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 <<<<<<< ForkJoinPool.java
107 * exceptionHandler} -- the class name of a {@code
108 =======
106   * exceptionHandler} -- the class name of a {@link
107   * java.lang.Thread.UncaughtExceptionHandler
111 >>>>>>> 1.111
108   * Thread.UncaughtExceptionHandler}. Upon any error in establishing
109   * these settings, default parameters are used.
110   *
# Line 320 | 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
321 <     * creation of new ones if fewer than the given parallelism
322 <     * level).  These primary signals are buttressed by signals
323 <     * whenever other threads scan for work or do not have a task to
324 <     * process (including the case of leaving a hint to unparked
325 <     * threads to help signal others upon wakeup).  On most platforms,
326 <     * signalling (unpark) overhead time is noticeably long, and the
327 <     * time between signalling a thread and it actually making
328 <     * progress can be very noticeably long, so it is worth offloading
329 <     * these delays from critical paths as much as possible.
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 >     * -- 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 538 | Line 535 | public class ForkJoinPool extends Abstra
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 603 | 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.)
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.
627       */
628      static final class WorkQueue {
629          /**
# Line 626 | 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
# Line 643 | Line 666 | public class ForkJoinPool extends Abstra
666          volatile ForkJoinTask<?> currentJoin;  // task being joined in awaitJoin
667          ForkJoinTask<?> currentSteal; // current non-local task being executed
668  
669 <        // Heuristic padding to ameliorate unfortunate memory placements
670 <        Object p00, p01, p02, p03, p04, p05, p06, p07;
648 <        Object p08, p09, p0a, p0b, p0c;
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) {
652            this.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY];
674              this.pool = pool;
675              this.owner = owner;
676              this.mode = mode;
677              this.seed = seed;
678 <            // Place indices in the center of array
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.
684 <         * Cases needing resizing or rejection are relayed to fullPush
685 <         * (that also handles shared queues).
683 >         * Returns the approximate number of tasks in the queue.
684 >         */
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. 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           * @throw RejectedExecutionException if array cannot be resized
# Line 670 | Line 714 | public class ForkJoinPool extends Abstra
714              ForkJoinTask<?>[] a; ForkJoinPool p;
715              int s = top, m, n;
716              if ((a = array) != null) {    // ignore if queue removed
717 <                U.putOrderedObject
718 <                    (a, (((m = a.length - 1) & s) << ASHIFT) + ABASE, task);
719 <                if ((n = (top = s + 1) - base) <= 1) {
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, 0);
721 >                        p.signalWork(this);
722                  }
723                  else if (n >= m)
724                      growArray();
725              }
726          }
727  
684        /**
685         * Pushes a task if lock is free and array is either big
686         * enough or can be resized to be big enough.
687         *
688         * @param task the task. Caller must ensure non-null.
689         * @return true if submitted
690         */
691        final boolean trySharedPush(ForkJoinTask<?> task) {
692            boolean submitted = false;
693            if (qlock == 0 && U.compareAndSwapInt(this, QLOCK, 0, 1)) {
694                ForkJoinTask<?>[] a = array;  ForkJoinPool p;
695                int s = top;
696                try {
697                    if ((a != null && a.length > s + 1 - base) ||
698                        (a = growArray()) != null) {   // must presize
699                        int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
700                        U.putOrderedObject(a, j, task);
701                        top = s + 1;
702                        submitted = true;
703                    }
704                } finally {
705                    qlock = 0;                         // unlock
706                }
707                if (submitted && (p = pool) != null)
708                    p.signalWork(this, 0);
709            }
710            return submitted;
711        }
712
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
# Line 859 | Line 874 | public class ForkJoinPool extends Abstra
874              return seed = r ^= r << 5;
875          }
876  
862        /**
863         * Provides a more accurate estimate of size than (top - base)
864         * by ordering reads and checking whether a near-empty queue
865         * has at least one unclaimed task.
866         */
867        final int queueSize() {
868            ForkJoinTask<?>[] a; int k, s, n;
869            return ((n = base - (s = top)) < 0 &&
870                    (n != -1 ||
871                     ((a = array) != null && (k = a.length) > 0 &&
872                      U.getObject
873                      (a, (long)((((k - 1) & (s - 1)) << ASHIFT) + ABASE)) != null))) ?
874                -n : 0;
875        }
876
877          // Specialized execution methods
878  
879          /**
# Line 988 | Line 988 | public class ForkJoinPool extends Abstra
988                  (currentSteal = t).doExec();
989                  currentSteal = null;
990                  ++nsteals;
991 <                if (top != base) {       // process remaining local tasks
991 >                if (base - top < 0) {       // process remaining local tasks
992                      if (mode == 0)
993                          popAndExecAll();
994                      else
# Line 1020 | Line 1020 | public class ForkJoinPool extends Abstra
1020                      s != Thread.State.TIMED_WAITING);
1021          }
1022  
1023        /**
1024         * If this owned and is not already interrupted, try to
1025         * interrupt and/or unpark, ignoring exceptions.
1026         */
1027        final void interruptOwner() {
1028            Thread wt, p;
1029            if ((wt = owner) != null && !wt.isInterrupted()) {
1030                try {
1031                    wt.interrupt();
1032                } catch (SecurityException ignore) {
1033                }
1034            }
1035            if ((p = parker) != null)
1036                U.unpark(p);
1037        }
1038
1023          // Unsafe mechanics
1024          private static final sun.misc.Unsafe U;
1025          private static final long QLOCK;
# Line 1070 | Line 1054 | public class ForkJoinPool extends Abstra
1054          defaultForkJoinWorkerThreadFactory;
1055  
1056      /**
1073     * Per-thread records for threads that submit to pools. Currently
1074     * holds only pseudo-random seed / index that is used to choose
1075     * submission queues in method externalPush. In the future, this may
1076     * also incorporate a means to implement different task rejection
1077     * and resubmission policies.
1078     *
1079     * Seeds for submitters and workers/workQueues work in basically
1080     * the same way but are initialized and updated using slightly
1081     * different mechanics. Both are initialized using the same
1082     * approach as in class ThreadLocal, where successive values are
1083     * unlikely to collide with previous values. Seeds are then
1084     * randomly modified upon collisions using xorshifts, which
1085     * requires a non-zero seed.
1086     */
1087    static final class Submitter {
1088        int seed;
1089        Submitter(int s) { seed = s; }
1090    }
1091
1092    /**
1057       * Per-thread submission bookkeeping. Shared across all pools
1058       * to reduce ThreadLocal pollution and because random motion
1059       * to avoid contention in one pool is likely to hold for others.
# Line 1099 | Line 1063 | public class ForkJoinPool extends Abstra
1063      static final ThreadLocal<Submitter> submitters;
1064  
1065      /**
1066 +     * Permission required for callers of methods that may start or
1067 +     * kill threads.
1068 +     */
1069 +    private static final RuntimePermission modifyThreadPermission;
1070 +
1071 +    /**
1072       * Common (static) pool. Non-null for public use unless a static
1073       * construction exception, but internal usages null-check on use
1074       * to paranoically avoid potential initialization circularities
# Line 1107 | Line 1077 | public class ForkJoinPool extends Abstra
1077      static final ForkJoinPool commonPool;
1078  
1079      /**
1110     * Permission required for callers of methods that may start or
1111     * kill threads.
1112     */
1113    private static final RuntimePermission modifyThreadPermission;
1114
1115    /**
1080       * Common pool parallelism. Must equal commonPool.parallelism.
1081       */
1082      static final int commonPoolParallelism;
# Line 1148 | Line 1112 | public class ForkJoinPool extends Abstra
1112      private static final long FAST_IDLE_TIMEOUT =  200L * 1000L * 1000L;
1113  
1114      /**
1115 +     * Tolerance for idle timeouts, to cope with timer undershoots
1116 +     */
1117 +    private static final long TIMEOUT_SLOP = 2000000L;
1118 +
1119 +    /**
1120       * The maximum stolen->joining link depth allowed in method
1121       * tryHelpStealer.  Must be a power of two.  Depths for legitimate
1122       * chains are unbounded, but we use a fixed constant to avoid
# Line 1259 | Line 1228 | public class ForkJoinPool extends Abstra
1228       * declaration order and may differ across JVMs, but the following
1229       * empirically works OK on current JVMs.
1230       */
1231 +
1232 +    // Heuristic padding to ameliorate unfortunate memory placements
1233 +    volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06;
1234 +
1235      volatile long stealCount;                  // collects worker counts
1236      volatile long ctl;                         // main pool control
1237      volatile int plock;                        // shutdown status and seqLock
# Line 1269 | Line 1242 | public class ForkJoinPool extends Abstra
1242      final Thread.UncaughtExceptionHandler ueh; // per-worker UEH
1243      final String workerNamePrefix;             // to create worker name string
1244  
1245 +    volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17;
1246 +    volatile Object pad18, pad19, pad1a, pad1b;
1247 +
1248      /*
1249       * Acquires the plock lock to protect worker array and related
1250       * updates. This method is called only if an initial CAS on plock
1251       * fails. This acts as a spinLock for normal cases, but falls back
1252       * to builtin monitor to block when (rarely) needed. This would be
1253       * a terrible idea for a highly contended lock, but works fine as
1254 <     * a more conservative alternative to a pure spinlock.  See
1279 <     * internal ConcurrentHashMap documentation for further
1280 <     * explanation of nearly the same construction.
1254 >     * a more conservative alternative to a pure spinlock.
1255       */
1256      private int acquirePlock() {
1257          int spins = PL_SPINS, r = 0, ps, nps;
# Line 1329 | Line 1303 | public class ForkJoinPool extends Abstra
1303      }
1304  
1305      /**
1332     * Tries to create and start a worker; adjusts counts etc on failure
1333     */
1334    private void addWorker() {
1335        ForkJoinWorkerThread wt = null;
1336        try {
1337            (wt = factory.newThread(this)).start();
1338        } catch (Throwable ex) {
1339            deregisterWorker(wt, ex); // adjust on failure
1340        }
1341    }
1342
1343    /**
1306       * Performs secondary initialization, called when plock is zero.
1307       * Creates workQueue array and sets plock to a valid value.  The
1308       * lock body must be exception-free (so no try/finally) so we
# Line 1349 | Line 1311 | public class ForkJoinPool extends Abstra
1311       * fullExternalPush.)  Because the plock seq value can eventually
1312       * wrap around zero, this method harmlessly fails to reinitialize
1313       * if workQueues exists, while still advancing plock.
1314 +     *
1315 +     * Additionally tries to create the first worker.
1316       */
1317 <    private void initWorkQueuesArray() {
1318 <        WorkQueue[] ws; int ps;
1317 >    private void initWorkers() {
1318 >        WorkQueue[] ws, nws; int ps;
1319          int p = config & SMASK;        // find power of two table size
1320          int n = (p > 1) ? p - 1 : 1;   // ensure at least 2 slots
1321          n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
1322 <        WorkQueue[] nws = new WorkQueue[(n + 1) << 1];
1322 >        n = (n + 1) << 1;
1323 >        if ((ws = workQueues) == null || ws.length == 0)
1324 >            nws = new WorkQueue[n];
1325 >        else
1326 >            nws = null;
1327          if (((ps = plock) & PL_LOCK) != 0 ||
1328              !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1329              ps = acquirePlock();
1330 <        if ((ws = workQueues) == null || ws.length == 0)
1330 >        if (((ws = workQueues) == null || ws.length == 0) && nws != null)
1331              workQueues = nws;
1332          int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1333          if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1334              releasePlock(nps);
1335 +        tryAddWorker();
1336 +    }
1337 +
1338 +    /**
1339 +     * Tries to create and start one worker if fewer than target
1340 +     * parallelism level exist. Adjusts counts etc on failure.
1341 +     */
1342 +    private void tryAddWorker() {
1343          long c; int u;
1344 <        if ((u = (int)((c = ctl) >>> 32)) < 0 && (int)c == 0) {
1344 >        while ((u = (int)((c = ctl) >>> 32)) < 0 &&
1345 >               (u & SHORT_SIGN) != 0 && (int)c == 0) {
1346              long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1347                               ((u + UAC_UNIT) & UAC_MASK)) << 32;
1348 <            if (U.compareAndSwapLong(this, CTL, c, nc))
1349 <                addWorker();
1348 >            if (U.compareAndSwapLong(this, CTL, c, nc)) {
1349 >                ForkJoinWorkerThreadFactory fac;
1350 >                Throwable ex = null;
1351 >                ForkJoinWorkerThread wt = null;
1352 >                try {
1353 >                    if ((fac = factory) != null &&
1354 >                        (wt = fac.newThread(this)) != null) {
1355 >                        wt.start();
1356 >                        break;
1357 >                    }
1358 >                } catch (Throwable e) {
1359 >                    ex = e;
1360 >                }
1361 >                deregisterWorker(wt, ex);
1362 >                break;
1363 >            }
1364          }
1374
1365      }
1366  
1367      //  Registering and deregistering workers
# Line 1384 | Line 1374 | public class ForkJoinPool extends Abstra
1374       * expanding as needed.
1375       *
1376       * @param wt the worker thread
1377 +     * @return the worker's queue
1378       */
1379 <    final void registerWorker(ForkJoinWorkerThread wt) {
1380 <        if (wt != null && wt.workQueue == null) {
1381 <            int s, ps;    // generate a rarely colliding candidate index seed
1382 <            do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
1383 <                                              s += SEED_INCREMENT) ||
1384 <                         s == 0); // skip 0
1385 <            WorkQueue w = new WorkQueue(this, wt, config >>> 16, s);
1386 <            if (((ps = plock) & PL_LOCK) != 0 ||
1387 <                !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1388 <                ps = acquirePlock();
1389 <            int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1390 <            try {
1391 <                WorkQueue[] ws;
1392 <                if ((ws = workQueues) != null && wt.workQueue == null) {
1393 <                    int n = ws.length, m = n - 1;
1394 <                    int r = (s << 1) | 1;           // use odd-numbered indices
1395 <                    if (ws[r &= m] != null) {       // collision
1396 <                        int probes = 0;             // step by approx half size
1397 <                        int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
1398 <                        while (ws[r = (r + step) & m] != null) {
1399 <                            if (++probes >= n) {
1400 <                                workQueues = ws = Arrays.copyOf(ws, n <<= 1);
1401 <                                m = n - 1;
1402 <                                probes = 0;
1403 <                            }
1379 >    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
1380 >        Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps;
1381 >        wt.setDaemon(true);
1382 >        if ((handler = ueh) != null)
1383 >            wt.setUncaughtExceptionHandler(handler);
1384 >        do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
1385 >                                          s += SEED_INCREMENT) ||
1386 >                     s == 0); // skip 0
1387 >        WorkQueue w = new WorkQueue(this, wt, config >>> 16, s);
1388 >        if (((ps = plock) & PL_LOCK) != 0 ||
1389 >            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
1390 >            ps = acquirePlock();
1391 >        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
1392 >        try {
1393 >            if ((ws = workQueues) != null) {    // skip if shutting down
1394 >                int n = ws.length, m = n - 1;
1395 >                int r = (s << 1) | 1;           // use odd-numbered indices
1396 >                if (ws[r &= m] != null) {       // collision
1397 >                    int probes = 0;             // step by approx half size
1398 >                    int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
1399 >                    while (ws[r = (r + step) & m] != null) {
1400 >                        if (++probes >= n) {
1401 >                            workQueues = ws = Arrays.copyOf(ws, n <<= 1);
1402 >                            m = n - 1;
1403 >                            probes = 0;
1404                          }
1405                      }
1415                    w.eventCount = w.poolIndex = r; // volatile write orders
1416                    wt.workQueue = ws[r] = w;
1406                  }
1407 <            } finally {
1408 <                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1420 <                    releasePlock(nps);
1407 >                w.eventCount = w.poolIndex = r; // volatile write orders
1408 >                ws[r] = w;
1409              }
1410 +        } finally {
1411 +            if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
1412 +                releasePlock(nps);
1413          }
1414 +        wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex)));
1415 +        return w;
1416      }
1417  
1418      /**
# Line 1454 | Line 1447 | public class ForkJoinPool extends Abstra
1447              }
1448          }
1449  
1450 <        long c;                             // adjust ctl counts
1450 >        long c;                          // adjust ctl counts
1451          do {} while (!U.compareAndSwapLong
1452                       (this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) |
1453                                             ((c - TC_UNIT) & TC_MASK) |
1454                                             (c & ~(AC_MASK|TC_MASK)))));
1455  
1456 <        if (!tryTerminate(false, false) && w != null) {
1457 <            w.cancelAll();                  // cancel remaining tasks
1458 <            if (w.array != null)            // suppress signal if never ran
1459 <                helpSignal(null, 0);        // wake up or create replacement
1460 <            if (ex == null)                 // help clean refs on way out
1461 <                ForkJoinTask.helpExpungeStaleExceptions();
1456 >        if (!tryTerminate(false, false) && w != null && w.array != null) {
1457 >            w.cancelAll();               // cancel remaining tasks
1458 >            WorkQueue[] ws; WorkQueue v; Thread p; int u, i, e;
1459 >            while ((u = (int)((c = ctl) >>> 32)) < 0 && (e = (int)c) >= 0) {
1460 >                if (e > 0) {             // activate or create replacement
1461 >                    if ((ws = workQueues) == null ||
1462 >                        (i = e & SMASK) >= ws.length ||
1463 >                        (v = ws[i]) == null)
1464 >                        break;
1465 >                    long nc = (((long)(v.nextWait & E_MASK)) |
1466 >                               ((long)(u + UAC_UNIT) << 32));
1467 >                    if (v.eventCount != (e | INT_SIGN))
1468 >                        break;
1469 >                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1470 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1471 >                        if ((p = v.parker) != null)
1472 >                            U.unpark(p);
1473 >                        break;
1474 >                    }
1475 >                }
1476 >                else {
1477 >                    if ((short)u < 0)
1478 >                        tryAddWorker();
1479 >                    break;
1480 >                }
1481 >            }
1482          }
1483 <
1484 <        if (ex != null)                     // rethrow
1483 >        if (ex == null)                     // help clean refs on way out
1484 >            ForkJoinTask.helpExpungeStaleExceptions();
1485 >        else                                // rethrow
1486              ForkJoinTask.rethrow(ex);
1487      }
1488  
# Line 1490 | Line 1504 | public class ForkJoinPool extends Abstra
1504              U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock
1505              int b = q.base, s = q.top, n, an;
1506              if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) {
1507 <                U.putObject(a, (long)(((an - 1) & s) << ASHIFT) + ABASE, task);
1507 >                int j = (((an - 1) & s) << ASHIFT) + ABASE;
1508 >                U.putOrderedObject(a, j, task);
1509                  q.top = s + 1;                     // push on to deque
1510                  q.qlock = 0;
1511                  if (n <= 2)
1512 <                    signalWork(q, 0);
1512 >                    signalWork(q);
1513                  return;
1514              }
1515              q.qlock = 0;
# Line 1506 | Line 1521 | public class ForkJoinPool extends Abstra
1521       * Full version of externalPush. This method is called, among
1522       * other times, upon the first submission of the first task to the
1523       * pool, so must perform secondary initialization (via
1524 <     * initWorkQueuesArray). It also detects first submission by an
1525 <     * external thread by looking up its ThreadLocal, and creates a
1526 <     * new shared queue if the one at index if empty or contended. The
1527 <     * lock body must be exception-free (so no try/finally) so we
1524 >     * initWorkers). It also detects first submission by an external
1525 >     * thread by looking up its ThreadLocal, and creates a new shared
1526 >     * queue if the one at index if empty or contended. The plock lock
1527 >     * body must be exception-free (so no try/finally) so we
1528       * optimistically allocate new queues outside the lock and throw
1529       * them away if (very rarely) not needed.
1530       */
1531      private void fullExternalPush(ForkJoinTask<?> task) {
1532 <        int r = 0;
1532 >        int r = 0; // random index seed
1533          for (Submitter z = submitters.get();;) {
1534              WorkQueue[] ws; WorkQueue q; int ps, m, k;
1535              if (z == null) {
# Line 1532 | Line 1547 | public class ForkJoinPool extends Abstra
1547                  throw new RejectedExecutionException();
1548              else if (ps == 0 || (ws = workQueues) == null ||
1549                       (m = ws.length - 1) < 0)
1550 <                initWorkQueuesArray();
1550 >                initWorkers();
1551              else if ((q = ws[k = r & m & SQMASK]) != null) {
1552 <                if (q.trySharedPush(task))
1553 <                    return;
1554 <                else
1555 <                    r = 0; // move on contention
1552 >                if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
1553 >                    ForkJoinTask<?>[] a = q.array;
1554 >                    int s = q.top;
1555 >                    boolean submitted = false;
1556 >                    try {                      // locked version of push
1557 >                        if ((a != null && a.length > s + 1 - q.base) ||
1558 >                            (a = q.growArray()) != null) {   // must presize
1559 >                            int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
1560 >                            U.putOrderedObject(a, j, task);
1561 >                            q.top = s + 1;
1562 >                            submitted = true;
1563 >                        }
1564 >                    } finally {
1565 >                        q.qlock = 0;  // unlock
1566 >                    }
1567 >                    if (submitted) {
1568 >                        signalWork(q);
1569 >                        return;
1570 >                    }
1571 >                }
1572 >                r = 0; // move on failure
1573              }
1574              else if (((ps = plock) & PL_LOCK) == 0) { // create new queue
1575                  q = new WorkQueue(this, null, SHARED_QUEUE, r);
# Line 1566 | Line 1598 | public class ForkJoinPool extends Abstra
1598      }
1599  
1600      /**
1601 <     * Tries to create (at most one) or activate (possibly several)
1602 <     * workers if too few are active. On contention failure, continues
1603 <     * until at least one worker is signalled or the given queue is
1572 <     * empty or all workers are active.
1573 <     *
1574 <     * @param q if non-null, the queue holding tasks to be signalled
1575 <     * @param signals the target number of signals (at least one --
1576 <     * if argument is zero also sets signallee hint if parked).
1601 >     * Tries to create or activate a worker if too few are active.
1602 >     *
1603 >     * @param q the (non-null) queue holding tasks to be signalled
1604       */
1605 <    final void signalWork(WorkQueue q, int signals) {
1606 <        long c; int e, u, i, s; WorkQueue[] ws; WorkQueue w; Thread p;
1605 >    final void signalWork(WorkQueue q) {
1606 >        int hint = q.poolIndex;
1607 >        long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p;
1608          while ((u = (int)((c = ctl) >>> 32)) < 0) {
1609              if ((e = (int)c) > 0) {
1610                  if ((ws = workQueues) != null && ws.length > (i = e & SMASK) &&
# Line 1584 | Line 1612 | public class ForkJoinPool extends Abstra
1612                      long nc = (((long)(w.nextWait & E_MASK)) |
1613                                 ((long)(u + UAC_UNIT) << 32));
1614                      if (U.compareAndSwapLong(this, CTL, c, nc)) {
1615 +                        w.hint = hint;
1616                          w.eventCount = (e + E_SEQ) & E_MASK;
1617 <                        if ((p = w.parker) != null) {
1589 <                            if (q != null && signals == 0)
1590 <                                w.hint = q.poolIndex;
1617 >                        if ((p = w.parker) != null)
1618                              U.unpark(p);
1619 <                        }
1593 <                        if (--signals <= 0)
1594 <                            break;
1619 >                        break;
1620                      }
1621 <                    if (q != null && (s = q.queueSize()) <= signals &&
1597 <                         (signals = s) <= 0)
1621 >                    if (q.top - q.base <= 0)
1622                          break;
1623                  }
1624                  else
1625                      break;
1626              }
1627 <            else if (e == 0 && (u & SHORT_SIGN) != 0) {
1628 <                long nc = (long)(((u + UTC_UNIT) & UTC_MASK) |
1629 <                                 ((u + UAC_UNIT) & UAC_MASK)) << 32;
1606 <                if (U.compareAndSwapLong(this, CTL, c, nc)) {
1607 <                    addWorker();
1608 <                    break;
1609 <                }
1610 <            }
1611 <            else
1627 >            else {
1628 >                if ((short)u < 0)
1629 >                    tryAddWorker();
1630                  break;
1631 +            }
1632          }
1633      }
1634  
# Line 1619 | Line 1638 | public class ForkJoinPool extends Abstra
1638       * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
1639       */
1640      final void runWorker(WorkQueue w) {
1641 <        if (w != null) // skip on initialization failure
1642 <            do { w.runTask(scan(w)); } while (w.qlock >= 0);
1641 >        w.growArray(); // allocate queue
1642 >        do { w.runTask(scan(w)); } while (w.qlock >= 0);
1643      }
1644  
1645      /**
# Line 1648 | Line 1667 | public class ForkJoinPool extends Abstra
1667       *
1668       * * If not already enqueued, try to inactivate and enqueue the
1669       * worker on wait queue. Or, if inactivating has caused the pool
1670 <     * to be quiescent, relay to idleAwaitWork to check for
1671 <     * termination and possibly shrink pool.
1670 >     * to be quiescent, relay to idleAwaitWork to possibly shrink
1671 >     * pool.
1672       *
1673       * * If already enqueued and none of the above apply, possibly
1674 <     * (with 1/2 probability) park awaiting signal, else lingering to
1675 <     * help scan and signal.
1674 >     * park awaiting signal, else lingering to help scan and signal.
1675 >     *
1676 >     * * If a non-empty queue discovered or left as a hint,
1677 >     * help wake up other workers before return
1678       *
1679       * @param w the worker (via its WorkQueue)
1680       * @return a task or null if none found
1681       */
1682      private final ForkJoinTask<?> scan(WorkQueue w) {
1683 <        WorkQueue[] ws; int m, hint;
1683 >        WorkQueue[] ws; int m;
1684          int ps = plock;                          // read plock before ws
1685          if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1686              int ec = w.eventCount;               // ec is negative if inactive
1687              int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5;
1688 <            for (int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; ; --j) {
1688 >            w.hint = -1;                         // update seed and clear hint
1689 >            int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN;
1690 >            do {
1691                  WorkQueue q; ForkJoinTask<?>[] a; int b;
1692                  if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 &&
1693                      (a = q.array) != null) {     // probably nonempty
# Line 1674 | Line 1697 | public class ForkJoinPool extends Abstra
1697                      if (q.base == b && ec >= 0 && t != null &&
1698                          U.compareAndSwapObject(a, i, t, null)) {
1699                          if ((q.base = b + 1) - q.top < 0)
1700 <                            signalWork(q, 0);
1700 >                            signalWork(q);
1701                          return t;                // taken
1702                      }
1703 <                    else if (ec < 0 || j < m) {  // cannot take or cannot rescan
1704 <                        w.hint = q.poolIndex;    // use hint below
1705 <                        break;                   // let caller retry after signal
1706 <                    }
1707 <                }
1708 <                else if (j < 0) { // end of scan; in loop to simplify code
1709 <                    long c, sc; int e, ns;
1710 <                    if ((ns = w.nsteals) != 0) {
1711 <                        if (U.compareAndSwapLong(this, STEALCOUNT,
1712 <                                                 sc = stealCount, sc + ns))
1713 <                            w.nsteals = 0;       // collect steals
1714 <                    }
1715 <                    else if (plock != ps)        // ws may have changed
1716 <                        break;
1717 <                    else if ((e = (int)(c = ctl)) < 0)
1718 <                        w.qlock = -1;            // pool is terminating
1719 <                    else if (ec >= 0) {          // try to enqueue/inactivate
1720 <                        long nc = ((long)ec |
1721 <                                   ((c - AC_UNIT) & (AC_MASK|TC_MASK)));
1703 >                    else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) {
1704 >                        w.hint = (r + j) & m;    // help signal below
1705 >                        break;                   // cannot take
1706 >                    }
1707 >                }
1708 >            } while (--j >= 0);
1709 >
1710 >            int h, e, ns; long c, sc; WorkQueue q;
1711 >            if ((ns = w.nsteals) != 0) {
1712 >                if (U.compareAndSwapLong(this, STEALCOUNT,
1713 >                                         sc = stealCount, sc + ns))
1714 >                    w.nsteals = 0;               // collect steals and rescan
1715 >            }
1716 >            else if (plock != ps)                // consistency check
1717 >                ;                                // skip
1718 >            else if ((e = (int)(c = ctl)) < 0)
1719 >                w.qlock = -1;                    // pool is terminating
1720 >            else {
1721 >                if ((h = w.hint) < 0) {
1722 >                    if (ec >= 0) {               // try to enqueue/inactivate
1723 >                        long nc = (((long)ec |
1724 >                                    ((c - AC_UNIT) & (AC_MASK|TC_MASK))));
1725                          w.nextWait = e;          // link and mark inactive
1700                        w.hint = -1;             // use hint if set while parked
1726                          w.eventCount = ec | INT_SIGN;
1727 <                        if (ctl != c ||
1728 <                            !U.compareAndSwapLong(this, CTL, c, nc))
1704 <                            w.eventCount = ec;  // unmark on CAS failure
1727 >                        if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc))
1728 >                            w.eventCount = ec;   // unmark on CAS failure
1729                          else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK))
1730                              idleAwaitWork(w, nc, c);
1731                      }
1732 <                    else if (w.eventCount < 0) { // block
1732 >                    else if (w.eventCount < 0 && !tryTerminate(false, false) &&
1733 >                             ctl == c) {         // block
1734                          Thread wt = Thread.currentThread();
1735                          Thread.interrupted();    // clear status
1736                          U.putObject(wt, PARKBLOCKER, this);
# Line 1715 | Line 1740 | public class ForkJoinPool extends Abstra
1740                          w.parker = null;
1741                          U.putObject(wt, PARKBLOCKER, null);
1742                      }
1718                    break;
1743                  }
1744 <            }
1745 <            if ((hint = w.hint) >= 0) {          // help signal
1746 <                WorkQueue[] vs; WorkQueue v; int k;
1747 <                w.hint = -1;                     // suppress resignal
1748 <                if ((vs = workQueues) != null && hint < vs.length &&
1749 <                    (v = vs[hint]) != null && (k = v.base - v.top) < -1)
1750 <                    signalWork(v, 1 - k);
1744 >                if ((h >= 0 || (h = w.hint) >= 0) &&
1745 >                    (ws = workQueues) != null && h < ws.length &&
1746 >                    (q = ws[h]) != null) {      // signal others before retry
1747 >                    WorkQueue v; Thread p; int u, i, s;
1748 >                    for (int n = (config & SMASK) >>> 1;;) {
1749 >                        int idleCount = (w.eventCount < 0) ? 0 : -1;
1750 >                        if (((s = idleCount - q.base + q.top) <= n &&
1751 >                             (n = s) <= 0) ||
1752 >                            (u = (int)((c = ctl) >>> 32)) >= 0 ||
1753 >                            (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1754 >                            (v = ws[i]) == null)
1755 >                            break;
1756 >                        long nc = (((long)(v.nextWait & E_MASK)) |
1757 >                                   ((long)(u + UAC_UNIT) << 32));
1758 >                        if (v.eventCount != (e | INT_SIGN) ||
1759 >                            !U.compareAndSwapLong(this, CTL, c, nc))
1760 >                            break;
1761 >                        v.hint = h;
1762 >                        v.eventCount = (e + E_SEQ) & E_MASK;
1763 >                        if ((p = v.parker) != null)
1764 >                            U.unpark(p);
1765 >                        if (--n <= 0)
1766 >                            break;
1767 >                    }
1768 >                }
1769              }
1770          }
1771          return null;
# Line 1746 | Line 1788 | public class ForkJoinPool extends Abstra
1788              !tryTerminate(false, false) && (int)prevCtl != 0) {
1789              int dc = -(short)(currentCtl >>> TC_SHIFT);
1790              long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT;
1791 <            long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop
1791 >            long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP;
1792              Thread wt = Thread.currentThread();
1793              while (ctl == currentCtl) {
1794                  Thread.interrupted();  // timed variant of version in scan()
# Line 1762 | Line 1804 | public class ForkJoinPool extends Abstra
1804                      U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) {
1805                      w.eventCount = (w.eventCount + E_SEQ) | E_MASK;
1806                      w.qlock = -1;   // shrink
1765                    w.hint = -1;    // suppress helping
1807                      break;
1808                  }
1809              }
# Line 1770 | Line 1811 | public class ForkJoinPool extends Abstra
1811      }
1812  
1813      /**
1814 <     * Scans through queues looking for work (optionally, while
1815 <     * joining a task); if any are present, signals. May return early
1816 <     * if more signalling is detectably unneeded.
1814 >     * Scans through queues looking for work while joining a task; if
1815 >     * any present, signals. May return early if more signalling is
1816 >     * detectably unneeded.
1817       *
1818 <     * @param task if non-null, return early if done
1818 >     * @param task return early if done
1819       * @param origin an index to start scan
1820       */
1821 <    final int helpSignal(ForkJoinTask<?> task, int origin) {
1822 <        WorkQueue[] ws; WorkQueue q; int m, n, s, u;
1823 <        if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1824 <            for (int i = 0; i <= m; ++i) {
1825 <                if (task != null && (s = task.status) < 0)
1826 <                    return s;
1827 <                if ((q = ws[(i + origin) & m]) != null &&
1828 <                    (n = q.queueSize()) > 0) {
1829 <                    signalWork(q, n);
1830 <                    if ((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
1821 >    private void helpSignal(ForkJoinTask<?> task, int origin) {
1822 >        WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s;
1823 >        if (task != null && task.status >= 0 &&
1824 >            (u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 &&
1825 >            (ws = workQueues) != null && (m = ws.length - 1) >= 0) {
1826 >            outer: for (int k = origin, j = m; j >= 0; --j) {
1827 >                WorkQueue q = ws[k++ & m];
1828 >                for (int n = m;;) { // limit to at most m signals
1829 >                    if (task.status < 0)
1830 >                        break outer;
1831 >                    if (q == null ||
1832 >                        ((s = -q.base + q.top) <= n && (n = s) <= 0))
1833                          break;
1834 +                    if ((u = (int)((c = ctl) >>> 32)) >= 0 ||
1835 +                        (e = (int)c) <= 0 || m < (i = e & SMASK) ||
1836 +                        (w = ws[i]) == null)
1837 +                        break outer;
1838 +                    long nc = (((long)(w.nextWait & E_MASK)) |
1839 +                               ((long)(u + UAC_UNIT) << 32));
1840 +                    if (w.eventCount != (e | INT_SIGN))
1841 +                        break outer;
1842 +                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
1843 +                        w.eventCount = (e + E_SEQ) & E_MASK;
1844 +                        if ((p = w.parker) != null)
1845 +                            U.unpark(p);
1846 +                        if (--n <= 0)
1847 +                            break;
1848 +                    }
1849                  }
1850              }
1851          }
1794        return 0;
1852      }
1853  
1854      /**
# Line 1940 | Line 1997 | public class ForkJoinPool extends Abstra
1997              else if (tc + pc < MAX_CAP) {
1998                  long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
1999                  if (U.compareAndSwapLong(this, CTL, c, nc)) {
2000 <                    addWorker();
2001 <                    return true;
2000 >                    ForkJoinWorkerThreadFactory fac;
2001 >                    Throwable ex = null;
2002 >                    ForkJoinWorkerThread wt = null;
2003 >                    try {
2004 >                        if ((fac = factory) != null &&
2005 >                            (wt = fac.newThread(this)) != null) {
2006 >                            wt.start();
2007 >                            return true;
2008 >                        }
2009 >                    } catch (Throwable rex) {
2010 >                        ex = rex;
2011 >                    }
2012 >                    deregisterWorker(wt, ex); // clean up and return false
2013                  }
2014              }
2015          }
# Line 1960 | Line 2028 | public class ForkJoinPool extends Abstra
2028          if (joiner != null && task != null && (s = task.status) >= 0) {
2029              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2030              joiner.currentJoin = task;
2031 <            do {} while ((s = task.status) >= 0 &&
1964 <                         joiner.queueSize() > 0 &&
2031 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2032                           joiner.tryRemoveAndExec(task)); // process local tasks
2033 <            if (s >= 0 && (s = task.status) >= 0 &&
2034 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2035 <                (task instanceof CountedCompleter))
2036 <                s = helpComplete(task, LIFO_QUEUE);
2037 <            int k = 0; // to perform pre-block yield for politeness
2033 >            if (s >= 0 && (s = task.status) >= 0) {
2034 >                helpSignal(task, joiner.poolIndex);
2035 >                if ((s = task.status) >= 0 &&
2036 >                    (task instanceof CountedCompleter))
2037 >                    s = helpComplete(task, LIFO_QUEUE);
2038 >            }
2039              while (s >= 0 && (s = task.status) >= 0) {
2040 <                if ((joiner.queueSize() > 0 ||           // try helping
2040 >                if ((!joiner.isEmpty() ||           // try helping
2041                       (s = tryHelpStealer(joiner, task)) == 0) &&
2042                      (s = task.status) >= 0) {
2043 <                    if (k < 3) {
2044 <                        if (++k < 3)
1977 <                            s = helpSignal(task, joiner.poolIndex);
1978 <                        else
1979 <                            Thread.yield();
1980 <                    }
1981 <                    else if (!tryCompensate())
1982 <                        k = 0;
1983 <                    else {
2043 >                    helpSignal(task, joiner.poolIndex);
2044 >                    if ((s = task.status) >= 0 && tryCompensate()) {
2045                          if (task.trySetSignal() && (s = task.status) >= 0) {
2046                              synchronized (task) {
2047                                  if (task.status >= 0) {
# Line 2017 | Line 2078 | public class ForkJoinPool extends Abstra
2078          if (joiner != null && task != null && (s = task.status) >= 0) {
2079              ForkJoinTask<?> prevJoin = joiner.currentJoin;
2080              joiner.currentJoin = task;
2081 <            do {} while ((s = task.status) >= 0 &&
2021 <                         joiner.queueSize() > 0 &&
2081 >            do {} while ((s = task.status) >= 0 && !joiner.isEmpty() &&
2082                           joiner.tryRemoveAndExec(task));
2083 <            if (s >= 0 && (s = task.status) >= 0 &&
2084 <                (s = helpSignal(task, joiner.poolIndex)) >= 0 &&
2085 <                (task instanceof CountedCompleter))
2086 <                s = helpComplete(task, LIFO_QUEUE);
2087 <            if (s >= 0 && joiner.queueSize() == 0) {
2083 >            if (s >= 0 && (s = task.status) >= 0) {
2084 >                helpSignal(task, joiner.poolIndex);
2085 >                if ((s = task.status) >= 0 &&
2086 >                    (task instanceof CountedCompleter))
2087 >                    s = helpComplete(task, LIFO_QUEUE);
2088 >            }
2089 >            if (s >= 0 && joiner.isEmpty()) {
2090                  do {} while (task.status >= 0 &&
2091                               tryHelpStealer(joiner, task) > 0);
2092              }
# Line 2041 | Line 2103 | public class ForkJoinPool extends Abstra
2103       */
2104      private WorkQueue findNonEmptyStealQueue(int r) {
2105          for (WorkQueue[] ws;;) {
2106 <            int ps = plock, m, n;
2106 >            int ps = plock, m;
2107              if ((ws = workQueues) == null || (m = ws.length - 1) < 1)
2108                  return null;
2109              for (int j = (m + 1) << 2; ;) {
2110                  WorkQueue q = ws[(((r + j) << 1) | 1) & m];
2111 <                if (q != null && (n = q.queueSize()) > 0) {
2050 <                    if (n > 1)
2051 <                        signalWork(q, 0);
2111 >                if (q != null && q.base - q.top < 0)
2112                      return q;
2053                }
2113                  else if (--j < 0) {
2114                      if (plock == ps)
2115                          return null;
# Line 2081 | Line 2140 | public class ForkJoinPool extends Abstra
2140                      do {} while (!U.compareAndSwapLong
2141                                   (this, CTL, c = ctl, c + AC_UNIT));
2142                  }
2143 <                if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null)
2143 >                if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) {
2144 >                    if (q.base - q.top < 0)
2145 >                        signalWork(q);
2146                      w.runSubtask(t);
2147 +                }
2148              }
2149              else {
2150                  long c;
# Line 2114 | Line 2176 | public class ForkJoinPool extends Abstra
2176                  return t;
2177              if ((q = findNonEmptyStealQueue(w.nextSeed())) == null)
2178                  return null;
2179 <            if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null)
2179 >            if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) {
2180 >                if (q.base - q.top < 0)
2181 >                    signalWork(q);
2182                  return t;
2183 +            }
2184          }
2185      }
2186  
# Line 2215 | Line 2280 | public class ForkJoinPool extends Abstra
2280                  if (((ps = plock) & PL_LOCK) != 0 ||
2281                      !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
2282                      ps = acquirePlock();
2283 <                int nps = SHUTDOWN;
2284 <                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
2220 <                    releasePlock(nps);
2283 >                if (!U.compareAndSwapInt(this, PLOCK, ps, SHUTDOWN))
2284 >                    releasePlock(SHUTDOWN);
2285              }
2286              if (!now) {                             // check if idle & no tasks
2287                  if ((int)(c >> AC_SHIFT) != -(config & SMASK) ||
# Line 2236 | Line 2300 | public class ForkJoinPool extends Abstra
2300                  for (int pass = 0; pass < 3; ++pass) {
2301                      WorkQueue[] ws = workQueues;
2302                      if (ws != null) {
2303 <                        WorkQueue w;
2303 >                        WorkQueue w; Thread wt;
2304                          int n = ws.length;
2305                          for (int i = 0; i < n; ++i) {
2306                              if ((w = ws[i]) != null) {
2307                                  w.qlock = -1;
2308                                  if (pass > 0) {
2309                                      w.cancelAll();
2310 <                                    if (pass > 1)
2311 <                                        w.interruptOwner();
2310 >                                    if (pass > 1 && (wt = w.owner) != null) {
2311 >                                        if (!wt.isInterrupted()) {
2312 >                                            try {
2313 >                                                wt.interrupt();
2314 >                                            } catch (SecurityException ignore) {
2315 >                                            }
2316 >                                        }
2317 >                                        U.unpark(wt);
2318 >                                    }
2319                                  }
2320                              }
2321                          }
# Line 2290 | Line 2361 | public class ForkJoinPool extends Abstra
2361       */
2362      static boolean tryExternalUnpush(ForkJoinTask<?> t) {
2363          ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z;
2364 <        ForkJoinTask<?>[] a;  int m, s; long j;
2365 <        if ((z = submitters.get()) != null &&
2364 >        ForkJoinTask<?>[] a;  int m, s;
2365 >        if (t != null &&
2366 >            (z = submitters.get()) != null &&
2367              (p = commonPool) != null &&
2368              (ws = p.workQueues) != null &&
2369              (m = ws.length - 1) >= 0 &&
2370              (q = ws[m & z.seed & SQMASK]) != null &&
2371              (s = q.top) != q.base &&
2372 <            (a = q.array) != null &&
2373 <            U.getObjectVolatile
2374 <            (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2375 <            U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2376 <            if (q.array == a && q.top == s && // recheck
2377 <                U.compareAndSwapObject(a, j, t, null)) {
2378 <                q.top = s - 1;
2372 >            (a = q.array) != null) {
2373 >            long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
2374 >            if (U.getObject(a, j) == t &&
2375 >                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2376 >                if (q.array == a && q.top == s && // recheck
2377 >                    U.compareAndSwapObject(a, j, t, null)) {
2378 >                    q.top = s - 1;
2379 >                    q.qlock = 0;
2380 >                    return true;
2381 >                }
2382                  q.qlock = 0;
2308                return true;
2383              }
2310            q.qlock = 0;
2384          }
2385          return false;
2386      }
# Line 2349 | Line 2422 | public class ForkJoinPool extends Abstra
2422                      (u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)
2423                      break;
2424                  if (task == null) {
2425 <                    if (helpSignal(root, q.poolIndex) >= 0)
2425 >                    helpSignal(root, q.poolIndex);
2426 >                    if (root.status >= 0)
2427                          helpComplete(root, SHARED_QUEUE);
2428                      break;
2429                  }
# Line 2364 | Line 2438 | public class ForkJoinPool extends Abstra
2438      static void externalHelpJoin(ForkJoinTask<?> t) {
2439          // Some hard-to-avoid overlap with tryExternalUnpush
2440          ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z;
2441 <        ForkJoinTask<?>[] a;  int m, s, n; long j;
2441 >        ForkJoinTask<?>[] a;  int m, s, n;
2442          if (t != null &&
2443              (z = submitters.get()) != null &&
2444              (p = commonPool) != null &&
2445              (ws = p.workQueues) != null &&
2446              (m = ws.length - 1) >= 0 &&
2447              (q = ws[m & z.seed & SQMASK]) != null &&
2448 <            (a = q.array) != null &&
2449 <            t.status >= 0) {
2450 <            if ((s = q.top) != q.base &&
2451 <                U.getObjectVolatile
2452 <                (a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t &&
2453 <                U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2454 <                if (q.array == a && q.top == s &&
2455 <                    U.compareAndSwapObject(a, j, t, null)) {
2456 <                    q.top = s - 1;
2457 <                    q.qlock = 0;
2458 <                    t.doExec();
2448 >            (a = q.array) != null) {
2449 >            int am = a.length - 1;
2450 >            if ((s = q.top) != q.base) {
2451 >                long j = ((am & (s - 1)) << ASHIFT) + ABASE;
2452 >                if (U.getObject(a, j) == t &&
2453 >                    U.compareAndSwapInt(q, QLOCK, 0, 1)) {
2454 >                    if (q.array == a && q.top == s &&
2455 >                        U.compareAndSwapObject(a, j, t, null)) {
2456 >                        q.top = s - 1;
2457 >                        q.qlock = 0;
2458 >                        t.doExec();
2459 >                    }
2460 >                    else
2461 >                        q.qlock = 0;
2462                  }
2386                else
2387                    q.qlock = 0;
2463              }
2464              if (t.status >= 0) {
2465                  if (t instanceof CountedCompleter)
# Line 2403 | Line 2478 | public class ForkJoinPool extends Abstra
2478          if ((p = commonPool) != null &&
2479              (q = p.findNonEmptyStealQueue(1)) != null &&
2480              (b = q.base) - q.top < 0 &&
2481 <            (t = q.pollAt(b)) != null)
2481 >            (t = q.pollAt(b)) != null) {
2482 >            if (q.base - q.top < 0)
2483 >                p.signalWork(q);
2484              t.doExec();
2485 +        }
2486      }
2487  
2488      // Exported methods
# Line 2480 | Line 2558 | public class ForkJoinPool extends Abstra
2558              throw new IllegalArgumentException();
2559          this.factory = factory;
2560          this.ueh = handler;
2561 <        this.config = parallelism | (asyncMode? (FIFO_QUEUE << 16) : 0);
2561 >        this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0);
2562          long np = (long)(-parallelism); // offset ctl counts
2563          this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
2564          int pn = nextPoolId();
# Line 2505 | Line 2583 | public class ForkJoinPool extends Abstra
2583      }
2584  
2585      /**
2586 <     * Returns the common pool instance.
2586 >     * Returns the common pool instance. This pool is statically
2587 >     * constructed; its run state is unaffected by attempts to
2588 >     * {@link #shutdown} or {@link #shutdownNow}.
2589       *
2590       * @return the common pool instance
2591       */
# Line 2838 | Line 2918 | public class ForkJoinPool extends Abstra
2918          WorkQueue[] ws; WorkQueue w;
2919          if ((ws = workQueues) != null) {
2920              for (int i = 0; i < ws.length; i += 2) {
2921 <                if ((w = ws[i]) != null && w.queueSize() != 0)
2921 >                if ((w = ws[i]) != null && !w.isEmpty())
2922                      return true;
2923              }
2924          }
# Line 3005 | Line 3085 | public class ForkJoinPool extends Abstra
3085       * commenced but not yet completed.  This method may be useful for
3086       * debugging. A return of {@code true} reported a sufficient
3087       * period after shutdown may indicate that submitted tasks have
3088 <     * ignored or suppressed interruption, or are waiting for IO,
3088 >     * ignored or suppressed interruption, or are waiting for I/O,
3089       * causing this executor not to properly terminate. (See the
3090       * advisory notes for class {@link ForkJoinTask} stating that
3091       * tasks should not normally entail blocking operations.  But if
# Line 3160 | Line 3240 | public class ForkJoinPool extends Abstra
3240          if (t instanceof ForkJoinWorkerThread) {
3241              ForkJoinPool p = ((ForkJoinWorkerThread)t).pool;
3242              while (!blocker.isReleasable()) { // variant of helpSignal
3243 <                WorkQueue[] ws; WorkQueue q; int m, n, u;
3243 >                WorkQueue[] ws; WorkQueue q; int m, u;
3244                  if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) {
3245                      for (int i = 0; i <= m; ++i) {
3246                          if (blocker.isReleasable())
3247                              return;
3248 <                        if ((q = ws[i]) != null && (n = q.queueSize()) > 0) {
3249 <                            p.signalWork(q, n);
3248 >                        if ((q = ws[i]) != null && q.base - q.top < 0) {
3249 >                            p.signalWork(q);
3250                              if ((u = (int)(p.ctl >>> 32)) >= 0 ||
3251                                  (u >> UAC_SHIFT) >= 0)
3252                                  break;
# Line 3245 | Line 3325 | public class ForkJoinPool extends Abstra
3325          submitters = new ThreadLocal<Submitter>();
3326          ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory =
3327              new DefaultForkJoinWorkerThreadFactory();
3328 +        modifyThreadPermission = new RuntimePermission("modifyThread");
3329 +
3330          /*
3331           * Establish common pool parameters.  For extra caution,
3332           * computations to set up common pool state are here; the
# Line 3280 | Line 3362 | public class ForkJoinPool extends Abstra
3362          long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
3363  
3364          commonPool = new ForkJoinPool(par, ct, fac, handler);
3283        modifyThreadPermission = new RuntimePermission("modifyThread");
3365      }
3366  
3367      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines