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.22 by dl, Wed Jul 29 12:05:55 2009 UTC vs.
Revision 1.41 by dl, Tue Aug 17 18:30:33 2010 UTC

# Line 8 | Line 8 | package jsr166y;
8  
9   import java.util.concurrent.*;
10  
11 + import java.util.Random;
12   import java.util.Collection;
13 + import java.util.concurrent.locks.LockSupport;
14  
15   /**
16   * A thread managed by a {@link ForkJoinPool}.  This class is
17   * subclassable solely for the sake of adding functionality -- there
18 < * are no overridable methods dealing with scheduling or
19 < * execution. However, you can override initialization and termination
20 < * methods surrounding the main task processing loop.  If you do
21 < * create such a subclass, you will also need to supply a custom
22 < * ForkJoinWorkerThreadFactory to use it in a ForkJoinPool.
18 > * are no overridable methods dealing with scheduling or execution.
19 > * However, you can override initialization and termination methods
20 > * surrounding the main task processing loop.  If you do create such a
21 > * subclass, you will also need to supply a custom {@link
22 > * ForkJoinPool.ForkJoinWorkerThreadFactory} to use it in a {@code
23 > * 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
82 >     * version of deq rather than pop when locallyFifo is true (as set
83 >     * by the ForkJoinPool).  This allows use in message-passing
84 >     * frameworks in which tasks are never joined.
85 >     *
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.tryAwaitJoin).
114       *
115 <     * Efficient implementation of this approach currently relies on
116 <     * an uncomfortable amount of "Unsafe" mechanics. To maintain
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
92 <     * choices produce as much as a factor of 4 performance
93 <     * improvement compared to naive implementations, and enable the
94 <     * processing of billions of tasks per second, sometimes at the
95 <     * expense of ugliness.
96 <     *
97 <     * 2. Run control: The primary run control is based on a global
98 <     * counter (activeCount) held by the pool. It uses an algorithm
99 <     * similar to that in Herlihy and Shavit section 17.6 to cause
100 <     * threads to eventually block when all threads declare they are
101 <     * inactive. (See variable "scans".)  For this to work, threads
102 <     * must be declared active when executing tasks, and before
103 <     * stealing a task. They must be inactive before blocking on the
104 <     * Pool Barrier (awaiting a new submission or other Pool
105 <     * event). In between, there is some free play which we take
106 <     * advantage of to avoid contention and rapid flickering of the
107 <     * global activeCount: If inactive, we activate only if a victim
108 <     * queue appears to be nonempty (see above).  Similarly, a thread
109 <     * tries to inactivate only after a full scan of other threads.
110 <     * The net effect is that contention on activeCount is rarely a
111 <     * measurable performance issue. (There are also a few other cases
112 <     * where we scan for work rather than retry/block upon
113 <     * contention.)
114 <     *
115 <     * 3. Selection control. We maintain policy of always choosing to
116 <     * run local tasks rather than stealing, and always trying to
117 <     * steal tasks before trying to run a new submission. All steals
118 <     * are currently performed in randomly-chosen deq-order. It may be
119 <     * worthwhile to bias these with locality / anti-locality
120 <     * information, but doing this well probably requires more
121 <     * 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 +     * The wakeup interval (in nanoseconds) for the oldest worker
168 +     * suspended as spare.  On each wakeup not signalled by a
169 +     * resumption, it may ask the pool to reduce the number of spares.
170 +     */
171 +    private static final long TRIM_RATE_NANOS =
172 +        5L * 1000L * 1000L * 1000L; // 5sec
173 +
174 +    /**
175       * Capacity of work-stealing queue array upon initialization.
176 <     * Must be a power of two. Initial size must be at least 2, but is
176 >     * Must be a power of two. Initial size must be at least 4, but is
177       * padded to minimize cache effects.
178       */
179      private static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
# Line 143 | Line 193 | public class ForkJoinWorkerThread extend
193  
194      /**
195       * The work-stealing queue array. Size must be a power of two.
196 <     * Initialized when thread starts, to improve memory locality.
196 >     * Initialized in onStart, to improve memory locality.
197       */
198      private ForkJoinTask<?>[] queue;
199  
200      /**
151     * Index (mod queue.length) of next queue slot to push to or pop
152     * from. It is written only by owner thread, via ordered store.
153     * Both sp and base are allowed to wrap around on overflow, but
154     * (sp - base) still estimates size.
155     */
156    private volatile int sp;
157
158    /**
201       * Index (mod queue.length) of least valid queue slot, which is
202       * always the next position to steal from if nonempty.
203       */
204      private volatile int base;
205  
206      /**
207 <     * Activity status. When true, this worker is considered active.
208 <     * Must be false upon construction. It must be true when executing
209 <     * tasks, and BEFORE stealing a task. It must be false before
210 <     * calling pool.sync.
207 >     * Index (mod queue.length) of next queue slot to push to or pop
208 >     * from. It is written only by owner thread, and accessed by other
209 >     * threads only after reading (volatile) base.  Both sp and base
210 >     * are allowed to wrap around on overflow, but (sp - base) still
211 >     * estimates size.
212 >     */
213 >    private int sp;
214 >
215 >    /**
216 >     * The index of most recent stealer, used as a hint to avoid
217 >     * traversal in method helpJoinTask. This is only a hint because a
218 >     * worker might have had multiple steals and this only holds one
219 >     * of them (usually the most current). Declared non-volatile,
220 >     * relying on other prevailing sync to keep reasonably current.
221       */
222 <    private boolean active;
222 >    private int stealHint;
223  
224      /**
225 <     * Run state of this worker. Supports simple versions of the usual
226 <     * shutdown/shutdownNow control.
225 >     * Run state of this worker. In addition to the usual run levels,
226 >     * tracks if this worker is suspended as a spare, and if it was
227 >     * killed (trimmed) while suspended. However, "active" status is
228 >     * maintained separately and modified only in conjunction with
229 >     * CASes of the pool's runState (which are currently sadly manually
230 >     * inlined for performance.)
231       */
232      private volatile int runState;
233  
234 +    private static final int TERMINATING = 0x01;
235 +    private static final int TERMINATED  = 0x02;
236 +    private static final int SUSPENDED   = 0x04; // inactive spare
237 +    private static final int TRIMMED     = 0x08; // killed while suspended
238 +
239 +    /**
240 +     * Number of steals, transferred and reset in pool callbacks pool
241 +     * when idle Accessed directly by pool.
242 +     */
243 +    int stealCount;
244 +
245      /**
246       * Seed for random number generator for choosing steal victims.
247 <     * Uses Marsaglia xorshift. Must be nonzero upon initialization.
247 >     * Uses Marsaglia xorshift. Must be initialized as nonzero.
248       */
249      private int seed;
250  
251      /**
252 <     * Number of steals, transferred to pool when idle
252 >     * Activity status. When true, this worker is considered active.
253 >     * Accessed directly by pool.  Must be false upon construction.
254 >     */
255 >    boolean active;
256 >
257 >    /**
258 >     * True if use local fifo, not default lifo, for local polling.
259 >     * Shadows value from ForkJoinPool.
260       */
261 <    private int stealCount;
261 >    private final boolean locallyFifo;
262  
263      /**
264       * Index of this worker in pool array. Set once by pool before
265 <     * running, and accessed directly by pool during cleanup etc.
265 >     * running, and accessed directly by pool to locate this worker in
266 >     * its workers array.
267       */
268      int poolIndex;
269  
270      /**
271 <     * The last barrier event waited for. Accessed in pool callback
272 <     * methods, but only by current thread.
271 >     * The last pool event waited for. Accessed only by pool in
272 >     * callback methods invoked within this thread.
273 >     */
274 >    int lastEventCount;
275 >
276 >    /**
277 >     * Encoded index and event count of next event waiter. Used only
278 >     * by ForkJoinPool for managing event waiters.
279 >     */
280 >    volatile long nextWaiter;
281 >
282 >    /**
283 >     * Number of times this thread suspended as spare
284 >     */
285 >    int spareCount;
286 >
287 >    /**
288 >     * Encoded index and count of next spare waiter. Used only
289 >     * by ForkJoinPool for managing spares.
290       */
291 <    long lastEventCount;
291 >    volatile int nextSpare;
292  
293      /**
294 <     * True if use local fifo, not default lifo, for local polling
294 >     * The task currently being joined, set only when actively trying
295 >     * to helpStealer. Written only by current thread, but read by
296 >     * others.
297       */
298 <    private boolean locallyFifo;
298 >    private volatile ForkJoinTask<?> currentJoin;
299 >
300 >    /**
301 >     * The task most recently stolen from another worker (or
302 >     * submission queue).  Not volatile because always read/written in
303 >     * presence of related volatiles in those cases where it matters.
304 >     */
305 >    private ForkJoinTask<?> currentSteal;
306  
307      /**
308       * Creates a ForkJoinWorkerThread operating in the given pool.
# Line 210 | Line 311 | public class ForkJoinWorkerThread extend
311       * @throws NullPointerException if pool is null
312       */
313      protected ForkJoinWorkerThread(ForkJoinPool pool) {
213        if (pool == null) throw new NullPointerException();
314          this.pool = pool;
315 <        // Note: poolIndex is set by pool during construction
316 <        // Remaining initialization is deferred to onStart
315 >        this.locallyFifo = pool.locallyFifo;
316 >        setDaemon(true);
317 >        // To avoid exposing construction details to subclasses,
318 >        // remaining initialization is in start() and onStart()
319      }
320  
321 <    // Public access methods
321 >    /**
322 >     * Performs additional initialization and starts this thread
323 >     */
324 >    final void start(int poolIndex, UncaughtExceptionHandler ueh) {
325 >        this.poolIndex = poolIndex;
326 >        if (ueh != null)
327 >            setUncaughtExceptionHandler(ueh);
328 >        start();
329 >    }
330 >
331 >    // Public/protected methods
332  
333      /**
334       * Returns the pool hosting this thread.
# Line 241 | Line 353 | public class ForkJoinWorkerThread extend
353      }
354  
355      /**
356 <     * Establishes local first-in-first-out scheduling mode for forked
357 <     * tasks that are never joined.
358 <     *
359 <     * @param async if true, use locally FIFO scheduling
356 >     * Initializes internal state after construction but before
357 >     * processing any tasks. If you override this method, you must
358 >     * invoke super.onStart() at the beginning of the method.
359 >     * Initialization requires care: Most fields must have legal
360 >     * default values, to ensure that attempted accesses from other
361 >     * threads work correctly even before this thread starts
362 >     * processing tasks.
363       */
364 <    void setAsyncMode(boolean async) {
365 <        locallyFifo = async;
366 <    }
252 <
253 <    // Runstate management
254 <
255 <    // Runstate values. Order matters
256 <    private static final int RUNNING     = 0;
257 <    private static final int SHUTDOWN    = 1;
258 <    private static final int TERMINATING = 2;
259 <    private static final int TERMINATED  = 3;
364 >    protected void onStart() {
365 >        int rs = seedGenerator.nextInt();
366 >        seed = rs == 0? 1 : rs; // seed must be nonzero
367  
368 <    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
369 <    final boolean isTerminating() { return runState >= TERMINATING;  }
370 <    final boolean isTerminated()  { return runState == TERMINATED; }
371 <    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
265 <    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
368 >        // Allocate name string and arrays in this thread
369 >        String pid = Integer.toString(pool.getPoolNumber());
370 >        String wid = Integer.toString(poolIndex);
371 >        setName("ForkJoinPool-" + pid + "-worker-" + wid);
372  
373 <    /**
268 <     * Transitions to at least the given state.
269 <     *
270 <     * @return {@code true} if not already at least at given state
271 <     */
272 <    private boolean transitionRunStateTo(int state) {
273 <        for (;;) {
274 <            int s = runState;
275 <            if (s >= state)
276 <                return false;
277 <            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state))
278 <                return true;
279 <        }
280 <    }
281 <
282 <    /**
283 <     * Tries to set status to active; fails on contention.
284 <     */
285 <    private boolean tryActivate() {
286 <        if (!active) {
287 <            if (!pool.tryIncrementActiveCount())
288 <                return false;
289 <            active = true;
290 <        }
291 <        return true;
373 >        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
374      }
375  
376      /**
377 <     * Tries to set status to inactive; fails on contention.
377 >     * Performs cleanup associated with termination of this worker
378 >     * thread.  If you override this method, you must invoke
379 >     * {@code super.onTermination} at the end of the overridden method.
380 >     *
381 >     * @param exception the exception causing this thread to abort due
382 >     * to an unrecoverable error, or {@code null} if completed normally
383       */
384 <    private boolean tryInactivate() {
385 <        if (active) {
386 <            if (!pool.tryDecrementActiveCount())
387 <                return false;
388 <            active = false;
384 >    protected void onTermination(Throwable exception) {
385 >        try {
386 >            ForkJoinPool p = pool;
387 >            if (active) {
388 >                int a; // inline p.tryDecrementActiveCount
389 >                active = false;
390 >                do {} while(!UNSAFE.compareAndSwapInt
391 >                            (p, poolRunStateOffset, a = p.runState, a - 1));
392 >            }
393 >            cancelTasks();
394 >            setTerminated();
395 >            p.workerTerminated(this);
396 >        } catch (Throwable ex) {        // Shouldn't ever happen
397 >            if (exception == null)      // but if so, at least rethrown
398 >                exception = ex;
399 >        } finally {
400 >            if (exception != null)
401 >                UNSAFE.throwException(exception);
402          }
303        return true;
403      }
404  
405      /**
307     * Computes next value for random victim probe.  Scans don't
308     * require a very high quality generator, but also not a crummy
309     * one.  Marsaglia xor-shift is cheap and works well.
310     */
311    private static int xorShift(int r) {
312        r ^= r << 1;
313        r ^= r >>> 3;
314        r ^= r << 10;
315        return r;
316    }
317
318    // Lifecycle methods
319
320    /**
406       * This method is required to be public, but should never be
407       * called explicitly. It performs the main run loop to execute
408       * ForkJoinTasks.
# Line 326 | Line 411 | public class ForkJoinWorkerThread extend
411          Throwable exception = null;
412          try {
413              onStart();
329            pool.sync(this); // await first pool event
414              mainLoop();
415          } catch (Throwable ex) {
416              exception = ex;
# Line 335 | Line 419 | public class ForkJoinWorkerThread extend
419          }
420      }
421  
422 +    // helpers for run()
423 +
424      /**
425 <     * Executes tasks until shut down.
425 >     * Find and execute tasks and check status while running
426       */
427      private void mainLoop() {
428 <        while (!isShutdown()) {
429 <            ForkJoinTask<?> t = pollTask();
430 <            if (t != null || (t = pollSubmission()) != null)
431 <                t.quietlyExec();
432 <            else if (tryInactivate())
433 <                pool.sync(this);
428 >        int misses = 0; // track consecutive times failed to find work; max 2
429 >        ForkJoinPool p = pool;
430 >        for (;;) {
431 >            p.preStep(this, misses);
432 >            if (runState != 0)
433 >                break;
434 >            misses = ((tryExecSteal() || tryExecSubmission()) ? 0 :
435 >                      (misses < 2 ? misses + 1 : 2));
436          }
437      }
438  
439      /**
440 <     * Initializes internal state after construction but before
441 <     * processing any tasks. If you override this method, you must
442 <     * invoke super.onStart() at the beginning of the method.
355 <     * Initialization requires care: Most fields must have legal
356 <     * default values, to ensure that attempted accesses from other
357 <     * threads work correctly even before this thread starts
358 <     * processing tasks.
440 >     * Try to steal a task and execute it
441 >     *
442 >     * @return true if ran a task
443       */
444 <    protected void onStart() {
445 <        // Allocate while starting to improve chances of thread-local
446 <        // isolation
447 <        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
448 <        // Initial value of seed need not be especially random but
449 <        // should differ across workers and must be nonzero
450 <        int p = poolIndex + 1;
451 <        seed = p + (p << 8) + (p << 16) + (p << 24); // spread bits
444 >    private boolean tryExecSteal() {
445 >        ForkJoinTask<?> t;
446 >        if ((t  = scan()) != null) {
447 >            t.quietlyExec();
448 >            currentSteal = null;
449 >            if (sp != base)
450 >                execLocalTasks();
451 >            return true;
452 >        }
453 >        return false;
454      }
455  
456      /**
457 <     * 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.
457 >     * If a submission exists, try to activate and run it;
458       *
459 <     * @param exception the exception causing this thread to abort due
376 <     * to an unrecoverable error, or {@code null} if completed normally
459 >     * @return true if ran a task
460       */
461 <    protected void onTermination(Throwable exception) {
462 <        // Execute remaining local tasks unless aborting or terminating
463 <        while (exception == null &&  !pool.isTerminating() && base != sp) {
464 <            try {
465 <                ForkJoinTask<?> t = popTask();
466 <                if (t != null)
461 >    private boolean tryExecSubmission() {
462 >        ForkJoinPool p = pool;
463 >        while (p.hasQueuedSubmissions()) {
464 >            ForkJoinTask<?> t; int a;
465 >            if (active || // ugly/hacky: inline p.tryIncrementActiveCount
466 >                (active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
467 >                                                   a = p.runState, a + 1))) {
468 >                if ((t = p.pollSubmission()) != null) {
469 >                    currentSteal = t;
470                      t.quietlyExec();
471 <            } catch (Throwable ex) {
472 <                exception = ex;
471 >                    currentSteal = null;
472 >                    if (sp != base)
473 >                        execLocalTasks();
474 >                    return true;
475 >                }
476              }
477          }
478 <        // Cancel other tasks, transition status, notify pool, and
390 <        // propagate exception to uncaught exception handler
391 <        try {
392 <            do {} while (!tryInactivate()); // ensure inactive
393 <            cancelTasks();
394 <            runState = TERMINATED;
395 <            pool.workerTerminated(this);
396 <        } catch (Throwable ex) {        // Shouldn't ever happen
397 <            if (exception == null)      // but if so, at least rethrown
398 <                exception = ex;
399 <        } finally {
400 <            if (exception != null)
401 <                ForkJoinTask.rethrowException(exception);
402 <        }
478 >        return false;
479      }
480  
405    // Intrinsics-based support for queue operations.
406
481      /**
482 <     * Adds in store-order the given task at given slot of q to null.
483 <     * Caller must ensure q is non-null and index is in range.
482 >     * Runs local tasks until queue is empty or shut down.  Call only
483 >     * while active.
484       */
485 <    private static void setSlot(ForkJoinTask<?>[] q, int i,
486 <                                ForkJoinTask<?> t) {
487 <        UNSAFE.putOrderedObject(q, (i << qShift) + qBase, t);
485 >    private void execLocalTasks() {
486 >        while (runState == 0) {
487 >            ForkJoinTask<?> t = locallyFifo? locallyDeqTask() : popTask();
488 >            if (t != null)
489 >                t.quietlyExec();
490 >            else if (sp == base)
491 >                break;
492 >        }
493      }
494  
495 +    /*
496 +     * Intrinsics-based atomic writes for queue slots. These are
497 +     * basically the same as methods in AtomicObjectArray, but
498 +     * specialized for (1) ForkJoinTask elements (2) requirement that
499 +     * nullness and bounds checks have already been performed by
500 +     * callers and (3) effective offsets are known not to overflow
501 +     * from int to long (because of MAXIMUM_QUEUE_CAPACITY). We don't
502 +     * need corresponding version for reads: plain array reads are OK
503 +     * because they protected by other volatile reads and are
504 +     * confirmed by CASes.
505 +     *
506 +     * Most uses don't actually call these methods, but instead contain
507 +     * inlined forms that enable more predictable optimization.  We
508 +     * don't define the version of write used in pushTask at all, but
509 +     * instead inline there a store-fenced array slot write.
510 +     */
511 +
512      /**
513 <     * CAS given slot of q to null. Caller must ensure q is non-null
514 <     * and index is in range.
513 >     * CASes slot i of array q from t to null. Caller must ensure q is
514 >     * non-null and index is in range.
515       */
516 <    private static boolean casSlotNull(ForkJoinTask<?>[] q, int i,
517 <                                       ForkJoinTask<?> t) {
516 >    private static final boolean casSlotNull(ForkJoinTask<?>[] q, int i,
517 >                                             ForkJoinTask<?> t) {
518          return UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null);
519      }
520  
521      /**
522 <     * Sets sp in store-order.
522 >     * Performs a volatile write of the given task at given slot of
523 >     * array q.  Caller must ensure q is non-null and index is in
524 >     * range. This method is used only during resets and backouts.
525       */
526 <    private void storeSp(int s) {
527 <        UNSAFE.putOrderedInt(this, spOffset, s);
526 >    private static final void writeSlot(ForkJoinTask<?>[] q, int i,
527 >                                              ForkJoinTask<?> t) {
528 >        UNSAFE.putObjectVolatile(q, (i << qShift) + qBase, t);
529      }
530  
531 <    // Main queue methods
531 >    // queue methods
532  
533      /**
534 <     * Pushes a task. Called only by current thread.
534 >     * Pushes a task. Call only from this thread.
535       *
536       * @param t the task. Caller must ensure non-null.
537       */
538      final void pushTask(ForkJoinTask<?> t) {
539          ForkJoinTask<?>[] q = queue;
540 <        int mask = q.length - 1;
541 <        int s = sp;
542 <        setSlot(q, s & mask, t);
543 <        storeSp(++s);
544 <        if ((s -= base) == 1)
545 <            pool.signalWork();
546 <        else if (s >= mask)
448 <            growQueue();
540 >        int mask = q.length - 1; // implicit assert q != null
541 >        int s = sp++;            // ok to increment sp before slot write
542 >        UNSAFE.putOrderedObject(q, ((s & mask) << qShift) + qBase, t);
543 >        if ((s -= base) == 0)
544 >            pool.signalWork();   // was empty
545 >        else if (s == mask)
546 >            growQueue();         // is full
547      }
548  
549      /**
550       * Tries to take a task from the base of the queue, failing if
551 <     * either empty or contended.
551 >     * empty or contended. Note: Specializations of this code appear
552 >     * in locallyDeqTask and elsewhere.
553       *
554       * @return a task, or null if none or contended
555       */
556      final ForkJoinTask<?> deqTask() {
557          ForkJoinTask<?> t;
558          ForkJoinTask<?>[] q;
559 <        int i;
461 <        int b;
559 >        int b, i;
560          if (sp != (b = base) &&
561              (q = queue) != null && // must read q after b
562 <            (t = q[i = (q.length - 1) & b]) != null &&
563 <            casSlotNull(q, i, t)) {
562 >            (t = q[i = (q.length - 1) & b]) != null && base == b &&
563 >            UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
564              base = b + 1;
565              return t;
566          }
# Line 470 | Line 568 | public class ForkJoinWorkerThread extend
568      }
569  
570      /**
571 <     * Returns a popped task, or null if empty. Ensures active status
572 <     * if non-null. Called only by current thread.
571 >     * Tries to take a task from the base of own queue. Assumes active
572 >     * status.  Called only by current thread.
573 >     *
574 >     * @return a task, or null if none
575       */
576 <    final ForkJoinTask<?> popTask() {
577 <        int s = sp;
578 <        while (s != base) {
579 <            if (tryActivate()) {
580 <                ForkJoinTask<?>[] q = queue;
581 <                int mask = q.length - 1;
582 <                int i = (s - 1) & mask;
576 >    final ForkJoinTask<?> locallyDeqTask() {
577 >        ForkJoinTask<?>[] q = queue;
578 >        if (q != null) {
579 >            ForkJoinTask<?> t;
580 >            int b, i;
581 >            while (sp != (b = base)) {
582 >                if ((t = q[i = (q.length - 1) & b]) != null && base == b &&
583 >                    UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase,
584 >                                                t, null)) {
585 >                    base = b + 1;
586 >                    return t;
587 >                }
588 >            }
589 >        }
590 >        return null;
591 >    }
592 >
593 >    /**
594 >     * Returns a popped task, or null if empty. Assumes active status.
595 >     * Called only by current thread.
596 >     */
597 >    private ForkJoinTask<?> popTask() {
598 >        ForkJoinTask<?>[] q = queue;
599 >        if (q != null) {
600 >            int s;
601 >            while ((s = sp) != base) {
602 >                int i = (q.length - 1) & --s;
603 >                long u = (i << qShift) + qBase; // raw offset
604                  ForkJoinTask<?> t = q[i];
605 <                if (t == null || !casSlotNull(q, i, t))
605 >                if (t == null)   // lost to stealer
606                      break;
607 <                storeSp(s - 1);
608 <                return t;
607 >                if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
608 >                    sp = s; // putOrderedInt may encourage more timely write
609 >                    // UNSAFE.putOrderedInt(this, spOffset, s);
610 >                    return t;
611 >                }
612              }
613          }
614          return null;
615      }
616  
617      /**
618 <     * Specialized version of popTask to pop only if
619 <     * topmost element is the given task. Called only
620 <     * by current thread while active.
618 >     * Specialized version of popTask to pop only if topmost element
619 >     * is the given task. Called only by current thread while
620 >     * active.
621       *
622       * @param t the task. Caller must ensure non-null.
623       */
624      final boolean unpushTask(ForkJoinTask<?> t) {
625 +        int s;
626          ForkJoinTask<?>[] q = queue;
627 <        int mask = q.length - 1;
628 <        int s = sp - 1;
629 <        if (casSlotNull(q, s & mask, t)) {
630 <            storeSp(s);
627 >        if ((s = sp) != base && q != null &&
628 >            UNSAFE.compareAndSwapObject
629 >            (q, (((q.length - 1) & --s) << qShift) + qBase, t, null)) {
630 >            sp = s;
631 >            // UNSAFE.putOrderedInt(this, spOffset, s);
632              return true;
633          }
634          return false;
635      }
636  
637      /**
638 <     * Returns next task.
638 >     * Returns next task or null if empty or contended
639       */
640      final ForkJoinTask<?> peekTask() {
641          ForkJoinTask<?>[] q = queue;
# Line 542 | Line 668 | public class ForkJoinWorkerThread extend
668              ForkJoinTask<?> t = oldQ[oldIndex];
669              if (t != null && !casSlotNull(oldQ, oldIndex, t))
670                  t = null;
671 <            setSlot(newQ, b & newMask, t);
671 >            writeSlot(newQ, b & newMask, t);
672          } while (++b != bf);
673          pool.signalWork();
674      }
675  
676      /**
677 +     * Computes next value for random victim probe in scan().  Scans
678 +     * don't require a very high quality generator, but also not a
679 +     * crummy one.  Marsaglia xor-shift is cheap and works well enough.
680 +     * Note: This is manually inlined in scan()
681 +     */
682 +    private static final int xorShift(int r) {
683 +        r ^= r << 13;
684 +        r ^= r >>> 17;
685 +        return r ^ (r << 5);
686 +    }
687 +
688 +    /**
689       * Tries to steal a task from another worker. Starts at a random
690       * index of workers array, and probes workers until finding one
691       * with non-empty queue or finding that all are empty.  It
692       * randomly selects the first n probes. If these are empty, it
693 <     * resorts to a full circular traversal, which is necessary to
694 <     * accurately set active status by caller. Also restarts if pool
695 <     * events occurred since last scan, which forces refresh of
696 <     * workers array, in case barrier was associated with resize.
693 >     * resorts to a circular sweep, which is necessary to accurately
694 >     * set active status. (The circular sweep uses steps of
695 >     * approximately half the array size plus 1, to avoid bias
696 >     * stemming from leftmost packing of the array in ForkJoinPool.)
697       *
698       * This method must be both fast and quiet -- usually avoiding
699       * memory accesses that could disrupt cache sharing etc other than
700 <     * those needed to check for and take tasks. This accounts for,
701 <     * among other things, updating random seed in place without
702 <     * storing it until exit.
700 >     * those needed to check for and take tasks (or to activate if not
701 >     * already active). This accounts for, among other things,
702 >     * updating random seed in place without storing it until exit.
703       *
704       * @return a task, or null if none found
705       */
706      private ForkJoinTask<?> scan() {
707 <        ForkJoinTask<?> t = null;
708 <        int r = seed;                    // extract once to keep scan quiet
709 <        ForkJoinWorkerThread[] ws;       // refreshed on outer loop
710 <        int mask;                        // must be power 2 minus 1 and > 0
711 <        outer:do {
712 <            if ((ws = pool.workers) != null && (mask = ws.length - 1) > 0) {
713 <                int idx = r;
714 <                int probes = ~mask;      // use random index while negative
715 <                for (;;) {
716 <                    r = xorShift(r);     // update random seed
717 <                    ForkJoinWorkerThread v = ws[mask & idx];
718 <                    if (v == null || v.sp == v.base) {
719 <                        if (probes <= mask)
720 <                            idx = (probes++ < 0) ? r : (idx + 1);
721 <                        else
722 <                            break;
707 >        ForkJoinPool p = pool;
708 >        ForkJoinWorkerThread[] ws;        // worker array
709 >        int n;                            // upper bound of #workers
710 >        if ((ws = p.workers) != null && (n = ws.length) > 1) {
711 >            boolean canSteal = active;    // shadow active status
712 >            int r = seed;                 // extract seed once
713 >            int mask = n - 1;
714 >            int j = -n;                   // loop counter
715 >            int k = r;                    // worker index, random if j < 0
716 >            for (;;) {
717 >                ForkJoinWorkerThread v = ws[k & mask];
718 >                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // inline xorshift
719 >                if (v != null && v.base != v.sp) {
720 >                    ForkJoinTask<?>[] q; int b, a;
721 >                    if ((canSteal ||      // Ugly/hacky: inline
722 >                         (canSteal = active =  // p.tryIncrementActiveCount
723 >                          UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
724 >                                                   a = p.runState, a + 1))) &&
725 >                        (q = v.queue) != null && (b = v.base) != v.sp) {
726 >                        int i = (q.length - 1) & b;
727 >                        long u = (i << qShift) + qBase; // raw offset
728 >                        ForkJoinTask<?> t = q[i];
729 >                        if (v.base == b && t != null &&
730 >                            UNSAFE.compareAndSwapObject(q, u, t, null)) {
731 >                            int pid = poolIndex;
732 >                            currentSteal = t;
733 >                            v.stealHint = pid;
734 >                            v.base = b + 1;
735 >                            seed = r;
736 >                            ++stealCount;
737 >                            return t;
738 >                        }
739                      }
740 <                    else if (!tryActivate() || (t = v.deqTask()) == null)
741 <                        continue outer;  // restart on contention
588 <                    else
589 <                        break outer;
740 >                    j = -n;
741 >                    k = r;                // restart on contention
742                  }
743 +                else if (++j <= 0)
744 +                    k = r;
745 +                else if (j <= n)
746 +                    k += (n >>> 1) | 1;
747 +                else
748 +                    break;
749              }
750 <        } while (pool.hasNewSyncEvent(this)); // retry on pool events
751 <        seed = r;
594 <        return t;
750 >        }
751 >        return null;
752      }
753  
754 +    // Run State management
755 +
756 +    // status check methods used mainly by ForkJoinPool
757 +    final boolean isRunning()     { return runState == 0; }
758 +    final boolean isTerminating() { return (runState & TERMINATING) != 0; }
759 +    final boolean isTerminated()  { return (runState & TERMINATED) != 0; }
760 +    final boolean isSuspended()   { return (runState & SUSPENDED) != 0; }
761 +    final boolean isTrimmed()     { return (runState & TRIMMED) != 0; }
762 +
763      /**
764 <     * Gets and removes a local or stolen task.
765 <     *
600 <     * @return a task, if available
764 >     * Sets state to TERMINATING. Does NOT unpark or interrupt
765 >     * to wake up if currently blocked.
766       */
767 <    final ForkJoinTask<?> pollTask() {
768 <        ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask();
769 <        if (t == null && (t = scan()) != null)
770 <            ++stealCount;
771 <        return t;
767 >    final void shutdown() {
768 >        for (;;) {
769 >            int s = runState;
770 >            if ((s & (TERMINATING|TERMINATED)) != 0)
771 >                break;
772 >            if ((s & SUSPENDED) != 0) { // kill and wakeup if suspended
773 >                if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
774 >                                             (s & ~SUSPENDED) |
775 >                                             (TRIMMED|TERMINATING)))
776 >                    break;
777 >            }
778 >            else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
779 >                                              s | TERMINATING))
780 >                break;
781 >        }
782      }
783  
784      /**
785 <     * Gets a local task.
611 <     *
612 <     * @return a task, if available
785 >     * Sets state to TERMINATED. Called only by onTermination()
786       */
787 <    final ForkJoinTask<?> pollLocalTask() {
788 <        return locallyFifo ? deqTask() : popTask();
787 >    private void setTerminated() {
788 >        int s;
789 >        do {} while (!UNSAFE.compareAndSwapInt(this, runStateOffset,
790 >                                               s = runState,
791 >                                               s | (TERMINATING|TERMINATED)));
792      }
793  
794      /**
795 <     * Returns a pool submission, if one exists, activating first.
795 >     * If suspended, tries to set status to unsuspended.
796       *
797 <     * @return a submission, if available
797 >     * @return true if successful
798       */
799 <    private ForkJoinTask<?> pollSubmission() {
799 >    final boolean tryUnsuspend() {
800 >        int s;
801 >        while (((s = runState) & SUSPENDED) != 0) {
802 >            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
803 >                                         s & ~SUSPENDED))
804 >                return true;
805 >        }
806 >        return false;
807 >    }
808 >
809 >    /**
810 >     * Sets suspended status and blocks as spare until resumed
811 >     * or shutdown.
812 >     */
813 >    final void suspendAsSpare() {
814 >        for (;;) {                  // set suspended unless terminating
815 >            int s = runState;
816 >            if ((s & TERMINATING) != 0) { // must kill
817 >                if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
818 >                                             s | (TRIMMED | TERMINATING)))
819 >                    return;
820 >            }
821 >            else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
822 >                                              s | SUSPENDED))
823 >                break;
824 >        }
825          ForkJoinPool p = pool;
826 <        while (p.hasQueuedSubmissions()) {
827 <            ForkJoinTask<?> t;
828 <            if (tryActivate() && (t = p.pollSubmission()) != null)
829 <                return t;
826 >        p.pushSpare(this);
827 >        lastEventCount = 0;         // reset upon resume
828 >        while ((runState & SUSPENDED) != 0) {
829 >            if (p.tryAccumulateStealCount(this)) {
830 >                boolean untimed = nextSpare != 0;
831 >                long startTime = untimed? 0 : System.nanoTime();
832 >                interrupted();          // clear/ignore interrupts
833 >                if ((runState & SUSPENDED) == 0)
834 >                    break;
835 >                if (untimed)     // untimed
836 >                    LockSupport.park(this);
837 >                else {
838 >                    LockSupport.parkNanos(this, TRIM_RATE_NANOS);
839 >                    if ((runState & SUSPENDED) == 0)
840 >                        break;
841 >                    if (System.nanoTime() - startTime >= TRIM_RATE_NANOS)
842 >                        p.tryShutdownSpare();
843 >                }
844 >            }
845          }
630        return null;
846      }
847  
848 <    // Methods accessed only by Pool
848 >    // Misc support methods for ForkJoinPool
849 >
850 >    /**
851 >     * Returns an estimate of the number of tasks in the queue.  Also
852 >     * used by ForkJoinTask.
853 >     */
854 >    final int getQueueSize() {
855 >        int n; // external calls must read base first
856 >        return (n = -base + sp) <= 0 ? 0 : n;
857 >    }
858  
859      /**
860       * Removes and cancels all tasks in queue.  Can be called from any
861       * thread.
862       */
863      final void cancelTasks() {
864 <        ForkJoinTask<?> t;
865 <        while (base != sp && (t = deqTask()) != null)
866 <            t.cancelIgnoringExceptions();
864 >        ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks
865 >        if (cj != null) {
866 >            currentJoin = null;
867 >            cj.cancelIgnoringExceptions();
868 >            try {
869 >                this.interrupt(); // awaken wait
870 >            } catch (SecurityException ignore) {
871 >            }
872 >        }
873 >        ForkJoinTask<?> cs = currentSteal;
874 >        if (cs != null) {
875 >            currentSteal = null;
876 >            cs.cancelIgnoringExceptions();
877 >        }
878 >        while (base != sp) {
879 >            ForkJoinTask<?> t = deqTask();
880 >            if (t != null)
881 >                t.cancelIgnoringExceptions();
882 >        }
883      }
884  
885      /**
# Line 649 | Line 889 | public class ForkJoinWorkerThread extend
889       */
890      final int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
891          int n = 0;
892 <        ForkJoinTask<?> t;
893 <        while (base != sp && (t = deqTask()) != null) {
894 <            c.add(t);
895 <            ++n;
892 >        while (base != sp) {
893 >            ForkJoinTask<?> t = deqTask();
894 >            if (t != null) {
895 >                c.add(t);
896 >                ++n;
897 >            }
898          }
899          return n;
900      }
901  
902 +    // Support methods for ForkJoinTask
903 +
904      /**
905 <     * Gets and clears steal count for accumulation by pool.  Called
906 <     * only when known to be idle (in pool.sync and termination).
905 >     * Gets and removes a local task.
906 >     *
907 >     * @return a task, if available
908       */
909 <    final int getAndClearStealCount() {
910 <        int sc = stealCount;
911 <        stealCount = 0;
912 <        return sc;
909 >    final ForkJoinTask<?> pollLocalTask() {
910 >        ForkJoinPool p = pool;
911 >        while (sp != base) {
912 >            int a; // inline p.tryIncrementActiveCount
913 >            if (active ||
914 >                (active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
915 >                                                   a = p.runState, a + 1)))
916 >                return locallyFifo? locallyDeqTask() : popTask();
917 >        }
918 >        return null;
919      }
920  
921      /**
922 <     * Returns {@code true} if at least one worker in the given array
672 <     * appears to have at least one queued task.
922 >     * Gets and removes a local or stolen task.
923       *
924 <     * @param ws array of workers
924 >     * @return a task, if available
925       */
926 <    static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) {
927 <        if (ws != null) {
928 <            int len = ws.length;
929 <            for (int j = 0; j < 2; ++j) { // need two passes for clean sweep
930 <                for (int i = 0; i < len; ++i) {
681 <                    ForkJoinWorkerThread w = ws[i];
682 <                    if (w != null && w.sp != w.base)
683 <                        return true;
684 <                }
685 <            }
926 >    final ForkJoinTask<?> pollTask() {
927 >        ForkJoinTask<?> t = pollLocalTask();
928 >        if (t == null) {
929 >            t = scan();
930 >            currentSteal = null; // cannot retain/track/help
931          }
932 <        return false;
932 >        return t;
933      }
934  
690    // Support methods for ForkJoinTask
691
935      /**
936 <     * Returns an estimate of the number of tasks in the queue.
936 >     * Possibly runs some tasks and/or blocks, until task is done.
937 >     *
938 >     * @param joinMe the task to join
939       */
940 <    final int getQueueSize() {
941 <        // suppress momentarily negative values
942 <        return Math.max(0, sp - base);
940 >    final void joinTask(ForkJoinTask<?> joinMe) {
941 >        // currentJoin only written by this thread; only need ordered store
942 >        ForkJoinTask<?> prevJoin = currentJoin;
943 >        UNSAFE.putOrderedObject(this, currentJoinOffset, joinMe);
944 >        if (sp != base)
945 >            localHelpJoinTask(joinMe);
946 >        if (joinMe.status >= 0)
947 >            pool.awaitJoin(joinMe, this);
948 >        UNSAFE.putOrderedObject(this, currentJoinOffset, prevJoin);
949      }
950  
951      /**
952 <     * Returns an estimate of the number of tasks, offset by a
953 <     * function of number of idle workers.
952 >     * Run tasks in local queue until given task is done.
953 >     *
954 >     * @param joinMe the task to join
955       */
956 <    final int getEstimatedSurplusTaskCount() {
957 <        // The halving approximates weighting idle vs non-idle workers
958 <        return (sp - base) - (pool.getIdleThreadCount() >>> 1);
956 >    private void localHelpJoinTask(ForkJoinTask<?> joinMe) {
957 >        int s;
958 >        ForkJoinTask<?>[] q;
959 >        while (joinMe.status >= 0 && (s = sp) != base && (q = queue) != null) {
960 >            int i = (q.length - 1) & --s;
961 >            long u = (i << qShift) + qBase; // raw offset
962 >            ForkJoinTask<?> t = q[i];
963 >            if (t == null)  // lost to a stealer
964 >                break;
965 >            if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
966 >                /*
967 >                 * This recheck (and similarly in helpJoinTask)
968 >                 * handles cases where joinMe is independently
969 >                 * cancelled or forced even though there is other work
970 >                 * available. Back out of the pop by putting t back
971 >                 * into slot before we commit by writing sp.
972 >                 */
973 >                if (joinMe.status < 0) {
974 >                    UNSAFE.putObjectVolatile(q, u, t);
975 >                    break;
976 >                }
977 >                sp = s;
978 >                // UNSAFE.putOrderedInt(this, spOffset, s);
979 >                t.quietlyExec();
980 >            }
981 >        }
982      }
983  
984      /**
985 <     * Scans, returning early if joinMe done.
985 >     * Tries to locate and help perform tasks for a stealer of the
986 >     * given task, or in turn one of its stealers.  Traces
987 >     * currentSteal->currentJoin links looking for a thread working on
988 >     * a descendant of the given task and with a non-empty queue to
989 >     * steal back and execute tasks from.
990 >     *
991 >     * The implementation is very branchy to cope with the potential
992 >     * inconsistencies or loops encountering chains that are stale,
993 >     * unknown, or of length greater than MAX_HELP_DEPTH links.  All
994 >     * of these cases are dealt with by just returning back to the
995 >     * caller, who is expected to retry if other join mechanisms also
996 >     * don't work out.
997 >     *
998 >     * @param joinMe the task to join
999       */
1000 <    final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) {
1001 <        ForkJoinTask<?> t = pollTask();
1002 <        if (t != null && joinMe.status < 0 && sp == base) {
1003 <            pushTask(t); // unsteal if done and this task would be stealable
1004 <            t = null;
1000 >    final void helpJoinTask(ForkJoinTask<?> joinMe) {
1001 >        ForkJoinWorkerThread[] ws = pool.workers;
1002 >        int n; // need at least 2 workers
1003 >        if (ws != null && (n = ws.length) > 1 && joinMe.status >= 0) {
1004 >            ForkJoinTask<?> task = joinMe;        // base of chain
1005 >            ForkJoinWorkerThread thread = this;   // thread with stolen task
1006 >            for (int d = 0; d < MAX_HELP_DEPTH; ++d) { // chain length
1007 >                // Try to find v, the stealer of task, by first using hint
1008 >                ForkJoinWorkerThread v = ws[thread.stealHint & (n - 1)];
1009 >                if (v == null || v.currentSteal != task) {
1010 >                    for (int j = 0; ; ++j) {      // search array
1011 >                        if (j < n) {
1012 >                            if ((v = ws[j]) != null) {
1013 >                                if (task.status < 0)
1014 >                                    return;       // stale or done
1015 >                                if (v.currentSteal == task) {
1016 >                                    thread.stealHint = j;
1017 >                                    break;        // save hint for next time
1018 >                                }
1019 >                            }
1020 >                        }
1021 >                        else
1022 >                            return;               // no stealer
1023 >                    }
1024 >                }
1025 >                // Try to help v, using specialized form of deqTask
1026 >                int b;
1027 >                ForkJoinTask<?>[] q;
1028 >                while ((b = v.base) != v.sp && (q = v.queue) != null) {
1029 >                    int i = (q.length - 1) & b;
1030 >                    long u = (i << qShift) + qBase;
1031 >                    ForkJoinTask<?> t = q[i];
1032 >                    if (task.status < 0)
1033 >                        return;                   // stale or done
1034 >                    if (v.base == b) {
1035 >                        if (t == null)
1036 >                            return;               // producer stalled
1037 >                        if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
1038 >                            if (joinMe.status < 0) {
1039 >                                UNSAFE.putObjectVolatile(q, u, t);
1040 >                                return;           // back out on cancel
1041 >                            }
1042 >                            int pid = poolIndex;
1043 >                            ForkJoinTask<?> prevSteal = currentSteal;
1044 >                            currentSteal = t;
1045 >                            v.stealHint = pid;
1046 >                            v.base = b + 1;
1047 >                            t.quietlyExec();
1048 >                            currentSteal = prevSteal;
1049 >                        }
1050 >                    }
1051 >                    if (joinMe.status < 0)
1052 >                        return;
1053 >                }
1054 >                // Try to descend to find v's stealer
1055 >                ForkJoinTask<?> next = v.currentJoin;
1056 >                if (task.status < 0 || next == null || next == task ||
1057 >                    joinMe.status < 0)
1058 >                    return;
1059 >                task = next;
1060 >                thread = v;
1061 >            }
1062          }
1063 <        return t;
1063 >    }
1064 >
1065 >    /**
1066 >     * Returns an estimate of the number of tasks, offset by a
1067 >     * function of number of idle workers.
1068 >     *
1069 >     * This method provides a cheap heuristic guide for task
1070 >     * partitioning when programmers, frameworks, tools, or languages
1071 >     * have little or no idea about task granularity.  In essence by
1072 >     * offering this method, we ask users only about tradeoffs in
1073 >     * overhead vs expected throughput and its variance, rather than
1074 >     * how finely to partition tasks.
1075 >     *
1076 >     * In a steady state strict (tree-structured) computation, each
1077 >     * thread makes available for stealing enough tasks for other
1078 >     * threads to remain active. Inductively, if all threads play by
1079 >     * the same rules, each thread should make available only a
1080 >     * constant number of tasks.
1081 >     *
1082 >     * The minimum useful constant is just 1. But using a value of 1
1083 >     * would require immediate replenishment upon each steal to
1084 >     * maintain enough tasks, which is infeasible.  Further,
1085 >     * partitionings/granularities of offered tasks should minimize
1086 >     * steal rates, which in general means that threads nearer the top
1087 >     * of computation tree should generate more than those nearer the
1088 >     * bottom. In perfect steady state, each thread is at
1089 >     * approximately the same level of computation tree. However,
1090 >     * producing extra tasks amortizes the uncertainty of progress and
1091 >     * diffusion assumptions.
1092 >     *
1093 >     * So, users will want to use values larger, but not much larger
1094 >     * than 1 to both smooth over transient shortages and hedge
1095 >     * against uneven progress; as traded off against the cost of
1096 >     * extra task overhead. We leave the user to pick a threshold
1097 >     * value to compare with the results of this call to guide
1098 >     * decisions, but recommend values such as 3.
1099 >     *
1100 >     * When all threads are active, it is on average OK to estimate
1101 >     * surplus strictly locally. In steady-state, if one thread is
1102 >     * maintaining say 2 surplus tasks, then so are others. So we can
1103 >     * just use estimated queue length (although note that (sp - base)
1104 >     * can be an overestimate because of stealers lagging increments
1105 >     * of base).  However, this strategy alone leads to serious
1106 >     * mis-estimates in some non-steady-state conditions (ramp-up,
1107 >     * ramp-down, other stalls). We can detect many of these by
1108 >     * further considering the number of "idle" threads, that are
1109 >     * known to have zero queued tasks, so compensate by a factor of
1110 >     * (#idle/#active) threads.
1111 >     */
1112 >    final int getEstimatedSurplusTaskCount() {
1113 >        return sp - base - pool.idlePerActive();
1114      }
1115  
1116      /**
# Line 723 | Line 1118 | public class ForkJoinWorkerThread extend
1118       */
1119      final void helpQuiescePool() {
1120          for (;;) {
1121 <            ForkJoinTask<?> t = pollTask();
1122 <            if (t != null)
1121 >            ForkJoinTask<?> t = pollLocalTask();
1122 >            if (t != null || (t = scan()) != null) {
1123                  t.quietlyExec();
1124 <            else if (tryInactivate() && pool.isQuiescent())
1125 <                break;
1124 >                currentSteal = null;
1125 >            }
1126 >            else {
1127 >                ForkJoinPool p = pool;
1128 >                int a; // to inline CASes
1129 >                if (active) {
1130 >                    if (!UNSAFE.compareAndSwapInt
1131 >                        (p, poolRunStateOffset, a = p.runState, a - 1))
1132 >                        continue;   // retry later
1133 >                    active = false; // inactivate
1134 >                }
1135 >                if (p.isQuiescent()) {
1136 >                    active = true; // re-activate
1137 >                    do {} while(!UNSAFE.compareAndSwapInt
1138 >                                (p, poolRunStateOffset, a = p.runState, a+1));
1139 >                    return;
1140 >                }
1141 >            }
1142          }
732        do {} while (!tryActivate()); // re-activate on exit
1143      }
1144  
1145      // Unsafe mechanics
# Line 739 | Line 1149 | public class ForkJoinWorkerThread extend
1149          objectFieldOffset("sp", ForkJoinWorkerThread.class);
1150      private static final long runStateOffset =
1151          objectFieldOffset("runState", ForkJoinWorkerThread.class);
1152 <    private static final long qBase;
1152 >    private static final long currentJoinOffset =
1153 >        objectFieldOffset("currentJoin", ForkJoinWorkerThread.class);
1154 >    private static final long currentStealOffset =
1155 >        objectFieldOffset("currentSteal", ForkJoinWorkerThread.class);
1156 >    private static final long qBase =
1157 >        UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
1158 >    private static final long poolRunStateOffset = // to inline CAS
1159 >        objectFieldOffset("runState", ForkJoinPool.class);
1160 >
1161      private static final int qShift;
1162  
1163      static {
746        qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
1164          int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
1165          if ((s & (s-1)) != 0)
1166              throw new Error("data type scale not a power of two");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines