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 |
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 |
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 |
|
*/ |
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; |
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; |
639 |
|
* @return a task, if available |
640 |
|
*/ |
641 |
|
final ForkJoinTask<?> pollLocalTask() { |
642 |
< |
return locallyFifo ? deqTask() : popTask(); |
642 |
> |
return locallyFifo ? locallyDeqTask() : popTask(); |
643 |
|
} |
644 |
|
|
645 |
|
/** |