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

Comparing jsr166/src/jsr166y/ForkJoinWorkerThread.java (file contents):
Revision 1.24 by jsr166, Fri Jul 31 19:52:39 2009 UTC vs.
Revision 1.60 by dl, Tue Nov 23 00:10:39 2010 UTC

# Line 6 | Line 6
6  
7   package jsr166y;
8  
9 < import java.util.concurrent.*;
10 <
9 > import java.util.Random;
10   import java.util.Collection;
11 + import java.util.concurrent.locks.LockSupport;
12 + import java.util.concurrent.RejectedExecutionException;
13  
14   /**
15 < * A thread managed by a {@link ForkJoinPool}.  This class is
16 < * subclassable solely for the sake of adding functionality -- there
17 < * are no overridable methods dealing with scheduling or
18 < * execution. However, you can override initialization and termination
19 < * methods surrounding the main task processing loop.  If you do
20 < * create such a subclass, you will also need to supply a custom
21 < * ForkJoinWorkerThreadFactory to use it in a ForkJoinPool.
15 > * A thread managed by a {@link ForkJoinPool}, which executes
16 > * {@link ForkJoinTask}s.
17 > * This class is subclassable solely for the sake of adding
18 > * functionality -- there are no overridable methods dealing with
19 > * scheduling or execution.  However, you can override initialization
20 > * and termination methods surrounding the main task processing loop.
21 > * If you do create such a subclass, you will also need to supply a
22 > * custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it
23 > * in a {@code ForkJoinPool}.
24   *
25   * @since 1.7
26   * @author Doug Lea
27   */
28   public class ForkJoinWorkerThread extends Thread {
29      /*
30 <     * Algorithm overview:
30 >     * Overview:
31       *
32 <     * 1. Work-Stealing: Work-stealing queues are special forms of
33 <     * Deques that support only three of the four possible
34 <     * end-operations -- push, pop, and deq (aka steal), and only do
35 <     * so under the constraints that push and pop are called only from
36 <     * the owning thread, while deq may be called from other threads.
37 <     * (If you are unfamiliar with them, you probably want to read
38 <     * Herlihy and Shavit's book "The Art of Multiprocessor
39 <     * programming", chapter 16 describing these in more detail before
40 <     * proceeding.)  The main work-stealing queue design is roughly
41 <     * similar to "Dynamic Circular Work-Stealing Deque" by David
42 <     * Chase and Yossi Lev, SPAA 2005
43 <     * (http://research.sun.com/scalable/pubs/index.html).  The main
44 <     * difference ultimately stems from gc requirements that we null
45 <     * out taken slots as soon as we can, to maintain as small a
46 <     * footprint as possible even in programs generating huge numbers
47 <     * of tasks. To accomplish this, we shift the CAS arbitrating pop
48 <     * vs deq (steal) from being on the indices ("base" and "sp") to
49 <     * the slots themselves (mainly via method "casSlotNull()"). So,
50 <     * both a successful pop and deq mainly entail CAS'ing a non-null
51 <     * slot to null.  Because we rely on CASes of references, we do
52 <     * not need tag bits on base or sp.  They are simple ints as used
53 <     * in any circular array-based queue (see for example ArrayDeque).
54 <     * Updates to the indices must still be ordered in a way that
55 <     * guarantees that (sp - base) > 0 means the queue is empty, but
56 <     * otherwise may err on the side of possibly making the queue
57 <     * appear nonempty when a push, pop, or deq have not fully
58 <     * committed. Note that this means that the deq operation,
59 <     * considered individually, is not wait-free. One thief cannot
60 <     * successfully continue until another in-progress one (or, if
61 <     * previously empty, a push) completes.  However, in the
62 <     * aggregate, we ensure at least probabilistic non-blockingness. If
63 <     * an attempted steal fails, a thief always chooses a different
32 >     * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
33 >     * ForkJoinTasks. This class includes bookkeeping in support of
34 >     * worker activation, suspension, and lifecycle control described
35 >     * in more detail in the internal documentation of class
36 >     * ForkJoinPool. And as described further below, this class also
37 >     * includes special-cased support for some ForkJoinTask
38 >     * methods. But the main mechanics involve work-stealing:
39 >     *
40 >     * Work-stealing queues are special forms of Deques that support
41 >     * only three of the four possible end-operations -- push, pop,
42 >     * and deq (aka steal), under the further constraints that push
43 >     * and pop are called only from the owning thread, while deq may
44 >     * be called from other threads.  (If you are unfamiliar with
45 >     * them, you probably want to read Herlihy and Shavit's book "The
46 >     * Art of Multiprocessor programming", chapter 16 describing these
47 >     * in more detail before proceeding.)  The main work-stealing
48 >     * queue design is roughly similar to those in the papers "Dynamic
49 >     * Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005
50 >     * (http://research.sun.com/scalable/pubs/index.html) and
51 >     * "Idempotent work stealing" by Michael, Saraswat, and Vechev,
52 >     * PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186).
53 >     * The main differences ultimately stem from gc requirements that
54 >     * we null out taken slots as soon as we can, to maintain as small
55 >     * a footprint as possible even in programs generating huge
56 >     * numbers of tasks. To accomplish this, we shift the CAS
57 >     * arbitrating pop vs deq (steal) from being on the indices
58 >     * ("base" and "sp") to the slots themselves (mainly via method
59 >     * "casSlotNull()"). So, both a successful pop and deq mainly
60 >     * entail a CAS of a slot from non-null to null.  Because we rely
61 >     * on CASes of references, we do not need tag bits on base or sp.
62 >     * They are simple ints as used in any circular array-based queue
63 >     * (see for example ArrayDeque).  Updates to the indices must
64 >     * still be ordered in a way that guarantees that sp == base means
65 >     * the queue is empty, but otherwise may err on the side of
66 >     * possibly making the queue appear nonempty when a push, pop, or
67 >     * deq have not fully committed. Note that this means that the deq
68 >     * operation, considered individually, is not wait-free. One thief
69 >     * cannot successfully continue until another in-progress one (or,
70 >     * if previously empty, a push) completes.  However, in the
71 >     * aggregate, we ensure at least probabilistic non-blockingness.
72 >     * If an attempted steal fails, a thief always chooses a different
73       * random victim target to try next. So, in order for one thief to
74       * progress, it suffices for any in-progress deq or new push on
75       * any empty queue to complete. One reason this works well here is
76       * that apparently-nonempty often means soon-to-be-stealable,
77 <     * which gives threads a chance to activate if necessary before
78 <     * stealing (see below).
77 >     * which gives threads a chance to set activation status if
78 >     * necessary before stealing.
79       *
80       * This approach also enables support for "async mode" where local
81       * task processing is in FIFO, not LIFO order; simply by using a
# Line 71 | Line 83 | public class ForkJoinWorkerThread extend
83       * by the ForkJoinPool).  This allows use in message-passing
84       * frameworks in which tasks are never joined.
85       *
86 <     * Efficient implementation of this approach currently relies on
87 <     * an uncomfortable amount of "Unsafe" mechanics. To maintain
86 >     * When a worker would otherwise be blocked waiting to join a
87 >     * task, it first tries a form of linear helping: Each worker
88 >     * records (in field currentSteal) the most recent task it stole
89 >     * from some other worker. Plus, it records (in field currentJoin)
90 >     * the task it is currently actively joining. Method joinTask uses
91 >     * these markers to try to find a worker to help (i.e., steal back
92 >     * a task from and execute it) that could hasten completion of the
93 >     * actively joined task. In essence, the joiner executes a task
94 >     * that would be on its own local deque had the to-be-joined task
95 >     * not been stolen. This may be seen as a conservative variant of
96 >     * the approach in Wagner & Calder "Leapfrogging: a portable
97 >     * technique for implementing efficient futures" SIGPLAN Notices,
98 >     * 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs
99 >     * in that: (1) We only maintain dependency links across workers
100 >     * upon steals, rather than use per-task bookkeeping.  This may
101 >     * require a linear scan of workers array to locate stealers, but
102 >     * usually doesn't because stealers leave hints (that may become
103 >     * stale/wrong) of where to locate them. This isolates cost to
104 >     * when it is needed, rather than adding to per-task overhead.
105 >     * (2) It is "shallow", ignoring nesting and potentially cyclic
106 >     * mutual steals.  (3) It is intentionally racy: field currentJoin
107 >     * is updated only while actively joining, which means that we
108 >     * miss links in the chain during long-lived tasks, GC stalls etc
109 >     * (which is OK since blocking in such cases is usually a good
110 >     * idea).  (4) We bound the number of attempts to find work (see
111 >     * MAX_HELP_DEPTH) and fall back to suspending the worker and if
112 >     * necessary replacing it with a spare (see
113 >     * ForkJoinPool.awaitJoin).
114 >     *
115 >     * Efficient implementation of these algorithms currently relies
116 >     * on an uncomfortable amount of "Unsafe" mechanics. To maintain
117       * correct orderings, reads and writes of variable base require
118 <     * volatile ordering.  Variable sp does not require volatile write
119 <     * but needs cheaper store-ordering on writes.  Because they are
120 <     * protected by volatile base reads, reads of the queue array and
121 <     * its slots do not need volatile load semantics, but writes (in
122 <     * push) require store order and CASes (in pop and deq) require
123 <     * (volatile) CAS semantics. Since these combinations aren't
124 <     * supported using ordinary volatiles, the only way to accomplish
125 <     * these efficiently is to use direct Unsafe calls. (Using external
126 <     * AtomicIntegers and AtomicReferenceArrays for the indices and
127 <     * array is significantly slower because of memory locality and
128 <     * indirection effects.) Further, performance on most platforms is
129 <     * very sensitive to placement and sizing of the (resizable) queue
130 <     * array.  Even though these queues don't usually become all that
131 <     * big, the initial size must be large enough to counteract cache
118 >     * volatile ordering.  Variable sp does not require volatile
119 >     * writes but still needs store-ordering, which we accomplish by
120 >     * pre-incrementing sp before filling the slot with an ordered
121 >     * store.  (Pre-incrementing also enables backouts used in
122 >     * joinTask.)  Because they are protected by volatile base reads,
123 >     * reads of the queue array and its slots by other threads do not
124 >     * need volatile load semantics, but writes (in push) require
125 >     * store order and CASes (in pop and deq) require (volatile) CAS
126 >     * semantics.  (Michael, Saraswat, and Vechev's algorithm has
127 >     * similar properties, but without support for nulling slots.)
128 >     * Since these combinations aren't supported using ordinary
129 >     * volatiles, the only way to accomplish these efficiently is to
130 >     * use direct Unsafe calls. (Using external AtomicIntegers and
131 >     * AtomicReferenceArrays for the indices and array is
132 >     * significantly slower because of memory locality and indirection
133 >     * effects.)
134 >     *
135 >     * Further, performance on most platforms is very sensitive to
136 >     * placement and sizing of the (resizable) queue array.  Even
137 >     * though these queues don't usually become all that big, the
138 >     * initial size must be large enough to counteract cache
139       * contention effects across multiple queues (especially in the
140       * presence of GC cardmarking). Also, to improve thread-locality,
141 <     * queues are currently initialized immediately after the thread
142 <     * gets the initial signal to start processing tasks.  However,
143 <     * all queue-related methods except pushTask are written in a way
144 <     * that allows them to instead be lazily allocated and/or disposed
145 <     * of when empty. All together, these low-level implementation
146 <     * choices produce as much as a factor of 4 performance
147 <     * improvement compared to naive implementations, and enable the
148 <     * processing of billions of tasks per second, sometimes at the
149 <     * expense of ugliness.
150 <     *
151 <     * 2. Run control: The primary run control is based on a global
152 <     * counter (activeCount) held by the pool. It uses an algorithm
153 <     * similar to that in Herlihy and Shavit section 17.6 to cause
106 <     * threads to eventually block when all threads declare they are
107 <     * inactive. (See variable "scans".)  For this to work, threads
108 <     * must be declared active when executing tasks, and before
109 <     * stealing a task. They must be inactive before blocking on the
110 <     * Pool Barrier (awaiting a new submission or other Pool
111 <     * event). In between, there is some free play which we take
112 <     * advantage of to avoid contention and rapid flickering of the
113 <     * global activeCount: If inactive, we activate only if a victim
114 <     * queue appears to be nonempty (see above).  Similarly, a thread
115 <     * tries to inactivate only after a full scan of other threads.
116 <     * The net effect is that contention on activeCount is rarely a
117 <     * measurable performance issue. (There are also a few other cases
118 <     * where we scan for work rather than retry/block upon
119 <     * contention.)
120 <     *
121 <     * 3. Selection control. We maintain policy of always choosing to
122 <     * run local tasks rather than stealing, and always trying to
123 <     * steal tasks before trying to run a new submission. All steals
124 <     * are currently performed in randomly-chosen deq-order. It may be
125 <     * worthwhile to bias these with locality / anti-locality
126 <     * information, but doing this well probably requires more
127 <     * lower-level information from JVMs than currently provided.
141 >     * queues are initialized after starting.  All together, these
142 >     * low-level implementation choices produce as much as a factor of
143 >     * 4 performance improvement compared to naive implementations,
144 >     * and enable the processing of billions of tasks per second,
145 >     * sometimes at the expense of ugliness.
146 >     */
147 >
148 >    /**
149 >     * Generator for initial random seeds for random victim
150 >     * selection. This is used only to create initial seeds. Random
151 >     * steals use a cheaper xorshift generator per steal attempt. We
152 >     * expect only rare contention on seedGenerator, so just use a
153 >     * plain Random.
154       */
155 +    private static final Random seedGenerator = new Random();
156 +
157 +    /**
158 +     * The maximum stolen->joining link depth allowed in helpJoinTask.
159 +     * Depths for legitimate chains are unbounded, but we use a fixed
160 +     * constant to avoid (otherwise unchecked) cycles and bound
161 +     * staleness of traversal parameters at the expense of sometimes
162 +     * blocking when we could be helping.
163 +     */
164 +    private static final int MAX_HELP_DEPTH = 8;
165  
166      /**
167       * Capacity of work-stealing queue array upon initialization.
168 <     * Must be a power of two. Initial size must be at least 2, but is
168 >     * Must be a power of two. Initial size must be at least 4, but is
169       * padded to minimize cache effects.
170       */
171      private static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
172  
173      /**
174       * Maximum work-stealing queue array size.  Must be less than or
175 <     * equal to 1 << 28 to ensure lack of index wraparound. (This
176 <     * is less than usual bounds, because we need leftshift by 3
177 <     * to be in int range).
175 >     * equal to 1 << (31 - width of array entry) to ensure lack of
176 >     * index wraparound. The value is set in the static block
177 >     * at the end of this file after obtaining width.
178       */
179 <    private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28;
179 >    private static final int MAXIMUM_QUEUE_CAPACITY;
180  
181      /**
182       * The pool this thread works in. Accessed directly by ForkJoinTask.
# Line 149 | Line 185 | public class ForkJoinWorkerThread extend
185  
186      /**
187       * The work-stealing queue array. Size must be a power of two.
188 <     * Initialized when thread starts, to improve memory locality.
188 >     * Initialized in onStart, to improve memory locality.
189       */
190      private ForkJoinTask<?>[] queue;
191  
192      /**
193 +     * Index (mod queue.length) of least valid queue slot, which is
194 +     * always the next position to steal from if nonempty.
195 +     */
196 +    private volatile int base;
197 +
198 +    /**
199       * Index (mod queue.length) of next queue slot to push to or pop
200 <     * from. It is written only by owner thread, via ordered store.
201 <     * Both sp and base are allowed to wrap around on overflow, but
202 <     * (sp - base) still estimates size.
200 >     * from. It is written only by owner thread, and accessed by other
201 >     * threads only after reading (volatile) base.  Both sp and base
202 >     * are allowed to wrap around on overflow, but (sp - base) still
203 >     * estimates size.
204       */
205 <    private volatile int sp;
205 >    private int sp;
206  
207      /**
208 <     * Index (mod queue.length) of least valid queue slot, which is
209 <     * always the next position to steal from if nonempty.
208 >     * The index of most recent stealer, used as a hint to avoid
209 >     * traversal in method helpJoinTask. This is only a hint because a
210 >     * worker might have had multiple steals and this only holds one
211 >     * of them (usually the most current). Declared non-volatile,
212 >     * relying on other prevailing sync to keep reasonably current.
213       */
214 <    private volatile int base;
214 >    private int stealHint;
215  
216      /**
217 <     * Activity status. When true, this worker is considered active.
218 <     * Must be false upon construction. It must be true when executing
219 <     * tasks, and BEFORE stealing a task. It must be false before
220 <     * calling pool.sync.
217 >     * Run state of this worker. In addition to the usual run levels,
218 >     * tracks if this worker is suspended as a spare, and if it was
219 >     * killed (trimmed) while suspended. However, "active" status is
220 >     * maintained separately and modified only in conjunction with
221 >     * CASes of the pool's runState (which are currently sadly
222 >     * manually inlined for performance.)  Accessed directly by pool
223 >     * to simplify checks for normal (zero) status.
224       */
225 <    private boolean active;
225 >    volatile int runState;
226 >
227 >    private static final int TERMINATING = 0x01;
228 >    private static final int TERMINATED  = 0x02;
229 >    private static final int SUSPENDED   = 0x04; // inactive spare
230 >    private static final int TRIMMED     = 0x08; // killed while suspended
231  
232      /**
233 <     * Run state of this worker. Supports simple versions of the usual
234 <     * shutdown/shutdownNow control.
233 >     * Number of steals. Directly accessed (and reset) by
234 >     * pool.tryAccumulateStealCount when idle.
235       */
236 <    private volatile int runState;
236 >    int stealCount;
237  
238      /**
239       * Seed for random number generator for choosing steal victims.
240 <     * Uses Marsaglia xorshift. Must be nonzero upon initialization.
240 >     * Uses Marsaglia xorshift. Must be initialized as nonzero.
241       */
242      private int seed;
243  
244      /**
245 <     * Number of steals, transferred to pool when idle
245 >     * Activity status. When true, this worker is considered active.
246 >     * Accessed directly by pool.  Must be false upon construction.
247 >     */
248 >    boolean active;
249 >
250 >    /**
251 >     * True if use local fifo, not default lifo, for local polling.
252 >     * Shadows value from ForkJoinPool.
253       */
254 <    private int stealCount;
254 >    private final boolean locallyFifo;
255  
256      /**
257       * Index of this worker in pool array. Set once by pool before
258 <     * running, and accessed directly by pool during cleanup etc.
258 >     * running, and accessed directly by pool to locate this worker in
259 >     * its workers array.
260       */
261      int poolIndex;
262  
263      /**
264 <     * The last barrier event waited for. Accessed in pool callback
265 <     * methods, but only by current thread.
264 >     * The last pool event waited for. Accessed only by pool in
265 >     * callback methods invoked within this thread.
266 >     */
267 >    int lastEventCount;
268 >
269 >    /**
270 >     * Encoded index and event count of next event waiter. Accessed
271 >     * only by ForkJoinPool for managing event waiters.
272 >     */
273 >    volatile long nextWaiter;
274 >
275 >    /**
276 >     * Number of times this thread suspended as spare. Accessed only
277 >     * by pool.
278       */
279 <    long lastEventCount;
279 >    int spareCount;
280  
281      /**
282 <     * True if use local fifo, not default lifo, for local polling
282 >     * Encoded index and count of next spare waiter. Accessed only
283 >     * by ForkJoinPool for managing spares.
284       */
285 <    private boolean locallyFifo;
285 >    volatile int nextSpare;
286 >
287 >    /**
288 >     * The task currently being joined, set only when actively trying
289 >     * to help other stealers in helpJoinTask. Written only by this
290 >     * thread, but read by others.
291 >     */
292 >    private volatile ForkJoinTask<?> currentJoin;
293 >
294 >    /**
295 >     * The task most recently stolen from another worker (or
296 >     * submission queue).  Written only by this thread, but read by
297 >     * others.
298 >     */
299 >    private volatile ForkJoinTask<?> currentSteal;
300  
301      /**
302       * Creates a ForkJoinWorkerThread operating in the given pool.
# Line 216 | Line 305 | public class ForkJoinWorkerThread extend
305       * @throws NullPointerException if pool is null
306       */
307      protected ForkJoinWorkerThread(ForkJoinPool pool) {
219        if (pool == null) throw new NullPointerException();
308          this.pool = pool;
309 <        // Note: poolIndex is set by pool during construction
310 <        // Remaining initialization is deferred to onStart
309 >        this.locallyFifo = pool.locallyFifo;
310 >        setDaemon(true);
311 >        // To avoid exposing construction details to subclasses,
312 >        // remaining initialization is in start() and onStart()
313 >    }
314 >
315 >    /**
316 >     * Performs additional initialization and starts this thread.
317 >     */
318 >    final void start(int poolIndex, UncaughtExceptionHandler ueh) {
319 >        this.poolIndex = poolIndex;
320 >        if (ueh != null)
321 >            setUncaughtExceptionHandler(ueh);
322 >        start();
323      }
324  
325 <    // Public access methods
325 >    // Public/protected methods
326  
327      /**
328       * Returns the pool hosting this thread.
# Line 247 | Line 347 | public class ForkJoinWorkerThread extend
347      }
348  
349      /**
350 <     * Establishes local first-in-first-out scheduling mode for forked
351 <     * tasks that are never joined.
352 <     *
353 <     * @param async if true, use locally FIFO scheduling
350 >     * Initializes internal state after construction but before
351 >     * processing any tasks. If you override this method, you must
352 >     * invoke {@code super.onStart()} at the beginning of the method.
353 >     * Initialization requires care: Most fields must have legal
354 >     * default values, to ensure that attempted accesses from other
355 >     * threads work correctly even before this thread starts
356 >     * processing tasks.
357       */
358 <    void setAsyncMode(boolean async) {
359 <        locallyFifo = async;
360 <    }
258 <
259 <    // Runstate management
260 <
261 <    // Runstate values. Order matters
262 <    private static final int RUNNING     = 0;
263 <    private static final int SHUTDOWN    = 1;
264 <    private static final int TERMINATING = 2;
265 <    private static final int TERMINATED  = 3;
266 <
267 <    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
268 <    final boolean isTerminating() { return runState >= TERMINATING;  }
269 <    final boolean isTerminated()  { return runState == TERMINATED; }
270 <    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
271 <    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
358 >    protected void onStart() {
359 >        int rs = seedGenerator.nextInt();
360 >        seed = rs == 0? 1 : rs; // seed must be nonzero
361  
362 <    /**
363 <     * Transitions to at least the given state.
364 <     *
365 <     * @return {@code true} if not already at least at given state
277 <     */
278 <    private boolean transitionRunStateTo(int state) {
279 <        for (;;) {
280 <            int s = runState;
281 <            if (s >= state)
282 <                return false;
283 <            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state))
284 <                return true;
285 <        }
286 <    }
362 >        // Allocate name string and arrays in this thread
363 >        String pid = Integer.toString(pool.getPoolNumber());
364 >        String wid = Integer.toString(poolIndex);
365 >        setName("ForkJoinPool-" + pid + "-worker-" + wid);
366  
367 <    /**
289 <     * Tries to set status to active; fails on contention.
290 <     */
291 <    private boolean tryActivate() {
292 <        if (!active) {
293 <            if (!pool.tryIncrementActiveCount())
294 <                return false;
295 <            active = true;
296 <        }
297 <        return true;
367 >        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
368      }
369  
370      /**
371 <     * Tries to set status to inactive; fails on contention.
371 >     * Performs cleanup associated with termination of this worker
372 >     * thread.  If you override this method, you must invoke
373 >     * {@code super.onTermination} at the end of the overridden method.
374 >     *
375 >     * @param exception the exception causing this thread to abort due
376 >     * to an unrecoverable error, or {@code null} if completed normally
377       */
378 <    private boolean tryInactivate() {
379 <        if (active) {
380 <            if (!pool.tryDecrementActiveCount())
381 <                return false;
382 <            active = false;
378 >    protected void onTermination(Throwable exception) {
379 >        try {
380 >            ForkJoinPool p = pool;
381 >            if (active) {
382 >                int a; // inline p.tryDecrementActiveCount
383 >                active = false;
384 >                do {} while (!UNSAFE.compareAndSwapInt
385 >                             (p, poolRunStateOffset, a = p.runState, a - 1));
386 >            }
387 >            cancelTasks();
388 >            setTerminated();
389 >            p.workerTerminated(this);
390 >        } catch (Throwable ex) {        // Shouldn't ever happen
391 >            if (exception == null)      // but if so, at least rethrown
392 >                exception = ex;
393 >        } finally {
394 >            if (exception != null)
395 >                UNSAFE.throwException(exception);
396          }
309        return true;
310    }
311
312    /**
313     * Computes next value for random victim probe.  Scans don't
314     * require a very high quality generator, but also not a crummy
315     * one.  Marsaglia xor-shift is cheap and works well.
316     */
317    private static int xorShift(int r) {
318        r ^= (r << 13);
319        r ^= (r >>> 17);
320        return r ^ (r << 5);
397      }
398  
323    // Lifecycle methods
324
399      /**
400       * This method is required to be public, but should never be
401       * called explicitly. It performs the main run loop to execute
402 <     * ForkJoinTasks.
402 >     * {@link ForkJoinTask}s.
403       */
404      public void run() {
405          Throwable exception = null;
406          try {
407              onStart();
334            pool.sync(this); // await first pool event
408              mainLoop();
409          } catch (Throwable ex) {
410              exception = ex;
# Line 340 | Line 413 | public class ForkJoinWorkerThread extend
413          }
414      }
415  
416 +    // helpers for run()
417 +
418      /**
419 <     * Executes tasks until shut down.
419 >     * Finds and executes tasks, and checks status while running.
420       */
421      private void mainLoop() {
422 <        while (!isShutdown()) {
423 <            ForkJoinTask<?> t = pollTask();
424 <            if (t != null || (t = pollSubmission()) != null)
425 <                t.quietlyExec();
426 <            else if (tryInactivate())
427 <                pool.sync(this);
422 >        boolean ran = false; // true if ran a task on last step
423 >        ForkJoinPool p = pool;
424 >        for (;;) {
425 >            p.preStep(this, ran);
426 >            if (runState != 0)
427 >                break;
428 >            ran = tryExecSteal() || tryExecSubmission();
429          }
430      }
431  
432      /**
433 <     * Initializes internal state after construction but before
434 <     * processing any tasks. If you override this method, you must
435 <     * invoke super.onStart() at the beginning of the method.
360 <     * Initialization requires care: Most fields must have legal
361 <     * default values, to ensure that attempted accesses from other
362 <     * threads work correctly even before this thread starts
363 <     * processing tasks.
433 >     * Tries to steal a task and execute it.
434 >     *
435 >     * @return true if ran a task
436       */
437 <    protected void onStart() {
438 <        // Allocate while starting to improve chances of thread-local
439 <        // isolation
440 <        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
441 <        // Initial value of seed need not be especially random but
442 <        // should differ across workers and must be nonzero
443 <        int p = poolIndex + 1;
444 <        seed = p + (p << 8) + (p << 16) + (p << 24); // spread bits
437 >    private boolean tryExecSteal() {
438 >        ForkJoinTask<?> t;
439 >        if ((t = scan()) != null) {
440 >            t.quietlyExec();
441 >            UNSAFE.putOrderedObject(this, currentStealOffset, null);
442 >            if (sp != base)
443 >                execLocalTasks();
444 >            return true;
445 >        }
446 >        return false;
447      }
448  
449      /**
450 <     * Performs cleanup associated with termination of this worker
377 <     * thread.  If you override this method, you must invoke
378 <     * {@code super.onTermination} at the end of the overridden method.
450 >     * If a submission exists, try to activate and run it.
451       *
452 <     * @param exception the exception causing this thread to abort due
381 <     * to an unrecoverable error, or {@code null} if completed normally
452 >     * @return true if ran a task
453       */
454 <    protected void onTermination(Throwable exception) {
455 <        // Execute remaining local tasks unless aborting or terminating
456 <        while (exception == null &&  !pool.isTerminating() && base != sp) {
457 <            try {
458 <                ForkJoinTask<?> t = popTask();
459 <                if (t != null)
454 >    private boolean tryExecSubmission() {
455 >        ForkJoinPool p = pool;
456 >        // This loop is needed in case attempt to activate fails, in
457 >        // which case we only retry if there still appears to be a
458 >        // submission.
459 >        while (p.hasQueuedSubmissions()) {
460 >            ForkJoinTask<?> t; int a;
461 >            if (active || // inline p.tryIncrementActiveCount
462 >                (active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
463 >                                                   a = p.runState, a + 1))) {
464 >                if ((t = p.pollSubmission()) != null) {
465 >                    UNSAFE.putOrderedObject(this, currentStealOffset, t);
466                      t.quietlyExec();
467 <            } catch (Throwable ex) {
468 <                exception = ex;
467 >                    UNSAFE.putOrderedObject(this, currentStealOffset, null);
468 >                    if (sp != base)
469 >                        execLocalTasks();
470 >                    return true;
471 >                }
472              }
473          }
474 <        // Cancel other tasks, transition status, notify pool, and
395 <        // propagate exception to uncaught exception handler
396 <        try {
397 <            do {} while (!tryInactivate()); // ensure inactive
398 <            cancelTasks();
399 <            runState = TERMINATED;
400 <            pool.workerTerminated(this);
401 <        } catch (Throwable ex) {        // Shouldn't ever happen
402 <            if (exception == null)      // but if so, at least rethrown
403 <                exception = ex;
404 <        } finally {
405 <            if (exception != null)
406 <                ForkJoinTask.rethrowException(exception);
407 <        }
474 >        return false;
475      }
476  
410    // Intrinsics-based support for queue operations.
411
477      /**
478 <     * Adds in store-order the given task at given slot of q to null.
479 <     * Caller must ensure q is non-null and index is in range.
478 >     * Runs local tasks until queue is empty or shut down.  Call only
479 >     * while active.
480       */
481 <    private static void setSlot(ForkJoinTask<?>[] q, int i,
482 <                                ForkJoinTask<?> t) {
483 <        UNSAFE.putOrderedObject(q, (i << qShift) + qBase, t);
481 >    private void execLocalTasks() {
482 >        while (runState == 0) {
483 >            ForkJoinTask<?> t = locallyFifo ? locallyDeqTask() : popTask();
484 >            if (t != null)
485 >                t.quietlyExec();
486 >            else if (sp == base)
487 >                break;
488 >        }
489      }
490  
491 +    /*
492 +     * Intrinsics-based atomic writes for queue slots. These are
493 +     * basically the same as methods in AtomicReferenceArray, but
494 +     * specialized for (1) ForkJoinTask elements (2) requirement that
495 +     * nullness and bounds checks have already been performed by
496 +     * callers and (3) effective offsets are known not to overflow
497 +     * from int to long (because of MAXIMUM_QUEUE_CAPACITY). We don't
498 +     * need corresponding version for reads: plain array reads are OK
499 +     * because they are protected by other volatile reads and are
500 +     * confirmed by CASes.
501 +     *
502 +     * Most uses don't actually call these methods, but instead contain
503 +     * inlined forms that enable more predictable optimization.  We
504 +     * don't define the version of write used in pushTask at all, but
505 +     * instead inline there a store-fenced array slot write.
506 +     */
507 +
508      /**
509 <     * CAS given slot of q to null. Caller must ensure q is non-null
510 <     * and index is in range.
509 >     * CASes slot i of array q from t to null. Caller must ensure q is
510 >     * non-null and index is in range.
511       */
512 <    private static boolean casSlotNull(ForkJoinTask<?>[] q, int i,
513 <                                       ForkJoinTask<?> t) {
512 >    private static final boolean casSlotNull(ForkJoinTask<?>[] q, int i,
513 >                                             ForkJoinTask<?> t) {
514          return UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null);
515      }
516  
517      /**
518 <     * Sets sp in store-order.
518 >     * Performs a volatile write of the given task at given slot of
519 >     * array q.  Caller must ensure q is non-null and index is in
520 >     * range. This method is used only during resets and backouts.
521       */
522 <    private void storeSp(int s) {
523 <        UNSAFE.putOrderedInt(this, spOffset, s);
522 >    private static final void writeSlot(ForkJoinTask<?>[] q, int i,
523 >                                        ForkJoinTask<?> t) {
524 >        UNSAFE.putObjectVolatile(q, (i << qShift) + qBase, t);
525      }
526  
527 <    // Main queue methods
527 >    // queue methods
528  
529      /**
530 <     * Pushes a task. Called only by current thread.
530 >     * Pushes a task. Call only from this thread.
531       *
532       * @param t the task. Caller must ensure non-null.
533       */
534      final void pushTask(ForkJoinTask<?> t) {
535          ForkJoinTask<?>[] q = queue;
536 <        int mask = q.length - 1;
537 <        int s = sp;
538 <        setSlot(q, s & mask, t);
539 <        storeSp(++s);
540 <        if ((s -= base) == 1)
541 <            pool.signalWork();
542 <        else if (s >= mask)
453 <            growQueue();
536 >        int mask = q.length - 1; // implicit assert q != null
537 >        int s = sp++;            // ok to increment sp before slot write
538 >        UNSAFE.putOrderedObject(q, ((s & mask) << qShift) + qBase, t);
539 >        if ((s -= base) == 0)
540 >            pool.signalWork();   // was empty
541 >        else if (s == mask)
542 >            growQueue();         // is full
543      }
544  
545      /**
546       * Tries to take a task from the base of the queue, failing if
547 <     * either empty or contended.
547 >     * empty or contended. Note: Specializations of this code appear
548 >     * in locallyDeqTask and elsewhere.
549       *
550       * @return a task, or null if none or contended
551       */
552      final ForkJoinTask<?> deqTask() {
553          ForkJoinTask<?> t;
554          ForkJoinTask<?>[] q;
555 <        int i;
466 <        int b;
555 >        int b, i;
556          if (sp != (b = base) &&
557              (q = queue) != null && // must read q after b
558 <            (t = q[i = (q.length - 1) & b]) != null &&
559 <            casSlotNull(q, i, t)) {
558 >            (t = q[i = (q.length - 1) & b]) != null && base == b &&
559 >            UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
560              base = b + 1;
561              return t;
562          }
# Line 475 | Line 564 | public class ForkJoinWorkerThread extend
564      }
565  
566      /**
567 <     * Tries to take a task from the base of own queue, activating if
568 <     * necessary, failing only if empty. Called only by current thread.
567 >     * Tries to take a task from the base of own queue. Assumes active
568 >     * status.  Called only by this thread.
569       *
570       * @return a task, or null if none
571       */
572      final ForkJoinTask<?> locallyDeqTask() {
573 <        int b;
574 <        while (sp != (b = base)) {
575 <            if (tryActivate()) {
576 <                ForkJoinTask<?>[] q = queue;
577 <                int i = (q.length - 1) & b;
578 <                ForkJoinTask<?> t = q[i];
579 <                if (t != null && casSlotNull(q, i, t)) {
573 >        ForkJoinTask<?>[] q = queue;
574 >        if (q != null) {
575 >            ForkJoinTask<?> t;
576 >            int b, i;
577 >            while (sp != (b = base)) {
578 >                if ((t = q[i = (q.length - 1) & b]) != null && base == b &&
579 >                    UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase,
580 >                                                t, null)) {
581                      base = b + 1;
582                      return t;
583                  }
# Line 497 | Line 587 | public class ForkJoinWorkerThread extend
587      }
588  
589      /**
590 <     * Returns a popped task, or null if empty. Ensures active status
591 <     * if non-null. Called only by current thread.
590 >     * Returns a popped task, or null if empty. Assumes active status.
591 >     * Called only by this thread.
592       */
593 <    final ForkJoinTask<?> popTask() {
594 <        int s = sp;
595 <        while (s != base) {
596 <            if (tryActivate()) {
597 <                ForkJoinTask<?>[] q = queue;
598 <                int mask = q.length - 1;
599 <                int i = (s - 1) & mask;
593 >    private ForkJoinTask<?> popTask() {
594 >        ForkJoinTask<?>[] q = queue;
595 >        if (q != null) {
596 >            int s;
597 >            while ((s = sp) != base) {
598 >                int i = (q.length - 1) & --s;
599 >                long u = (i << qShift) + qBase; // raw offset
600                  ForkJoinTask<?> t = q[i];
601 <                if (t == null || !casSlotNull(q, i, t))
601 >                if (t == null)   // lost to stealer
602                      break;
603 <                storeSp(s - 1);
604 <                return t;
603 >                if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
604 >                    /*
605 >                     * Note: here and in related methods, as a
606 >                     * performance (not correctness) issue, we'd like
607 >                     * to encourage compiler not to arbitrarily
608 >                     * postpone setting sp after successful CAS.
609 >                     * Currently there is no intrinsic for arranging
610 >                     * this, but using Unsafe putOrderedInt may be a
611 >                     * preferable strategy on some compilers even
612 >                     * though its main effect is a pre-, not post-
613 >                     * fence. To simplify possible changes, the option
614 >                     * is left in comments next to the associated
615 >                     * assignments.
616 >                     */
617 >                    sp = s; // putOrderedInt may encourage more timely write
618 >                    // UNSAFE.putOrderedInt(this, spOffset, s);
619 >                    return t;
620 >                }
621              }
622          }
623          return null;
624      }
625  
626      /**
627 <     * Specialized version of popTask to pop only if
628 <     * topmost element is the given task. Called only
523 <     * by current thread while active.
627 >     * Specialized version of popTask to pop only if topmost element
628 >     * is the given task. Called only by this thread while active.
629       *
630       * @param t the task. Caller must ensure non-null.
631       */
632      final boolean unpushTask(ForkJoinTask<?> t) {
633 +        int s;
634          ForkJoinTask<?>[] q = queue;
635 <        int mask = q.length - 1;
636 <        int s = sp - 1;
637 <        if (casSlotNull(q, s & mask, t)) {
638 <            storeSp(s);
635 >        if ((s = sp) != base && q != null &&
636 >            UNSAFE.compareAndSwapObject
637 >            (q, (((q.length - 1) & --s) << qShift) + qBase, t, null)) {
638 >            sp = s; // putOrderedInt may encourage more timely write
639 >            // UNSAFE.putOrderedInt(this, spOffset, s);
640              return true;
641          }
642          return false;
643      }
644  
645      /**
646 <     * Returns next task or null if empty or contended
646 >     * Returns next task, or null if empty or contended.
647       */
648      final ForkJoinTask<?> peekTask() {
649          ForkJoinTask<?>[] q = queue;
# Line 569 | Line 676 | public class ForkJoinWorkerThread extend
676              ForkJoinTask<?> t = oldQ[oldIndex];
677              if (t != null && !casSlotNull(oldQ, oldIndex, t))
678                  t = null;
679 <            setSlot(newQ, b & newMask, t);
679 >            writeSlot(newQ, b & newMask, t);
680          } while (++b != bf);
681          pool.signalWork();
682      }
683  
684      /**
685 +     * Computes next value for random victim probe in scan().  Scans
686 +     * don't require a very high quality generator, but also not a
687 +     * crummy one.  Marsaglia xor-shift is cheap and works well enough.
688 +     * Note: This is manually inlined in scan().
689 +     */
690 +    private static final int xorShift(int r) {
691 +        r ^= r << 13;
692 +        r ^= r >>> 17;
693 +        return r ^ (r << 5);
694 +    }
695 +
696 +    /**
697       * Tries to steal a task from another worker. Starts at a random
698       * index of workers array, and probes workers until finding one
699       * with non-empty queue or finding that all are empty.  It
700       * randomly selects the first n probes. If these are empty, it
701 <     * resorts to a full circular traversal, which is necessary to
702 <     * accurately set active status by caller. Also restarts if pool
703 <     * events occurred since last scan, which forces refresh of
704 <     * workers array, in case barrier was associated with resize.
701 >     * resorts to a circular sweep, which is necessary to accurately
702 >     * set active status. (The circular sweep uses steps of
703 >     * approximately half the array size plus 1, to avoid bias
704 >     * stemming from leftmost packing of the array in ForkJoinPool.)
705       *
706       * This method must be both fast and quiet -- usually avoiding
707       * memory accesses that could disrupt cache sharing etc other than
708 <     * those needed to check for and take tasks. This accounts for,
709 <     * among other things, updating random seed in place without
710 <     * storing it until exit.
708 >     * those needed to check for and take tasks (or to activate if not
709 >     * already active). This accounts for, among other things,
710 >     * updating random seed in place without storing it until exit.
711       *
712       * @return a task, or null if none found
713       */
714      private ForkJoinTask<?> scan() {
715 <        ForkJoinTask<?> t = null;
716 <        int r = seed;                    // extract once to keep scan quiet
717 <        ForkJoinWorkerThread[] ws;       // refreshed on outer loop
718 <        int mask;                        // must be power 2 minus 1 and > 0
719 <        outer:do {
720 <            if ((ws = pool.workers) != null && (mask = ws.length - 1) > 0) {
721 <                int idx = r;
722 <                int probes = ~mask;      // use random index while negative
723 <                for (;;) {
724 <                    r = xorShift(r);     // update random seed
725 <                    ForkJoinWorkerThread v = ws[mask & idx];
726 <                    if (v == null || v.sp == v.base) {
727 <                        if (probes <= mask)
728 <                            idx = (probes++ < 0) ? r : (idx + 1);
729 <                        else
730 <                            break;
715 >        ForkJoinPool p = pool;
716 >        ForkJoinWorkerThread[] ws;        // worker array
717 >        int n;                            // upper bound of #workers
718 >        if ((ws = p.workers) != null && (n = ws.length) > 1) {
719 >            boolean canSteal = active;    // shadow active status
720 >            int r = seed;                 // extract seed once
721 >            int mask = n - 1;
722 >            int j = -n;                   // loop counter
723 >            int k = r;                    // worker index, random if j < 0
724 >            for (;;) {
725 >                ForkJoinWorkerThread v = ws[k & mask];
726 >                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // inline xorshift
727 >                ForkJoinTask<?>[] q; ForkJoinTask<?> t; int b, a;
728 >                if (v != null && (b = v.base) != v.sp &&
729 >                    (q = v.queue) != null) {
730 >                    int i = (q.length - 1) & b;
731 >                    long u = (i << qShift) + qBase; // raw offset
732 >                    int pid = poolIndex;
733 >                    if ((t = q[i]) != null) {
734 >                        if (!canSteal &&  // inline p.tryIncrementActiveCount
735 >                            UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
736 >                                                     a = p.runState, a + 1))
737 >                            canSteal = active = true;
738 >                        if (canSteal && v.base == b++ &&
739 >                            UNSAFE.compareAndSwapObject(q, u, t, null)) {
740 >                            v.base = b;
741 >                            v.stealHint = pid;
742 >                            UNSAFE.putOrderedObject(this,
743 >                                                    currentStealOffset, t);
744 >                            seed = r;
745 >                            ++stealCount;
746 >                            return t;
747 >                        }
748                      }
749 <                    else if (!tryActivate() || (t = v.deqTask()) == null)
750 <                        continue outer;  // restart on contention
615 <                    else
616 <                        break outer;
749 >                    j = -n;
750 >                    k = r;                // restart on contention
751                  }
752 +                else if (++j <= 0)
753 +                    k = r;
754 +                else if (j <= n)
755 +                    k += (n >>> 1) | 1;
756 +                else
757 +                    break;
758              }
759 <        } while (pool.hasNewSyncEvent(this)); // retry on pool events
760 <        seed = r;
761 <        return t;
759 >        }
760 >        return null;
761 >    }
762 >
763 >    // Run State management
764 >
765 >    // status check methods used mainly by ForkJoinPool
766 >    final boolean isRunning()    { return runState == 0; }
767 >    final boolean isTerminated() { return (runState & TERMINATED) != 0; }
768 >    final boolean isSuspended()  { return (runState & SUSPENDED) != 0; }
769 >    final boolean isTrimmed()    { return (runState & TRIMMED) != 0; }
770 >
771 >    final boolean isTerminating() {
772 >        if ((runState & TERMINATING) != 0)
773 >            return true;
774 >        if (pool.isAtLeastTerminating()) { // propagate pool state
775 >            shutdown();
776 >            return true;
777 >        }
778 >        return false;
779      }
780  
781      /**
782 <     * Gets and removes a local or stolen task.
783 <     *
627 <     * @return a task, if available
782 >     * Sets state to TERMINATING. Does NOT unpark or interrupt
783 >     * to wake up if currently blocked. Callers must do so if desired.
784       */
785 <    final ForkJoinTask<?> pollTask() {
786 <        ForkJoinTask<?> t = locallyFifo ? locallyDeqTask() : popTask();
787 <        if (t == null && (t = scan()) != null)
788 <            ++stealCount;
789 <        return t;
785 >    final void shutdown() {
786 >        for (;;) {
787 >            int s = runState;
788 >            if ((s & (TERMINATING|TERMINATED)) != 0)
789 >                break;
790 >            if ((s & SUSPENDED) != 0) { // kill and wakeup if suspended
791 >                if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
792 >                                             (s & ~SUSPENDED) |
793 >                                             (TRIMMED|TERMINATING)))
794 >                    break;
795 >            }
796 >            else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
797 >                                              s | TERMINATING))
798 >                break;
799 >        }
800      }
801  
802      /**
803 <     * Gets a local task.
638 <     *
639 <     * @return a task, if available
803 >     * Sets state to TERMINATED. Called only by onTermination().
804       */
805 <    final ForkJoinTask<?> pollLocalTask() {
806 <        return locallyFifo ? locallyDeqTask() : popTask();
805 >    private void setTerminated() {
806 >        int s;
807 >        do {} while (!UNSAFE.compareAndSwapInt(this, runStateOffset,
808 >                                               s = runState,
809 >                                               s | (TERMINATING|TERMINATED)));
810      }
811  
812      /**
813 <     * Returns a pool submission, if one exists, activating first.
813 >     * If suspended, tries to set status to unsuspended.
814 >     * Does NOT wake up if blocked.
815       *
816 <     * @return a submission, if available
816 >     * @return true if successful
817 >     */
818 >    final boolean tryUnsuspend() {
819 >        int s;
820 >        while (((s = runState) & SUSPENDED) != 0) {
821 >            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
822 >                                         s & ~SUSPENDED))
823 >                return true;
824 >        }
825 >        return false;
826 >    }
827 >
828 >    /**
829 >     * Sets suspended status and blocks as spare until resumed
830 >     * or shutdown.
831       */
832 <    private ForkJoinTask<?> pollSubmission() {
832 >    final void suspendAsSpare() {
833 >        for (;;) {                  // set suspended unless terminating
834 >            int s = runState;
835 >            if ((s & TERMINATING) != 0) { // must kill
836 >                if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
837 >                                             s | (TRIMMED | TERMINATING)))
838 >                    return;
839 >            }
840 >            else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
841 >                                              s | SUSPENDED))
842 >                break;
843 >        }
844          ForkJoinPool p = pool;
845 <        while (p.hasQueuedSubmissions()) {
846 <            ForkJoinTask<?> t;
847 <            if (tryActivate() && (t = p.pollSubmission()) != null)
848 <                return t;
845 >        p.pushSpare(this);
846 >        while ((runState & SUSPENDED) != 0) {
847 >            if (p.tryAccumulateStealCount(this)) {
848 >                interrupted();          // clear/ignore interrupts
849 >                if ((runState & SUSPENDED) == 0)
850 >                    break;
851 >                LockSupport.park(this);
852 >            }
853          }
657        return null;
854      }
855  
856 <    // Methods accessed only by Pool
856 >    // Misc support methods for ForkJoinPool
857 >
858 >    /**
859 >     * Returns an estimate of the number of tasks in the queue.  Also
860 >     * used by ForkJoinTask.
861 >     */
862 >    final int getQueueSize() {
863 >        int n; // external calls must read base first
864 >        return (n = -base + sp) <= 0 ? 0 : n;
865 >    }
866  
867      /**
868       * Removes and cancels all tasks in queue.  Can be called from any
869       * thread.
870       */
871      final void cancelTasks() {
872 <        ForkJoinTask<?> t;
873 <        while (base != sp && (t = deqTask()) != null)
874 <            t.cancelIgnoringExceptions();
872 >        ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks
873 >        if (cj != null && cj.status >= 0) {
874 >            cj.cancelIgnoringExceptions();
875 >            try {
876 >                this.interrupt(); // awaken wait
877 >            } catch (SecurityException ignore) {
878 >            }
879 >        }
880 >        ForkJoinTask<?> cs = currentSteal;
881 >        if (cs != null && cs.status >= 0)
882 >            cs.cancelIgnoringExceptions();
883 >        while (base != sp) {
884 >            ForkJoinTask<?> t = deqTask();
885 >            if (t != null)
886 >                t.cancelIgnoringExceptions();
887 >        }
888      }
889  
890      /**
# Line 676 | Line 894 | public class ForkJoinWorkerThread extend
894       */
895      final int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
896          int n = 0;
897 <        ForkJoinTask<?> t;
898 <        while (base != sp && (t = deqTask()) != null) {
899 <            c.add(t);
900 <            ++n;
897 >        while (base != sp) {
898 >            ForkJoinTask<?> t = deqTask();
899 >            if (t != null) {
900 >                c.add(t);
901 >                ++n;
902 >            }
903          }
904          return n;
905      }
906  
907 +    // Support methods for ForkJoinTask
908 +
909      /**
910 <     * Gets and clears steal count for accumulation by pool.  Called
911 <     * only when known to be idle (in pool.sync and termination).
910 >     * Gets and removes a local task.
911 >     *
912 >     * @return a task, if available
913       */
914 <    final int getAndClearStealCount() {
915 <        int sc = stealCount;
916 <        stealCount = 0;
917 <        return sc;
914 >    final ForkJoinTask<?> pollLocalTask() {
915 >        ForkJoinPool p = pool;
916 >        while (sp != base) {
917 >            int a; // inline p.tryIncrementActiveCount
918 >            if (active ||
919 >                (active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
920 >                                                   a = p.runState, a + 1)))
921 >                return locallyFifo ? locallyDeqTask() : popTask();
922 >        }
923 >        return null;
924      }
925  
926      /**
927 <     * Returns {@code true} if at least one worker in the given array
699 <     * appears to have at least one queued task.
927 >     * Gets and removes a local or stolen task.
928       *
929 <     * @param ws array of workers
929 >     * @return a task, if available
930       */
931 <    static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) {
932 <        if (ws != null) {
933 <            int len = ws.length;
934 <            for (int j = 0; j < 2; ++j) { // need two passes for clean sweep
935 <                for (int i = 0; i < len; ++i) {
936 <                    ForkJoinWorkerThread w = ws[i];
709 <                    if (w != null && w.sp != w.base)
710 <                        return true;
711 <                }
712 <            }
931 >    final ForkJoinTask<?> pollTask() {
932 >        ForkJoinTask<?> t = pollLocalTask();
933 >        if (t == null) {
934 >            t = scan();
935 >            // cannot retain/track/help steal
936 >            UNSAFE.putOrderedObject(this, currentStealOffset, null);
937          }
938 <        return false;
938 >        return t;
939      }
940  
941 <    // Support methods for ForkJoinTask
941 >    /**
942 >     * Possibly runs some tasks and/or blocks, until task is done.
943 >     *
944 >     * @param joinMe the task to join
945 >     * @param timed true if use timed wait
946 >     * @param nanos wait time if timed
947 >     */
948 >    final void joinTask(ForkJoinTask<?> joinMe, boolean timed, long nanos) {
949 >        // currentJoin only written by this thread; only need ordered store
950 >        ForkJoinTask<?> prevJoin = currentJoin;
951 >        UNSAFE.putOrderedObject(this, currentJoinOffset, joinMe);
952 >        pool.awaitJoin(joinMe, this, timed, nanos);
953 >        UNSAFE.putOrderedObject(this, currentJoinOffset, prevJoin);
954 >    }
955  
956      /**
957 <     * Returns an estimate of the number of tasks in the queue.
958 <     */
959 <    final int getQueueSize() {
960 <        // suppress momentarily negative values
961 <        return Math.max(0, sp - base);
957 >     * Tries to locate and help perform tasks for a stealer of the
958 >     * given task, or in turn one of its stealers.  Traces
959 >     * currentSteal->currentJoin links looking for a thread working on
960 >     * a descendant of the given task and with a non-empty queue to
961 >     * steal back and execute tasks from.
962 >     *
963 >     * The implementation is very branchy to cope with potential
964 >     * inconsistencies or loops encountering chains that are stale,
965 >     * unknown, or of length greater than MAX_HELP_DEPTH links.  All
966 >     * of these cases are dealt with by just returning back to the
967 >     * caller, who is expected to retry if other join mechanisms also
968 >     * don't work out.
969 >     *
970 >     * @param joinMe the task to join
971 >     * @param running if false, then must update pool count upon
972 >     *  running a task
973 >     * @return value of running on exit
974 >     */
975 >    final boolean helpJoinTask(ForkJoinTask<?> joinMe, boolean running) {
976 >        /*
977 >         * Initial checks to (1) abort if terminating; (2) clean out
978 >         * old cancelled tasks from local queue; (3) if joinMe is next
979 >         * task, run it; (4) omit scan if local queue nonempty (since
980 >         * it may contain non-descendents of joinMe).
981 >         */
982 >        ForkJoinPool p = pool;
983 >        for (;;) {
984 >            ForkJoinTask<?>[] q;
985 >            int s;
986 >            if (joinMe.status < 0)
987 >                return running;
988 >            else if ((runState & TERMINATING) != 0) {
989 >                joinMe.cancelIgnoringExceptions();
990 >                return running;
991 >            }
992 >            else if ((s = sp) == base || (q = queue) == null)
993 >                break;                            // queue empty
994 >            else {
995 >                int i = (q.length - 1) & --s;
996 >                long u = (i << qShift) + qBase;   // raw offset
997 >                ForkJoinTask<?> t = q[i];
998 >                if (t == null)
999 >                    break;                        // lost to a stealer
1000 >                else if (t != joinMe && t.status >= 0)
1001 >                    return running;               // cannot safely help
1002 >                else if ((running ||
1003 >                          (running = p.tryIncrementRunningCount())) &&
1004 >                         UNSAFE.compareAndSwapObject(q, u, t, null)) {
1005 >                    sp = s; // putOrderedInt may encourage more timely write
1006 >                    // UNSAFE.putOrderedInt(this, spOffset, s);
1007 >                    t.quietlyExec();
1008 >                }
1009 >            }
1010 >        }
1011 >
1012 >        int n;                                    // worker array size
1013 >        ForkJoinWorkerThread[] ws = p.workers;
1014 >        if (ws != null && (n = ws.length) > 1) {  // need at least 2 workers
1015 >            ForkJoinTask<?> task = joinMe;        // base of chain
1016 >            ForkJoinWorkerThread thread = this;   // thread with stolen task
1017 >
1018 >            outer:for (int d = 0; d < MAX_HELP_DEPTH; ++d) { // chain length
1019 >                // Try to find v, the stealer of task, by first using hint
1020 >                ForkJoinWorkerThread v = ws[thread.stealHint & (n - 1)];
1021 >                if (v == null || v.currentSteal != task) {
1022 >                    for (int j = 0; ; ++j) {      // search array
1023 >                        if (j < n) {
1024 >                            ForkJoinTask<?> vs;
1025 >                            if ((v = ws[j]) != null &&
1026 >                                (vs = v.currentSteal) != null) {
1027 >                                if (joinMe.status < 0)
1028 >                                    break outer;
1029 >                                if (vs == task) {
1030 >                                    if (task.status < 0)
1031 >                                        break outer; // stale
1032 >                                    thread.stealHint = j;
1033 >                                    break;        // save hint for next time
1034 >                                }
1035 >                            }
1036 >                        }
1037 >                        else
1038 >                            break outer;          // no stealer
1039 >                    }
1040 >                }
1041 >
1042 >                // Try to help v, using specialized form of deqTask
1043 >                for (;;) {
1044 >                    if (joinMe.status < 0)
1045 >                        break outer;
1046 >                    int b = v.base;
1047 >                    ForkJoinTask<?>[] q = v.queue;
1048 >                    if (b == v.sp || q == null)
1049 >                        break;                    // empty
1050 >                    int i = (q.length - 1) & b;
1051 >                    long u = (i << qShift) + qBase;
1052 >                    ForkJoinTask<?> t = q[i];
1053 >                    if (task.status < 0)
1054 >                        break outer;              // stale
1055 >                    if (t != null &&
1056 >                        (running ||
1057 >                         (running = p.tryIncrementRunningCount())) &&
1058 >                        v.base == b++ &&
1059 >                        UNSAFE.compareAndSwapObject(q, u, t, null)) {
1060 >                        if (t != joinMe && joinMe.status < 0) {
1061 >                            UNSAFE.putObjectVolatile(q, u, t);
1062 >                            break outer;          // joinMe cancelled; back out
1063 >                        }
1064 >                        v.base = b;
1065 >                        if (t.status >= 0) {
1066 >                            ForkJoinTask<?> ps = currentSteal;
1067 >                            int pid = poolIndex;
1068 >                            v.stealHint = pid;
1069 >                            UNSAFE.putOrderedObject(this,
1070 >                                                    currentStealOffset, t);
1071 >                            t.quietlyExec();
1072 >                            UNSAFE.putOrderedObject(this,
1073 >                                                    currentStealOffset, ps);
1074 >                        }
1075 >                    }
1076 >                    else if ((runState & TERMINATING) != 0) {
1077 >                        joinMe.cancelIgnoringExceptions();
1078 >                        break outer;
1079 >                    }
1080 >                }
1081 >                
1082 >                // Try to descend to find v's stealer
1083 >                ForkJoinTask<?> next = v.currentJoin;
1084 >                if (task.status < 0 || next == null || next == task ||
1085 >                    joinMe.status < 0)
1086 >                    break;                 // done, stale, dead-end, or cyclic
1087 >                task = next;
1088 >                thread = v;
1089 >            }
1090 >        }
1091 >        return running;
1092      }
1093  
1094      /**
1095 +     * Implements ForkJoinTask.getSurplusQueuedTaskCount().
1096       * Returns an estimate of the number of tasks, offset by a
1097       * function of number of idle workers.
1098 +     *
1099 +     * This method provides a cheap heuristic guide for task
1100 +     * partitioning when programmers, frameworks, tools, or languages
1101 +     * have little or no idea about task granularity.  In essence by
1102 +     * offering this method, we ask users only about tradeoffs in
1103 +     * overhead vs expected throughput and its variance, rather than
1104 +     * how finely to partition tasks.
1105 +     *
1106 +     * In a steady state strict (tree-structured) computation, each
1107 +     * thread makes available for stealing enough tasks for other
1108 +     * threads to remain active. Inductively, if all threads play by
1109 +     * the same rules, each thread should make available only a
1110 +     * constant number of tasks.
1111 +     *
1112 +     * The minimum useful constant is just 1. But using a value of 1
1113 +     * would require immediate replenishment upon each steal to
1114 +     * maintain enough tasks, which is infeasible.  Further,
1115 +     * partitionings/granularities of offered tasks should minimize
1116 +     * steal rates, which in general means that threads nearer the top
1117 +     * of computation tree should generate more than those nearer the
1118 +     * bottom. In perfect steady state, each thread is at
1119 +     * approximately the same level of computation tree. However,
1120 +     * producing extra tasks amortizes the uncertainty of progress and
1121 +     * diffusion assumptions.
1122 +     *
1123 +     * So, users will want to use values larger, but not much larger
1124 +     * than 1 to both smooth over transient shortages and hedge
1125 +     * against uneven progress; as traded off against the cost of
1126 +     * extra task overhead. We leave the user to pick a threshold
1127 +     * value to compare with the results of this call to guide
1128 +     * decisions, but recommend values such as 3.
1129 +     *
1130 +     * When all threads are active, it is on average OK to estimate
1131 +     * surplus strictly locally. In steady-state, if one thread is
1132 +     * maintaining say 2 surplus tasks, then so are others. So we can
1133 +     * just use estimated queue length (although note that (sp - base)
1134 +     * can be an overestimate because of stealers lagging increments
1135 +     * of base).  However, this strategy alone leads to serious
1136 +     * mis-estimates in some non-steady-state conditions (ramp-up,
1137 +     * ramp-down, other stalls). We can detect many of these by
1138 +     * further considering the number of "idle" threads, that are
1139 +     * known to have zero queued tasks, so compensate by a factor of
1140 +     * (#idle/#active) threads.
1141       */
1142      final int getEstimatedSurplusTaskCount() {
1143 <        // The halving approximates weighting idle vs non-idle workers
733 <        return (sp - base) - (pool.getIdleThreadCount() >>> 1);
734 <    }
735 <
736 <    /**
737 <     * Scans, returning early if joinMe done.
738 <     */
739 <    final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) {
740 <        ForkJoinTask<?> t = pollTask();
741 <        if (t != null && joinMe.status < 0 && sp == base) {
742 <            pushTask(t); // unsteal if done and this task would be stealable
743 <            t = null;
744 <        }
745 <        return t;
1143 >        return sp - base - pool.idlePerActive();
1144      }
1145  
1146      /**
1147       * Runs tasks until {@code pool.isQuiescent()}.
1148       */
1149      final void helpQuiescePool() {
1150 +        ForkJoinTask<?> ps = currentSteal; // to restore below
1151          for (;;) {
1152 <            ForkJoinTask<?> t = pollTask();
1153 <            if (t != null)
1152 >            ForkJoinTask<?> t = pollLocalTask();
1153 >            if (t != null || (t = scan()) != null)
1154                  t.quietlyExec();
1155 <            else if (tryInactivate() && pool.isQuiescent())
1156 <                break;
1155 >            else {
1156 >                ForkJoinPool p = pool;
1157 >                int a; // to inline CASes
1158 >                if (active) {
1159 >                    if (!UNSAFE.compareAndSwapInt
1160 >                        (p, poolRunStateOffset, a = p.runState, a - 1))
1161 >                        continue;   // retry later
1162 >                    active = false; // inactivate
1163 >                    UNSAFE.putOrderedObject(this, currentStealOffset, ps);
1164 >                }
1165 >                if (p.isQuiescent()) {
1166 >                    active = true; // re-activate
1167 >                    do {} while (!UNSAFE.compareAndSwapInt
1168 >                                 (p, poolRunStateOffset, a = p.runState, a+1));
1169 >                    return;
1170 >                }
1171 >            }
1172          }
759        do {} while (!tryActivate()); // re-activate on exit
1173      }
1174  
1175      // Unsafe mechanics
# Line 766 | Line 1179 | public class ForkJoinWorkerThread extend
1179          objectFieldOffset("sp", ForkJoinWorkerThread.class);
1180      private static final long runStateOffset =
1181          objectFieldOffset("runState", ForkJoinWorkerThread.class);
1182 <    private static final long qBase;
1182 >    private static final long currentJoinOffset =
1183 >        objectFieldOffset("currentJoin", ForkJoinWorkerThread.class);
1184 >    private static final long currentStealOffset =
1185 >        objectFieldOffset("currentSteal", ForkJoinWorkerThread.class);
1186 >    private static final long qBase =
1187 >        UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
1188 >    private static final long poolRunStateOffset = // to inline CAS
1189 >        objectFieldOffset("runState", ForkJoinPool.class);
1190 >
1191      private static final int qShift;
1192  
1193      static {
773        qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
1194          int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
1195          if ((s & (s-1)) != 0)
1196              throw new Error("data type scale not a power of two");
1197          qShift = 31 - Integer.numberOfLeadingZeros(s);
1198 +        MAXIMUM_QUEUE_CAPACITY = 1 << (31 - qShift);
1199      }
1200  
1201      private static long objectFieldOffset(String field, Class<?> klazz) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines