ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinTask.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinTask.java (file contents):
Revision 1.14 by jsr166, Thu Jul 23 23:07:57 2009 UTC vs.
Revision 1.18 by dl, Sat Jul 25 15:50:57 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 < import java.io.Serializable;
9 < import java.util.*;
8 >
9   import java.util.concurrent.*;
10 < import java.util.concurrent.atomic.*;
11 < import sun.misc.Unsafe;
12 < import java.lang.reflect.*;
10 >
11 > import java.io.Serializable;
12 > import java.util.Collection;
13 > import java.util.Collections;
14 > import java.util.List;
15 > import java.util.Map;
16 > import java.util.WeakHashMap;
17  
18   /**
19   * Abstract base class for tasks that run within a {@link
# Line 489 | Line 492 | public abstract class ForkJoinTask<V> im
492       * computations (as may be determined using method {@link
493       * #inForkJoinPool}). Attempts to invoke in other contexts result
494       * in exceptions or errors, possibly including ClassCastException.
495 +     *
496 +     * @return <code>this</code>, to simplify usage.
497       */
498 <    public final void fork() {
498 >    public final ForkJoinTask<V> fork() {
499          ((ForkJoinWorkerThread) Thread.currentThread())
500              .pushTask(this);
501 +        return this;
502      }
503  
504      /**
# Line 604 | Line 610 | public abstract class ForkJoinTask<V> im
610       * @throws RuntimeException or Error if any task did so
611       */
612      public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) {
613 <        if (!(tasks instanceof List)) {
613 >        if (!(tasks instanceof List<?>)) {
614              invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
615              return;
616          }
617 +        @SuppressWarnings("unchecked")
618          List<? extends ForkJoinTask<?>> ts =
619              (List<? extends ForkJoinTask<?>>) tasks;
620          Throwable ex = null;
# Line 874 | Line 881 | public abstract class ForkJoinTask<V> im
881       */
882      public static ForkJoinPool getPool() {
883          Thread t = Thread.currentThread();
884 <        return ((t instanceof ForkJoinWorkerThread) ?
885 <                ((ForkJoinWorkerThread) t).pool : null);
884 >        return (t instanceof ForkJoinWorkerThread) ?
885 >            ((ForkJoinWorkerThread) t).pool : null;
886      }
887  
888      /**
# Line 1030 | Line 1037 | public abstract class ForkJoinTask<V> im
1037              .pollTask();
1038      }
1039  
1040 +    // adaptors
1041 +
1042 +    /**
1043 +     * Returns a new ForkJoinTask that performs the <code>run</code>
1044 +     * method of the given Runnable as its action, and returns a null
1045 +     * result upon <code>join</code>.
1046 +     *
1047 +     * @param runnable the runnable action
1048 +     * @return the task
1049 +     */
1050 +    public static ForkJoinTask<Void> adapt(Runnable runnable) {
1051 +        return new ForkJoinPool.AdaptedRunnable<Void>(runnable, null);
1052 +    }
1053 +
1054 +    /**
1055 +     * Returns a new ForkJoinTask that performs the <code>run</code>
1056 +     * method of the given Runnable as its action, and returns the
1057 +     * given result upon <code>join</code>.
1058 +     *
1059 +     * @param runnable the runnable action
1060 +     * @param result the result upon completion
1061 +     * @return the task
1062 +     */
1063 +    public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
1064 +        return new ForkJoinPool.AdaptedRunnable<T>(runnable, result);
1065 +    }
1066 +
1067 +    /**
1068 +     * Returns a new ForkJoinTask that performs the <code>call</code>
1069 +     * method of the given Callable as its action, and returns its
1070 +     * result upon <code>join</code>, translating any checked
1071 +     * exceptions encountered into <code>RuntimeException<code>.
1072 +     *
1073 +     * @param callable the callable action
1074 +     * @return the task
1075 +     */
1076 +    public static <T> ForkJoinTask<T> adapt(Callable<T> callable) {
1077 +        return new ForkJoinPool.AdaptedCallable<T>(callable);
1078 +    }
1079 +
1080      // Serialization support
1081  
1082      private static final long serialVersionUID = -7721805057305804111L;
# Line 1062 | Line 1109 | public abstract class ForkJoinTask<V> im
1109              setDoneExceptionally((Throwable) ex);
1110      }
1111  
1112 <    // Temporary Unsafe mechanics for preliminary release
1113 <    private static Unsafe getUnsafe() throws Throwable {
1112 >    // Unsafe mechanics for jsr166y 3rd party package.
1113 >    private static sun.misc.Unsafe getUnsafe() {
1114          try {
1115 <            return Unsafe.getUnsafe();
1115 >            return sun.misc.Unsafe.getUnsafe();
1116          } catch (SecurityException se) {
1117              try {
1118                  return java.security.AccessController.doPrivileged
1119 <                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
1120 <                        public Unsafe run() throws Exception {
1121 <                            return getUnsafePrivileged();
1119 >                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
1120 >                        public sun.misc.Unsafe run() throws Exception {
1121 >                            return getUnsafeByReflection();
1122                          }});
1123              } catch (java.security.PrivilegedActionException e) {
1124 <                throw e.getCause();
1124 >                throw new RuntimeException("Could not initialize intrinsics",
1125 >                                           e.getCause());
1126              }
1127          }
1128      }
1129  
1130 <    private static Unsafe getUnsafePrivileged()
1130 >    private static sun.misc.Unsafe getUnsafeByReflection()
1131              throws NoSuchFieldException, IllegalAccessException {
1132 <        Field f = Unsafe.class.getDeclaredField("theUnsafe");
1132 >        java.lang.reflect.Field f =
1133 >            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
1134          f.setAccessible(true);
1135 <        return (Unsafe) f.get(null);
1087 <    }
1088 <
1089 <    private static long fieldOffset(String fieldName)
1090 <            throws NoSuchFieldException {
1091 <        return UNSAFE.objectFieldOffset
1092 <            (ForkJoinTask.class.getDeclaredField(fieldName));
1135 >        return (sun.misc.Unsafe) f.get(null);
1136      }
1137  
1138 <    static final Unsafe UNSAFE;
1096 <    static final long statusOffset;
1097 <
1098 <    static {
1138 >    private static long fieldOffset(String fieldName, Class<?> klazz) {
1139          try {
1140 <            UNSAFE = getUnsafe();
1141 <            statusOffset = fieldOffset("status");
1142 <        } catch (Throwable e) {
1143 <            throw new RuntimeException("Could not initialize intrinsics", e);
1140 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
1141 >        } catch (NoSuchFieldException e) {
1142 >            // Convert Exception to Error
1143 >            NoSuchFieldError error = new NoSuchFieldError(fieldName);
1144 >            error.initCause(e);
1145 >            throw error;
1146          }
1147      }
1148  
1149 +    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1150 +    static final long statusOffset =
1151 +        fieldOffset("status", ForkJoinTask.class);
1152 +
1153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines