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.20 by jsr166, Fri Jul 24 22:05:22 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.*;
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
# Line 541 | 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 576 | 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 592 | 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 1844 | Line 1876 | public class ForkJoinPool extends Abstra
1876          return new AdaptedCallable<T>(callable);
1877      }
1878  
1879 +    // Unsafe mechanics
1880  
1881 <    // Temporary Unsafe mechanics for preliminary release
1882 <    private static Unsafe getUnsafe() throws Throwable {
1883 <        try {
1884 <            return Unsafe.getUnsafe();
1885 <        } catch (SecurityException se) {
1886 <            try {
1887 <                return java.security.AccessController.doPrivileged
1888 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
1889 <                        public Unsafe run() throws Exception {
1890 <                            return getUnsafePrivileged();
1891 <                        }});
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 <    }
1881 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
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 1910 | 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