9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import java.util.concurrent.atomic.*; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
12 |
|
|
13 |
|
/** |
14 |
|
* Abstract base class for tasks that run within a {@link |
79 |
|
* class. While these methods have {@code public} access (to allow |
80 |
|
* instances of different task subclasses to call each others |
81 |
|
* methods), some of them may only be called from within other |
82 |
< |
* ForkJoinTasks. Attempts to invoke them in other contexts result in |
83 |
< |
* exceptions or errors possibly including ClassCastException. |
82 |
> |
* ForkJoinTasks (as may be determined using method {@link |
83 |
> |
* #inForkJoinPool}). Attempts to invoke them in other contexts |
84 |
> |
* result in exceptions or errors, possibly including |
85 |
> |
* ClassCastException. |
86 |
|
* |
87 |
|
* <p>Most base support methods are {@code final} because their |
88 |
|
* implementations are intrinsically tied to the underlying |
107 |
|
* in general sensible to serialize tasks only before or after, but |
108 |
|
* not during execution. Serialization is not relied on during |
109 |
|
* execution itself. |
110 |
+ |
* |
111 |
+ |
* @since 1.7 |
112 |
+ |
* @author Doug Lea |
113 |
|
*/ |
114 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
115 |
|
|
144 |
|
/** |
145 |
|
* Table of exceptions thrown by tasks, to enable reporting by |
146 |
|
* callers. Because exceptions are rare, we don't directly keep |
147 |
< |
* them with task objects, but instead us a weak ref table. Note |
147 |
> |
* them with task objects, but instead use a weak ref table. Note |
148 |
|
* that cancellation exceptions don't appear in the table, but are |
149 |
|
* instead recorded as status values. |
150 |
< |
* Todo: Use ConcurrentReferenceHashMap |
150 |
> |
* TODO: Use ConcurrentReferenceHashMap |
151 |
|
*/ |
152 |
|
static final Map<ForkJoinTask<?>, Throwable> exceptionMap = |
153 |
|
Collections.synchronizedMap |
156 |
|
// within-package utilities |
157 |
|
|
158 |
|
/** |
159 |
< |
* Get current worker thread, or null if not a worker thread |
159 |
> |
* Gets current worker thread, or null if not a worker thread. |
160 |
|
*/ |
161 |
|
static ForkJoinWorkerThread getWorker() { |
162 |
|
Thread t = Thread.currentThread(); |
163 |
< |
return ((t instanceof ForkJoinWorkerThread)? |
164 |
< |
(ForkJoinWorkerThread)t : null); |
163 |
> |
return ((t instanceof ForkJoinWorkerThread) ? |
164 |
> |
(ForkJoinWorkerThread) t : null); |
165 |
|
} |
166 |
|
|
167 |
|
final boolean casStatus(int cmp, int val) { |
168 |
< |
return _unsafe.compareAndSwapInt(this, statusOffset, cmp, val); |
168 |
> |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
169 |
|
} |
170 |
|
|
171 |
|
/** |
173 |
|
*/ |
174 |
|
static void rethrowException(Throwable ex) { |
175 |
|
if (ex != null) |
176 |
< |
_unsafe.throwException(ex); |
176 |
> |
UNSAFE.throwException(ex); |
177 |
|
} |
178 |
|
|
179 |
|
// Setting completion status |
180 |
|
|
181 |
|
/** |
182 |
< |
* Mark completion and wake up threads waiting to join this task. |
182 |
> |
* Marks completion and wakes up threads waiting to join this task. |
183 |
> |
* |
184 |
|
* @param completion one of NORMAL, CANCELLED, EXCEPTIONAL |
185 |
|
*/ |
186 |
|
final void setCompletion(int completion) { |
187 |
|
ForkJoinPool pool = getPool(); |
188 |
|
if (pool != null) { |
189 |
|
int s; // Clear signal bits while setting completion status |
190 |
< |
do;while ((s = status) >= 0 && !casStatus(s, completion)); |
190 |
> |
do {} while ((s = status) >= 0 && !casStatus(s, completion)); |
191 |
|
|
192 |
|
if ((s & SIGNAL_MASK) != 0) { |
193 |
|
if ((s &= INTERNAL_SIGNAL_MASK) != 0) |
194 |
|
pool.updateRunningCount(s); |
195 |
< |
synchronized(this) { notifyAll(); } |
195 |
> |
synchronized (this) { notifyAll(); } |
196 |
|
} |
197 |
|
} |
198 |
|
else |
205 |
|
*/ |
206 |
|
private void externallySetCompletion(int completion) { |
207 |
|
int s; |
208 |
< |
do;while ((s = status) >= 0 && |
209 |
< |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
210 |
< |
synchronized(this) { notifyAll(); } |
208 |
> |
do {} while ((s = status) >= 0 && |
209 |
> |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
210 |
> |
synchronized (this) { notifyAll(); } |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
< |
* Sets status to indicate normal completion |
214 |
> |
* Sets status to indicate normal completion. |
215 |
|
*/ |
216 |
|
final void setNormalCompletion() { |
217 |
|
// Try typical fast case -- single CAS, no signal, not already done. |
218 |
|
// Manually expand casStatus to improve chances of inlining it |
219 |
< |
if (!_unsafe.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
219 |
> |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
220 |
|
setCompletion(NORMAL); |
221 |
|
} |
222 |
|
|
223 |
|
// internal waiting and notification |
224 |
|
|
225 |
|
/** |
226 |
< |
* Performs the actual monitor wait for awaitDone |
226 |
> |
* Performs the actual monitor wait for awaitDone. |
227 |
|
*/ |
228 |
|
private void doAwaitDone() { |
229 |
|
// Minimize lock bias and in/de-flation effects by maximizing |
230 |
|
// chances of waiting inside sync |
231 |
|
try { |
232 |
|
while (status >= 0) |
233 |
< |
synchronized(this) { if (status >= 0) wait(); } |
233 |
> |
synchronized (this) { if (status >= 0) wait(); } |
234 |
|
} catch (InterruptedException ie) { |
235 |
|
onInterruptedWait(); |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* Performs the actual monitor wait for awaitDone |
240 |
> |
* Performs the actual timed monitor wait for awaitDone. |
241 |
|
*/ |
242 |
|
private void doAwaitDone(long startTime, long nanos) { |
243 |
< |
synchronized(this) { |
243 |
> |
synchronized (this) { |
244 |
|
try { |
245 |
|
while (status >= 0) { |
246 |
|
long nt = nanos - System.nanoTime() - startTime; |
247 |
|
if (nt <= 0) |
248 |
|
break; |
249 |
< |
wait(nt / 1000000, (int)(nt % 1000000)); |
249 |
> |
wait(nt / 1000000, (int) (nt % 1000000)); |
250 |
|
} |
251 |
|
} catch (InterruptedException ie) { |
252 |
|
onInterruptedWait(); |
259 |
|
/** |
260 |
|
* Sets status to indicate there is joiner, then waits for join, |
261 |
|
* surrounded with pool notifications. |
262 |
+ |
* |
263 |
|
* @return status upon exit |
264 |
|
*/ |
265 |
< |
private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { |
266 |
< |
ForkJoinPool pool = w == null? null : w.pool; |
265 |
> |
private int awaitDone(ForkJoinWorkerThread w, |
266 |
> |
boolean maintainParallelism) { |
267 |
> |
ForkJoinPool pool = (w == null) ? null : w.pool; |
268 |
|
int s; |
269 |
|
while ((s = status) >= 0) { |
270 |
< |
if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) { |
270 |
> |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
271 |
|
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
272 |
|
doAwaitDone(); |
273 |
|
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
280 |
|
|
281 |
|
/** |
282 |
|
* Timed version of awaitDone |
283 |
+ |
* |
284 |
|
* @return status upon exit |
285 |
|
*/ |
286 |
|
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
287 |
< |
ForkJoinPool pool = w == null? null : w.pool; |
287 |
> |
ForkJoinPool pool = (w == null) ? null : w.pool; |
288 |
|
int s; |
289 |
|
while ((s = status) >= 0) { |
290 |
< |
if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) { |
290 |
> |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
291 |
|
long startTime = System.nanoTime(); |
292 |
|
if (pool == null || !pool.preJoin(this, false)) |
293 |
|
doAwaitDone(startTime, nanos); |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* Notify pool that thread is unblocked. Called by signalled |
307 |
> |
* Notifies pool that thread is unblocked. Called by signalled |
308 |
|
* threads when woken by non-FJ threads (which is atypical). |
309 |
|
*/ |
310 |
|
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
311 |
|
int s; |
312 |
< |
do;while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
312 |
> |
do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
313 |
|
if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0) |
314 |
|
pool.updateRunningCount(s); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* Notify pool to adjust counts on cancelled or timed out wait |
318 |
> |
* Notifies pool to adjust counts on cancelled or timed out wait. |
319 |
|
*/ |
320 |
|
private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) { |
321 |
|
if (pool != null) { |
330 |
|
} |
331 |
|
|
332 |
|
/** |
333 |
< |
* Handle interruptions during waits. |
333 |
> |
* Handles interruptions during waits. |
334 |
|
*/ |
335 |
|
private void onInterruptedWait() { |
336 |
|
ForkJoinWorkerThread w = getWorker(); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
< |
* Throws the exception associated with status s; |
352 |
> |
* Throws the exception associated with status s. |
353 |
> |
* |
354 |
|
* @throws the exception |
355 |
|
*/ |
356 |
|
private void reportException(int s) { |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* Returns result or throws exception using j.u.c.Future conventions |
367 |
< |
* Only call when isDone known to be true. |
366 |
> |
* Returns result or throws exception using j.u.c.Future conventions. |
367 |
> |
* Only call when {@code isDone} known to be true. |
368 |
|
*/ |
369 |
|
private V reportFutureResult() |
370 |
|
throws ExecutionException, InterruptedException { |
383 |
|
|
384 |
|
/** |
385 |
|
* Returns result or throws exception using j.u.c.Future conventions |
386 |
< |
* with timeouts |
386 |
> |
* with timeouts. |
387 |
|
*/ |
388 |
|
private V reportTimedFutureResult() |
389 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
404 |
|
|
405 |
|
/** |
406 |
|
* Calls exec, recording completion, and rethrowing exception if |
407 |
< |
* encountered. Caller should normally check status before calling |
407 |
> |
* encountered. Caller should normally check status before calling. |
408 |
> |
* |
409 |
|
* @return true if completed normally |
410 |
|
*/ |
411 |
|
private boolean tryExec() { |
423 |
|
|
424 |
|
/** |
425 |
|
* Main execution method used by worker threads. Invokes |
426 |
< |
* base computation unless already complete |
426 |
> |
* base computation unless already complete. |
427 |
|
*/ |
428 |
|
final void quietlyExec() { |
429 |
|
if (status >= 0) { |
430 |
|
try { |
431 |
|
if (!exec()) |
432 |
|
return; |
433 |
< |
} catch(Throwable rex) { |
433 |
> |
} catch (Throwable rex) { |
434 |
|
setDoneExceptionally(rex); |
435 |
|
return; |
436 |
|
} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* Calls exec, recording but not rethrowing exception |
443 |
< |
* Caller should normally check status before calling |
442 |
> |
* Calls exec(), recording but not rethrowing exception. |
443 |
> |
* Caller should normally check status before calling. |
444 |
> |
* |
445 |
|
* @return true if completed normally |
446 |
|
*/ |
447 |
|
private boolean tryQuietlyInvoke() { |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* Cancel, ignoring any exceptions it throws |
460 |
> |
* Cancels, ignoring any exceptions it throws. |
461 |
|
*/ |
462 |
|
final void cancelIgnoringExceptions() { |
463 |
|
try { |
464 |
|
cancel(false); |
465 |
< |
} catch(Throwable ignore) { |
465 |
> |
} catch (Throwable ignore) { |
466 |
|
} |
467 |
|
} |
468 |
|
|
474 |
|
ForkJoinTask<?> t; |
475 |
|
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
476 |
|
t.quietlyExec(); |
477 |
< |
return (s >= 0)? awaitDone(w, false) : s; // block if no work |
477 |
> |
return (s >= 0) ? awaitDone(w, false) : s; // block if no work |
478 |
|
} |
479 |
|
|
480 |
|
// public methods |
484 |
|
* necessarily enforced, it is a usage error to fork a task more |
485 |
|
* than once unless it has completed and been reinitialized. This |
486 |
|
* method may be invoked only from within ForkJoinTask |
487 |
< |
* computations. Attempts to invoke in other contexts result in |
488 |
< |
* exceptions or errors possibly including ClassCastException. |
487 |
> |
* computations (as may be determined using method {@link |
488 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
489 |
> |
* in exceptions or errors, possibly including ClassCastException. |
490 |
|
*/ |
491 |
|
public final void fork() { |
492 |
< |
((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this); |
492 |
> |
((ForkJoinWorkerThread) Thread.currentThread()) |
493 |
> |
.pushTask(this); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
511 |
|
/** |
512 |
|
* Commences performing this task, awaits its completion if |
513 |
|
* necessary, and return its result. |
514 |
+ |
* |
515 |
|
* @throws Throwable (a RuntimeException, Error, or unchecked |
516 |
< |
* exception) if the underlying computation did so. |
516 |
> |
* exception) if the underlying computation did so |
517 |
|
* @return the computed result |
518 |
|
*/ |
519 |
|
public final V invoke() { |
526 |
|
/** |
527 |
|
* Forks both tasks, returning when {@code isDone} holds for |
528 |
|
* both of them or an exception is encountered. This method may be |
529 |
< |
* invoked only from within ForkJoinTask computations. Attempts to |
530 |
< |
* invoke in other contexts result in exceptions or errors |
529 |
> |
* invoked only from within ForkJoinTask computations (as may be |
530 |
> |
* determined using method {@link #inForkJoinPool}). Attempts to |
531 |
> |
* invoke in other contexts result in exceptions or errors, |
532 |
|
* possibly including ClassCastException. |
533 |
+ |
* |
534 |
|
* @param t1 one task |
535 |
|
* @param t2 the other task |
536 |
|
* @throws NullPointerException if t1 or t2 are null |
537 |
< |
* @throws RuntimeException or Error if either task did so. |
537 |
> |
* @throws RuntimeException or Error if either task did so |
538 |
|
*/ |
539 |
|
public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) { |
540 |
|
t2.fork(); |
546 |
|
* Forks the given tasks, returning when {@code isDone} holds |
547 |
|
* for all of them. If any task encounters an exception, others |
548 |
|
* may be cancelled. This method may be invoked only from within |
549 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
550 |
< |
* result in exceptions or errors possibly including ClassCastException. |
549 |
> |
* ForkJoinTask computations (as may be determined using method |
550 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
551 |
> |
* result in exceptions or errors, possibly including |
552 |
> |
* ClassCastException. |
553 |
> |
* |
554 |
|
* @param tasks the array of tasks |
555 |
< |
* @throws NullPointerException if tasks or any element are null. |
556 |
< |
* @throws RuntimeException or Error if any task did so. |
555 |
> |
* @throws NullPointerException if tasks or any element are null |
556 |
> |
* @throws RuntimeException or Error if any task did so |
557 |
|
*/ |
558 |
|
public static void invokeAll(ForkJoinTask<?>... tasks) { |
559 |
|
Throwable ex = null; |
592 |
|
* Forks all tasks in the collection, returning when |
593 |
|
* {@code isDone} holds for all of them. If any task |
594 |
|
* encounters an exception, others may be cancelled. This method |
595 |
< |
* may be invoked only from within ForkJoinTask |
596 |
< |
* computations. Attempts to invoke in other contexts result in |
597 |
< |
* exceptions or errors possibly including ClassCastException. |
595 |
> |
* may be invoked only from within ForkJoinTask computations (as |
596 |
> |
* may be determined using method {@link |
597 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
598 |
> |
* in exceptions or errors, possibly including ClassCastException. |
599 |
> |
* |
600 |
|
* @param tasks the collection of tasks |
601 |
< |
* @throws NullPointerException if tasks or any element are null. |
602 |
< |
* @throws RuntimeException or Error if any task did so. |
601 |
> |
* @throws NullPointerException if tasks or any element are null |
602 |
> |
* @throws RuntimeException or Error if any task did so |
603 |
|
*/ |
604 |
|
public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) { |
605 |
< |
if (!(tasks instanceof List)) { |
606 |
< |
invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()])); |
605 |
> |
if (!(tasks instanceof List<?>)) { |
606 |
> |
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
607 |
|
return; |
608 |
|
} |
609 |
+ |
@SuppressWarnings("unchecked") |
610 |
|
List<? extends ForkJoinTask<?>> ts = |
611 |
< |
(List<? extends ForkJoinTask<?>>)tasks; |
611 |
> |
(List<? extends ForkJoinTask<?>>) tasks; |
612 |
|
Throwable ex = null; |
613 |
|
int last = ts.size() - 1; |
614 |
|
for (int i = last; i >= 0; --i) { |
644 |
|
/** |
645 |
|
* Returns true if the computation performed by this task has |
646 |
|
* completed (or has been cancelled). |
647 |
+ |
* |
648 |
|
* @return true if this computation has completed |
649 |
|
*/ |
650 |
|
public final boolean isDone() { |
653 |
|
|
654 |
|
/** |
655 |
|
* Returns true if this task was cancelled. |
656 |
+ |
* |
657 |
|
* @return true if this task was cancelled |
658 |
|
*/ |
659 |
|
public final boolean isCancelled() { |
683 |
|
* |
684 |
|
* @param mayInterruptIfRunning this value is ignored in the |
685 |
|
* default implementation because tasks are not in general |
686 |
< |
* cancelled via interruption. |
686 |
> |
* cancelled via interruption |
687 |
|
* |
688 |
|
* @return true if this task is now cancelled |
689 |
|
*/ |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* Returns true if this task threw an exception or was cancelled |
696 |
> |
* Returns true if this task threw an exception or was cancelled. |
697 |
> |
* |
698 |
|
* @return true if this task threw an exception or was cancelled |
699 |
|
*/ |
700 |
|
public final boolean isCompletedAbnormally() { |
705 |
|
* Returns the exception thrown by the base computation, or a |
706 |
|
* CancellationException if cancelled, or null if none or if the |
707 |
|
* method has not yet completed. |
708 |
+ |
* |
709 |
|
* @return the exception, or null if none |
710 |
|
*/ |
711 |
|
public final Throwable getException() { |
733 |
|
*/ |
734 |
|
public void completeExceptionally(Throwable ex) { |
735 |
|
setDoneExceptionally((ex instanceof RuntimeException) || |
736 |
< |
(ex instanceof Error)? ex : |
736 |
> |
(ex instanceof Error) ? ex : |
737 |
|
new RuntimeException(ex)); |
738 |
|
} |
739 |
|
|
747 |
|
* overridable, but overridden versions must invoke {@code super} |
748 |
|
* implementation to maintain guarantees. |
749 |
|
* |
750 |
< |
* @param value the result value for this task. |
750 |
> |
* @param value the result value for this task |
751 |
|
*/ |
752 |
|
public void complete(V value) { |
753 |
|
try { |
754 |
|
setRawResult(value); |
755 |
< |
} catch(Throwable rex) { |
755 |
> |
} catch (Throwable rex) { |
756 |
|
setDoneExceptionally(rex); |
757 |
|
return; |
758 |
|
} |
782 |
|
* current task and that of any other task that might be executed |
783 |
|
* while helping. (This usually holds for pure divide-and-conquer |
784 |
|
* tasks). This method may be invoked only from within |
785 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
786 |
< |
* result in exceptions or errors possibly including ClassCastException. |
785 |
> |
* ForkJoinTask computations (as may be determined using method |
786 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
787 |
> |
* result in exceptions or errors, possibly including |
788 |
> |
* ClassCastException. |
789 |
> |
* |
790 |
|
* @return the computed result |
791 |
|
*/ |
792 |
|
public final V helpJoin() { |
793 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); |
793 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
794 |
|
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
795 |
|
reportException(busyJoin(w)); |
796 |
|
return getRawResult(); |
799 |
|
/** |
800 |
|
* Possibly executes other tasks until this task is ready. This |
801 |
|
* method may be invoked only from within ForkJoinTask |
802 |
< |
* computations. Attempts to invoke in other contexts result in |
803 |
< |
* exceptions or errors possibly including ClassCastException. |
802 |
> |
* computations (as may be determined using method {@link |
803 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
804 |
> |
* in exceptions or errors, possibly including ClassCastException. |
805 |
|
*/ |
806 |
|
public final void quietlyHelpJoin() { |
807 |
|
if (status >= 0) { |
808 |
|
ForkJoinWorkerThread w = |
809 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
809 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
810 |
|
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
811 |
|
busyJoin(w); |
812 |
|
} |
845 |
|
* joined, instead executing them until all are processed. |
846 |
|
*/ |
847 |
|
public static void helpQuiesce() { |
848 |
< |
((ForkJoinWorkerThread)(Thread.currentThread())). |
849 |
< |
helpQuiescePool(); |
848 |
> |
((ForkJoinWorkerThread) Thread.currentThread()) |
849 |
> |
.helpQuiescePool(); |
850 |
|
} |
851 |
|
|
852 |
|
/** |
867 |
|
|
868 |
|
/** |
869 |
|
* Returns the pool hosting the current task execution, or null |
870 |
< |
* if this task is executing outside of any pool. |
871 |
< |
* @return the pool, or null if none. |
870 |
> |
* if this task is executing outside of any ForkJoinPool. |
871 |
> |
* |
872 |
> |
* @return the pool, or null if none |
873 |
|
*/ |
874 |
|
public static ForkJoinPool getPool() { |
875 |
|
Thread t = Thread.currentThread(); |
876 |
< |
return ((t instanceof ForkJoinWorkerThread)? |
877 |
< |
((ForkJoinWorkerThread)t).pool : null); |
876 |
> |
return (t instanceof ForkJoinWorkerThread) ? |
877 |
> |
((ForkJoinWorkerThread) t).pool : null; |
878 |
> |
} |
879 |
> |
|
880 |
> |
/** |
881 |
> |
* Returns {@code true} if the current thread is executing as a |
882 |
> |
* ForkJoinPool computation. |
883 |
> |
* |
884 |
> |
* @return {@code true} if the current thread is executing as a |
885 |
> |
* ForkJoinPool computation, or false otherwise |
886 |
> |
*/ |
887 |
> |
public static boolean inForkJoinPool() { |
888 |
> |
return Thread.currentThread() instanceof ForkJoinWorkerThread; |
889 |
|
} |
890 |
|
|
891 |
|
/** |
895 |
|
* another thread. This method may be useful when arranging |
896 |
|
* alternative local processing of tasks that could have been, but |
897 |
|
* were not, stolen. This method may be invoked only from within |
898 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
899 |
< |
* result in exceptions or errors possibly including ClassCastException. |
898 |
> |
* ForkJoinTask computations (as may be determined using method |
899 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
900 |
> |
* result in exceptions or errors, possibly including |
901 |
> |
* ClassCastException. |
902 |
> |
* |
903 |
|
* @return true if unforked |
904 |
|
*/ |
905 |
|
public boolean tryUnfork() { |
906 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this); |
906 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
907 |
> |
.unpushTask(this); |
908 |
|
} |
909 |
|
|
910 |
|
/** |
912 |
|
* forked by the current worker thread but not yet executed. This |
913 |
|
* value may be useful for heuristic decisions about whether to |
914 |
|
* fork other tasks. |
915 |
+ |
* |
916 |
|
* @return the number of tasks |
917 |
|
*/ |
918 |
|
public static int getQueuedTaskCount() { |
919 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
920 |
< |
getQueueSize(); |
919 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
920 |
> |
.getQueueSize(); |
921 |
|
} |
922 |
|
|
923 |
|
/** |
924 |
< |
* Returns a estimate of how many more locally queued tasks are |
924 |
> |
* Returns an estimate of how many more locally queued tasks are |
925 |
|
* held by the current worker thread than there are other worker |
926 |
|
* threads that might steal them. This value may be useful for |
927 |
|
* heuristic decisions about whether to fork other tasks. In many |
929 |
|
* aim to maintain a small constant surplus (for example, 3) of |
930 |
|
* tasks, and to process computations locally if this threshold is |
931 |
|
* exceeded. |
932 |
+ |
* |
933 |
|
* @return the surplus number of tasks, which may be negative |
934 |
|
*/ |
935 |
|
public static int getSurplusQueuedTaskCount() { |
936 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())) |
936 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
937 |
|
.getEstimatedSurplusTaskCount(); |
938 |
|
} |
939 |
|
|
946 |
|
* aid debugging, as well as to support extensions. Its use in any |
947 |
|
* other context is discouraged. |
948 |
|
* |
949 |
< |
* @return the result, or null if not completed. |
949 |
> |
* @return the result, or null if not completed |
950 |
|
*/ |
951 |
|
public abstract V getRawResult(); |
952 |
|
|
967 |
|
* asynchronous actions that require explicit invocations of |
968 |
|
* {@code complete} to become joinable. It may throw exceptions |
969 |
|
* to indicate abnormal exit. |
970 |
+ |
* |
971 |
|
* @return true if completed normally |
972 |
|
* @throws Error or RuntimeException if encountered during computation |
973 |
|
*/ |
980 |
|
* be polled or executed next. This method is designed primarily |
981 |
|
* to support extensions, and is unlikely to be useful otherwise. |
982 |
|
* This method may be invoked only from within ForkJoinTask |
983 |
< |
* computations. Attempts to invoke in other contexts result in |
984 |
< |
* exceptions or errors possibly including ClassCastException. |
983 |
> |
* computations (as may be determined using method {@link |
984 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
985 |
> |
* in exceptions or errors, possibly including ClassCastException. |
986 |
|
* |
987 |
|
* @return the next task, or null if none are available |
988 |
|
*/ |
989 |
|
protected static ForkJoinTask<?> peekNextLocalTask() { |
990 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask(); |
990 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
991 |
> |
.peekTask(); |
992 |
|
} |
993 |
|
|
994 |
|
/** |
996 |
|
* queued by the current thread but not yet executed. This method |
997 |
|
* is designed primarily to support extensions, and is unlikely to |
998 |
|
* be useful otherwise. This method may be invoked only from |
999 |
< |
* within ForkJoinTask computations. Attempts to invoke in other |
1000 |
< |
* contexts result in exceptions or errors possibly including |
999 |
> |
* within ForkJoinTask computations (as may be determined using |
1000 |
> |
* method {@link #inForkJoinPool}). Attempts to invoke in other |
1001 |
> |
* contexts result in exceptions or errors, possibly including |
1002 |
|
* ClassCastException. |
1003 |
|
* |
1004 |
|
* @return the next task, or null if none are available |
1005 |
|
*/ |
1006 |
|
protected static ForkJoinTask<?> pollNextLocalTask() { |
1007 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask(); |
1007 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1008 |
> |
.pollLocalTask(); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1017 |
|
* of the pool this task is operating in. This method is designed |
1018 |
|
* primarily to support extensions, and is unlikely to be useful |
1019 |
|
* otherwise. This method may be invoked only from within |
1020 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
1021 |
< |
* result in exceptions or errors possibly including |
1020 |
> |
* ForkJoinTask computations (as may be determined using method |
1021 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1022 |
> |
* result in exceptions or errors, possibly including |
1023 |
|
* ClassCastException. |
1024 |
|
* |
1025 |
|
* @return a task, or null if none are available |
1026 |
|
*/ |
1027 |
|
protected static ForkJoinTask<?> pollTask() { |
1028 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
1029 |
< |
pollTask(); |
1028 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1029 |
> |
.pollTask(); |
1030 |
|
} |
1031 |
|
|
1032 |
|
// Serialization support |
1037 |
|
* Save the state to a stream. |
1038 |
|
* |
1039 |
|
* @serialData the current run status and the exception thrown |
1040 |
< |
* during execution, or null if none. |
1040 |
> |
* during execution, or null if none |
1041 |
|
* @param s the stream |
1042 |
|
*/ |
1043 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1048 |
|
|
1049 |
|
/** |
1050 |
|
* Reconstitute the instance from a stream. |
1051 |
+ |
* |
1052 |
|
* @param s the stream |
1053 |
|
*/ |
1054 |
|
private void readObject(java.io.ObjectInputStream s) |
1058 |
|
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1059 |
|
Object ex = s.readObject(); |
1060 |
|
if (ex != null) |
1061 |
< |
setDoneExceptionally((Throwable)ex); |
1061 |
> |
setDoneExceptionally((Throwable) ex); |
1062 |
|
} |
1063 |
|
|
1064 |
< |
// Temporary Unsafe mechanics for preliminary release |
1065 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1064 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1065 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1066 |
|
try { |
1067 |
< |
return Unsafe.getUnsafe(); |
1067 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1068 |
|
} catch (SecurityException se) { |
1069 |
|
try { |
1070 |
|
return java.security.AccessController.doPrivileged |
1071 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1072 |
< |
public Unsafe run() throws Exception { |
1073 |
< |
return getUnsafePrivileged(); |
1071 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1072 |
> |
public sun.misc.Unsafe run() throws Exception { |
1073 |
> |
return getUnsafeByReflection(); |
1074 |
|
}}); |
1075 |
|
} catch (java.security.PrivilegedActionException e) { |
1076 |
< |
throw e.getCause(); |
1076 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1077 |
> |
e.getCause()); |
1078 |
|
} |
1079 |
|
} |
1080 |
|
} |
1081 |
|
|
1082 |
< |
private static Unsafe getUnsafePrivileged() |
1082 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1083 |
|
throws NoSuchFieldException, IllegalAccessException { |
1084 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1084 |
> |
java.lang.reflect.Field f = |
1085 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1086 |
|
f.setAccessible(true); |
1087 |
< |
return (Unsafe) f.get(null); |
1087 |
> |
return (sun.misc.Unsafe) f.get(null); |
1088 |
|
} |
1089 |
|
|
1090 |
< |
private static long fieldOffset(String fieldName) |
1035 |
< |
throws NoSuchFieldException { |
1036 |
< |
return _unsafe.objectFieldOffset |
1037 |
< |
(ForkJoinTask.class.getDeclaredField(fieldName)); |
1038 |
< |
} |
1039 |
< |
|
1040 |
< |
static final Unsafe _unsafe; |
1041 |
< |
static final long statusOffset; |
1042 |
< |
|
1043 |
< |
static { |
1090 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1091 |
|
try { |
1092 |
< |
_unsafe = getUnsafe(); |
1093 |
< |
statusOffset = fieldOffset("status"); |
1094 |
< |
} catch (Throwable e) { |
1095 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1092 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1093 |
> |
} catch (NoSuchFieldException e) { |
1094 |
> |
// Convert Exception to Error |
1095 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1096 |
> |
error.initCause(e); |
1097 |
> |
throw error; |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1101 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1102 |
+ |
static final long statusOffset = |
1103 |
+ |
fieldOffset("status", ForkJoinTask.class); |
1104 |
+ |
|
1105 |
|
} |