ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinPool.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinPool.java (file contents):
Revision 1.26 by jsr166, Sun Jul 26 13:34:25 2009 UTC vs.
Revision 1.28 by jsr166, Mon Jul 27 20:57:44 2009 UTC

# Line 311 | Line 311 | public class ForkJoinPool extends Abstra
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) {
# Line 720 | Line 720 | public class ForkJoinPool extends Abstra
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;
# Line 741 | Line 741 | public class ForkJoinPool extends Abstra
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
# Line 851 | Line 851 | public class ForkJoinPool extends Abstra
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.
857 <     * 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;
# Line 867 | Line 866 | public class ForkJoinPool extends Abstra
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;
# Line 900 | Line 899 | public class ForkJoinPool extends Abstra
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;
# Line 944 | Line 943 | public class ForkJoinPool extends Abstra
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;
# Line 1018 | Line 1017 | public class ForkJoinPool extends Abstra
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       */
# Line 1032 | Line 1031 | public class ForkJoinPool extends Abstra
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();
# Line 1492 | Line 1491 | public class ForkJoinPool extends Abstra
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
# Line 1500 | Line 1499 | public class ForkJoinPool extends Abstra
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;
# Line 1519 | Line 1518 | public class ForkJoinPool extends Abstra
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;
# Line 1604 | Line 1603 | public class ForkJoinPool extends Abstra
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
# Line 1774 | Line 1773 | public class ForkJoinPool extends Abstra
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:
# Line 1801 | Line 1800 | public class ForkJoinPool extends Abstra
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      }
# Line 1819 | Line 1818 | public class ForkJoinPool extends Abstra
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
# Line 1837 | Line 1836 | public class ForkJoinPool extends Abstra
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,
# Line 1876 | Line 1875 | public class ForkJoinPool extends Abstra
1875          return new AdaptedCallable<T>(callable);
1876      }
1877  
1878 <
1880 <    // Unsafe mechanics for jsr166y 3rd party package.
1881 <    private static sun.misc.Unsafe getUnsafe() {
1882 <        try {
1883 <            return sun.misc.Unsafe.getUnsafe();
1884 <        } catch (SecurityException se) {
1885 <            try {
1886 <                return java.security.AccessController.doPrivileged
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 new RuntimeException("Could not initialize intrinsics",
1893 <                                           e.getCause());
1894 <            }
1895 <        }
1896 <    }
1897 <
1898 <    private static sun.misc.Unsafe getUnsafeByReflection()
1899 <            throws NoSuchFieldException, IllegalAccessException {
1900 <        java.lang.reflect.Field f =
1901 <            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1902 <        f.setAccessible(true);
1903 <        return (sun.misc.Unsafe) f.get(null);
1904 <    }
1905 <
1906 <    private static long fieldOffset(String fieldName, Class<?> klazz) {
1907 <        try {
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 <    }
1878 >    // Unsafe mechanics
1879  
1880      private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1881      private static final long eventCountOffset =
1882 <        fieldOffset("eventCount", ForkJoinPool.class);
1882 >        objectFieldOffset("eventCount", ForkJoinPool.class);
1883      private static final long workerCountsOffset =
1884 <        fieldOffset("workerCounts", ForkJoinPool.class);
1884 >        objectFieldOffset("workerCounts", ForkJoinPool.class);
1885      private static final long runControlOffset =
1886 <        fieldOffset("runControl", ForkJoinPool.class);
1886 >        objectFieldOffset("runControl", ForkJoinPool.class);
1887      private static final long syncStackOffset =
1888 <        fieldOffset("syncStack",ForkJoinPool.class);
1888 >        objectFieldOffset("syncStack",ForkJoinPool.class);
1889      private static final long spareStackOffset =
1890 <        fieldOffset("spareStack", ForkJoinPool.class);
1890 >        objectFieldOffset("spareStack", ForkJoinPool.class);
1891  
1892      private boolean casEventCount(long cmp, long val) {
1893          return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val);
# Line 1941 | Line 1904 | public class ForkJoinPool extends Abstra
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines