13 |
|
/** |
14 |
|
* A thread managed by a {@link ForkJoinPool}. This class is |
15 |
|
* subclassable solely for the sake of adding functionality -- there |
16 |
< |
* are no overridable methods dealing with scheduling or |
17 |
< |
* execution. However, you can override initialization and termination |
18 |
< |
* methods surrounding the main task processing loop. If you do |
19 |
< |
* create such a subclass, you will also need to supply a custom |
20 |
< |
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
16 |
> |
* are no overridable methods dealing with scheduling or execution. |
17 |
> |
* However, you can override initialization and termination methods |
18 |
> |
* surrounding the main task processing loop. If you do create such a |
19 |
> |
* subclass, you will also need to supply a custom {@link |
20 |
> |
* ForkJoinPool.ForkJoinWorkerThreadFactory} to use it in a {@code |
21 |
> |
* ForkJoinPool}. |
22 |
|
* |
23 |
|
* @since 1.7 |
24 |
|
* @author Doug Lea |
66 |
|
* which gives threads a chance to activate if necessary before |
67 |
|
* stealing (see below). |
68 |
|
* |
69 |
+ |
* This approach also enables support for "async mode" where local |
70 |
+ |
* task processing is in FIFO, not LIFO order; simply by using a |
71 |
+ |
* version of deq rather than pop when locallyFifo is true (as set |
72 |
+ |
* by the ForkJoinPool). This allows use in message-passing |
73 |
+ |
* frameworks in which tasks are never joined. |
74 |
+ |
* |
75 |
|
* Efficient implementation of this approach currently relies on |
76 |
|
* an uncomfortable amount of "Unsafe" mechanics. To maintain |
77 |
|
* correct orderings, reads and writes of variable base require |
316 |
|
* one. Marsaglia xor-shift is cheap and works well. |
317 |
|
*/ |
318 |
|
private static int xorShift(int r) { |
319 |
< |
r ^= r << 1; |
320 |
< |
r ^= r >>> 3; |
321 |
< |
r ^= r << 10; |
315 |
< |
return r; |
319 |
> |
r ^= (r << 13); |
320 |
> |
r ^= (r >>> 17); |
321 |
> |
return r ^ (r << 5); |
322 |
|
} |
323 |
|
|
324 |
|
// Lifecycle methods |
383 |
|
*/ |
384 |
|
protected void onTermination(Throwable exception) { |
385 |
|
// Execute remaining local tasks unless aborting or terminating |
386 |
< |
while (exception == null && !pool.isTerminating() && base != sp) { |
386 |
> |
while (exception == null && pool.isProcessingTasks() && base != sp) { |
387 |
|
try { |
388 |
|
ForkJoinTask<?> t = popTask(); |
389 |
|
if (t != null) |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
+ |
* Tries to take a task from the base of own queue, activating if |
480 |
+ |
* necessary, failing only if empty. Called only by current thread. |
481 |
+ |
* |
482 |
+ |
* @return a task, or null if none |
483 |
+ |
*/ |
484 |
+ |
final ForkJoinTask<?> locallyDeqTask() { |
485 |
+ |
int b; |
486 |
+ |
while (sp != (b = base)) { |
487 |
+ |
if (tryActivate()) { |
488 |
+ |
ForkJoinTask<?>[] q = queue; |
489 |
+ |
int i = (q.length - 1) & b; |
490 |
+ |
ForkJoinTask<?> t = q[i]; |
491 |
+ |
if (t != null && casSlotNull(q, i, t)) { |
492 |
+ |
base = b + 1; |
493 |
+ |
return t; |
494 |
+ |
} |
495 |
+ |
} |
496 |
+ |
} |
497 |
+ |
return null; |
498 |
+ |
} |
499 |
+ |
|
500 |
+ |
/** |
501 |
|
* Returns a popped task, or null if empty. Ensures active status |
502 |
|
* if non-null. Called only by current thread. |
503 |
|
*/ |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* Returns next task. |
540 |
> |
* Returns next task or null if empty or contended |
541 |
|
*/ |
542 |
|
final ForkJoinTask<?> peekTask() { |
543 |
|
ForkJoinTask<?>[] q = queue; |
628 |
|
* @return a task, if available |
629 |
|
*/ |
630 |
|
final ForkJoinTask<?> pollTask() { |
631 |
< |
ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask(); |
631 |
> |
ForkJoinTask<?> t = locallyFifo ? locallyDeqTask() : popTask(); |
632 |
|
if (t == null && (t = scan()) != null) |
633 |
|
++stealCount; |
634 |
|
return t; |
640 |
|
* @return a task, if available |
641 |
|
*/ |
642 |
|
final ForkJoinTask<?> pollLocalTask() { |
643 |
< |
return locallyFifo ? deqTask() : popTask(); |
643 |
> |
return locallyFifo ? locallyDeqTask() : popTask(); |
644 |
|
} |
645 |
|
|
646 |
|
/** |