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.23 by dl, Sat Jul 25 15:50:57 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 AdaptedCallable) // avoid re-wrap
590 >            job = (AdaptedCallable<?>)task;
591 >        else if (task instanceof AdaptedRunnable)
592 >            job = (AdaptedRunnable<?>)task;
593 >        else
594 >            job = new AdaptedRunnable<Void>(task, null);
595 >        doSubmit(job);
596      }
597  
598      public <T> ForkJoinTask<T> submit(Callable<T> task) {
# Line 592 | Line 608 | public class ForkJoinPool extends Abstra
608      }
609  
610      public ForkJoinTask<?> submit(Runnable task) {
611 <        ForkJoinTask<Void> job = new AdaptedRunnable<Void>(task, null);
611 >        ForkJoinTask<?> job;
612 >        if (task instanceof AdaptedCallable) // avoid re-wrap
613 >            job = (AdaptedCallable<?>)task;
614 >        else if (task instanceof AdaptedRunnable)
615 >            job = (AdaptedRunnable<?>)task;
616 >        else
617 >            job = new AdaptedRunnable<Void>(task, null);
618          doSubmit(job);
619          return job;
620      }
621  
622      /**
623 +     * Submits a ForkJoinTask for execution.
624 +     *
625 +     * @param task the task to submit
626 +     * @return the task
627 +     * @throws RejectedExecutionException if the task cannot be
628 +     *         scheduled for execution
629 +     * @throws NullPointerException if the task is null
630 +     */
631 +    public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
632 +        doSubmit(task);
633 +        return task;
634 +    }
635 +
636 +    /**
637       * Adaptor for Runnables. This implements RunnableFuture
638       * to be compliant with AbstractExecutorService constraints.
639       */
# Line 652 | Line 688 | public class ForkJoinPool extends Abstra
688      }
689  
690      public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
691 <        ArrayList<ForkJoinTask<T>> ts =
691 >        ArrayList<ForkJoinTask<T>> forkJoinTasks =
692              new ArrayList<ForkJoinTask<T>>(tasks.size());
693 <        for (Callable<T> c : tasks)
694 <            ts.add(new AdaptedCallable<T>(c));
695 <        invoke(new InvokeAll<T>(ts));
696 <        return (List<Future<T>>) (List) ts;
693 >        for (Callable<T> task : tasks)
694 >            forkJoinTasks.add(new AdaptedCallable<T>(task));
695 >        invoke(new InvokeAll<T>(forkJoinTasks));
696 >
697 >        @SuppressWarnings({"unchecked", "rawtypes"})
698 >        List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks;
699 >        return futures;
700      }
701  
702      static final class InvokeAll<T> extends RecursiveAction {
# Line 1842 | Line 1881 | public class ForkJoinPool extends Abstra
1881      }
1882  
1883  
1884 <    // Temporary Unsafe mechanics for preliminary release
1885 <    private static Unsafe getUnsafe() throws Throwable {
1884 >    // Unsafe mechanics for jsr166y 3rd party package.
1885 >    private static sun.misc.Unsafe getUnsafe() {
1886          try {
1887 <            return Unsafe.getUnsafe();
1887 >            return sun.misc.Unsafe.getUnsafe();
1888          } catch (SecurityException se) {
1889              try {
1890                  return java.security.AccessController.doPrivileged
1891 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
1892 <                        public Unsafe run() throws Exception {
1893 <                            return getUnsafePrivileged();
1891 >                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1892 >                        public sun.misc.Unsafe run() throws Exception {
1893 >                            return getUnsafeByReflection();
1894                          }});
1895              } catch (java.security.PrivilegedActionException e) {
1896 <                throw e.getCause();
1896 >                throw new RuntimeException("Could not initialize intrinsics",
1897 >                                           e.getCause());
1898              }
1899          }
1900      }
1901  
1902 <    private static Unsafe getUnsafePrivileged()
1902 >    private static sun.misc.Unsafe getUnsafeByReflection()
1903              throws NoSuchFieldException, IllegalAccessException {
1904 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
1904 >        java.lang.reflect.Field f =
1905 >            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1906          f.setAccessible(true);
1907 <        return (Unsafe) f.get(null);
1907 >        return (sun.misc.Unsafe) f.get(null);
1908      }
1909  
1910 <    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 {
1910 >    private static long fieldOffset(String fieldName, Class<?> klazz) {
1911          try {
1912 <            UNSAFE = getUnsafe();
1913 <            eventCountOffset = fieldOffset("eventCount");
1914 <            workerCountsOffset = fieldOffset("workerCounts");
1915 <            runControlOffset = fieldOffset("runControl");
1916 <            syncStackOffset = fieldOffset("syncStack");
1917 <            spareStackOffset = fieldOffset("spareStack");
1890 <        } catch (Throwable e) {
1891 <            throw new RuntimeException("Could not initialize intrinsics", e);
1912 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1913 >        } catch (NoSuchFieldException e) {
1914 >            // Convert Exception to Error
1915 >            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1916 >            error.initCause(e);
1917 >            throw error;
1918          }
1919      }
1920  
1921 +    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1922 +    static final long eventCountOffset =
1923 +        fieldOffset("eventCount", ForkJoinPool.class);
1924 +    static final long workerCountsOffset =
1925 +        fieldOffset("workerCounts", ForkJoinPool.class);
1926 +    static final long runControlOffset =
1927 +        fieldOffset("runControl", ForkJoinPool.class);
1928 +    static final long syncStackOffset =
1929 +        fieldOffset("syncStack",ForkJoinPool.class);
1930 +    static final long spareStackOffset =
1931 +        fieldOffset("spareStack", ForkJoinPool.class);
1932 +
1933      private boolean casEventCount(long cmp, long val) {
1934          return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val);
1935      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines