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.19 by jsr166, Fri Jul 24 18:57:56 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 652 | Line 684 | public class ForkJoinPool extends Abstra
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 {
# Line 1841 | 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 <                        }});
1856 <            } catch (java.security.PrivilegedActionException e) {
1857 <                throw e.getCause();
1858 <            }
1859 <        }
1860 <    }
1861 <
1862 <    private static Unsafe getUnsafePrivileged()
1863 <            throws NoSuchFieldException, IllegalAccessException {
1864 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
1865 <        f.setAccessible(true);
1866 <        return (Unsafe) f.get(null);
1867 <    }
1868 <
1869 <    private static long fieldOffset(String fieldName)
1870 <            throws NoSuchFieldException {
1871 <        return UNSAFE.objectFieldOffset
1872 <            (ForkJoinPool.class.getDeclaredField(fieldName));
1873 <    }
1874 <
1875 <    static final Unsafe UNSAFE;
1876 <    static final long eventCountOffset;
1877 <    static final long workerCountsOffset;
1878 <    static final long runControlOffset;
1879 <    static final long syncStackOffset;
1880 <    static final long spareStackOffset;
1881 <
1882 <    static {
1883 <        try {
1884 <            UNSAFE = getUnsafe();
1885 <            eventCountOffset = fieldOffset("eventCount");
1886 <            workerCountsOffset = fieldOffset("workerCounts");
1887 <            runControlOffset = fieldOffset("runControl");
1888 <            syncStackOffset = fieldOffset("syncStack");
1889 <            spareStackOffset = fieldOffset("spareStack");
1890 <        } catch (Throwable e) {
1891 <            throw new RuntimeException("Could not initialize intrinsics", e);
1892 <        }
1893 <    }
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 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