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 |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
< |
* Returns true if argument represents zero active count and |
315 |
< |
* nonzero runstate, which is the triggering condition for |
314 |
> |
* Returns {@code true} if argument represents zero active count |
315 |
> |
* and nonzero runstate, which is the triggering condition for |
316 |
|
* terminating on shutdown. |
317 |
|
*/ |
318 |
|
private static boolean canTerminateOnShutdown(int c) { |
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 |
|
*/ |
720 |
|
* Returns the handler for internal worker threads that terminate |
721 |
|
* due to unrecoverable errors encountered while executing tasks. |
722 |
|
* |
723 |
< |
* @return the handler, or null if none |
723 |
> |
* @return the handler, or {@code null} if none |
724 |
|
*/ |
725 |
|
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { |
726 |
|
Thread.UncaughtExceptionHandler h; |
741 |
|
* as handler. |
742 |
|
* |
743 |
|
* @param h the new handler |
744 |
< |
* @return the old handler, or null if none |
744 |
> |
* @return the old handler, or {@code null} if none |
745 |
|
* @throws SecurityException if a security manager exists and |
746 |
|
* the caller is not permitted to modify threads |
747 |
|
* because it does not hold {@link |
851 |
|
|
852 |
|
|
853 |
|
/** |
854 |
< |
* Returns true if this pool dynamically maintains its target |
855 |
< |
* parallelism level. If false, new threads are added only to |
856 |
< |
* avoid possible starvation. |
825 |
< |
* This setting is by default true. |
854 |
> |
* Returns {@code true} if this pool dynamically maintains its |
855 |
> |
* target parallelism level. If false, new threads are added only |
856 |
> |
* to avoid possible starvation. This setting is by default true. |
857 |
|
* |
858 |
< |
* @return true if maintains parallelism |
858 |
> |
* @return {@code true} if maintains parallelism |
859 |
|
*/ |
860 |
|
public boolean getMaintainsParallelism() { |
861 |
|
return maintainsParallelism; |
866 |
|
* parallelism level. If false, new threads are added only to |
867 |
|
* avoid possible starvation. |
868 |
|
* |
869 |
< |
* @param enable true to maintains parallelism |
869 |
> |
* @param enable {@code true} to maintain parallelism |
870 |
|
*/ |
871 |
|
public void setMaintainsParallelism(boolean enable) { |
872 |
|
maintainsParallelism = enable; |
899 |
|
} |
900 |
|
|
901 |
|
/** |
902 |
< |
* Returns true if this pool uses local first-in-first-out |
902 |
> |
* Returns {@code true} if this pool uses local first-in-first-out |
903 |
|
* scheduling mode for forked tasks that are never joined. |
904 |
|
* |
905 |
< |
* @return true if this pool uses async mode |
905 |
> |
* @return {@code true} if this pool uses async mode |
906 |
|
*/ |
907 |
|
public boolean getAsyncMode() { |
908 |
|
return locallyFifo; |
943 |
|
} |
944 |
|
|
945 |
|
/** |
946 |
< |
* Returns true if all worker threads are currently idle. An idle |
947 |
< |
* worker is one that cannot obtain a task to execute because none |
948 |
< |
* are available to steal from other threads, and there are no |
949 |
< |
* pending submissions to the pool. This method is conservative; |
950 |
< |
* it might not return true immediately upon idleness of all |
951 |
< |
* threads, but will eventually become true if threads remain |
952 |
< |
* inactive. |
946 |
> |
* Returns {@code true} if all worker threads are currently idle. |
947 |
> |
* An idle worker is one that cannot obtain a task to execute |
948 |
> |
* because none are available to steal from other threads, and |
949 |
> |
* there are no pending submissions to the pool. This method is |
950 |
> |
* conservative; it might not return {@code true} immediately upon |
951 |
> |
* idleness of all threads, but will eventually become true if |
952 |
> |
* threads remain inactive. |
953 |
|
* |
954 |
< |
* @return true if all threads are currently idle |
954 |
> |
* @return {@code true} if all threads are currently idle |
955 |
|
*/ |
956 |
|
public boolean isQuiescent() { |
957 |
|
return activeCountOf(runControl) == 0; |
1017 |
|
} |
1018 |
|
|
1019 |
|
/** |
1020 |
< |
* Returns true if there are any tasks submitted to this pool |
1021 |
< |
* that have not yet begun executing. |
1020 |
> |
* Returns {@code true} if there are any tasks submitted to this |
1021 |
> |
* pool that have not yet begun executing. |
1022 |
|
* |
1023 |
|
* @return {@code true} if there are any queued submissions |
1024 |
|
*/ |
1031 |
|
* available. This method may be useful in extensions to this |
1032 |
|
* class that re-assign work in systems with multiple pools. |
1033 |
|
* |
1034 |
< |
* @return the next submission, or null if none |
1034 |
> |
* @return the next submission, or {@code null} if none |
1035 |
|
*/ |
1036 |
|
protected ForkJoinTask<?> pollSubmission() { |
1037 |
|
return submissionQueue.poll(); |
1491 |
|
} |
1492 |
|
|
1493 |
|
/** |
1494 |
< |
* Returns true if worker waiting on sync can proceed: |
1494 |
> |
* Returns {@code true} if worker waiting on sync can proceed: |
1495 |
|
* - on signal (thread == null) |
1496 |
|
* - on event count advance (winning race to notify vs signaller) |
1497 |
|
* - on interrupt |
1499 |
|
* If node was not signalled and event count not advanced on exit, |
1500 |
|
* then we also help advance event count. |
1501 |
|
* |
1502 |
< |
* @return true if node can be released |
1502 |
> |
* @return {@code true} if node can be released |
1503 |
|
*/ |
1504 |
|
final boolean syncIsReleasable(WaitQueueNode node) { |
1505 |
|
long prev = node.count; |
1518 |
|
} |
1519 |
|
|
1520 |
|
/** |
1521 |
< |
* Returns true if a new sync event occurred since last call to |
1522 |
< |
* sync or this method, if so, updating caller's count. |
1521 |
> |
* Returns {@code true} if a new sync event occurred since last |
1522 |
> |
* call to sync or this method, if so, updating caller's count. |
1523 |
|
*/ |
1524 |
|
final boolean hasNewSyncEvent(ForkJoinWorkerThread w) { |
1525 |
|
long lc = w.lastEventCount; |
1603 |
|
} |
1604 |
|
|
1605 |
|
/** |
1606 |
< |
* Returns true if a spare thread appears to be needed. If |
1607 |
< |
* maintaining parallelism, returns true when the deficit in |
1606 |
> |
* Returns {@code true} if a spare thread appears to be needed. |
1607 |
> |
* If maintaining parallelism, returns true when the deficit in |
1608 |
|
* running threads is more than the surplus of total threads, and |
1609 |
|
* there is apparently some work to do. This self-limiting rule |
1610 |
|
* means that the more threads that have already been added, the |
1773 |
|
/** |
1774 |
|
* Interface for extending managed parallelism for tasks running |
1775 |
|
* in ForkJoinPools. A ManagedBlocker provides two methods. |
1776 |
< |
* Method {@code isReleasable} must return true if blocking is not |
1777 |
< |
* necessary. Method {@code block} blocks the current thread if |
1778 |
< |
* necessary (perhaps internally invoking {@code isReleasable} |
1779 |
< |
* before actually blocking.). |
1776 |
> |
* Method {@code isReleasable} must return {@code true} if |
1777 |
> |
* blocking is not necessary. Method {@code block} blocks the |
1778 |
> |
* current thread if necessary (perhaps internally invoking |
1779 |
> |
* {@code isReleasable} before actually blocking.). |
1780 |
|
* |
1781 |
|
* <p>For example, here is a ManagedBlocker based on a |
1782 |
|
* ReentrantLock: |
1800 |
|
* Possibly blocks the current thread, for example waiting for |
1801 |
|
* a lock or condition. |
1802 |
|
* |
1803 |
< |
* @return true if no additional blocking is necessary (i.e., |
1804 |
< |
* if isReleasable would return true) |
1803 |
> |
* @return {@code true} if no additional blocking is necessary |
1804 |
> |
* (i.e., if isReleasable would return true) |
1805 |
|
* @throws InterruptedException if interrupted while waiting |
1806 |
|
* (the method is not required to do so, but is allowed to) |
1807 |
|
*/ |
1808 |
|
boolean block() throws InterruptedException; |
1809 |
|
|
1810 |
|
/** |
1811 |
< |
* Returns true if blocking is unnecessary. |
1811 |
> |
* Returns {@code true} if blocking is unnecessary. |
1812 |
|
*/ |
1813 |
|
boolean isReleasable(); |
1814 |
|
} |
1818 |
|
* is a ForkJoinWorkerThread, this method possibly arranges for a |
1819 |
|
* spare thread to be activated if necessary to ensure parallelism |
1820 |
|
* while the current thread is blocked. If |
1821 |
< |
* {@code maintainParallelism} is true and the pool supports |
1821 |
> |
* {@code maintainParallelism} is {@code true} and the pool supports |
1822 |
|
* it ({@link #getMaintainsParallelism}), this method attempts to |
1823 |
|
* maintain the pool's nominal parallelism. Otherwise it activates |
1824 |
|
* a thread only if necessary to avoid complete starvation. This |
1836 |
|
* be expanded to ensure parallelism, and later adjusted. |
1837 |
|
* |
1838 |
|
* @param blocker the blocker |
1839 |
< |
* @param maintainParallelism if true and supported by this pool, |
1840 |
< |
* attempt to maintain the pool's nominal parallelism; otherwise |
1841 |
< |
* activate a thread only if necessary to avoid complete |
1842 |
< |
* starvation. |
1839 |
> |
* @param maintainParallelism if {@code true} and supported by |
1840 |
> |
* this pool, attempt to maintain the pool's nominal parallelism; |
1841 |
> |
* otherwise activate a thread only if necessary to avoid |
1842 |
> |
* complete starvation. |
1843 |
|
* @throws InterruptedException if blocker.block did so |
1844 |
|
*/ |
1845 |
|
public static void managedBlock(ManagedBlocker blocker, |
1875 |
|
return new AdaptedCallable<T>(callable); |
1876 |
|
} |
1877 |
|
|
1878 |
+ |
// Unsafe mechanics |
1879 |
|
|
1880 |
< |
// Temporary Unsafe mechanics for preliminary release |
1881 |
< |
private static Unsafe getUnsafe() throws Throwable { |
1882 |
< |
try { |
1883 |
< |
return 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(); |
1890 |
< |
}}); |
1859 |
< |
} catch (java.security.PrivilegedActionException e) { |
1860 |
< |
throw e.getCause(); |
1861 |
< |
} |
1862 |
< |
} |
1863 |
< |
} |
1864 |
< |
|
1865 |
< |
private static Unsafe getUnsafePrivileged() |
1866 |
< |
throws NoSuchFieldException, IllegalAccessException { |
1867 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
1868 |
< |
f.setAccessible(true); |
1869 |
< |
return (Unsafe) f.get(null); |
1870 |
< |
} |
1871 |
< |
|
1872 |
< |
private static long fieldOffset(String fieldName) |
1873 |
< |
throws NoSuchFieldException { |
1874 |
< |
return UNSAFE.objectFieldOffset |
1875 |
< |
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1876 |
< |
} |
1877 |
< |
|
1878 |
< |
static final Unsafe UNSAFE; |
1879 |
< |
static final long eventCountOffset; |
1880 |
< |
static final long workerCountsOffset; |
1881 |
< |
static final long runControlOffset; |
1882 |
< |
static final long syncStackOffset; |
1883 |
< |
static final long spareStackOffset; |
1884 |
< |
|
1885 |
< |
static { |
1886 |
< |
try { |
1887 |
< |
UNSAFE = getUnsafe(); |
1888 |
< |
eventCountOffset = fieldOffset("eventCount"); |
1889 |
< |
workerCountsOffset = fieldOffset("workerCounts"); |
1890 |
< |
runControlOffset = fieldOffset("runControl"); |
1891 |
< |
syncStackOffset = fieldOffset("syncStack"); |
1892 |
< |
spareStackOffset = fieldOffset("spareStack"); |
1893 |
< |
} catch (Throwable e) { |
1894 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
1895 |
< |
} |
1896 |
< |
} |
1880 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
1881 |
> |
private static final long eventCountOffset = |
1882 |
> |
objectFieldOffset("eventCount", ForkJoinPool.class); |
1883 |
> |
private static final long workerCountsOffset = |
1884 |
> |
objectFieldOffset("workerCounts", ForkJoinPool.class); |
1885 |
> |
private static final long runControlOffset = |
1886 |
> |
objectFieldOffset("runControl", ForkJoinPool.class); |
1887 |
> |
private static final long syncStackOffset = |
1888 |
> |
objectFieldOffset("syncStack",ForkJoinPool.class); |
1889 |
> |
private static final long spareStackOffset = |
1890 |
> |
objectFieldOffset("spareStack", ForkJoinPool.class); |
1891 |
|
|
1892 |
|
private boolean casEventCount(long cmp, long val) { |
1893 |
|
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1904 |
|
private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) { |
1905 |
|
return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1906 |
|
} |
1907 |
+ |
|
1908 |
+ |
private static long objectFieldOffset(String field, Class<?> klazz) { |
1909 |
+ |
try { |
1910 |
+ |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
1911 |
+ |
} catch (NoSuchFieldException e) { |
1912 |
+ |
// Convert Exception to corresponding Error |
1913 |
+ |
NoSuchFieldError error = new NoSuchFieldError(field); |
1914 |
+ |
error.initCause(e); |
1915 |
+ |
throw error; |
1916 |
+ |
} |
1917 |
+ |
} |
1918 |
+ |
|
1919 |
+ |
/** |
1920 |
+ |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
1921 |
+ |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
1922 |
+ |
* into a jdk. |
1923 |
+ |
* |
1924 |
+ |
* @return a sun.misc.Unsafe |
1925 |
+ |
*/ |
1926 |
+ |
private static sun.misc.Unsafe getUnsafe() { |
1927 |
+ |
try { |
1928 |
+ |
return sun.misc.Unsafe.getUnsafe(); |
1929 |
+ |
} catch (SecurityException se) { |
1930 |
+ |
try { |
1931 |
+ |
return java.security.AccessController.doPrivileged |
1932 |
+ |
(new java.security |
1933 |
+ |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
1934 |
+ |
public sun.misc.Unsafe run() throws Exception { |
1935 |
+ |
java.lang.reflect.Field f = sun.misc |
1936 |
+ |
.Unsafe.class.getDeclaredField("theUnsafe"); |
1937 |
+ |
f.setAccessible(true); |
1938 |
+ |
return (sun.misc.Unsafe) f.get(null); |
1939 |
+ |
}}); |
1940 |
+ |
} catch (java.security.PrivilegedActionException e) { |
1941 |
+ |
throw new RuntimeException("Could not initialize intrinsics", |
1942 |
+ |
e.getCause()); |
1943 |
+ |
} |
1944 |
+ |
} |
1945 |
+ |
} |
1946 |
|
} |