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