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 |
607 |
|
* @throws RuntimeException or Error if any task did so |
608 |
|
*/ |
609 |
|
public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) { |
610 |
< |
if (!(tasks instanceof List)) { |
610 |
> |
if (!(tasks instanceof List<?>)) { |
611 |
|
invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()])); |
612 |
|
return; |
613 |
|
} |
614 |
+ |
@SuppressWarnings("unchecked") |
615 |
|
List<? extends ForkJoinTask<?>> ts = |
616 |
|
(List<? extends ForkJoinTask<?>>) tasks; |
617 |
|
Throwable ex = null; |
878 |
|
*/ |
879 |
|
public static ForkJoinPool getPool() { |
880 |
|
Thread t = Thread.currentThread(); |
881 |
< |
return ((t instanceof ForkJoinWorkerThread) ? |
882 |
< |
((ForkJoinWorkerThread) t).pool : null); |
881 |
> |
return (t instanceof ForkJoinWorkerThread) ? |
882 |
> |
((ForkJoinWorkerThread) t).pool : null; |
883 |
|
} |
884 |
|
|
885 |
|
/** |
1066 |
|
setDoneExceptionally((Throwable) ex); |
1067 |
|
} |
1068 |
|
|
1069 |
< |
// Temporary Unsafe mechanics for preliminary release |
1070 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1069 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1070 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1071 |
|
try { |
1072 |
< |
return Unsafe.getUnsafe(); |
1072 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1073 |
|
} catch (SecurityException se) { |
1074 |
|
try { |
1075 |
|
return java.security.AccessController.doPrivileged |
1076 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1077 |
< |
public Unsafe run() throws Exception { |
1078 |
< |
return getUnsafePrivileged(); |
1076 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1077 |
> |
public sun.misc.Unsafe run() throws Exception { |
1078 |
> |
return getUnsafeByReflection(); |
1079 |
|
}}); |
1080 |
|
} catch (java.security.PrivilegedActionException e) { |
1081 |
< |
throw e.getCause(); |
1081 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1082 |
> |
e.getCause()); |
1083 |
|
} |
1084 |
|
} |
1085 |
|
} |
1086 |
|
|
1087 |
< |
private static Unsafe getUnsafePrivileged() |
1087 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1088 |
|
throws NoSuchFieldException, IllegalAccessException { |
1089 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1089 |
> |
java.lang.reflect.Field f = |
1090 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1091 |
|
f.setAccessible(true); |
1092 |
< |
return (Unsafe) f.get(null); |
1092 |
> |
return (sun.misc.Unsafe) f.get(null); |
1093 |
|
} |
1094 |
|
|
1095 |
< |
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 { |
1095 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1096 |
|
try { |
1097 |
< |
UNSAFE = getUnsafe(); |
1098 |
< |
statusOffset = fieldOffset("status"); |
1099 |
< |
} catch (Throwable e) { |
1100 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1097 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1098 |
> |
} catch (NoSuchFieldException e) { |
1099 |
> |
// Convert Exception to Error |
1100 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1101 |
> |
error.initCause(e); |
1102 |
> |
throw error; |
1103 |
|
} |
1104 |
|
} |
1105 |
|
|
1106 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1107 |
+ |
static final long statusOffset = |
1108 |
+ |
fieldOffset("status", ForkJoinTask.class); |
1109 |
+ |
|
1110 |
|
} |