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.31 by dl, Mon Apr 5 15:52:26 2010 UTC vs.
Revision 1.51 by jsr166, Mon Sep 20 20:42:37 2010 UTC

# Line 83 | 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
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 <     * scanWhileJoining.)  Because they are protected by volatile base
123 <     * reads, reads of the queue array and its slots by other threads
124 <     * do not need volatile load semantics, but writes (in push)
125 <     * require store order and CASes (in pop and deq) require
126 <     * (volatile) CAS semantics.  (Michael, Saraswat, and Vechev's
127 <     * algorithm has similar properties, but without support for
128 <     * nulling slots.)  Since these combinations aren't supported
129 <     * using ordinary volatiles, the only way to accomplish these
130 <     * efficiently is to use direct Unsafe calls. (Using external
131 <     * AtomicIntegers and AtomicReferenceArrays for the indices and
132 <     * array is significantly slower because of memory locality and
133 <     * indirection effects.)
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
# Line 126 | Line 155 | public class ForkJoinWorkerThread extend
155      private static final Random seedGenerator = new Random();
156  
157      /**
158 <     * The timeout value for suspending spares. Spare workers that
159 <     * remain unsignalled for more than this time may be trimmed
160 <     * (killed and removed from pool).  Since our goal is to avoid
161 <     * long-term thread buildup, the exact value of timeout does not
162 <     * matter too much so long as it avoids most false-alarm timeouts
134 <     * under GC stalls or momentarily high system load.
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 long SPARE_KEEPALIVE_NANOS =
137 <        5L * 1000L * 1000L * 1000L; // 5 secs
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 178 | Line 205 | public class ForkJoinWorkerThread extend
205      private int sp;
206  
207      /**
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 int stealHint;
215 +
216 +    /**
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.
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 volatile int runState;
225 >    volatile int runState;
226  
227      private static final int TERMINATING = 0x01;
228      private static final int TERMINATED  = 0x02;
# Line 191 | Line 230 | public class ForkJoinWorkerThread extend
230      private static final int TRIMMED     = 0x08; // killed while suspended
231  
232      /**
233 <     * Number of LockSupport.park calls to block this thread for
234 <     * suspension or event waits. Used for internal instrumention;
196 <     * currently not exported but included because volatile write upon
197 <     * park also provides a workaround for a JVM bug.
198 <     */
199 <    private volatile int parkCount;
200 <
201 <    /**
202 <     * Number of steals, transferred and reset in pool callbacks pool
203 <     * when idle Accessed directly by pool.
233 >     * Number of steals. Directly accessed (and reset) by
234 >     * pool.tryAccumulateStealCount when idle.
235       */
236      int stealCount;
237  
# Line 218 | Line 249 | public class ForkJoinWorkerThread extend
249  
250      /**
251       * True if use local fifo, not default lifo, for local polling.
252 <     * Shadows value from ForkJoinPool, which resets it if changed
222 <     * pool-wide.
252 >     * Shadows value from ForkJoinPool.
253       */
254 <    private boolean locallyFifo;
254 >    private final boolean locallyFifo;
255  
256      /**
257       * Index of this worker in pool array. Set once by pool before
# Line 237 | Line 267 | public class ForkJoinWorkerThread extend
267      int lastEventCount;
268  
269      /**
270 <     * Encoded index and event count of next event waiter. Used only
271 <     * by ForkJoinPool for managing event waiters.
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 +    int spareCount;
280 +
281 +    /**
282 +     * Encoded index and count of next spare waiter. Accessed only
283 +     * by ForkJoinPool for managing spares.
284 +     */
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.
303       *
304       * @param pool the pool this thread works in
305       * @throws NullPointerException if pool is null
306       */
307      protected ForkJoinWorkerThread(ForkJoinPool pool) {
252        if (pool == null) throw new NullPointerException();
308          this.pool = pool;
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
316 >     * Performs additional initialization and starts this thread.
317       */
318 <    final void start(int poolIndex, boolean locallyFifo,
262 <                     UncaughtExceptionHandler ueh) {
318 >    final void start(int poolIndex, UncaughtExceptionHandler ueh) {
319          this.poolIndex = poolIndex;
264        this.locallyFifo = locallyFifo;
320          if (ueh != null)
321              setUncaughtExceptionHandler(ueh);
267        setDaemon(true);
322          start();
323      }
324  
# Line 295 | Line 349 | public class ForkJoinWorkerThread extend
349      /**
350       * Initializes internal state after construction but before
351       * processing any tasks. If you override this method, you must
352 <     * invoke super.onStart() at the beginning of the method.
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
# Line 305 | Line 359 | public class ForkJoinWorkerThread extend
359          int rs = seedGenerator.nextInt();
360          seed = rs == 0? 1 : rs; // seed must be nonzero
361  
362 <        // Allocate name string and queue array in this thread
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);
# Line 323 | Line 377 | public class ForkJoinWorkerThread extend
377       */
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 <            pool.workerTerminated(this);
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;
# Line 355 | Line 416 | public class ForkJoinWorkerThread extend
416      // helpers for run()
417  
418      /**
419 <     * Find and execute tasks and check status while running
419 >     * Finds and executes tasks, and checks status while running.
420       */
421      private void mainLoop() {
422 <        boolean ran = false; // true if ran task on previous step
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 <                return;
428 <            ForkJoinTask<?> t; // try to get and run stolen or submitted task
368 <            if (ran = (t = scan()) != null || (t = pollSubmission()) != null) {
369 <                t.tryExec();
370 <                if (base != sp)
371 <                    runLocalTasks();
372 <            }
427 >                break;
428 >            ran = tryExecSteal() || tryExecSubmission();
429          }
430      }
431  
432      /**
433 <     * Runs local tasks until queue is empty or shut down.  Call only
434 <     * while active.
433 >     * Tries to steal a task and execute it.
434 >     *
435 >     * @return true if ran a task
436       */
437 <    private void runLocalTasks() {
438 <        while (runState == 0) {
439 <            ForkJoinTask<?> t = locallyFifo? locallyDeqTask() : popTask();
440 <            if (t != null)
441 <                t.tryExec();
442 <            else if (base == sp)
443 <                break;
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 <     * If a submission exists, try to activate and take it
450 >     * If a submission exists, try to activate and run it.
451       *
452 <     * @return a task, if available
452 >     * @return true if ran a task
453       */
454 <    private ForkJoinTask<?> pollSubmission() {
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 <            if (active || (active = p.tryIncrementActiveCount())) {
461 <                ForkJoinTask<?> t = p.pollSubmission();
462 <                return t != null ? t : scan(); // if missed, rescan
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 >                    UNSAFE.putOrderedObject(this, currentStealOffset, null);
468 >                    if (sp != base)
469 >                        execLocalTasks();
470 >                    return true;
471 >                }
472              }
473          }
474 <        return null;
474 >        return false;
475 >    }
476 >
477 >    /**
478 >     * Runs local tasks until queue is empty or shut down.  Call only
479 >     * while active.
480 >     */
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 AtomicObjectArray, but
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 protected by other volatile reads and are
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
# Line 435 | Line 520 | public class ForkJoinWorkerThread extend
520       * range. This method is used only during resets and backouts.
521       */
522      private static final void writeSlot(ForkJoinTask<?>[] q, int i,
523 <                                              ForkJoinTask<?> t) {
523 >                                        ForkJoinTask<?> t) {
524          UNSAFE.putObjectVolatile(q, (i << qShift) + qBase, t);
525      }
526  
# Line 447 | Line 532 | public class ForkJoinWorkerThread extend
532       * @param t the task. Caller must ensure non-null.
533       */
534      final void pushTask(ForkJoinTask<?> t) {
450        int s;
535          ForkJoinTask<?>[] q = queue;
536          int mask = q.length - 1; // implicit assert q != null
537 <        UNSAFE.putOrderedObject(q, (((s = sp++) & mask) << qShift) + qBase, t);
538 <        if ((s -= base) <= 0)
539 <            pool.signalWork();
540 <        else if (s + 1 >= mask)
541 <            growQueue();
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       * empty or contended. Note: Specializations of this code appear
548 <     * in scan and scanWhileJoining.
548 >     * in locallyDeqTask and elsewhere.
549       *
550       * @return a task, or null if none or contended
551       */
# Line 468 | Line 553 | public class ForkJoinWorkerThread extend
553          ForkJoinTask<?> t;
554          ForkJoinTask<?>[] q;
555          int b, i;
556 <        if ((b = base) != sp &&
556 >        if (sp != (b = base) &&
557              (q = queue) != null && // must read q after b
558 <            (t = q[i = (q.length - 1) & b]) != null &&
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;
# Line 480 | Line 565 | public class ForkJoinWorkerThread extend
565  
566      /**
567       * Tries to take a task from the base of own queue. Assumes active
568 <     * status.  Called only by current thread.
568 >     * status.  Called only by this thread.
569       *
570       * @return a task, or null if none
571       */
# Line 490 | Line 575 | public class ForkJoinWorkerThread extend
575              ForkJoinTask<?> t;
576              int b, i;
577              while (sp != (b = base)) {
578 <                if ((t = q[i = (q.length - 1) & b]) != null &&
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;
# Line 503 | Line 588 | public class ForkJoinWorkerThread extend
588  
589      /**
590       * Returns a popped task, or null if empty. Assumes active status.
591 <     * Called only by current thread. (Note: a specialization of this
507 <     * code appears in scanWhileJoining.)
591 >     * Called only by this thread.
592       */
593 <    final ForkJoinTask<?> popTask() {
510 <        int s;
593 >    private ForkJoinTask<?> popTask() {
594          ForkJoinTask<?>[] q = queue;
595 <        if (q != null && (s = sp) != base) {
596 <            int i = (q.length - 1) & --s;
597 <            ForkJoinTask<?> t = q[i];
598 <            if (t != null && UNSAFE.compareAndSwapObject
599 <                (q, (i << qShift) + qBase, t, null)) {
600 <                sp = s;
601 <                return t;
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)   // lost to stealer
602 >                    break;
603 >                if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
604 >                    sp = s; // putOrderedInt may encourage more timely write
605 >                    // UNSAFE.putOrderedInt(this, spOffset, s);
606 >                    return t;
607 >                }
608              }
609          }
610          return null;
611      }
612  
613      /**
614 <     * Specialized version of popTask to pop only if
615 <     * topmost element is the given task. Called only
527 <     * by current thread while active.
614 >     * Specialized version of popTask to pop only if topmost element
615 >     * is the given task. Called only by this thread while active.
616       *
617       * @param t the task. Caller must ensure non-null.
618       */
619      final boolean unpushTask(ForkJoinTask<?> t) {
620          int s;
621          ForkJoinTask<?>[] q = queue;
622 <        if (q != null && UNSAFE.compareAndSwapObject
623 <            (q, (((q.length - 1) & (s = sp - 1)) << qShift) + qBase, t, null)){
624 <            sp = s;
622 >        if ((s = sp) != base && q != null &&
623 >            UNSAFE.compareAndSwapObject
624 >            (q, (((q.length - 1) & --s) << qShift) + qBase, t, null)) {
625 >            sp = s; // putOrderedInt may encourage more timely write
626 >            // UNSAFE.putOrderedInt(this, spOffset, s);
627              return true;
628          }
629          return false;
630      }
631  
632      /**
633 <     * Returns next task or null if empty or contended
633 >     * Returns next task, or null if empty or contended.
634       */
635      final ForkJoinTask<?> peekTask() {
636          ForkJoinTask<?>[] q = queue;
# Line 582 | Line 672 | public class ForkJoinWorkerThread extend
672       * Computes next value for random victim probe in scan().  Scans
673       * don't require a very high quality generator, but also not a
674       * crummy one.  Marsaglia xor-shift is cheap and works well enough.
675 <     * Note: This is manually inlined in scan()
675 >     * Note: This is manually inlined in scan().
676       */
677      private static final int xorShift(int r) {
678          r ^= r << 13;
# Line 610 | Line 700 | public class ForkJoinWorkerThread extend
700       */
701      private ForkJoinTask<?> scan() {
702          ForkJoinPool p = pool;
703 <        ForkJoinWorkerThread[] ws = p.workers;
704 <        int n = ws.length;            // upper bound of #workers
705 <        boolean canSteal = active;    // shadow active status
706 <        int r = seed;                 // extract seed once
707 <        int k = r;                    // index: random if j<0 else step
708 <        for (int j = -n; j < n; ++j) {
709 <            ForkJoinWorkerThread v = ws[k & (n - 1)];
710 <            r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift
711 <            if (v != null && v.base != v.sp) {
712 <                if (canSteal ||       // ensure active status
713 <                    (canSteal = active = p.tryIncrementActiveCount())) {
714 <                    int b, i;         // inlined specialization of deqTask
715 <                    ForkJoinTask<?> t;
716 <                    ForkJoinTask<?>[] q;
717 <                    if ((b = v.base) != v.sp &&  // recheck
718 <                        (q = v.queue) != null &&
719 <                        (t = q[i = (q.length - 1) & b]) != null &&
720 <                        UNSAFE.compareAndSwapObject
721 <                        (q, (i << qShift) + qBase, t, null)) {
722 <                        v.base = b + 1;
723 <                        seed = r;
724 <                        ++stealCount;
725 <                        return t;
703 >        ForkJoinWorkerThread[] ws;        // worker array
704 >        int n;                            // upper bound of #workers
705 >        if ((ws = p.workers) != null && (n = ws.length) > 1) {
706 >            boolean canSteal = active;    // shadow active status
707 >            int r = seed;                 // extract seed once
708 >            int mask = n - 1;
709 >            int j = -n;                   // loop counter
710 >            int k = r;                    // worker index, random if j < 0
711 >            for (;;) {
712 >                ForkJoinWorkerThread v = ws[k & mask];
713 >                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // inline xorshift
714 >                ForkJoinTask<?>[] q; ForkJoinTask<?> t; int b, a;
715 >                if (v != null && (b = v.base) != v.sp &&
716 >                    (q = v.queue) != null) {
717 >                    int i = (q.length - 1) & b;
718 >                    long u = (i << qShift) + qBase; // raw offset
719 >                    int pid = poolIndex;
720 >                    if ((t = q[i]) != null) {
721 >                        if (!canSteal &&  // inline p.tryIncrementActiveCount
722 >                            UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
723 >                                                     a = p.runState, a + 1))
724 >                            canSteal = active = true;
725 >                        if (canSteal && v.base == b++ &&
726 >                            UNSAFE.compareAndSwapObject(q, u, t, null)) {
727 >                            v.base = b;
728 >                            v.stealHint = pid;
729 >                            UNSAFE.putOrderedObject(this,
730 >                                                    currentStealOffset, t);
731 >                            seed = r;
732 >                            ++stealCount;
733 >                            return t;
734 >                        }
735                      }
736 +                    j = -n;
737 +                    k = r;                // restart on contention
738                  }
739 <                j = -n;               // reset on contention
739 >                else if (++j <= 0)
740 >                    k = r;
741 >                else if (j <= n)
742 >                    k += (n >>> 1) | 1;
743 >                else
744 >                    break;
745              }
640            k = j >= 0? k + ((n >>> 1) | 1) : r;
746          }
747          return null;
748      }
# Line 645 | Line 750 | public class ForkJoinWorkerThread extend
750      // Run State management
751  
752      // status check methods used mainly by ForkJoinPool
753 <    final boolean isTerminating() { return (runState & TERMINATING) != 0; }
753 >    final boolean isRunning()     { return runState == 0; }
754      final boolean isTerminated()  { return (runState & TERMINATED) != 0; }
755      final boolean isSuspended()   { return (runState & SUSPENDED) != 0; }
756      final boolean isTrimmed()     { return (runState & TRIMMED) != 0; }
757  
758 +    final boolean isTerminating() {
759 +        if ((runState & TERMINATING) != 0)
760 +            return true;
761 +        if (pool.isAtLeastTerminating()) { // propagate pool state
762 +            shutdown();
763 +            return true;
764 +        }
765 +        return false;
766 +    }
767 +
768      /**
769 <     * Sets state to TERMINATING, also resuming if suspended.
769 >     * Sets state to TERMINATING. Does NOT unpark or interrupt
770 >     * to wake up if currently blocked. Callers must do so if desired.
771       */
772      final void shutdown() {
773          for (;;) {
774              int s = runState;
775 +            if ((s & (TERMINATING|TERMINATED)) != 0)
776 +                break;
777              if ((s & SUSPENDED) != 0) { // kill and wakeup if suspended
778                  if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
779                                               (s & ~SUSPENDED) |
780 <                                             (TRIMMED|TERMINATING))) {
663 <                    LockSupport.unpark(this);
780 >                                             (TRIMMED|TERMINATING)))
781                      break;
665                }
782              }
783              else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
784                                                s | TERMINATING))
# Line 671 | Line 787 | public class ForkJoinWorkerThread extend
787      }
788  
789      /**
790 <     * Sets state to TERMINATED. Called only by this thread.
790 >     * Sets state to TERMINATED. Called only by onTermination().
791       */
792      private void setTerminated() {
793          int s;
# Line 681 | Line 797 | public class ForkJoinWorkerThread extend
797      }
798  
799      /**
684     * Instrumented version of park. Also used by ForkJoinPool.awaitEvent
685     */
686    final void doPark() {
687        ++parkCount;
688        LockSupport.park(this);
689    }
690
691    /**
800       * If suspended, tries to set status to unsuspended.
801 <     * Caller must unpark to actually resume
801 >     * Does NOT wake up if blocked.
802       *
803       * @return true if successful
804       */
805      final boolean tryUnsuspend() {
806          int s;
807 <        return (((s = runState) & SUSPENDED) != 0 &&
808 <                UNSAFE.compareAndSwapInt(this, runStateOffset, s,
809 <                                         s & ~SUSPENDED));
807 >        while (((s = runState) & SUSPENDED) != 0) {
808 >            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
809 >                                         s & ~SUSPENDED))
810 >                return true;
811 >        }
812 >        return false;
813      }
814  
815      /**
816 <     * Sets suspended status and blocks as spare until resumed,
817 <     * shutdown, or timed out.
707 <     *
708 <     * @return false if trimmed
816 >     * Sets suspended status and blocks as spare until resumed
817 >     * or shutdown.
818       */
819 <    final boolean suspendAsSpare() {
820 <        for (;;) {               // set suspended unless terminating
819 >    final void suspendAsSpare() {
820 >        for (;;) {                  // set suspended unless terminating
821              int s = runState;
822              if ((s & TERMINATING) != 0) { // must kill
823                  if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
824                                               s | (TRIMMED | TERMINATING)))
825 <                    return false;
825 >                    return;
826              }
827              else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
828                                                s | SUSPENDED))
829                  break;
830          }
722        lastEventCount = 0;      // reset upon resume
831          ForkJoinPool p = pool;
832 <        p.releaseWaiters();      // help others progress
725 <        p.accumulateStealCount(this);
726 <        interrupted();           // clear/ignore interrupts
727 <        if (poolIndex < p.getParallelism()) { // untimed wait
728 <            while ((runState & SUSPENDED) != 0)
729 <                doPark();
730 <            return true;
731 <        }
732 <        return timedSuspend();   // timed wait if apparently non-core
733 <    }
734 <
735 <    /**
736 <     * Blocks as spare until resumed or timed out
737 <     * @return false if trimmed
738 <     */
739 <    private boolean timedSuspend() {
740 <        long nanos = SPARE_KEEPALIVE_NANOS;
741 <        long startTime = System.nanoTime();
832 >        p.pushSpare(this);
833          while ((runState & SUSPENDED) != 0) {
834 <            ++parkCount;
835 <            if ((nanos -= (System.nanoTime() - startTime)) > 0)
836 <                LockSupport.parkNanos(this, nanos);
837 <            else { // try to trim on timeout
838 <                int s = runState;
748 <                if (UNSAFE.compareAndSwapInt(this, runStateOffset, s,
749 <                                             (s & ~SUSPENDED) |
750 <                                             (TRIMMED|TERMINATING)))
751 <                    return false;
834 >            if (p.tryAccumulateStealCount(this)) {
835 >                interrupted();          // clear/ignore interrupts
836 >                if ((runState & SUSPENDED) == 0)
837 >                    break;
838 >                LockSupport.park(this);
839              }
840          }
754        return true;
841      }
842  
843      // Misc support methods for ForkJoinPool
# Line 761 | Line 847 | public class ForkJoinWorkerThread extend
847       * used by ForkJoinTask.
848       */
849      final int getQueueSize() {
850 <        return -base + sp;
851 <    }
766 <
767 <    /**
768 <     * Set locallyFifo mode. Called only by ForkJoinPool
769 <     */
770 <    final void setAsyncMode(boolean async) {
771 <        locallyFifo = async;
850 >        int n; // external calls must read base first
851 >        return (n = -base + sp) <= 0 ? 0 : n;
852      }
853  
854      /**
# Line 776 | Line 856 | public class ForkJoinWorkerThread extend
856       * thread.
857       */
858      final void cancelTasks() {
859 +        ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks
860 +        if (cj != null) {
861 +            currentJoin = null;
862 +            cj.cancelIgnoringExceptions();
863 +            try {
864 +                this.interrupt(); // awaken wait
865 +            } catch (SecurityException ignore) {
866 +            }
867 +        }
868 +        ForkJoinTask<?> cs = currentSteal;
869 +        if (cs != null) {
870 +            currentSteal = null;
871 +            cs.cancelIgnoringExceptions();
872 +        }
873          while (base != sp) {
874              ForkJoinTask<?> t = deqTask();
875              if (t != null)
# Line 803 | Line 897 | public class ForkJoinWorkerThread extend
897      // Support methods for ForkJoinTask
898  
899      /**
900 +     * Gets and removes a local task.
901 +     *
902 +     * @return a task, if available
903 +     */
904 +    final ForkJoinTask<?> pollLocalTask() {
905 +        ForkJoinPool p = pool;
906 +        while (sp != base) {
907 +            int a; // inline p.tryIncrementActiveCount
908 +            if (active ||
909 +                (active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset,
910 +                                                   a = p.runState, a + 1)))
911 +                return locallyFifo ? locallyDeqTask() : popTask();
912 +        }
913 +        return null;
914 +    }
915 +
916 +    /**
917 +     * Gets and removes a local or stolen task.
918 +     *
919 +     * @return a task, if available
920 +     */
921 +    final ForkJoinTask<?> pollTask() {
922 +        ForkJoinTask<?> t = pollLocalTask();
923 +        if (t == null) {
924 +            t = scan();
925 +            // cannot retain/track/help steal
926 +            UNSAFE.putOrderedObject(this, currentStealOffset, null);
927 +        }
928 +        return t;
929 +    }
930 +
931 +    /**
932 +     * Possibly runs some tasks and/or blocks, until task is done.
933 +     *
934 +     * @param joinMe the task to join
935 +     */
936 +    final void joinTask(ForkJoinTask<?> joinMe) {
937 +        // currentJoin only written by this thread; only need ordered store
938 +        ForkJoinTask<?> prevJoin = currentJoin;
939 +        UNSAFE.putOrderedObject(this, currentJoinOffset, joinMe);
940 +        if (sp != base)
941 +            localHelpJoinTask(joinMe);
942 +        if (joinMe.status >= 0)
943 +            pool.awaitJoin(joinMe, this);
944 +        UNSAFE.putOrderedObject(this, currentJoinOffset, prevJoin);
945 +    }
946 +
947 +    /**
948 +     * Run tasks in local queue until given task is done.
949 +     *
950 +     * @param joinMe the task to join
951 +     */
952 +    private void localHelpJoinTask(ForkJoinTask<?> joinMe) {
953 +        int s;
954 +        ForkJoinTask<?>[] q;
955 +        while (joinMe.status >= 0 && (s = sp) != base && (q = queue) != null) {
956 +            int i = (q.length - 1) & --s;
957 +            long u = (i << qShift) + qBase; // raw offset
958 +            ForkJoinTask<?> t = q[i];
959 +            if (t == null)  // lost to a stealer
960 +                break;
961 +            if (UNSAFE.compareAndSwapObject(q, u, t, null)) {
962 +                /*
963 +                 * This recheck (and similarly in helpJoinTask)
964 +                 * handles cases where joinMe is independently
965 +                 * cancelled or forced even though there is other work
966 +                 * available. Back out of the pop by putting t back
967 +                 * into slot before we commit by writing sp.
968 +                 */
969 +                if (joinMe.status < 0) {
970 +                    UNSAFE.putObjectVolatile(q, u, t);
971 +                    break;
972 +                }
973 +                sp = s;
974 +                // UNSAFE.putOrderedInt(this, spOffset, s);
975 +                t.quietlyExec();
976 +            }
977 +        }
978 +    }
979 +
980 +    /**
981 +     * Unless terminating, tries to locate and help perform tasks for
982 +     * a stealer of the given task, or in turn one of its stealers.
983 +     * Traces currentSteal->currentJoin links looking for a thread
984 +     * working on a descendant of the given task and with a non-empty
985 +     * queue to steal back and execute tasks from.
986 +     *
987 +     * The implementation is very branchy to cope with potential
988 +     * inconsistencies or loops encountering chains that are stale,
989 +     * unknown, or of length greater than MAX_HELP_DEPTH links.  All
990 +     * of these cases are dealt with by just returning back to the
991 +     * caller, who is expected to retry if other join mechanisms also
992 +     * don't work out.
993 +     *
994 +     * @param joinMe the task to join
995 +     */
996 +    final void helpJoinTask(ForkJoinTask<?> joinMe) {
997 +        ForkJoinWorkerThread[] ws;
998 +        int n;
999 +        if (joinMe.status < 0)                // already done
1000 +            return;
1001 +        if ((runState & TERMINATING) != 0) {  // cancel if shutting down
1002 +            joinMe.cancelIgnoringExceptions();
1003 +            return;
1004 +        }
1005 +        if ((ws = pool.workers) == null || (n = ws.length) <= 1)
1006 +            return;                           // need at least 2 workers
1007 +
1008 +        ForkJoinTask<?> task = joinMe;        // base of chain
1009 +        ForkJoinWorkerThread thread = this;   // thread with stolen task
1010 +        for (int d = 0; d < MAX_HELP_DEPTH; ++d) { // chain length
1011 +            // Try to find v, the stealer of task, by first using hint
1012 +            ForkJoinWorkerThread v = ws[thread.stealHint & (n - 1)];
1013 +            if (v == null || v.currentSteal != task) {
1014 +                for (int j = 0; ; ++j) {      // search array
1015 +                    if (j < n) {
1016 +                        ForkJoinTask<?> vs;
1017 +                        if ((v = ws[j]) != null &&
1018 +                            (vs = v.currentSteal) != null) {
1019 +                            if (joinMe.status < 0 || task.status < 0)
1020 +                                return;       // stale or done
1021 +                            if (vs == task) {
1022 +                                thread.stealHint = j;
1023 +                                break;        // save hint for next time
1024 +                            }
1025 +                        }
1026 +                    }
1027 +                    else
1028 +                        return;               // no stealer
1029 +                }
1030 +            }
1031 +            for (;;) { // Try to help v, using specialized form of deqTask
1032 +                if (joinMe.status < 0)
1033 +                    return;
1034 +                int b = v.base;
1035 +                ForkJoinTask<?>[] q = v.queue;
1036 +                if (b == v.sp || q == null)
1037 +                    break;
1038 +                int i = (q.length - 1) & b;
1039 +                long u = (i << qShift) + qBase;
1040 +                ForkJoinTask<?> t = q[i];
1041 +                int pid = poolIndex;
1042 +                ForkJoinTask<?> ps = currentSteal;
1043 +                if (task.status < 0)
1044 +                    return;                   // stale or done
1045 +                if (t != null && v.base == b++ &&
1046 +                    UNSAFE.compareAndSwapObject(q, u, t, null)) {
1047 +                    if (joinMe.status < 0) {
1048 +                        UNSAFE.putObjectVolatile(q, u, t);
1049 +                        return;               // back out on cancel
1050 +                    }
1051 +                    v.base = b;
1052 +                    v.stealHint = pid;
1053 +                    UNSAFE.putOrderedObject(this, currentStealOffset, t);
1054 +                    t.quietlyExec();
1055 +                    UNSAFE.putOrderedObject(this, currentStealOffset, ps);
1056 +                }
1057 +            }
1058 +            // Try to descend to find v's stealer
1059 +            ForkJoinTask<?> next = v.currentJoin;
1060 +            if (task.status < 0 || next == null || next == task ||
1061 +                joinMe.status < 0)
1062 +                return;
1063 +            task = next;
1064 +            thread = v;
1065 +        }
1066 +    }
1067 +
1068 +    /**
1069 +     * Implements ForkJoinTask.getSurplusQueuedTaskCount().
1070       * Returns an estimate of the number of tasks, offset by a
1071       * function of number of idle workers.
1072       *
# Line 854 | Line 1118 | public class ForkJoinWorkerThread extend
1118      }
1119  
1120      /**
857     * Gets and removes a local task.
858     *
859     * @return a task, if available
860     */
861    final ForkJoinTask<?> pollLocalTask() {
862        while (base != sp) {
863            if (active || (active = pool.tryIncrementActiveCount()))
864                return locallyFifo? locallyDeqTask() : popTask();
865        }
866        return null;
867    }
868
869    /**
870     * Gets and removes a local or stolen task.
871     *
872     * @return a task, if available
873     */
874    final ForkJoinTask<?> pollTask() {
875        ForkJoinTask<?> t;
876        return (t = pollLocalTask()) != null ? t : scan();
877    }
878
879    /**
880     * Returns a stolen task, if available, unless joinMe is done
881     *
882     * This method is intrinsically nonmodular. To maintain the
883     * property that tasks are never stolen if the awaited task is
884     * ready, we must interleave mechanics of scan with status
885     * checks. We rely here on the commit points of deq that allow us
886     * to cancel a steal even after CASing slot to null, but before
887     * adjusting base index: If, after the CAS, we see that joinMe is
888     * ready, we can back out by placing the task back into the slot,
889     * without adjusting index. The scan loop is otherwise the same as
890     * in scan.
891     *
892     * The outer loop cannot be allowed to run forever, because it
893     * could lead to a form of deadlock if all threads are executing
894     * this method. However, we must also be patient before giving up,
895     * to cope with GC stalls, transient high loads, etc. The loop
896     * terminates (causing caller to possibly block this thread and
897     * create a replacement) only after #workers clean sweeps during
898     * which all running threads are active.
899     */
900    final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) {
901        int sweeps = 0;
902        int r = seed;
903        ForkJoinPool p = pool;
904        p.releaseWaiters(); // help other threads progress
905        while (joinMe.status >= 0) {
906            ForkJoinWorkerThread[] ws = p.workers;
907            int n = ws.length;
908            int k = r;
909            for (int j = -n; j < n; ++j) {
910                ForkJoinWorkerThread v = ws[k & (n - 1)];
911                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift
912                if (v != null) {
913                    int b = v.base;
914                    ForkJoinTask<?>[] q;
915                    if (b != v.sp && (q = v.queue) != null) {
916                        int i = (q.length - 1) & b;
917                        ForkJoinTask<?> t = q[i];
918                        if (t != null) {
919                            if (joinMe.status < 0)
920                                return null;
921                            if (UNSAFE.compareAndSwapObject
922                                (q, (i << qShift) + qBase, t, null)) {
923                                if (joinMe.status < 0) {
924                                    writeSlot(q, i, t); // back out
925                                    return null;
926                                }
927                                v.base = b + 1;
928                                seed = r;
929                                ++stealCount;
930                                return t;
931                            }
932                        }
933                        sweeps = 0; // ensure rescan on contention
934                    }
935                }
936                k = j >= 0? k + ((n >>> 1) | 1) : r;
937                if ((j & 7) == 0 && joinMe.status < 0) // periodically recheck
938                    return null;
939            }
940            if ((sweeps = p.inactiveCount() == 0 ? sweeps + 1 : 0) > n)
941                return null;
942        }
943        return null;
944    }
945
946    /**
1121       * Runs tasks until {@code pool.isQuiescent()}.
1122       */
1123      final void helpQuiescePool() {
1124 +        ForkJoinTask<?> ps = currentSteal; // to restore below
1125          for (;;) {
1126              ForkJoinTask<?> t = pollLocalTask();
1127              if (t != null || (t = scan()) != null)
1128 <                t.tryExec();
1128 >                t.quietlyExec();
1129              else {
1130                  ForkJoinPool p = pool;
1131 +                int a; // to inline CASes
1132                  if (active) {
1133 +                    if (!UNSAFE.compareAndSwapInt
1134 +                        (p, poolRunStateOffset, a = p.runState, a - 1))
1135 +                        continue;   // retry later
1136                      active = false; // inactivate
1137 <                    do {} while (!p.tryDecrementActiveCount());
1137 >                    UNSAFE.putOrderedObject(this, currentStealOffset, ps);
1138                  }
1139                  if (p.isQuiescent()) {
1140                      active = true; // re-activate
1141 <                    do {} while (!p.tryIncrementActiveCount());
1141 >                    do {} while (!UNSAFE.compareAndSwapInt
1142 >                                 (p, poolRunStateOffset, a = p.runState, a+1));
1143                      return;
1144                  }
1145              }
# Line 969 | Line 1149 | public class ForkJoinWorkerThread extend
1149      // Unsafe mechanics
1150  
1151      private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1152 +    private static final long spOffset =
1153 +        objectFieldOffset("sp", ForkJoinWorkerThread.class);
1154      private static final long runStateOffset =
1155          objectFieldOffset("runState", ForkJoinWorkerThread.class);
1156 +    private static final long currentJoinOffset =
1157 +        objectFieldOffset("currentJoin", ForkJoinWorkerThread.class);
1158 +    private static final long currentStealOffset =
1159 +        objectFieldOffset("currentSteal", ForkJoinWorkerThread.class);
1160      private static final long qBase =
1161          UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
1162 +    private static final long poolRunStateOffset = // to inline CAS
1163 +        objectFieldOffset("runState", ForkJoinPool.class);
1164 +
1165      private static final int qShift;
1166  
1167      static {
# Line 980 | Line 1169 | public class ForkJoinWorkerThread extend
1169          if ((s & (s-1)) != 0)
1170              throw new Error("data type scale not a power of two");
1171          qShift = 31 - Integer.numberOfLeadingZeros(s);
1172 +        MAXIMUM_QUEUE_CAPACITY = 1 << (31 - qShift);
1173      }
1174  
1175      private static long objectFieldOffset(String field, Class<?> klazz) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines