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 |
548 |
|
* Common code for execute, invoke and submit |
549 |
|
*/ |
550 |
|
private <T> void doSubmit(ForkJoinTask<T> task) { |
551 |
+ |
if (task == null) |
552 |
+ |
throw new NullPointerException(); |
553 |
|
if (isShutdown()) |
554 |
|
throw new RejectedExecutionException(); |
555 |
|
if (workers == null) |
585 |
|
// AbstractExecutorService methods |
586 |
|
|
587 |
|
public void execute(Runnable task) { |
588 |
< |
doSubmit(new AdaptedRunnable<Void>(task, null)); |
588 |
> |
ForkJoinTask<?> job; |
589 |
> |
if (task instanceof ForkJoinTask) // avoid re-wrap |
590 |
> |
job = (ForkJoinTask<?>)task; |
591 |
> |
else |
592 |
> |
job = new AdaptedRunnable<Void>(task, null); |
593 |
> |
doSubmit(job); |
594 |
|
} |
595 |
|
|
596 |
|
public <T> ForkJoinTask<T> submit(Callable<T> task) { |
606 |
|
} |
607 |
|
|
608 |
|
public ForkJoinTask<?> submit(Runnable task) { |
609 |
< |
ForkJoinTask<Void> job = new AdaptedRunnable<Void>(task, null); |
609 |
> |
ForkJoinTask<?> job; |
610 |
> |
if (task instanceof ForkJoinTask) // avoid re-wrap |
611 |
> |
job = (ForkJoinTask<?>)task; |
612 |
> |
else |
613 |
> |
job = new AdaptedRunnable<Void>(task, null); |
614 |
|
doSubmit(job); |
615 |
|
return job; |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
+ |
* Submits a ForkJoinTask for execution. |
620 |
+ |
* |
621 |
+ |
* @param task the task to submit |
622 |
+ |
* @return the task |
623 |
+ |
* @throws RejectedExecutionException if the task cannot be |
624 |
+ |
* scheduled for execution |
625 |
+ |
* @throws NullPointerException if the task is null |
626 |
+ |
*/ |
627 |
+ |
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) { |
628 |
+ |
doSubmit(task); |
629 |
+ |
return task; |
630 |
+ |
} |
631 |
+ |
|
632 |
+ |
/** |
633 |
|
* Adaptor for Runnables. This implements RunnableFuture |
634 |
|
* to be compliant with AbstractExecutorService constraints. |
635 |
|
*/ |
651 |
|
return true; |
652 |
|
} |
653 |
|
public void run() { invoke(); } |
654 |
+ |
private static final long serialVersionUID = 5232453952276885070L; |
655 |
|
} |
656 |
|
|
657 |
|
/** |
680 |
|
} |
681 |
|
} |
682 |
|
public void run() { invoke(); } |
683 |
+ |
private static final long serialVersionUID = 2838392045355241008L; |
684 |
|
} |
685 |
|
|
686 |
|
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
687 |
< |
ArrayList<ForkJoinTask<T>> ts = |
687 |
> |
ArrayList<ForkJoinTask<T>> forkJoinTasks = |
688 |
|
new ArrayList<ForkJoinTask<T>>(tasks.size()); |
689 |
< |
for (Callable<T> c : tasks) |
690 |
< |
ts.add(new AdaptedCallable<T>(c)); |
691 |
< |
invoke(new InvokeAll<T>(ts)); |
692 |
< |
return (List<Future<T>>) (List) ts; |
689 |
> |
for (Callable<T> task : tasks) |
690 |
> |
forkJoinTasks.add(new AdaptedCallable<T>(task)); |
691 |
> |
invoke(new InvokeAll<T>(forkJoinTasks)); |
692 |
> |
|
693 |
> |
@SuppressWarnings({"unchecked", "rawtypes"}) |
694 |
> |
List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks; |
695 |
> |
return futures; |
696 |
|
} |
697 |
|
|
698 |
|
static final class InvokeAll<T> extends RecursiveAction { |
702 |
|
try { invokeAll(tasks); } |
703 |
|
catch (Exception ignore) {} |
704 |
|
} |
705 |
+ |
private static final long serialVersionUID = -7914297376763021607L; |
706 |
|
} |
707 |
|
|
708 |
|
// Configuration and status settings and queries |
1869 |
|
// AbstractExecutorService overrides |
1870 |
|
|
1871 |
|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
1872 |
< |
return new AdaptedRunnable(runnable, value); |
1872 |
> |
return new AdaptedRunnable<T>(runnable, value); |
1873 |
|
} |
1874 |
|
|
1875 |
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
1876 |
< |
return new AdaptedCallable(callable); |
1876 |
> |
return new AdaptedCallable<T>(callable); |
1877 |
|
} |
1878 |
|
|
1879 |
|
|
1880 |
< |
// Temporary Unsafe mechanics for preliminary release |
1881 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1880 |
> |
// Unsafe mechanics for jsr166y 3rd party package. |
1881 |
> |
private static sun.misc.Unsafe getUnsafe() { |
1882 |
|
try { |
1883 |
< |
return Unsafe.getUnsafe(); |
1883 |
> |
return sun.misc.Unsafe.getUnsafe(); |
1884 |
|
} catch (SecurityException se) { |
1885 |
|
try { |
1886 |
|
return java.security.AccessController.doPrivileged |
1887 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
1888 |
< |
public Unsafe run() throws Exception { |
1889 |
< |
return getUnsafePrivileged(); |
1887 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1888 |
> |
public sun.misc.Unsafe run() throws Exception { |
1889 |
> |
return getUnsafeByReflection(); |
1890 |
|
}}); |
1891 |
|
} catch (java.security.PrivilegedActionException e) { |
1892 |
< |
throw e.getCause(); |
1892 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
1893 |
> |
e.getCause()); |
1894 |
|
} |
1895 |
|
} |
1896 |
|
} |
1897 |
|
|
1898 |
< |
private static Unsafe getUnsafePrivileged() |
1898 |
> |
private static sun.misc.Unsafe getUnsafeByReflection() |
1899 |
|
throws NoSuchFieldException, IllegalAccessException { |
1900 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1900 |
> |
java.lang.reflect.Field f = |
1901 |
> |
sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
1902 |
|
f.setAccessible(true); |
1903 |
< |
return (Unsafe) f.get(null); |
1903 |
> |
return (sun.misc.Unsafe) f.get(null); |
1904 |
|
} |
1905 |
|
|
1906 |
< |
private static long fieldOffset(String fieldName) |
1867 |
< |
throws NoSuchFieldException { |
1868 |
< |
return UNSAFE.objectFieldOffset |
1869 |
< |
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1870 |
< |
} |
1871 |
< |
|
1872 |
< |
static final Unsafe UNSAFE; |
1873 |
< |
static final long eventCountOffset; |
1874 |
< |
static final long workerCountsOffset; |
1875 |
< |
static final long runControlOffset; |
1876 |
< |
static final long syncStackOffset; |
1877 |
< |
static final long spareStackOffset; |
1878 |
< |
|
1879 |
< |
static { |
1906 |
> |
private static long fieldOffset(String fieldName, Class<?> klazz) { |
1907 |
|
try { |
1908 |
< |
UNSAFE = getUnsafe(); |
1909 |
< |
eventCountOffset = fieldOffset("eventCount"); |
1910 |
< |
workerCountsOffset = fieldOffset("workerCounts"); |
1911 |
< |
runControlOffset = fieldOffset("runControl"); |
1912 |
< |
syncStackOffset = fieldOffset("syncStack"); |
1913 |
< |
spareStackOffset = fieldOffset("spareStack"); |
1887 |
< |
} catch (Throwable e) { |
1888 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1908 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName)); |
1909 |
> |
} catch (NoSuchFieldException e) { |
1910 |
> |
// Convert Exception to Error |
1911 |
> |
NoSuchFieldError error = new NoSuchFieldError(fieldName); |
1912 |
> |
error.initCause(e); |
1913 |
> |
throw error; |
1914 |
|
} |
1915 |
|
} |
1916 |
|
|
1917 |
+ |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1918 |
+ |
static final long eventCountOffset = |
1919 |
+ |
fieldOffset("eventCount", ForkJoinPool.class); |
1920 |
+ |
static final long workerCountsOffset = |
1921 |
+ |
fieldOffset("workerCounts", ForkJoinPool.class); |
1922 |
+ |
static final long runControlOffset = |
1923 |
+ |
fieldOffset("runControl", ForkJoinPool.class); |
1924 |
+ |
static final long syncStackOffset = |
1925 |
+ |
fieldOffset("syncStack",ForkJoinPool.class); |
1926 |
+ |
static final long spareStackOffset = |
1927 |
+ |
fieldOffset("spareStack", ForkJoinPool.class); |
1928 |
+ |
|
1929 |
|
private boolean casEventCount(long cmp, long val) { |
1930 |
|
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1931 |
|
} |