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 |
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}, to simplify usage. |
497 |
|
*/ |
498 |
< |
public final void fork() { |
498 |
> |
public final ForkJoinTask<V> fork() { |
499 |
|
((ForkJoinWorkerThread) Thread.currentThread()) |
500 |
|
.pushTask(this); |
501 |
+ |
return this; |
502 |
|
} |
503 |
|
|
504 |
|
/** |
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)) { |
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; |
616 |
> |
return tasks; |
617 |
|
} |
618 |
+ |
@SuppressWarnings("unchecked") |
619 |
|
List<? extends ForkJoinTask<?>> ts = |
620 |
|
(List<? extends ForkJoinTask<?>>) tasks; |
621 |
|
Throwable ex = null; |
648 |
|
} |
649 |
|
if (ex != null) |
650 |
|
rethrowException(ex); |
651 |
+ |
return tasks; |
652 |
|
} |
653 |
|
|
654 |
|
/** |
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 |
|
/** |
1039 |
|
.pollTask(); |
1040 |
|
} |
1041 |
|
|
1042 |
+ |
// adaptors |
1043 |
+ |
|
1044 |
+ |
/** |
1045 |
+ |
* Returns a new ForkJoinTask that performs the {@code run} |
1046 |
+ |
* method of the given Runnable as its action, and returns a null |
1047 |
+ |
* result upon {@code join}. |
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} |
1058 |
+ |
* method of the given Runnable as its action, and returns the |
1059 |
+ |
* given result upon {@code join}. |
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} |
1071 |
+ |
* method of the given Callable as its action, and returns its |
1072 |
+ |
* result upon {@code join}, translating any checked |
1073 |
+ |
* exceptions encountered into {@code RuntimeException}. |
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 |
1083 |
|
|
1084 |
|
private static final long serialVersionUID = -7721805057305804111L; |
1111 |
|
setDoneExceptionally((Throwable) ex); |
1112 |
|
} |
1113 |
|
|
1114 |
< |
// Temporary Unsafe mechanics for preliminary release |
1115 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1114 |
> |
// Unsafe mechanics |
1115 |
> |
|
1116 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1117 |
> |
private static final long statusOffset = |
1118 |
> |
objectFieldOffset("status", ForkJoinTask.class); |
1119 |
> |
|
1120 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1121 |
> |
try { |
1122 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1123 |
> |
} catch (NoSuchFieldException e) { |
1124 |
> |
// Convert Exception to corresponding Error |
1125 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
1126 |
> |
error.initCause(e); |
1127 |
> |
throw error; |
1128 |
> |
} |
1129 |
> |
} |
1130 |
> |
|
1131 |
> |
/** |
1132 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
1133 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
1134 |
> |
* into a jdk. |
1135 |
> |
* |
1136 |
> |
* @return a sun.misc.Unsafe |
1137 |
> |
*/ |
1138 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1139 |
|
try { |
1140 |
< |
return Unsafe.getUnsafe(); |
1140 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1141 |
|
} catch (SecurityException se) { |
1142 |
|
try { |
1143 |
|
return java.security.AccessController.doPrivileged |
1144 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1145 |
< |
public Unsafe run() throws Exception { |
1146 |
< |
return getUnsafePrivileged(); |
1144 |
> |
(new java.security |
1145 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1146 |
> |
public sun.misc.Unsafe run() throws Exception { |
1147 |
> |
java.lang.reflect.Field f = sun.misc |
1148 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
1149 |
> |
f.setAccessible(true); |
1150 |
> |
return (sun.misc.Unsafe) f.get(null); |
1151 |
|
}}); |
1152 |
|
} catch (java.security.PrivilegedActionException e) { |
1153 |
< |
throw e.getCause(); |
1153 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1154 |
> |
e.getCause()); |
1155 |
|
} |
1156 |
|
} |
1157 |
|
} |
1081 |
– |
|
1082 |
– |
private static Unsafe getUnsafePrivileged() |
1083 |
– |
throws NoSuchFieldException, IllegalAccessException { |
1084 |
– |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1085 |
– |
f.setAccessible(true); |
1086 |
– |
return (Unsafe) f.get(null); |
1087 |
– |
} |
1088 |
– |
|
1089 |
– |
private static long fieldOffset(String fieldName) |
1090 |
– |
throws NoSuchFieldException { |
1091 |
– |
return UNSAFE.objectFieldOffset |
1092 |
– |
(ForkJoinTask.class.getDeclaredField(fieldName)); |
1093 |
– |
} |
1094 |
– |
|
1095 |
– |
static final Unsafe UNSAFE; |
1096 |
– |
static final long statusOffset; |
1097 |
– |
|
1098 |
– |
static { |
1099 |
– |
try { |
1100 |
– |
UNSAFE = getUnsafe(); |
1101 |
– |
statusOffset = fieldOffset("status"); |
1102 |
– |
} catch (Throwable e) { |
1103 |
– |
throw new RuntimeException("Could not initialize intrinsics", e); |
1104 |
– |
} |
1105 |
– |
} |
1106 |
– |
|
1158 |
|
} |