33 |
|
* <p>A "main" {@code ForkJoinTask} begins execution when it is |
34 |
|
* explicitly submitted to a {@link ForkJoinPool}, or, if not already |
35 |
|
* engaged in a ForkJoin computation, commenced in the {@link |
36 |
< |
* ForkJoinPool#commonPool} via {@link #fork}, {@link #invoke}, or |
36 |
> |
* ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or |
37 |
|
* related methods. Once started, it will usually in turn start other |
38 |
|
* subtasks. As indicated by the name of this class, many programs |
39 |
|
* using {@code ForkJoinTask} employ only methods {@link #fork} and |
55 |
|
* minimize other blocking synchronization apart from joining other |
56 |
|
* tasks or using synchronizers such as Phasers that are advertised to |
57 |
|
* cooperate with fork/join scheduling. Subdividable tasks should also |
58 |
< |
* not perform blocking IO, and should ideally access variables that |
58 |
> |
* not perform blocking I/O, and should ideally access variables that |
59 |
|
* are completely independent of those accessed by other running |
60 |
|
* tasks. These guidelines are loosely enforced by not permitting |
61 |
|
* checked exceptions such as {@code IOExceptions} to be |
73 |
|
* <p>It is possible to define and use ForkJoinTasks that may block, |
74 |
|
* but doing do requires three further considerations: (1) Completion |
75 |
|
* of few if any <em>other</em> tasks should be dependent on a task |
76 |
< |
* that blocks on external synchronization or IO. Event-style async |
76 |
> |
* that blocks on external synchronization or I/O. Event-style async |
77 |
|
* tasks that are never joined (for example, those subclassing {@link |
78 |
|
* CountedCompleter}) often fall into this category. (2) To minimize |
79 |
|
* resource impact, tasks should be small; ideally performing only the |
285 |
|
*/ |
286 |
|
private int externalAwaitDone() { |
287 |
|
int s; |
288 |
+ |
ForkJoinPool.externalHelpJoin(this); |
289 |
|
boolean interrupted = false; |
290 |
< |
if ((s = status) >= 0 && ForkJoinPool.tryUnsubmitFromCommonPool(this)) |
290 |
< |
s = doExec(); |
291 |
< |
while (s >= 0) { |
290 |
> |
while ((s = status) >= 0) { |
291 |
|
if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) { |
292 |
|
synchronized (this) { |
293 |
|
if (status >= 0) { |
301 |
|
notifyAll(); |
302 |
|
} |
303 |
|
} |
305 |
– |
s = status; |
304 |
|
} |
305 |
|
if (interrupted) |
306 |
|
Thread.currentThread().interrupt(); |
311 |
|
* Blocks a non-worker-thread until completion or interruption. |
312 |
|
*/ |
313 |
|
private int externalInterruptibleAwaitDone() throws InterruptedException { |
314 |
+ |
int s; |
315 |
|
if (Thread.interrupted()) |
316 |
|
throw new InterruptedException(); |
317 |
< |
int s; |
318 |
< |
if ((s = status) >= 0 && ForkJoinPool.tryUnsubmitFromCommonPool(this)) |
320 |
< |
s = doExec(); |
321 |
< |
while (s >= 0) { |
317 |
> |
ForkJoinPool.externalHelpJoin(this); |
318 |
> |
while ((s = status) >= 0) { |
319 |
|
if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) { |
320 |
|
synchronized (this) { |
321 |
|
if (status >= 0) |
324 |
|
notifyAll(); |
325 |
|
} |
326 |
|
} |
330 |
– |
s = status; |
327 |
|
} |
328 |
|
return s; |
329 |
|
} |
330 |
|
|
331 |
+ |
|
332 |
|
/** |
333 |
|
* Implementation for join, get, quietlyJoin. Directly handles |
334 |
|
* only cases of already-completed, external wait, and |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* Records exception and possibly propagates |
438 |
> |
* Records exception and possibly propagates. |
439 |
|
* |
440 |
|
* @return status on exit |
441 |
|
*/ |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
+ |
* A version of "sneaky throw" to relay exceptions |
602 |
+ |
*/ |
603 |
+ |
static void rethrow(final Throwable ex) { |
604 |
+ |
if (ex != null) { |
605 |
+ |
if (ex instanceof Error) |
606 |
+ |
throw (Error)ex; |
607 |
+ |
if (ex instanceof RuntimeException) |
608 |
+ |
throw (RuntimeException)ex; |
609 |
+ |
throw uncheckedThrowable(ex, RuntimeException.class); |
610 |
+ |
} |
611 |
+ |
} |
612 |
+ |
|
613 |
+ |
/** |
614 |
+ |
* The sneaky part of sneaky throw, relying on generics |
615 |
+ |
* limitations to evade compiler complaints about rethrowing |
616 |
+ |
* unchecked exceptions |
617 |
+ |
*/ |
618 |
+ |
@SuppressWarnings("unchecked") static <T extends Throwable> |
619 |
+ |
T uncheckedThrowable(final Throwable t, final Class<T> c) { |
620 |
+ |
return (T)t; // rely on vacuous cast |
621 |
+ |
} |
622 |
+ |
|
623 |
+ |
/** |
624 |
|
* Throws exception, if any, associated with the given status. |
625 |
|
*/ |
626 |
|
private void reportException(int s) { |
627 |
< |
Throwable ex = ((s == CANCELLED) ? new CancellationException() : |
628 |
< |
(s == EXCEPTIONAL) ? getThrowableException() : |
629 |
< |
null); |
630 |
< |
if (ex != null) |
611 |
< |
U.throwException(ex); |
627 |
> |
if (s == CANCELLED) |
628 |
> |
throw new CancellationException(); |
629 |
> |
if (s == EXCEPTIONAL) |
630 |
> |
rethrow(getThrowableException()); |
631 |
|
} |
632 |
|
|
633 |
|
// public methods |
635 |
|
/** |
636 |
|
* Arranges to asynchronously execute this task in the pool the |
637 |
|
* current task is running in, if applicable, or using the {@link |
638 |
< |
* ForkJoinPool#commonPool} if not {@link #inForkJoinPool}. While |
638 |
> |
* ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}. While |
639 |
|
* it is not necessarily enforced, it is a usage error to fork a |
640 |
|
* task more than once unless it has completed and been |
641 |
|
* reinitialized. Subsequent modifications to the state of this |
652 |
|
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) |
653 |
|
((ForkJoinWorkerThread)t).workQueue.push(this); |
654 |
|
else |
655 |
< |
ForkJoinPool.submitToCommonPool(this); |
655 |
> |
ForkJoinPool.commonPool.externalPush(this); |
656 |
|
return this; |
657 |
|
} |
658 |
|
|
754 |
|
} |
755 |
|
} |
756 |
|
if (ex != null) |
757 |
< |
U.throwException(ex); |
757 |
> |
rethrow(ex); |
758 |
|
} |
759 |
|
|
760 |
|
/** |
805 |
|
} |
806 |
|
} |
807 |
|
if (ex != null) |
808 |
< |
U.throwException(ex); |
808 |
> |
rethrow(ex); |
809 |
|
return tasks; |
810 |
|
} |
811 |
|
|
978 |
|
if (Thread.interrupted()) |
979 |
|
throw new InterruptedException(); |
980 |
|
// Messy in part because we measure in nanosecs, but wait in millisecs |
981 |
< |
int s; long ns, ms; |
982 |
< |
if ((s = status) >= 0 && (ns = unit.toNanos(timeout)) > 0L) { |
981 |
> |
int s; long ms; |
982 |
> |
long ns = unit.toNanos(timeout); |
983 |
> |
if ((s = status) >= 0 && ns > 0L) { |
984 |
|
long deadline = System.nanoTime() + ns; |
985 |
|
ForkJoinPool p = null; |
986 |
|
ForkJoinPool.WorkQueue w = null; |
989 |
|
ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t; |
990 |
|
p = wt.pool; |
991 |
|
w = wt.workQueue; |
992 |
< |
s = p.helpJoinOnce(w, this); // no retries on failure |
992 |
> |
p.helpJoinOnce(w, this); // no retries on failure |
993 |
|
} |
994 |
+ |
else |
995 |
+ |
ForkJoinPool.externalHelpJoin(this); |
996 |
|
boolean canBlock = false; |
997 |
|
boolean interrupted = false; |
998 |
|
try { |
999 |
|
while ((s = status) >= 0) { |
1000 |
< |
if (w != null && w.runState < 0) |
1000 |
> |
if (w != null && w.qlock < 0) |
1001 |
|
cancelIgnoringExceptions(this); |
1002 |
|
else if (!canBlock) { |
1003 |
< |
if (p == null || p.tryCompensate(this, null)) |
1003 |
> |
if (p == null || p.tryCompensate()) |
1004 |
|
canBlock = true; |
1005 |
|
} |
1006 |
|
else { |
1139 |
|
*/ |
1140 |
|
public boolean tryUnfork() { |
1141 |
|
Thread t; |
1142 |
< |
return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ? |
1143 |
< |
((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) : |
1144 |
< |
ForkJoinPool.tryUnsubmitFromCommonPool(this); |
1142 |
> |
return (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ? |
1143 |
> |
((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) : |
1144 |
> |
ForkJoinPool.tryExternalUnpush(this)); |
1145 |
|
} |
1146 |
|
|
1147 |
|
/** |
1153 |
|
* @return the number of tasks |
1154 |
|
*/ |
1155 |
|
public static int getQueuedTaskCount() { |
1156 |
< |
Thread t; |
1157 |
< |
return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ? |
1158 |
< |
((ForkJoinWorkerThread)t).workQueue.queueSize() : |
1159 |
< |
ForkJoinPool.getEstimatedSubmitterQueueLength(); |
1156 |
> |
Thread t; ForkJoinPool.WorkQueue q; |
1157 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) |
1158 |
> |
q = ((ForkJoinWorkerThread)t).workQueue; |
1159 |
> |
else |
1160 |
> |
q = ForkJoinPool.commonSubmitterQueue(); |
1161 |
> |
return (q == null) ? 0 : q.queueSize(); |
1162 |
|
} |
1163 |
|
|
1164 |
|
/** |
1175 |
|
* @return the surplus number of tasks, which may be negative |
1176 |
|
*/ |
1177 |
|
public static int getSurplusQueuedTaskCount() { |
1178 |
< |
/* |
1155 |
< |
* The aim of this method is to return a cheap heuristic guide |
1156 |
< |
* for task partitioning when programmers, frameworks, tools, |
1157 |
< |
* or languages have little or no idea about task granularity. |
1158 |
< |
* In essence by offering this method, we ask users only about |
1159 |
< |
* tradeoffs in overhead vs expected throughput and its |
1160 |
< |
* variance, rather than how finely to partition tasks. |
1161 |
< |
* |
1162 |
< |
* In a steady state strict (tree-structured) computation, |
1163 |
< |
* each thread makes available for stealing enough tasks for |
1164 |
< |
* other threads to remain active. Inductively, if all threads |
1165 |
< |
* play by the same rules, each thread should make available |
1166 |
< |
* only a constant number of tasks. |
1167 |
< |
* |
1168 |
< |
* The minimum useful constant is just 1. But using a value of |
1169 |
< |
* 1 would require immediate replenishment upon each steal to |
1170 |
< |
* maintain enough tasks, which is infeasible. Further, |
1171 |
< |
* partitionings/granularities of offered tasks should |
1172 |
< |
* minimize steal rates, which in general means that threads |
1173 |
< |
* nearer the top of computation tree should generate more |
1174 |
< |
* than those nearer the bottom. In perfect steady state, each |
1175 |
< |
* thread is at approximately the same level of computation |
1176 |
< |
* tree. However, producing extra tasks amortizes the |
1177 |
< |
* uncertainty of progress and diffusion assumptions. |
1178 |
< |
* |
1179 |
< |
* So, users will want to use values larger, but not much |
1180 |
< |
* larger than 1 to both smooth over transient shortages and |
1181 |
< |
* hedge against uneven progress; as traded off against the |
1182 |
< |
* cost of extra task overhead. We leave the user to pick a |
1183 |
< |
* threshold value to compare with the results of this call to |
1184 |
< |
* guide decisions, but recommend values such as 3. |
1185 |
< |
* |
1186 |
< |
* When all threads are active, it is on average OK to |
1187 |
< |
* estimate surplus strictly locally. In steady-state, if one |
1188 |
< |
* thread is maintaining say 2 surplus tasks, then so are |
1189 |
< |
* others. So we can just use estimated queue length. |
1190 |
< |
* However, this strategy alone leads to serious mis-estimates |
1191 |
< |
* in some non-steady-state conditions (ramp-up, ramp-down, |
1192 |
< |
* other stalls). We can detect many of these by further |
1193 |
< |
* considering the number of "idle" threads, that are known to |
1194 |
< |
* have zero queued tasks, so compensate by a factor of |
1195 |
< |
* (#idle/#active) threads. |
1196 |
< |
*/ |
1197 |
< |
Thread t; ForkJoinWorkerThread wt; |
1198 |
< |
return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ? |
1199 |
< |
(wt = (ForkJoinWorkerThread)t).workQueue.queueSize() - wt.pool.idlePerActive() : |
1200 |
< |
0; |
1178 |
> |
return ForkJoinPool.getSurplusQueuedTaskCount(); |
1179 |
|
} |
1180 |
|
|
1181 |
|
// Extension methods |
1219 |
|
/** |
1220 |
|
* Returns, but does not unschedule or execute, a task queued by |
1221 |
|
* the current thread but not yet executed, if one is immediately |
1222 |
< |
* available and the current thread is operating in a |
1223 |
< |
* ForkJoinPool. There is no guarantee that this task will |
1224 |
< |
* actually be polled or executed next. Conversely, this method |
1225 |
< |
* may return null even if a task exists but cannot be accessed |
1248 |
< |
* without contention with other threads. This method is designed |
1222 |
> |
* available. There is no guarantee that this task will actually |
1223 |
> |
* be polled or executed next. Conversely, this method may return |
1224 |
> |
* null even if a task exists but cannot be accessed without |
1225 |
> |
* contention with other threads. This method is designed |
1226 |
|
* primarily to support extensions, and is unlikely to be useful |
1227 |
|
* otherwise. |
1228 |
|
* |
1229 |
|
* @return the next task, or {@code null} if none are available |
1230 |
|
*/ |
1231 |
|
protected static ForkJoinTask<?> peekNextLocalTask() { |
1232 |
< |
Thread t; |
1233 |
< |
return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ? |
1234 |
< |
((ForkJoinWorkerThread)t).workQueue.peek() : |
1235 |
< |
null; |
1232 |
> |
Thread t; ForkJoinPool.WorkQueue q; |
1233 |
> |
if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) |
1234 |
> |
q = ((ForkJoinWorkerThread)t).workQueue; |
1235 |
> |
else |
1236 |
> |
q = ForkJoinPool.commonSubmitterQueue(); |
1237 |
> |
return (q == null) ? null : q.peek(); |
1238 |
|
} |
1239 |
|
|
1240 |
|
/** |
1459 |
|
// Unsafe mechanics |
1460 |
|
private static final sun.misc.Unsafe U; |
1461 |
|
private static final long STATUS; |
1462 |
+ |
|
1463 |
|
static { |
1464 |
|
exceptionTableLock = new ReentrantLock(); |
1465 |
|
exceptionTableRefQueue = new ReferenceQueue<Object>(); |
1466 |
|
exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY]; |
1467 |
|
try { |
1468 |
|
U = getUnsafe(); |
1469 |
+ |
Class<?> k = ForkJoinTask.class; |
1470 |
|
STATUS = U.objectFieldOffset |
1471 |
< |
(ForkJoinTask.class.getDeclaredField("status")); |
1471 |
> |
(k.getDeclaredField("status")); |
1472 |
|
} catch (Exception e) { |
1473 |
|
throw new Error(e); |
1474 |
|
} |
1501 |
|
} |
1502 |
|
} |
1503 |
|
} |
1504 |
+ |
|
1505 |
|
} |