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