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). |
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.tryAwaitJoin). |
114 |
|
* |
115 |
|
* Efficient implementation of these algorithms currently relies |
116 |
|
* on an uncomfortable amount of "Unsafe" mechanics. To maintain |
155 |
|
private static final Random seedGenerator = new Random(); |
156 |
|
|
157 |
|
/** |
157 |
– |
* The timeout value for suspending spares. Spare workers that |
158 |
– |
* remain unsignalled for more than this time may be trimmed |
159 |
– |
* (killed and removed from pool). Since our goal is to avoid |
160 |
– |
* long-term thread buildup, the exact value of timeout does not |
161 |
– |
* matter too much so long as it avoids most false-alarm timeouts |
162 |
– |
* under GC stalls or momentarily high system load. |
163 |
– |
*/ |
164 |
– |
private static final long SPARE_KEEPALIVE_NANOS = |
165 |
– |
5L * 1000L * 1000L * 1000L; // 5 secs |
166 |
– |
|
167 |
– |
/** |
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 |
164 |
|
private static final int MAX_HELP_DEPTH = 8; |
165 |
|
|
166 |
|
/** |
167 |
+ |
* The wakeup interval (in nanoseconds) for the oldest worker |
168 |
+ |
* suspended as spare. On each wakeup not signalled by a |
169 |
+ |
* resumption, it may ask the pool to reduce the number of spares. |
170 |
+ |
*/ |
171 |
+ |
private static final long TRIM_RATE_NANOS = |
172 |
+ |
5L * 1000L * 1000L * 1000L; // 5sec |
173 |
+ |
|
174 |
+ |
/** |
175 |
|
* Capacity of work-stealing queue array upon initialization. |
176 |
|
* Must be a power of two. Initial size must be at least 4, but is |
177 |
|
* padded to minimize cache effects. |
196 |
|
* Initialized in onStart, to improve memory locality. |
197 |
|
*/ |
198 |
|
private ForkJoinTask<?>[] queue; |
199 |
< |
|
199 |
> |
|
200 |
|
/** |
201 |
|
* Index (mod queue.length) of least valid queue slot, which is |
202 |
|
* always the next position to steal from if nonempty. |
225 |
|
* Run state of this worker. In addition to the usual run levels, |
226 |
|
* tracks if this worker is suspended as a spare, and if it was |
227 |
|
* killed (trimmed) while suspended. However, "active" status is |
228 |
< |
* maintained separately. |
228 |
> |
* maintained separately and modified only in conjunction with |
229 |
> |
* CASes of the pool's runState (which are currently sadly manually |
230 |
> |
* inlined for performance.) |
231 |
|
*/ |
232 |
|
private volatile int runState; |
233 |
|
|
237 |
|
private static final int TRIMMED = 0x08; // killed while suspended |
238 |
|
|
239 |
|
/** |
240 |
– |
* Number of LockSupport.park calls to block this thread for |
241 |
– |
* suspension or event waits. Used for internal instrumention; |
242 |
– |
* currently not exported but included because volatile write upon |
243 |
– |
* park also provides a workaround for a JVM bug. |
244 |
– |
*/ |
245 |
– |
volatile int parkCount; |
246 |
– |
|
247 |
– |
/** |
240 |
|
* Number of steals, transferred and reset in pool callbacks pool |
241 |
|
* when idle Accessed directly by pool. |
242 |
|
*/ |
248 |
|
*/ |
249 |
|
private int seed; |
250 |
|
|
259 |
– |
|
251 |
|
/** |
252 |
|
* Activity status. When true, this worker is considered active. |
253 |
|
* Accessed directly by pool. Must be false upon construction. |
256 |
|
|
257 |
|
/** |
258 |
|
* True if use local fifo, not default lifo, for local polling. |
259 |
< |
* Shadows value from ForkJoinPool, which resets it if changed |
269 |
< |
* pool-wide. |
259 |
> |
* Shadows value from ForkJoinPool. |
260 |
|
*/ |
261 |
|
private final boolean locallyFifo; |
262 |
< |
|
262 |
> |
|
263 |
|
/** |
264 |
|
* Index of this worker in pool array. Set once by pool before |
265 |
|
* running, and accessed directly by pool to locate this worker in |
280 |
|
volatile long nextWaiter; |
281 |
|
|
282 |
|
/** |
283 |
+ |
* Number of times this thread suspended as spare |
284 |
+ |
*/ |
285 |
+ |
int spareCount; |
286 |
+ |
|
287 |
+ |
/** |
288 |
+ |
* Encoded index and count of next spare waiter. Used only |
289 |
+ |
* by ForkJoinPool for managing spares. |
290 |
+ |
*/ |
291 |
+ |
volatile int nextSpare; |
292 |
+ |
|
293 |
+ |
/** |
294 |
|
* The task currently being joined, set only when actively trying |
295 |
|
* to helpStealer. Written only by current thread, but read by |
296 |
|
* others. |
297 |
|
*/ |
298 |
|
private volatile ForkJoinTask<?> currentJoin; |
299 |
< |
|
299 |
> |
|
300 |
|
/** |
301 |
|
* The task most recently stolen from another worker (or |
302 |
|
* submission queue). Not volatile because always read/written in |
313 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
314 |
|
this.pool = pool; |
315 |
|
this.locallyFifo = pool.locallyFifo; |
316 |
+ |
setDaemon(true); |
317 |
|
// To avoid exposing construction details to subclasses, |
318 |
|
// remaining initialization is in start() and onStart() |
319 |
|
} |
325 |
|
this.poolIndex = poolIndex; |
326 |
|
if (ueh != null) |
327 |
|
setUncaughtExceptionHandler(ueh); |
326 |
– |
setDaemon(true); |
328 |
|
start(); |
329 |
|
} |
330 |
|
|
383 |
|
*/ |
384 |
|
protected void onTermination(Throwable exception) { |
385 |
|
try { |
386 |
+ |
ForkJoinPool p = pool; |
387 |
+ |
if (active) { |
388 |
+ |
int a; // inline p.tryDecrementActiveCount |
389 |
+ |
active = false; |
390 |
+ |
do {} while(!UNSAFE.compareAndSwapInt |
391 |
+ |
(p, poolRunStateOffset, a = p.runState, a - 1)); |
392 |
+ |
} |
393 |
|
cancelTasks(); |
394 |
|
setTerminated(); |
395 |
< |
pool.workerTerminated(this); |
395 |
> |
p.workerTerminated(this); |
396 |
|
} catch (Throwable ex) { // Shouldn't ever happen |
397 |
|
if (exception == null) // but if so, at least rethrown |
398 |
|
exception = ex; |
425 |
|
* Find and execute tasks and check status while running |
426 |
|
*/ |
427 |
|
private void mainLoop() { |
428 |
< |
int emptyScans = 0; // consecutive times failed to find work |
428 |
> |
int misses = 0; // track consecutive times failed to find work; max 2 |
429 |
|
ForkJoinPool p = pool; |
430 |
|
for (;;) { |
431 |
< |
p.preStep(this, emptyScans); |
431 |
> |
p.preStep(this, misses); |
432 |
|
if (runState != 0) |
433 |
< |
return; |
434 |
< |
ForkJoinTask<?> t; // try to get and run stolen or submitted task |
435 |
< |
if ((t = scan()) != null || (t = pollSubmission()) != null) { |
428 |
< |
t.tryExec(); |
429 |
< |
if (base != sp) |
430 |
< |
runLocalTasks(); |
431 |
< |
currentSteal = null; |
432 |
< |
emptyScans = 0; |
433 |
< |
} |
434 |
< |
else |
435 |
< |
++emptyScans; |
433 |
> |
break; |
434 |
> |
misses = ((tryExecSteal() || tryExecSubmission()) ? 0 : |
435 |
> |
(misses < 2 ? misses + 1 : 2)); |
436 |
|
} |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
< |
* Runs local tasks until queue is empty or shut down. Call only |
441 |
< |
* while active. |
440 |
> |
* Try to steal a task and execute it |
441 |
> |
* |
442 |
> |
* @return true if ran a task |
443 |
|
*/ |
444 |
< |
private void runLocalTasks() { |
445 |
< |
while (runState == 0) { |
446 |
< |
ForkJoinTask<?> t = locallyFifo? locallyDeqTask() : popTask(); |
447 |
< |
if (t != null) |
448 |
< |
t.tryExec(); |
449 |
< |
else if (base == sp) |
450 |
< |
break; |
444 |
> |
private boolean tryExecSteal() { |
445 |
> |
ForkJoinTask<?> t; |
446 |
> |
if ((t = scan()) != null) { |
447 |
> |
t.quietlyExec(); |
448 |
> |
currentSteal = null; |
449 |
> |
if (sp != base) |
450 |
> |
execLocalTasks(); |
451 |
> |
return true; |
452 |
|
} |
453 |
+ |
return false; |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
< |
* If a submission exists, try to activate and take it |
457 |
> |
* If a submission exists, try to activate and run it; |
458 |
|
* |
459 |
< |
* @return a task, if available |
459 |
> |
* @return true if ran a task |
460 |
|
*/ |
461 |
< |
private ForkJoinTask<?> pollSubmission() { |
461 |
> |
private boolean tryExecSubmission() { |
462 |
|
ForkJoinPool p = pool; |
463 |
|
while (p.hasQueuedSubmissions()) { |
464 |
< |
if (active || (active = p.tryIncrementActiveCount())) { |
465 |
< |
ForkJoinTask<?> t = p.pollSubmission(); |
466 |
< |
if (t != null) { |
464 |
> |
ForkJoinTask<?> t; int a; |
465 |
> |
if (active || // ugly/hacky: inline p.tryIncrementActiveCount |
466 |
> |
(active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset, |
467 |
> |
a = p.runState, a + 1))) { |
468 |
> |
if ((t = p.pollSubmission()) != null) { |
469 |
|
currentSteal = t; |
470 |
< |
return t; |
470 |
> |
t.quietlyExec(); |
471 |
> |
currentSteal = null; |
472 |
> |
if (sp != base) |
473 |
> |
execLocalTasks(); |
474 |
> |
return true; |
475 |
|
} |
467 |
– |
return scan(); // if missed, rescan |
476 |
|
} |
477 |
|
} |
478 |
< |
return null; |
478 |
> |
return false; |
479 |
> |
} |
480 |
> |
|
481 |
> |
/** |
482 |
> |
* Runs local tasks until queue is empty or shut down. Call only |
483 |
> |
* while active. |
484 |
> |
*/ |
485 |
> |
private void execLocalTasks() { |
486 |
> |
while (runState == 0) { |
487 |
> |
ForkJoinTask<?> t = locallyFifo? locallyDeqTask() : popTask(); |
488 |
> |
if (t != null) |
489 |
> |
t.quietlyExec(); |
490 |
> |
else if (sp == base) |
491 |
> |
break; |
492 |
> |
} |
493 |
|
} |
494 |
|
|
495 |
|
/* |
557 |
|
ForkJoinTask<?> t; |
558 |
|
ForkJoinTask<?>[] q; |
559 |
|
int b, i; |
560 |
< |
if ((b = base) != sp && |
560 |
> |
if (sp != (b = base) && |
561 |
|
(q = queue) != null && // must read q after b |
562 |
|
(t = q[i = (q.length - 1) & b]) != null && base == b && |
563 |
|
UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) { |
592 |
|
|
593 |
|
/** |
594 |
|
* Returns a popped task, or null if empty. Assumes active status. |
595 |
< |
* Called only by current thread. |
595 |
> |
* Called only by current thread. |
596 |
|
*/ |
597 |
< |
final ForkJoinTask<?> popTask() { |
598 |
< |
int s; |
599 |
< |
ForkJoinTask<?>[] q; |
600 |
< |
if (base != (s = sp) && (q = queue) != null) { |
601 |
< |
int i = (q.length - 1) & --s; |
602 |
< |
ForkJoinTask<?> t = q[i]; |
603 |
< |
if (t != null && UNSAFE.compareAndSwapObject |
604 |
< |
(q, (i << qShift) + qBase, t, null)) { |
605 |
< |
sp = s; |
606 |
< |
return t; |
597 |
> |
private ForkJoinTask<?> popTask() { |
598 |
> |
ForkJoinTask<?>[] q = queue; |
599 |
> |
if (q != null) { |
600 |
> |
int s; |
601 |
> |
while ((s = sp) != base) { |
602 |
> |
int i = (q.length - 1) & --s; |
603 |
> |
long u = (i << qShift) + qBase; // raw offset |
604 |
> |
ForkJoinTask<?> t = q[i]; |
605 |
> |
if (t == null) // lost to stealer |
606 |
> |
break; |
607 |
> |
if (UNSAFE.compareAndSwapObject(q, u, t, null)) { |
608 |
> |
sp = s; // putOrderedInt may encourage more timely write |
609 |
> |
// UNSAFE.putOrderedInt(this, spOffset, s); |
610 |
> |
return t; |
611 |
> |
} |
612 |
|
} |
613 |
|
} |
614 |
|
return null; |
623 |
|
*/ |
624 |
|
final boolean unpushTask(ForkJoinTask<?> t) { |
625 |
|
int s; |
626 |
< |
ForkJoinTask<?>[] q; |
627 |
< |
if (base != (s = sp) && (q = queue) != null && |
626 |
> |
ForkJoinTask<?>[] q = queue; |
627 |
> |
if ((s = sp) != base && q != null && |
628 |
|
UNSAFE.compareAndSwapObject |
629 |
|
(q, (((q.length - 1) & --s) << qShift) + qBase, t, null)) { |
630 |
|
sp = s; |
631 |
+ |
// UNSAFE.putOrderedInt(this, spOffset, s); |
632 |
|
return true; |
633 |
|
} |
634 |
|
return false; |
717 |
|
ForkJoinWorkerThread v = ws[k & mask]; |
718 |
|
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // inline xorshift |
719 |
|
if (v != null && v.base != v.sp) { |
720 |
< |
if (canSteal || // ensure active status |
721 |
< |
(canSteal = active = p.tryIncrementActiveCount())) { |
722 |
< |
int b = v.base; // inline specialized deqTask |
723 |
< |
ForkJoinTask<?>[] q; |
724 |
< |
if (b != v.sp && (q = v.queue) != null) { |
725 |
< |
ForkJoinTask<?> t; |
726 |
< |
int i = (q.length - 1) & b; |
727 |
< |
long u = (i << qShift) + qBase; // raw offset |
728 |
< |
if ((t = q[i]) != null && v.base == b && |
729 |
< |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
730 |
< |
currentSteal = t; |
731 |
< |
v.stealHint = poolIndex; |
732 |
< |
v.base = b + 1; |
733 |
< |
seed = r; |
734 |
< |
++stealCount; |
735 |
< |
return t; |
736 |
< |
} |
720 |
> |
ForkJoinTask<?>[] q; int b, a; |
721 |
> |
if ((canSteal || // Ugly/hacky: inline |
722 |
> |
(canSteal = active = // p.tryIncrementActiveCount |
723 |
> |
UNSAFE.compareAndSwapInt(p, poolRunStateOffset, |
724 |
> |
a = p.runState, a + 1))) && |
725 |
> |
(q = v.queue) != null && (b = v.base) != v.sp) { |
726 |
> |
int i = (q.length - 1) & b; |
727 |
> |
long u = (i << qShift) + qBase; // raw offset |
728 |
> |
ForkJoinTask<?> t = q[i]; |
729 |
> |
if (v.base == b && t != null && |
730 |
> |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
731 |
> |
int pid = poolIndex; |
732 |
> |
currentSteal = t; |
733 |
> |
v.stealHint = pid; |
734 |
> |
v.base = b + 1; |
735 |
> |
seed = r; |
736 |
> |
++stealCount; |
737 |
> |
return t; |
738 |
|
} |
739 |
|
} |
740 |
|
j = -n; |
754 |
|
// Run State management |
755 |
|
|
756 |
|
// status check methods used mainly by ForkJoinPool |
757 |
+ |
final boolean isRunning() { return runState == 0; } |
758 |
|
final boolean isTerminating() { return (runState & TERMINATING) != 0; } |
759 |
|
final boolean isTerminated() { return (runState & TERMINATED) != 0; } |
760 |
|
final boolean isSuspended() { return (runState & SUSPENDED) != 0; } |
761 |
|
final boolean isTrimmed() { return (runState & TRIMMED) != 0; } |
762 |
|
|
763 |
|
/** |
764 |
< |
* Sets state to TERMINATING, also resuming if suspended. |
764 |
> |
* Sets state to TERMINATING. Does NOT unpark or interrupt |
765 |
> |
* to wake up if currently blocked. |
766 |
|
*/ |
767 |
|
final void shutdown() { |
768 |
|
for (;;) { |
769 |
|
int s = runState; |
770 |
+ |
if ((s & (TERMINATING|TERMINATED)) != 0) |
771 |
+ |
break; |
772 |
|
if ((s & SUSPENDED) != 0) { // kill and wakeup if suspended |
773 |
|
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
774 |
|
(s & ~SUSPENDED) | |
775 |
< |
(TRIMMED|TERMINATING))) { |
743 |
< |
LockSupport.unpark(this); |
775 |
> |
(TRIMMED|TERMINATING))) |
776 |
|
break; |
745 |
– |
} |
777 |
|
} |
778 |
|
else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
779 |
|
s | TERMINATING)) |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
< |
* Sets state to TERMINATED. Called only by this thread. |
785 |
> |
* Sets state to TERMINATED. Called only by onTermination() |
786 |
|
*/ |
787 |
|
private void setTerminated() { |
788 |
|
int s; |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* Instrumented version of park used by ForkJoinPool.awaitEvent |
765 |
< |
*/ |
766 |
< |
final void doPark() { |
767 |
< |
++parkCount; |
768 |
< |
LockSupport.park(this); |
769 |
< |
} |
770 |
< |
|
771 |
< |
/** |
772 |
< |
* If suspended, tries to set status to unsuspended and unparks. |
795 |
> |
* If suspended, tries to set status to unsuspended. |
796 |
|
* |
797 |
|
* @return true if successful |
798 |
|
*/ |
799 |
< |
final boolean tryResumeSpare() { |
800 |
< |
int s = runState; |
801 |
< |
if ((s & SUSPENDED) != 0 && |
802 |
< |
UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
803 |
< |
s & ~SUSPENDED)) { |
804 |
< |
LockSupport.unpark(this); |
782 |
< |
return true; |
799 |
> |
final boolean tryUnsuspend() { |
800 |
> |
int s; |
801 |
> |
while (((s = runState) & SUSPENDED) != 0) { |
802 |
> |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
803 |
> |
s & ~SUSPENDED)) |
804 |
> |
return true; |
805 |
|
} |
806 |
|
return false; |
807 |
|
} |
808 |
|
|
809 |
|
/** |
810 |
< |
* Sets suspended status and blocks as spare until resumed, |
811 |
< |
* shutdown, or timed out. |
790 |
< |
* |
791 |
< |
* @return false if trimmed |
810 |
> |
* Sets suspended status and blocks as spare until resumed |
811 |
> |
* or shutdown. |
812 |
|
*/ |
813 |
< |
final boolean suspendAsSpare() { |
814 |
< |
for (;;) { // set suspended unless terminating |
813 |
> |
final void suspendAsSpare() { |
814 |
> |
for (;;) { // set suspended unless terminating |
815 |
|
int s = runState; |
816 |
|
if ((s & TERMINATING) != 0) { // must kill |
817 |
|
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
818 |
|
s | (TRIMMED | TERMINATING))) |
819 |
< |
return false; |
819 |
> |
return; |
820 |
|
} |
821 |
|
else if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
822 |
|
s | SUSPENDED)) |
823 |
|
break; |
824 |
|
} |
825 |
< |
boolean timed; |
826 |
< |
long nanos; |
827 |
< |
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 |
820 |
< |
interrupted(); // clear/ignore interrupts |
825 |
> |
ForkJoinPool p = pool; |
826 |
> |
p.pushSpare(this); |
827 |
> |
lastEventCount = 0; // reset upon resume |
828 |
|
while ((runState & SUSPENDED) != 0) { |
829 |
< |
++parkCount; |
830 |
< |
if (!timed) |
831 |
< |
LockSupport.park(this); |
832 |
< |
else if ((nanos -= (System.nanoTime() - startTime)) > 0) |
833 |
< |
LockSupport.parkNanos(this, nanos); |
834 |
< |
else { // try to trim on timeout |
835 |
< |
int s = runState; |
836 |
< |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, |
837 |
< |
(s & ~SUSPENDED) | |
838 |
< |
(TRIMMED|TERMINATING))) |
839 |
< |
return false; |
829 |
> |
if (p.tryAccumulateStealCount(this)) { |
830 |
> |
boolean untimed = nextSpare != 0; |
831 |
> |
long startTime = untimed? 0 : System.nanoTime(); |
832 |
> |
interrupted(); // clear/ignore interrupts |
833 |
> |
if ((runState & SUSPENDED) == 0) |
834 |
> |
break; |
835 |
> |
if (untimed) // untimed |
836 |
> |
LockSupport.park(this); |
837 |
> |
else { |
838 |
> |
LockSupport.parkNanos(this, TRIM_RATE_NANOS); |
839 |
> |
if ((runState & SUSPENDED) == 0) |
840 |
> |
break; |
841 |
> |
if (System.nanoTime() - startTime >= TRIM_RATE_NANOS) |
842 |
> |
p.tryShutdownSpare(); |
843 |
> |
} |
844 |
|
} |
845 |
|
} |
835 |
– |
return true; |
846 |
|
} |
847 |
|
|
848 |
|
// Misc support methods for ForkJoinPool |
852 |
|
* used by ForkJoinTask. |
853 |
|
*/ |
854 |
|
final int getQueueSize() { |
855 |
< |
return -base + sp; |
855 |
> |
int n; // external calls must read base first |
856 |
> |
return (n = -base + sp) <= 0 ? 0 : n; |
857 |
|
} |
858 |
|
|
859 |
|
/** |
861 |
|
* thread. |
862 |
|
*/ |
863 |
|
final void cancelTasks() { |
864 |
< |
ForkJoinTask<?> cj = currentJoin; // try to kill live tasks |
864 |
> |
ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks |
865 |
|
if (cj != null) { |
866 |
|
currentJoin = null; |
867 |
|
cj.cancelIgnoringExceptions(); |
868 |
+ |
try { |
869 |
+ |
this.interrupt(); // awaken wait |
870 |
+ |
} catch (SecurityException ignore) { |
871 |
+ |
} |
872 |
|
} |
873 |
|
ForkJoinTask<?> cs = currentSteal; |
874 |
|
if (cs != null) { |
907 |
|
* @return a task, if available |
908 |
|
*/ |
909 |
|
final ForkJoinTask<?> pollLocalTask() { |
910 |
+ |
ForkJoinPool p = pool; |
911 |
|
while (sp != base) { |
912 |
< |
if (active || (active = pool.tryIncrementActiveCount())) |
912 |
> |
int a; // inline p.tryIncrementActiveCount |
913 |
> |
if (active || |
914 |
> |
(active = UNSAFE.compareAndSwapInt(p, poolRunStateOffset, |
915 |
> |
a = p.runState, a + 1))) |
916 |
|
return locallyFifo? locallyDeqTask() : popTask(); |
917 |
|
} |
918 |
|
return null; |
924 |
|
* @return a task, if available |
925 |
|
*/ |
926 |
|
final ForkJoinTask<?> pollTask() { |
927 |
< |
ForkJoinTask<?> t; |
928 |
< |
return (t = pollLocalTask()) != null ? t : scan(); |
927 |
> |
ForkJoinTask<?> t = pollLocalTask(); |
928 |
> |
if (t == null) { |
929 |
> |
t = scan(); |
930 |
> |
currentSteal = null; // cannot retain/track/help |
931 |
> |
} |
932 |
> |
return t; |
933 |
|
} |
934 |
|
|
935 |
|
/** |
936 |
|
* 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. |
937 |
|
* |
938 |
|
* @param joinMe the task to join |
921 |
– |
* @return task status on exit |
939 |
|
*/ |
940 |
< |
final int joinTask(ForkJoinTask<?> joinMe) { |
941 |
< |
int stat; |
940 |
> |
final void joinTask(ForkJoinTask<?> joinMe) { |
941 |
> |
// currentJoin only written by this thread; only need ordered store |
942 |
|
ForkJoinTask<?> prevJoin = currentJoin; |
943 |
< |
currentJoin = joinMe; |
944 |
< |
if ((stat = joinMe.status) >= 0 && |
945 |
< |
(sp == base || (stat = localHelpJoinTask(joinMe)) >= 0)) { |
946 |
< |
ForkJoinPool p = pool; |
947 |
< |
int helpRetries = 2; // initial patience values |
948 |
< |
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; |
943 |
> |
UNSAFE.putOrderedObject(this, currentJoinOffset, joinMe); |
944 |
> |
if (sp != base) |
945 |
> |
localHelpJoinTask(joinMe); |
946 |
> |
if (joinMe.status >= 0) |
947 |
> |
pool.awaitJoin(joinMe, this); |
948 |
> |
UNSAFE.putOrderedObject(this, currentJoinOffset, prevJoin); |
949 |
|
} |
950 |
|
|
951 |
|
/** |
952 |
|
* Run tasks in local queue until given task is done. |
953 |
|
* |
954 |
|
* @param joinMe the task to join |
956 |
– |
* @return task status on exit |
955 |
|
*/ |
956 |
< |
private int localHelpJoinTask(ForkJoinTask<?> joinMe) { |
957 |
< |
int stat, s; |
956 |
> |
private void localHelpJoinTask(ForkJoinTask<?> joinMe) { |
957 |
> |
int s; |
958 |
|
ForkJoinTask<?>[] q; |
959 |
< |
while ((stat = joinMe.status) >= 0 && |
962 |
< |
base != (s = sp) && (q = queue) != null) { |
963 |
< |
ForkJoinTask<?> t; |
959 |
> |
while (joinMe.status >= 0 && (s = sp) != base && (q = queue) != null) { |
960 |
|
int i = (q.length - 1) & --s; |
961 |
|
long u = (i << qShift) + qBase; // raw offset |
962 |
< |
if ((t = q[i]) != null && |
963 |
< |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
962 |
> |
ForkJoinTask<?> t = q[i]; |
963 |
> |
if (t == null) // lost to a stealer |
964 |
> |
break; |
965 |
> |
if (UNSAFE.compareAndSwapObject(q, u, t, null)) { |
966 |
|
/* |
967 |
|
* This recheck (and similarly in helpJoinTask) |
968 |
|
* handles cases where joinMe is independently |
970 |
|
* available. Back out of the pop by putting t back |
971 |
|
* into slot before we commit by writing sp. |
972 |
|
*/ |
973 |
< |
if ((stat = joinMe.status) < 0) { |
973 |
> |
if (joinMe.status < 0) { |
974 |
|
UNSAFE.putObjectVolatile(q, u, t); |
975 |
|
break; |
976 |
|
} |
977 |
|
sp = s; |
978 |
< |
t.tryExec(); |
978 |
> |
// UNSAFE.putOrderedInt(this, spOffset, s); |
979 |
> |
t.quietlyExec(); |
980 |
|
} |
981 |
|
} |
983 |
– |
return stat; |
982 |
|
} |
983 |
|
|
984 |
|
/** |
986 |
|
* given task, or in turn one of its stealers. Traces |
987 |
|
* currentSteal->currentJoin links looking for a thread working on |
988 |
|
* a descendant of the given task and with a non-empty queue to |
989 |
< |
* steal back and execute tasks from. Restarts search upon |
990 |
< |
* encountering chains that are stale, unknown, or of length |
991 |
< |
* greater than MAX_HELP_DEPTH links, to avoid unbounded cycles. |
992 |
< |
* |
993 |
< |
* The implementation is very branchy to cope with the restart |
994 |
< |
* cases. Returns void, not task status (which must be reread by |
995 |
< |
* caller anyway) to slightly simplify control paths. |
989 |
> |
* steal back and execute tasks from. |
990 |
> |
* |
991 |
> |
* The implementation is very branchy to cope with the potential |
992 |
> |
* inconsistencies or loops encountering chains that are stale, |
993 |
> |
* unknown, or of length greater than MAX_HELP_DEPTH links. All |
994 |
> |
* of these cases are dealt with by just returning back to the |
995 |
> |
* caller, who is expected to retry if other join mechanisms also |
996 |
> |
* don't work out. |
997 |
|
* |
998 |
|
* @param joinMe the task to join |
999 |
|
*/ |
1000 |
< |
final void helpJoinTask(ForkJoinTask<?> joinMe, int retries) { |
1000 |
> |
final void helpJoinTask(ForkJoinTask<?> joinMe) { |
1001 |
|
ForkJoinWorkerThread[] ws = pool.workers; |
1002 |
< |
int n; |
1003 |
< |
if (ws == null || (n = ws.length) <= 1) |
1005 |
< |
return; // need at least 2 workers |
1006 |
< |
|
1007 |
< |
restart:while (joinMe.status >= 0 && --retries >= 0) { |
1002 |
> |
int n; // need at least 2 workers |
1003 |
> |
if (ws != null && (n = ws.length) > 1 && joinMe.status >= 0) { |
1004 |
|
ForkJoinTask<?> task = joinMe; // base of chain |
1005 |
|
ForkJoinWorkerThread thread = this; // thread with stolen task |
1006 |
< |
for (int depth = 0; depth < MAX_HELP_DEPTH; ++depth) { |
1006 |
> |
for (int d = 0; d < MAX_HELP_DEPTH; ++d) { // chain length |
1007 |
|
// Try to find v, the stealer of task, by first using hint |
1008 |
|
ForkJoinWorkerThread v = ws[thread.stealHint & (n - 1)]; |
1009 |
|
if (v == null || v.currentSteal != task) { |
1010 |
|
for (int j = 0; ; ++j) { // search array |
1011 |
< |
if (task.status < 0 || j == n) |
1012 |
< |
continue restart; // stale or no stealer |
1013 |
< |
if ((v = ws[j]) != null && v.currentSteal == task) { |
1014 |
< |
thread.stealHint = j; // save for next time |
1015 |
< |
break; |
1011 |
> |
if (j < n) { |
1012 |
> |
if ((v = ws[j]) != null) { |
1013 |
> |
if (task.status < 0) |
1014 |
> |
return; // stale or done |
1015 |
> |
if (v.currentSteal == task) { |
1016 |
> |
thread.stealHint = j; |
1017 |
> |
break; // save hint for next time |
1018 |
> |
} |
1019 |
> |
} |
1020 |
|
} |
1021 |
+ |
else |
1022 |
+ |
return; // no stealer |
1023 |
|
} |
1024 |
|
} |
1025 |
|
// Try to help v, using specialized form of deqTask |
1027 |
|
ForkJoinTask<?>[] q; |
1028 |
|
while ((b = v.base) != v.sp && (q = v.queue) != null) { |
1029 |
|
int i = (q.length - 1) & b; |
1030 |
< |
long u = (i << qShift) + qBase; |
1030 |
> |
long u = (i << qShift) + qBase; |
1031 |
|
ForkJoinTask<?> t = q[i]; |
1032 |
< |
if (task.status < 0) // stale |
1033 |
< |
continue restart; |
1034 |
< |
if (v.base == b) { // recheck after reading t |
1035 |
< |
if (t == null) // producer stalled |
1036 |
< |
continue restart; // retry via restart |
1032 |
> |
if (task.status < 0) |
1033 |
> |
return; // stale or done |
1034 |
> |
if (v.base == b) { |
1035 |
> |
if (t == null) |
1036 |
> |
return; // producer stalled |
1037 |
|
if (UNSAFE.compareAndSwapObject(q, u, t, null)) { |
1038 |
< |
if (joinMe.status < 0) { |
1038 |
> |
if (joinMe.status < 0) { |
1039 |
|
UNSAFE.putObjectVolatile(q, u, t); |
1040 |
|
return; // back out on cancel |
1041 |
|
} |
1042 |
+ |
int pid = poolIndex; |
1043 |
|
ForkJoinTask<?> prevSteal = currentSteal; |
1044 |
|
currentSteal = t; |
1045 |
< |
v.stealHint = poolIndex; |
1045 |
> |
v.stealHint = pid; |
1046 |
|
v.base = b + 1; |
1047 |
< |
t.tryExec(); |
1047 |
> |
t.quietlyExec(); |
1048 |
|
currentSteal = prevSteal; |
1049 |
|
} |
1050 |
|
} |
1053 |
|
} |
1054 |
|
// Try to descend to find v's stealer |
1055 |
|
ForkJoinTask<?> next = v.currentJoin; |
1056 |
< |
if (next == null || task.status < 0) |
1057 |
< |
continue restart; // no descendent or stale |
1055 |
< |
if (joinMe.status < 0) |
1056 |
> |
if (task.status < 0 || next == null || next == task || |
1057 |
> |
joinMe.status < 0) |
1058 |
|
return; |
1059 |
|
task = next; |
1060 |
|
thread = v; |
1120 |
|
for (;;) { |
1121 |
|
ForkJoinTask<?> t = pollLocalTask(); |
1122 |
|
if (t != null || (t = scan()) != null) { |
1123 |
< |
t.tryExec(); |
1123 |
> |
t.quietlyExec(); |
1124 |
|
currentSteal = null; |
1125 |
|
} |
1126 |
|
else { |
1127 |
|
ForkJoinPool p = pool; |
1128 |
+ |
int a; // to inline CASes |
1129 |
|
if (active) { |
1130 |
+ |
if (!UNSAFE.compareAndSwapInt |
1131 |
+ |
(p, poolRunStateOffset, a = p.runState, a - 1)) |
1132 |
+ |
continue; // retry later |
1133 |
|
active = false; // inactivate |
1128 |
– |
do {} while (!p.tryDecrementActiveCount()); |
1134 |
|
} |
1135 |
|
if (p.isQuiescent()) { |
1136 |
|
active = true; // re-activate |
1137 |
< |
do {} while (!p.tryIncrementActiveCount()); |
1137 |
> |
do {} while(!UNSAFE.compareAndSwapInt |
1138 |
> |
(p, poolRunStateOffset, a = p.runState, a+1)); |
1139 |
|
return; |
1140 |
|
} |
1141 |
|
} |
1145 |
|
// Unsafe mechanics |
1146 |
|
|
1147 |
|
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1148 |
+ |
private static final long spOffset = |
1149 |
+ |
objectFieldOffset("sp", ForkJoinWorkerThread.class); |
1150 |
|
private static final long runStateOffset = |
1151 |
|
objectFieldOffset("runState", ForkJoinWorkerThread.class); |
1152 |
+ |
private static final long currentJoinOffset = |
1153 |
+ |
objectFieldOffset("currentJoin", ForkJoinWorkerThread.class); |
1154 |
+ |
private static final long currentStealOffset = |
1155 |
+ |
objectFieldOffset("currentSteal", ForkJoinWorkerThread.class); |
1156 |
|
private static final long qBase = |
1157 |
|
UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
1158 |
< |
private static final long threadStatusOffset = |
1159 |
< |
objectFieldOffset("threadStatus", Thread.class); |
1158 |
> |
private static final long poolRunStateOffset = // to inline CAS |
1159 |
> |
objectFieldOffset("runState", ForkJoinPool.class); |
1160 |
> |
|
1161 |
|
private static final int qShift; |
1162 |
|
|
1163 |
|
static { |