1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent;
  37 
  38 import java.io.Serializable;
  39 import java.lang.ref.ReferenceQueue;
  40 import java.lang.ref.WeakReference;
  41 import java.lang.reflect.Constructor;
  42 import java.util.Collection;
  43 import java.util.List;
  44 import java.util.RandomAccess;
  45 import java.util.concurrent.locks.ReentrantLock;
  46 
  47 /**
  48  * Abstract base class for tasks that run within a {@link ForkJoinPool}.
  49  * A {@code ForkJoinTask} is a thread-like entity that is much
  50  * lighter weight than a normal thread.  Huge numbers of tasks and
  51  * subtasks may be hosted by a small number of actual threads in a
  52  * ForkJoinPool, at the price of some usage limitations.
  53  *
  54  * <p>A "main" {@code ForkJoinTask} begins execution when it is
  55  * explicitly submitted to a {@link ForkJoinPool}, or, if not already
  56  * engaged in a ForkJoin computation, commenced in the {@link
  57  * ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or
  58  * related methods.  Once started, it will usually in turn start other
  59  * subtasks.  As indicated by the name of this class, many programs
  60  * using {@code ForkJoinTask} employ only methods {@link #fork} and
  61  * {@link #join}, or derivatives such as {@link
  62  * #invokeAll(ForkJoinTask...) invokeAll}.  However, this class also
  63  * provides a number of other methods that can come into play in
  64  * advanced usages, as well as extension mechanics that allow support
  65  * of new forms of fork/join processing.
  66  *
  67  * <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}.
  68  * The efficiency of {@code ForkJoinTask}s stems from a set of
  69  * restrictions (that are only partially statically enforceable)
  70  * reflecting their main use as computational tasks calculating pure
  71  * functions or operating on purely isolated objects.  The primary
  72  * coordination mechanisms are {@link #fork}, that arranges
  73  * asynchronous execution, and {@link #join}, that doesn't proceed
  74  * until the task's result has been computed.  Computations should
  75  * ideally avoid {@code synchronized} methods or blocks, and should
  76  * minimize other blocking synchronization apart from joining other
  77  * tasks or using synchronizers such as Phasers that are advertised to
  78  * cooperate with fork/join scheduling. Subdividable tasks should also
  79  * not perform blocking I/O, and should ideally access variables that
  80  * are completely independent of those accessed by other running
  81  * tasks. These guidelines are loosely enforced by not permitting
  82  * checked exceptions such as {@code IOExceptions} to be
  83  * thrown. However, computations may still encounter unchecked
  84  * exceptions, that are rethrown to callers attempting to join
  85  * them. These exceptions may additionally include {@link
  86  * RejectedExecutionException} stemming from internal resource
  87  * exhaustion, such as failure to allocate internal task
  88  * queues. Rethrown exceptions behave in the same way as regular
  89  * exceptions, but, when possible, contain stack traces (as displayed
  90  * for example using {@code ex.printStackTrace()}) of both the thread
  91  * that initiated the computation as well as the thread actually
  92  * encountering the exception; minimally only the latter.
  93  *
  94  * <p>It is possible to define and use ForkJoinTasks that may block,
  95  * but doing do requires three further considerations: (1) Completion
  96  * of few if any <em>other</em> tasks should be dependent on a task
  97  * that blocks on external synchronization or I/O. Event-style async
  98  * tasks that are never joined (for example, those subclassing {@link
  99  * CountedCompleter}) often fall into this category.  (2) To minimize
 100  * resource impact, tasks should be small; ideally performing only the
 101  * (possibly) blocking action. (3) Unless the {@link
 102  * ForkJoinPool.ManagedBlocker} API is used, or the number of possibly
 103  * blocked tasks is known to be less than the pool's {@link
 104  * ForkJoinPool#getParallelism} level, the pool cannot guarantee that
 105  * enough threads will be available to ensure progress or good
 106  * performance.
 107  *
 108  * <p>The primary method for awaiting completion and extracting
 109  * results of a task is {@link #join}, but there are several variants:
 110  * The {@link Future#get} methods support interruptible and/or timed
 111  * waits for completion and report results using {@code Future}
 112  * conventions. Method {@link #invoke} is semantically
 113  * equivalent to {@code fork(); join()} but always attempts to begin
 114  * execution in the current thread. The "<em>quiet</em>" forms of
 115  * these methods do not extract results or report exceptions. These
 116  * may be useful when a set of tasks are being executed, and you need
 117  * to delay processing of results or exceptions until all complete.
 118  * Method {@code invokeAll} (available in multiple versions)
 119  * performs the most common form of parallel invocation: forking a set
 120  * of tasks and joining them all.
 121  *
 122  * <p>In the most typical usages, a fork-join pair act like a call
 123  * (fork) and return (join) from a parallel recursive function. As is
 124  * the case with other forms of recursive calls, returns (joins)
 125  * should be performed innermost-first. For example, {@code a.fork();
 126  * b.fork(); b.join(); a.join();} is likely to be substantially more
 127  * efficient than joining {@code a} before {@code b}.
 128  *
 129  * <p>The execution status of tasks may be queried at several levels
 130  * of detail: {@link #isDone} is true if a task completed in any way
 131  * (including the case where a task was cancelled without executing);
 132  * {@link #isCompletedNormally} is true if a task completed without
 133  * cancellation or encountering an exception; {@link #isCancelled} is
 134  * true if the task was cancelled (in which case {@link #getException}
 135  * returns a {@link java.util.concurrent.CancellationException}); and
 136  * {@link #isCompletedAbnormally} is true if a task was either
 137  * cancelled or encountered an exception, in which case {@link
 138  * #getException} will return either the encountered exception or
 139  * {@link java.util.concurrent.CancellationException}.
 140  *
 141  * <p>The ForkJoinTask class is not usually directly subclassed.
 142  * Instead, you subclass one of the abstract classes that support a
 143  * particular style of fork/join processing, typically {@link
 144  * RecursiveAction} for most computations that do not return results,
 145  * {@link RecursiveTask} for those that do, and {@link
 146  * CountedCompleter} for those in which completed actions trigger
 147  * other actions.  Normally, a concrete ForkJoinTask subclass declares
 148  * fields comprising its parameters, established in a constructor, and
 149  * then defines a {@code compute} method that somehow uses the control
 150  * methods supplied by this base class.
 151  *
 152  * <p>Method {@link #join} and its variants are appropriate for use
 153  * only when completion dependencies are acyclic; that is, the
 154  * parallel computation can be described as a directed acyclic graph
 155  * (DAG). Otherwise, executions may encounter a form of deadlock as
 156  * tasks cyclically wait for each other.  However, this framework
 157  * supports other methods and techniques (for example the use of
 158  * {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
 159  * may be of use in constructing custom subclasses for problems that
 160  * are not statically structured as DAGs. To support such usages, a
 161  * ForkJoinTask may be atomically <em>tagged</em> with a {@code short}
 162  * value using {@link #setForkJoinTaskTag} or {@link
 163  * #compareAndSetForkJoinTaskTag} and checked using {@link
 164  * #getForkJoinTaskTag}. The ForkJoinTask implementation does not use
 165  * these {@code protected} methods or tags for any purpose, but they
 166  * may be of use in the construction of specialized subclasses.  For
 167  * example, parallel graph traversals can use the supplied methods to
 168  * avoid revisiting nodes/tasks that have already been processed.
 169  * (Method names for tagging are bulky in part to encourage definition
 170  * of methods that reflect their usage patterns.)
 171  *
 172  * <p>Most base support methods are {@code final}, to prevent
 173  * overriding of implementations that are intrinsically tied to the
 174  * underlying lightweight task scheduling framework.  Developers
 175  * creating new basic styles of fork/join processing should minimally
 176  * implement {@code protected} methods {@link #exec}, {@link
 177  * #setRawResult}, and {@link #getRawResult}, while also introducing
 178  * an abstract computational method that can be implemented in its
 179  * subclasses, possibly relying on other {@code protected} methods
 180  * provided by this class.
 181  *
 182  * <p>ForkJoinTasks should perform relatively small amounts of
 183  * computation. Large tasks should be split into smaller subtasks,
 184  * usually via recursive decomposition. As a very rough rule of thumb,
 185  * a task should perform more than 100 and less than 10000 basic
 186  * computational steps, and should avoid indefinite looping. If tasks
 187  * are too big, then parallelism cannot improve throughput. If too
 188  * small, then memory and internal task maintenance overhead may
 189  * overwhelm processing.
 190  *
 191  * <p>This class provides {@code adapt} methods for {@link Runnable}
 192  * and {@link Callable}, that may be of use when mixing execution of
 193  * {@code ForkJoinTasks} with other kinds of tasks. When all tasks are
 194  * of this form, consider using a pool constructed in <em>asyncMode</em>.
 195  *
 196  * <p>ForkJoinTasks are {@code Serializable}, which enables them to be
 197  * used in extensions such as remote execution frameworks. It is
 198  * sensible to serialize tasks only before or after, but not during,
 199  * execution. Serialization is not relied on during execution itself.
 200  *
 201  * @since 1.7
 202  * @author Doug Lea
 203  */
 204 public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
 205 
 206     /*
 207      * See the internal documentation of class ForkJoinPool for a
 208      * general implementation overview.  ForkJoinTasks are mainly
 209      * responsible for maintaining their "status" field amidst relays
 210      * to methods in ForkJoinWorkerThread and ForkJoinPool.
 211      *
 212      * The methods of this class are more-or-less layered into
 213      * (1) basic status maintenance
 214      * (2) execution and awaiting completion
 215      * (3) user-level methods that additionally report results.
 216      * This is sometimes hard to see because this file orders exported
 217      * methods in a way that flows well in javadocs.
 218      */
 219 
 220     /*
 221      * The status field holds run control status bits packed into a
 222      * single int to minimize footprint and to ensure atomicity (via
 223      * CAS).  Status is initially zero, and takes on nonnegative
 224      * values until completed, upon which status (anded with
 225      * DONE_MASK) holds value NORMAL, CANCELLED, or EXCEPTIONAL. Tasks
 226      * undergoing blocking waits by other threads have the SIGNAL bit
 227      * set.  Completion of a stolen task with SIGNAL set awakens any
 228      * waiters via notifyAll. Even though suboptimal for some
 229      * purposes, we use basic builtin wait/notify to take advantage of
 230      * "monitor inflation" in JVMs that we would otherwise need to
 231      * emulate to avoid adding further per-task bookkeeping overhead.
 232      * We want these monitors to be "fat", i.e., not use biasing or
 233      * thin-lock techniques, so use some odd coding idioms that tend
 234      * to avoid them, mainly by arranging that every synchronized
 235      * block performs a wait, notifyAll or both.
 236      *
 237      * These control bits occupy only (some of) the upper half (16
 238      * bits) of status field. The lower bits are used for user-defined
 239      * tags.
 240      */
 241 
 242     /** The run status of this task */
 243     volatile int status; // accessed directly by pool and workers
 244     static final int DONE_MASK   = 0xf0000000;  // mask out non-completion bits
 245     static final int NORMAL      = 0xf0000000;  // must be negative
 246     static final int CANCELLED   = 0xc0000000;  // must be < NORMAL
 247     static final int EXCEPTIONAL = 0x80000000;  // must be < CANCELLED
 248     static final int SIGNAL      = 0x00010000;  // must be >= 1 << 16
 249     static final int SMASK       = 0x0000ffff;  // short bits for tags
 250 
 251     /**
 252      * Marks completion and wakes up threads waiting to join this
 253      * task.
 254      *
 255      * @param completion one of NORMAL, CANCELLED, EXCEPTIONAL
 256      * @return completion status on exit
 257      */
 258     private int setCompletion(int completion) {
 259         for (int s;;) {
 260             if ((s = status) < 0)
 261                 return s;
 262             if (U.compareAndSwapInt(this, STATUS, s, s | completion)) {
 263                 if ((s >>> 16) != 0)
 264                     synchronized (this) { notifyAll(); }
 265                 return completion;
 266             }
 267         }
 268     }
 269 
 270     /**
 271      * Primary execution method for stolen tasks. Unless done, calls
 272      * exec and records status if completed, but doesn't wait for
 273      * completion otherwise.
 274      *
 275      * @return status on exit from this method
 276      */
 277     final int doExec() {
 278         int s; boolean completed;
 279         if ((s = status) >= 0) {
 280             try {
 281                 completed = exec();
 282             } catch (Throwable rex) {
 283                 return setExceptionalCompletion(rex);
 284             }
 285             if (completed)
 286                 s = setCompletion(NORMAL);
 287         }
 288         return s;
 289     }
 290 
 291     /**
 292      * If not done, sets SIGNAL status and performs Object.wait(timeout).
 293      * This task may or may not be done on exit. Ignores interrupts.
 294      *
 295      * @param timeout using Object.wait conventions.
 296      */
 297     final void internalWait(long timeout) {
 298         int s;
 299         if ((s = status) >= 0 && // force completer to issue notify
 300             U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
 301             synchronized (this) {
 302                 if (status >= 0)
 303                     try { wait(timeout); } catch (InterruptedException ie) { }
 304                 else
 305                     notifyAll();
 306             }
 307         }
 308     }
 309 
 310     /**
 311      * Blocks a non-worker-thread until completion.
 312      * @return status upon completion
 313      */
 314     private int externalAwaitDone() {
 315         int s = ((this instanceof CountedCompleter) ? // try helping
 316                  ForkJoinPool.common.externalHelpComplete(
 317                      (CountedCompleter<?>)this, 0) :
 318                  ForkJoinPool.common.tryExternalUnpush(this) ? doExec() : 0);
 319         if (s >= 0 && (s = status) >= 0) {
 320             boolean interrupted = false;
 321             do {
 322                 if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
 323                     synchronized (this) {
 324                         if (status >= 0) {
 325                             try {
 326                                 wait(0L);
 327                             } catch (InterruptedException ie) {
 328                                 interrupted = true;
 329                             }
 330                         }
 331                         else
 332                             notifyAll();
 333                     }
 334                 }
 335             } while ((s = status) >= 0);
 336             if (interrupted)
 337                 Thread.currentThread().interrupt();
 338         }
 339         return s;
 340     }
 341 
 342     /**
 343      * Blocks a non-worker-thread until completion or interruption.
 344      */
 345     private int externalInterruptibleAwaitDone() throws InterruptedException {
 346         int s;
 347         if (Thread.interrupted())
 348             throw new InterruptedException();
 349         if ((s = status) >= 0 &&
 350             (s = ((this instanceof CountedCompleter) ?
 351                   ForkJoinPool.common.externalHelpComplete(
 352                       (CountedCompleter<?>)this, 0) :
 353                   ForkJoinPool.common.tryExternalUnpush(this) ? doExec() :
 354                   0)) >= 0) {
 355             while ((s = status) >= 0) {
 356                 if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
 357                     synchronized (this) {
 358                         if (status >= 0)
 359                             wait(0L);
 360                         else
 361                             notifyAll();
 362                     }
 363                 }
 364             }
 365         }
 366         return s;
 367     }
 368 
 369     /**
 370      * Implementation for join, get, quietlyJoin. Directly handles
 371      * only cases of already-completed, external wait, and
 372      * unfork+exec.  Others are relayed to ForkJoinPool.awaitJoin.
 373      *
 374      * @return status upon completion
 375      */
 376     private int doJoin() {
 377         int s; Thread t; ForkJoinWorkerThread wt; ForkJoinPool.WorkQueue w;
 378         return (s = status) < 0 ? s :
 379             ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
 380             (w = (wt = (ForkJoinWorkerThread)t).workQueue).
 381             tryUnpush(this) && (s = doExec()) < 0 ? s :
 382             wt.pool.awaitJoin(w, this, 0L) :
 383             externalAwaitDone();
 384     }
 385 
 386     /**
 387      * Implementation for invoke, quietlyInvoke.
 388      *
 389      * @return status upon completion
 390      */
 391     private int doInvoke() {
 392         int s; Thread t; ForkJoinWorkerThread wt;
 393         return (s = doExec()) < 0 ? s :
 394             ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
 395             (wt = (ForkJoinWorkerThread)t).pool.
 396             awaitJoin(wt.workQueue, this, 0L) :
 397             externalAwaitDone();
 398     }
 399 
 400     // Exception table support
 401 
 402     /**
 403      * Table of exceptions thrown by tasks, to enable reporting by
 404      * callers. Because exceptions are rare, we don't directly keep
 405      * them with task objects, but instead use a weak ref table.  Note
 406      * that cancellation exceptions don't appear in the table, but are
 407      * instead recorded as status values.
 408      *
 409      * Note: These statics are initialized below in static block.
 410      */
 411     private static final ExceptionNode[] exceptionTable;
 412     private static final ReentrantLock exceptionTableLock;
 413     private static final ReferenceQueue<Object> exceptionTableRefQueue;
 414 
 415     /**
 416      * Fixed capacity for exceptionTable.
 417      */
 418     private static final int EXCEPTION_MAP_CAPACITY = 32;
 419 
 420     /**
 421      * Key-value nodes for exception table.  The chained hash table
 422      * uses identity comparisons, full locking, and weak references
 423      * for keys. The table has a fixed capacity because it only
 424      * maintains task exceptions long enough for joiners to access
 425      * them, so should never become very large for sustained
 426      * periods. However, since we do not know when the last joiner
 427      * completes, we must use weak references and expunge them. We do
 428      * so on each operation (hence full locking). Also, some thread in
 429      * any ForkJoinPool will call helpExpungeStaleExceptions when its
 430      * pool becomes isQuiescent.
 431      */
 432     static final class ExceptionNode extends WeakReference<ForkJoinTask<?>> {
 433         final Throwable ex;
 434         ExceptionNode next;
 435         final long thrower;  // use id not ref to avoid weak cycles
 436         final int hashCode;  // store task hashCode before weak ref disappears
 437         ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next,
 438                       ReferenceQueue<Object> exceptionTableRefQueue) {
 439             super(task, exceptionTableRefQueue);
 440             this.ex = ex;
 441             this.next = next;
 442             this.thrower = Thread.currentThread().getId();
 443             this.hashCode = System.identityHashCode(task);
 444         }
 445     }
 446 
 447     /**
 448      * Records exception and sets status.
 449      *
 450      * @return status on exit
 451      */
 452     final int recordExceptionalCompletion(Throwable ex) {
 453         int s;
 454         if ((s = status) >= 0) {
 455             int h = System.identityHashCode(this);
 456             final ReentrantLock lock = exceptionTableLock;
 457             lock.lock();
 458             try {
 459                 expungeStaleExceptions();
 460                 ExceptionNode[] t = exceptionTable;
 461                 int i = h & (t.length - 1);
 462                 for (ExceptionNode e = t[i]; ; e = e.next) {
 463                     if (e == null) {
 464                         t[i] = new ExceptionNode(this, ex, t[i],
 465                                                  exceptionTableRefQueue);
 466                         break;
 467                     }
 468                     if (e.get() == this) // already present
 469                         break;
 470                 }
 471             } finally {
 472                 lock.unlock();
 473             }
 474             s = setCompletion(EXCEPTIONAL);
 475         }
 476         return s;
 477     }
 478 
 479     /**
 480      * Records exception and possibly propagates.
 481      *
 482      * @return status on exit
 483      */
 484     private int setExceptionalCompletion(Throwable ex) {
 485         int s = recordExceptionalCompletion(ex);
 486         if ((s & DONE_MASK) == EXCEPTIONAL)
 487             internalPropagateException(ex);
 488         return s;
 489     }
 490 
 491     /**
 492      * Hook for exception propagation support for tasks with completers.
 493      */
 494     void internalPropagateException(Throwable ex) {
 495     }
 496 
 497     /**
 498      * Cancels, ignoring any exceptions thrown by cancel. Used during
 499      * worker and pool shutdown. Cancel is spec'ed not to throw any
 500      * exceptions, but if it does anyway, we have no recourse during
 501      * shutdown, so guard against this case.
 502      */
 503     static final void cancelIgnoringExceptions(ForkJoinTask<?> t) {
 504         if (t != null && t.status >= 0) {
 505             try {
 506                 t.cancel(false);
 507             } catch (Throwable ignore) {
 508             }
 509         }
 510     }
 511 
 512     /**
 513      * Removes exception node and clears status.
 514      */
 515     private void clearExceptionalCompletion() {
 516         int h = System.identityHashCode(this);
 517         final ReentrantLock lock = exceptionTableLock;
 518         lock.lock();
 519         try {
 520             ExceptionNode[] t = exceptionTable;
 521             int i = h & (t.length - 1);
 522             ExceptionNode e = t[i];
 523             ExceptionNode pred = null;
 524             while (e != null) {
 525                 ExceptionNode next = e.next;
 526                 if (e.get() == this) {
 527                     if (pred == null)
 528                         t[i] = next;
 529                     else
 530                         pred.next = next;
 531                     break;
 532                 }
 533                 pred = e;
 534                 e = next;
 535             }
 536             expungeStaleExceptions();
 537             status = 0;
 538         } finally {
 539             lock.unlock();
 540         }
 541     }
 542 
 543     /**
 544      * Returns a rethrowable exception for the given task, if
 545      * available. To provide accurate stack traces, if the exception
 546      * was not thrown by the current thread, we try to create a new
 547      * exception of the same type as the one thrown, but with the
 548      * recorded exception as its cause. If there is no such
 549      * constructor, we instead try to use a no-arg constructor,
 550      * followed by initCause, to the same effect. If none of these
 551      * apply, or any fail due to other exceptions, we return the
 552      * recorded exception, which is still correct, although it may
 553      * contain a misleading stack trace.
 554      *
 555      * @return the exception, or null if none
 556      */
 557     private Throwable getThrowableException() {
 558         int h = System.identityHashCode(this);
 559         ExceptionNode e;
 560         final ReentrantLock lock = exceptionTableLock;
 561         lock.lock();
 562         try {
 563             expungeStaleExceptions();
 564             ExceptionNode[] t = exceptionTable;
 565             e = t[h & (t.length - 1)];
 566             while (e != null && e.get() != this)
 567                 e = e.next;
 568         } finally {
 569             lock.unlock();
 570         }
 571         Throwable ex;
 572         if (e == null || (ex = e.ex) == null)
 573             return null;
 574         if (e.thrower != Thread.currentThread().getId()) {
 575             Class<? extends Throwable> ec = ex.getClass();
 576             try {
 577                 Constructor<?> noArgCtor = null;
 578                 Constructor<?>[] cs = ec.getConstructors();// public ctors only
 579                 for (int i = 0; i < cs.length; ++i) {
 580                     Constructor<?> c = cs[i];
 581                     Class<?>[] ps = c.getParameterTypes();
 582                     if (ps.length == 0)
 583                         noArgCtor = c;
 584                     else if (ps.length == 1 && ps[0] == Throwable.class) {
 585                         Throwable wx = (Throwable)c.newInstance(ex);
 586                         return (wx == null) ? ex : wx;
 587                     }
 588                 }
 589                 if (noArgCtor != null) {
 590                     Throwable wx = (Throwable)(noArgCtor.newInstance());
 591                     if (wx != null) {
 592                         wx.initCause(ex);
 593                         return wx;
 594                     }
 595                 }
 596             } catch (Exception ignore) {
 597             }
 598         }
 599         return ex;
 600     }
 601 
 602     /**
 603      * Polls stale refs and removes them. Call only while holding lock.
 604      */
 605     private static void expungeStaleExceptions() {
 606         for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
 607             if (x instanceof ExceptionNode) {
 608                 int hashCode = ((ExceptionNode)x).hashCode;
 609                 ExceptionNode[] t = exceptionTable;
 610                 int i = hashCode & (t.length - 1);
 611                 ExceptionNode e = t[i];
 612                 ExceptionNode pred = null;
 613                 while (e != null) {
 614                     ExceptionNode next = e.next;
 615                     if (e == x) {
 616                         if (pred == null)
 617                             t[i] = next;
 618                         else
 619                             pred.next = next;
 620                         break;
 621                     }
 622                     pred = e;
 623                     e = next;
 624                 }
 625             }
 626         }
 627     }
 628 
 629     /**
 630      * If lock is available, polls stale refs and removes them.
 631      * Called from ForkJoinPool when pools become quiescent.
 632      */
 633     static final void helpExpungeStaleExceptions() {
 634         final ReentrantLock lock = exceptionTableLock;
 635         if (lock.tryLock()) {
 636             try {
 637                 expungeStaleExceptions();
 638             } finally {
 639                 lock.unlock();
 640             }
 641         }
 642     }
 643 
 644     /**
 645      * A version of "sneaky throw" to relay exceptions.
 646      */
 647     static void rethrow(Throwable ex) {
 648         ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
 649     }
 650 
 651     /**
 652      * The sneaky part of sneaky throw, relying on generics
 653      * limitations to evade compiler complaints about rethrowing
 654      * unchecked exceptions.
 655      */
 656     @SuppressWarnings("unchecked") static <T extends Throwable>
 657     void uncheckedThrow(Throwable t) throws T {
 658         if (t != null)
 659             throw (T)t; // rely on vacuous cast
 660         else
 661             throw new Error("Unknown Exception");
 662     }
 663 
 664     /**
 665      * Throws exception, if any, associated with the given status.
 666      */
 667     private void reportException(int s) {
 668         if (s == CANCELLED)
 669             throw new CancellationException();
 670         if (s == EXCEPTIONAL)
 671             rethrow(getThrowableException());
 672     }
 673 
 674     // public methods
 675 
 676     /**
 677      * Arranges to asynchronously execute this task in the pool the
 678      * current task is running in, if applicable, or using the {@link
 679      * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
 680      * it is not necessarily enforced, it is a usage error to fork a
 681      * task more than once unless it has completed and been
 682      * reinitialized.  Subsequent modifications to the state of this
 683      * task or any data it operates on are not necessarily
 684      * consistently observable by any thread other than the one
 685      * executing it unless preceded by a call to {@link #join} or
 686      * related methods, or a call to {@link #isDone} returning {@code
 687      * true}.
 688      *
 689      * @return {@code this}, to simplify usage
 690      */
 691     public final ForkJoinTask<V> fork() {
 692         Thread t;
 693         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
 694             ((ForkJoinWorkerThread)t).workQueue.push(this);
 695         else
 696             ForkJoinPool.common.externalPush(this);
 697         return this;
 698     }
 699 
 700     /**
 701      * Returns the result of the computation when it {@link #isDone is
 702      * done}.  This method differs from {@link #get()} in that
 703      * abnormal completion results in {@code RuntimeException} or
 704      * {@code Error}, not {@code ExecutionException}, and that
 705      * interrupts of the calling thread do <em>not</em> cause the
 706      * method to abruptly return by throwing {@code
 707      * InterruptedException}.
 708      *
 709      * @return the computed result
 710      */
 711     public final V join() {
 712         int s;
 713         if ((s = doJoin() & DONE_MASK) != NORMAL)
 714             reportException(s);
 715         return getRawResult();
 716     }
 717 
 718     /**
 719      * Commences performing this task, awaits its completion if
 720      * necessary, and returns its result, or throws an (unchecked)
 721      * {@code RuntimeException} or {@code Error} if the underlying
 722      * computation did so.
 723      *
 724      * @return the computed result
 725      */
 726     public final V invoke() {
 727         int s;
 728         if ((s = doInvoke() & DONE_MASK) != NORMAL)
 729             reportException(s);
 730         return getRawResult();
 731     }
 732 
 733     /**
 734      * Forks the given tasks, returning when {@code isDone} holds for
 735      * each task or an (unchecked) exception is encountered, in which
 736      * case the exception is rethrown. If more than one task
 737      * encounters an exception, then this method throws any one of
 738      * these exceptions. If any task encounters an exception, the
 739      * other may be cancelled. However, the execution status of
 740      * individual tasks is not guaranteed upon exceptional return. The
 741      * status of each task may be obtained using {@link
 742      * #getException()} and related methods to check if they have been
 743      * cancelled, completed normally or exceptionally, or left
 744      * unprocessed.
 745      *
 746      * @param t1 the first task
 747      * @param t2 the second task
 748      * @throws NullPointerException if any task is null
 749      */
 750     public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) {
 751         int s1, s2;
 752         t2.fork();
 753         if ((s1 = t1.doInvoke() & DONE_MASK) != NORMAL)
 754             t1.reportException(s1);
 755         if ((s2 = t2.doJoin() & DONE_MASK) != NORMAL)
 756             t2.reportException(s2);
 757     }
 758 
 759     /**
 760      * Forks the given tasks, returning when {@code isDone} holds for
 761      * each task or an (unchecked) exception is encountered, in which
 762      * case the exception is rethrown. If more than one task
 763      * encounters an exception, then this method throws any one of
 764      * these exceptions. If any task encounters an exception, others
 765      * may be cancelled. However, the execution status of individual
 766      * tasks is not guaranteed upon exceptional return. The status of
 767      * each task may be obtained using {@link #getException()} and
 768      * related methods to check if they have been cancelled, completed
 769      * normally or exceptionally, or left unprocessed.
 770      *
 771      * @param tasks the tasks
 772      * @throws NullPointerException if any task is null
 773      */
 774     public static void invokeAll(ForkJoinTask<?>... tasks) {
 775         Throwable ex = null;
 776         int last = tasks.length - 1;
 777         for (int i = last; i >= 0; --i) {
 778             ForkJoinTask<?> t = tasks[i];
 779             if (t == null) {
 780                 if (ex == null)
 781                     ex = new NullPointerException();
 782             }
 783             else if (i != 0)
 784                 t.fork();
 785             else if (t.doInvoke() < NORMAL && ex == null)
 786                 ex = t.getException();
 787         }
 788         for (int i = 1; i <= last; ++i) {
 789             ForkJoinTask<?> t = tasks[i];
 790             if (t != null) {
 791                 if (ex != null)
 792                     t.cancel(false);
 793                 else if (t.doJoin() < NORMAL)
 794                     ex = t.getException();
 795             }
 796         }
 797         if (ex != null)
 798             rethrow(ex);
 799     }
 800 
 801     /**
 802      * Forks all tasks in the specified collection, returning when
 803      * {@code isDone} holds for each task or an (unchecked) exception
 804      * is encountered, in which case the exception is rethrown. If
 805      * more than one task encounters an exception, then this method
 806      * throws any one of these exceptions. If any task encounters an
 807      * exception, others may be cancelled. However, the execution
 808      * status of individual tasks is not guaranteed upon exceptional
 809      * return. The status of each task may be obtained using {@link
 810      * #getException()} and related methods to check if they have been
 811      * cancelled, completed normally or exceptionally, or left
 812      * unprocessed.
 813      *
 814      * @param tasks the collection of tasks
 815      * @param <T> the type of the values returned from the tasks
 816      * @return the tasks argument, to simplify usage
 817      * @throws NullPointerException if tasks or any element are null
 818      */
 819     public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) {
 820         if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) {
 821             invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
 822             return tasks;
 823         }
 824         @SuppressWarnings("unchecked")
 825         List<? extends ForkJoinTask<?>> ts =
 826             (List<? extends ForkJoinTask<?>>) tasks;
 827         Throwable ex = null;
 828         int last = ts.size() - 1;
 829         for (int i = last; i >= 0; --i) {
 830             ForkJoinTask<?> t = ts.get(i);
 831             if (t == null) {
 832                 if (ex == null)
 833                     ex = new NullPointerException();
 834             }
 835             else if (i != 0)
 836                 t.fork();
 837             else if (t.doInvoke() < NORMAL && ex == null)
 838                 ex = t.getException();
 839         }
 840         for (int i = 1; i <= last; ++i) {
 841             ForkJoinTask<?> t = ts.get(i);
 842             if (t != null) {
 843                 if (ex != null)
 844                     t.cancel(false);
 845                 else if (t.doJoin() < NORMAL)
 846                     ex = t.getException();
 847             }
 848         }
 849         if (ex != null)
 850             rethrow(ex);
 851         return tasks;
 852     }
 853 
 854     /**
 855      * Attempts to cancel execution of this task. This attempt will
 856      * fail if the task has already completed or could not be
 857      * cancelled for some other reason. If successful, and this task
 858      * has not started when {@code cancel} is called, execution of
 859      * this task is suppressed. After this method returns
 860      * successfully, unless there is an intervening call to {@link
 861      * #reinitialize}, subsequent calls to {@link #isCancelled},
 862      * {@link #isDone}, and {@code cancel} will return {@code true}
 863      * and calls to {@link #join} and related methods will result in
 864      * {@code CancellationException}.
 865      *
 866      * <p>This method may be overridden in subclasses, but if so, must
 867      * still ensure that these properties hold. In particular, the
 868      * {@code cancel} method itself must not throw exceptions.
 869      *
 870      * <p>This method is designed to be invoked by <em>other</em>
 871      * tasks. To terminate the current task, you can just return or
 872      * throw an unchecked exception from its computation method, or
 873      * invoke {@link #completeExceptionally(Throwable)}.
 874      *
 875      * @param mayInterruptIfRunning this value has no effect in the
 876      * default implementation because interrupts are not used to
 877      * control cancellation.
 878      *
 879      * @return {@code true} if this task is now cancelled
 880      */
 881     public boolean cancel(boolean mayInterruptIfRunning) {
 882         return (setCompletion(CANCELLED) & DONE_MASK) == CANCELLED;
 883     }
 884 
 885     public final boolean isDone() {
 886         return status < 0;
 887     }
 888 
 889     public final boolean isCancelled() {
 890         return (status & DONE_MASK) == CANCELLED;
 891     }
 892 
 893     /**
 894      * Returns {@code true} if this task threw an exception or was cancelled.
 895      *
 896      * @return {@code true} if this task threw an exception or was cancelled
 897      */
 898     public final boolean isCompletedAbnormally() {
 899         return status < NORMAL;
 900     }
 901 
 902     /**
 903      * Returns {@code true} if this task completed without throwing an
 904      * exception and was not cancelled.
 905      *
 906      * @return {@code true} if this task completed without throwing an
 907      * exception and was not cancelled
 908      */
 909     public final boolean isCompletedNormally() {
 910         return (status & DONE_MASK) == NORMAL;
 911     }
 912 
 913     /**
 914      * Returns the exception thrown by the base computation, or a
 915      * {@code CancellationException} if cancelled, or {@code null} if
 916      * none or if the method has not yet completed.
 917      *
 918      * @return the exception, or {@code null} if none
 919      */
 920     public final Throwable getException() {
 921         int s = status & DONE_MASK;
 922         return ((s >= NORMAL)    ? null :
 923                 (s == CANCELLED) ? new CancellationException() :
 924                 getThrowableException());
 925     }
 926 
 927     /**
 928      * Completes this task abnormally, and if not already aborted or
 929      * cancelled, causes it to throw the given exception upon
 930      * {@code join} and related operations. This method may be used
 931      * to induce exceptions in asynchronous tasks, or to force
 932      * completion of tasks that would not otherwise complete.  Its use
 933      * in other situations is discouraged.  This method is
 934      * overridable, but overridden versions must invoke {@code super}
 935      * implementation to maintain guarantees.
 936      *
 937      * @param ex the exception to throw. If this exception is not a
 938      * {@code RuntimeException} or {@code Error}, the actual exception
 939      * thrown will be a {@code RuntimeException} with cause {@code ex}.
 940      */
 941     public void completeExceptionally(Throwable ex) {
 942         setExceptionalCompletion((ex instanceof RuntimeException) ||
 943                                  (ex instanceof Error) ? ex :
 944                                  new RuntimeException(ex));
 945     }
 946 
 947     /**
 948      * Completes this task, and if not already aborted or cancelled,
 949      * returning the given value as the result of subsequent
 950      * invocations of {@code join} and related operations. This method
 951      * may be used to provide results for asynchronous tasks, or to
 952      * provide alternative handling for tasks that would not otherwise
 953      * complete normally. Its use in other situations is
 954      * discouraged. This method is overridable, but overridden
 955      * versions must invoke {@code super} implementation to maintain
 956      * guarantees.
 957      *
 958      * @param value the result value for this task
 959      */
 960     public void complete(V value) {
 961         try {
 962             setRawResult(value);
 963         } catch (Throwable rex) {
 964             setExceptionalCompletion(rex);
 965             return;
 966         }
 967         setCompletion(NORMAL);
 968     }
 969 
 970     /**
 971      * Completes this task normally without setting a value. The most
 972      * recent value established by {@link #setRawResult} (or {@code
 973      * null} by default) will be returned as the result of subsequent
 974      * invocations of {@code join} and related operations.
 975      *
 976      * @since 1.8
 977      */
 978     public final void quietlyComplete() {
 979         setCompletion(NORMAL);
 980     }
 981 
 982     /**
 983      * Waits if necessary for the computation to complete, and then
 984      * retrieves its result.
 985      *
 986      * @return the computed result
 987      * @throws CancellationException if the computation was cancelled
 988      * @throws ExecutionException if the computation threw an
 989      * exception
 990      * @throws InterruptedException if the current thread is not a
 991      * member of a ForkJoinPool and was interrupted while waiting
 992      */
 993     public final V get() throws InterruptedException, ExecutionException {
 994         int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ?
 995             doJoin() : externalInterruptibleAwaitDone();
 996         if ((s &= DONE_MASK) == CANCELLED)
 997             throw new CancellationException();
 998         if (s == EXCEPTIONAL)
 999             throw new ExecutionException(getThrowableException());
1000         return getRawResult();
1001     }
1002 
1003     /**
1004      * Waits if necessary for at most the given time for the computation
1005      * to complete, and then retrieves its result, if available.
1006      *
1007      * @param timeout the maximum time to wait
1008      * @param unit the time unit of the timeout argument
1009      * @return the computed result
1010      * @throws CancellationException if the computation was cancelled
1011      * @throws ExecutionException if the computation threw an
1012      * exception
1013      * @throws InterruptedException if the current thread is not a
1014      * member of a ForkJoinPool and was interrupted while waiting
1015      * @throws TimeoutException if the wait timed out
1016      */
1017     public final V get(long timeout, TimeUnit unit)
1018         throws InterruptedException, ExecutionException, TimeoutException {
1019         int s;
1020         long nanos = unit.toNanos(timeout);
1021         if (Thread.interrupted())
1022             throw new InterruptedException();
1023         if ((s = status) >= 0 && nanos > 0L) {
1024             long d = System.nanoTime() + nanos;
1025             long deadline = (d == 0L) ? 1L : d; // avoid 0
1026             Thread t = Thread.currentThread();
1027             if (t instanceof ForkJoinWorkerThread) {
1028                 ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
1029                 s = wt.pool.awaitJoin(wt.workQueue, this, deadline);
1030             }
1031             else if ((s = ((this instanceof CountedCompleter) ?
1032                            ForkJoinPool.common.externalHelpComplete(
1033                                (CountedCompleter<?>)this, 0) :
1034                            ForkJoinPool.common.tryExternalUnpush(this) ?
1035                            doExec() : 0)) >= 0) {
1036                 long ns, ms; // measure in nanosecs, but wait in millisecs
1037                 while ((s = status) >= 0 &&
1038                        (ns = deadline - System.nanoTime()) > 0L) {
1039                     if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L &&
1040                         U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
1041                         synchronized (this) {
1042                             if (status >= 0)
1043                                 wait(ms); // OK to throw InterruptedException
1044                             else
1045                                 notifyAll();
1046                         }
1047                     }
1048                 }
1049             }
1050         }
1051         if (s >= 0)
1052             s = status;
1053         if ((s &= DONE_MASK) != NORMAL) {
1054             if (s == CANCELLED)
1055                 throw new CancellationException();
1056             if (s != EXCEPTIONAL)
1057                 throw new TimeoutException();
1058             throw new ExecutionException(getThrowableException());
1059         }
1060         return getRawResult();
1061     }
1062 
1063     /**
1064      * Joins this task, without returning its result or throwing its
1065      * exception. This method may be useful when processing
1066      * collections of tasks when some have been cancelled or otherwise
1067      * known to have aborted.
1068      */
1069     public final void quietlyJoin() {
1070         doJoin();
1071     }
1072 
1073     /**
1074      * Commences performing this task and awaits its completion if
1075      * necessary, without returning its result or throwing its
1076      * exception.
1077      */
1078     public final void quietlyInvoke() {
1079         doInvoke();
1080     }
1081 
1082     /**
1083      * Possibly executes tasks until the pool hosting the current task
1084      * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
1085      * method may be of use in designs in which many tasks are forked,
1086      * but none are explicitly joined, instead executing them until
1087      * all are processed.
1088      */
1089     public static void helpQuiesce() {
1090         Thread t;
1091         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
1092             ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
1093             wt.pool.helpQuiescePool(wt.workQueue);
1094         }
1095         else
1096             ForkJoinPool.quiesceCommonPool();
1097     }
1098 
1099     /**
1100      * Resets the internal bookkeeping state of this task, allowing a
1101      * subsequent {@code fork}. This method allows repeated reuse of
1102      * this task, but only if reuse occurs when this task has either
1103      * never been forked, or has been forked, then completed and all
1104      * outstanding joins of this task have also completed. Effects
1105      * under any other usage conditions are not guaranteed.
1106      * This method may be useful when executing
1107      * pre-constructed trees of subtasks in loops.
1108      *
1109      * <p>Upon completion of this method, {@code isDone()} reports
1110      * {@code false}, and {@code getException()} reports {@code
1111      * null}. However, the value returned by {@code getRawResult} is
1112      * unaffected. To clear this value, you can invoke {@code
1113      * setRawResult(null)}.
1114      */
1115     public void reinitialize() {
1116         if ((status & DONE_MASK) == EXCEPTIONAL)
1117             clearExceptionalCompletion();
1118         else
1119             status = 0;
1120     }
1121 
1122     /**
1123      * Returns the pool hosting the current thread, or {@code null}
1124      * if the current thread is executing outside of any ForkJoinPool.
1125      *
1126      * <p>This method returns {@code null} if and only if {@link
1127      * #inForkJoinPool} returns {@code false}.
1128      *
1129      * @return the pool, or {@code null} if none
1130      */
1131     public static ForkJoinPool getPool() {
1132         Thread t = Thread.currentThread();
1133         return (t instanceof ForkJoinWorkerThread) ?
1134             ((ForkJoinWorkerThread) t).pool : null;
1135     }
1136 
1137     /**
1138      * Returns {@code true} if the current thread is a {@link
1139      * ForkJoinWorkerThread} executing as a ForkJoinPool computation.
1140      *
1141      * @return {@code true} if the current thread is a {@link
1142      * ForkJoinWorkerThread} executing as a ForkJoinPool computation,
1143      * or {@code false} otherwise
1144      */
1145     public static boolean inForkJoinPool() {
1146         return Thread.currentThread() instanceof ForkJoinWorkerThread;
1147     }
1148 
1149     /**
1150      * Tries to unschedule this task for execution. This method will
1151      * typically (but is not guaranteed to) succeed if this task is
1152      * the most recently forked task by the current thread, and has
1153      * not commenced executing in another thread.  This method may be
1154      * useful when arranging alternative local processing of tasks
1155      * that could have been, but were not, stolen.
1156      *
1157      * @return {@code true} if unforked
1158      */
1159     public boolean tryUnfork() {
1160         Thread t;
1161         return (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1162                 ((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) :
1163                 ForkJoinPool.common.tryExternalUnpush(this));
1164     }
1165 
1166     /**
1167      * Returns an estimate of the number of tasks that have been
1168      * forked by the current worker thread but not yet executed. This
1169      * value may be useful for heuristic decisions about whether to
1170      * fork other tasks.
1171      *
1172      * @return the number of tasks
1173      */
1174     public static int getQueuedTaskCount() {
1175         Thread t; ForkJoinPool.WorkQueue q;
1176         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
1177             q = ((ForkJoinWorkerThread)t).workQueue;
1178         else
1179             q = ForkJoinPool.commonSubmitterQueue();
1180         return (q == null) ? 0 : q.queueSize();
1181     }
1182 
1183     /**
1184      * Returns an estimate of how many more locally queued tasks are
1185      * held by the current worker thread than there are other worker
1186      * threads that might steal them, or zero if this thread is not
1187      * operating in a ForkJoinPool. This value may be useful for
1188      * heuristic decisions about whether to fork other tasks. In many
1189      * usages of ForkJoinTasks, at steady state, each worker should
1190      * aim to maintain a small constant surplus (for example, 3) of
1191      * tasks, and to process computations locally if this threshold is
1192      * exceeded.
1193      *
1194      * @return the surplus number of tasks, which may be negative
1195      */
1196     public static int getSurplusQueuedTaskCount() {
1197         return ForkJoinPool.getSurplusQueuedTaskCount();
1198     }
1199 
1200     // Extension methods
1201 
1202     /**
1203      * Returns the result that would be returned by {@link #join}, even
1204      * if this task completed abnormally, or {@code null} if this task
1205      * is not known to have been completed.  This method is designed
1206      * to aid debugging, as well as to support extensions. Its use in
1207      * any other context is discouraged.
1208      *
1209      * @return the result, or {@code null} if not completed
1210      */
1211     public abstract V getRawResult();
1212 
1213     /**
1214      * Forces the given value to be returned as a result.  This method
1215      * is designed to support extensions, and should not in general be
1216      * called otherwise.
1217      *
1218      * @param value the value
1219      */
1220     protected abstract void setRawResult(V value);
1221 
1222     /**
1223      * Immediately performs the base action of this task and returns
1224      * true if, upon return from this method, this task is guaranteed
1225      * to have completed normally. This method may return false
1226      * otherwise, to indicate that this task is not necessarily
1227      * complete (or is not known to be complete), for example in
1228      * asynchronous actions that require explicit invocations of
1229      * completion methods. This method may also throw an (unchecked)
1230      * exception to indicate abnormal exit. This method is designed to
1231      * support extensions, and should not in general be called
1232      * otherwise.
1233      *
1234      * @return {@code true} if this task is known to have completed normally
1235      */
1236     protected abstract boolean exec();
1237 
1238     /**
1239      * Returns, but does not unschedule or execute, a task queued by
1240      * the current thread but not yet executed, if one is immediately
1241      * available. There is no guarantee that this task will actually
1242      * be polled or executed next. Conversely, this method may return
1243      * null even if a task exists but cannot be accessed without
1244      * contention with other threads.  This method is designed
1245      * primarily to support extensions, and is unlikely to be useful
1246      * otherwise.
1247      *
1248      * @return the next task, or {@code null} if none are available
1249      */
1250     protected static ForkJoinTask<?> peekNextLocalTask() {
1251         Thread t; ForkJoinPool.WorkQueue q;
1252         if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
1253             q = ((ForkJoinWorkerThread)t).workQueue;
1254         else
1255             q = ForkJoinPool.commonSubmitterQueue();
1256         return (q == null) ? null : q.peek();
1257     }
1258 
1259     /**
1260      * Unschedules and returns, without executing, the next task
1261      * queued by the current thread but not yet executed, if the
1262      * current thread is operating in a ForkJoinPool.  This method is
1263      * designed primarily to support extensions, and is unlikely to be
1264      * useful otherwise.
1265      *
1266      * @return the next task, or {@code null} if none are available
1267      */
1268     protected static ForkJoinTask<?> pollNextLocalTask() {
1269         Thread t;
1270         return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1271             ((ForkJoinWorkerThread)t).workQueue.nextLocalTask() :
1272             null;
1273     }
1274 
1275     /**
1276      * If the current thread is operating in a ForkJoinPool,
1277      * unschedules and returns, without executing, the next task
1278      * queued by the current thread but not yet executed, if one is
1279      * available, or if not available, a task that was forked by some
1280      * other thread, if available. Availability may be transient, so a
1281      * {@code null} result does not necessarily imply quiescence of
1282      * the pool this task is operating in.  This method is designed
1283      * primarily to support extensions, and is unlikely to be useful
1284      * otherwise.
1285      *
1286      * @return a task, or {@code null} if none are available
1287      */
1288     protected static ForkJoinTask<?> pollTask() {
1289         Thread t; ForkJoinWorkerThread wt;
1290         return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1291             (wt = (ForkJoinWorkerThread)t).pool.nextTaskFor(wt.workQueue) :
1292             null;
1293     }
1294 
1295     /**
1296      * If the current thread is operating in a ForkJoinPool,
1297      * unschedules and returns, without executing, a task externally
1298      * submitted to the pool, if one is available. Availability may be
1299      * transient, so a {@code null} result does not necessarily imply
1300      * quiescence of the pool.  This method is designed primarily to
1301      * support extensions, and is unlikely to be useful otherwise.
1302      *
1303      * @return a task, or {@code null} if none are available
1304      * @since 1.9
1305      */
1306     protected static ForkJoinTask<?> pollSubmission() {
1307         Thread t;
1308         return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1309             ((ForkJoinWorkerThread)t).pool.pollSubmission() : null;
1310     }
1311 
1312     // tag operations
1313 
1314     /**
1315      * Returns the tag for this task.
1316      *
1317      * @return the tag for this task
1318      * @since 1.8
1319      */
1320     public final short getForkJoinTaskTag() {
1321         return (short)status;
1322     }
1323 
1324     /**
1325      * Atomically sets the tag value for this task and returns the old value.
1326      *
1327      * @param newValue the new tag value
1328      * @return the previous value of the tag
1329      * @since 1.8
1330      */
1331     public final short setForkJoinTaskTag(short newValue) {
1332         for (int s;;) {
1333             if (U.compareAndSwapInt(this, STATUS, s = status,
1334                                     (s & ~SMASK) | (newValue & SMASK)))
1335                 return (short)s;
1336         }
1337     }
1338 
1339     /**
1340      * Atomically conditionally sets the tag value for this task.
1341      * Among other applications, tags can be used as visit markers
1342      * in tasks operating on graphs, as in methods that check: {@code
1343      * if (task.compareAndSetForkJoinTaskTag((short)0, (short)1))}
1344      * before processing, otherwise exiting because the node has
1345      * already been visited.
1346      *
1347      * @param expect the expected tag value
1348      * @param update the new tag value
1349      * @return {@code true} if successful; i.e., the current value was
1350      * equal to {@code expect} and was changed to {@code update}.
1351      * @since 1.8
1352      */
1353     public final boolean compareAndSetForkJoinTaskTag(short expect, short update) {
1354         for (int s;;) {
1355             if ((short)(s = status) != expect)
1356                 return false;
1357             if (U.compareAndSwapInt(this, STATUS, s,
1358                                     (s & ~SMASK) | (update & SMASK)))
1359                 return true;
1360         }
1361     }
1362 
1363     /**
1364      * Adapter for Runnables. This implements RunnableFuture
1365      * to be compliant with AbstractExecutorService constraints
1366      * when used in ForkJoinPool.
1367      */
1368     static final class AdaptedRunnable<T> extends ForkJoinTask<T>
1369         implements RunnableFuture<T> {
1370         final Runnable runnable;
1371         T result;
1372         AdaptedRunnable(Runnable runnable, T result) {
1373             if (runnable == null) throw new NullPointerException();
1374             this.runnable = runnable;
1375             this.result = result; // OK to set this even before completion
1376         }
1377         public final T getRawResult() { return result; }
1378         public final void setRawResult(T v) { result = v; }
1379         public final boolean exec() { runnable.run(); return true; }
1380         public final void run() { invoke(); }
1381         private static final long serialVersionUID = 5232453952276885070L;
1382     }
1383 
1384     /**
1385      * Adapter for Runnables without results.
1386      */
1387     static final class AdaptedRunnableAction extends ForkJoinTask<Void>
1388         implements RunnableFuture<Void> {
1389         final Runnable runnable;
1390         AdaptedRunnableAction(Runnable runnable) {
1391             if (runnable == null) throw new NullPointerException();
1392             this.runnable = runnable;
1393         }
1394         public final Void getRawResult() { return null; }
1395         public final void setRawResult(Void v) { }
1396         public final boolean exec() { runnable.run(); return true; }
1397         public final void run() { invoke(); }
1398         private static final long serialVersionUID = 5232453952276885070L;
1399     }
1400 
1401     /**
1402      * Adapter for Runnables in which failure forces worker exception.
1403      */
1404     static final class RunnableExecuteAction extends ForkJoinTask<Void> {
1405         final Runnable runnable;
1406         RunnableExecuteAction(Runnable runnable) {
1407             if (runnable == null) throw new NullPointerException();
1408             this.runnable = runnable;
1409         }
1410         public final Void getRawResult() { return null; }
1411         public final void setRawResult(Void v) { }
1412         public final boolean exec() { runnable.run(); return true; }
1413         void internalPropagateException(Throwable ex) {
1414             rethrow(ex); // rethrow outside exec() catches.
1415         }
1416         private static final long serialVersionUID = 5232453952276885070L;
1417     }
1418 
1419     /**
1420      * Adapter for Callables.
1421      */
1422     static final class AdaptedCallable<T> extends ForkJoinTask<T>
1423         implements RunnableFuture<T> {
1424         final Callable<? extends T> callable;
1425         T result;
1426         AdaptedCallable(Callable<? extends T> callable) {
1427             if (callable == null) throw new NullPointerException();
1428             this.callable = callable;
1429         }
1430         public final T getRawResult() { return result; }
1431         public final void setRawResult(T v) { result = v; }
1432         public final boolean exec() {
1433             try {
1434                 result = callable.call();
1435                 return true;
1436             } catch (RuntimeException rex) {
1437                 throw rex;
1438             } catch (Exception ex) {
1439                 throw new RuntimeException(ex);
1440             }
1441         }
1442         public final void run() { invoke(); }
1443         private static final long serialVersionUID = 2838392045355241008L;
1444     }
1445 
1446     /**
1447      * Returns a new {@code ForkJoinTask} that performs the {@code run}
1448      * method of the given {@code Runnable} as its action, and returns
1449      * a null result upon {@link #join}.
1450      *
1451      * @param runnable the runnable action
1452      * @return the task
1453      */
1454     public static ForkJoinTask<?> adapt(Runnable runnable) {
1455         return new AdaptedRunnableAction(runnable);
1456     }
1457 
1458     /**
1459      * Returns a new {@code ForkJoinTask} that performs the {@code run}
1460      * method of the given {@code Runnable} as its action, and returns
1461      * the given result upon {@link #join}.
1462      *
1463      * @param runnable the runnable action
1464      * @param result the result upon completion
1465      * @param <T> the type of the result
1466      * @return the task
1467      */
1468     public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
1469         return new AdaptedRunnable<T>(runnable, result);
1470     }
1471 
1472     /**
1473      * Returns a new {@code ForkJoinTask} that performs the {@code call}
1474      * method of the given {@code Callable} as its action, and returns
1475      * its result upon {@link #join}, translating any checked exceptions
1476      * encountered into {@code RuntimeException}.
1477      *
1478      * @param callable the callable action
1479      * @param <T> the type of the callable's result
1480      * @return the task
1481      */
1482     public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) {
1483         return new AdaptedCallable<T>(callable);
1484     }
1485 
1486     // Serialization support
1487 
1488     private static final long serialVersionUID = -7721805057305804111L;
1489 
1490     /**
1491      * Saves this task to a stream (that is, serializes it).
1492      *
1493      * @param s the stream
1494      * @throws java.io.IOException if an I/O error occurs
1495      * @serialData the current run status and the exception thrown
1496      * during execution, or {@code null} if none
1497      */
1498     private void writeObject(java.io.ObjectOutputStream s)
1499         throws java.io.IOException {
1500         s.defaultWriteObject();
1501         s.writeObject(getException());
1502     }
1503 
1504     /**
1505      * Reconstitutes this task from a stream (that is, deserializes it).
1506      * @param s the stream
1507      * @throws ClassNotFoundException if the class of a serialized object
1508      *         could not be found
1509      * @throws java.io.IOException if an I/O error occurs
1510      */
1511     private void readObject(java.io.ObjectInputStream s)
1512         throws java.io.IOException, ClassNotFoundException {
1513         s.defaultReadObject();
1514         Object ex = s.readObject();
1515         if (ex != null)
1516             setExceptionalCompletion((Throwable)ex);
1517     }
1518 
1519     // Unsafe mechanics
1520     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
1521     private static final long STATUS;
1522 
1523     static {
1524         exceptionTableLock = new ReentrantLock();
1525         exceptionTableRefQueue = new ReferenceQueue<Object>();
1526         exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY];
1527         try {
1528             STATUS = U.objectFieldOffset
1529                 (ForkJoinTask.class.getDeclaredField("status"));
1530         } catch (ReflectiveOperationException e) {
1531             throw new Error(e);
1532         }
1533     }
1534 
1535 }