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.21 by jsr166, Fri Jul 24 23:47:01 2009 UTC vs.
Revision 1.27 by jsr166, Sun Jul 26 17:33:37 2009 UTC

# Line 5 | Line 5
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.*;
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
# Line 539 | Line 548 | public class ForkJoinPool extends Abstra
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)
# Line 574 | Line 585 | public class ForkJoinPool extends Abstra
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) {
# Line 590 | Line 606 | public class ForkJoinPool extends Abstra
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       */
# Line 1842 | Line 1876 | public class ForkJoinPool extends Abstra
1876          return new AdaptedCallable<T>(callable);
1877      }
1878  
1879 <
1846 <    // Unsafe mechanics for jsr166y 3rd party package.
1847 <    private static sun.misc.Unsafe getUnsafe() {
1848 <        try {
1849 <            return sun.misc.Unsafe.getUnsafe();
1850 <        } catch (SecurityException se) {
1851 <            try {
1852 <                return java.security.AccessController.doPrivileged
1853 <                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1854 <                        public sun.misc.Unsafe run() throws Exception {
1855 <                            return getUnsafeByReflection();
1856 <                        }});
1857 <            } catch (java.security.PrivilegedActionException e) {
1858 <                throw new RuntimeException("Could not initialize intrinsics",
1859 <                                           e.getCause());
1860 <            }
1861 <        }
1862 <    }
1863 <
1864 <    private static sun.misc.Unsafe getUnsafeByReflection()
1865 <            throws NoSuchFieldException, IllegalAccessException {
1866 <        java.lang.reflect.Field f =
1867 <            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1868 <        f.setAccessible(true);
1869 <        return (sun.misc.Unsafe) f.get(null);
1870 <    }
1871 <
1872 <    private static long fieldOffset(String fieldName, Class<?> klazz) {
1873 <        try {
1874 <            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1875 <        } catch (NoSuchFieldException e) {
1876 <            // Convert Exception to Error
1877 <            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1878 <            error.initCause(e);
1879 <            throw error;
1880 <        }
1881 <    }
1879 >    // Unsafe mechanics
1880  
1881      private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1882 <    static final long eventCountOffset =
1883 <        fieldOffset("eventCount", ForkJoinPool.class);
1884 <    static final long workerCountsOffset =
1885 <        fieldOffset("workerCounts", ForkJoinPool.class);
1886 <    static final long runControlOffset =
1887 <        fieldOffset("runControl", ForkJoinPool.class);
1888 <    static final long syncStackOffset =
1889 <        fieldOffset("syncStack",ForkJoinPool.class);
1890 <    static final long spareStackOffset =
1891 <        fieldOffset("spareStack", ForkJoinPool.class);
1882 >    private static final long eventCountOffset =
1883 >        objectFieldOffset("eventCount", ForkJoinPool.class);
1884 >    private static final long workerCountsOffset =
1885 >        objectFieldOffset("workerCounts", ForkJoinPool.class);
1886 >    private static final long runControlOffset =
1887 >        objectFieldOffset("runControl", ForkJoinPool.class);
1888 >    private static final long syncStackOffset =
1889 >        objectFieldOffset("syncStack",ForkJoinPool.class);
1890 >    private static final long spareStackOffset =
1891 >        objectFieldOffset("spareStack", ForkJoinPool.class);
1892  
1893      private boolean casEventCount(long cmp, long val) {
1894          return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val);
# Line 1907 | Line 1905 | public class ForkJoinPool extends Abstra
1905      private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) {
1906          return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val);
1907      }
1908 +
1909 +    private static long objectFieldOffset(String field, Class<?> klazz) {
1910 +        try {
1911 +            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1912 +        } catch (NoSuchFieldException e) {
1913 +            // Convert Exception to corresponding Error
1914 +            NoSuchFieldError error = new NoSuchFieldError(field);
1915 +            error.initCause(e);
1916 +            throw error;
1917 +        }
1918 +    }
1919 +
1920 +    /**
1921 +     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
1922 +     * Replace with a simple call to Unsafe.getUnsafe when integrating
1923 +     * into a jdk.
1924 +     *
1925 +     * @return a sun.misc.Unsafe
1926 +     */
1927 +    private static sun.misc.Unsafe getUnsafe() {
1928 +        try {
1929 +            return sun.misc.Unsafe.getUnsafe();
1930 +        } catch (SecurityException se) {
1931 +            try {
1932 +                return java.security.AccessController.doPrivileged
1933 +                    (new java.security
1934 +                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1935 +                        public sun.misc.Unsafe run() throws Exception {
1936 +                            java.lang.reflect.Field f = sun.misc
1937 +                                .Unsafe.class.getDeclaredField("theUnsafe");
1938 +                            f.setAccessible(true);
1939 +                            return (sun.misc.Unsafe) f.get(null);
1940 +                        }});
1941 +            } catch (java.security.PrivilegedActionException e) {
1942 +                throw new RuntimeException("Could not initialize intrinsics",
1943 +                                           e.getCause());
1944 +            }
1945 +        }
1946 +    }
1947   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines