81 |
|
* class. While these methods have {@code public} access (to allow |
82 |
|
* instances of different task subclasses to call each others |
83 |
|
* methods), some of them may only be called from within other |
84 |
< |
* ForkJoinTasks. Attempts to invoke them in other contexts result in |
85 |
< |
* exceptions or errors possibly including ClassCastException. |
84 |
> |
* ForkJoinTasks (as may be determined using method {@link |
85 |
> |
* #inForkJoinPool}). Attempts to invoke them in other contexts |
86 |
> |
* result in exceptions or errors possibly including |
87 |
> |
* ClassCastException. |
88 |
|
* |
89 |
|
* <p>Most base support methods are {@code final} because their |
90 |
|
* implementations are intrinsically tied to the underlying |
109 |
|
* in general sensible to serialize tasks only before or after, but |
110 |
|
* not during execution. Serialization is not relied on during |
111 |
|
* execution itself. |
112 |
+ |
* |
113 |
+ |
* @since 1.7 |
114 |
+ |
* @author Doug Lea |
115 |
|
*/ |
116 |
|
public abstract class ForkJoinTask<V> implements Future<V>, Serializable { |
117 |
|
|
167 |
|
} |
168 |
|
|
169 |
|
final boolean casStatus(int cmp, int val) { |
170 |
< |
return _unsafe.compareAndSwapInt(this, statusOffset, cmp, val); |
170 |
> |
return UNSAFE.compareAndSwapInt(this, statusOffset, cmp, val); |
171 |
|
} |
172 |
|
|
173 |
|
/** |
175 |
|
*/ |
176 |
|
static void rethrowException(Throwable ex) { |
177 |
|
if (ex != null) |
178 |
< |
_unsafe.throwException(ex); |
178 |
> |
UNSAFE.throwException(ex); |
179 |
|
} |
180 |
|
|
181 |
|
// Setting completion status |
218 |
|
final void setNormalCompletion() { |
219 |
|
// Try typical fast case -- single CAS, no signal, not already done. |
220 |
|
// Manually expand casStatus to improve chances of inlining it |
221 |
< |
if (!_unsafe.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
221 |
> |
if (!UNSAFE.compareAndSwapInt(this, statusOffset, 0, NORMAL)) |
222 |
|
setCompletion(NORMAL); |
223 |
|
} |
224 |
|
|
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); |
525 |
|
/** |
526 |
|
* Forks both tasks, returning when {@code isDone} holds for |
527 |
|
* both of them or an exception is encountered. This method may be |
528 |
< |
* invoked only from within ForkJoinTask computations. Attempts to |
528 |
> |
* invoked only from within ForkJoinTask computations (as may be |
529 |
> |
* determined using method {@link #inForkJoinPool}). Attempts to |
530 |
|
* invoke in other contexts result in exceptions or errors |
531 |
|
* possibly including ClassCastException. |
532 |
|
* |
545 |
|
* Forks the given tasks, returning when {@code isDone} holds |
546 |
|
* for all of them. If any task encounters an exception, others |
547 |
|
* may be cancelled. This method may be invoked only from within |
548 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
549 |
< |
* result in exceptions or errors possibly including ClassCastException. |
550 |
< |
* |
548 |
> |
* ForkJoinTask computations (as may be determined using method |
549 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
550 |
> |
* result in exceptions or errors possibly including |
551 |
> |
* ClassCastException. |
552 |
|
* @param tasks the array of tasks |
553 |
|
* @throws NullPointerException if tasks or any element are null |
554 |
|
* @throws RuntimeException or Error if any task did so |
590 |
|
* Forks all tasks in the collection, returning when |
591 |
|
* {@code isDone} holds for all of them. If any task |
592 |
|
* encounters an exception, others may be cancelled. This method |
593 |
< |
* may be invoked only from within ForkJoinTask |
594 |
< |
* computations. Attempts to invoke in other contexts result in |
595 |
< |
* exceptions or errors possibly including ClassCastException. |
593 |
> |
* may be invoked only from within ForkJoinTask computations (as |
594 |
> |
* may be determined using method {@link |
595 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts resul!t |
596 |
> |
* in exceptions or errors possibly including ClassCastException. |
597 |
|
* |
598 |
|
* @param tasks the collection of tasks |
599 |
|
* @throws NullPointerException if tasks or any element are null |
779 |
|
* current task and that of any other task that might be executed |
780 |
|
* while helping. (This usually holds for pure divide-and-conquer |
781 |
|
* tasks). This method may be invoked only from within |
782 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
783 |
< |
* result in exceptions or errors possibly including ClassCastException. |
782 |
> |
* ForkJoinTask computations (as may be determined using method |
783 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
784 |
> |
* resul!t in exceptions or errors possibly including |
785 |
> |
* ClassCastException. |
786 |
|
* |
787 |
|
* @return the computed result |
788 |
|
*/ |
796 |
|
/** |
797 |
|
* Possibly executes other tasks until this task is ready. This |
798 |
|
* method may be invoked only from within ForkJoinTask |
799 |
< |
* computations. Attempts to invoke in other contexts result in |
800 |
< |
* exceptions or errors possibly including ClassCastException. |
799 |
> |
* computations (as may be determined using method {@link |
800 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts resul!t |
801 |
> |
* in exceptions or errors possibly including ClassCastException. |
802 |
|
*/ |
803 |
|
public final void quietlyHelpJoin() { |
804 |
|
if (status >= 0) { |
864 |
|
|
865 |
|
/** |
866 |
|
* Returns the pool hosting the current task execution, or null |
867 |
< |
* if this task is executing outside of any pool. |
867 |
> |
* if this task is executing outside of any ForkJoinPool. |
868 |
|
* |
869 |
< |
* @return the pool, or null if none |
869 |
> |
* @return the pool, or null if none. |
870 |
|
*/ |
871 |
|
public static ForkJoinPool getPool() { |
872 |
|
Thread t = Thread.currentThread(); |
875 |
|
} |
876 |
|
|
877 |
|
/** |
878 |
+ |
* Returns true if the current thread is executing as a |
879 |
+ |
* ForkJoinPool computation. |
880 |
+ |
* @return <code>true</code> if the current thread is executing as a |
881 |
+ |
* ForkJoinPool computation, or false otherwise |
882 |
+ |
*/ |
883 |
+ |
public static boolean inForkJoinPool() { |
884 |
+ |
return Thread.currentThread() instanceof ForkJoinWorkerThread; |
885 |
+ |
} |
886 |
+ |
|
887 |
+ |
/** |
888 |
|
* Tries to unschedule this task for execution. This method will |
889 |
|
* typically succeed if this task is the most recently forked task |
890 |
|
* by the current thread, and has not commenced executing in |
891 |
|
* another thread. This method may be useful when arranging |
892 |
|
* alternative local processing of tasks that could have been, but |
893 |
|
* were not, stolen. This method may be invoked only from within |
894 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
895 |
< |
* result in exceptions or errors possibly including ClassCastException. |
894 |
> |
* ForkJoinTask computations (as may be determined using method |
895 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
896 |
> |
* result in exceptions or errors possibly including |
897 |
> |
* ClassCastException. |
898 |
|
* |
899 |
|
* @return true if unforked |
900 |
|
*/ |
975 |
|
* be polled or executed next. This method is designed primarily |
976 |
|
* to support extensions, and is unlikely to be useful otherwise. |
977 |
|
* This method may be invoked only from within ForkJoinTask |
978 |
< |
* computations. Attempts to invoke in other contexts result in |
979 |
< |
* exceptions or errors possibly including ClassCastException. |
978 |
> |
* computations (as may be determined using method {@link |
979 |
> |
* #inForkJoinPool}). Attempts to invoke in other contexts result |
980 |
> |
* in exceptions or errors possibly including ClassCastException. |
981 |
|
* |
982 |
|
* @return the next task, or null if none are available |
983 |
|
*/ |
990 |
|
* queued by the current thread but not yet executed. This method |
991 |
|
* is designed primarily to support extensions, and is unlikely to |
992 |
|
* be useful otherwise. This method may be invoked only from |
993 |
< |
* within ForkJoinTask computations. Attempts to invoke in other |
993 |
> |
* within ForkJoinTask computations (as may be determined using |
994 |
> |
* method {@link #inForkJoinPool}). Attempts to invoke in other |
995 |
|
* contexts result in exceptions or errors possibly including |
996 |
|
* ClassCastException. |
997 |
|
* |
1010 |
|
* of the pool this task is operating in. This method is designed |
1011 |
|
* primarily to support extensions, and is unlikely to be useful |
1012 |
|
* otherwise. This method may be invoked only from within |
1013 |
< |
* ForkJoinTask computations. Attempts to invoke in other contexts |
1013 |
> |
* ForkJoinTask computations (as may be determined using method |
1014 |
> |
* {@link #inForkJoinPool}). Attempts to invoke in other contexts |
1015 |
|
* result in exceptions or errors possibly including |
1016 |
|
* ClassCastException. |
1017 |
|
* |
1080 |
|
|
1081 |
|
private static long fieldOffset(String fieldName) |
1082 |
|
throws NoSuchFieldException { |
1083 |
< |
return _unsafe.objectFieldOffset |
1083 |
> |
return UNSAFE.objectFieldOffset |
1084 |
|
(ForkJoinTask.class.getDeclaredField(fieldName)); |
1085 |
|
} |
1086 |
|
|
1087 |
< |
static final Unsafe _unsafe; |
1087 |
> |
static final Unsafe UNSAFE; |
1088 |
|
static final long statusOffset; |
1089 |
|
|
1090 |
|
static { |
1091 |
|
try { |
1092 |
< |
_unsafe = getUnsafe(); |
1092 |
> |
UNSAFE = getUnsafe(); |
1093 |
|
statusOffset = fieldOffset("status"); |
1094 |
|
} catch (Throwable e) { |
1095 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |