--- jsr166/src/jsr166y/ForkJoinPool.java 2012/01/26 00:08:13 1.111 +++ jsr166/src/jsr166y/ForkJoinPool.java 2012/01/31 01:33:21 1.121 @@ -5,6 +5,7 @@ */ package jsr166y; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -20,7 +21,7 @@ import java.util.concurrent.RunnableFutu import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.Condition; /** @@ -59,17 +60,16 @@ import java.util.concurrent.locks.Condit * convenient form for informal monitoring. * *

As is the case with other ExecutorServices, there are three - * main task execution methods summarized in the following - * table. These are designed to be used primarily by clients not - * already engaged in fork/join computations in the current pool. The - * main forms of these methods accept instances of {@code - * ForkJoinTask}, but overloaded forms also allow mixed execution of - * plain {@code Runnable}- or {@code Callable}- based activities as - * well. However, tasks that are already executing in a pool should - * normally instead use the within-computation forms listed in the - * table unless using async event-style tasks that are not usually - * joined, in which case there is little difference among choice of - * methods. + * main task execution methods summarized in the following table. + * These are designed to be used primarily by clients not already + * engaged in fork/join computations in the current pool. The main + * forms of these methods accept instances of {@code ForkJoinTask}, + * but overloaded forms also allow mixed execution of plain {@code + * Runnable}- or {@code Callable}- based activities as well. However, + * tasks that are already executing in a pool should normally instead + * use the within-computation forms listed in the table unless using + * async event-style tasks that are not usually joined, in which case + * there is little difference among choice of methods. * * * @@ -130,14 +130,14 @@ public class ForkJoinPool extends Abstra * * This class and its nested classes provide the main * functionality and control for a set of worker threads: - * Submissions from non-FJ threads enter into submission - * queues. Workers take these tasks and typically split them into - * subtasks that may be stolen by other workers. Preference rules - * give first priority to processing tasks from their own queues - * (LIFO or FIFO, depending on mode), then to randomized FIFO - * steals of tasks in other queues. + * Submissions from non-FJ threads enter into submission queues. + * Workers take these tasks and typically split them into subtasks + * that may be stolen by other workers. Preference rules give + * first priority to processing tasks from their own queues (LIFO + * or FIFO, depending on mode), then to randomized FIFO steals of + * tasks in other queues. * - * WorkQueues. + * WorkQueues * ========== * * Most operations occur within work-stealing queues (in nested @@ -155,7 +155,7 @@ public class ForkJoinPool extends Abstra * (http://research.sun.com/scalable/pubs/index.html) and * "Idempotent work stealing" by Michael, Saraswat, and Vechev, * PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186). - * The main differences ultimately stem from gc requirements that + * The main differences ultimately stem from GC requirements that * we null out taken slots as soon as we can, to maintain as small * a footprint as possible even in programs generating huge * numbers of tasks. To accomplish this, we shift the CAS @@ -179,7 +179,7 @@ public class ForkJoinPool extends Abstra * progress, it suffices for any in-progress poll or new push on * any empty queue to complete. * - * This approach also enables support a user mode in which local + * This approach also enables support of a user mode in which local * task processing is in FIFO, not LIFO order, simply by using * poll rather than pop. This can be useful in message-passing * frameworks in which tasks are never joined. However neither @@ -187,28 +187,29 @@ public class ForkJoinPool extends Abstra * rarely provide the best possible performance on a given * machine, but portably provide good throughput by averaging over * these factors. (Further, even if we did try to use such - * information, we do not usually have a basis for exploiting - * it. For example, some sets of tasks profit from cache - * affinities, but others are harmed by cache pollution effects.) + * information, we do not usually have a basis for exploiting it. + * For example, some sets of tasks profit from cache affinities, + * but others are harmed by cache pollution effects.) * * WorkQueues are also used in a similar way for tasks submitted * to the pool. We cannot mix these tasks in the same queues used * for work-stealing (this would contaminate lifo/fifo - * processing). Instead, we loosely associate (via hashing) - * submission queues with submitting threads, and randomly scan - * these queues as well when looking for work. In essence, - * submitters act like workers except that they never take tasks, - * and they are multiplexed on to a finite number of shared work - * queues. However, classes are set up so that future extensions - * could allow submitters to optionally help perform tasks as - * well. Pool submissions from internal workers are also allowed, - * but use randomized rather than thread-hashed queue indices to - * avoid imbalance. Insertion of tasks in shared mode requires a + * processing). Instead, we loosely associate submission queues + * with submitting threads, using a form of hashing. The + * ThreadLocal Submitter class contains a value initially used as + * a hash code for choosing existing queues, but may be randomly + * repositioned upon contention with other submitters. In + * essence, submitters act like workers except that they never + * take tasks, and they are multiplexed on to a finite number of + * shared work queues. However, classes are set up so that future + * extensions could allow submitters to optionally help perform + * tasks as well. Insertion of tasks in shared mode requires a * lock (mainly to protect in the case of resizing) but we use * only a simple spinlock (using bits in field runState), because - * submitters encountering a busy queue try others so never block. + * submitters encountering a busy queue move on to try or create + * other queues, so never block. * - * Management. + * Management * ========== * * The main throughput advantages of work-stealing stem from @@ -218,7 +219,7 @@ public class ForkJoinPool extends Abstra * tactic for avoiding bottlenecks is packing nearly all * essentially atomic control state into two volatile variables * that are by far most often read (not written) as status and - * consistency checks + * consistency checks. * * Field "ctl" contains 64 bits holding all the information needed * to atomically decide to add, inactivate, enqueue (on an event @@ -232,7 +233,10 @@ public class ForkJoinPool extends Abstra * deregister WorkQueues, as well as to enable shutdown. It is * only modified under a lock (normally briefly held, but * occasionally protecting allocations and resizings) but even - * when locked remains available to check consistency. + * when locked remains available to check consistency. An + * auxiliary field "growHints", also only modified under lock, + * contains a candidate index for the next WorkQueue and + * a mask for submission queue indices. * * Recording WorkQueues. WorkQueues are recorded in the * "workQueues" array that is created upon pool construction and @@ -248,9 +252,11 @@ public class ForkJoinPool extends Abstra * presized to hold twice #parallelism workers (which is unlikely * to need further resizing during execution). But to avoid * dealing with so many null slots, variable runState includes a - * mask for the nearest power of two that contains all current - * workers. All worker thread creation is on-demand, triggered by - * task submissions, replacement of terminated workers, and/or + * mask for the nearest power of two that contains all currently + * used indices. + * + * All worker thread creation is on-demand, triggered by task + * submissions, replacement of terminated workers, and/or * compensation for blocked workers. However, all other support * code is set up to work with other policies. To ensure that we * do not hold on to worker references that would prevent GC, ALL @@ -263,13 +269,12 @@ public class ForkJoinPool extends Abstra * both index-check and null-check the IDs. All such accesses * ignore bad IDs by returning out early from what they are doing, * since this can only be associated with termination, in which - * case it is OK to give up. - * - * All uses of the workQueues array check that it is non-null - * (even if previously non-null). This allows nulling during - * termination, which is currently not necessary, but remains an - * option for resource-revocation-based shutdown schemes. It also - * helps reduce JIT issuance of uncommon-trap code, which tends to + * case it is OK to give up. All uses of the workQueues array + * also check that it is non-null (even if previously + * non-null). This allows nulling during termination, which is + * currently not necessary, but remains an option for + * resource-revocation-based shutdown schemes. It also helps + * reduce JIT issuance of uncommon-trap code, which tends to * unnecessarily complicate control flow in some methods. * * Event Queuing. Unlike HPC work-stealing frameworks, we cannot @@ -297,7 +302,7 @@ public class ForkJoinPool extends Abstra * some other queued worker rather than itself, which has the same * net effect. Because enqueued workers may actually be rescanning * rather than waiting, we set and clear the "parker" field of - * Workqueues to reduce unnecessary calls to unpark. (this + * WorkQueues to reduce unnecessary calls to unpark. (This * requires a secondary recheck to avoid missed signals.) Note * the unusual conventions about Thread.interrupts surrounding * parking and other blocking: Because interrupts are used solely @@ -325,7 +330,7 @@ public class ForkJoinPool extends Abstra * terminating all workers after long periods of non-use. * * Shutdown and Termination. A call to shutdownNow atomically sets - * a runState bit and then (non-atomically) sets each workers + * a runState bit and then (non-atomically) sets each worker's * runState status, cancels all unprocessed tasks, and wakes up * all waiting workers. Detecting whether termination should * commence after a non-abrupt shutdown() call requires more work @@ -334,18 +339,18 @@ public class ForkJoinPool extends Abstra * indication but non-abrupt shutdown still requires a rechecking * scan for any workers that are inactive but not queued. * - * Joining Tasks. - * ============== + * Joining Tasks + * ============= * * Any of several actions may be taken when one worker is waiting - * to join a task stolen (or always held by) another. Because we + * to join a task stolen (or always held) by another. Because we * are multiplexing many tasks on to a pool of workers, we can't * just let them block (as in Thread.join). We also cannot just * reassign the joiner's run-time stack with another and replace * it later, which would be a form of "continuation", that even if * possible is not necessarily a good idea since we sometimes need - * both an unblocked task and its continuation to - * progress. Instead we combine two tactics: + * both an unblocked task and its continuation to progress. + * Instead we combine two tactics: * * Helping: Arranging for the joiner to execute some task that it * would be running if the steal had not occurred. @@ -380,7 +385,7 @@ public class ForkJoinPool extends Abstra * (http://portal.acm.org/citation.cfm?id=155354). It differs in * that: (1) We only maintain dependency links across workers upon * steals, rather than use per-task bookkeeping. This sometimes - * requires a linear scan of workers array to locate stealers, but + * requires a linear scan of workQueues array to locate stealers, but * often doesn't because stealers leave hints (that may become * stale/wrong) of where to locate them. A stealHint is only a * hint because a worker might have had multiple steals and the @@ -415,33 +420,58 @@ public class ForkJoinPool extends Abstra * managed by ForkJoinPool, so are directly accessed. There is * little point trying to reduce this, since any associated future * changes in representations will need to be accompanied by - * algorithmic changes anyway. All together, these low-level - * implementation choices produce as much as a factor of 4 - * performance improvement compared to naive implementations, and - * enable the processing of billions of tasks per second, at the - * expense of some ugliness. - * - * Methods signalWork() and scan() are the main bottlenecks so are - * especially heavily micro-optimized/mangled. There are lots of - * inline assignments (of form "while ((local = field) != 0)") - * which are usually the simplest way to ensure the required read - * orderings (which are sometimes critical). This leads to a - * "C"-like style of listing declarations of these locals at the - * heads of methods or blocks. There are several occurrences of - * the unusual "do {} while (!cas...)" which is the simplest way - * to force an update of a CAS'ed variable. There are also other - * coding oddities that help some methods perform reasonably even - * when interpreted (not compiled). - * - * The order of declarations in this file is: (1) declarations of - * statics (2) fields (along with constants used when unpacking - * some of them), listed in an order that tends to reduce - * contention among them a bit under most JVMs; (3) nested - * classes; (4) internal control methods; (5) callbacks and other - * support for ForkJoinTask methods; (6) exported methods (plus a - * few little helpers); (7) static block initializing all statics - * in a minimally dependent order. + * algorithmic changes anyway. Several methods intrinsically + * sprawl because they must accumulate sets of consistent reads of + * volatiles held in local variables. Methods signalWork() and + * scan() are the main bottlenecks, so are especially heavily + * micro-optimized/mangled. There are lots of inline assignments + * (of form "while ((local = field) != 0)") which are usually the + * simplest way to ensure the required read orderings (which are + * sometimes critical). This leads to a "C"-like style of listing + * declarations of these locals at the heads of methods or blocks. + * There are several occurrences of the unusual "do {} while + * (!cas...)" which is the simplest way to force an update of a + * CAS'ed variable. There are also other coding oddities that help + * some methods perform reasonably even when interpreted (not + * compiled). + * + * The order of declarations in this file is: + * (1) Static utility functions + * (2) Nested (static) classes + * (3) Static fields + * (4) Fields, along with constants used when unpacking some of them + * (5) Internal control methods + * (6) Callbacks and other support for ForkJoinTask methods + * (7) Exported methods + * (8) Static block initializing statics in minimally dependent order + */ + + // Static utilities + + /** + * Computes an initial hash code (also serving as a non-zero + * random seed) for a thread id. This method is expected to + * provide higher-quality hash codes than using method hashCode(). + */ + static final int hashId(long id) { + int h = (int)id ^ (int)(id >>> 32); // Use MurmurHash of thread id + h ^= h >>> 16; h *= 0x85ebca6b; + h ^= h >>> 13; h *= 0xc2b2ae35; + h ^= h >>> 16; + return (h == 0) ? 1 : h; // ensure nonzero + } + + /** + * If there is a security manager, makes sure caller has + * permission to modify threads. */ + private static void checkPermission() { + SecurityManager security = System.getSecurityManager(); + if (security != null) + security.checkPermission(modifyThreadPermission); + } + + // Nested classes /** * Factory for creating new {@link ForkJoinWorkerThread}s. @@ -471,164 +501,40 @@ public class ForkJoinPool extends Abstra } /** - * Creates a new ForkJoinWorkerThread. This factory is used unless - * overridden in ForkJoinPool constructors. - */ - public static final ForkJoinWorkerThreadFactory - defaultForkJoinWorkerThreadFactory; - - /** - * Permission required for callers of methods that may start or - * kill threads. - */ - private static final RuntimePermission modifyThreadPermission; - - /** - * If there is a security manager, makes sure caller has - * permission to modify threads. - */ - private static void checkPermission() { - SecurityManager security = System.getSecurityManager(); - if (security != null) - security.checkPermission(modifyThreadPermission); + * A simple non-reentrant lock used for exclusion when managing + * queues and workers. We use a custom lock so that we can readily + * probe lock state in constructions that check among alternative + * actions. The lock is normally only very briefly held, and + * sometimes treated as a spinlock, but other usages block to + * reduce overall contention in those cases where locked code + * bodies perform allocation/resizing. + */ + static final class Mutex extends AbstractQueuedSynchronizer { + public final boolean tryAcquire(int ignore) { + return compareAndSetState(0, 1); + } + public final boolean tryRelease(int ignore) { + setState(0); + return true; + } + public final void lock() { acquire(0); } + public final void unlock() { release(0); } + public final boolean isHeldExclusively() { return getState() == 1; } + public final Condition newCondition() { return new ConditionObject(); } } /** - * Generator for assigning sequence numbers as pool names. - */ - private static final AtomicInteger poolNumberGenerator; - - /** - * Bits and masks for control variables - * - * Field ctl is a long packed with: - * AC: Number of active running workers minus target parallelism (16 bits) - * TC: Number of total workers minus target parallelism (16 bits) - * ST: true if pool is terminating (1 bit) - * EC: the wait count of top waiting thread (15 bits) - * ID: ~(poolIndex >>> 1) of top of Treiber stack of waiters (16 bits) - * - * When convenient, we can extract the upper 32 bits of counts and - * the lower 32 bits of queue state, u = (int)(ctl >>> 32) and e = - * (int)ctl. The ec field is never accessed alone, but always - * together with id and st. The offsets of counts by the target - * parallelism and the positionings of fields makes it possible to - * perform the most common checks via sign tests of fields: When - * ac is negative, there are not enough active workers, when tc is - * negative, there are not enough total workers, when id is - * negative, there is at least one waiting worker, and when e is - * negative, the pool is terminating. To deal with these possibly - * negative fields, we use casts in and out of "short" and/or - * signed shifts to maintain signedness. - * - * When a thread is queued (inactivated), its eventCount field is - * negative, which is the only way to tell if a worker is - * prevented from executing tasks, even though it must continue to - * scan for them to avoid queuing races. - * - * Field runState is an int packed with: - * SHUTDOWN: true if shutdown is enabled (1 bit) - * SEQ: a sequence number updated upon (de)registering workers (15 bits) - * MASK: mask (power of 2 - 1) covering all registered poolIndexes (16 bits) - * - * The combination of mask and sequence number enables simple - * consistency checks: Staleness of read-only operations on the - * workers and queues arrays can be checked by comparing runState - * before vs after the reads. The low 16 bits (i.e, anding with - * SMASK) hold (the smallest power of two covering all worker - * indices, minus one. The mask for queues (vs workers) is twice - * this value plus 1. - */ - - // bit positions/shifts for fields - private static final int AC_SHIFT = 48; - private static final int TC_SHIFT = 32; - private static final int ST_SHIFT = 31; - private static final int EC_SHIFT = 16; - - // bounds - private static final int MAX_ID = 0x7fff; // max poolIndex - private static final int SMASK = 0xffff; // mask short bits - private static final int SHORT_SIGN = 1 << 15; - private static final int INT_SIGN = 1 << 31; - - // masks - private static final long STOP_BIT = 0x0001L << ST_SHIFT; - private static final long AC_MASK = ((long)SMASK) << AC_SHIFT; - private static final long TC_MASK = ((long)SMASK) << TC_SHIFT; - - // units for incrementing and decrementing - private static final long TC_UNIT = 1L << TC_SHIFT; - private static final long AC_UNIT = 1L << AC_SHIFT; - - // masks and units for dealing with u = (int)(ctl >>> 32) - private static final int UAC_SHIFT = AC_SHIFT - 32; - private static final int UTC_SHIFT = TC_SHIFT - 32; - private static final int UAC_MASK = SMASK << UAC_SHIFT; - private static final int UTC_MASK = SMASK << UTC_SHIFT; - private static final int UAC_UNIT = 1 << UAC_SHIFT; - private static final int UTC_UNIT = 1 << UTC_SHIFT; - - // masks and units for dealing with e = (int)ctl - private static final int E_MASK = 0x7fffffff; // no STOP_BIT - private static final int E_SEQ = 1 << EC_SHIFT; - - // runState bits - private static final int SHUTDOWN = 1 << 31; - private static final int RS_SEQ = 1 << 16; - private static final int RS_SEQ_MASK = 0x7fff0000; - - // access mode for WorkQueue - static final int LIFO_QUEUE = 0; - static final int FIFO_QUEUE = 1; - static final int SHARED_QUEUE = -1; - - /** - * The wakeup interval (in nanoseconds) for a worker waiting for a - * task when the pool is quiescent to instead try to shrink the - * number of workers. The exact value does not matter too - * much. It must be short enough to release resources during - * sustained periods of idleness, but not so short that threads - * are continually re-created. - */ - private static final long SHRINK_RATE = - 4L * 1000L * 1000L * 1000L; // 4 seconds - - /** - * The timeout value for attempted shrinkage, includes - * some slop to cope with system timer imprecision. - */ - private static final long SHRINK_TIMEOUT = SHRINK_RATE - (SHRINK_RATE / 10); - - /** - * The maximum stolen->joining link depth allowed in tryHelpStealer. - * Depths for legitimate chains are unbounded, but we use a fixed - * constant to avoid (otherwise unchecked) cycles and to bound - * staleness of traversal parameters at the expense of sometimes - * blocking when we could be helping. - */ - private static final int MAX_HELP_DEPTH = 16; - - /* - * Field layout order in this class tends to matter more than one - * would like. Runtime layout order is only loosely related to - * declaration order and may differ across JVMs, but the following - * empirically works OK on current JVMs. + * Class for artificial tasks that are used to replace the target + * of local joins if they are removed from an interior queue slot + * in WorkQueue.tryRemoveAndExec. We don't need the proxy to + * actually do anything beyond having a unique identity. */ - - volatile long ctl; // main pool control - final int parallelism; // parallelism level - final int localMode; // per-worker scheduling mode - int nextPoolIndex; // hint used in registerWorker - volatile int runState; // shutdown status, seq, and mask - WorkQueue[] workQueues; // main registry - final ReentrantLock lock; // for registration - final Condition termination; // for awaitTermination - final ForkJoinWorkerThreadFactory factory; // factory for new workers - final Thread.UncaughtExceptionHandler ueh; // per-worker UEH - final AtomicLong stealCount; // collect counts when terminated - final AtomicInteger nextWorkerNumber; // to create worker name string - final String workerNamePrefix; // Prefix for assigning worker names + static final class EmptyTask extends ForkJoinTask { + EmptyTask() { status = ForkJoinTask.NORMAL; } // force done + public final Void getRawResult() { return null; } + public final void setRawResult(Void x) {} + public final boolean exec() { return true; } + } /** * Queues supporting work-stealing as well as external task @@ -647,7 +553,7 @@ public class ForkJoinPool extends Abstra * for push, or under lock for trySharedPush, and accessed by * other threads only after reading (volatile) base. Both top and * base are allowed to wrap around on overflow, but (top - base) - * (or more comonly -(base - top) to force volatile read of base + * (or more commonly -(base - top) to force volatile read of base * before top) still estimates size. * * The array slots are read and written using the emulation of @@ -679,7 +585,7 @@ public class ForkJoinPool extends Abstra * avoiding really bad worst-case access. (Until better JVM * support is in place, this padding is dependent on transient * properties of JVM field layout rules.) We also take care in - * allocating and sizing and resizing the array. Non-shared queue + * allocating, sizing and resizing the array. Non-shared queue * arrays are initialized (via method growArray) by workers before * use. Others are allocated on first use. */ @@ -728,7 +634,7 @@ public class ForkJoinPool extends Abstra } /** - * Returns number of tasks in the queue + * Returns number of tasks in the queue. */ final int queueSize() { int n = base - top; // non-owner callers must read base first @@ -739,12 +645,10 @@ public class ForkJoinPool extends Abstra * Pushes a task. Call only by owner in unshared queues. * * @param task the task. Caller must ensure non-null. - * @param p, if non-null, pool to signal if necessary - * @throw RejectedExecutionException if array cannot - * be resized + * @param p if non-null, pool to signal if necessary + * @throw RejectedExecutionException if array cannot be resized */ final void push(ForkJoinTask task, ForkJoinPool p) { - boolean signal = false; ForkJoinTask[] a; int s = top, m, n; if ((a = array) != null) { // ignore if queue removed @@ -770,9 +674,9 @@ public class ForkJoinPool extends Abstra boolean submitted = false; if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { ForkJoinTask[] a = array; - int s = top, n = s - base; + int s = top; try { - if ((a != null && n < a.length - 1) || + if ((a != null && a.length > s + 1 - base) || (a = growArray(false)) != null) { // must presize int j = (((a.length - 1) & s) << ASHIFT) + ABASE; U.putObject(a, (long)j, task); // don't need "ordered" @@ -790,12 +694,11 @@ public class ForkJoinPool extends Abstra * Takes next task, if one exists, in FIFO order. */ final ForkJoinTask poll() { - ForkJoinTask[] a; int b, i; - while ((b = base) - top < 0 && (a = array) != null && - (i = (a.length - 1) & b) >= 0) { - int j = (i << ASHIFT) + ABASE; - ForkJoinTask t = (ForkJoinTask)U.getObjectVolatile(a, j); - if (t != null && base == b && + ForkJoinTask[] a; int b; ForkJoinTask t; + while ((b = base) - top < 0 && (a = array) != null) { + int j = (((a.length - 1) & b) << ASHIFT) + ABASE; + if ((t = (ForkJoinTask)U.getObjectVolatile(a, j)) != null && + base == b && U.compareAndSwapObject(a, j, t, null)) { base = b + 1; return t; @@ -805,8 +708,9 @@ public class ForkJoinPool extends Abstra } /** - * Takes next task, if one exists, in LIFO order. - * Call only by owner in unshared queues. + * Takes next task, if one exists, in LIFO order. Call only + * by owner in unshared queues. (We do not have a shared + * version of this method because it is never needed.) */ final ForkJoinTask pop() { ForkJoinTask t; int m; @@ -848,18 +752,17 @@ public class ForkJoinPool extends Abstra * Returns task at index b if b is current base of queue. */ final ForkJoinTask pollAt(int b) { - ForkJoinTask[] a; int i; - ForkJoinTask task = null; - if ((a = array) != null && (i = ((a.length - 1) & b)) >= 0) { - int j = (i << ASHIFT) + ABASE; - ForkJoinTask t = (ForkJoinTask)U.getObjectVolatile(a, j); - if (t != null && base == b && + ForkJoinTask t; ForkJoinTask[] a; + if ((a = array) != null) { + int j = (((a.length - 1) & b) << ASHIFT) + ABASE; + if ((t = (ForkJoinTask)U.getObjectVolatile(a, j)) != null && + base == b && U.compareAndSwapObject(a, j, t, null)) { base = b + 1; - task = t; + return t; } } - return task; + return null; } /** @@ -880,10 +783,9 @@ public class ForkJoinPool extends Abstra * Polls the given task only if it is at the current base. */ final boolean pollFor(ForkJoinTask task) { - ForkJoinTask[] a; int b, i; - if ((b = base) - top < 0 && (a = array) != null && - (i = (a.length - 1) & b) >= 0) { - int j = (i << ASHIFT) + ABASE; + ForkJoinTask[] a; int b; + if ((b = base) - top < 0 && (a = array) != null) { + int j = (((a.length - 1) & b) << ASHIFT) + ABASE; if (U.getObjectVolatile(a, j) == task && base == b && U.compareAndSwapObject(a, j, task, null)) { base = b + 1; @@ -977,7 +879,7 @@ public class ForkJoinPool extends Abstra } /** - * Removes and cancels all known tasks, ignoring any exceptions + * Removes and cancels all known tasks, ignoring any exceptions. */ final void cancelAll() { ForkJoinTask.cancelIgnoringExceptions(currentJoin); @@ -986,6 +888,20 @@ public class ForkJoinPool extends Abstra ForkJoinTask.cancelIgnoringExceptions(t); } + /** + * Computes next value for random probes. Scans don't require + * a very high quality generator, but also not a crummy one. + * Marsaglia xor-shift is cheap and works well enough. Note: + * This is manually inlined in several usages in ForkJoinPool + * to avoid writes inside busy scan loops. + */ + final int nextSeed() { + int r = seed; + r ^= r << 13; + r ^= r >>> 17; + return seed = r ^= r << 5; + } + // Execution methods /** @@ -1020,7 +936,7 @@ public class ForkJoinPool extends Abstra } /** - * Executes a non-top-level (stolen) task + * Executes a non-top-level (stolen) task. */ final void runSubtask(ForkJoinTask t) { if (t != null) { @@ -1032,18 +948,31 @@ public class ForkJoinPool extends Abstra } /** - * Computes next value for random probes. Scans don't require - * a very high quality generator, but also not a crummy one. - * Marsaglia xor-shift is cheap and works well enough. Note: - * This is manually inlined in several usages in ForkJoinPool - * to avoid writes inside busy scan loops. + * Returns true if owned and not known to be blocked. */ - final int nextSeed() { - int r = seed; - r ^= r << 13; - r ^= r >>> 17; - r ^= r << 5; - return seed = r; + final boolean isApparentlyUnblocked() { + Thread wt; Thread.State s; + return (eventCount >= 0 && + (wt = owner) != null && + (s = wt.getState()) != Thread.State.BLOCKED && + s != Thread.State.WAITING && + s != Thread.State.TIMED_WAITING); + } + + /** + * If this owned and is not already interrupted, try to + * interrupt and/or unpark, ignoring exceptions. + */ + final void interruptOwner() { + Thread wt, p; + if ((wt = owner) != null && !wt.isInterrupted()) { + try { + wt.interrupt(); + } catch (SecurityException ignore) { + } + } + if ((p = parker) != null) + U.unpark(p); } // Unsafe mechanics @@ -1071,61 +1000,202 @@ public class ForkJoinPool extends Abstra } /** - * Class for artificial tasks that are used to replace the target - * of local joins if they are removed from an interior queue slot - * in WorkQueue.tryRemoveAndExec. We don't need the proxy to - * actually do anything beyond having a unique identity. + * Per-thread records for threads that submit to pools. Currently + * holds only pseudo-random seed / index that is used to choose + * submission queues in method doSubmit. In the future, this may + * also incorporate a means to implement different task rejection + * and resubmission policies. */ - static final class EmptyTask extends ForkJoinTask { - EmptyTask() { status = ForkJoinTask.NORMAL; } // force done - public Void getRawResult() { return null; } - public void setRawResult(Void x) {} - public boolean exec() { return true; } + static final class Submitter { + int seed; + Submitter() { seed = hashId(Thread.currentThread().getId()); } + } + + /** ThreadLocal class for Submitters */ + static final class ThreadSubmitter extends ThreadLocal { + public Submitter initialValue() { return new Submitter(); } } + // static fields (initialized in static initializer below) + /** - * Computes a hash code for the given thread. This method is - * expected to provide higher-quality hash codes than those using - * method hashCode(). + * Creates a new ForkJoinWorkerThread. This factory is used unless + * overridden in ForkJoinPool constructors. */ - static final int hashThread(Thread t) { - long id = (t == null)? 0L : t.getId(); // Use MurmurHash of thread id - int h = (int)id ^ (int)(id >>> 32); - h ^= h >>> 16; - h *= 0x85ebca6b; - h ^= h >>> 13; - h *= 0xc2b2ae35; - return h ^ (h >>> 16); - } + public static final ForkJoinWorkerThreadFactory + defaultForkJoinWorkerThreadFactory; /** - * Top-level runloop for workers + * Generator for assigning sequence numbers as pool names. */ - final void runWorker(ForkJoinWorkerThread wt) { - WorkQueue w = wt.workQueue; - w.growArray(false); // Initialize queue array and seed in this thread - w.seed = hashThread(Thread.currentThread()) | (1 << 31); // force < 0 + private static final AtomicInteger poolNumberGenerator; - do {} while (w.runTask(scan(w))); - } + /** + * Permission required for callers of methods that may start or + * kill threads. + */ + private static final RuntimePermission modifyThreadPermission; + + /** + * Per-thread submission bookeeping. Shared across all pools + * to reduce ThreadLocal pollution and because random motion + * to avoid contention in one pool is likely to hold for others. + */ + private static final ThreadSubmitter submitters; - // Creating, registering and deregistering workers + // static constants + + /** + * The wakeup interval (in nanoseconds) for a worker waiting for a + * task when the pool is quiescent to instead try to shrink the + * number of workers. The exact value does not matter too + * much. It must be short enough to release resources during + * sustained periods of idleness, but not so short that threads + * are continually re-created. + */ + private static final long SHRINK_RATE = + 4L * 1000L * 1000L * 1000L; // 4 seconds + + /** + * The timeout value for attempted shrinkage, includes + * some slop to cope with system timer imprecision. + */ + private static final long SHRINK_TIMEOUT = SHRINK_RATE - (SHRINK_RATE / 10); + + /** + * The maximum stolen->joining link depth allowed in tryHelpStealer. + * Depths for legitimate chains are unbounded, but we use a fixed + * constant to avoid (otherwise unchecked) cycles and to bound + * staleness of traversal parameters at the expense of sometimes + * blocking when we could be helping. + */ + private static final int MAX_HELP_DEPTH = 16; + + /** + * Bits and masks for control variables + * + * Field ctl is a long packed with: + * AC: Number of active running workers minus target parallelism (16 bits) + * TC: Number of total workers minus target parallelism (16 bits) + * ST: true if pool is terminating (1 bit) + * EC: the wait count of top waiting thread (15 bits) + * ID: poolIndex of top of Treiber stack of waiters (16 bits) + * + * When convenient, we can extract the upper 32 bits of counts and + * the lower 32 bits of queue state, u = (int)(ctl >>> 32) and e = + * (int)ctl. The ec field is never accessed alone, but always + * together with id and st. The offsets of counts by the target + * parallelism and the positionings of fields makes it possible to + * perform the most common checks via sign tests of fields: When + * ac is negative, there are not enough active workers, when tc is + * negative, there are not enough total workers, and when e is + * negative, the pool is terminating. To deal with these possibly + * negative fields, we use casts in and out of "short" and/or + * signed shifts to maintain signedness. + * + * When a thread is queued (inactivated), its eventCount field is + * set negative, which is the only way to tell if a worker is + * prevented from executing tasks, even though it must continue to + * scan for them to avoid queuing races. Note however that + * eventCount updates lag releases so usage requires care. + * + * Field runState is an int packed with: + * SHUTDOWN: true if shutdown is enabled (1 bit) + * SEQ: a sequence number updated upon (de)registering workers (15 bits) + * MASK: mask (power of 2 - 1) covering all registered poolIndexes (16 bits) + * + * The combination of mask and sequence number enables simple + * consistency checks: Staleness of read-only operations on the + * workQueues array can be checked by comparing runState before vs + * after the reads. The low 16 bits (i.e, anding with SMASK) hold + * the smallest power of two covering all indices, minus + * one. + */ + + // bit positions/shifts for fields + private static final int AC_SHIFT = 48; + private static final int TC_SHIFT = 32; + private static final int ST_SHIFT = 31; + private static final int EC_SHIFT = 16; + + // bounds + private static final int POOL_MAX = 0x7fff; // max #workers - 1 + private static final int SMASK = 0xffff; // short bits + private static final int SQMASK = 0xfffe; // even short bits + private static final int SHORT_SIGN = 1 << 15; + private static final int INT_SIGN = 1 << 31; + + // masks + private static final long STOP_BIT = 0x0001L << ST_SHIFT; + private static final long AC_MASK = ((long)SMASK) << AC_SHIFT; + private static final long TC_MASK = ((long)SMASK) << TC_SHIFT; + + // units for incrementing and decrementing + private static final long TC_UNIT = 1L << TC_SHIFT; + private static final long AC_UNIT = 1L << AC_SHIFT; + + // masks and units for dealing with u = (int)(ctl >>> 32) + private static final int UAC_SHIFT = AC_SHIFT - 32; + private static final int UTC_SHIFT = TC_SHIFT - 32; + private static final int UAC_MASK = SMASK << UAC_SHIFT; + private static final int UTC_MASK = SMASK << UTC_SHIFT; + private static final int UAC_UNIT = 1 << UAC_SHIFT; + private static final int UTC_UNIT = 1 << UTC_SHIFT; + + // masks and units for dealing with e = (int)ctl + private static final int E_MASK = 0x7fffffff; // no STOP_BIT + private static final int E_SEQ = 1 << EC_SHIFT; + + // runState bits + private static final int SHUTDOWN = 1 << 31; + private static final int RS_SEQ = 1 << 16; + private static final int RS_SEQ_MASK = 0x7fff0000; + + // access mode for WorkQueue + static final int LIFO_QUEUE = 0; + static final int FIFO_QUEUE = 1; + static final int SHARED_QUEUE = -1; + + // Instance fields + + /* + * Field layout order in this class tends to matter more than one + * would like. Runtime layout order is only loosely related to + * declaration order and may differ across JVMs, but the following + * empirically works OK on current JVMs. + */ + + volatile long ctl; // main pool control + final int parallelism; // parallelism level + final int localMode; // per-worker scheduling mode + int growHints; // for expanding indices/ranges + volatile int runState; // shutdown status, seq, and mask + WorkQueue[] workQueues; // main registry + final Mutex lock; // for registration + final Condition termination; // for awaitTermination + final ForkJoinWorkerThreadFactory factory; // factory for new workers + final Thread.UncaughtExceptionHandler ueh; // per-worker UEH + final AtomicLong stealCount; // collect counts when terminated + final AtomicInteger nextWorkerNumber; // to create worker name string + final String workerNamePrefix; // to create worker name string + + // Creating, registering, deregistering and running workers /** * Tries to create and start a worker */ private void addWorker() { Throwable ex = null; - ForkJoinWorkerThread w = null; + ForkJoinWorkerThread wt = null; try { - if ((w = factory.newThread(this)) != null) { - w.start(); + if ((wt = factory.newThread(this)) != null) { + wt.start(); return; } } catch (Throwable e) { ex = e; } - deregisterWorker(w, ex); + deregisterWorker(wt, ex); // adjust counts etc on failure } /** @@ -1141,29 +1211,28 @@ public class ForkJoinPool extends Abstra /** * Callback from ForkJoinWorkerThread constructor to establish and - * record its WorkQueue + * record its WorkQueue. * * @param wt the worker thread */ final void registerWorker(ForkJoinWorkerThread wt) { WorkQueue w = wt.workQueue; - ReentrantLock lock = this.lock; + Mutex lock = this.lock; lock.lock(); try { - int k = nextPoolIndex; + int g = growHints, k = g & SMASK; WorkQueue[] ws = workQueues; if (ws != null) { // ignore on shutdown int n = ws.length; - if (k < 0 || (k & 1) == 0 || k >= n || ws[k] != null) { + if ((k & 1) == 0 || k >= n || ws[k] != null) { for (k = 1; k < n && ws[k] != null; k += 2) ; // workers are at odd indices if (k >= n) // resize workQueues = ws = Arrays.copyOf(ws, n << 1); } - w.poolIndex = k; - w.eventCount = ~(k >>> 1) & SMASK; // Set up wait count - ws[k] = w; // record worker - nextPoolIndex = k + 2; + w.eventCount = w.poolIndex = k; // establish before recording + ws[k] = w; + growHints = (g & ~SMASK) | ((k + 2) & SMASK); int rs = runState; int m = rs & SMASK; // recalculate runState mask if (k > m) @@ -1176,8 +1245,8 @@ public class ForkJoinPool extends Abstra } /** - * Final callback from terminating worker, as well as failure to - * construct or start a worker in addWorker. Removes record of + * Final callback from terminating worker, as well as upon failure + * to construct or start a worker in addWorker. Removes record of * worker from array, and adjusts counts. If pool is shutting * down, tries to complete termination. * @@ -1190,12 +1259,14 @@ public class ForkJoinPool extends Abstra w.runState = -1; // ensure runState is set stealCount.getAndAdd(w.totalSteals + w.nsteals); int idx = w.poolIndex; - ReentrantLock lock = this.lock; + Mutex lock = this.lock; lock.lock(); try { // remove record from array WorkQueue[] ws = workQueues; - if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w) - ws[nextPoolIndex = idx] = null; + if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w) { + ws[idx] = null; + growHints = (growHints & ~SMASK) | idx; + } } finally { lock.unlock(); } @@ -1207,21 +1278,92 @@ public class ForkJoinPool extends Abstra ((c - TC_UNIT) & TC_MASK) | (c & ~(AC_MASK|TC_MASK))))); - if (!tryTerminate(false) && w != null) { + if (!tryTerminate(false, false) && w != null) { w.cancelAll(); // cancel remaining tasks if (w.array != null) // suppress signal if never ran signalWork(); // wake up or create replacement + if (ex == null) // help clean refs on way out + ForkJoinTask.helpExpungeStaleExceptions(); } if (ex != null) // rethrow U.throwException(ex); } + /** + * Top-level runloop for workers, called by ForkJoinWorkerThread.run. + */ + final void runWorker(ForkJoinWorkerThread wt) { + // Initialize queue array and seed in this thread + WorkQueue w = wt.workQueue; + w.growArray(false); + w.seed = hashId(Thread.currentThread().getId()); + + do {} while (w.runTask(scan(w))); + } + + // Submissions + + /** + * Unless shutting down, adds the given task to a submission queue + * at submitter's current queue index (modulo submission + * range). If no queue exists at the index, one is created unless + * pool lock is busy. If the queue and/or lock are busy, another + * index is randomly chosen. The mask in growHints controls the + * effective index range of queues considered. The mask is + * expanded, up to the current workerQueue mask, upon any detected + * contention but otherwise remains small to avoid needlessly + * creating queues when there is no contention. + */ + private void doSubmit(ForkJoinTask task) { + if (task == null) + throw new NullPointerException(); + Submitter s = submitters.get(); + for (int r = s.seed, m = growHints >>> 16;;) { + WorkQueue[] ws; WorkQueue q; Mutex lk; + int k = r & m & SQMASK; // use only even indices + if (runState < 0 || (ws = workQueues) == null || ws.length <= k) + throw new RejectedExecutionException(); // shutting down + if ((q = ws[k]) == null && (lk = lock).tryAcquire(0)) { + try { // try to create new queue + if (ws == workQueues && (q = ws[k]) == null) { + int rs; // update runState seq + ws[k] = q = new WorkQueue(null, SHARED_QUEUE); + runState = (((rs = runState) & SHUTDOWN) | + ((rs + RS_SEQ) & ~SHUTDOWN)); + } + } finally { + lk.unlock(); + } + } + if (q != null) { + if (q.trySharedPush(task)) { + signalWork(); + return; + } + else if (m < parallelism - 1 && m < (runState & SMASK)) { + Mutex lock = this.lock; + lock.lock(); // block until lock free + int g = growHints; + if (g >>> 16 == m) // expand range + growHints = (((m << 1) + 1) << 16) | (g & SMASK); + lock.unlock(); // no need for try/finally + } + else if ((r & m) == 0) + Thread.yield(); // occasionally yield if busy + } + if (m == (m = growHints >>> 16)) { + r ^= r << 13; // update seed unless new range + r ^= r >>> 17; // same xorshift as WorkQueues + s.seed = r ^= r << 5; + } + } + } // Maintaining ctl counts /** - * Increments active count; mainly called upon return from blocking + * Increments active count; mainly called upon return from blocking. */ final void incrementActiveCount() { long c; @@ -1229,42 +1371,32 @@ public class ForkJoinPool extends Abstra } /** - * Activates or creates a worker + * Tries to activate or create a worker if too few are active. */ final void signalWork() { - /* - * The while condition is true if: (there is are too few total - * workers OR there is at least one waiter) AND (there are too - * few active workers OR the pool is terminating). The value - * of e distinguishes the remaining cases: zero (no waiters) - * for create, negative if terminating (in which case do - * nothing), else release a waiter. The secondary checks for - * release (non-null array etc) can fail if the pool begins - * terminating after the test, and don't impose any added cost - * because JVMs must perform null and bounds checks anyway. - */ - long c; int e, u; - while ((((e = (int)(c = ctl)) | (u = (int)(c >>> 32))) & - (INT_SIGN|SHORT_SIGN)) == (INT_SIGN|SHORT_SIGN)) { - WorkQueue[] ws = workQueues; int i; WorkQueue w; Thread p; - if (e == 0) { // add a new worker - if (U.compareAndSwapLong - (this, CTL, c, (long)(((u + UTC_UNIT) & UTC_MASK) | - ((u + UAC_UNIT) & UAC_MASK)) << 32)) { - addWorker(); - break; + long c; int u; + while ((u = (int)((c = ctl) >>> 32)) < 0) { // too few active + WorkQueue[] ws = workQueues; int e, i; WorkQueue w; Thread p; + if ((e = (int)c) > 0) { // at least one waiting + if (ws != null && (i = e & SMASK) < ws.length && + (w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) { + long nc = (((long)(w.nextWait & E_MASK)) | + ((long)(u + UAC_UNIT) << 32)); + if (U.compareAndSwapLong(this, CTL, c, nc)) { + w.eventCount = (e + E_SEQ) & E_MASK; + if ((p = w.parker) != null) + U.unpark(p); // activate and release + break; + } } + else + break; } - else if (e > 0 && ws != null && - (i = ((~e << 1) | 1) & SMASK) < ws.length && - (w = ws[i]) != null && - w.eventCount == (e | INT_SIGN)) { - if (U.compareAndSwapLong - (this, CTL, c, (((long)(w.nextWait & E_MASK)) | - ((long)(u + UAC_UNIT) << 32)))) { - w.eventCount = (e + E_SEQ) & E_MASK; - if ((p = w.parker) != null) - U.unpark(p); // release a waiting worker + else if (e == 0 && (u & SHORT_SIGN) != 0) { // too few total + long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | + ((u + UAC_UNIT) & UAC_MASK)) << 32; + if (U.compareAndSwapLong(this, CTL, c, nc)) { + addWorker(); break; } } @@ -1281,19 +1413,17 @@ public class ForkJoinPool extends Abstra * @return true if the caller can block, else should recheck and retry */ final boolean tryCompensate() { - WorkQueue[] ws; WorkQueue w; Thread p; + WorkQueue w; Thread p; int pc = parallelism, e, u, ac, tc, i; long c = ctl; - + WorkQueue[] ws = workQueues; if ((e = (int)c) >= 0) { if ((ac = ((u = (int)(c >>> 32)) >> UAC_SHIFT)) <= 0 && - e != 0 && (ws = workQueues) != null && - (i = ((~e << 1) | 1) & SMASK) < ws.length && + e != 0 && ws != null && (i = e & SMASK) < ws.length && (w = ws[i]) != null) { + long nc = (long)(w.nextWait & E_MASK) | (c & (AC_MASK|TC_MASK)); if (w.eventCount == (e | INT_SIGN) && - U.compareAndSwapLong - (this, CTL, c, ((long)(w.nextWait & E_MASK) | - (c & (AC_MASK|TC_MASK))))) { + U.compareAndSwapLong(this, CTL, c, nc)) { w.eventCount = (e + E_SEQ) & E_MASK; if ((p = w.parker) != null) U.unpark(p); @@ -1305,7 +1435,7 @@ public class ForkJoinPool extends Abstra if (U.compareAndSwapLong(this, CTL, c, nc)) return true; // no compensation needed } - else if (tc + pc < MAX_ID) { + else if (tc + pc < POOL_MAX) { long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); if (U.compareAndSwapLong(this, CTL, c, nc)) { addWorker(); @@ -1316,77 +1446,6 @@ public class ForkJoinPool extends Abstra return false; } - // Submissions - - /** - * Unless shutting down, adds the given task to some submission - * queue; using a randomly chosen queue index if the caller is a - * ForkJoinWorkerThread, else one based on caller thread's hash - * code. If no queue exists at the index, one is created. If the - * queue is busy, another is chosen by sweeping through the queues - * array. - */ - private void doSubmit(ForkJoinTask task) { - if (task == null) - throw new NullPointerException(); - Thread t = Thread.currentThread(); - int r = ((t instanceof ForkJoinWorkerThread) ? - ((ForkJoinWorkerThread)t).workQueue.nextSeed() : hashThread(t)); - for (;;) { - int rs = runState, m = rs & SMASK; - int j = r &= (m & ~1); // even numbered queues - WorkQueue[] ws = workQueues; - if (rs < 0 || ws == null) - throw new RejectedExecutionException(); // shutting down - if (ws.length > m) { // consistency check - for (WorkQueue q;;) { // circular sweep - if (((q = ws[j]) != null || - (q = tryAddSharedQueue(j)) != null) && - q.trySharedPush(task)) { - signalWork(); - return; - } - if ((j = (j + 2) & m) == r) { - Thread.yield(); // all queues busy - break; - } - } - } - } - } - - /** - * Tries to add and register a new queue at the given index. - * - * @param idx the workQueues array index to register the queue - * @return the queue, or null if could not add because could - * not acquire lock or idx is unusable - */ - private WorkQueue tryAddSharedQueue(int idx) { - WorkQueue q = null; - ReentrantLock lock = this.lock; - if (idx >= 0 && (idx & 1) == 0 && !lock.isLocked()) { - // create queue outside of lock but only if apparently free - WorkQueue nq = new WorkQueue(null, SHARED_QUEUE); - if (lock.tryLock()) { - try { - WorkQueue[] ws = workQueues; - if (ws != null && idx < ws.length) { - if ((q = ws[idx]) == null) { - int rs; // update runState seq - ws[idx] = q = nq; - runState = (((rs = runState) & SHUTDOWN) | - ((rs + RS_SEQ) & ~SHUTDOWN)); - } - } - } finally { - lock.unlock(); - } - } - } - return q; - } - // Scanning for tasks /** @@ -1398,7 +1457,7 @@ public class ForkJoinPool extends Abstra * re-invocation. * * The scan searches for tasks across queues, randomly selecting - * the first #queues probes, favoring steals 2:1 over submissions + * the first #queues probes, favoring steals over submissions * (by exploiting even/odd indexing), and then performing a * circular sweep of all queues. The scan terminates upon either * finding a non-empty queue, or completing a full sweep. If the @@ -1407,6 +1466,8 @@ public class ForkJoinPool extends Abstra * following actions, after which the caller will retry calling * this method unless terminated. * + * * If pool is terminating, terminate the worker. + * * * If not a complete sweep, try to release a waiting worker. If * the scan terminated because the worker is inactivated, then the * released worker will often be the calling worker, and it can @@ -1414,14 +1475,10 @@ public class ForkJoinPool extends Abstra * another worker, but with same net effect. Releasing in other * cases as well ensures that we have enough workers running. * - * * If the caller has run a task since the the last empty scan, + * * If the caller has run a task since the last empty scan, * return (to allow rescan) if other workers are not also yet * enqueued. Field WorkQueue.rescans counts down on each scan to - * ensure eventual inactivation, and occasional calls to - * Thread.yield to help avoid interference with more useful - * activities on the system. - * - * * If pool is terminating, terminate the worker + * ensure eventual inactivation and blocking. * * * If not already enqueued, try to inactivate and enqueue the * worker on wait queue. @@ -1435,83 +1492,80 @@ public class ForkJoinPool extends Abstra * @return a task or null of none found */ private final ForkJoinTask scan(WorkQueue w) { - boolean swept = false; // true after full empty scan - WorkQueue[] ws; // volatile read order matters - int r = w.seed, ec = w.eventCount; // ec is negative if inactive + boolean swept = false; // true after full empty scan + WorkQueue[] ws; // volatile read order matters + int r = w.seed, ec = w.eventCount; // ec is negative if inactive int rs = runState, m = rs & SMASK; - if ((ws = workQueues) != null && ws.length > m) { - ForkJoinTask task = null; - for (int k = 0, j = -2 - m; ; ++j) { + if ((ws = workQueues) != null && ws.length > m) { // consistency check + for (int k = 0, j = -1 - m; ; ++j) { WorkQueue q; int b; - if (j < 0) { // random probes while j negative + if (j < 0) { // random probes while j negative r ^= r << 13; r ^= r >>> 17; k = (r ^= r << 5) | (j & 1); - } // worker (not submit) for odd j - else // cyclic scan when j >= 0 - k += (m >>> 1) | 1; // step by half to reduce bias - + } // worker (not submit) for odd j + else // cyclic scan when j >= 0 + k += 7; // step 7 reduces array packing bias if ((q = ws[k & m]) != null && (b = q.base) - q.top < 0) { - if (ec >= 0) - task = q.pollAt(b); // steal + ForkJoinTask t = (ec >= 0) ? q.pollAt(b) : null; + w.seed = r; // save seed for next scan + if (t != null) + return t; break; } - else if (j > m) { - if (rs == runState) // staleness check + else if (j - m > m) { + if (rs == runState) // staleness check swept = true; break; } } - w.seed = r; // save seed for next scan - if (task != null) - return task; - } - - // Decode ctl on empty scan - long c = ctl; int e = (int)c, a = (int)(c >> AC_SHIFT), nr, ns; - if (!swept) { // try to release a waiter - WorkQueue v; Thread p; - if (e > 0 && a < 0 && ws != null && - (v = ws[((~e << 1) | 1) & m]) != null && - v.eventCount == (e | INT_SIGN) && U.compareAndSwapLong - (this, CTL, c, ((long)(v.nextWait & E_MASK) | - ((c + AC_UNIT) & (AC_MASK|TC_MASK))))) { - v.eventCount = (e + E_SEQ) & E_MASK; - if ((p = v.parker) != null) - U.unpark(p); - } - } - else if ((nr = w.rescans) > 0) { // continue rescanning - int ac = a + parallelism; - if ((w.rescans = (ac < nr) ? ac : nr - 1) > 0 && w.seed < 0 && - w.eventCount == ec) - Thread.yield(); // 1 bit randomness for yield call - } - else if (e < 0) // pool is terminating - w.runState = -1; - else if (ec >= 0) { // try to enqueue - long nc = (long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)); - w.nextWait = e; - w.eventCount = ec | INT_SIGN; // mark as inactive - if (!U.compareAndSwapLong(this, CTL, c, nc)) - w.eventCount = ec; // back out on CAS failure - else if ((ns = w.nsteals) != 0) { // set rescans if ran task - if (a <= 0) // ... unless too many active + + // Decode ctl on empty scan + long c = ctl; int e = (int)c, a = (int)(c >> AC_SHIFT), nr, ns; + if (e < 0) // pool is terminating + w.runState = -1; + else if (!swept) { // try to release a waiter + WorkQueue v; Thread p; + if (e > 0 && a < 0 && (v = ws[e & m]) != null && + v.eventCount == (e | INT_SIGN)) { + long nc = ((long)(v.nextWait & E_MASK) | + ((c + AC_UNIT) & (AC_MASK|TC_MASK))); + if (U.compareAndSwapLong(this, CTL, c, nc)) { + v.eventCount = (e + E_SEQ) & E_MASK; + if ((p = v.parker) != null) + U.unpark(p); + } + } + } + else if ((nr = w.rescans) > 0) { // continue rescanning + int ac = a + parallelism; + if (((w.rescans = (ac < nr) ? ac : nr - 1) & 3) == 0 && + w.eventCount == ec) + Thread.yield(); // occasionally yield + } + else if (ec >= 0) { // try to enqueue + long nc = (long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)); + w.nextWait = e; + w.eventCount = ec | INT_SIGN;// mark as inactive + if (!U.compareAndSwapLong(this, CTL, c, nc)) + w.eventCount = ec; // unmark on CAS failure + else if ((ns = w.nsteals) != 0) { + w.nsteals = 0; // set rescans if ran task w.rescans = a + parallelism; - w.nsteals = 0; - w.totalSteals += ns; + w.totalSteals += ns; + } } - } - else{ // already queued - if (parallelism == -a) - idleAwaitWork(w); // quiescent - if (w.eventCount == ec) { - Thread.interrupted(); // clear status - ForkJoinWorkerThread wt = w.owner; - U.putObject(wt, PARKBLOCKER, this); - w.parker = wt; // emulate LockSupport.park - if (w.eventCount == ec) // recheck - U.park(false, 0L); // block - w.parker = null; - U.putObject(wt, PARKBLOCKER, null); + else{ // already queued + if (parallelism == -a) + idleAwaitWork(w); // quiescent + if (w.eventCount == ec) { + Thread.interrupted(); // clear status + ForkJoinWorkerThread wt = w.owner; + U.putObject(wt, PARKBLOCKER, this); + w.parker = wt; // emulate LockSupport.park + if (w.eventCount == ec) // recheck + U.park(false, 0L); // block + w.parker = null; + U.putObject(wt, PARKBLOCKER, null); + } } } return null; @@ -1519,23 +1573,22 @@ public class ForkJoinPool extends Abstra /** * If inactivating worker w has caused pool to become quiescent, - * check for pool termination, and, so long as this is not the - * only worker, wait for event for up to SHRINK_RATE nanosecs On - * timeout, if ctl has not changed, terminate the worker, which - * will in turn wake up another worker to possibly repeat this - * process. + * checks for pool termination, and, so long as this is not the + * only worker, waits for event for up to SHRINK_RATE nanosecs. + * On timeout, if ctl has not changed, terminates the worker, + * which will in turn wake up another worker to possibly repeat + * this process. * * @param w the calling worker */ private void idleAwaitWork(WorkQueue w) { long c; int nw, ec; - if (!tryTerminate(false) && + if (!tryTerminate(false, false) && (int)((c = ctl) >> AC_SHIFT) + parallelism == 0 && (ec = w.eventCount) == ((int)c | INT_SIGN) && (nw = w.nextWait) != 0) { long nc = ((long)(nw & E_MASK) | // ctl to restore on timeout ((c + AC_UNIT) & AC_MASK) | (c & TC_MASK)); - ForkJoinTask.helpExpungeStaleExceptions(); // help clean ForkJoinWorkerThread wt = w.owner; while (ctl == c) { long startTime = System.nanoTime(); @@ -1550,8 +1603,8 @@ public class ForkJoinPool extends Abstra break; if (System.nanoTime() - startTime >= SHRINK_TIMEOUT && U.compareAndSwapLong(this, CTL, c, nc)) { - w.runState = -1; // shrink w.eventCount = (ec + E_SEQ) | E_MASK; + w.runState = -1; // shrink break; } } @@ -1651,7 +1704,7 @@ public class ForkJoinPool extends Abstra } /** - * Returns a non-empty steal queue, if is found during a random, + * Returns a non-empty steal queue, if one is found during a random, * then cyclic scan, else null. This method must be retried by * caller if, by the time it tries to use the queue, it is empty. */ @@ -1663,17 +1716,18 @@ public class ForkJoinPool extends Abstra return null; if (ws.length > m) { WorkQueue q; - for (int n = m << 2, k = r, j = -n;;) { - r ^= r << 13; r ^= r >>> 17; r ^= r << 5; + for (int k = 0, j = -1 - m;; ++j) { + if (j < 0) { + r ^= r << 13; r ^= r >>> 17; k = r ^= r << 5; + } + else + k += 7; if ((q = ws[(k | 1) & m]) != null && q.base - q.top < 0) { w.seed = r; return q; } - else if (j > n) + else if (j - m > m) return null; - else - k = (j++ < 0) ? r : k + ((m >>> 1) | 1); - } } } @@ -1719,7 +1773,7 @@ public class ForkJoinPool extends Abstra } /** - * Gets and removes a local or stolen task for the given worker + * Gets and removes a local or stolen task for the given worker. * * @return a task, if available */ @@ -1751,99 +1805,85 @@ public class ForkJoinPool extends Abstra 8); } - // Termination + // Termination /** - * Sets SHUTDOWN bit of runState under lock - */ - private void enableShutdown() { - ReentrantLock lock = this.lock; - if (runState >= 0) { - lock.lock(); // don't need try/finally - runState |= SHUTDOWN; - lock.unlock(); - } - } - - /** - * Possibly initiates and/or completes termination. Upon - * termination, cancels all queued tasks and then + * Possibly initiates and/or completes termination. The caller + * triggering termination runs three passes through workQueues: + * (0) Setting termination status, followed by wakeups of queued + * workers; (1) cancelling all tasks; (2) interrupting lagging + * threads (likely in external tasks, but possibly also blocked in + * joins). Each pass repeats previous steps because of potential + * lagging thread creation. * * @param now if true, unconditionally terminate, else only * if no work and no active workers + * @param enable if true, enable shutdown when next possible * @return true if now terminating or terminated */ - private boolean tryTerminate(boolean now) { + private boolean tryTerminate(boolean now, boolean enable) { + Mutex lock = this.lock; for (long c;;) { if (((c = ctl) & STOP_BIT) != 0) { // already terminating if ((short)(c >>> TC_SHIFT) == -parallelism) { - ReentrantLock lock = this.lock; // signal when no workers lock.lock(); // don't need try/finally termination.signalAll(); // signal when 0 workers lock.unlock(); } return true; } - if (!now) { - if ((int)(c >> AC_SHIFT) != -parallelism || runState >= 0 || + if (runState >= 0) { // not yet enabled + if (!enable) + return false; + lock.lock(); + runState |= SHUTDOWN; + lock.unlock(); + } + if (!now) { // check if idle & no tasks + if ((int)(c >> AC_SHIFT) != -parallelism || hasQueuedSubmissions()) return false; // Check for unqueued inactive workers. One pass suffices. WorkQueue[] ws = workQueues; WorkQueue w; if (ws != null) { - int n = ws.length; - for (int i = 1; i < n; i += 2) { + for (int i = 1; i < ws.length; i += 2) { if ((w = ws[i]) != null && w.eventCount >= 0) return false; } } } - if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) - startTerminating(); - } - } - - /** - * Initiates termination: Runs three passes through workQueues: - * (0) Setting termination status, followed by wakeups of queued - * workers; (1) cancelling all tasks; (2) interrupting lagging - * threads (likely in external tasks, but possibly also blocked in - * joins). Each pass repeats previous steps because of potential - * lagging thread creation. - */ - private void startTerminating() { - for (int pass = 0; pass < 3; ++pass) { - WorkQueue[] ws = workQueues; - if (ws != null) { - WorkQueue w; Thread wt; - int n = ws.length; - for (int j = 0; j < n; ++j) { - if ((w = ws[j]) != null) { - w.runState = -1; - if (pass > 0) { - w.cancelAll(); - if (pass > 1 && (wt = w.owner) != null && - !wt.isInterrupted()) { - try { - wt.interrupt(); - } catch (SecurityException ignore) { + if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) { + for (int pass = 0; pass < 3; ++pass) { + WorkQueue[] ws = workQueues; + if (ws != null) { + WorkQueue w; + int n = ws.length; + for (int i = 0; i < n; ++i) { + if ((w = ws[i]) != null) { + w.runState = -1; + if (pass > 0) { + w.cancelAll(); + if (pass > 1) + w.interruptOwner(); } } } - } - } - // Wake up workers parked on event queue - int i, e; long c; Thread p; - while ((i = ((~(e = (int)(c = ctl)) << 1) | 1) & SMASK) < n && - (w = ws[i]) != null && - w.eventCount == (e | INT_SIGN)) { - long nc = ((long)(w.nextWait & E_MASK) | - ((c + AC_UNIT) & AC_MASK) | - (c & (TC_MASK|STOP_BIT))); - if (U.compareAndSwapLong(this, CTL, c, nc)) { - w.eventCount = (e + E_SEQ) & E_MASK; - if ((p = w.parker) != null) - U.unpark(p); + // Wake up workers parked on event queue + int i, e; long cc; Thread p; + while ((e = (int)(cc = ctl) & E_MASK) != 0 && + (i = e & SMASK) < n && + (w = ws[i]) != null) { + long nc = ((long)(w.nextWait & E_MASK) | + ((cc + AC_UNIT) & AC_MASK) | + (cc & (TC_MASK|STOP_BIT))); + if (w.eventCount == (e | INT_SIGN) && + U.compareAndSwapLong(this, CTL, cc, nc)) { + w.eventCount = (e + E_SEQ) & E_MASK; + w.runState = -1; + if ((p = w.parker) != null) + U.unpark(p); + } + } } } } @@ -1919,35 +1959,30 @@ public class ForkJoinPool extends Abstra checkPermission(); if (factory == null) throw new NullPointerException(); - if (parallelism <= 0 || parallelism > MAX_ID) + if (parallelism <= 0 || parallelism > POOL_MAX) throw new IllegalArgumentException(); this.parallelism = parallelism; this.factory = factory; this.ueh = handler; this.localMode = asyncMode ? FIFO_QUEUE : LIFO_QUEUE; - this.nextPoolIndex = 1; + this.growHints = 1; long np = (long)(-parallelism); // offset ctl counts this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); // initialize workQueues array with room for 2*parallelism if possible int n = parallelism << 1; - if (n >= MAX_ID) - n = MAX_ID; + if (n >= POOL_MAX) + n = POOL_MAX; else { // See Hackers Delight, sec 3.2, where n < (1 << 16) n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; } - this.workQueues = new WorkQueue[(n + 1) << 1]; - ReentrantLock lck = this.lock = new ReentrantLock(); - this.termination = lck.newCondition(); + this.workQueues = new WorkQueue[(n + 1) << 1]; // #slots = 2 * #workers + this.termination = (this.lock = new Mutex()).newCondition(); this.stealCount = new AtomicLong(); this.nextWorkerNumber = new AtomicInteger(); StringBuilder sb = new StringBuilder("ForkJoinPool-"); sb.append(poolNumberGenerator.incrementAndGet()); sb.append("-worker-"); this.workerNamePrefix = sb.toString(); - // Create initial submission queue - WorkQueue sq = tryAddSharedQueue(0); - if (sq != null) - sq.growArray(false); } // Execution methods @@ -2065,25 +2100,31 @@ public class ForkJoinPool extends Abstra * @throws RejectedExecutionException {@inheritDoc} */ public List> invokeAll(Collection> tasks) { - ArrayList> forkJoinTasks = - new ArrayList>(tasks.size()); - for (Callable task : tasks) - forkJoinTasks.add(ForkJoinTask.adapt(task)); - invoke(new InvokeAll(forkJoinTasks)); - + // In previous versions of this class, this method constructed + // a task to run ForkJoinTask.invokeAll, but now external + // invocation of multiple tasks is at least as efficient. + List> fs = new ArrayList>(tasks.size()); + // Workaround needed because method wasn't declared with + // wildcards in return type but should have been. @SuppressWarnings({"unchecked", "rawtypes"}) - List> futures = (List>) (List) forkJoinTasks; - return futures; - } + List> futures = (List>) (List) fs; - static final class InvokeAll extends RecursiveAction { - final ArrayList> tasks; - InvokeAll(ArrayList> tasks) { this.tasks = tasks; } - public void compute() { - try { invokeAll(tasks); } - catch (Exception ignore) {} + boolean done = false; + try { + for (Callable t : tasks) { + ForkJoinTask f = ForkJoinTask.adapt(t); + doSubmit(f); + fs.add(f); + } + for (ForkJoinTask f : fs) + f.quietlyJoin(); + done = true; + return futures; + } finally { + if (!done) + for (ForkJoinTask f : fs) + f.cancel(false); } - private static final long serialVersionUID = -7914297376763021607L; } /** @@ -2148,14 +2189,8 @@ public class ForkJoinPool extends Abstra int rc = 0; WorkQueue[] ws; WorkQueue w; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 1; i < n; i += 2) { - Thread.State s; ForkJoinWorkerThread wt; - if ((w = ws[i]) != null && (wt = w.owner) != null && - w.eventCount >= 0 && - (s = wt.getState()) != Thread.State.BLOCKED && - s != Thread.State.WAITING && - s != Thread.State.TIMED_WAITING) + for (int i = 1; i < ws.length; i += 2) { + if ((w = ws[i]) != null && w.isApparentlyUnblocked()) ++rc; } } @@ -2204,8 +2239,7 @@ public class ForkJoinPool extends Abstra long count = stealCount.get(); WorkQueue[] ws; WorkQueue w; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 1; i < n; i += 2) { + for (int i = 1; i < ws.length; i += 2) { if ((w = ws[i]) != null) count += w.totalSteals; } @@ -2227,8 +2261,7 @@ public class ForkJoinPool extends Abstra long count = 0; WorkQueue[] ws; WorkQueue w; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 1; i < n; i += 2) { + for (int i = 1; i < ws.length; i += 2) { if ((w = ws[i]) != null) count += w.queueSize(); } @@ -2247,8 +2280,7 @@ public class ForkJoinPool extends Abstra int count = 0; WorkQueue[] ws; WorkQueue w; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 0; i < n; i += 2) { + for (int i = 0; i < ws.length; i += 2) { if ((w = ws[i]) != null) count += w.queueSize(); } @@ -2265,8 +2297,7 @@ public class ForkJoinPool extends Abstra public boolean hasQueuedSubmissions() { WorkQueue[] ws; WorkQueue w; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 0; i < n; i += 2) { + for (int i = 0; i < ws.length; i += 2) { if ((w = ws[i]) != null && w.queueSize() != 0) return true; } @@ -2284,8 +2315,7 @@ public class ForkJoinPool extends Abstra protected ForkJoinTask pollSubmission() { WorkQueue[] ws; WorkQueue w; ForkJoinTask t; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 0; i < n; i += 2) { + for (int i = 0; i < ws.length; i += 2) { if ((w = ws[i]) != null && (t = w.poll()) != null) return t; } @@ -2314,8 +2344,7 @@ public class ForkJoinPool extends Abstra int count = 0; WorkQueue[] ws; WorkQueue w; ForkJoinTask t; if ((ws = workQueues) != null) { - int n = ws.length; - for (int i = 0; i < n; ++i) { + for (int i = 0; i < ws.length; ++i) { if ((w = ws[i]) != null) { while ((t = w.poll()) != null) { c.add(t); @@ -2335,12 +2364,27 @@ public class ForkJoinPool extends Abstra * @return a string identifying this pool, as well as its state */ public String toString() { - long st = getStealCount(); - long qt = getQueuedTaskCount(); - long qs = getQueuedSubmissionCount(); - int rc = getRunningThreadCount(); - int pc = parallelism; + // Use a single pass through workQueues to collect counts + long qt = 0L, qs = 0L; int rc = 0; + long st = stealCount.get(); long c = ctl; + WorkQueue[] ws; WorkQueue w; + if ((ws = workQueues) != null) { + for (int i = 0; i < ws.length; ++i) { + if ((w = ws[i]) != null) { + int size = w.queueSize(); + if ((i & 1) == 0) + qs += size; + else { + qt += size; + st += w.totalSteals; + if (w.isApparentlyUnblocked()) + ++rc; + } + } + } + } + int pc = parallelism; int tc = pc + (short)(c >>> TC_SHIFT); int ac = pc + (int)(c >> AC_SHIFT); if (ac < 0) // ignore transient negative @@ -2376,8 +2420,7 @@ public class ForkJoinPool extends Abstra */ public void shutdown() { checkPermission(); - enableShutdown(); - tryTerminate(false); + tryTerminate(false, true); } /** @@ -2398,8 +2441,7 @@ public class ForkJoinPool extends Abstra */ public List shutdownNow() { checkPermission(); - enableShutdown(); - tryTerminate(true); + tryTerminate(true, true); return Collections.emptyList(); } @@ -2456,7 +2498,7 @@ public class ForkJoinPool extends Abstra public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); - final ReentrantLock lock = this.lock; + final Mutex lock = this.lock; lock.lock(); try { for (;;) { @@ -2552,7 +2594,7 @@ public class ForkJoinPool extends Abstra * *

