1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
|
9 |
dl |
1.70 |
import jdk.internal.misc.Unsafe; |
10 |
dl |
1.55 |
|
11 |
dl |
1.1 |
/** |
12 |
dl |
1.14 |
* A {@link ForkJoinTask} with a completion action performed when |
13 |
jsr166 |
1.38 |
* triggered and there are no remaining pending actions. |
14 |
|
|
* CountedCompleters are in general more robust in the |
15 |
dl |
1.15 |
* presence of subtask stalls and blockage than are other forms of |
16 |
|
|
* ForkJoinTasks, but are less intuitive to program. Uses of |
17 |
|
|
* CountedCompleter are similar to those of other completion based |
18 |
dl |
1.14 |
* components (such as {@link java.nio.channels.CompletionHandler}) |
19 |
|
|
* except that multiple <em>pending</em> completions may be necessary |
20 |
jsr166 |
1.31 |
* to trigger the completion action {@link #onCompletion(CountedCompleter)}, |
21 |
|
|
* not just one. |
22 |
jsr166 |
1.25 |
* Unless initialized otherwise, the {@linkplain #getPendingCount pending |
23 |
|
|
* count} starts at zero, but may be (atomically) changed using |
24 |
|
|
* methods {@link #setPendingCount}, {@link #addToPendingCount}, and |
25 |
|
|
* {@link #compareAndSetPendingCount}. Upon invocation of {@link |
26 |
dl |
1.1 |
* #tryComplete}, if the pending action count is nonzero, it is |
27 |
|
|
* decremented; otherwise, the completion action is performed, and if |
28 |
|
|
* this completer itself has a completer, the process is continued |
29 |
dl |
1.2 |
* with its completer. As is the case with related synchronization |
30 |
jsr166 |
1.64 |
* components such as {@link Phaser} and {@link Semaphore}, these methods |
31 |
jsr166 |
1.5 |
* affect only internal counts; they do not establish any further |
32 |
|
|
* internal bookkeeping. In particular, the identities of pending |
33 |
|
|
* tasks are not maintained. As illustrated below, you can create |
34 |
jsr166 |
1.6 |
* subclasses that do record some or all pending tasks or their |
35 |
dl |
1.15 |
* results when needed. As illustrated below, utility methods |
36 |
|
|
* supporting customization of completion traversals are also |
37 |
|
|
* provided. However, because CountedCompleters provide only basic |
38 |
dl |
1.14 |
* synchronization mechanisms, it may be useful to create further |
39 |
dl |
1.15 |
* abstract subclasses that maintain linkages, fields, and additional |
40 |
|
|
* support methods appropriate for a set of related usages. |
41 |
dl |
1.1 |
* |
42 |
|
|
* <p>A concrete CountedCompleter class must define method {@link |
43 |
dl |
1.14 |
* #compute}, that should in most cases (as illustrated below), invoke |
44 |
|
|
* {@code tryComplete()} once before returning. The class may also |
45 |
jsr166 |
1.31 |
* optionally override method {@link #onCompletion(CountedCompleter)} |
46 |
|
|
* to perform an action upon normal completion, and method |
47 |
|
|
* {@link #onExceptionalCompletion(Throwable, CountedCompleter)} to |
48 |
|
|
* perform an action upon any exception. |
49 |
dl |
1.1 |
* |
50 |
dl |
1.4 |
* <p>CountedCompleters most often do not bear results, in which case |
51 |
|
|
* they are normally declared as {@code CountedCompleter<Void>}, and |
52 |
|
|
* will always return {@code null} as a result value. In other cases, |
53 |
|
|
* you should override method {@link #getRawResult} to provide a |
54 |
dl |
1.14 |
* result from {@code join(), invoke()}, and related methods. In |
55 |
|
|
* general, this method should return the value of a field (or a |
56 |
|
|
* function of one or more fields) of the CountedCompleter object that |
57 |
|
|
* holds the result upon completion. Method {@link #setRawResult} by |
58 |
|
|
* default plays no role in CountedCompleters. It is possible, but |
59 |
dl |
1.15 |
* rarely applicable, to override this method to maintain other |
60 |
dl |
1.14 |
* objects or fields holding result data. |
61 |
dl |
1.4 |
* |
62 |
dl |
1.1 |
* <p>A CountedCompleter that does not itself have a completer (i.e., |
63 |
|
|
* one for which {@link #getCompleter} returns {@code null}) can be |
64 |
|
|
* used as a regular ForkJoinTask with this added functionality. |
65 |
|
|
* However, any completer that in turn has another completer serves |
66 |
|
|
* only as an internal helper for other computations, so its own task |
67 |
|
|
* status (as reported in methods such as {@link ForkJoinTask#isDone}) |
68 |
|
|
* is arbitrary; this status changes only upon explicit invocations of |
69 |
jsr166 |
1.31 |
* {@link #complete}, {@link ForkJoinTask#cancel}, |
70 |
|
|
* {@link ForkJoinTask#completeExceptionally(Throwable)} or upon |
71 |
|
|
* exceptional completion of method {@code compute}. Upon any |
72 |
|
|
* exceptional completion, the exception may be relayed to a task's |
73 |
|
|
* completer (and its completer, and so on), if one exists and it has |
74 |
|
|
* not otherwise already completed. Similarly, cancelling an internal |
75 |
|
|
* CountedCompleter has only a local effect on that completer, so is |
76 |
|
|
* not often useful. |
77 |
dl |
1.1 |
* |
78 |
|
|
* <p><b>Sample Usages.</b> |
79 |
|
|
* |
80 |
|
|
* <p><b>Parallel recursive decomposition.</b> CountedCompleters may |
81 |
|
|
* be arranged in trees similar to those often used with {@link |
82 |
|
|
* RecursiveAction}s, although the constructions involved in setting |
83 |
dl |
1.4 |
* them up typically vary. Here, the completer of each task is its |
84 |
|
|
* parent in the computation tree. Even though they entail a bit more |
85 |
dl |
1.1 |
* bookkeeping, CountedCompleters may be better choices when applying |
86 |
|
|
* a possibly time-consuming operation (that cannot be further |
87 |
|
|
* subdivided) to each element of an array or collection; especially |
88 |
|
|
* when the operation takes a significantly different amount of time |
89 |
|
|
* to complete for some elements than others, either because of |
90 |
jsr166 |
1.27 |
* intrinsic variation (for example I/O) or auxiliary effects such as |
91 |
dl |
1.1 |
* garbage collection. Because CountedCompleters provide their own |
92 |
jsr166 |
1.58 |
* continuations, other tasks need not block waiting to perform them. |
93 |
dl |
1.1 |
* |
94 |
jsr166 |
1.58 |
* <p>For example, here is an initial version of a utility method that |
95 |
|
|
* uses divide-by-two recursive decomposition to divide work into |
96 |
|
|
* single pieces (leaf tasks). Even when work is split into individual |
97 |
|
|
* calls, tree-based techniques are usually preferable to directly |
98 |
|
|
* forking leaf tasks, because they reduce inter-thread communication |
99 |
|
|
* and improve load balancing. In the recursive case, the second of |
100 |
|
|
* each pair of subtasks to finish triggers completion of their parent |
101 |
dl |
1.1 |
* (because no result combination is performed, the default no-op |
102 |
jsr166 |
1.38 |
* implementation of method {@code onCompletion} is not overridden). |
103 |
jsr166 |
1.58 |
* The utility method sets up the root task and invokes it (here, |
104 |
|
|
* implicitly using the {@link ForkJoinPool#commonPool()}). It is |
105 |
|
|
* straightforward and reliable (but not optimal) to always set the |
106 |
|
|
* pending count to the number of child tasks and call {@code |
107 |
|
|
* tryComplete()} immediately before returning. |
108 |
dl |
1.1 |
* |
109 |
|
|
* <pre> {@code |
110 |
jsr166 |
1.58 |
* public static <E> void forEach(E[] array, Consumer<E> action) { |
111 |
|
|
* class Task extends CountedCompleter<Void> { |
112 |
|
|
* final int lo, hi; |
113 |
|
|
* Task(Task parent, int lo, int hi) { |
114 |
|
|
* super(parent); this.lo = lo; this.hi = hi; |
115 |
|
|
* } |
116 |
jsr166 |
1.59 |
* |
117 |
jsr166 |
1.58 |
* public void compute() { |
118 |
|
|
* if (hi - lo >= 2) { |
119 |
|
|
* int mid = (lo + hi) >>> 1; |
120 |
|
|
* // must set pending count before fork |
121 |
|
|
* setPendingCount(2); |
122 |
|
|
* new Task(this, mid, hi).fork(); // right child |
123 |
|
|
* new Task(this, lo, mid).fork(); // left child |
124 |
|
|
* } |
125 |
|
|
* else if (hi > lo) |
126 |
|
|
* action.accept(array[lo]); |
127 |
|
|
* tryComplete(); |
128 |
|
|
* } |
129 |
dl |
1.15 |
* } |
130 |
jsr166 |
1.58 |
* new Task(null, 0, array.length).invoke(); |
131 |
jsr166 |
1.21 |
* }}</pre> |
132 |
dl |
1.1 |
* |
133 |
|
|
* This design can be improved by noticing that in the recursive case, |
134 |
|
|
* the task has nothing to do after forking its right task, so can |
135 |
|
|
* directly invoke its left task before returning. (This is an analog |
136 |
jsr166 |
1.58 |
* of tail recursion removal.) Also, when the last action in a task |
137 |
|
|
* is to fork or invoke a subtask (a "tail call"), the call to {@code |
138 |
|
|
* tryComplete()} can be optimized away, at the cost of making the |
139 |
|
|
* pending count look "off by one". |
140 |
dl |
1.1 |
* |
141 |
|
|
* <pre> {@code |
142 |
jsr166 |
1.58 |
* public void compute() { |
143 |
|
|
* if (hi - lo >= 2) { |
144 |
|
|
* int mid = (lo + hi) >>> 1; |
145 |
jsr166 |
1.63 |
* setPendingCount(1); // looks off by one, but correct! |
146 |
jsr166 |
1.58 |
* new Task(this, mid, hi).fork(); // right child |
147 |
|
|
* new Task(this, lo, mid).compute(); // direct invoke |
148 |
|
|
* } else { |
149 |
|
|
* if (hi > lo) |
150 |
|
|
* action.accept(array[lo]); |
151 |
|
|
* tryComplete(); |
152 |
|
|
* } |
153 |
jsr166 |
1.61 |
* }}</pre> |
154 |
dl |
1.1 |
* |
155 |
jsr166 |
1.52 |
* As a further optimization, notice that the left task need not even exist. |
156 |
jsr166 |
1.60 |
* Instead of creating a new one, we can continue using the original task, |
157 |
jsr166 |
1.31 |
* and add a pending count for each fork. Additionally, because no task |
158 |
|
|
* in this tree implements an {@link #onCompletion(CountedCompleter)} method, |
159 |
jsr166 |
1.58 |
* {@code tryComplete} can be replaced with {@link #propagateCompletion}. |
160 |
dl |
1.1 |
* |
161 |
|
|
* <pre> {@code |
162 |
jsr166 |
1.58 |
* public void compute() { |
163 |
|
|
* int n = hi - lo; |
164 |
|
|
* for (; n >= 2; n /= 2) { |
165 |
|
|
* addToPendingCount(1); |
166 |
|
|
* new Task(this, lo + n/2, lo + n).fork(); |
167 |
|
|
* } |
168 |
|
|
* if (n > 0) |
169 |
|
|
* action.accept(array[lo]); |
170 |
|
|
* propagateCompletion(); |
171 |
jsr166 |
1.61 |
* }}</pre> |
172 |
jsr166 |
1.58 |
* |
173 |
|
|
* When pending counts can be precomputed, they can be established in |
174 |
|
|
* the constructor: |
175 |
|
|
* |
176 |
|
|
* <pre> {@code |
177 |
|
|
* public static <E> void forEach(E[] array, Consumer<E> action) { |
178 |
|
|
* class Task extends CountedCompleter<Void> { |
179 |
|
|
* final int lo, hi; |
180 |
|
|
* Task(Task parent, int lo, int hi) { |
181 |
|
|
* super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo)); |
182 |
|
|
* this.lo = lo; this.hi = hi; |
183 |
|
|
* } |
184 |
jsr166 |
1.59 |
* |
185 |
jsr166 |
1.58 |
* public void compute() { |
186 |
|
|
* for (int n = hi - lo; n >= 2; n /= 2) |
187 |
|
|
* new Task(this, lo + n/2, lo + n).fork(); |
188 |
|
|
* action.accept(array[lo]); |
189 |
|
|
* propagateCompletion(); |
190 |
dl |
1.15 |
* } |
191 |
|
|
* } |
192 |
jsr166 |
1.58 |
* if (array.length > 0) |
193 |
|
|
* new Task(null, 0, array.length).invoke(); |
194 |
jsr166 |
1.46 |
* }}</pre> |
195 |
dl |
1.1 |
* |
196 |
jsr166 |
1.58 |
* Additional optimizations of such classes might entail specializing |
197 |
|
|
* classes for leaf steps, subdividing by say, four, instead of two |
198 |
|
|
* per iteration, and using an adaptive threshold instead of always |
199 |
|
|
* subdividing down to single elements. |
200 |
dl |
1.1 |
* |
201 |
dl |
1.15 |
* <p><b>Searching.</b> A tree of CountedCompleters can search for a |
202 |
|
|
* value or property in different parts of a data structure, and |
203 |
jsr166 |
1.26 |
* report a result in an {@link |
204 |
|
|
* java.util.concurrent.atomic.AtomicReference AtomicReference} as |
205 |
|
|
* soon as one is found. The others can poll the result to avoid |
206 |
|
|
* unnecessary work. (You could additionally {@linkplain #cancel |
207 |
|
|
* cancel} other tasks, but it is usually simpler and more efficient |
208 |
|
|
* to just let them notice that the result is set and if so skip |
209 |
|
|
* further processing.) Illustrating again with an array using full |
210 |
dl |
1.15 |
* partitioning (again, in practice, leaf tasks will almost always |
211 |
|
|
* process more than one element): |
212 |
|
|
* |
213 |
|
|
* <pre> {@code |
214 |
|
|
* class Searcher<E> extends CountedCompleter<E> { |
215 |
|
|
* final E[] array; final AtomicReference<E> result; final int lo, hi; |
216 |
|
|
* Searcher(CountedCompleter<?> p, E[] array, AtomicReference<E> result, int lo, int hi) { |
217 |
|
|
* super(p); |
218 |
|
|
* this.array = array; this.result = result; this.lo = lo; this.hi = hi; |
219 |
|
|
* } |
220 |
|
|
* public E getRawResult() { return result.get(); } |
221 |
|
|
* public void compute() { // similar to ForEach version 3 |
222 |
jsr166 |
1.47 |
* int l = lo, h = hi; |
223 |
dl |
1.15 |
* while (result.get() == null && h >= l) { |
224 |
|
|
* if (h - l >= 2) { |
225 |
|
|
* int mid = (l + h) >>> 1; |
226 |
|
|
* addToPendingCount(1); |
227 |
|
|
* new Searcher(this, array, result, mid, h).fork(); |
228 |
|
|
* h = mid; |
229 |
|
|
* } |
230 |
|
|
* else { |
231 |
|
|
* E x = array[l]; |
232 |
|
|
* if (matches(x) && result.compareAndSet(null, x)) |
233 |
|
|
* quietlyCompleteRoot(); // root task is now joinable |
234 |
|
|
* break; |
235 |
|
|
* } |
236 |
|
|
* } |
237 |
|
|
* tryComplete(); // normally complete whether or not found |
238 |
|
|
* } |
239 |
|
|
* boolean matches(E e) { ... } // return true if found |
240 |
|
|
* |
241 |
|
|
* public static <E> E search(E[] array) { |
242 |
|
|
* return new Searcher<E>(null, array, new AtomicReference<E>(), 0, array.length).invoke(); |
243 |
|
|
* } |
244 |
jsr166 |
1.28 |
* }}</pre> |
245 |
dl |
1.15 |
* |
246 |
|
|
* In this example, as well as others in which tasks have no other |
247 |
jsr166 |
1.51 |
* effects except to {@code compareAndSet} a common result, the |
248 |
|
|
* trailing unconditional invocation of {@code tryComplete} could be |
249 |
|
|
* made conditional ({@code if (result.get() == null) tryComplete();}) |
250 |
dl |
1.15 |
* because no further bookkeeping is required to manage completions |
251 |
|
|
* once the root task completes. |
252 |
|
|
* |
253 |
dl |
1.1 |
* <p><b>Recording subtasks.</b> CountedCompleter tasks that combine |
254 |
|
|
* results of multiple subtasks usually need to access these results |
255 |
jsr166 |
1.31 |
* in method {@link #onCompletion(CountedCompleter)}. As illustrated in the following |
256 |
dl |
1.1 |
* class (that performs a simplified form of map-reduce where mappings |
257 |
|
|
* and reductions are all of type {@code E}), one way to do this in |
258 |
|
|
* divide and conquer designs is to have each subtask record its |
259 |
|
|
* sibling, so that it can be accessed in method {@code onCompletion}. |
260 |
dl |
1.4 |
* This technique applies to reductions in which the order of |
261 |
|
|
* combining left and right results does not matter; ordered |
262 |
|
|
* reductions require explicit left/right designations. Variants of |
263 |
|
|
* other streamlinings seen in the above examples may also apply. |
264 |
dl |
1.1 |
* |
265 |
|
|
* <pre> {@code |
266 |
|
|
* class MyMapper<E> { E apply(E v) { ... } } |
267 |
|
|
* class MyReducer<E> { E apply(E x, E y) { ... } } |
268 |
dl |
1.4 |
* class MapReducer<E> extends CountedCompleter<E> { |
269 |
dl |
1.15 |
* final E[] array; final MyMapper<E> mapper; |
270 |
|
|
* final MyReducer<E> reducer; final int lo, hi; |
271 |
|
|
* MapReducer<E> sibling; |
272 |
|
|
* E result; |
273 |
|
|
* MapReducer(CountedCompleter<?> p, E[] array, MyMapper<E> mapper, |
274 |
|
|
* MyReducer<E> reducer, int lo, int hi) { |
275 |
|
|
* super(p); |
276 |
|
|
* this.array = array; this.mapper = mapper; |
277 |
|
|
* this.reducer = reducer; this.lo = lo; this.hi = hi; |
278 |
|
|
* } |
279 |
|
|
* public void compute() { |
280 |
|
|
* if (hi - lo >= 2) { |
281 |
|
|
* int mid = (lo + hi) >>> 1; |
282 |
|
|
* MapReducer<E> left = new MapReducer(this, array, mapper, reducer, lo, mid); |
283 |
|
|
* MapReducer<E> right = new MapReducer(this, array, mapper, reducer, mid, hi); |
284 |
|
|
* left.sibling = right; |
285 |
|
|
* right.sibling = left; |
286 |
|
|
* setPendingCount(1); // only right is pending |
287 |
|
|
* right.fork(); |
288 |
|
|
* left.compute(); // directly execute left |
289 |
|
|
* } |
290 |
|
|
* else { |
291 |
|
|
* if (hi > lo) |
292 |
|
|
* result = mapper.apply(array[lo]); |
293 |
|
|
* tryComplete(); |
294 |
|
|
* } |
295 |
|
|
* } |
296 |
|
|
* public void onCompletion(CountedCompleter<?> caller) { |
297 |
|
|
* if (caller != this) { |
298 |
jsr166 |
1.23 |
* MapReducer<E> child = (MapReducer<E>)caller; |
299 |
|
|
* MapReducer<E> sib = child.sibling; |
300 |
|
|
* if (sib == null || sib.result == null) |
301 |
|
|
* result = child.result; |
302 |
|
|
* else |
303 |
|
|
* result = reducer.apply(child.result, sib.result); |
304 |
dl |
1.15 |
* } |
305 |
|
|
* } |
306 |
|
|
* public E getRawResult() { return result; } |
307 |
|
|
* |
308 |
|
|
* public static <E> E mapReduce(E[] array, MyMapper<E> mapper, MyReducer<E> reducer) { |
309 |
|
|
* return new MapReducer<E>(null, array, mapper, reducer, |
310 |
|
|
* 0, array.length).invoke(); |
311 |
|
|
* } |
312 |
jsr166 |
1.21 |
* }}</pre> |
313 |
dl |
1.1 |
* |
314 |
dl |
1.14 |
* Here, method {@code onCompletion} takes a form common to many |
315 |
|
|
* completion designs that combine results. This callback-style method |
316 |
|
|
* is triggered once per task, in either of the two different contexts |
317 |
|
|
* in which the pending count is, or becomes, zero: (1) by a task |
318 |
|
|
* itself, if its pending count is zero upon invocation of {@code |
319 |
|
|
* tryComplete}, or (2) by any of its subtasks when they complete and |
320 |
|
|
* decrement the pending count to zero. The {@code caller} argument |
321 |
|
|
* distinguishes cases. Most often, when the caller is {@code this}, |
322 |
|
|
* no action is necessary. Otherwise the caller argument can be used |
323 |
|
|
* (usually via a cast) to supply a value (and/or links to other |
324 |
jsr166 |
1.19 |
* values) to be combined. Assuming proper use of pending counts, the |
325 |
dl |
1.14 |
* actions inside {@code onCompletion} occur (once) upon completion of |
326 |
|
|
* a task and its subtasks. No additional synchronization is required |
327 |
|
|
* within this method to ensure thread safety of accesses to fields of |
328 |
|
|
* this task or other completed tasks. |
329 |
|
|
* |
330 |
dl |
1.69 |
* <p><b>Completion Traversals.</b> If using {@code onCompletion} to |
331 |
dl |
1.15 |
* process completions is inapplicable or inconvenient, you can use |
332 |
|
|
* methods {@link #firstComplete} and {@link #nextComplete} to create |
333 |
|
|
* custom traversals. For example, to define a MapReducer that only |
334 |
|
|
* splits out right-hand tasks in the form of the third ForEach |
335 |
|
|
* example, the completions must cooperatively reduce along |
336 |
|
|
* unexhausted subtask links, which can be done as follows: |
337 |
dl |
1.14 |
* |
338 |
|
|
* <pre> {@code |
339 |
dl |
1.15 |
* class MapReducer<E> extends CountedCompleter<E> { // version 2 |
340 |
|
|
* final E[] array; final MyMapper<E> mapper; |
341 |
|
|
* final MyReducer<E> reducer; final int lo, hi; |
342 |
|
|
* MapReducer<E> forks, next; // record subtask forks in list |
343 |
|
|
* E result; |
344 |
|
|
* MapReducer(CountedCompleter<?> p, E[] array, MyMapper<E> mapper, |
345 |
|
|
* MyReducer<E> reducer, int lo, int hi, MapReducer<E> next) { |
346 |
|
|
* super(p); |
347 |
|
|
* this.array = array; this.mapper = mapper; |
348 |
|
|
* this.reducer = reducer; this.lo = lo; this.hi = hi; |
349 |
|
|
* this.next = next; |
350 |
|
|
* } |
351 |
|
|
* public void compute() { |
352 |
jsr166 |
1.47 |
* int l = lo, h = hi; |
353 |
dl |
1.15 |
* while (h - l >= 2) { |
354 |
|
|
* int mid = (l + h) >>> 1; |
355 |
|
|
* addToPendingCount(1); |
356 |
dl |
1.29 |
* (forks = new MapReducer(this, array, mapper, reducer, mid, h, forks)).fork(); |
357 |
dl |
1.15 |
* h = mid; |
358 |
|
|
* } |
359 |
|
|
* if (h > l) |
360 |
|
|
* result = mapper.apply(array[l]); |
361 |
|
|
* // process completions by reducing along and advancing subtask links |
362 |
|
|
* for (CountedCompleter<?> c = firstComplete(); c != null; c = c.nextComplete()) { |
363 |
jsr166 |
1.47 |
* for (MapReducer t = (MapReducer)c, s = t.forks; s != null; s = t.forks = s.next) |
364 |
dl |
1.15 |
* t.result = reducer.apply(t.result, s.result); |
365 |
|
|
* } |
366 |
|
|
* } |
367 |
|
|
* public E getRawResult() { return result; } |
368 |
|
|
* |
369 |
|
|
* public static <E> E mapReduce(E[] array, MyMapper<E> mapper, MyReducer<E> reducer) { |
370 |
|
|
* return new MapReducer<E>(null, array, mapper, reducer, |
371 |
|
|
* 0, array.length, null).invoke(); |
372 |
|
|
* } |
373 |
|
|
* }}</pre> |
374 |
dl |
1.14 |
* |
375 |
dl |
1.1 |
* <p><b>Triggers.</b> Some CountedCompleters are themselves never |
376 |
|
|
* forked, but instead serve as bits of plumbing in other designs; |
377 |
jsr166 |
1.37 |
* including those in which the completion of one or more async tasks |
378 |
dl |
1.1 |
* triggers another async task. For example: |
379 |
|
|
* |
380 |
|
|
* <pre> {@code |
381 |
dl |
1.4 |
* class HeaderBuilder extends CountedCompleter<...> { ... } |
382 |
|
|
* class BodyBuilder extends CountedCompleter<...> { ... } |
383 |
|
|
* class PacketSender extends CountedCompleter<...> { |
384 |
dl |
1.15 |
* PacketSender(...) { super(null, 1); ... } // trigger on second completion |
385 |
|
|
* public void compute() { } // never called |
386 |
|
|
* public void onCompletion(CountedCompleter<?> caller) { sendPacket(); } |
387 |
dl |
1.1 |
* } |
388 |
|
|
* // sample use: |
389 |
|
|
* PacketSender p = new PacketSender(); |
390 |
|
|
* new HeaderBuilder(p, ...).fork(); |
391 |
jsr166 |
1.46 |
* new BodyBuilder(p, ...).fork();}</pre> |
392 |
dl |
1.1 |
* |
393 |
|
|
* @since 1.8 |
394 |
|
|
* @author Doug Lea |
395 |
|
|
*/ |
396 |
dl |
1.4 |
public abstract class CountedCompleter<T> extends ForkJoinTask<T> { |
397 |
dl |
1.2 |
private static final long serialVersionUID = 5232453752276485070L; |
398 |
|
|
|
399 |
dl |
1.1 |
/** This task's completer, or null if none */ |
400 |
dl |
1.4 |
final CountedCompleter<?> completer; |
401 |
dl |
1.1 |
/** The number of pending tasks until completion */ |
402 |
|
|
volatile int pending; |
403 |
|
|
|
404 |
|
|
/** |
405 |
|
|
* Creates a new CountedCompleter with the given completer |
406 |
|
|
* and initial pending count. |
407 |
|
|
* |
408 |
jsr166 |
1.24 |
* @param completer this task's completer, or {@code null} if none |
409 |
dl |
1.1 |
* @param initialPendingCount the initial pending count |
410 |
|
|
*/ |
411 |
dl |
1.4 |
protected CountedCompleter(CountedCompleter<?> completer, |
412 |
dl |
1.1 |
int initialPendingCount) { |
413 |
|
|
this.completer = completer; |
414 |
|
|
this.pending = initialPendingCount; |
415 |
|
|
} |
416 |
|
|
|
417 |
|
|
/** |
418 |
|
|
* Creates a new CountedCompleter with the given completer |
419 |
|
|
* and an initial pending count of zero. |
420 |
|
|
* |
421 |
jsr166 |
1.24 |
* @param completer this task's completer, or {@code null} if none |
422 |
dl |
1.1 |
*/ |
423 |
dl |
1.4 |
protected CountedCompleter(CountedCompleter<?> completer) { |
424 |
dl |
1.1 |
this.completer = completer; |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
/** |
428 |
|
|
* Creates a new CountedCompleter with no completer |
429 |
|
|
* and an initial pending count of zero. |
430 |
|
|
*/ |
431 |
|
|
protected CountedCompleter() { |
432 |
|
|
this.completer = null; |
433 |
|
|
} |
434 |
|
|
|
435 |
|
|
/** |
436 |
|
|
* The main computation performed by this task. |
437 |
|
|
*/ |
438 |
|
|
public abstract void compute(); |
439 |
|
|
|
440 |
|
|
/** |
441 |
dl |
1.2 |
* Performs an action when method {@link #tryComplete} is invoked |
442 |
jsr166 |
1.24 |
* and the pending count is zero, or when the unconditional |
443 |
dl |
1.2 |
* method {@link #complete} is invoked. By default, this method |
444 |
dl |
1.14 |
* does nothing. You can distinguish cases by checking the |
445 |
|
|
* identity of the given caller argument. If not equal to {@code |
446 |
|
|
* this}, then it is typically a subtask that may contain results |
447 |
|
|
* (and/or links to other results) to combine. |
448 |
dl |
1.1 |
* |
449 |
|
|
* @param caller the task invoking this method (which may |
450 |
jsr166 |
1.30 |
* be this task itself) |
451 |
dl |
1.1 |
*/ |
452 |
dl |
1.4 |
public void onCompletion(CountedCompleter<?> caller) { |
453 |
dl |
1.1 |
} |
454 |
|
|
|
455 |
|
|
/** |
456 |
dl |
1.36 |
* Performs an action when method {@link |
457 |
|
|
* #completeExceptionally(Throwable)} is invoked or method {@link |
458 |
jsr166 |
1.39 |
* #compute} throws an exception, and this task has not already |
459 |
|
|
* otherwise completed normally. On entry to this method, this task |
460 |
dl |
1.36 |
* {@link ForkJoinTask#isCompletedAbnormally}. The return value |
461 |
|
|
* of this method controls further propagation: If {@code true} |
462 |
|
|
* and this task has a completer that has not completed, then that |
463 |
|
|
* completer is also completed exceptionally, with the same |
464 |
|
|
* exception as this completer. The default implementation of |
465 |
|
|
* this method does nothing except return {@code true}. |
466 |
dl |
1.2 |
* |
467 |
|
|
* @param ex the exception |
468 |
|
|
* @param caller the task invoking this method (which may |
469 |
jsr166 |
1.30 |
* be this task itself) |
470 |
jsr166 |
1.33 |
* @return {@code true} if this exception should be propagated to this |
471 |
jsr166 |
1.30 |
* task's completer, if one exists |
472 |
dl |
1.2 |
*/ |
473 |
dl |
1.4 |
public boolean onExceptionalCompletion(Throwable ex, CountedCompleter<?> caller) { |
474 |
dl |
1.2 |
return true; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/** |
478 |
dl |
1.1 |
* Returns the completer established in this task's constructor, |
479 |
|
|
* or {@code null} if none. |
480 |
|
|
* |
481 |
|
|
* @return the completer |
482 |
|
|
*/ |
483 |
dl |
1.4 |
public final CountedCompleter<?> getCompleter() { |
484 |
dl |
1.1 |
return completer; |
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
/** |
488 |
|
|
* Returns the current pending count. |
489 |
|
|
* |
490 |
|
|
* @return the current pending count |
491 |
|
|
*/ |
492 |
|
|
public final int getPendingCount() { |
493 |
|
|
return pending; |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
/** |
497 |
|
|
* Sets the pending count to the given value. |
498 |
|
|
* |
499 |
|
|
* @param count the count |
500 |
|
|
*/ |
501 |
|
|
public final void setPendingCount(int count) { |
502 |
|
|
pending = count; |
503 |
|
|
} |
504 |
|
|
|
505 |
|
|
/** |
506 |
|
|
* Adds (atomically) the given value to the pending count. |
507 |
|
|
* |
508 |
|
|
* @param delta the value to add |
509 |
|
|
*/ |
510 |
|
|
public final void addToPendingCount(int delta) { |
511 |
dl |
1.70 |
U.getAndAddInt(this, PENDING, delta); |
512 |
dl |
1.1 |
} |
513 |
|
|
|
514 |
|
|
/** |
515 |
|
|
* Sets (atomically) the pending count to the given count only if |
516 |
|
|
* it currently holds the given expected value. |
517 |
|
|
* |
518 |
|
|
* @param expected the expected value |
519 |
|
|
* @param count the new value |
520 |
jsr166 |
1.33 |
* @return {@code true} if successful |
521 |
dl |
1.1 |
*/ |
522 |
|
|
public final boolean compareAndSetPendingCount(int expected, int count) { |
523 |
dl |
1.70 |
return U.compareAndSetInt(this, PENDING, expected, count); |
524 |
dl |
1.1 |
} |
525 |
|
|
|
526 |
dl |
1.67 |
// internal-only weak version |
527 |
|
|
final boolean weakCompareAndSetPendingCount(int expected, int count) { |
528 |
dl |
1.70 |
return U.weakCompareAndSetInt(this, PENDING, expected, count); |
529 |
dl |
1.67 |
} |
530 |
|
|
|
531 |
dl |
1.1 |
/** |
532 |
dl |
1.15 |
* If the pending count is nonzero, (atomically) decrements it. |
533 |
|
|
* |
534 |
jsr166 |
1.16 |
* @return the initial (undecremented) pending count holding on entry |
535 |
dl |
1.15 |
* to this method |
536 |
|
|
*/ |
537 |
|
|
public final int decrementPendingCountUnlessZero() { |
538 |
|
|
int c; |
539 |
|
|
do {} while ((c = pending) != 0 && |
540 |
dl |
1.67 |
!weakCompareAndSetPendingCount(c, c - 1)); |
541 |
dl |
1.15 |
return c; |
542 |
|
|
} |
543 |
|
|
|
544 |
|
|
/** |
545 |
dl |
1.7 |
* Returns the root of the current computation; i.e., this |
546 |
|
|
* task if it has no completer, else its completer's root. |
547 |
|
|
* |
548 |
|
|
* @return the root of the current computation |
549 |
|
|
*/ |
550 |
|
|
public final CountedCompleter<?> getRoot() { |
551 |
|
|
CountedCompleter<?> a = this, p; |
552 |
|
|
while ((p = a.completer) != null) |
553 |
|
|
a = p; |
554 |
|
|
return a; |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
/** |
558 |
dl |
1.1 |
* If the pending count is nonzero, decrements the count; |
559 |
jsr166 |
1.31 |
* otherwise invokes {@link #onCompletion(CountedCompleter)} |
560 |
|
|
* and then similarly tries to complete this task's completer, |
561 |
|
|
* if one exists, else marks this task as complete. |
562 |
dl |
1.1 |
*/ |
563 |
|
|
public final void tryComplete() { |
564 |
dl |
1.4 |
CountedCompleter<?> a = this, s = a; |
565 |
dl |
1.2 |
for (int c;;) { |
566 |
dl |
1.1 |
if ((c = a.pending) == 0) { |
567 |
|
|
a.onCompletion(s); |
568 |
|
|
if ((a = (s = a).completer) == null) { |
569 |
|
|
s.quietlyComplete(); |
570 |
|
|
return; |
571 |
|
|
} |
572 |
|
|
} |
573 |
dl |
1.67 |
else if (a.weakCompareAndSetPendingCount(c, c - 1)) |
574 |
dl |
1.1 |
return; |
575 |
|
|
} |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
/** |
579 |
dl |
1.15 |
* Equivalent to {@link #tryComplete} but does not invoke {@link |
580 |
jsr166 |
1.31 |
* #onCompletion(CountedCompleter)} along the completion path: |
581 |
|
|
* If the pending count is nonzero, decrements the count; |
582 |
|
|
* otherwise, similarly tries to complete this task's completer, if |
583 |
|
|
* one exists, else marks this task as complete. This method may be |
584 |
|
|
* useful in cases where {@code onCompletion} should not, or need |
585 |
|
|
* not, be invoked for each completer in a computation. |
586 |
dl |
1.15 |
*/ |
587 |
|
|
public final void propagateCompletion() { |
588 |
jsr166 |
1.54 |
CountedCompleter<?> a = this, s; |
589 |
dl |
1.15 |
for (int c;;) { |
590 |
|
|
if ((c = a.pending) == 0) { |
591 |
|
|
if ((a = (s = a).completer) == null) { |
592 |
|
|
s.quietlyComplete(); |
593 |
|
|
return; |
594 |
|
|
} |
595 |
|
|
} |
596 |
dl |
1.67 |
else if (a.weakCompareAndSetPendingCount(c, c - 1)) |
597 |
dl |
1.15 |
return; |
598 |
|
|
} |
599 |
|
|
} |
600 |
|
|
|
601 |
|
|
/** |
602 |
jsr166 |
1.31 |
* Regardless of pending count, invokes |
603 |
|
|
* {@link #onCompletion(CountedCompleter)}, marks this task as |
604 |
|
|
* complete and further triggers {@link #tryComplete} on this |
605 |
|
|
* task's completer, if one exists. The given rawResult is |
606 |
|
|
* used as an argument to {@link #setRawResult} before invoking |
607 |
|
|
* {@link #onCompletion(CountedCompleter)} or marking this task |
608 |
|
|
* as complete; its value is meaningful only for classes |
609 |
jsr166 |
1.40 |
* overriding {@code setRawResult}. This method does not modify |
610 |
|
|
* the pending count. |
611 |
dl |
1.14 |
* |
612 |
|
|
* <p>This method may be useful when forcing completion as soon as |
613 |
|
|
* any one (versus all) of several subtask results are obtained. |
614 |
|
|
* However, in the common (and recommended) case in which {@code |
615 |
|
|
* setRawResult} is not overridden, this effect can be obtained |
616 |
jsr166 |
1.50 |
* more simply using {@link #quietlyCompleteRoot()}. |
617 |
dl |
1.1 |
* |
618 |
dl |
1.4 |
* @param rawResult the raw result |
619 |
dl |
1.1 |
*/ |
620 |
dl |
1.4 |
public void complete(T rawResult) { |
621 |
|
|
CountedCompleter<?> p; |
622 |
dl |
1.14 |
setRawResult(rawResult); |
623 |
dl |
1.1 |
onCompletion(this); |
624 |
|
|
quietlyComplete(); |
625 |
|
|
if ((p = completer) != null) |
626 |
|
|
p.tryComplete(); |
627 |
|
|
} |
628 |
|
|
|
629 |
dl |
1.15 |
/** |
630 |
|
|
* If this task's pending count is zero, returns this task; |
631 |
jsr166 |
1.51 |
* otherwise decrements its pending count and returns {@code null}. |
632 |
|
|
* This method is designed to be used with {@link #nextComplete} in |
633 |
|
|
* completion traversal loops. |
634 |
dl |
1.15 |
* |
635 |
|
|
* @return this task, if pending count was zero, else {@code null} |
636 |
|
|
*/ |
637 |
|
|
public final CountedCompleter<?> firstComplete() { |
638 |
|
|
for (int c;;) { |
639 |
|
|
if ((c = pending) == 0) |
640 |
|
|
return this; |
641 |
dl |
1.67 |
else if (weakCompareAndSetPendingCount(c, c - 1)) |
642 |
dl |
1.15 |
return null; |
643 |
|
|
} |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
/** |
647 |
|
|
* If this task does not have a completer, invokes {@link |
648 |
|
|
* ForkJoinTask#quietlyComplete} and returns {@code null}. Or, if |
649 |
dl |
1.29 |
* the completer's pending count is non-zero, decrements that |
650 |
|
|
* pending count and returns {@code null}. Otherwise, returns the |
651 |
dl |
1.15 |
* completer. This method can be used as part of a completion |
652 |
jsr166 |
1.18 |
* traversal loop for homogeneous task hierarchies: |
653 |
dl |
1.15 |
* |
654 |
|
|
* <pre> {@code |
655 |
jsr166 |
1.17 |
* for (CountedCompleter<?> c = firstComplete(); |
656 |
|
|
* c != null; |
657 |
|
|
* c = c.nextComplete()) { |
658 |
dl |
1.15 |
* // ... process c ... |
659 |
|
|
* }}</pre> |
660 |
|
|
* |
661 |
|
|
* @return the completer, or {@code null} if none |
662 |
|
|
*/ |
663 |
|
|
public final CountedCompleter<?> nextComplete() { |
664 |
|
|
CountedCompleter<?> p; |
665 |
|
|
if ((p = completer) != null) |
666 |
|
|
return p.firstComplete(); |
667 |
|
|
else { |
668 |
|
|
quietlyComplete(); |
669 |
|
|
return null; |
670 |
|
|
} |
671 |
|
|
} |
672 |
|
|
|
673 |
|
|
/** |
674 |
|
|
* Equivalent to {@code getRoot().quietlyComplete()}. |
675 |
|
|
*/ |
676 |
|
|
public final void quietlyCompleteRoot() { |
677 |
|
|
for (CountedCompleter<?> a = this, p;;) { |
678 |
|
|
if ((p = a.completer) == null) { |
679 |
|
|
a.quietlyComplete(); |
680 |
|
|
return; |
681 |
|
|
} |
682 |
|
|
a = p; |
683 |
|
|
} |
684 |
|
|
} |
685 |
|
|
|
686 |
dl |
1.1 |
/** |
687 |
dl |
1.42 |
* If this task has not completed, attempts to process at most the |
688 |
|
|
* given number of other unprocessed tasks for which this task is |
689 |
dl |
1.43 |
* on the completion path, if any are known to exist. |
690 |
dl |
1.42 |
* |
691 |
dl |
1.44 |
* @param maxTasks the maximum number of tasks to process. If |
692 |
|
|
* less than or equal to zero, then no tasks are |
693 |
|
|
* processed. |
694 |
dl |
1.42 |
*/ |
695 |
|
|
public final void helpComplete(int maxTasks) { |
696 |
dl |
1.67 |
ForkJoinPool.WorkQueue q; Thread t; boolean owned; |
697 |
|
|
if (owned = (t = Thread.currentThread()) instanceof ForkJoinWorkerThread) |
698 |
|
|
q = ((ForkJoinWorkerThread)t).workQueue; |
699 |
|
|
else |
700 |
|
|
q = ForkJoinPool.commonQueue(); |
701 |
|
|
if (q != null && maxTasks > 0) |
702 |
|
|
q.helpComplete(this, owned, maxTasks); |
703 |
|
|
} |
704 |
|
|
|
705 |
|
|
// ForkJoinTask overrides |
706 |
|
|
|
707 |
|
|
/** |
708 |
jsr166 |
1.22 |
* Supports ForkJoinTask exception propagation. |
709 |
dl |
1.2 |
*/ |
710 |
dl |
1.67 |
@Override |
711 |
|
|
final int trySetException(Throwable ex) { |
712 |
|
|
CountedCompleter<?> a = this, p = a; |
713 |
|
|
do {} while (isExceptionalStatus(a.trySetThrown(ex)) && |
714 |
|
|
a.onExceptionalCompletion(ex, p) && |
715 |
|
|
(a = (p = a).completer) != null && a.status >= 0); |
716 |
|
|
return status; |
717 |
dl |
1.2 |
} |
718 |
|
|
|
719 |
|
|
/** |
720 |
jsr166 |
1.22 |
* Implements execution conventions for CountedCompleters. |
721 |
dl |
1.1 |
*/ |
722 |
dl |
1.67 |
@Override |
723 |
dl |
1.1 |
protected final boolean exec() { |
724 |
|
|
compute(); |
725 |
|
|
return false; |
726 |
|
|
} |
727 |
|
|
|
728 |
|
|
/** |
729 |
jsr166 |
1.45 |
* Returns the result of the computation. By default, |
730 |
dl |
1.4 |
* returns {@code null}, which is appropriate for {@code Void} |
731 |
dl |
1.15 |
* actions, but in other cases should be overridden, almost |
732 |
|
|
* always to return a field or function of a field that |
733 |
|
|
* holds the result upon completion. |
734 |
dl |
1.1 |
* |
735 |
dl |
1.4 |
* @return the result of the computation |
736 |
dl |
1.1 |
*/ |
737 |
dl |
1.67 |
@Override |
738 |
dl |
1.4 |
public T getRawResult() { return null; } |
739 |
dl |
1.1 |
|
740 |
|
|
/** |
741 |
dl |
1.4 |
* A method that result-bearing CountedCompleters may optionally |
742 |
|
|
* use to help maintain result data. By default, does nothing. |
743 |
dl |
1.15 |
* Overrides are not recommended. However, if this method is |
744 |
|
|
* overridden to update existing objects or fields, then it must |
745 |
|
|
* in general be defined to be thread-safe. |
746 |
dl |
1.1 |
*/ |
747 |
dl |
1.67 |
@Override |
748 |
dl |
1.4 |
protected void setRawResult(T t) { } |
749 |
dl |
1.1 |
|
750 |
dl |
1.70 |
/* |
751 |
|
|
This class uses |
752 |
|
|
* jdk-internal Unsafe for atomics and special memory modes, |
753 |
|
|
* rather than VarHandles, to avoid initialization dependencies in |
754 |
|
|
* other jdk components that require early parallelism. |
755 |
|
|
*/ |
756 |
|
|
private static final Unsafe U; |
757 |
|
|
private static final long PENDING; |
758 |
dl |
1.1 |
static { |
759 |
dl |
1.70 |
U = Unsafe.getUnsafe(); |
760 |
|
|
PENDING = U.objectFieldOffset(CountedCompleter.class, "pending"); |
761 |
dl |
1.1 |
} |
762 |
|
|
} |