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.3 by dl, Wed Jan 7 19:12:36 2009 UTC vs.
Revision 1.10 by jsr166, Tue Jul 21 00:15:13 2009 UTC

# Line 18 | Line 18 | import java.lang.reflect.*;
18   * lighter weight than a normal thread.  Huge numbers of tasks and
19   * subtasks may be hosted by a small number of actual threads in a
20   * ForkJoinPool, at the price of some usage limitations.
21 < *
21 > *
22   * <p> A "main" ForkJoinTask begins execution when submitted to a
23   * {@link ForkJoinPool}. Once started, it will usually in turn start
24   * other subtasks.  As indicated by the name of this class, many
25 < * programs using ForkJoinTasks employ only methods <code>fork</code>
26 < * and <code>join</code>, or derivatives such as
27 < * <code>invokeAll</code>.  However, this class also provides a number
25 > * programs using ForkJoinTasks employ only methods {@code fork}
26 > * and {@code join}, or derivatives such as
27 > * {@code invokeAll}.  However, this class also provides a number
28   * of other methods that can come into play in advanced usages, as
29   * well as extension mechanics that allow support of new forms of
30   * fork/join processing.
31 < *
31 > *
32   * <p>A ForkJoinTask is a lightweight form of {@link Future}.  The
33   * efficiency of ForkJoinTasks stems from a set of restrictions (that
34   * are only partially statically enforceable) reflecting their
# Line 36 | Line 36 | import java.lang.reflect.*;
36   * operating on purely isolated objects.  The primary coordination
37   * mechanisms are {@link #fork}, that arranges asynchronous execution,
38   * and {@link #join}, that doesn't proceed until the task's result has
39 < * been computed.  Computations should avoid <code>synchronized</code>
39 > * been computed.  Computations should avoid {@code synchronized}
40   * methods or blocks, and should minimize other blocking
41   * synchronization apart from joining other tasks or using
42   * synchronizers such as Phasers that are advertised to cooperate with
# Line 48 | Line 48 | import java.lang.reflect.*;
48   * performance, and the potential to indefinitely stall if the number
49   * of threads not waiting for IO or other external synchronization
50   * becomes exhausted. This usage restriction is in part enforced by
51 < * not permitting checked exceptions such as <code>IOExceptions</code>
51 > * not permitting checked exceptions such as {@code IOExceptions}
52   * to be thrown. However, computations may still encounter unchecked
53   * exceptions, that are rethrown to callers attempting join
54   * them. These exceptions may additionally include
# Line 58 | Line 58 | import java.lang.reflect.*;
58   * <p>The primary method for awaiting completion and extracting
59   * results of a task is {@link #join}, but there are several variants:
60   * The {@link Future#get} methods support interruptible and/or timed
61 < * waits for completion and report results using <code>Future</code>
61 > * waits for completion and report results using {@code Future}
62   * conventions. Method {@link #helpJoin} enables callers to actively
63   * execute other tasks while awaiting joins, which is sometimes more
64   * efficient but only applies when all subtasks are known to be
65   * strictly tree-structured. Method {@link #invoke} is semantically
66 < * equivalent to <code>fork(); join()</code> but always attempts to
66 > * equivalent to {@code fork(); join()} but always attempts to
67   * begin execution in the current thread. The "<em>quiet</em>" forms
68   * of these methods do not extract results or report exceptions. These
69   * may be useful when a set of tasks are being executed, and you need
70   * to delay processing of results or exceptions until all complete.
71 < * Method <code>invokeAll</code> (available in multiple versions)
71 > * Method {@code invokeAll} (available in multiple versions)
72   * performs the most common form of parallel invocation: forking a set
73   * of tasks and joining them all.
74   *
# Line 76 | Line 76 | import java.lang.reflect.*;
76   * Instead, you subclass one of the abstract classes that support a
77   * particular style of fork/join processing.  Normally, a concrete
78   * ForkJoinTask subclass declares fields comprising its parameters,
79 < * established in a constructor, and then defines a <code>compute</code>
79 > * established in a constructor, and then defines a {@code compute}
80   * method that somehow uses the control methods supplied by this base
81 < * class. While these methods have <code>public</code> access (to allow
81 > * class. While these methods have {@code public} access (to allow
82   * instances of different task subclasses to call each others
83   * methods), some of them may only be called from within other
84   * ForkJoinTasks. Attempts to invoke them in other contexts result in
85 < * exceptions or errors including ClassCastException.
85 > * exceptions or errors possibly including ClassCastException.
86   *
87 < * <p>Most base support methods are <code>final</code> because their
87 > * <p>Most base support methods are {@code final} because their
88   * implementations are intrinsically tied to the underlying
89   * lightweight task scheduling framework, and so cannot be overridden.
90   * Developers creating new basic styles of fork/join processing should
91 < * minimally implement <code>protected</code> methods
92 < * <code>exec</code>, <code>setRawResult</code>, and
93 < * <code>getRawResult</code>, while also introducing an abstract
91 > * minimally implement {@code protected} methods
92 > * {@code exec}, {@code setRawResult}, and
93 > * {@code getRawResult}, while also introducing an abstract
94   * computational method that can be implemented in its subclasses,
95 < * possibly relying on other <code>protected</code> methods provided
95 > * possibly relying on other {@code protected} methods provided
96   * by this class.
97   *
98   * <p>ForkJoinTasks should perform relatively small amounts of
99 < * computations, othewise splitting into smaller tasks. As a very
99 > * computations, otherwise splitting into smaller tasks. As a very
100   * rough rule of thumb, a task should perform more than 100 and less
101   * than 10000 basic computational steps. If tasks are too big, then
102 < * parellelism cannot improve throughput. If too small, then memory
102 > * parallelism cannot improve throughput. If too small, then memory
103   * and internal task maintenance overhead may overwhelm processing.
104   *
105 < * <p>ForkJoinTasks are <code>Serializable</code>, which enables them
105 > * <p>ForkJoinTasks are {@code Serializable}, which enables them
106   * to be used in extensions such as remote execution frameworks. It is
107   * in general sensible to serialize tasks only before or after, but
108   * not during execution. Serialization is not relied on during
# Line 128 | Line 128 | public abstract class ForkJoinTask<V> im
128       * currently unused. Also value 0x80000000 is available as spare
129       * completion value.
130       */
131 <    volatile int status; // accessed directy by pool and workers
131 >    volatile int status; // accessed directly by pool and workers
132  
133      static final int COMPLETION_MASK      = 0xe0000000;
134      static final int NORMAL               = 0xe0000000; // == mask
# Line 141 | Line 141 | public abstract class ForkJoinTask<V> im
141      /**
142       * Table of exceptions thrown by tasks, to enable reporting by
143       * callers. Because exceptions are rare, we don't directly keep
144 <     * them with task objects, but instead us a weak ref table.  Note
144 >     * them with task objects, but instead use a weak ref table.  Note
145       * that cancellation exceptions don't appear in the table, but are
146       * instead recorded as status values.
147 <     * Todo: Use ConcurrentReferenceHashMap
147 >     * TODO: Use ConcurrentReferenceHashMap
148       */
149      static final Map<ForkJoinTask<?>, Throwable> exceptionMap =
150          Collections.synchronizedMap
# Line 153 | Line 153 | public abstract class ForkJoinTask<V> im
153      // within-package utilities
154  
155      /**
156 <     * Get current worker thread, or null if not a worker thread
156 >     * Gets current worker thread, or null if not a worker thread.
157       */
158      static ForkJoinWorkerThread getWorker() {
159          Thread t = Thread.currentThread();
# Line 176 | Line 176 | public abstract class ForkJoinTask<V> im
176      // Setting completion status
177  
178      /**
179 <     * Mark completion and wake up threads waiting to join this task.
179 >     * Marks completion and wakes up threads waiting to join this task.
180 >     *
181       * @param completion one of NORMAL, CANCELLED, EXCEPTIONAL
182       */
183      final void setCompletion(int completion) {
# Line 255 | Line 256 | public abstract class ForkJoinTask<V> im
256      /**
257       * Sets status to indicate there is joiner, then waits for join,
258       * surrounded with pool notifications.
259 +     *
260       * @return status upon exit
261       */
262      private int awaitDone(ForkJoinWorkerThread w, boolean maintainParallelism) {
# Line 297 | Line 299 | public abstract class ForkJoinTask<V> im
299      }
300  
301      /**
302 <     * Notify pool that thread is unblocked. Called by signalled
302 >     * Notifies pool that thread is unblocked. Called by signalled
303       * threads when woken by non-FJ threads (which is atypical).
304       */
305      private void adjustPoolCountsOnUnblock(ForkJoinPool pool) {
# Line 308 | Line 310 | public abstract class ForkJoinTask<V> im
310      }
311  
312      /**
313 <     * Notify pool to adjust counts on cancelled or timed out wait
313 >     * Notifies pool to adjust counts on cancelled or timed out wait.
314       */
315      private void adjustPoolCountsOnCancelledWait(ForkJoinPool pool) {
316          if (pool != null) {
# Line 323 | Line 325 | public abstract class ForkJoinTask<V> im
325      }
326  
327      /**
328 <     * Handle interruptions during waits.
328 >     * Handles interruptions during waits.
329       */
330      private void onInterruptedWait() {
331          ForkJoinWorkerThread w = getWorker();
# Line 342 | Line 344 | public abstract class ForkJoinTask<V> im
344      }
345  
346      /**
347 <     * Throws the exception associated with status s;
347 >     * Throws the exception associated with status s.
348 >     *
349       * @throws the exception
350       */
351      private void reportException(int s) {
# Line 355 | Line 358 | public abstract class ForkJoinTask<V> im
358      }
359  
360      /**
361 <     * Returns result or throws exception using j.u.c.Future conventions
361 >     * Returns result or throws exception using j.u.c.Future conventions.
362       * Only call when isDone known to be true.
363       */
364      private V reportFutureResult()
# Line 375 | Line 378 | public abstract class ForkJoinTask<V> im
378  
379      /**
380       * Returns result or throws exception using j.u.c.Future conventions
381 <     * with timeouts
381 >     * with timeouts.
382       */
383      private V reportTimedFutureResult()
384          throws InterruptedException, ExecutionException, TimeoutException {
# Line 396 | Line 399 | public abstract class ForkJoinTask<V> im
399  
400      /**
401       * Calls exec, recording completion, and rethrowing exception if
402 <     * encountered. Caller should normally check status before calling
402 >     * encountered. Caller should normally check status before calling.
403 >     *
404       * @return true if completed normally
405       */
406      private boolean tryExec() {
# Line 414 | Line 418 | public abstract class ForkJoinTask<V> im
418  
419      /**
420       * Main execution method used by worker threads. Invokes
421 <     * base computation unless already complete
421 >     * base computation unless already complete.
422       */
423      final void quietlyExec() {
424          if (status >= 0) {
# Line 430 | Line 434 | public abstract class ForkJoinTask<V> im
434      }
435  
436      /**
437 <     * Calls exec, recording but not rethrowing exception
438 <     * Caller should normally check status before calling
437 >     * Calls exec(), recording but not rethrowing exception.
438 >     * Caller should normally check status before calling.
439 >     *
440       * @return true if completed normally
441       */
442      private boolean tryQuietlyInvoke() {
# Line 447 | Line 452 | public abstract class ForkJoinTask<V> im
452      }
453  
454      /**
455 <     * Cancel, ignoring any exceptions it throws
455 >     * Cancels, ignoring any exceptions it throws.
456       */
457      final void cancelIgnoringExceptions() {
458          try {
# Line 475 | Line 480 | public abstract class ForkJoinTask<V> im
480       * than once unless it has completed and been reinitialized.  This
481       * method may be invoked only from within ForkJoinTask
482       * computations. Attempts to invoke in other contexts result in
483 <     * exceptions or errors including ClassCastException.
483 >     * exceptions or errors possibly including ClassCastException.
484       */
485      public final void fork() {
486          ((ForkJoinWorkerThread)(Thread.currentThread())).pushTask(this);
# Line 483 | Line 488 | public abstract class ForkJoinTask<V> im
488  
489      /**
490       * Returns the result of the computation when it is ready.
491 <     * This method differs from <code>get</code> in that abnormal
491 >     * This method differs from {@code get} in that abnormal
492       * completion results in RuntimeExceptions or Errors, not
493       * ExecutionExceptions.
494       *
# Line 499 | Line 504 | public abstract class ForkJoinTask<V> im
504      /**
505       * Commences performing this task, awaits its completion if
506       * necessary, and return its result.
507 +     *
508       * @throws Throwable (a RuntimeException, Error, or unchecked
509 <     * exception) if the underlying computation did so.
509 >     * exception) if the underlying computation did so
510       * @return the computed result
511       */
512      public final V invoke() {
# Line 511 | Line 517 | public abstract class ForkJoinTask<V> im
517      }
518  
519      /**
520 <     * Forks both tasks, returning when <code>isDone</code> holds for
520 >     * Forks both tasks, returning when {@code isDone} holds for
521       * both of them or an exception is encountered. This method may be
522       * invoked only from within ForkJoinTask computations. Attempts to
523       * invoke in other contexts result in exceptions or errors
524 <     * including ClassCastException.
524 >     * possibly including ClassCastException.
525 >     *
526       * @param t1 one task
527       * @param t2 the other task
528       * @throws NullPointerException if t1 or t2 are null
529 <     * @throws RuntimeException or Error if either task did so.
529 >     * @throws RuntimeException or Error if either task did so
530       */
531      public static void invokeAll(ForkJoinTask<?>t1, ForkJoinTask<?> t2) {
532          t2.fork();
# Line 528 | Line 535 | public abstract class ForkJoinTask<V> im
535      }
536  
537      /**
538 <     * Forks the given tasks, returning when <code>isDone</code> holds
538 >     * Forks the given tasks, returning when {@code isDone} holds
539       * for all of them. If any task encounters an exception, others
540       * may be cancelled.  This method may be invoked only from within
541       * ForkJoinTask computations. Attempts to invoke in other contexts
542 <     * result in exceptions or errors including ClassCastException.
542 >     * result in exceptions or errors possibly including ClassCastException.
543 >     *
544       * @param tasks the array of tasks
545 <     * @throws NullPointerException if tasks or any element are null.
546 <     * @throws RuntimeException or Error if any task did so.
545 >     * @throws NullPointerException if tasks or any element are null
546 >     * @throws RuntimeException or Error if any task did so
547       */
548      public static void invokeAll(ForkJoinTask<?>... tasks) {
549          Throwable ex = null;
# Line 572 | Line 580 | public abstract class ForkJoinTask<V> im
580  
581      /**
582       * Forks all tasks in the collection, returning when
583 <     * <code>isDone</code> holds for all of them. If any task
583 >     * {@code isDone} holds for all of them. If any task
584       * encounters an exception, others may be cancelled.  This method
585       * may be invoked only from within ForkJoinTask
586 <     * computations. Attempts to invoke in other contexts resul!t in
587 <     * exceptions or errors including ClassCastException.
586 >     * computations. Attempts to invoke in other contexts result in
587 >     * exceptions or errors possibly including ClassCastException.
588 >     *
589       * @param tasks the collection of tasks
590 <     * @throws NullPointerException if tasks or any element are null.
591 <     * @throws RuntimeException or Error if any task did so.
590 >     * @throws NullPointerException if tasks or any element are null
591 >     * @throws RuntimeException or Error if any task did so
592       */
593      public static void invokeAll(Collection<? extends ForkJoinTask<?>> tasks) {
594          if (!(tasks instanceof List)) {
# Line 623 | Line 632 | public abstract class ForkJoinTask<V> im
632      /**
633       * Returns true if the computation performed by this task has
634       * completed (or has been cancelled).
635 +     *
636       * @return true if this computation has completed
637       */
638      public final boolean isDone() {
# Line 631 | Line 641 | public abstract class ForkJoinTask<V> im
641  
642      /**
643       * Returns true if this task was cancelled.
644 +     *
645       * @return true if this task was cancelled
646       */
647      public final boolean isCancelled() {
# Line 639 | Line 650 | public abstract class ForkJoinTask<V> im
650  
651      /**
652       * Asserts that the results of this task's computation will not be
653 <     * used. If a cancellation occurs before atempting to execute this
654 <     * task, then execution will be suppressed, <code>isCancelled</code>
655 <     * will report true, and <code>join</code> will result in a
656 <     * <code>CancellationException</code> being thrown. Otherwise, when
653 >     * used. If a cancellation occurs before attempting to execute this
654 >     * task, then execution will be suppressed, {@code isCancelled}
655 >     * will report true, and {@code join} will result in a
656 >     * {@code CancellationException} being thrown. Otherwise, when
657       * cancellation races with completion, there are no guarantees
658 <     * about whether <code>isCancelled</code> will report true, whether
659 <     * <code>join</code> will return normally or via an exception, or
658 >     * about whether {@code isCancelled} will report true, whether
659 >     * {@code join} will return normally or via an exception, or
660       * whether these behaviors will remain consistent upon repeated
661       * invocation.
662       *
# Line 656 | Line 667 | public abstract class ForkJoinTask<V> im
667       * <p> This method is designed to be invoked by <em>other</em>
668       * tasks. To terminate the current task, you can just return or
669       * throw an unchecked exception from its computation method, or
670 <     * invoke <code>completeExceptionally</code>.
670 >     * invoke {@code completeExceptionally}.
671       *
672       * @param mayInterruptIfRunning this value is ignored in the
673       * default implementation because tasks are not in general
# Line 670 | Line 681 | public abstract class ForkJoinTask<V> im
681      }
682  
683      /**
684 <     * Returns true if this task threw an exception or was cancelled
684 >     * Returns true if this task threw an exception or was cancelled.
685 >     *
686       * @return true if this task threw an exception or was cancelled
687       */
688      public final boolean isCompletedAbnormally() {
# Line 681 | Line 693 | public abstract class ForkJoinTask<V> im
693       * Returns the exception thrown by the base computation, or a
694       * CancellationException if cancelled, or null if none or if the
695       * method has not yet completed.
696 +     *
697       * @return the exception, or null if none
698       */
699      public final Throwable getException() {
# Line 695 | Line 708 | public abstract class ForkJoinTask<V> im
708      /**
709       * Completes this task abnormally, and if not already aborted or
710       * cancelled, causes it to throw the given exception upon
711 <     * <code>join</code> and related operations. This method may be used
711 >     * {@code join} and related operations. This method may be used
712       * to induce exceptions in asynchronous tasks, or to force
713       * completion of tasks that would not otherwise complete.  Its use
714       * in other situations is likely to be wrong.  This method is
715 <     * overridable, but overridden versions must invoke <code>super</code>
715 >     * overridable, but overridden versions must invoke {@code super}
716       * implementation to maintain guarantees.
717       *
718       * @param ex the exception to throw. If this exception is
# Line 714 | Line 727 | public abstract class ForkJoinTask<V> im
727  
728      /**
729       * Completes this task, and if not already aborted or cancelled,
730 <     * returning a <code>null</code> result upon <code>join</code> and related
730 >     * returning a {@code null} result upon {@code join} and related
731       * operations. This method may be used to provide results for
732       * asynchronous tasks, or to provide alternative handling for
733       * tasks that would not otherwise complete normally. Its use in
734       * other situations is likely to be wrong. This method is
735 <     * overridable, but overridden versions must invoke <code>super</code>
735 >     * overridable, but overridden versions must invoke {@code super}
736       * implementation to maintain guarantees.
737       *
738 <     * @param value the result value for this task.
738 >     * @param value the result value for this task
739       */
740      public void complete(V value) {
741          try {
# Line 752 | Line 765 | public abstract class ForkJoinTask<V> im
765      /**
766       * Possibly executes other tasks until this task is ready, then
767       * returns the result of the computation.  This method may be more
768 <     * efficient than <code>join</code>, but is only applicable when
769 <     * there are no potemtial dependencies between continuation of the
768 >     * efficient than {@code join}, but is only applicable when
769 >     * there are no potential dependencies between continuation of the
770       * current task and that of any other task that might be executed
771       * while helping. (This usually holds for pure divide-and-conquer
772       * tasks). This method may be invoked only from within
773       * ForkJoinTask computations. Attempts to invoke in other contexts
774 <     * resul!t in exceptions or errors including ClassCastException.
774 >     * result in exceptions or errors possibly including ClassCastException.
775 >     *
776       * @return the computed result
777       */
778      public final V helpJoin() {
# Line 771 | Line 785 | public abstract class ForkJoinTask<V> im
785      /**
786       * Possibly executes other tasks until this task is ready.  This
787       * method may be invoked only from within ForkJoinTask
788 <     * computations. Attempts to invoke in other contexts resul!t in
789 <     * exceptions or errors including ClassCastException.
788 >     * computations. Attempts to invoke in other contexts result in
789 >     * exceptions or errors possibly including ClassCastException.
790       */
791      public final void quietlyHelpJoin() {
792          if (status >= 0) {
# Line 822 | Line 836 | public abstract class ForkJoinTask<V> im
836  
837      /**
838       * Resets the internal bookkeeping state of this task, allowing a
839 <     * subsequent <code>fork</code>. This method allows repeated reuse of
839 >     * subsequent {@code fork}. This method allows repeated reuse of
840       * this task, but only if reuse occurs when this task has either
841       * never been forked, or has been forked, then completed and all
842       * outstanding joins of this task have also completed. Effects
# Line 839 | Line 853 | public abstract class ForkJoinTask<V> im
853      /**
854       * Returns the pool hosting the current task execution, or null
855       * if this task is executing outside of any pool.
856 <     * @return the pool, or null if none.
856 >     *
857 >     * @return the pool, or null if none
858       */
859      public static ForkJoinPool getPool() {
860          Thread t = Thread.currentThread();
# Line 855 | Line 870 | public abstract class ForkJoinTask<V> im
870       * alternative local processing of tasks that could have been, but
871       * were not, stolen. This method may be invoked only from within
872       * ForkJoinTask computations. Attempts to invoke in other contexts
873 <     * result in exceptions or errors including ClassCastException.
873 >     * result in exceptions or errors possibly including ClassCastException.
874 >     *
875       * @return true if unforked
876       */
877      public boolean tryUnfork() {
# Line 867 | Line 883 | public abstract class ForkJoinTask<V> im
883       * forked by the current worker thread but not yet executed. This
884       * value may be useful for heuristic decisions about whether to
885       * fork other tasks.
886 +     *
887       * @return the number of tasks
888       */
889      public static int getQueuedTaskCount() {
# Line 875 | Line 892 | public abstract class ForkJoinTask<V> im
892      }
893  
894      /**
895 <     * Returns a estimate of how many more locally queued tasks are
895 >     * Returns an estimate of how many more locally queued tasks are
896       * held by the current worker thread than there are other worker
897       * threads that might steal them.  This value may be useful for
898       * heuristic decisions about whether to fork other tasks. In many
# Line 883 | Line 900 | public abstract class ForkJoinTask<V> im
900       * aim to maintain a small constant surplus (for example, 3) of
901       * tasks, and to process computations locally if this threshold is
902       * exceeded.
903 +     *
904       * @return the surplus number of tasks, which may be negative
905       */
906      public static int getSurplusQueuedTaskCount() {
# Line 893 | Line 911 | public abstract class ForkJoinTask<V> im
911      // Extension methods
912  
913      /**
914 <     * Returns the result that would be returned by <code>join</code>,
914 >     * Returns the result that would be returned by {@code join},
915       * even if this task completed abnormally, or null if this task is
916       * not known to have been completed.  This method is designed to
917       * aid debugging, as well as to support extensions. Its use in any
918       * other context is discouraged.
919       *
920 <     * @return the result, or null if not completed.
920 >     * @return the result, or null if not completed
921       */
922      public abstract V getRawResult();
923  
# Line 918 | Line 936 | public abstract class ForkJoinTask<V> im
936       * called otherwise. The return value controls whether this task
937       * is considered to be done normally. It may return false in
938       * asynchronous actions that require explicit invocations of
939 <     * <code>complete</code> to become joinable. It may throw exceptions
939 >     * {@code complete} to become joinable. It may throw exceptions
940       * to indicate abnormal exit.
941 +     *
942       * @return true if completed normally
943       * @throws Error or RuntimeException if encountered during computation
944       */
945      protected abstract boolean exec();
946  
947      /**
948 <     * Returns, but does not unschedule or execute, the task most
949 <     * recently forked by the current thread but not yet executed, if
950 <     * one is available. There is no guarantee that this task will
951 <     * actually be polled or executed next.
952 <     * This method is designed primarily to support extensions,
953 <     * and is unlikely to be useful otherwise.
948 >     * Returns, but does not unschedule or execute, the task queued by
949 >     * the current thread but not yet executed, if one is
950 >     * available. There is no guarantee that this task will actually
951 >     * be polled or executed next.  This method is designed primarily
952 >     * to support extensions, and is unlikely to be useful otherwise.
953 >     * This method may be invoked only from within ForkJoinTask
954 >     * computations. Attempts to invoke in other contexts result in
955 >     * exceptions or errors possibly including ClassCastException.
956       *
957       * @return the next task, or null if none are available
958       */
# Line 940 | Line 961 | public abstract class ForkJoinTask<V> im
961      }
962  
963      /**
964 <     * Unschedules and returns, without executing, the task most
965 <     * recently forked by the current thread but not yet executed.
966 <     * This method is designed primarily to support extensions,
967 <     * and is unlikely to be useful otherwise.
964 >     * Unschedules and returns, without executing, the next task
965 >     * queued by the current thread but not yet executed.  This method
966 >     * is designed primarily to support extensions, and is unlikely to
967 >     * be useful otherwise.  This method may be invoked only from
968 >     * within ForkJoinTask computations. Attempts to invoke in other
969 >     * contexts result in exceptions or errors possibly including
970 >     * ClassCastException.
971       *
972       * @return the next task, or null if none are available
973       */
974      protected static ForkJoinTask<?> pollNextLocalTask() {
975 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).popTask();
975 >        return ((ForkJoinWorkerThread)(Thread.currentThread())).pollLocalTask();
976      }
977  
978      /**
979 <     * Unschedules and returns, without executing, the task most
980 <     * recently forked by the current thread but not yet executed, if
981 <     * one is available, or if not available, a task that was forked
982 <     * by some other thread, if available. Availability may be
983 <     * transient, so a <code>null</code> result does not necessarily
984 <     * imply quiecence of the pool this task is operating in.
985 <     * This method is designed primarily to support extensions,
986 <     * and is unlikely to be useful otherwise.
987 <     *
979 >     * Unschedules and returns, without executing, the next task
980 >     * queued by the current thread but not yet executed, if one is
981 >     * available, or if not available, a task that was forked by some
982 >     * other thread, if available. Availability may be transient, so a
983 >     * {@code null} result does not necessarily imply quiescence
984 >     * of the pool this task is operating in.  This method is designed
985 >     * primarily to support extensions, and is unlikely to be useful
986 >     * otherwise.  This method may be invoked only from within
987 >     * ForkJoinTask computations. Attempts to invoke in other contexts
988 >     * result in exceptions or errors possibly including
989 >     * ClassCastException.
990 >     *
991       * @return a task, or null if none are available
992       */
993      protected static ForkJoinTask<?> pollTask() {
994          return ((ForkJoinWorkerThread)(Thread.currentThread())).
995 <            pollLocalOrStolenTask();
995 >            pollTask();
996      }
997  
998      // Serialization support
# Line 976 | Line 1003 | public abstract class ForkJoinTask<V> im
1003       * Save the state to a stream.
1004       *
1005       * @serialData the current run status and the exception thrown
1006 <     * during execution, or null if none.
1006 >     * during execution, or null if none
1007       * @param s the stream
1008       */
1009      private void writeObject(java.io.ObjectOutputStream s)
# Line 987 | Line 1014 | public abstract class ForkJoinTask<V> im
1014  
1015      /**
1016       * Reconstitute the instance from a stream.
1017 +     *
1018       * @param s the stream
1019       */
1020      private void readObject(java.io.ObjectInputStream s)
# Line 1000 | Line 1028 | public abstract class ForkJoinTask<V> im
1028      }
1029  
1030      // Temporary Unsafe mechanics for preliminary release
1031 +    private static Unsafe getUnsafe() throws Throwable {
1032 +        try {
1033 +            return Unsafe.getUnsafe();
1034 +        } catch (SecurityException se) {
1035 +            try {
1036 +                return java.security.AccessController.doPrivileged
1037 +                    (new java.security.PrivilegedExceptionAction<Unsafe>() {
1038 +                        public Unsafe run() throws Exception {
1039 +                            return getUnsafePrivileged();
1040 +                        }});
1041 +            } catch (java.security.PrivilegedActionException e) {
1042 +                throw e.getCause();
1043 +            }
1044 +        }
1045 +    }
1046 +
1047 +    private static Unsafe getUnsafePrivileged()
1048 +            throws NoSuchFieldException, IllegalAccessException {
1049 +        Field f = Unsafe.class.getDeclaredField("theUnsafe");
1050 +        f.setAccessible(true);
1051 +        return (Unsafe) f.get(null);
1052 +    }
1053 +
1054 +    private static long fieldOffset(String fieldName)
1055 +            throws NoSuchFieldException {
1056 +        return _unsafe.objectFieldOffset
1057 +            (ForkJoinTask.class.getDeclaredField(fieldName));
1058 +    }
1059  
1060      static final Unsafe _unsafe;
1061      static final long statusOffset;
1062  
1063      static {
1064          try {
1065 <            if (ForkJoinTask.class.getClassLoader() != null) {
1066 <                Field f = Unsafe.class.getDeclaredField("theUnsafe");
1067 <                f.setAccessible(true);
1068 <                _unsafe = (Unsafe)f.get(null);
1069 <            }
1014 <            else
1015 <                _unsafe = Unsafe.getUnsafe();
1016 <            statusOffset = _unsafe.objectFieldOffset
1017 <                (ForkJoinTask.class.getDeclaredField("status"));
1018 <        } catch (Exception ex) { throw new Error(ex); }
1065 >            _unsafe = getUnsafe();
1066 >            statusOffset = fieldOffset("status");
1067 >        } catch (Throwable e) {
1068 >            throw new RuntimeException("Could not initialize intrinsics", e);
1069 >        }
1070      }
1071  
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines