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.22 by jsr166, Sat Jul 25 00:34:00 2009 UTC vs.
Revision 1.27 by jsr166, Sun Jul 26 17:33:37 2009 UTC

# Line 548 | 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 583 | 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 599 | 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 1851 | Line 1876 | public class ForkJoinPool extends Abstra
1876          return new AdaptedCallable<T>(callable);
1877      }
1878  
1879 <
1855 <    // Unsafe mechanics for jsr166y 3rd party package.
1856 <    private static sun.misc.Unsafe getUnsafe() {
1857 <        try {
1858 <            return sun.misc.Unsafe.getUnsafe();
1859 <        } catch (SecurityException se) {
1860 <            try {
1861 <                return java.security.AccessController.doPrivileged
1862 <                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1863 <                        public sun.misc.Unsafe run() throws Exception {
1864 <                            return getUnsafeByReflection();
1865 <                        }});
1866 <            } catch (java.security.PrivilegedActionException e) {
1867 <                throw new RuntimeException("Could not initialize intrinsics",
1868 <                                           e.getCause());
1869 <            }
1870 <        }
1871 <    }
1872 <
1873 <    private static sun.misc.Unsafe getUnsafeByReflection()
1874 <            throws NoSuchFieldException, IllegalAccessException {
1875 <        java.lang.reflect.Field f =
1876 <            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1877 <        f.setAccessible(true);
1878 <        return (sun.misc.Unsafe) f.get(null);
1879 <    }
1880 <
1881 <    private static long fieldOffset(String fieldName, Class<?> klazz) {
1882 <        try {
1883 <            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1884 <        } catch (NoSuchFieldException e) {
1885 <            // Convert Exception to Error
1886 <            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1887 <            error.initCause(e);
1888 <            throw error;
1889 <        }
1890 <    }
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 1916 | 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