5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import java.io.Serializable; |
9 |
< |
import java.util.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import sun.misc.Unsafe; |
12 |
< |
import java.lang.reflect.*; |
10 |
> |
|
11 |
> |
import java.io.Serializable; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Collections; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.Map; |
16 |
> |
import java.util.WeakHashMap; |
17 |
|
|
18 |
|
/** |
19 |
|
* Abstract base class for tasks that run within a {@link |
84 |
|
* class. While these methods have {@code public} access (to allow |
85 |
|
* instances of different task subclasses to call each others |
86 |
|
* methods), some of them may only be called from within other |
87 |
< |
* ForkJoinTasks. Attempts to invoke them in other contexts result in |
88 |
< |
* exceptions or errors possibly including ClassCastException. |
87 |
> |
* ForkJoinTasks (as may be determined using method {@link |
88 |
> |
* #inForkJoinPool}). Attempts to invoke them in other contexts |
89 |
> |
* result in exceptions or errors, possibly including |
90 |
> |
* ClassCastException. |
91 |
|
* |
92 |
|
* <p>Most base support methods are {@code final} because their |
93 |
|
* implementations are intrinsically tied to the underlying |
112 |
|
* in general sensible to serialize tasks only before or after, but |
113 |
|
* not during execution. Serialization is not relied on during |
114 |
|
* execution itself. |
115 |
+ |
* |
116 |
+ |
* @since 1.7 |
117 |
+ |
* @author Doug Lea |
118 |
|
*/ |
119 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
120 |
|
|
165 |
|
*/ |
166 |
|
static ForkJoinWorkerThread getWorker() { |
167 |
|
Thread t = Thread.currentThread(); |
168 |
< |
return ((t instanceof ForkJoinWorkerThread)? |
169 |
< |
(ForkJoinWorkerThread)t : null); |
168 |
> |
return ((t instanceof ForkJoinWorkerThread) ? |
169 |
> |
(ForkJoinWorkerThread) t : null); |
170 |
|
} |
171 |
|
|
172 |
|
final boolean casStatus(int cmp, int val) { |
192 |
|
ForkJoinPool pool = getPool(); |
193 |
|
if (pool != null) { |
194 |
|
int s; // Clear signal bits while setting completion status |
195 |
< |
do;while ((s = status) >= 0 && !casStatus(s, completion)); |
195 |
> |
do {} while ((s = status) >= 0 && !casStatus(s, completion)); |
196 |
|
|
197 |
|
if ((s & SIGNAL_MASK) != 0) { |
198 |
|
if ((s &= INTERNAL_SIGNAL_MASK) != 0) |
199 |
|
pool.updateRunningCount(s); |
200 |
< |
synchronized(this) { notifyAll(); } |
200 |
> |
synchronized (this) { notifyAll(); } |
201 |
|
} |
202 |
|
} |
203 |
|
else |
210 |
|
*/ |
211 |
|
private void externallySetCompletion(int completion) { |
212 |
|
int s; |
213 |
< |
do;while ((s = status) >= 0 && |
214 |
< |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
215 |
< |
synchronized(this) { notifyAll(); } |
213 |
> |
do {} while ((s = status) >= 0 && |
214 |
> |
!casStatus(s, (s & SIGNAL_MASK) | completion)); |
215 |
> |
synchronized (this) { notifyAll(); } |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* Sets status to indicate normal completion |
219 |
> |
* Sets status to indicate normal completion. |
220 |
|
*/ |
221 |
|
final void setNormalCompletion() { |
222 |
|
// Try typical fast case -- single CAS, no signal, not already done. |
228 |
|
// internal waiting and notification |
229 |
|
|
230 |
|
/** |
231 |
< |
* Performs the actual monitor wait for awaitDone |
231 |
> |
* Performs the actual monitor wait for awaitDone. |
232 |
|
*/ |
233 |
|
private void doAwaitDone() { |
234 |
|
// Minimize lock bias and in/de-flation effects by maximizing |
235 |
|
// chances of waiting inside sync |
236 |
|
try { |
237 |
|
while (status >= 0) |
238 |
< |
synchronized(this) { if (status >= 0) wait(); } |
238 |
> |
synchronized (this) { if (status >= 0) wait(); } |
239 |
|
} catch (InterruptedException ie) { |
240 |
|
onInterruptedWait(); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* Performs the actual monitor wait for awaitDone |
245 |
> |
* Performs the actual timed monitor wait for awaitDone. |
246 |
|
*/ |
247 |
|
private void doAwaitDone(long startTime, long nanos) { |
248 |
< |
synchronized(this) { |
248 |
> |
synchronized (this) { |
249 |
|
try { |
250 |
|
while (status >= 0) { |
251 |
|
long nt = nanos - System.nanoTime() - startTime; |
252 |
|
if (nt <= 0) |
253 |
|
break; |
254 |
< |
wait(nt / 1000000, (int)(nt % 1000000)); |
254 |
> |
wait(nt / 1000000, (int) (nt % 1000000)); |
255 |
|
} |
256 |
|
} catch (InterruptedException ie) { |
257 |
|
onInterruptedWait(); |
267 |
|
* |
268 |
|
* @return status upon exit |
269 |
|
*/ |
270 |
< |
private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) { |
271 |
< |
ForkJoinPool pool = w == null? null : w.pool; |
270 |
> |
private int awaitDone(ForkJoinWorkerThread w, |
271 |
> |
boolean maintainParallelism) { |
272 |
> |
ForkJoinPool pool = (w == null) ? null : w.pool; |
273 |
|
int s; |
274 |
|
while ((s = status) >= 0) { |
275 |
< |
if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) { |
275 |
> |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
276 |
|
if (pool == null || !pool.preJoin(this, maintainParallelism)) |
277 |
|
doAwaitDone(); |
278 |
|
if (((s = status) & INTERNAL_SIGNAL_MASK) != 0) |
285 |
|
|
286 |
|
/** |
287 |
|
* Timed version of awaitDone |
288 |
+ |
* |
289 |
|
* @return status upon exit |
290 |
|
*/ |
291 |
|
private int awaitDone(ForkJoinWorkerThread w, long nanos) { |
292 |
< |
ForkJoinPool pool = w == null? null : w.pool; |
292 |
> |
ForkJoinPool pool = (w == null) ? null : w.pool; |
293 |
|
int s; |
294 |
|
while ((s = status) >= 0) { |
295 |
< |
if (casStatus(s, pool == null? s|EXTERNAL_SIGNAL : s+1)) { |
295 |
> |
if (casStatus(s, (pool == null) ? s|EXTERNAL_SIGNAL : s+1)) { |
296 |
|
long startTime = System.nanoTime(); |
297 |
|
if (pool == null || !pool.preJoin(this, false)) |
298 |
|
doAwaitDone(startTime, nanos); |
314 |
|
*/ |
315 |
|
private void adjustPoolCountsOnUnblock(ForkJoinPool pool) { |
316 |
|
int s; |
317 |
< |
do;while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
317 |
> |
do {} while ((s = status) < 0 && !casStatus(s, s & COMPLETION_MASK)); |
318 |
|
if (pool != null && (s &= INTERNAL_SIGNAL_MASK) != 0) |
319 |
|
pool.updateRunningCount(s); |
320 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
|
* Returns result or throws exception using j.u.c.Future conventions. |
372 |
< |
* Only call when isDone known to be true. |
372 |
> |
* Only call when {@code isDone} known to be true. |
373 |
|
*/ |
374 |
|
private V reportFutureResult() |
375 |
|
throws ExecutionException, InterruptedException { |
435 |
|
try { |
436 |
|
if (!exec()) |
437 |
|
return; |
438 |
< |
} catch(Throwable rex) { |
438 |
> |
} catch (Throwable rex) { |
439 |
|
setDoneExceptionally(rex); |
440 |
|
return; |
441 |
|
} |
467 |
|
final void cancelIgnoringExceptions() { |
468 |
|
try { |
469 |
|
cancel(false); |
470 |
< |
} catch(Throwable ignore) { |
470 |
> |
} catch (Throwable ignore) { |
471 |
|
} |
472 |
|
} |
473 |
|
|
479 |
|
ForkJoinTask<?> t; |
480 |
|
while ((s = status) >= 0 && (t = w.scanWhileJoining(this)) != null) |
481 |
|
t.quietlyExec(); |
482 |
< |
return (s >= 0)? awaitDone(w, false) : s; // block if no work |
482 |
> |
return (s >= 0) ? awaitDone(w, false) : s; // block if no work |
483 |
|
} |
484 |
|
|
485 |
|
// public methods |
489 |
|
* necessarily enforced, it is a usage error to fork a task more |
490 |
|
* than once unless it has completed and been reinitialized. This |
491 |
|
* method may be invoked only from within ForkJoinTask |
492 |
< |
* computations. Attempts to invoke in other contexts result in |
493 |
< |
* exceptions or errors possibly including ClassCastException. |
494 |
< |
*/ |
495 |
< |
public final void fork() { |
496 |
< |
((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this); |
492 |
> |
* computations (as may be determined using method {@link |
493 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
494 |
> |
* in exceptions or errors, possibly including ClassCastException. |
495 |
> |
* |
496 |
> |
* @return <code>this</code>, to simplify usage. |
497 |
> |
*/ |
498 |
> |
public final ForkJoinTask<V> fork() { |
499 |
> |
((ForkJoinWorkerThread) Thread.currentThread()) |
500 |
> |
.pushTask(this); |
501 |
> |
return this; |
502 |
|
} |
503 |
|
|
504 |
|
/** |
534 |
|
/** |
535 |
|
* Forks both tasks, returning when {@code isDone} holds for |
536 |
|
* both of them or an exception is encountered. This method may be |
537 |
< |
* invoked only from within ForkJoinTask computations. Attempts to |
538 |
< |
* invoke in other contexts result in exceptions or errors |
537 |
> |
* invoked only from within ForkJoinTask computations (as may be |
538 |
> |
* determined using method {@link #inForkJoinPool}). Attempts to |
539 |
> |
* invoke in other contexts result in exceptions or errors, |
540 |
|
* possibly including ClassCastException. |
541 |
|
* |
542 |
|
* @param t1 one task |
554 |
|
* Forks the given tasks, returning when {@code isDone} holds |
555 |
|
* for all of them. If any task encounters an exception, others |
556 |
|
* may be cancelled. This method may be invoked only from within |
557 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
558 |
< |
* result in exceptions or errors possibly including ClassCastException. |
557 |
> |
* ForkJoinTask computations (as may be determined using method |
558 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
559 |
> |
* result in exceptions or errors, possibly including |
560 |
> |
* ClassCastException. |
561 |
|
* |
562 |
|
* @param tasks the array of tasks |
563 |
|
* @throws NullPointerException if tasks or any element are null |
600 |
|
* Forks all tasks in the collection, returning when |
601 |
|
* {@code isDone} holds for all of them. If any task |
602 |
|
* encounters an exception, others may be cancelled. This method |
603 |
< |
* may be invoked only from within ForkJoinTask |
604 |
< |
* computations. Attempts to invoke in other contexts result in |
605 |
< |
* exceptions or errors possibly including ClassCastException. |
603 |
> |
* may be invoked only from within ForkJoinTask computations (as |
604 |
> |
* may be determined using method {@link |
605 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
606 |
> |
* in exceptions or errors, possibly including ClassCastException. |
607 |
|
* |
608 |
|
* @param tasks the collection of tasks |
609 |
+ |
* @return the tasks argument, to simplify usage |
610 |
|
* @throws NullPointerException if tasks or any element are null |
611 |
|
* @throws RuntimeException or Error if any task did so |
612 |
|
*/ |
613 |
< |
public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) { |
614 |
< |
if (!(tasks instanceof List)) { |
615 |
< |
invokeAll(tasks.toArray(new ForkJoinTask[tasks.size()])); |
616 |
< |
return; |
613 |
> |
public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) { |
614 |
> |
if (!(tasks instanceof List<?>)) { |
615 |
> |
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
616 |
> |
return tasks; |
617 |
|
} |
618 |
+ |
@SuppressWarnings("unchecked") |
619 |
|
List<? extends ForkJoinTask<?>> ts = |
620 |
< |
(List<? extends ForkJoinTask<?>>)tasks; |
620 |
> |
(List<? extends ForkJoinTask<?>>) tasks; |
621 |
|
Throwable ex = null; |
622 |
|
int last = ts.size() - 1; |
623 |
|
for (int i = last; i >= 0; --i) { |
648 |
|
} |
649 |
|
if (ex != null) |
650 |
|
rethrowException(ex); |
651 |
+ |
return tasks; |
652 |
|
} |
653 |
|
|
654 |
|
/** |
693 |
|
* |
694 |
|
* @param mayInterruptIfRunning this value is ignored in the |
695 |
|
* default implementation because tasks are not in general |
696 |
< |
* cancelled via interruption. |
696 |
> |
* cancelled via interruption |
697 |
|
* |
698 |
|
* @return true if this task is now cancelled |
699 |
|
*/ |
743 |
|
*/ |
744 |
|
public void completeExceptionally(Throwable ex) { |
745 |
|
setDoneExceptionally((ex instanceof RuntimeException) || |
746 |
< |
(ex instanceof Error)? ex : |
746 |
> |
(ex instanceof Error) ? ex : |
747 |
|
new RuntimeException(ex)); |
748 |
|
} |
749 |
|
|
762 |
|
public void complete(V value) { |
763 |
|
try { |
764 |
|
setRawResult(value); |
765 |
< |
} catch(Throwable rex) { |
765 |
> |
} catch (Throwable rex) { |
766 |
|
setDoneExceptionally(rex); |
767 |
|
return; |
768 |
|
} |
792 |
|
* current task and that of any other task that might be executed |
793 |
|
* while helping. (This usually holds for pure divide-and-conquer |
794 |
|
* tasks). This method may be invoked only from within |
795 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
796 |
< |
* result in exceptions or errors possibly including ClassCastException. |
795 |
> |
* ForkJoinTask computations (as may be determined using method |
796 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
797 |
> |
* result in exceptions or errors, possibly including |
798 |
> |
* ClassCastException. |
799 |
|
* |
800 |
|
* @return the computed result |
801 |
|
*/ |
802 |
|
public final V helpJoin() { |
803 |
< |
ForkJoinWorkerThread w = (ForkJoinWorkerThread)(Thread.currentThread()); |
803 |
> |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); |
804 |
|
if (status < 0 || !w.unpushTask(this) || !tryExec()) |
805 |
|
reportException(busyJoin(w)); |
806 |
|
return getRawResult(); |
809 |
|
/** |
810 |
|
* Possibly executes other tasks until this task is ready. This |
811 |
|
* method may be invoked only from within ForkJoinTask |
812 |
< |
* computations. Attempts to invoke in other contexts result in |
813 |
< |
* exceptions or errors possibly including ClassCastException. |
812 |
> |
* computations (as may be determined using method {@link |
813 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
814 |
> |
* in exceptions or errors, possibly including ClassCastException. |
815 |
|
*/ |
816 |
|
public final void quietlyHelpJoin() { |
817 |
|
if (status >= 0) { |
818 |
|
ForkJoinWorkerThread w = |
819 |
< |
(ForkJoinWorkerThread)(Thread.currentThread()); |
819 |
> |
(ForkJoinWorkerThread) Thread.currentThread(); |
820 |
|
if (!w.unpushTask(this) || !tryQuietlyInvoke()) |
821 |
|
busyJoin(w); |
822 |
|
} |
855 |
|
* joined, instead executing them until all are processed. |
856 |
|
*/ |
857 |
|
public static void helpQuiesce() { |
858 |
< |
((ForkJoinWorkerThread)(Thread.currentThread())). |
859 |
< |
helpQuiescePool(); |
858 |
> |
((ForkJoinWorkerThread) Thread.currentThread()) |
859 |
> |
.helpQuiescePool(); |
860 |
|
} |
861 |
|
|
862 |
|
/** |
877 |
|
|
878 |
|
/** |
879 |
|
* Returns the pool hosting the current task execution, or null |
880 |
< |
* if this task is executing outside of any pool. |
880 |
> |
* if this task is executing outside of any ForkJoinPool. |
881 |
|
* |
882 |
|
* @return the pool, or null if none |
883 |
|
*/ |
884 |
|
public static ForkJoinPool getPool() { |
885 |
|
Thread t = Thread.currentThread(); |
886 |
< |
return ((t instanceof ForkJoinWorkerThread)? |
887 |
< |
((ForkJoinWorkerThread)t).pool : null); |
886 |
> |
return (t instanceof ForkJoinWorkerThread) ? |
887 |
> |
((ForkJoinWorkerThread) t).pool : null; |
888 |
> |
} |
889 |
> |
|
890 |
> |
/** |
891 |
> |
* Returns {@code true} if the current thread is executing as a |
892 |
> |
* ForkJoinPool computation. |
893 |
> |
* |
894 |
> |
* @return {@code true} if the current thread is executing as a |
895 |
> |
* ForkJoinPool computation, or false otherwise |
896 |
> |
*/ |
897 |
> |
public static boolean inForkJoinPool() { |
898 |
> |
return Thread.currentThread() instanceof ForkJoinWorkerThread; |
899 |
|
} |
900 |
|
|
901 |
|
/** |
905 |
|
* another thread. This method may be useful when arranging |
906 |
|
* alternative local processing of tasks that could have been, but |
907 |
|
* were not, stolen. This method may be invoked only from within |
908 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
909 |
< |
* result in exceptions or errors possibly including ClassCastException. |
908 |
> |
* ForkJoinTask computations (as may be determined using method |
909 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
910 |
> |
* result in exceptions or errors, possibly including |
911 |
> |
* ClassCastException. |
912 |
|
* |
913 |
|
* @return true if unforked |
914 |
|
*/ |
915 |
|
public boolean tryUnfork() { |
916 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).unpushTask(this); |
916 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
917 |
> |
.unpushTask(this); |
918 |
|
} |
919 |
|
|
920 |
|
/** |
926 |
|
* @return the number of tasks |
927 |
|
*/ |
928 |
|
public static int getQueuedTaskCount() { |
929 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
930 |
< |
getQueueSize(); |
929 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
930 |
> |
.getQueueSize(); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
943 |
|
* @return the surplus number of tasks, which may be negative |
944 |
|
*/ |
945 |
|
public static int getSurplusQueuedTaskCount() { |
946 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())) |
946 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
947 |
|
.getEstimatedSurplusTaskCount(); |
948 |
|
} |
949 |
|
|
990 |
|
* be polled or executed next. This method is designed primarily |
991 |
|
* to support extensions, and is unlikely to be useful otherwise. |
992 |
|
* This method may be invoked only from within ForkJoinTask |
993 |
< |
* computations. Attempts to invoke in other contexts result in |
994 |
< |
* exceptions or errors possibly including ClassCastException. |
993 |
> |
* computations (as may be determined using method {@link |
994 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
995 |
> |
* in exceptions or errors, possibly including ClassCastException. |
996 |
|
* |
997 |
|
* @return the next task, or null if none are available |
998 |
|
*/ |
999 |
|
protected static ForkJoinTask<?> peekNextLocalTask() { |
1000 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask(); |
1000 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1001 |
> |
.peekTask(); |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1006 |
|
* queued by the current thread but not yet executed. This method |
1007 |
|
* is designed primarily to support extensions, and is unlikely to |
1008 |
|
* be useful otherwise. This method may be invoked only from |
1009 |
< |
* within ForkJoinTask computations. Attempts to invoke in other |
1010 |
< |
* contexts result in exceptions or errors possibly including |
1009 |
> |
* within ForkJoinTask computations (as may be determined using |
1010 |
> |
* method {@link #inForkJoinPool}). Attempts to invoke in other |
1011 |
> |
* contexts result in exceptions or errors, possibly including |
1012 |
|
* ClassCastException. |
1013 |
|
* |
1014 |
|
* @return the next task, or null if none are available |
1015 |
|
*/ |
1016 |
|
protected static ForkJoinTask<?> pollNextLocalTask() { |
1017 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask(); |
1017 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1018 |
> |
.pollLocalTask(); |
1019 |
|
} |
1020 |
|
|
1021 |
|
/** |
1027 |
|
* of the pool this task is operating in. This method is designed |
1028 |
|
* primarily to support extensions, and is unlikely to be useful |
1029 |
|
* otherwise. This method may be invoked only from within |
1030 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
1031 |
< |
* result in exceptions or errors possibly including |
1030 |
> |
* ForkJoinTask computations (as may be determined using method |
1031 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1032 |
> |
* result in exceptions or errors, possibly including |
1033 |
|
* ClassCastException. |
1034 |
|
* |
1035 |
|
* @return a task, or null if none are available |
1036 |
|
*/ |
1037 |
|
protected static ForkJoinTask<?> pollTask() { |
1038 |
< |
return ((ForkJoinWorkerThread)(Thread.currentThread())). |
1039 |
< |
pollTask(); |
1038 |
> |
return ((ForkJoinWorkerThread) Thread.currentThread()) |
1039 |
> |
.pollTask(); |
1040 |
> |
} |
1041 |
> |
|
1042 |
> |
// adaptors |
1043 |
> |
|
1044 |
> |
/** |
1045 |
> |
* Returns a new ForkJoinTask that performs the <code>run</code> |
1046 |
> |
* method of the given Runnable as its action, and returns a null |
1047 |
> |
* result upon <code>join</code>. |
1048 |
> |
* |
1049 |
> |
* @param runnable the runnable action |
1050 |
> |
* @return the task |
1051 |
> |
*/ |
1052 |
> |
public static ForkJoinTask<Void> adapt(Runnable runnable) { |
1053 |
> |
return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null); |
1054 |
> |
} |
1055 |
> |
|
1056 |
> |
/** |
1057 |
> |
* Returns a new ForkJoinTask that performs the <code>run</code> |
1058 |
> |
* method of the given Runnable as its action, and returns the |
1059 |
> |
* given result upon <code>join</code>. |
1060 |
> |
* |
1061 |
> |
* @param runnable the runnable action |
1062 |
> |
* @param result the result upon completion |
1063 |
> |
* @return the task |
1064 |
> |
*/ |
1065 |
> |
public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) { |
1066 |
> |
return new ForkJoinPool.AdaptedRunnable<T>(runnable, result); |
1067 |
> |
} |
1068 |
> |
|
1069 |
> |
/** |
1070 |
> |
* Returns a new ForkJoinTask that performs the <code>call</code> |
1071 |
> |
* method of the given Callable as its action, and returns its |
1072 |
> |
* result upon <code>join</code>, translating any checked |
1073 |
> |
* exceptions encountered into <code>RuntimeException<code>. |
1074 |
> |
* |
1075 |
> |
* @param callable the callable action |
1076 |
> |
* @return the task |
1077 |
> |
*/ |
1078 |
> |
public static <T> ForkJoinTask<T> adapt(Callable<T> callable) { |
1079 |
> |
return new ForkJoinPool.AdaptedCallable<T>(callable); |
1080 |
|
} |
1081 |
|
|
1082 |
|
// Serialization support |
1108 |
|
status |= EXTERNAL_SIGNAL; // conservatively set external signal |
1109 |
|
Object ex = s.readObject(); |
1110 |
|
if (ex != null) |
1111 |
< |
setDoneExceptionally((Throwable)ex); |
1111 |
> |
setDoneExceptionally((Throwable) ex); |
1112 |
|
} |
1113 |
|
|
1114 |
< |
// Temporary Unsafe mechanics for preliminary release |
1115 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1114 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1115 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1116 |
|
try { |
1117 |
< |
return Unsafe.getUnsafe(); |
1117 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1118 |
|
} catch (SecurityException se) { |
1119 |
|
try { |
1120 |
|
return java.security.AccessController.doPrivileged |
1121 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1122 |
< |
public Unsafe run() throws Exception { |
1123 |
< |
return getUnsafePrivileged(); |
1121 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1122 |
> |
public sun.misc.Unsafe run() throws Exception { |
1123 |
> |
return getUnsafeByReflection(); |
1124 |
|
}}); |
1125 |
|
} catch (java.security.PrivilegedActionException e) { |
1126 |
< |
throw e.getCause(); |
1126 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1127 |
> |
e.getCause()); |
1128 |
|
} |
1129 |
|
} |
1130 |
|
} |
1131 |
|
|
1132 |
< |
private static Unsafe getUnsafePrivileged() |
1132 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1133 |
|
throws NoSuchFieldException, IllegalAccessException { |
1134 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1134 |
> |
java.lang.reflect.Field f = |
1135 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1136 |
|
f.setAccessible(true); |
1137 |
< |
return (Unsafe) f.get(null); |
1137 |
> |
return (sun.misc.Unsafe) f.get(null); |
1138 |
|
} |
1139 |
|
|
1140 |
< |
private static long fieldOffset(String fieldName) |
1055 |
< |
throws NoSuchFieldException { |
1056 |
< |
return UNSAFE.objectFieldOffset |
1057 |
< |
(ForkJoinTask.class.getDeclaredField(fieldName)); |
1058 |
< |
} |
1059 |
< |
|
1060 |
< |
static final Unsafe UNSAFE; |
1061 |
< |
static final long statusOffset; |
1062 |
< |
|
1063 |
< |
static { |
1140 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1141 |
|
try { |
1142 |
< |
UNSAFE = getUnsafe(); |
1143 |
< |
statusOffset = fieldOffset("status"); |
1144 |
< |
} catch (Throwable e) { |
1145 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1142 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1143 |
> |
} catch (NoSuchFieldException e) { |
1144 |
> |
// Convert Exception to Error |
1145 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1146 |
> |
error.initCause(e); |
1147 |
> |
throw error; |
1148 |
|
} |
1149 |
|
} |
1150 |
|
|
1151 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1152 |
+ |
static final long statusOffset = |
1153 |
+ |
fieldOffset("status", ForkJoinTask.class); |
1154 |
+ |
|
1155 |
|
} |