5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import java.util.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.locks.*; |
11 |
< |
import java.util.concurrent.atomic.*; |
12 |
< |
import sun.misc.Unsafe; |
13 |
< |
import java.lang.reflect.*; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Collections; |
15 |
> |
import java.util.List; |
16 |
> |
import java.util.concurrent.locks.Condition; |
17 |
> |
import java.util.concurrent.locks.LockSupport; |
18 |
> |
import java.util.concurrent.locks.ReentrantLock; |
19 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
20 |
> |
import java.util.concurrent.atomic.AtomicLong; |
21 |
|
|
22 |
|
/** |
23 |
|
* An {@link ExecutorService} for running {@link ForkJoinTask}s. A |
659 |
|
} |
660 |
|
|
661 |
|
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
662 |
< |
ArrayList<ForkJoinTask<T>> ts = |
662 |
> |
ArrayList<ForkJoinTask<T>> forkJoinTasks = |
663 |
|
new ArrayList<ForkJoinTask<T>>(tasks.size()); |
664 |
< |
for (Callable<T> c : tasks) |
665 |
< |
ts.add(new AdaptedCallable<T>(c)); |
666 |
< |
invoke(new InvokeAll<T>(ts)); |
667 |
< |
return (List<Future<T>>) (List) ts; |
664 |
> |
for (Callable<T> task : tasks) |
665 |
> |
forkJoinTasks.add(new AdaptedCallable<T>(task)); |
666 |
> |
invoke(new InvokeAll<T>(forkJoinTasks)); |
667 |
> |
|
668 |
> |
@SuppressWarnings({"unchecked", "rawtypes"}) |
669 |
> |
List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks; |
670 |
> |
return futures; |
671 |
|
} |
672 |
|
|
673 |
|
static final class InvokeAll<T> extends RecursiveAction { |
1844 |
|
// AbstractExecutorService overrides |
1845 |
|
|
1846 |
|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
1847 |
< |
return new AdaptedRunnable(runnable, value); |
1847 |
> |
return new AdaptedRunnable<T>(runnable, value); |
1848 |
|
} |
1849 |
|
|
1850 |
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
1851 |
< |
return new AdaptedCallable(callable); |
1851 |
> |
return new AdaptedCallable<T>(callable); |
1852 |
|
} |
1853 |
|
|
1854 |
|
|
1855 |
< |
// Temporary Unsafe mechanics for preliminary release |
1856 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1855 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1856 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1857 |
|
try { |
1858 |
< |
return Unsafe.getUnsafe(); |
1858 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1859 |
|
} catch (SecurityException se) { |
1860 |
|
try { |
1861 |
|
return java.security.AccessController.doPrivileged |
1862 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1863 |
< |
public Unsafe run() throws Exception { |
1864 |
< |
return getUnsafePrivileged(); |
1862 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1863 |
> |
public sun.misc.Unsafe run() throws Exception { |
1864 |
> |
return getUnsafeByReflection(); |
1865 |
|
}}); |
1866 |
|
} catch (java.security.PrivilegedActionException e) { |
1867 |
< |
throw e.getCause(); |
1867 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1868 |
> |
e.getCause()); |
1869 |
|
} |
1870 |
|
} |
1871 |
|
} |
1872 |
|
|
1873 |
< |
private static Unsafe getUnsafePrivileged() |
1873 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1874 |
|
throws NoSuchFieldException, IllegalAccessException { |
1875 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1875 |
> |
java.lang.reflect.Field f = |
1876 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1877 |
|
f.setAccessible(true); |
1878 |
< |
return (Unsafe) f.get(null); |
1867 |
< |
} |
1868 |
< |
|
1869 |
< |
private static long fieldOffset(String fieldName) |
1870 |
< |
throws NoSuchFieldException { |
1871 |
< |
return UNSAFE.objectFieldOffset |
1872 |
< |
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1878 |
> |
return (sun.misc.Unsafe) f.get(null); |
1879 |
|
} |
1880 |
|
|
1881 |
< |
static final Unsafe UNSAFE; |
1876 |
< |
static final long eventCountOffset; |
1877 |
< |
static final long workerCountsOffset; |
1878 |
< |
static final long runControlOffset; |
1879 |
< |
static final long syncStackOffset; |
1880 |
< |
static final long spareStackOffset; |
1881 |
< |
|
1882 |
< |
static { |
1881 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1882 |
|
try { |
1883 |
< |
UNSAFE = getUnsafe(); |
1884 |
< |
eventCountOffset = fieldOffset("eventCount"); |
1885 |
< |
workerCountsOffset = fieldOffset("workerCounts"); |
1886 |
< |
runControlOffset = fieldOffset("runControl"); |
1887 |
< |
syncStackOffset = fieldOffset("syncStack"); |
1888 |
< |
spareStackOffset = fieldOffset("spareStack"); |
1890 |
< |
} catch (Throwable e) { |
1891 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1883 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1884 |
> |
} catch (NoSuchFieldException e) { |
1885 |
> |
// Convert Exception to Error |
1886 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1887 |
> |
error.initCause(e); |
1888 |
> |
throw error; |
1889 |
|
} |
1890 |
|
} |
1891 |
|
|
1892 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1893 |
+ |
static final long eventCountOffset = |
1894 |
+ |
fieldOffset("eventCount", ForkJoinPool.class); |
1895 |
+ |
static final long workerCountsOffset = |
1896 |
+ |
fieldOffset("workerCounts", ForkJoinPool.class); |
1897 |
+ |
static final long runControlOffset = |
1898 |
+ |
fieldOffset("runControl", ForkJoinPool.class); |
1899 |
+ |
static final long syncStackOffset = |
1900 |
+ |
fieldOffset("syncStack",ForkJoinPool.class); |
1901 |
+ |
static final long spareStackOffset = |
1902 |
+ |
fieldOffset("spareStack", ForkJoinPool.class); |
1903 |
+ |
|
1904 |
|
private boolean casEventCount(long cmp, long val) { |
1905 |
|
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1906 |
|
} |