If the caller is not a {@link ForkJoinTask}, this method is * behaviorally equivalent to -a *

 {@code
+     *  
 {@code
      * while (!blocker.isReleasable())
      *   if (blocker.block())
      *     return;
@@ -2597,7 +2639,6 @@ a     *  
 {@code
     // Unsafe mechanics
     private static final sun.misc.Unsafe U;
     private static final long CTL;
-    private static final long RUNSTATE;
     private static final long PARKBLOCKER;
 
     static {
@@ -2605,15 +2646,13 @@ a     *  
 {@code
         modifyThreadPermission = new RuntimePermission("modifyThread");
         defaultForkJoinWorkerThreadFactory =
             new DefaultForkJoinWorkerThreadFactory();
-        int s;
+        submitters = new ThreadSubmitter();
         try {
             U = getUnsafe();
             Class k = ForkJoinPool.class;
-            Class tk = Thread.class;
             CTL = U.objectFieldOffset
                 (k.getDeclaredField("ctl"));
-            RUNSTATE = U.objectFieldOffset
-                (k.getDeclaredField("runState"));
+            Class tk = Thread.class;
             PARKBLOCKER = U.objectFieldOffset
                 (tk.getDeclaredField("parkBlocker"));
         } catch (Exception e) {
@@ -2648,4 +2687,5 @@ a     *  
 {@code
             }
         }
     }
+
 }