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.23 by dl, Fri Jul 31 16:27:08 2009 UTC

# Line 65 | Line 65 | public class ForkJoinWorkerThread extend
65       * which gives threads a chance to activate if necessary before
66       * stealing (see below).
67       *
68 +     * This approach also enables support for "async mode" where local
69 +     * task processing is in FIFO, not LIFO order; simply by using a
70 +     * version of deq rather than pop when locallyFifo is true (as set
71 +     * by the ForkJoinPool).  This allows use in message-passing
72 +     * frameworks in which tasks are never joined.
73 +     *
74       * Efficient implementation of this approach currently relies on
75       * an uncomfortable amount of "Unsafe" mechanics. To maintain
76       * correct orderings, reads and writes of variable base require
# Line 309 | Line 315 | public class ForkJoinWorkerThread extend
315       * one.  Marsaglia xor-shift is cheap and works well.
316       */
317      private static int xorShift(int r) {
318 <        r ^= r << 1;
319 <        r ^= r >>> 3;
320 <        r ^= r << 10;
315 <        return r;
318 >        r ^= (r << 13);
319 >        r ^= (r >>> 17);
320 >        return r ^ (r << 5);
321      }
322  
323      // Lifecycle methods
# Line 470 | Line 475 | public class ForkJoinWorkerThread extend
475      }
476  
477      /**
478 +     * Tries to take a task from the base of own queue, activating if
479 +     * necessary, failing only if empty. Called only by current thread.
480 +     *
481 +     * @return a task, or null if none
482 +     */
483 +    final ForkJoinTask<?> locallyDeqTask() {
484 +        int b;
485 +        while (sp != (b = base)) {
486 +            if (tryActivate()) {
487 +                ForkJoinTask<?>[] q = queue;
488 +                int i = (q.length - 1) & b;
489 +                ForkJoinTask<?> t = q[i];
490 +                if (t != null && casSlotNull(q, i, t)) {
491 +                    base = b + 1;
492 +                    return t;
493 +                }
494 +            }
495 +        }
496 +        return null;
497 +    }
498 +
499 +    /**
500       * Returns a popped task, or null if empty. Ensures active status
501       * if non-null. Called only by current thread.
502       */
# Line 509 | Line 536 | public class ForkJoinWorkerThread extend
536      }
537  
538      /**
539 <     * Returns next task.
539 >     * Returns next task or null if empty or contended
540       */
541      final ForkJoinTask<?> peekTask() {
542          ForkJoinTask<?>[] q = queue;
# Line 600 | Line 627 | public class ForkJoinWorkerThread extend
627       * @return a task, if available
628       */
629      final ForkJoinTask<?> pollTask() {
630 <        ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask();
630 >        ForkJoinTask<?> t = locallyFifo ? locallyDeqTask() : popTask();
631          if (t == null && (t = scan()) != null)
632              ++stealCount;
633          return t;
# Line 612 | Line 639 | public class ForkJoinWorkerThread extend
639       * @return a task, if available
640       */
641      final ForkJoinTask<?> pollLocalTask() {
642 <        return locallyFifo ? deqTask() : popTask();
642 >        return locallyFifo ? locallyDeqTask() : popTask();
643      }
644  
645      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines