1a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson/*
229957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
329957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer *
429957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * This code is free software; you can redistribute it and/or modify it
529957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * under the terms of the GNU General Public License version 2 only, as
629957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * published by the Free Software Foundation.  Oracle designates this
729957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * particular file as subject to the "Classpath" exception as provided
829957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * by Oracle in the LICENSE file that accompanied this code.
929957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer *
1029957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * This code is distributed in the hope that it will be useful, but WITHOUT
1129957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1229957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1329957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * version 2 for more details (a copy is included in the LICENSE file that
1429957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * accompanied this code).
1529957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer *
1629957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * You should have received a copy of the GNU General Public License version
1729957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * 2 along with this work; if not, write to the Free Software Foundation,
1829957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1929957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer *
2029957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2129957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * or visit www.oracle.com if you need additional information or have any
2229957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * questions.
2329957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer */
2429957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer
2529957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer/*
2629957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * This file is available under and governed by the GNU General Public
2729957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * License version 2 only, as published by the Free Software Foundation.
2829957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * However, the following notice accompanied the original version of this
2929957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer * file:
3029957558cf0db700bfaae360a80c42dc3871d0e5Tobias Thierer *
31a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * Written by Doug Lea with assistance from members of JCP JSR-166
32a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * Expert Group and released to the public domain, as explained at
33a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * http://creativecommons.org/publicdomain/zero/1.0/
34a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson */
35a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
36a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonpackage java.util.concurrent;
37a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
38a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonimport java.io.Serializable;
39b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniakimport java.lang.ref.ReferenceQueue;
40b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniakimport java.lang.ref.WeakReference;
41b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniakimport java.lang.reflect.Constructor;
42a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonimport java.util.Collection;
43a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonimport java.util.List;
44a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonimport java.util.RandomAccess;
45a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonimport java.util.concurrent.locks.ReentrantLock;
46b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak
47b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak// BEGIN android-note
48b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak// removed java 9 code
49b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak// END android-note
50a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
51a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson/**
52a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * Abstract base class for tasks that run within a {@link ForkJoinPool}.
53a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * A {@code ForkJoinTask} is a thread-like entity that is much
54a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * lighter weight than a normal thread.  Huge numbers of tasks and
55a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * subtasks may be hosted by a small number of actual threads in a
56a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * ForkJoinPool, at the price of some usage limitations.
57a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
5891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <p>A "main" {@code ForkJoinTask} begins execution when it is
5991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * explicitly submitted to a {@link ForkJoinPool}, or, if not already
60b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * engaged in a ForkJoin computation, commenced in the {@link
61b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or
6291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * related methods.  Once started, it will usually in turn start other
6391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * subtasks.  As indicated by the name of this class, many programs
6491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * using {@code ForkJoinTask} employ only methods {@link #fork} and
6591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * {@link #join}, or derivatives such as {@link
66a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * #invokeAll(ForkJoinTask...) invokeAll}.  However, this class also
67a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * provides a number of other methods that can come into play in
6891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * advanced usages, as well as extension mechanics that allow support
6991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * of new forms of fork/join processing.
70a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
71a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}.
72a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * The efficiency of {@code ForkJoinTask}s stems from a set of
73a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * restrictions (that are only partially statically enforceable)
7491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * reflecting their main use as computational tasks calculating pure
7591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * functions or operating on purely isolated objects.  The primary
7691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * coordination mechanisms are {@link #fork}, that arranges
77a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * asynchronous execution, and {@link #join}, that doesn't proceed
78a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * until the task's result has been computed.  Computations should
7991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * ideally avoid {@code synchronized} methods or blocks, and should
8091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * minimize other blocking synchronization apart from joining other
8191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * tasks or using synchronizers such as Phasers that are advertised to
8291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * cooperate with fork/join scheduling. Subdividable tasks should also
8391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * not perform blocking I/O, and should ideally access variables that
8491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * are completely independent of those accessed by other running
8591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * tasks. These guidelines are loosely enforced by not permitting
8691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * checked exceptions such as {@code IOExceptions} to be
8791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * thrown. However, computations may still encounter unchecked
8891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * exceptions, that are rethrown to callers attempting to join
8991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * them. These exceptions may additionally include {@link
9091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * RejectedExecutionException} stemming from internal resource
9191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * exhaustion, such as failure to allocate internal task
9291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * queues. Rethrown exceptions behave in the same way as regular
9391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * exceptions, but, when possible, contain stack traces (as displayed
9491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * for example using {@code ex.printStackTrace()}) of both the thread
9591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * that initiated the computation as well as the thread actually
9691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * encountering the exception; minimally only the latter.
9791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle *
9891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <p>It is possible to define and use ForkJoinTasks that may block,
9991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * but doing do requires three further considerations: (1) Completion
10091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * of few if any <em>other</em> tasks should be dependent on a task
10191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * that blocks on external synchronization or I/O. Event-style async
102b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * tasks that are never joined (for example, those subclassing {@link
103b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * CountedCompleter}) often fall into this category.  (2) To minimize
104b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * resource impact, tasks should be small; ideally performing only the
105b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * (possibly) blocking action. (3) Unless the {@link
10691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * ForkJoinPool.ManagedBlocker} API is used, or the number of possibly
10791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * blocked tasks is known to be less than the pool's {@link
10891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * ForkJoinPool#getParallelism} level, the pool cannot guarantee that
10991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * enough threads will be available to ensure progress or good
11091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * performance.
111a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
112a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>The primary method for awaiting completion and extracting
113a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * results of a task is {@link #join}, but there are several variants:
114a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * The {@link Future#get} methods support interruptible and/or timed
115a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * waits for completion and report results using {@code Future}
116a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * conventions. Method {@link #invoke} is semantically
117a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * equivalent to {@code fork(); join()} but always attempts to begin
118a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * execution in the current thread. The "<em>quiet</em>" forms of
119a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * these methods do not extract results or report exceptions. These
120a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * may be useful when a set of tasks are being executed, and you need
121a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * to delay processing of results or exceptions until all complete.
122a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * Method {@code invokeAll} (available in multiple versions)
123a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * performs the most common form of parallel invocation: forking a set
124a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * of tasks and joining them all.
125a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
12691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * <p>In the most typical usages, a fork-join pair act like a call
12791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * (fork) and return (join) from a parallel recursive function. As is
12891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * the case with other forms of recursive calls, returns (joins)
12991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * should be performed innermost-first. For example, {@code a.fork();
13091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * b.fork(); b.join(); a.join();} is likely to be substantially more
13191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * efficient than joining {@code a} before {@code b}.
13291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle *
133a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>The execution status of tasks may be queried at several levels
134a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * of detail: {@link #isDone} is true if a task completed in any way
135a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * (including the case where a task was cancelled without executing);
136a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * {@link #isCompletedNormally} is true if a task completed without
137a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * cancellation or encountering an exception; {@link #isCancelled} is
138a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * true if the task was cancelled (in which case {@link #getException}
139a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * returns a {@link java.util.concurrent.CancellationException}); and
140a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * {@link #isCompletedAbnormally} is true if a task was either
141a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * cancelled or encountered an exception, in which case {@link
142a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * #getException} will return either the encountered exception or
143a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * {@link java.util.concurrent.CancellationException}.
144a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
145a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>The ForkJoinTask class is not usually directly subclassed.
146a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * Instead, you subclass one of the abstract classes that support a
147a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * particular style of fork/join processing, typically {@link
148b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * RecursiveAction} for most computations that do not return results,
149b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * {@link RecursiveTask} for those that do, and {@link
150b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * CountedCompleter} for those in which completed actions trigger
151b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * other actions.  Normally, a concrete ForkJoinTask subclass declares
152b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * fields comprising its parameters, established in a constructor, and
153b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * then defines a {@code compute} method that somehow uses the control
154b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * methods supplied by this base class.
155a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
156a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>Method {@link #join} and its variants are appropriate for use
157a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * only when completion dependencies are acyclic; that is, the
158a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * parallel computation can be described as a directed acyclic graph
159a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * (DAG). Otherwise, executions may encounter a form of deadlock as
160a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * tasks cyclically wait for each other.  However, this framework
161a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * supports other methods and techniques (for example the use of
162a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
163a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * may be of use in constructing custom subclasses for problems that
16475a06e56a4cc4599946e21422513e4bafa759509Calin Juravle * are not statically structured as DAGs. To support such usages, a
16591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * ForkJoinTask may be atomically <em>tagged</em> with a {@code short}
166b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * value using {@link #setForkJoinTaskTag} or {@link
167b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * #compareAndSetForkJoinTaskTag} and checked using {@link
168b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak * #getForkJoinTaskTag}. The ForkJoinTask implementation does not use
16991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * these {@code protected} methods or tags for any purpose, but they
17091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * may be of use in the construction of specialized subclasses.  For
17191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * example, parallel graph traversals can use the supplied methods to
17291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * avoid revisiting nodes/tasks that have already been processed.
17391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * (Method names for tagging are bulky in part to encourage definition
17491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle * of methods that reflect their usage patterns.)
175a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
176a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>Most base support methods are {@code final}, to prevent
177a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * overriding of implementations that are intrinsically tied to the
178a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * underlying lightweight task scheduling framework.  Developers
179a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * creating new basic styles of fork/join processing should minimally
180a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * implement {@code protected} methods {@link #exec}, {@link
181a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * #setRawResult}, and {@link #getRawResult}, while also introducing
182a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * an abstract computational method that can be implemented in its
183a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * subclasses, possibly relying on other {@code protected} methods
184a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * provided by this class.
185a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
186a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>ForkJoinTasks should perform relatively small amounts of
187a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * computation. Large tasks should be split into smaller subtasks,
188a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * usually via recursive decomposition. As a very rough rule of thumb,
189a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * a task should perform more than 100 and less than 10000 basic
190a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * computational steps, and should avoid indefinite looping. If tasks
191a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * are too big, then parallelism cannot improve throughput. If too
192a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * small, then memory and internal task maintenance overhead may
193a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * overwhelm processing.
194a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
195a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>This class provides {@code adapt} methods for {@link Runnable}
196a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * and {@link Callable}, that may be of use when mixing execution of
197a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * {@code ForkJoinTasks} with other kinds of tasks. When all tasks are
198a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * of this form, consider using a pool constructed in <em>asyncMode</em>.
199a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
200a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * <p>ForkJoinTasks are {@code Serializable}, which enables them to be
201a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * used in extensions such as remote execution frameworks. It is
202a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * sensible to serialize tasks only before or after, but not during,
203a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * execution. Serialization is not relied on during execution itself.
204a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson *
205a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * @since 1.7
206a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson * @author Doug Lea
207a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson */
208a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilsonpublic abstract class ForkJoinTask<V> implements Future<V>, Serializable {
209a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
210a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /*
211a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * See the internal documentation of class ForkJoinPool for a
212a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * general implementation overview.  ForkJoinTasks are mainly
213a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * responsible for maintaining their "status" field amidst relays
214a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * to methods in ForkJoinWorkerThread and ForkJoinPool.
215a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
216a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * The methods of this class are more-or-less layered into
217a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * (1) basic status maintenance
218a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * (2) execution and awaiting completion
219a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * (3) user-level methods that additionally report results.
220a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * This is sometimes hard to see because this file orders exported
221a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * methods in a way that flows well in javadocs.
222a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
223a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
224a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /*
225a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * The status field holds run control status bits packed into a
226a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * single int to minimize footprint and to ensure atomicity (via
227a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * CAS).  Status is initially zero, and takes on nonnegative
22891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * values until completed, upon which status (anded with
22991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * DONE_MASK) holds value NORMAL, CANCELLED, or EXCEPTIONAL. Tasks
23091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * undergoing blocking waits by other threads have the SIGNAL bit
23191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * set.  Completion of a stolen task with SIGNAL set awakens any
23291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * waiters via notifyAll. Even though suboptimal for some
23391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * purposes, we use basic builtin wait/notify to take advantage of
23491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * "monitor inflation" in JVMs that we would otherwise need to
23591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * emulate to avoid adding further per-task bookkeeping overhead.
23691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * We want these monitors to be "fat", i.e., not use biasing or
23791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * thin-lock techniques, so use some odd coding idioms that tend
23891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * to avoid them, mainly by arranging that every synchronized
23991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * block performs a wait, notifyAll or both.
24091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
24191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * These control bits occupy only (some of) the upper half (16
24291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * bits) of status field. The lower bits are used for user-defined
24391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * tags.
244a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
245a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
246a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /** The run status of this task */
247a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    volatile int status; // accessed directly by pool and workers
24891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int DONE_MASK   = 0xf0000000;  // mask out non-completion bits
24991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int NORMAL      = 0xf0000000;  // must be negative
25091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int CANCELLED   = 0xc0000000;  // must be < NORMAL
25191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int EXCEPTIONAL = 0x80000000;  // must be < CANCELLED
25291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int SIGNAL      = 0x00010000;  // must be >= 1 << 16
25391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final int SMASK       = 0x0000ffff;  // short bits for tags
254a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
255a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
25691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Marks completion and wakes up threads waiting to join this
25791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * task.
258a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
259a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param completion one of NORMAL, CANCELLED, EXCEPTIONAL
260a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return completion status on exit
261a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
262a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private int setCompletion(int completion) {
263a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (int s;;) {
264a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if ((s = status) < 0)
265a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                return s;
26691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (U.compareAndSwapInt(this, STATUS, s, s | completion)) {
26791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                if ((s >>> 16) != 0)
268a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    synchronized (this) { notifyAll(); }
269a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                return completion;
270a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
271a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
272a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
273a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
274a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
27591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Primary execution method for stolen tasks. Unless done, calls
27691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * exec and records status if completed, but doesn't wait for
27791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * completion otherwise.
278a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
27991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @return status on exit from this method
280a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
28191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    final int doExec() {
28291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s; boolean completed;
28391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s = status) >= 0) {
28491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            try {
28591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                completed = exec();
28691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            } catch (Throwable rex) {
28791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                return setExceptionalCompletion(rex);
288a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
28991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (completed)
29091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                s = setCompletion(NORMAL);
291a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
29291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return s;
29391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
29491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
29591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
296b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * If not done, sets SIGNAL status and performs Object.wait(timeout).
297b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * This task may or may not be done on exit. Ignores interrupts.
29891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
299b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param timeout using Object.wait conventions.
30091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
301b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    final void internalWait(long timeout) {
302b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        int s;
303b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if ((s = status) >= 0 && // force completer to issue notify
304b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
305b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            synchronized (this) {
306b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                if (status >= 0)
307b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    try { wait(timeout); } catch (InterruptedException ie) { }
308b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                else
309b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    notifyAll();
310b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            }
311b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        }
312a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
313a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
314a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
315a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Blocks a non-worker-thread until completion.
316a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return status upon completion
317a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
318a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private int externalAwaitDone() {
319b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        int s = ((this instanceof CountedCompleter) ? // try helping
320b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                 ForkJoinPool.common.externalHelpComplete(
321b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                     (CountedCompleter<?>)this, 0) :
322b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                 ForkJoinPool.common.tryExternalUnpush(this) ? doExec() : 0);
323b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if (s >= 0 && (s = status) >= 0) {
324b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            boolean interrupted = false;
325b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            do {
326b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
327b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    synchronized (this) {
328b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        if (status >= 0) {
329b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            try {
330b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                wait(0L);
331b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            } catch (InterruptedException ie) {
332b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                interrupted = true;
33375a06e56a4cc4599946e21422513e4bafa759509Calin Juravle                            }
334a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        }
335b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        else
336b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            notifyAll();
337a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    }
338b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                }
339b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            } while ((s = status) >= 0);
340b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            if (interrupted)
341b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                Thread.currentThread().interrupt();
342a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
343a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return s;
344a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
345a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
346a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
34791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Blocks a non-worker-thread until completion or interruption.
348a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
34991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    private int externalInterruptibleAwaitDone() throws InterruptedException {
350a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int s;
351a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (Thread.interrupted())
352a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            throw new InterruptedException();
353b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if ((s = status) >= 0 &&
354b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            (s = ((this instanceof CountedCompleter) ?
355b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                  ForkJoinPool.common.externalHelpComplete(
356b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                      (CountedCompleter<?>)this, 0) :
357b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                  ForkJoinPool.common.tryExternalUnpush(this) ? doExec() :
358b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                  0)) >= 0) {
359b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            while ((s = status) >= 0) {
360b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                if (U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
361b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    synchronized (this) {
362b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        if (status >= 0)
363b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            wait(0L);
364b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        else
365b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            notifyAll();
366b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    }
367a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
368a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
369a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
370a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return s;
371a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
372a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
373a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
37491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Implementation for join, get, quietlyJoin. Directly handles
37591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * only cases of already-completed, external wait, and
37691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * unfork+exec.  Others are relayed to ForkJoinPool.awaitJoin.
37791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
378a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return status upon completion
379a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
380a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private int doJoin() {
38191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s; Thread t; ForkJoinWorkerThread wt; ForkJoinPool.WorkQueue w;
38291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (s = status) < 0 ? s :
38391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
38491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            (w = (wt = (ForkJoinWorkerThread)t).workQueue).
38591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            tryUnpush(this) && (s = doExec()) < 0 ? s :
386b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            wt.pool.awaitJoin(w, this, 0L) :
38791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            externalAwaitDone();
388a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
389a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
390a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
39191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Implementation for invoke, quietlyInvoke.
39291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
393a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return status upon completion
394a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
395a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private int doInvoke() {
39691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s; Thread t; ForkJoinWorkerThread wt;
39791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (s = doExec()) < 0 ? s :
39891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
399b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            (wt = (ForkJoinWorkerThread)t).pool.
400b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            awaitJoin(wt.workQueue, this, 0L) :
40191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            externalAwaitDone();
402a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
403a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
404a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    // Exception table support
405a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
406a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
407a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Table of exceptions thrown by tasks, to enable reporting by
408a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * callers. Because exceptions are rare, we don't directly keep
409a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * them with task objects, but instead use a weak ref table.  Note
410a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * that cancellation exceptions don't appear in the table, but are
411a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * instead recorded as status values.
412a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
413a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Note: These statics are initialized below in static block.
414a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
415a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static final ExceptionNode[] exceptionTable;
416a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static final ReentrantLock exceptionTableLock;
417a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static final ReferenceQueue<Object> exceptionTableRefQueue;
418a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
419a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
420a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Fixed capacity for exceptionTable.
421a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
422a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static final int EXCEPTION_MAP_CAPACITY = 32;
423a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
424a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
425a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Key-value nodes for exception table.  The chained hash table
426a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * uses identity comparisons, full locking, and weak references
427a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * for keys. The table has a fixed capacity because it only
428a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * maintains task exceptions long enough for joiners to access
429a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * them, so should never become very large for sustained
430a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * periods. However, since we do not know when the last joiner
431a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * completes, we must use weak references and expunge them. We do
432a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * so on each operation (hence full locking). Also, some thread in
433a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * any ForkJoinPool will call helpExpungeStaleExceptions when its
434a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * pool becomes isQuiescent.
435a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
43691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final class ExceptionNode extends WeakReference<ForkJoinTask<?>> {
437a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final Throwable ex;
438a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        ExceptionNode next;
439a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final long thrower;  // use id not ref to avoid weak cycles
440edf43d27e240d82106f39ae91404963c23987234Narayan Kamath        final int hashCode;  // store task hashCode before weak ref disappears
441b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next,
442b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                      ReferenceQueue<Object> exceptionTableRefQueue) {
443a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            super(task, exceptionTableRefQueue);
444a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            this.ex = ex;
445a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            this.next = next;
446a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            this.thrower = Thread.currentThread().getId();
447edf43d27e240d82106f39ae91404963c23987234Narayan Kamath            this.hashCode = System.identityHashCode(task);
448a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
449a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
450a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
451a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
45291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Records exception and sets status.
453a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
454a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return status on exit
455a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
45691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    final int recordExceptionalCompletion(Throwable ex) {
45791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s;
45891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s = status) >= 0) {
45991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            int h = System.identityHashCode(this);
46091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            final ReentrantLock lock = exceptionTableLock;
46191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            lock.lock();
46291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            try {
46391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                expungeStaleExceptions();
46491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                ExceptionNode[] t = exceptionTable;
46591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                int i = h & (t.length - 1);
46691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                for (ExceptionNode e = t[i]; ; e = e.next) {
46791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                    if (e == null) {
468b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        t[i] = new ExceptionNode(this, ex, t[i],
469b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                                 exceptionTableRefQueue);
47091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                        break;
47191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                    }
47291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                    if (e.get() == this) // already present
47391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                        break;
474a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
47591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            } finally {
47691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                lock.unlock();
47791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            }
47891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            s = setCompletion(EXCEPTIONAL);
47991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        }
48091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return s;
48191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
48291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
48391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
48491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Records exception and possibly propagates.
48591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
48691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @return status on exit
48791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
48891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    private int setExceptionalCompletion(Throwable ex) {
48991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s = recordExceptionalCompletion(ex);
49091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s & DONE_MASK) == EXCEPTIONAL)
49191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            internalPropagateException(ex);
49291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return s;
49391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
49491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
49591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
49691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Hook for exception propagation support for tasks with completers.
49791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
49891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    void internalPropagateException(Throwable ex) {
49991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
50091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
50191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
50291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Cancels, ignoring any exceptions thrown by cancel. Used during
50391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * worker and pool shutdown. Cancel is spec'ed not to throw any
50491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * exceptions, but if it does anyway, we have no recourse during
50591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * shutdown, so guard against this case.
50691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
50791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final void cancelIgnoringExceptions(ForkJoinTask<?> t) {
50891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if (t != null && t.status >= 0) {
50991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            try {
51091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                t.cancel(false);
51191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            } catch (Throwable ignore) {
512a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
513a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
514a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
515a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
516a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
51791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Removes exception node and clears status.
518a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
519a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private void clearExceptionalCompletion() {
520a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int h = System.identityHashCode(this);
521a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final ReentrantLock lock = exceptionTableLock;
522a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        lock.lock();
523a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        try {
524a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ExceptionNode[] t = exceptionTable;
525a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            int i = h & (t.length - 1);
526a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ExceptionNode e = t[i];
527a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ExceptionNode pred = null;
528a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            while (e != null) {
529a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ExceptionNode next = e.next;
530a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (e.get() == this) {
531a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    if (pred == null)
532a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        t[i] = next;
533a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    else
534a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        pred.next = next;
535a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    break;
536a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
537a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                pred = e;
538a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                e = next;
539a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
540a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            expungeStaleExceptions();
541a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            status = 0;
542a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        } finally {
543a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            lock.unlock();
544a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
545a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
546a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
547a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
548b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Returns a rethrowable exception for this task, if available.
549b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * To provide accurate stack traces, if the exception was not
550b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * thrown by the current thread, we try to create a new exception
551b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * of the same type as the one thrown, but with the recorded
552b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * exception as its cause. If there is no such constructor, we
553b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * instead try to use a no-arg constructor, followed by initCause,
554b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * to the same effect. If none of these apply, or any fail due to
555b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * other exceptions, we return the recorded exception, which is
556b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * still correct, although it may contain a misleading stack
557b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * trace.
558a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
559a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the exception, or null if none
560a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
561a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private Throwable getThrowableException() {
562a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int h = System.identityHashCode(this);
563a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        ExceptionNode e;
564a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final ReentrantLock lock = exceptionTableLock;
565a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        lock.lock();
566a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        try {
567a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            expungeStaleExceptions();
568a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ExceptionNode[] t = exceptionTable;
569a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            e = t[h & (t.length - 1)];
570a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            while (e != null && e.get() != this)
571a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                e = e.next;
572a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        } finally {
573a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            lock.unlock();
574a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
575a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        Throwable ex;
576a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (e == null || (ex = e.ex) == null)
577a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            return null;
578b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if (e.thrower != Thread.currentThread().getId()) {
579a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            try {
580a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                Constructor<?> noArgCtor = null;
581b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                // public ctors only
582b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                for (Constructor<?> c : ex.getClass().getConstructors()) {
583a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    Class<?>[] ps = c.getParameterTypes();
584a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    if (ps.length == 0)
585a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        noArgCtor = c;
586a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    else if (ps.length == 1 && ps[0] == Throwable.class)
587b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        return (Throwable)c.newInstance(ex);
588a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
589a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (noArgCtor != null) {
590b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    Throwable wx = (Throwable)noArgCtor.newInstance();
591a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    wx.initCause(ex);
592a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    return wx;
593a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
594a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            } catch (Exception ignore) {
595a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
596a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
597a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return ex;
598a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
599a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
600a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
601b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Polls stale refs and removes them. Call only while holding lock.
602a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
603a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static void expungeStaleExceptions() {
604a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
605a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (x instanceof ExceptionNode) {
606edf43d27e240d82106f39ae91404963c23987234Narayan Kamath                int hashCode = ((ExceptionNode)x).hashCode;
607a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ExceptionNode[] t = exceptionTable;
608edf43d27e240d82106f39ae91404963c23987234Narayan Kamath                int i = hashCode & (t.length - 1);
609a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ExceptionNode e = t[i];
610a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ExceptionNode pred = null;
611a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                while (e != null) {
612a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    ExceptionNode next = e.next;
613a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    if (e == x) {
614a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        if (pred == null)
615a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                            t[i] = next;
616a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        else
617a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                            pred.next = next;
618a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                        break;
619a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    }
620a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    pred = e;
621a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    e = next;
622a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
623a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
624a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
625a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
626a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
627a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
628b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * If lock is available, polls stale refs and removes them.
629a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Called from ForkJoinPool when pools become quiescent.
630a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
631a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    static final void helpExpungeStaleExceptions() {
632a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final ReentrantLock lock = exceptionTableLock;
633a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (lock.tryLock()) {
634a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            try {
635a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                expungeStaleExceptions();
636a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            } finally {
637a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                lock.unlock();
638a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
639a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
640a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
641a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
642a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
643b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * A version of "sneaky throw" to relay exceptions.
64491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
64575a06e56a4cc4599946e21422513e4bafa759509Calin Juravle    static void rethrow(Throwable ex) {
646b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
64791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
64891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
64991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
65091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * The sneaky part of sneaky throw, relying on generics
65191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * limitations to evade compiler complaints about rethrowing
652b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * unchecked exceptions.
653a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
65491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    @SuppressWarnings("unchecked") static <T extends Throwable>
655b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    void uncheckedThrow(Throwable t) throws T {
656b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if (t != null)
657b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            throw (T)t; // rely on vacuous cast
658b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        else
659b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            throw new Error("Unknown Exception");
66091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
66191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
66291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
66391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Throws exception, if any, associated with the given status.
66491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
66591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    private void reportException(int s) {
66691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if (s == CANCELLED)
667a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            throw new CancellationException();
66891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if (s == EXCEPTIONAL)
66991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            rethrow(getThrowableException());
670a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
671a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
672a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    // public methods
673a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
674a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
67591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Arranges to asynchronously execute this task in the pool the
676b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * current task is running in, if applicable, or using the {@link
677b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
67891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * it is not necessarily enforced, it is a usage error to fork a
67991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * task more than once unless it has completed and been
68091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * reinitialized.  Subsequent modifications to the state of this
68191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * task or any data it operates on are not necessarily
68291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * consistently observable by any thread other than the one
68391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * executing it unless preceded by a call to {@link #join} or
68491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * related methods, or a call to {@link #isDone} returning {@code
68591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * true}.
686a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
687a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code this}, to simplify usage
688a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
689a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final ForkJoinTask<V> fork() {
69091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t;
69191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
69291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            ((ForkJoinWorkerThread)t).workQueue.push(this);
69391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        else
69475a06e56a4cc4599946e21422513e4bafa759509Calin Juravle            ForkJoinPool.common.externalPush(this);
695a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return this;
696a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
697a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
698a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
699a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns the result of the computation when it {@link #isDone is
700a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * done}.  This method differs from {@link #get()} in that
701a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * abnormal completion results in {@code RuntimeException} or
702a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code Error}, not {@code ExecutionException}, and that
703a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * interrupts of the calling thread do <em>not</em> cause the
704a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * method to abruptly return by throwing {@code
705a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * InterruptedException}.
706a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
707a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the computed result
708a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
709a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final V join() {
71091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s;
71191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s = doJoin() & DONE_MASK) != NORMAL)
71291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            reportException(s);
71391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return getRawResult();
714a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
715a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
716a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
717a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Commences performing this task, awaits its completion if
718a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * necessary, and returns its result, or throws an (unchecked)
719a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code RuntimeException} or {@code Error} if the underlying
720a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * computation did so.
721a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
722a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the computed result
723a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
724a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final V invoke() {
72591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s;
72691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s = doInvoke() & DONE_MASK) != NORMAL)
72791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            reportException(s);
72891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return getRawResult();
729a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
730a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
731a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
732a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Forks the given tasks, returning when {@code isDone} holds for
733a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * each task or an (unchecked) exception is encountered, in which
734a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * case the exception is rethrown. If more than one task
735a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * encounters an exception, then this method throws any one of
736a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * these exceptions. If any task encounters an exception, the
737a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * other may be cancelled. However, the execution status of
738a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * individual tasks is not guaranteed upon exceptional return. The
739a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * status of each task may be obtained using {@link
740a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * #getException()} and related methods to check if they have been
741a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * cancelled, completed normally or exceptionally, or left
742a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * unprocessed.
743a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
744a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param t1 the first task
745a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param t2 the second task
746a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws NullPointerException if any task is null
747a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
748a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) {
74991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s1, s2;
750a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        t2.fork();
75191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s1 = t1.doInvoke() & DONE_MASK) != NORMAL)
75291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            t1.reportException(s1);
75391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s2 = t2.doJoin() & DONE_MASK) != NORMAL)
75491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            t2.reportException(s2);
755a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
756a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
757a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
758a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Forks the given tasks, returning when {@code isDone} holds for
759a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * each task or an (unchecked) exception is encountered, in which
760a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * case the exception is rethrown. If more than one task
761a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * encounters an exception, then this method throws any one of
762a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * these exceptions. If any task encounters an exception, others
763a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * may be cancelled. However, the execution status of individual
764a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * tasks is not guaranteed upon exceptional return. The status of
765a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * each task may be obtained using {@link #getException()} and
766a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * related methods to check if they have been cancelled, completed
767a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * normally or exceptionally, or left unprocessed.
768a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
769a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param tasks the tasks
770a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws NullPointerException if any task is null
771a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
772a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static void invokeAll(ForkJoinTask<?>... tasks) {
773a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        Throwable ex = null;
774a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int last = tasks.length - 1;
775a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (int i = last; i >= 0; --i) {
776a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ForkJoinTask<?> t = tasks[i];
777a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (t == null) {
778a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (ex == null)
779a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    ex = new NullPointerException();
780a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
781a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            else if (i != 0)
782a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                t.fork();
783a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            else if (t.doInvoke() < NORMAL && ex == null)
784a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ex = t.getException();
785a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
786a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (int i = 1; i <= last; ++i) {
787a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ForkJoinTask<?> t = tasks[i];
788a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (t != null) {
789a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (ex != null)
790a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    t.cancel(false);
791a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                else if (t.doJoin() < NORMAL)
792a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    ex = t.getException();
793a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
794a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
795a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (ex != null)
79691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            rethrow(ex);
797a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
798a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
799a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
800a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Forks all tasks in the specified collection, returning when
801a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code isDone} holds for each task or an (unchecked) exception
802a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * is encountered, in which case the exception is rethrown. If
803a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * more than one task encounters an exception, then this method
804a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * throws any one of these exceptions. If any task encounters an
805a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception, others may be cancelled. However, the execution
806a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * status of individual tasks is not guaranteed upon exceptional
807a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * return. The status of each task may be obtained using {@link
808a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * #getException()} and related methods to check if they have been
809a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * cancelled, completed normally or exceptionally, or left
810a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * unprocessed.
811a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
812a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param tasks the collection of tasks
813fd81a99407d318ea3d8b5782294a0644e580d126Neil Fuller     * @param <T> the type of the values returned from the tasks
814a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the tasks argument, to simplify usage
815a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws NullPointerException if tasks or any element are null
816a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
817a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) {
818a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) {
819a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            invokeAll(tasks.toArray(new ForkJoinTask<?>[tasks.size()]));
820a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            return tasks;
821a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
822a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        @SuppressWarnings("unchecked")
823a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        List<? extends ForkJoinTask<?>> ts =
824a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            (List<? extends ForkJoinTask<?>>) tasks;
825a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        Throwable ex = null;
826a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int last = ts.size() - 1;
827a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (int i = last; i >= 0; --i) {
828a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ForkJoinTask<?> t = ts.get(i);
829a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (t == null) {
830a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (ex == null)
831a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    ex = new NullPointerException();
832a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
833a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            else if (i != 0)
834a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                t.fork();
835a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            else if (t.doInvoke() < NORMAL && ex == null)
836a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                ex = t.getException();
837a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
838a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        for (int i = 1; i <= last; ++i) {
839a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ForkJoinTask<?> t = ts.get(i);
840a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (t != null) {
841a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                if (ex != null)
842a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    t.cancel(false);
843a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                else if (t.doJoin() < NORMAL)
844a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    ex = t.getException();
845a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
846a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
847a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (ex != null)
84891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            rethrow(ex);
849a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return tasks;
850a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
851a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
852a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
853a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Attempts to cancel execution of this task. This attempt will
854a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * fail if the task has already completed or could not be
855a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * cancelled for some other reason. If successful, and this task
856a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * has not started when {@code cancel} is called, execution of
857a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * this task is suppressed. After this method returns
858a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * successfully, unless there is an intervening call to {@link
859a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * #reinitialize}, subsequent calls to {@link #isCancelled},
860a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@link #isDone}, and {@code cancel} will return {@code true}
861a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * and calls to {@link #join} and related methods will result in
862a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code CancellationException}.
863a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
864a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * <p>This method may be overridden in subclasses, but if so, must
865a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * still ensure that these properties hold. In particular, the
866a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code cancel} method itself must not throw exceptions.
867a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
868a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * <p>This method is designed to be invoked by <em>other</em>
869a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * tasks. To terminate the current task, you can just return or
870a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * throw an unchecked exception from its computation method, or
87175a06e56a4cc4599946e21422513e4bafa759509Calin Juravle     * invoke {@link #completeExceptionally(Throwable)}.
872a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
873a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param mayInterruptIfRunning this value has no effect in the
874a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * default implementation because interrupts are not used to
875a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * control cancellation.
876a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
877a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code true} if this task is now cancelled
878a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
879a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public boolean cancel(boolean mayInterruptIfRunning) {
88091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (setCompletion(CANCELLED) & DONE_MASK) == CANCELLED;
881a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
882a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
883a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final boolean isDone() {
884a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return status < 0;
885a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
886a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
887a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final boolean isCancelled() {
88891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (status & DONE_MASK) == CANCELLED;
889a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
890a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
891a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
892a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns {@code true} if this task threw an exception or was cancelled.
893a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
894a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code true} if this task threw an exception or was cancelled
895a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
896a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final boolean isCompletedAbnormally() {
897a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return status < NORMAL;
898a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
899a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
900a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
901a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns {@code true} if this task completed without throwing an
902a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception and was not cancelled.
903a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
904a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code true} if this task completed without throwing an
905a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception and was not cancelled
906a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
907a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final boolean isCompletedNormally() {
90891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (status & DONE_MASK) == NORMAL;
909a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
910a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
911a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
912a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns the exception thrown by the base computation, or a
913a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code CancellationException} if cancelled, or {@code null} if
914a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * none or if the method has not yet completed.
915a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
916a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the exception, or {@code null} if none
917a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
918a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final Throwable getException() {
91991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        int s = status & DONE_MASK;
920a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return ((s >= NORMAL)    ? null :
921a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                (s == CANCELLED) ? new CancellationException() :
922a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                getThrowableException());
923a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
924a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
925a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
926a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Completes this task abnormally, and if not already aborted or
927a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * cancelled, causes it to throw the given exception upon
928a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code join} and related operations. This method may be used
929a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * to induce exceptions in asynchronous tasks, or to force
930a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * completion of tasks that would not otherwise complete.  Its use
931a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * in other situations is discouraged.  This method is
932a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * overridable, but overridden versions must invoke {@code super}
933a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * implementation to maintain guarantees.
934a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
935a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param ex the exception to throw. If this exception is not a
936a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code RuntimeException} or {@code Error}, the actual exception
937a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * thrown will be a {@code RuntimeException} with cause {@code ex}.
938a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
939a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public void completeExceptionally(Throwable ex) {
940a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        setExceptionalCompletion((ex instanceof RuntimeException) ||
941a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                                 (ex instanceof Error) ? ex :
942a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                                 new RuntimeException(ex));
943a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
944a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
945a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
946a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Completes this task, and if not already aborted or cancelled,
947a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * returning the given value as the result of subsequent
948a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * invocations of {@code join} and related operations. This method
949a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * may be used to provide results for asynchronous tasks, or to
950a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * provide alternative handling for tasks that would not otherwise
951a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * complete normally. Its use in other situations is
952a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * discouraged. This method is overridable, but overridden
953a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * versions must invoke {@code super} implementation to maintain
954a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * guarantees.
955a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
956a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param value the result value for this task
957a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
958a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public void complete(V value) {
959a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        try {
960a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            setRawResult(value);
961a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        } catch (Throwable rex) {
962a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            setExceptionalCompletion(rex);
963a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            return;
964a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
965a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        setCompletion(NORMAL);
966a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
967a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
968a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
96991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Completes this task normally without setting a value. The most
97091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * recent value established by {@link #setRawResult} (or {@code
97191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * null} by default) will be returned as the result of subsequent
97291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * invocations of {@code join} and related operations.
97391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
97491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @since 1.8
97591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
97691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    public final void quietlyComplete() {
97791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        setCompletion(NORMAL);
97891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
97991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
98091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
981a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Waits if necessary for the computation to complete, and then
982a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * retrieves its result.
983a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
984a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the computed result
985a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws CancellationException if the computation was cancelled
986a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws ExecutionException if the computation threw an
987a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception
988a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws InterruptedException if the current thread is not a
989a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * member of a ForkJoinPool and was interrupted while waiting
990a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
991a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final V get() throws InterruptedException, ExecutionException {
992a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ?
99391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            doJoin() : externalInterruptibleAwaitDone();
99491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s &= DONE_MASK) == CANCELLED)
995a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            throw new CancellationException();
996b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if (s == EXCEPTIONAL)
997b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            throw new ExecutionException(getThrowableException());
998a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return getRawResult();
999a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1000a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1001a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1002a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Waits if necessary for at most the given time for the computation
1003a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * to complete, and then retrieves its result, if available.
1004a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1005a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param timeout the maximum time to wait
1006a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param unit the time unit of the timeout argument
1007a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the computed result
1008a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws CancellationException if the computation was cancelled
1009a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws ExecutionException if the computation threw an
1010a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception
1011a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws InterruptedException if the current thread is not a
1012a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * member of a ForkJoinPool and was interrupted while waiting
1013a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @throws TimeoutException if the wait timed out
1014a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1015a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final V get(long timeout, TimeUnit unit)
1016a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        throws InterruptedException, ExecutionException, TimeoutException {
1017b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        int s;
1018b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        long nanos = unit.toNanos(timeout);
101991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if (Thread.interrupted())
102091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            throw new InterruptedException();
1021b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if ((s = status) >= 0 && nanos > 0L) {
1022b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            long d = System.nanoTime() + nanos;
1023b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            long deadline = (d == 0L) ? 1L : d; // avoid 0
102491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            Thread t = Thread.currentThread();
102591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (t instanceof ForkJoinWorkerThread) {
102691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
1027b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                s = wt.pool.awaitJoin(wt.workQueue, this, deadline);
1028ed4f365789d43b1961657195df223a19bf4ef20fPrzemyslaw Szczepaniak            }
1029b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            else if ((s = ((this instanceof CountedCompleter) ?
1030b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                           ForkJoinPool.common.externalHelpComplete(
1031b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                               (CountedCompleter<?>)this, 0) :
1032b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                           ForkJoinPool.common.tryExternalUnpush(this) ?
1033b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                           doExec() : 0)) >= 0) {
1034b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                long ns, ms; // measure in nanosecs, but wait in millisecs
1035b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                while ((s = status) >= 0 &&
1036b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                       (ns = deadline - System.nanoTime()) > 0L) {
1037b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                    if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L &&
1038b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        U.compareAndSwapInt(this, STATUS, s, s | SIGNAL)) {
1039b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                        synchronized (this) {
1040b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            if (status >= 0)
1041b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                wait(ms); // OK to throw InterruptedException
1042b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                            else
1043b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                notifyAll();
104491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                        }
1045a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                    }
1046a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                }
1047a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
1048a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
1049b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        if (s >= 0)
1050b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            s = status;
105191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((s &= DONE_MASK) != NORMAL) {
1052a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (s == CANCELLED)
1053a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                throw new CancellationException();
1054a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (s != EXCEPTIONAL)
1055a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                throw new TimeoutException();
1056b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            throw new ExecutionException(getThrowableException());
1057a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
1058a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return getRawResult();
1059a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1060a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1061a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1062a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Joins this task, without returning its result or throwing its
1063a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception. This method may be useful when processing
1064a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * collections of tasks when some have been cancelled or otherwise
1065a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * known to have aborted.
1066a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1067a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final void quietlyJoin() {
1068a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        doJoin();
1069a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1070a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1071a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1072a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Commences performing this task and awaits its completion if
1073a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * necessary, without returning its result or throwing its
1074a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exception.
1075a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1076a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public final void quietlyInvoke() {
1077a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        doInvoke();
1078a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1079a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1080a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1081a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Possibly executes tasks until the pool hosting the current task
1082b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
1083b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * method may be of use in designs in which many tasks are forked,
1084b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * but none are explicitly joined, instead executing them until
1085b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * all are processed.
1086a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1087a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static void helpQuiesce() {
108891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t;
108991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
109091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
109191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            wt.pool.helpQuiescePool(wt.workQueue);
109291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        }
109391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        else
109475a06e56a4cc4599946e21422513e4bafa759509Calin Juravle            ForkJoinPool.quiesceCommonPool();
1095a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1096a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1097a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1098a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Resets the internal bookkeeping state of this task, allowing a
1099a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * subsequent {@code fork}. This method allows repeated reuse of
1100a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * this task, but only if reuse occurs when this task has either
1101a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * never been forked, or has been forked, then completed and all
1102a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * outstanding joins of this task have also completed. Effects
1103a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * under any other usage conditions are not guaranteed.
1104a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * This method may be useful when executing
1105a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * pre-constructed trees of subtasks in loops.
1106a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1107a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * <p>Upon completion of this method, {@code isDone()} reports
1108a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * {@code false}, and {@code getException()} reports {@code
1109a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * null}. However, the value returned by {@code getRawResult} is
1110a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * unaffected. To clear this value, you can invoke {@code
1111a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * setRawResult(null)}.
1112a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1113a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public void reinitialize() {
111491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((status & DONE_MASK) == EXCEPTIONAL)
1115a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            clearExceptionalCompletion();
1116a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        else
1117a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            status = 0;
1118a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1119a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1120a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1121b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Returns the pool hosting the current thread, or {@code null}
1122b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * if the current thread is executing outside of any ForkJoinPool.
1123b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     *
1124b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * <p>This method returns {@code null} if and only if {@link
1125b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * #inForkJoinPool} returns {@code false}.
1126a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1127a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the pool, or {@code null} if none
1128a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1129a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static ForkJoinPool getPool() {
1130a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        Thread t = Thread.currentThread();
1131a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return (t instanceof ForkJoinWorkerThread) ?
1132a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            ((ForkJoinWorkerThread) t).pool : null;
1133a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1134a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1135a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1136a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns {@code true} if the current thread is a {@link
1137a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * ForkJoinWorkerThread} executing as a ForkJoinPool computation.
1138a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1139a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code true} if the current thread is a {@link
1140a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * ForkJoinWorkerThread} executing as a ForkJoinPool computation,
1141a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * or {@code false} otherwise
1142a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1143a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static boolean inForkJoinPool() {
1144a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return Thread.currentThread() instanceof ForkJoinWorkerThread;
1145a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1146a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1147a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1148a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Tries to unschedule this task for execution. This method will
114991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * typically (but is not guaranteed to) succeed if this task is
115091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * the most recently forked task by the current thread, and has
115191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * not commenced executing in another thread.  This method may be
115291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * useful when arranging alternative local processing of tasks
115391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * that could have been, but were not, stolen.
1154a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1155a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return {@code true} if unforked
1156a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1157a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public boolean tryUnfork() {
115891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t;
115991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
116091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                ((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) :
116175a06e56a4cc4599946e21422513e4bafa759509Calin Juravle                ForkJoinPool.common.tryExternalUnpush(this));
1162a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1163a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1164a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1165a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns an estimate of the number of tasks that have been
1166a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * forked by the current worker thread but not yet executed. This
1167a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * value may be useful for heuristic decisions about whether to
1168a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * fork other tasks.
1169a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1170a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the number of tasks
1171a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1172a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static int getQueuedTaskCount() {
117391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t; ForkJoinPool.WorkQueue q;
117491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
117591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            q = ((ForkJoinWorkerThread)t).workQueue;
117691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        else
117791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            q = ForkJoinPool.commonSubmitterQueue();
117891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (q == null) ? 0 : q.queueSize();
1179a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1180a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1181a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1182a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns an estimate of how many more locally queued tasks are
1183a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * held by the current worker thread than there are other worker
118491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * threads that might steal them, or zero if this thread is not
118591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * operating in a ForkJoinPool. This value may be useful for
1186a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * heuristic decisions about whether to fork other tasks. In many
1187a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * usages of ForkJoinTasks, at steady state, each worker should
1188a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * aim to maintain a small constant surplus (for example, 3) of
1189a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * tasks, and to process computations locally if this threshold is
1190a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * exceeded.
1191a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1192a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the surplus number of tasks, which may be negative
1193a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1194a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static int getSurplusQueuedTaskCount() {
119591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return ForkJoinPool.getSurplusQueuedTaskCount();
1196a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1197a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1198a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    // Extension methods
1199a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1200a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1201a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns the result that would be returned by {@link #join}, even
1202a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * if this task completed abnormally, or {@code null} if this task
1203a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * is not known to have been completed.  This method is designed
1204a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * to aid debugging, as well as to support extensions. Its use in
1205a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * any other context is discouraged.
1206a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1207a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the result, or {@code null} if not completed
1208a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1209a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public abstract V getRawResult();
1210a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1211a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1212a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Forces the given value to be returned as a result.  This method
1213a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * is designed to support extensions, and should not in general be
1214a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * called otherwise.
1215a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1216a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param value the value
1217a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1218a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    protected abstract void setRawResult(V value);
1219a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1220a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
122191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Immediately performs the base action of this task and returns
122291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * true if, upon return from this method, this task is guaranteed
122391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * to have completed normally. This method may return false
122491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * otherwise, to indicate that this task is not necessarily
122591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * complete (or is not known to be complete), for example in
1226a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * asynchronous actions that require explicit invocations of
122791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * completion methods. This method may also throw an (unchecked)
122891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * exception to indicate abnormal exit. This method is designed to
122991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * support extensions, and should not in general be called
123091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * otherwise.
1231a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
123291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @return {@code true} if this task is known to have completed normally
1233a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1234a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    protected abstract boolean exec();
1235a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1236a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1237a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns, but does not unschedule or execute, a task queued by
1238a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * the current thread but not yet executed, if one is immediately
1239a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * available. There is no guarantee that this task will actually
1240a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * be polled or executed next. Conversely, this method may return
1241a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * null even if a task exists but cannot be accessed without
1242a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * contention with other threads.  This method is designed
1243a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * primarily to support extensions, and is unlikely to be useful
1244a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * otherwise.
1245a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1246a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the next task, or {@code null} if none are available
1247a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1248a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    protected static ForkJoinTask<?> peekNextLocalTask() {
124991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t; ForkJoinPool.WorkQueue q;
125091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
125191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            q = ((ForkJoinWorkerThread)t).workQueue;
125291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        else
125391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            q = ForkJoinPool.commonSubmitterQueue();
125491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (q == null) ? null : q.peek();
1255a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1256a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1257a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1258a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Unschedules and returns, without executing, the next task
125991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * queued by the current thread but not yet executed, if the
126091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * current thread is operating in a ForkJoinPool.  This method is
126191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * designed primarily to support extensions, and is unlikely to be
126291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * useful otherwise.
1263a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1264a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the next task, or {@code null} if none are available
1265a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1266a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    protected static ForkJoinTask<?> pollNextLocalTask() {
126791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t;
126891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
126991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            ((ForkJoinWorkerThread)t).workQueue.nextLocalTask() :
127091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            null;
1271a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1272a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1273a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
127491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * If the current thread is operating in a ForkJoinPool,
127591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * unschedules and returns, without executing, the next task
1276a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * queued by the current thread but not yet executed, if one is
1277a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * available, or if not available, a task that was forked by some
1278a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * other thread, if available. Availability may be transient, so a
127991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * {@code null} result does not necessarily imply quiescence of
128091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * the pool this task is operating in.  This method is designed
1281a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * primarily to support extensions, and is unlikely to be useful
1282a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * otherwise.
1283a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1284a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return a task, or {@code null} if none are available
1285a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1286a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    protected static ForkJoinTask<?> pollTask() {
128791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        Thread t; ForkJoinWorkerThread wt;
128891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
128991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            (wt = (ForkJoinWorkerThread)t).pool.nextTaskFor(wt.workQueue) :
129091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            null;
129191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
129291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
1293b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    /**
1294b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * If the current thread is operating in a ForkJoinPool,
1295b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * unschedules and returns, without executing, a task externally
1296b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * submitted to the pool, if one is available. Availability may be
1297b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * transient, so a {@code null} result does not necessarily imply
1298b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * quiescence of the pool.  This method is designed primarily to
1299b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * support extensions, and is unlikely to be useful otherwise.
1300b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     *
1301b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @return a task, or {@code null} if none are available
1302b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @since 9
1303b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @hide
1304b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     */
13056975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer    // Android-changed: hidden
1306b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    protected static ForkJoinTask<?> pollSubmission() {
1307b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        Thread t;
1308b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
1309b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            ((ForkJoinWorkerThread)t).pool.pollSubmission() : null;
1310b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    }
1311b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak
131291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    // tag operations
131391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
131491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
131591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Returns the tag for this task.
131691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
131791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @return the tag for this task
131891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @since 1.8
131991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
132091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    public final short getForkJoinTaskTag() {
132191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return (short)status;
132291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
132391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
132491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
1325b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Atomically sets the tag value for this task and returns the old value.
132691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
1327b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param newValue the new tag value
132891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @return the previous value of the tag
132991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @since 1.8
133091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
1331b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    public final short setForkJoinTaskTag(short newValue) {
133291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        for (int s;;) {
133391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (U.compareAndSwapInt(this, STATUS, s = status,
1334b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                    (s & ~SMASK) | (newValue & SMASK)))
133591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                return (short)s;
133691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        }
133791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
133891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
133991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
134091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Atomically conditionally sets the tag value for this task.
134191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Among other applications, tags can be used as visit markers
134291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * in tasks operating on graphs, as in methods that check: {@code
134391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * if (task.compareAndSetForkJoinTaskTag((short)0, (short)1))}
134491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * before processing, otherwise exiting because the node has
134591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * already been visited.
134691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     *
1347b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param expect the expected tag value
1348b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param update the new tag value
134975a06e56a4cc4599946e21422513e4bafa759509Calin Juravle     * @return {@code true} if successful; i.e., the current value was
1350b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * equal to {@code expect} and was changed to {@code update}.
135191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * @since 1.8
135291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
1353b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    public final boolean compareAndSetForkJoinTaskTag(short expect, short update) {
135491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        for (int s;;) {
1355b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak            if ((short)(s = status) != expect)
135691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                return false;
135791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (U.compareAndSwapInt(this, STATUS, s,
1358b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                                    (s & ~SMASK) | (update & SMASK)))
135991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle                return true;
136091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        }
1361a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1362a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1363a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1364b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Adapter for Runnables. This implements RunnableFuture
1365a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * to be compliant with AbstractExecutorService constraints
1366a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * when used in ForkJoinPool.
1367a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1368a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    static final class AdaptedRunnable<T> extends ForkJoinTask<T>
1369a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        implements RunnableFuture<T> {
1370a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final Runnable runnable;
1371a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        T result;
1372a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        AdaptedRunnable(Runnable runnable, T result) {
1373a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (runnable == null) throw new NullPointerException();
1374a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            this.runnable = runnable;
137591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            this.result = result; // OK to set this even before completion
1376a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
137791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final T getRawResult() { return result; }
137891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void setRawResult(T v) { result = v; }
137991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final boolean exec() { runnable.run(); return true; }
138091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void run() { invoke(); }
138191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        private static final long serialVersionUID = 5232453952276885070L;
138291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    }
138391770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
138491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    /**
1385b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Adapter for Runnables without results.
138691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     */
138791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    static final class AdaptedRunnableAction extends ForkJoinTask<Void>
138891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        implements RunnableFuture<Void> {
138991770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        final Runnable runnable;
139091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        AdaptedRunnableAction(Runnable runnable) {
139191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            if (runnable == null) throw new NullPointerException();
139291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            this.runnable = runnable;
1393a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
139491770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final Void getRawResult() { return null; }
139591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void setRawResult(Void v) { }
139691770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final boolean exec() { runnable.run(); return true; }
139791770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void run() { invoke(); }
1398a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        private static final long serialVersionUID = 5232453952276885070L;
1399a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1400a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1401a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1402b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Adapter for Runnables in which failure forces worker exception.
140375a06e56a4cc4599946e21422513e4bafa759509Calin Juravle     */
140475a06e56a4cc4599946e21422513e4bafa759509Calin Juravle    static final class RunnableExecuteAction extends ForkJoinTask<Void> {
140575a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        final Runnable runnable;
140675a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        RunnableExecuteAction(Runnable runnable) {
140775a06e56a4cc4599946e21422513e4bafa759509Calin Juravle            if (runnable == null) throw new NullPointerException();
140875a06e56a4cc4599946e21422513e4bafa759509Calin Juravle            this.runnable = runnable;
140975a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        }
141075a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        public final Void getRawResult() { return null; }
141175a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        public final void setRawResult(Void v) { }
141275a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        public final boolean exec() { runnable.run(); return true; }
141375a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        void internalPropagateException(Throwable ex) {
141475a06e56a4cc4599946e21422513e4bafa759509Calin Juravle            rethrow(ex); // rethrow outside exec() catches.
141575a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        }
141675a06e56a4cc4599946e21422513e4bafa759509Calin Juravle        private static final long serialVersionUID = 5232453952276885070L;
141775a06e56a4cc4599946e21422513e4bafa759509Calin Juravle    }
141875a06e56a4cc4599946e21422513e4bafa759509Calin Juravle
141975a06e56a4cc4599946e21422513e4bafa759509Calin Juravle    /**
1420b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * Adapter for Callables.
1421a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1422a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    static final class AdaptedCallable<T> extends ForkJoinTask<T>
1423a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        implements RunnableFuture<T> {
1424a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        final Callable<? extends T> callable;
1425a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        T result;
1426a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        AdaptedCallable(Callable<? extends T> callable) {
1427a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            if (callable == null) throw new NullPointerException();
1428a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            this.callable = callable;
1429a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
143091770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final T getRawResult() { return result; }
143191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void setRawResult(T v) { result = v; }
143291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final boolean exec() {
1433a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            try {
1434a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                result = callable.call();
1435a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                return true;
1436a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            } catch (RuntimeException rex) {
1437a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                throw rex;
1438a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            } catch (Exception ex) {
1439a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson                throw new RuntimeException(ex);
1440a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            }
1441a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
144291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        public final void run() { invoke(); }
1443a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        private static final long serialVersionUID = 2838392045355241008L;
1444a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1445a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1446a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1447a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns a new {@code ForkJoinTask} that performs the {@code run}
1448a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * method of the given {@code Runnable} as its action, and returns
1449a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * a null result upon {@link #join}.
1450a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1451a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param runnable the runnable action
1452a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the task
1453a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1454a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static ForkJoinTask<?> adapt(Runnable runnable) {
145591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle        return new AdaptedRunnableAction(runnable);
1456a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1457a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1458a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1459a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns a new {@code ForkJoinTask} that performs the {@code run}
1460a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * method of the given {@code Runnable} as its action, and returns
1461a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * the given result upon {@link #join}.
1462a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1463a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param runnable the runnable action
1464a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param result the result upon completion
1465fd81a99407d318ea3d8b5782294a0644e580d126Neil Fuller     * @param <T> the type of the result
1466a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the task
1467a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1468a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
1469a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return new AdaptedRunnable<T>(runnable, result);
1470a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1471a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1472a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
1473a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * Returns a new {@code ForkJoinTask} that performs the {@code call}
1474a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * method of the given {@code Callable} as its action, and returns
1475a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * its result upon {@link #join}, translating any checked exceptions
1476a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * encountered into {@code RuntimeException}.
1477a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1478a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @param callable the callable action
1479fd81a99407d318ea3d8b5782294a0644e580d126Neil Fuller     * @param <T> the type of the callable's result
1480a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @return the task
1481a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1482a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) {
1483a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        return new AdaptedCallable<T>(callable);
1484a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1485a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1486a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    // Serialization support
1487a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1488a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private static final long serialVersionUID = -7721805057305804111L;
1489a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1490a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
149191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Saves this task to a stream (that is, serializes it).
1492a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     *
1493b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param s the stream
1494b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @throws java.io.IOException if an I/O error occurs
1495a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * @serialData the current run status and the exception thrown
1496a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     * during execution, or {@code null} if none
1497a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1498a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private void writeObject(java.io.ObjectOutputStream s)
1499a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        throws java.io.IOException {
1500a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        s.defaultWriteObject();
1501a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        s.writeObject(getException());
1502a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1503a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1504a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    /**
150591770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle     * Reconstitutes this task from a stream (that is, deserializes it).
1506b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @param s the stream
1507b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @throws ClassNotFoundException if the class of a serialized object
1508b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     *         could not be found
1509b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak     * @throws java.io.IOException if an I/O error occurs
1510a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson     */
1511a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    private void readObject(java.io.ObjectInputStream s)
1512a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        throws java.io.IOException, ClassNotFoundException {
1513a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        s.defaultReadObject();
1514a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        Object ex = s.readObject();
1515a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        if (ex != null)
1516a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            setExceptionalCompletion((Throwable)ex);
1517a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1518a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson
1519a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    // Unsafe mechanics
1520b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak    private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
152191770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle    private static final long STATUS;
152291770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle
1523a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    static {
1524a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        exceptionTableLock = new ReentrantLock();
1525a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        exceptionTableRefQueue = new ReferenceQueue<Object>();
1526a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        exceptionTable = new ExceptionNode[EXCEPTION_MAP_CAPACITY];
1527a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        try {
152891770798d8b9280d48d30df2ed7f63b3ed9b036fCalin Juravle            STATUS = U.objectFieldOffset
1529b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak                (ForkJoinTask.class.getDeclaredField("status"));
1530b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak        } catch (ReflectiveOperationException e) {
1531a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson            throw new Error(e);
1532a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson        }
1533a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson    }
1534b8b75116273ecfdb8ffdd1869b1c0dd04570a95ePrzemyslaw Szczepaniak
1535a807b4d808d2591894daf13aab179b2e9c46a2f5Jesse Wilson}
1536