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 maintain per-task bookkeeping. This |
101 |
> |
* may require a linear scan of workers array to locate stealers, |
102 |
> |
* but usually doesn't because stealers leave hints (that may |
103 |
> |
* become stale/wrong) of where to locate the kathem. This |
104 |
> |
* isolates cost to when it is needed, rather than adding to |
105 |
> |
* per-task overhead. (2) It is "shallow", ignoring nesting and |
106 |
> |
* potentially cyclic mutual steals. (3) It is intentionally |
107 |
> |
* racy: field currentJoin is updated only while actively joining, |
108 |
> |
* which means that we could miss links in the chain during |
109 |
> |
* long-lived tasks, GC stalls etc. (4) We bound the number of |
110 |
> |
* attempts to find work (see MAX_HELP_DEPTH) and fall back to |
111 |
> |
* suspending the worker and if necessary replacing it with a |
112 |
> |
* spare (see ForkJoinPool.tryAwaitJoin). |
113 |
> |
* |
114 |
> |
* Efficient implementation of these algorithms currently relies |
115 |
> |
* on an uncomfortable amount of "Unsafe" mechanics. To maintain |
116 |
|
* correct orderings, reads and writes of variable base require |
117 |
|
* volatile ordering. Variable sp does not require volatile |
118 |
|
* writes but still needs store-ordering, which we accomplish by |
119 |
|
* pre-incrementing sp before filling the slot with an ordered |
120 |
|
* store. (Pre-incrementing also enables backouts used in |
121 |
< |
* scanWhileJoining.) Because they are protected by volatile base |
122 |
< |
* reads, reads of the queue array and its slots by other threads |
123 |
< |
* do not need volatile load semantics, but writes (in push) |
124 |
< |
* require store order and CASes (in pop and deq) require |
125 |
< |
* (volatile) CAS semantics. (Michael, Saraswat, and Vechev's |
126 |
< |
* algorithm has similar properties, but without support for |
127 |
< |
* nulling slots.) Since these combinations aren't supported |
128 |
< |
* using ordinary volatiles, the only way to accomplish these |
129 |
< |
* efficiently is to use direct Unsafe calls. (Using external |
130 |
< |
* AtomicIntegers and AtomicReferenceArrays for the indices and |
131 |
< |
* array is significantly slower because of memory locality and |
132 |
< |
* indirection effects.) |
121 |
> |
* joinTask.) Because they are protected by volatile base reads, |
122 |
> |
* reads of the queue array and its slots by other threads do not |
123 |
> |
* need volatile load semantics, but writes (in push) require |
124 |
> |
* store order and CASes (in pop and deq) require (volatile) CAS |
125 |
> |
* semantics. (Michael, Saraswat, and Vechev's algorithm has |
126 |
> |
* similar properties, but without support for nulling slots.) |
127 |
> |
* Since these combinations aren't supported using ordinary |
128 |
> |
* volatiles, the only way to accomplish these efficiently is to |
129 |
> |
* use direct Unsafe calls. (Using external AtomicIntegers and |
130 |
> |
* AtomicReferenceArrays for the indices and array is |
131 |
> |
* significantly slower because of memory locality and indirection |
132 |
> |
* effects.) |
133 |
|
* |
134 |
|
* Further, performance on most platforms is very sensitive to |
135 |
|
* placement and sizing of the (resizable) queue array. Even |
165 |
|
5L * 1000L * 1000L * 1000L; // 5 secs |
166 |
|
|
167 |
|
/** |
168 |
+ |
* The maximum stolen->joining link depth allowed in helpJoinTask. |
169 |
+ |
* Depths for legitimate chains are unbounded, but we use a fixed |
170 |
+ |
* constant to avoid (otherwise unchecked) cycles and bound |
171 |
+ |
* staleness of traversal parameters at the expense of sometimes |
172 |
+ |
* blocking when we could be helping. |
173 |
+ |
*/ |
174 |
+ |
private static final int MAX_HELP_DEPTH = 8; |
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* Capacity of work-stealing queue array upon initialization. |
178 |
|
* Must be a power of two. Initial size must be at least 4, but is |
179 |
|
* padded to minimize cache effects. |
215 |
|
private int sp; |
216 |
|
|
217 |
|
/** |
218 |
+ |
* The index of most recent stealer, used as a hint to avoid |
219 |
+ |
* traversal in method helpJoinTask. This is only a hint because a |
220 |
+ |
* worker might have had multiple steals and this only holds one |
221 |
+ |
* of them (usually the most current). Declared non-volatile, |
222 |
+ |
* relying on other prevailing sync to keep reasonably current. |
223 |
+ |
*/ |
224 |
+ |
private int stealHint; |
225 |
+ |
|
226 |
+ |
/** |
227 |
|
* Run state of this worker. In addition to the usual run levels, |
228 |
|
* tracks if this worker is suspended as a spare, and if it was |
229 |
|
* killed (trimmed) while suspended. However, "active" status is |
242 |
|
* currently not exported but included because volatile write upon |
243 |
|
* park also provides a workaround for a JVM bug. |
244 |
|
*/ |
245 |
< |
private volatile int parkCount; |
245 |
> |
volatile int parkCount; |
246 |
|
|
247 |
|
/** |
248 |
|
* Number of steals, transferred and reset in pool callbacks pool |
256 |
|
*/ |
257 |
|
private int seed; |
258 |
|
|
259 |
+ |
|
260 |
|
/** |
261 |
|
* Activity status. When true, this worker is considered active. |
262 |
|
* Accessed directly by pool. Must be false upon construction. |
268 |
|
* Shadows value from ForkJoinPool, which resets it if changed |
269 |
|
* pool-wide. |
270 |
|
*/ |
271 |
< |
private boolean locallyFifo; |
271 |
> |
private final boolean locallyFifo; |
272 |
|
|
273 |
|
/** |
274 |
|
* Index of this worker in pool array. Set once by pool before |
290 |
|
volatile long nextWaiter; |
291 |
|
|
292 |
|
/** |
293 |
+ |
* The task currently being joined, set only when actively trying |
294 |
+ |
* to helpStealer. Written only by current thread, but read by |
295 |
+ |
* others. |
296 |
+ |
*/ |
297 |
+ |
private volatile ForkJoinTask<?> currentJoin; |
298 |
+ |
|
299 |
+ |
/** |
300 |
+ |
* The task most recently stolen from another worker (or |
301 |
+ |
* submission queue). Not volatile because always read/written in |
302 |
+ |
* presence of related volatiles in those cases where it matters. |
303 |
+ |
*/ |
304 |
+ |
private ForkJoinTask<?> currentSteal; |
305 |
+ |
|
306 |
+ |
/** |
307 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
308 |
|
* |
309 |
|
* @param pool the pool this thread works in |
310 |
|
* @throws NullPointerException if pool is null |
311 |
|
*/ |
312 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
252 |
– |
if (pool == null) throw new NullPointerException(); |
313 |
|
this.pool = pool; |
314 |
+ |
this.locallyFifo = pool.locallyFifo; |
315 |
|
// To avoid exposing construction details to subclasses, |
316 |
|
// remaining initialization is in start() and onStart() |
317 |
|
} |
319 |
|
/** |
320 |
|
* Performs additional initialization and starts this thread |
321 |
|
*/ |
322 |
< |
final void start(int poolIndex, boolean locallyFifo, |
262 |
< |
UncaughtExceptionHandler ueh) { |
322 |
> |
final void start(int poolIndex, UncaughtExceptionHandler ueh) { |
323 |
|
this.poolIndex = poolIndex; |
264 |
– |
this.locallyFifo = locallyFifo; |
324 |
|
if (ueh != null) |
325 |
|
setUncaughtExceptionHandler(ueh); |
326 |
|
setDaemon(true); |
364 |
|
int rs = seedGenerator.nextInt(); |
365 |
|
seed = rs == 0? 1 : rs; // seed must be nonzero |
366 |
|
|
367 |
< |
// Allocate name string and queue array in this thread |
367 |
> |
// Allocate name string and arrays in this thread |
368 |
|
String pid = Integer.toString(pool.getPoolNumber()); |
369 |
|
String wid = Integer.toString(poolIndex); |
370 |
|
setName("ForkJoinPool-" + pid + "-worker-" + wid); |
417 |
|
* Find and execute tasks and check status while running |
418 |
|
*/ |
419 |
|
private void mainLoop() { |
420 |
< |
boolean ran = false; // true if ran task in last loop iter |
362 |
< |
boolean prevRan = false; // true if ran on last or previous step |
420 |
> |
int emptyScans = 0; // consecutive times failed to find work |
421 |
|
ForkJoinPool p = pool; |
422 |
|
for (;;) { |
423 |
< |
p.preStep(this, prevRan); |
423 |
> |
p.preStep(this, emptyScans); |
424 |
|
if (runState != 0) |
425 |
|
return; |
426 |
|
ForkJoinTask<?> t; // try to get and run stolen or submitted task |
428 |
|
t.tryExec(); |
429 |
|
if (base != sp) |
430 |
|
runLocalTasks(); |
431 |
< |
prevRan = ran = true; |
432 |
< |
} |
375 |
< |
else { |
376 |
< |
prevRan = ran; |
377 |
< |
ran = false; |
431 |
> |
currentSteal = null; |
432 |
> |
emptyScans = 0; |
433 |
|
} |
434 |
+ |
else |
435 |
+ |
++emptyScans; |
436 |
|
} |
437 |
|
} |
438 |
|
|
460 |
|
while (p.hasQueuedSubmissions()) { |
461 |
|
if (active || (active = p.tryIncrementActiveCount())) { |
462 |
|
ForkJoinTask<?> t = p.pollSubmission(); |
463 |
< |
return t != null ? t : scan(); // if missed, rescan |
463 |
> |
if (t != null) { |
464 |
> |
currentSteal = t; |
465 |
> |
return t; |
466 |
> |
} |
467 |
> |
return scan(); // if missed, rescan |
468 |
|
} |
469 |
|
} |
470 |
|
return null; |
527 |
|
/** |
528 |
|
* Tries to take a task from the base of the queue, failing if |
529 |
|
* empty or contended. Note: Specializations of this code appear |
530 |
< |
* in scan and scanWhileJoining. |
530 |
> |
* in locallyDeqTask and elsewhere. |
531 |
|
* |
532 |
|
* @return a task, or null if none or contended |
533 |
|
*/ |
537 |
|
int b, i; |
538 |
|
if ((b = base) != sp && |
539 |
|
(q = queue) != null && // must read q after b |
540 |
< |
(t = q[i = (q.length - 1) & b]) != null && |
540 |
> |
(t = q[i = (q.length - 1) & b]) != null && base == b && |
541 |
|
UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) { |
542 |
|
base = b + 1; |
543 |
|
return t; |
557 |
|
ForkJoinTask<?> t; |
558 |
|
int b, i; |
559 |
|
while (sp != (b = base)) { |
560 |
< |
if ((t = q[i = (q.length - 1) & b]) != null && |
560 |
> |
if ((t = q[i = (q.length - 1) & b]) != null && base == b && |
561 |
|
UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, |
562 |
|
t, null)) { |
563 |
|
base = b + 1; |
570 |
|
|
571 |
|
/** |
572 |
|
* Returns a popped task, or null if empty. Assumes active status. |
573 |
< |
* Called only by current thread. (Note: a specialization of this |
513 |
< |
* code appears in popWhileJoining.) |
573 |
> |
* Called only by current thread. |
574 |
|
*/ |
575 |
|
final ForkJoinTask<?> popTask() { |
576 |
|
int s; |
689 |
|
ForkJoinWorkerThread v = ws[k & mask]; |
690 |
|
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // inline xorshift |
691 |
|
if (v != null && v.base != v.sp) { |
692 |
< |
int b, i; // inline specialized deqTask |
693 |
< |
ForkJoinTask<?>[] q; |
694 |
< |
ForkJoinTask<?> t; |
695 |
< |
if ((canSteal || // ensure active status |
696 |
< |
(canSteal = active = p.tryIncrementActiveCount())) && |
697 |
< |
(q = v.queue) != null && |
698 |
< |
(t = q[i = (q.length - 1) & (b = v.base)]) != null && |
699 |
< |
UNSAFE.compareAndSwapObject |
700 |
< |
(q, (i << qShift) + qBase, t, null)) { |
701 |
< |
v.base = b + 1; |
702 |
< |
seed = r; |
703 |
< |
++stealCount; |
704 |
< |
return t; |
692 |
> |
if (canSteal || // ensure active status |
693 |
> |
(canSteal = active = p.tryIncrementActiveCount())) { |
694 |
> |
int b = v.base; // inline specialized deqTask |
695 |
> |
ForkJoinTask<?>[] q; |
696 |
> |
if (b != v.sp && (q = v.queue) != null) { |
697 |
> |
ForkJoinTask<?> t; |
698 |
> |
int i = (q.length - 1) & b; |
699 |
> |
long u = (i << qShift) + qBase; // raw offset |
700 |
> |
if ((t = q[i]) != null && v.base == b && |
701 |
> |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
702 |
> |
currentSteal = t; |
703 |
> |
v.stealHint = poolIndex; |
704 |
> |
v.base = b + 1; |
705 |
> |
seed = r; |
706 |
> |
++stealCount; |
707 |
> |
return t; |
708 |
> |
} |
709 |
> |
} |
710 |
|
} |
711 |
|
j = -n; |
712 |
|
k = r; // restart on contention |
761 |
|
} |
762 |
|
|
763 |
|
/** |
764 |
< |
* Instrumented version of park. Also used by ForkJoinPool.awaitEvent |
764 |
> |
* Instrumented version of park used by ForkJoinPool.awaitEvent |
765 |
|
*/ |
766 |
|
final void doPark() { |
767 |
|
++parkCount; |
769 |
|
} |
770 |
|
|
771 |
|
/** |
772 |
< |
* If suspended, tries to set status to unsuspended. |
708 |
< |
* Caller must unpark to actually resume |
772 |
> |
* If suspended, tries to set status to unsuspended and unparks. |
773 |
|
* |
774 |
|
* @return true if successful |
775 |
|
*/ |
776 |
< |
final boolean tryUnsuspend() { |
777 |
< |
int s; |
778 |
< |
return (((s = runState) & SUSPENDED) != 0 && |
779 |
< |
UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
780 |
< |
s & ~SUSPENDED)); |
776 |
> |
final boolean tryResumeSpare() { |
777 |
> |
int s = runState; |
778 |
> |
if ((s & SUSPENDED) != 0 && |
779 |
> |
UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
780 |
> |
s & ~SUSPENDED)) { |
781 |
> |
LockSupport.unpark(this); |
782 |
> |
return true; |
783 |
> |
} |
784 |
> |
return false; |
785 |
|
} |
786 |
|
|
787 |
|
/** |
802 |
|
s | SUSPENDED)) |
803 |
|
break; |
804 |
|
} |
805 |
+ |
boolean timed; |
806 |
+ |
long nanos; |
807 |
+ |
long startTime; |
808 |
+ |
if (poolIndex < pool.parallelism) { |
809 |
+ |
timed = false; |
810 |
+ |
nanos = 0L; |
811 |
+ |
startTime = 0L; |
812 |
+ |
} |
813 |
+ |
else { |
814 |
+ |
timed = true; |
815 |
+ |
nanos = SPARE_KEEPALIVE_NANOS; |
816 |
+ |
startTime = System.nanoTime(); |
817 |
+ |
} |
818 |
+ |
pool.accumulateStealCount(this); |
819 |
|
lastEventCount = 0; // reset upon resume |
738 |
– |
ForkJoinPool p = pool; |
739 |
– |
p.releaseWaiters(); // help others progress |
740 |
– |
p.accumulateStealCount(this); |
820 |
|
interrupted(); // clear/ignore interrupts |
742 |
– |
if (poolIndex < p.getParallelism()) { // untimed wait |
743 |
– |
while ((runState & SUSPENDED) != 0) |
744 |
– |
doPark(); |
745 |
– |
return true; |
746 |
– |
} |
747 |
– |
return timedSuspend(); // timed wait if apparently non-core |
748 |
– |
} |
749 |
– |
|
750 |
– |
/** |
751 |
– |
* Blocks as spare until resumed or timed out |
752 |
– |
* @return false if trimmed |
753 |
– |
*/ |
754 |
– |
private boolean timedSuspend() { |
755 |
– |
long nanos = SPARE_KEEPALIVE_NANOS; |
756 |
– |
long startTime = System.nanoTime(); |
821 |
|
while ((runState & SUSPENDED) != 0) { |
822 |
|
++parkCount; |
823 |
< |
if ((nanos -= (System.nanoTime() - startTime)) > 0) |
823 |
> |
if (!timed) |
824 |
> |
LockSupport.park(this); |
825 |
> |
else if ((nanos -= (System.nanoTime() - startTime)) > 0) |
826 |
|
LockSupport.parkNanos(this, nanos); |
827 |
|
else { // try to trim on timeout |
828 |
|
int s = runState; |
846 |
|
} |
847 |
|
|
848 |
|
/** |
783 |
– |
* Set locallyFifo mode. Called only by ForkJoinPool |
784 |
– |
*/ |
785 |
– |
final void setAsyncMode(boolean async) { |
786 |
– |
locallyFifo = async; |
787 |
– |
} |
788 |
– |
|
789 |
– |
/** |
849 |
|
* Removes and cancels all tasks in queue. Can be called from any |
850 |
|
* thread. |
851 |
|
*/ |
852 |
|
final void cancelTasks() { |
853 |
+ |
ForkJoinTask<?> cj = currentJoin; // try to kill live tasks |
854 |
+ |
if (cj != null) { |
855 |
+ |
currentJoin = null; |
856 |
+ |
cj.cancelIgnoringExceptions(); |
857 |
+ |
} |
858 |
+ |
ForkJoinTask<?> cs = currentSteal; |
859 |
+ |
if (cs != null) { |
860 |
+ |
currentSteal = null; |
861 |
+ |
cs.cancelIgnoringExceptions(); |
862 |
+ |
} |
863 |
|
while (base != sp) { |
864 |
|
ForkJoinTask<?> t = deqTask(); |
865 |
|
if (t != null) |
887 |
|
// Support methods for ForkJoinTask |
888 |
|
|
889 |
|
/** |
890 |
+ |
* Gets and removes a local task. |
891 |
+ |
* |
892 |
+ |
* @return a task, if available |
893 |
+ |
*/ |
894 |
+ |
final ForkJoinTask<?> pollLocalTask() { |
895 |
+ |
while (sp != base) { |
896 |
+ |
if (active || (active = pool.tryIncrementActiveCount())) |
897 |
+ |
return locallyFifo? locallyDeqTask() : popTask(); |
898 |
+ |
} |
899 |
+ |
return null; |
900 |
+ |
} |
901 |
+ |
|
902 |
+ |
/** |
903 |
+ |
* Gets and removes a local or stolen task. |
904 |
+ |
* |
905 |
+ |
* @return a task, if available |
906 |
+ |
*/ |
907 |
+ |
final ForkJoinTask<?> pollTask() { |
908 |
+ |
ForkJoinTask<?> t; |
909 |
+ |
return (t = pollLocalTask()) != null ? t : scan(); |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* Possibly runs some tasks and/or blocks, until task is done. |
914 |
+ |
* The main body is basically a big spinloop, alternating between |
915 |
+ |
* calls to helpJoinTask and pool.tryAwaitJoin with increased |
916 |
+ |
* patience parameters until either the task is done without |
917 |
+ |
* waiting, or we have, if necessary, created or resumed a |
918 |
+ |
* replacement for this thread while it blocks. |
919 |
+ |
* |
920 |
+ |
* @param joinMe the task to join |
921 |
+ |
* @return task status on exit |
922 |
+ |
*/ |
923 |
+ |
final int joinTask(ForkJoinTask<?> joinMe) { |
924 |
+ |
int stat; |
925 |
+ |
ForkJoinTask<?> prevJoin = currentJoin; |
926 |
+ |
currentJoin = joinMe; |
927 |
+ |
if ((stat = joinMe.status) >= 0 && |
928 |
+ |
(sp == base || (stat = localHelpJoinTask(joinMe)) >= 0)) { |
929 |
+ |
ForkJoinPool p = pool; |
930 |
+ |
int helpRetries = 2; // initial patience values |
931 |
+ |
int awaitRetries = -1; // -1 is sentinel for replace-check only |
932 |
+ |
do { |
933 |
+ |
helpJoinTask(joinMe, helpRetries); |
934 |
+ |
if ((stat = joinMe.status) < 0) |
935 |
+ |
break; |
936 |
+ |
boolean busy = p.tryAwaitJoin(joinMe, awaitRetries); |
937 |
+ |
if ((stat = joinMe.status) < 0) |
938 |
+ |
break; |
939 |
+ |
if (awaitRetries == -1) |
940 |
+ |
awaitRetries = 0; |
941 |
+ |
else if (busy) |
942 |
+ |
++awaitRetries; |
943 |
+ |
if (helpRetries < p.parallelism) |
944 |
+ |
helpRetries <<= 1; |
945 |
+ |
Thread.yield(); // tame unbounded loop |
946 |
+ |
} while (joinMe.status >= 0); |
947 |
+ |
} |
948 |
+ |
currentJoin = prevJoin; |
949 |
+ |
return stat; |
950 |
+ |
} |
951 |
+ |
|
952 |
+ |
/** |
953 |
+ |
* Run tasks in local queue until given task is done. |
954 |
+ |
* |
955 |
+ |
* @param joinMe the task to join |
956 |
+ |
* @return task status on exit |
957 |
+ |
*/ |
958 |
+ |
private int localHelpJoinTask(ForkJoinTask<?> joinMe) { |
959 |
+ |
int stat, s; |
960 |
+ |
ForkJoinTask<?>[] q; |
961 |
+ |
while ((stat = joinMe.status) >= 0 && |
962 |
+ |
base != (s = sp) && (q = queue) != null) { |
963 |
+ |
ForkJoinTask<?> t; |
964 |
+ |
int i = (q.length - 1) & --s; |
965 |
+ |
long u = (i << qShift) + qBase; // raw offset |
966 |
+ |
if ((t = q[i]) != null && |
967 |
+ |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
968 |
+ |
/* |
969 |
+ |
* This recheck (and similarly in helpJoinTask) |
970 |
+ |
* handles cases where joinMe is independently |
971 |
+ |
* cancelled or forced even though there is other work |
972 |
+ |
* available. Back out of the pop by putting t back |
973 |
+ |
* into slot before we commit by writing sp. |
974 |
+ |
*/ |
975 |
+ |
if ((stat = joinMe.status) < 0) { |
976 |
+ |
UNSAFE.putObjectVolatile(q, u, t); |
977 |
+ |
break; |
978 |
+ |
} |
979 |
+ |
sp = s; |
980 |
+ |
t.tryExec(); |
981 |
+ |
} |
982 |
+ |
} |
983 |
+ |
return stat; |
984 |
+ |
} |
985 |
+ |
|
986 |
+ |
/** |
987 |
+ |
* Tries to locate and help perform tasks for a stealer of the |
988 |
+ |
* given task, or in turn one of its stealers. Traces |
989 |
+ |
* currentSteal->currentJoin links looking for a thread working on |
990 |
+ |
* a descendant of the given task and with a non-empty queue to |
991 |
+ |
* steal back and execute tasks from. Restarts search upon |
992 |
+ |
* encountering chains that are stale, unknown, or of length |
993 |
+ |
* greater than MAX_HELP_DEPTH links, to avoid unbounded cycles. |
994 |
+ |
* |
995 |
+ |
* The implementation is very branchy to cope with the restart |
996 |
+ |
* cases. Returns void, not task status (which must be reread by |
997 |
+ |
* caller anyway) to slightly simplify control paths. |
998 |
+ |
* |
999 |
+ |
* @param joinMe the task to join |
1000 |
+ |
*/ |
1001 |
+ |
final void helpJoinTask(ForkJoinTask<?> joinMe, int retries) { |
1002 |
+ |
ForkJoinWorkerThread[] ws = pool.workers; |
1003 |
+ |
int n; |
1004 |
+ |
if (ws == null || (n = ws.length) <= 1) |
1005 |
+ |
return; // need at least 2 workers |
1006 |
+ |
|
1007 |
+ |
restart:while (joinMe.status >= 0 && --retries >= 0) { |
1008 |
+ |
ForkJoinTask<?> task = joinMe; // base of chain |
1009 |
+ |
ForkJoinWorkerThread thread = this; // thread with stolen task |
1010 |
+ |
for (int depth = 0; depth < MAX_HELP_DEPTH; ++depth) { |
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 (task.status < 0 || j == n) |
1016 |
+ |
continue restart; // stale or no stealer |
1017 |
+ |
if ((v = ws[j]) != null && v.currentSteal == task) { |
1018 |
+ |
thread.stealHint = j; // save for next time |
1019 |
+ |
break; |
1020 |
+ |
} |
1021 |
+ |
} |
1022 |
+ |
} |
1023 |
+ |
// Try to help v, using specialized form of deqTask |
1024 |
+ |
int b; |
1025 |
+ |
ForkJoinTask<?>[] q; |
1026 |
+ |
while ((b = v.base) != v.sp && (q = v.queue) != null) { |
1027 |
+ |
int i = (q.length - 1) & b; |
1028 |
+ |
long u = (i << qShift) + qBase; |
1029 |
+ |
ForkJoinTask<?> t = q[i]; |
1030 |
+ |
if (task.status < 0) // stale |
1031 |
+ |
continue restart; |
1032 |
+ |
if (v.base == b) { // recheck after reading t |
1033 |
+ |
if (t == null) // producer stalled |
1034 |
+ |
continue restart; // retry via restart |
1035 |
+ |
if (UNSAFE.compareAndSwapObject(q, u, t, null)) { |
1036 |
+ |
if (joinMe.status < 0) { |
1037 |
+ |
UNSAFE.putObjectVolatile(q, u, t); |
1038 |
+ |
return; // back out on cancel |
1039 |
+ |
} |
1040 |
+ |
ForkJoinTask<?> prevSteal = currentSteal; |
1041 |
+ |
currentSteal = t; |
1042 |
+ |
v.stealHint = poolIndex; |
1043 |
+ |
v.base = b + 1; |
1044 |
+ |
t.tryExec(); |
1045 |
+ |
currentSteal = prevSteal; |
1046 |
+ |
} |
1047 |
+ |
} |
1048 |
+ |
if (joinMe.status < 0) |
1049 |
+ |
return; |
1050 |
+ |
} |
1051 |
+ |
// Try to descend to find v's stealer |
1052 |
+ |
ForkJoinTask<?> next = v.currentJoin; |
1053 |
+ |
if (next == null || task.status < 0) |
1054 |
+ |
continue restart; // no descendent or stale |
1055 |
+ |
if (joinMe.status < 0) |
1056 |
+ |
return; |
1057 |
+ |
task = next; |
1058 |
+ |
thread = v; |
1059 |
+ |
} |
1060 |
+ |
} |
1061 |
+ |
} |
1062 |
+ |
|
1063 |
+ |
/** |
1064 |
|
* Returns an estimate of the number of tasks, offset by a |
1065 |
|
* function of number of idle workers. |
1066 |
|
* |
1112 |
|
} |
1113 |
|
|
1114 |
|
/** |
872 |
– |
* Gets and removes a local task. |
873 |
– |
* |
874 |
– |
* @return a task, if available |
875 |
– |
*/ |
876 |
– |
final ForkJoinTask<?> pollLocalTask() { |
877 |
– |
while (base != sp) { |
878 |
– |
if (active || (active = pool.tryIncrementActiveCount())) |
879 |
– |
return locallyFifo? locallyDeqTask() : popTask(); |
880 |
– |
} |
881 |
– |
return null; |
882 |
– |
} |
883 |
– |
|
884 |
– |
/** |
885 |
– |
* Gets and removes a local or stolen task. |
886 |
– |
* |
887 |
– |
* @return a task, if available |
888 |
– |
*/ |
889 |
– |
final ForkJoinTask<?> pollTask() { |
890 |
– |
ForkJoinTask<?> t; |
891 |
– |
return (t = pollLocalTask()) != null ? t : scan(); |
892 |
– |
} |
893 |
– |
|
894 |
– |
/** |
895 |
– |
* Executes or processes other tasks awaiting the given task |
896 |
– |
* @return task completion status |
897 |
– |
*/ |
898 |
– |
final int execWhileJoining(ForkJoinTask<?> joinMe) { |
899 |
– |
int s; |
900 |
– |
while ((s = joinMe.status) >= 0) { |
901 |
– |
ForkJoinTask<?> t = base != sp? |
902 |
– |
popWhileJoining(joinMe) : |
903 |
– |
scanWhileJoining(joinMe); |
904 |
– |
if (t != null) |
905 |
– |
t.tryExec(); |
906 |
– |
} |
907 |
– |
return s; |
908 |
– |
} |
909 |
– |
|
910 |
– |
/** |
911 |
– |
* Returns or stolen task, if available, unless joinMe is done |
912 |
– |
* |
913 |
– |
* This method is intrinsically nonmodular. To maintain the |
914 |
– |
* property that tasks are never stolen if the awaited task is |
915 |
– |
* ready, we must interleave mechanics of scan with status |
916 |
– |
* checks. We rely here on the commit points of deq that allow us |
917 |
– |
* to cancel a steal even after CASing slot to null, but before |
918 |
– |
* adjusting base index: If, after the CAS, we see that joinMe is |
919 |
– |
* ready, we can back out by placing the task back into the slot, |
920 |
– |
* without adjusting index. The loop is otherwise a variant of the |
921 |
– |
* one in scan(). |
922 |
– |
* |
923 |
– |
*/ |
924 |
– |
private ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) { |
925 |
– |
int r = seed; |
926 |
– |
ForkJoinPool p = pool; |
927 |
– |
ForkJoinWorkerThread[] ws; |
928 |
– |
int n; |
929 |
– |
outer:while ((ws = p.workers) != null && (n = ws.length) > 1) { |
930 |
– |
int mask = n - 1; |
931 |
– |
int k = r; |
932 |
– |
boolean contended = false; // to retry loop if deq contends |
933 |
– |
for (int j = -n; j <= n; ++j) { |
934 |
– |
if (joinMe.status < 0) |
935 |
– |
break outer; |
936 |
– |
int b; |
937 |
– |
ForkJoinTask<?>[] q; |
938 |
– |
ForkJoinWorkerThread v = ws[k & mask]; |
939 |
– |
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift |
940 |
– |
if (v != null && (b=v.base) != v.sp && (q=v.queue) != null) { |
941 |
– |
int i = (q.length - 1) & b; |
942 |
– |
ForkJoinTask<?> t = q[i]; |
943 |
– |
if (t != null && UNSAFE.compareAndSwapObject |
944 |
– |
(q, (i << qShift) + qBase, t, null)) { |
945 |
– |
if (joinMe.status >= 0) { |
946 |
– |
v.base = b + 1; |
947 |
– |
seed = r; |
948 |
– |
++stealCount; |
949 |
– |
return t; |
950 |
– |
} |
951 |
– |
UNSAFE.putObjectVolatile(q, (i<<qShift)+qBase, t); |
952 |
– |
break outer; // back out |
953 |
– |
} |
954 |
– |
contended = true; |
955 |
– |
} |
956 |
– |
k = j < 0 ? r : (k + ((n >>> 1) | 1)); |
957 |
– |
} |
958 |
– |
if (!contended && p.tryAwaitBusyJoin(joinMe)) |
959 |
– |
break; |
960 |
– |
} |
961 |
– |
return null; |
962 |
– |
} |
963 |
– |
|
964 |
– |
/** |
965 |
– |
* Version of popTask with join checks surrounding extraction. |
966 |
– |
* Uses the same backout strategy as helpJoinTask. Note that |
967 |
– |
* we ignore locallyFifo flag for local tasks here since helping |
968 |
– |
* joins only make sense in LIFO mode. |
969 |
– |
* |
970 |
– |
* @return a popped task, if available, unless joinMe is done |
971 |
– |
*/ |
972 |
– |
private ForkJoinTask<?> popWhileJoining(ForkJoinTask<?> joinMe) { |
973 |
– |
int s; |
974 |
– |
ForkJoinTask<?>[] q; |
975 |
– |
while ((s = sp) != base && (q = queue) != null && joinMe.status >= 0) { |
976 |
– |
int i = (q.length - 1) & --s; |
977 |
– |
ForkJoinTask<?> t = q[i]; |
978 |
– |
if (t != null && UNSAFE.compareAndSwapObject |
979 |
– |
(q, (i << qShift) + qBase, t, null)) { |
980 |
– |
if (joinMe.status >= 0) { |
981 |
– |
sp = s; |
982 |
– |
return t; |
983 |
– |
} |
984 |
– |
UNSAFE.putObjectVolatile(q, (i << qShift) + qBase, t); |
985 |
– |
break; // back out |
986 |
– |
} |
987 |
– |
} |
988 |
– |
return null; |
989 |
– |
} |
990 |
– |
|
991 |
– |
/** |
1115 |
|
* Runs tasks until {@code pool.isQuiescent()}. |
1116 |
|
*/ |
1117 |
|
final void helpQuiescePool() { |
1118 |
|
for (;;) { |
1119 |
|
ForkJoinTask<?> t = pollLocalTask(); |
1120 |
< |
if (t != null || (t = scan()) != null) |
1120 |
> |
if (t != null || (t = scan()) != null) { |
1121 |
|
t.tryExec(); |
1122 |
+ |
currentSteal = null; |
1123 |
+ |
} |
1124 |
|
else { |
1125 |
|
ForkJoinPool p = pool; |
1126 |
|
if (active) { |