author | dl |
Wed, 21 Jan 2015 09:46:21 +0000 | |
changeset 28540 | 04b399ec3dfc |
parent 26336 | 1eee453c24c1 |
child 32987 | e5e5ab01398e |
permissions | -rw-r--r-- |
16745 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. Oracle designates this |
|
7 |
* particular file as subject to the "Classpath" exception as provided |
|
8 |
* by Oracle in the LICENSE file that accompanied this code. |
|
9 |
* |
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
34 |
*/ |
|
35 |
||
36 |
package java.util.concurrent; |
|
37 |
import java.util.function.Supplier; |
|
38 |
import java.util.function.Consumer; |
|
39 |
import java.util.function.BiConsumer; |
|
40 |
import java.util.function.Function; |
|
41 |
import java.util.function.BiFunction; |
|
42 |
import java.util.concurrent.Future; |
|
43 |
import java.util.concurrent.TimeUnit; |
|
44 |
import java.util.concurrent.ForkJoinPool; |
|
45 |
import java.util.concurrent.ForkJoinTask; |
|
46 |
import java.util.concurrent.Executor; |
|
47 |
import java.util.concurrent.ThreadLocalRandom; |
|
48 |
import java.util.concurrent.ExecutionException; |
|
49 |
import java.util.concurrent.TimeoutException; |
|
50 |
import java.util.concurrent.CancellationException; |
|
19192 | 51 |
import java.util.concurrent.CompletionException; |
52 |
import java.util.concurrent.CompletionStage; |
|
16745 | 53 |
import java.util.concurrent.locks.LockSupport; |
54 |
||
55 |
/** |
|
56 |
* A {@link Future} that may be explicitly completed (setting its |
|
19192 | 57 |
* value and status), and may be used as a {@link CompletionStage}, |
58 |
* supporting dependent functions and actions that trigger upon its |
|
59 |
* completion. |
|
16745 | 60 |
* |
61 |
* <p>When two or more threads attempt to |
|
62 |
* {@link #complete complete}, |
|
63 |
* {@link #completeExceptionally completeExceptionally}, or |
|
64 |
* {@link #cancel cancel} |
|
65 |
* a CompletableFuture, only one of them succeeds. |
|
66 |
* |
|
19192 | 67 |
* <p>In addition to these and related methods for directly |
68 |
* manipulating status and results, CompletableFuture implements |
|
69 |
* interface {@link CompletionStage} with the following policies: <ul> |
|
16745 | 70 |
* |
19192 | 71 |
* <li>Actions supplied for dependent completions of |
72 |
* <em>non-async</em> methods may be performed by the thread that |
|
73 |
* completes the current CompletableFuture, or by any other caller of |
|
74 |
* a completion method.</li> |
|
75 |
* |
|
76 |
* <li>All <em>async</em> methods without an explicit Executor |
|
77 |
* argument are performed using the {@link ForkJoinPool#commonPool()} |
|
78 |
* (unless it does not support a parallelism level of at least two, in |
|
26336 | 79 |
* which case, a new Thread is created to run each task). To simplify |
80 |
* monitoring, debugging, and tracking, all generated asynchronous |
|
81 |
* tasks are instances of the marker interface {@link |
|
19192 | 82 |
* AsynchronousCompletionTask}. </li> |
16745 | 83 |
* |
19192 | 84 |
* <li>All CompletionStage methods are implemented independently of |
85 |
* other public methods, so the behavior of one method is not impacted |
|
86 |
* by overrides of others in subclasses. </li> </ul> |
|
16745 | 87 |
* |
19192 | 88 |
* <p>CompletableFuture also implements {@link Future} with the following |
89 |
* policies: <ul> |
|
16745 | 90 |
* |
19192 | 91 |
* <li>Since (unlike {@link FutureTask}) this class has no direct |
92 |
* control over the computation that causes it to be completed, |
|
93 |
* cancellation is treated as just another form of exceptional |
|
94 |
* completion. Method {@link #cancel cancel} has the same effect as |
|
95 |
* {@code completeExceptionally(new CancellationException())}. Method |
|
96 |
* {@link #isCompletedExceptionally} can be used to determine if a |
|
97 |
* CompletableFuture completed in any exceptional fashion.</li> |
|
16745 | 98 |
* |
19192 | 99 |
* <li>In case of exceptional completion with a CompletionException, |
16745 | 100 |
* methods {@link #get()} and {@link #get(long, TimeUnit)} throw an |
101 |
* {@link ExecutionException} with the same cause as held in the |
|
19192 | 102 |
* corresponding CompletionException. To simplify usage in most |
103 |
* contexts, this class also defines methods {@link #join()} and |
|
104 |
* {@link #getNow} that instead throw the CompletionException directly |
|
105 |
* in these cases.</li> </ul> |
|
16745 | 106 |
* |
107 |
* @author Doug Lea |
|
108 |
* @since 1.8 |
|
109 |
*/ |
|
19192 | 110 |
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { |
16745 | 111 |
|
112 |
/* |
|
113 |
* Overview: |
|
114 |
* |
|
26336 | 115 |
* A CompletableFuture may have dependent completion actions, |
116 |
* collected in a linked stack. It atomically completes by CASing |
|
117 |
* a result field, and then pops off and runs those actions. This |
|
118 |
* applies across normal vs exceptional outcomes, sync vs async |
|
119 |
* actions, binary triggers, and various forms of completions. |
|
120 |
* |
|
121 |
* Non-nullness of field result (set via CAS) indicates done. An |
|
122 |
* AltResult is used to box null as a result, as well as to hold |
|
123 |
* exceptions. Using a single field makes completion simple to |
|
124 |
* detect and trigger. Encoding and decoding is straightforward |
|
125 |
* but adds to the sprawl of trapping and associating exceptions |
|
126 |
* with targets. Minor simplifications rely on (static) NIL (to |
|
127 |
* box null results) being the only AltResult with a null |
|
128 |
* exception field, so we don't usually need explicit comparisons. |
|
129 |
* Even though some of the generics casts are unchecked (see |
|
130 |
* SuppressWarnings annotations), they are placed to be |
|
131 |
* appropriate even if checked. |
|
16745 | 132 |
* |
26336 | 133 |
* Dependent actions are represented by Completion objects linked |
134 |
* as Treiber stacks headed by field "stack". There are Completion |
|
135 |
* classes for each kind of action, grouped into single-input |
|
136 |
* (UniCompletion), two-input (BiCompletion), projected |
|
137 |
* (BiCompletions using either (not both) of two inputs), shared |
|
138 |
* (CoCompletion, used by the second of two sources), zero-input |
|
139 |
* source actions, and Signallers that unblock waiters. Class |
|
140 |
* Completion extends ForkJoinTask to enable async execution |
|
141 |
* (adding no space overhead because we exploit its "tag" methods |
|
142 |
* to maintain claims). It is also declared as Runnable to allow |
|
143 |
* usage with arbitrary executors. |
|
144 |
* |
|
145 |
* Support for each kind of CompletionStage relies on a separate |
|
146 |
* class, along with two CompletableFuture methods: |
|
147 |
* |
|
148 |
* * A Completion class with name X corresponding to function, |
|
149 |
* prefaced with "Uni", "Bi", or "Or". Each class contains |
|
150 |
* fields for source(s), actions, and dependent. They are |
|
151 |
* boringly similar, differing from others only with respect to |
|
152 |
* underlying functional forms. We do this so that users don't |
|
153 |
* encounter layers of adaptors in common usages. We also |
|
154 |
* include "Relay" classes/methods that don't correspond to user |
|
155 |
* methods; they copy results from one stage to another. |
|
156 |
* |
|
157 |
* * Boolean CompletableFuture method x(...) (for example |
|
158 |
* uniApply) takes all of the arguments needed to check that an |
|
159 |
* action is triggerable, and then either runs the action or |
|
160 |
* arranges its async execution by executing its Completion |
|
161 |
* argument, if present. The method returns true if known to be |
|
162 |
* complete. |
|
16745 | 163 |
* |
26336 | 164 |
* * Completion method tryFire(int mode) invokes the associated x |
165 |
* method with its held arguments, and on success cleans up. |
|
166 |
* The mode argument allows tryFire to be called twice (SYNC, |
|
167 |
* then ASYNC); the first to screen and trap exceptions while |
|
168 |
* arranging to execute, and the second when called from a |
|
169 |
* task. (A few classes are not used async so take slightly |
|
170 |
* different forms.) The claim() callback suppresses function |
|
171 |
* invocation if already claimed by another thread. |
|
172 |
* |
|
173 |
* * CompletableFuture method xStage(...) is called from a public |
|
174 |
* stage method of CompletableFuture x. It screens user |
|
175 |
* arguments and invokes and/or creates the stage object. If |
|
176 |
* not async and x is already complete, the action is run |
|
177 |
* immediately. Otherwise a Completion c is created, pushed to |
|
178 |
* x's stack (unless done), and started or triggered via |
|
179 |
* c.tryFire. This also covers races possible if x completes |
|
180 |
* while pushing. Classes with two inputs (for example BiApply) |
|
181 |
* deal with races across both while pushing actions. The |
|
182 |
* second completion is a CoCompletion pointing to the first, |
|
183 |
* shared so that at most one performs the action. The |
|
184 |
* multiple-arity methods allOf and anyOf do this pairwise to |
|
185 |
* form trees of completions. |
|
186 |
* |
|
187 |
* Note that the generic type parameters of methods vary according |
|
188 |
* to whether "this" is a source, dependent, or completion. |
|
16745 | 189 |
* |
26336 | 190 |
* Method postComplete is called upon completion unless the target |
191 |
* is guaranteed not to be observable (i.e., not yet returned or |
|
192 |
* linked). Multiple threads can call postComplete, which |
|
193 |
* atomically pops each dependent action, and tries to trigger it |
|
194 |
* via method tryFire, in NESTED mode. Triggering can propagate |
|
195 |
* recursively, so NESTED mode returns its completed dependent (if |
|
196 |
* one exists) for further processing by its caller (see method |
|
197 |
* postFire). |
|
198 |
* |
|
199 |
* Blocking methods get() and join() rely on Signaller Completions |
|
200 |
* that wake up waiting threads. The mechanics are similar to |
|
201 |
* Treiber stack wait-nodes used in FutureTask, Phaser, and |
|
202 |
* SynchronousQueue. See their internal documentation for |
|
203 |
* algorithmic details. |
|
204 |
* |
|
205 |
* Without precautions, CompletableFutures would be prone to |
|
206 |
* garbage accumulation as chains of Completions build up, each |
|
207 |
* pointing back to its sources. So we null out fields as soon as |
|
208 |
* possible (see especially method Completion.detach). The |
|
209 |
* screening checks needed anyway harmlessly ignore null arguments |
|
210 |
* that may have been obtained during races with threads nulling |
|
211 |
* out fields. We also try to unlink fired Completions from |
|
212 |
* stacks that might never be popped (see method postFire). |
|
213 |
* Completion fields need not be declared as final or volatile |
|
214 |
* because they are only visible to other threads upon safe |
|
215 |
* publication. |
|
16745 | 216 |
*/ |
217 |
||
26336 | 218 |
volatile Object result; // Either the result or boxed AltResult |
219 |
volatile Completion stack; // Top of Treiber stack of dependent actions |
|
220 |
||
221 |
final boolean internalComplete(Object r) { // CAS from null to r |
|
222 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, r); |
|
223 |
} |
|
224 |
||
225 |
final boolean casStack(Completion cmp, Completion val) { |
|
226 |
return UNSAFE.compareAndSwapObject(this, STACK, cmp, val); |
|
227 |
} |
|
228 |
||
229 |
/** Returns true if successfully pushed c onto stack. */ |
|
230 |
final boolean tryPushStack(Completion c) { |
|
231 |
Completion h = stack; |
|
232 |
lazySetNext(c, h); |
|
233 |
return UNSAFE.compareAndSwapObject(this, STACK, h, c); |
|
234 |
} |
|
235 |
||
236 |
/** Unconditionally pushes c onto stack, retrying if necessary. */ |
|
237 |
final void pushStack(Completion c) { |
|
238 |
do {} while (!tryPushStack(c)); |
|
239 |
} |
|
240 |
||
241 |
/* ------------- Encoding and decoding outcomes -------------- */ |
|
242 |
||
243 |
static final class AltResult { // See above |
|
244 |
final Throwable ex; // null only for NIL |
|
245 |
AltResult(Throwable x) { this.ex = x; } |
|
246 |
} |
|
16745 | 247 |
|
26336 | 248 |
/** The encoding of the null value. */ |
249 |
static final AltResult NIL = new AltResult(null); |
|
250 |
||
251 |
/** Completes with the null value, unless already completed. */ |
|
252 |
final boolean completeNull() { |
|
253 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, |
|
254 |
NIL); |
|
255 |
} |
|
256 |
||
257 |
/** Returns the encoding of the given non-exceptional value. */ |
|
258 |
final Object encodeValue(T t) { |
|
259 |
return (t == null) ? NIL : t; |
|
260 |
} |
|
261 |
||
262 |
/** Completes with a non-exceptional result, unless already completed. */ |
|
263 |
final boolean completeValue(T t) { |
|
264 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, |
|
265 |
(t == null) ? NIL : t); |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Returns the encoding of the given (non-null) exception as a |
|
270 |
* wrapped CompletionException unless it is one already. |
|
271 |
*/ |
|
272 |
static AltResult encodeThrowable(Throwable x) { |
|
273 |
return new AltResult((x instanceof CompletionException) ? x : |
|
274 |
new CompletionException(x)); |
|
275 |
} |
|
276 |
||
277 |
/** Completes with an exceptional result, unless already completed. */ |
|
278 |
final boolean completeThrowable(Throwable x) { |
|
279 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, |
|
280 |
encodeThrowable(x)); |
|
16745 | 281 |
} |
282 |
||
26336 | 283 |
/** |
284 |
* Returns the encoding of the given (non-null) exception as a |
|
285 |
* wrapped CompletionException unless it is one already. May |
|
286 |
* return the given Object r (which must have been the result of a |
|
287 |
* source future) if it is equivalent, i.e. if this is a simple |
|
288 |
* relay of an existing CompletionException. |
|
289 |
*/ |
|
290 |
static Object encodeThrowable(Throwable x, Object r) { |
|
291 |
if (!(x instanceof CompletionException)) |
|
292 |
x = new CompletionException(x); |
|
293 |
else if (r instanceof AltResult && x == ((AltResult)r).ex) |
|
294 |
return r; |
|
295 |
return new AltResult(x); |
|
296 |
} |
|
16745 | 297 |
|
26336 | 298 |
/** |
299 |
* Completes with the given (non-null) exceptional result as a |
|
300 |
* wrapped CompletionException unless it is one already, unless |
|
301 |
* already completed. May complete with the given Object r |
|
302 |
* (which must have been the result of a source future) if it is |
|
303 |
* equivalent, i.e. if this is a simple propagation of an |
|
304 |
* existing CompletionException. |
|
305 |
*/ |
|
306 |
final boolean completeThrowable(Throwable x, Object r) { |
|
307 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, |
|
308 |
encodeThrowable(x, r)); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Returns the encoding of the given arguments: if the exception |
|
313 |
* is non-null, encodes as AltResult. Otherwise uses the given |
|
314 |
* value, boxed as NIL if null. |
|
315 |
*/ |
|
316 |
Object encodeOutcome(T t, Throwable x) { |
|
317 |
return (x == null) ? (t == null) ? NIL : t : encodeThrowable(x); |
|
318 |
} |
|
16745 | 319 |
|
26336 | 320 |
/** |
321 |
* Returns the encoding of a copied outcome; if exceptional, |
|
322 |
* rewraps as a CompletionException, else returns argument. |
|
323 |
*/ |
|
324 |
static Object encodeRelay(Object r) { |
|
325 |
Throwable x; |
|
326 |
return (((r instanceof AltResult) && |
|
327 |
(x = ((AltResult)r).ex) != null && |
|
328 |
!(x instanceof CompletionException)) ? |
|
329 |
new AltResult(new CompletionException(x)) : r); |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Completes with r or a copy of r, unless already completed. |
|
334 |
* If exceptional, r is first coerced to a CompletionException. |
|
335 |
*/ |
|
336 |
final boolean completeRelay(Object r) { |
|
337 |
return UNSAFE.compareAndSwapObject(this, RESULT, null, |
|
338 |
encodeRelay(r)); |
|
339 |
} |
|
16745 | 340 |
|
26336 | 341 |
/** |
342 |
* Reports result using Future.get conventions. |
|
343 |
*/ |
|
344 |
private static <T> T reportGet(Object r) |
|
345 |
throws InterruptedException, ExecutionException { |
|
346 |
if (r == null) // by convention below, null means interrupted |
|
347 |
throw new InterruptedException(); |
|
348 |
if (r instanceof AltResult) { |
|
349 |
Throwable x, cause; |
|
350 |
if ((x = ((AltResult)r).ex) == null) |
|
351 |
return null; |
|
352 |
if (x instanceof CancellationException) |
|
353 |
throw (CancellationException)x; |
|
354 |
if ((x instanceof CompletionException) && |
|
355 |
(cause = x.getCause()) != null) |
|
356 |
x = cause; |
|
357 |
throw new ExecutionException(x); |
|
358 |
} |
|
359 |
@SuppressWarnings("unchecked") T t = (T) r; |
|
360 |
return t; |
|
361 |
} |
|
16745 | 362 |
|
363 |
/** |
|
26336 | 364 |
* Decodes outcome to return result or throw unchecked exception. |
365 |
*/ |
|
366 |
private static <T> T reportJoin(Object r) { |
|
367 |
if (r instanceof AltResult) { |
|
368 |
Throwable x; |
|
369 |
if ((x = ((AltResult)r).ex) == null) |
|
370 |
return null; |
|
371 |
if (x instanceof CancellationException) |
|
372 |
throw (CancellationException)x; |
|
373 |
if (x instanceof CompletionException) |
|
374 |
throw (CompletionException)x; |
|
375 |
throw new CompletionException(x); |
|
376 |
} |
|
377 |
@SuppressWarnings("unchecked") T t = (T) r; |
|
378 |
return t; |
|
379 |
} |
|
380 |
||
381 |
/* ------------- Async task preliminaries -------------- */ |
|
382 |
||
383 |
/** |
|
384 |
* A marker interface identifying asynchronous tasks produced by |
|
385 |
* {@code async} methods. This may be useful for monitoring, |
|
386 |
* debugging, and tracking asynchronous activities. |
|
387 |
* |
|
388 |
* @since 1.8 |
|
389 |
*/ |
|
390 |
public static interface AsynchronousCompletionTask { |
|
391 |
} |
|
392 |
||
393 |
private static final boolean useCommonPool = |
|
394 |
(ForkJoinPool.getCommonPoolParallelism() > 1); |
|
395 |
||
396 |
/** |
|
397 |
* Default executor -- ForkJoinPool.commonPool() unless it cannot |
|
398 |
* support parallelism. |
|
399 |
*/ |
|
400 |
private static final Executor asyncPool = useCommonPool ? |
|
401 |
ForkJoinPool.commonPool() : new ThreadPerTaskExecutor(); |
|
402 |
||
403 |
/** Fallback if ForkJoinPool.commonPool() cannot support parallelism */ |
|
404 |
static final class ThreadPerTaskExecutor implements Executor { |
|
405 |
public void execute(Runnable r) { new Thread(r).start(); } |
|
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* Null-checks user executor argument, and translates uses of |
|
410 |
* commonPool to asyncPool in case parallelism disabled. |
|
411 |
*/ |
|
412 |
static Executor screenExecutor(Executor e) { |
|
413 |
if (!useCommonPool && e == ForkJoinPool.commonPool()) |
|
414 |
return asyncPool; |
|
415 |
if (e == null) throw new NullPointerException(); |
|
416 |
return e; |
|
417 |
} |
|
418 |
||
419 |
// Modes for Completion.tryFire. Signedness matters. |
|
420 |
static final int SYNC = 0; |
|
421 |
static final int ASYNC = 1; |
|
422 |
static final int NESTED = -1; |
|
423 |
||
424 |
/* ------------- Base Completion classes and operations -------------- */ |
|
425 |
||
426 |
@SuppressWarnings("serial") |
|
427 |
abstract static class Completion extends ForkJoinTask<Void> |
|
428 |
implements Runnable, AsynchronousCompletionTask { |
|
429 |
volatile Completion next; // Treiber stack link |
|
430 |
||
431 |
/** |
|
432 |
* Performs completion action if triggered, returning a |
|
433 |
* dependent that may need propagation, if one exists. |
|
434 |
* |
|
435 |
* @param mode SYNC, ASYNC, or NESTED |
|
436 |
*/ |
|
437 |
abstract CompletableFuture<?> tryFire(int mode); |
|
438 |
||
439 |
/** Returns true if possibly still triggerable. Used by cleanStack. */ |
|
440 |
abstract boolean isLive(); |
|
441 |
||
442 |
public final void run() { tryFire(ASYNC); } |
|
443 |
public final boolean exec() { tryFire(ASYNC); return true; } |
|
444 |
public final Void getRawResult() { return null; } |
|
445 |
public final void setRawResult(Void v) {} |
|
446 |
} |
|
447 |
||
448 |
static void lazySetNext(Completion c, Completion next) { |
|
449 |
UNSAFE.putOrderedObject(c, NEXT, next); |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Pops and tries to trigger all reachable dependents. Call only |
|
454 |
* when known to be done. |
|
16745 | 455 |
*/ |
456 |
final void postComplete() { |
|
26336 | 457 |
/* |
458 |
* On each step, variable f holds current dependents to pop |
|
459 |
* and run. It is extended along only one path at a time, |
|
460 |
* pushing others to avoid unbounded recursion. |
|
461 |
*/ |
|
462 |
CompletableFuture<?> f = this; Completion h; |
|
463 |
while ((h = f.stack) != null || |
|
464 |
(f != this && (h = (f = this).stack) != null)) { |
|
465 |
CompletableFuture<?> d; Completion t; |
|
466 |
if (f.casStack(h, t = h.next)) { |
|
467 |
if (t != null) { |
|
468 |
if (f != this) { |
|
469 |
pushStack(h); |
|
470 |
continue; |
|
471 |
} |
|
472 |
h.next = null; // detach |
|
473 |
} |
|
474 |
f = (d = h.tryFire(NESTED)) == null ? this : d; |
|
16745 | 475 |
} |
476 |
} |
|
26336 | 477 |
} |
16745 | 478 |
|
26336 | 479 |
/** Traverses stack and unlinks dead Completions. */ |
480 |
final void cleanStack() { |
|
481 |
for (Completion p = null, q = stack; q != null;) { |
|
482 |
Completion s = q.next; |
|
483 |
if (q.isLive()) { |
|
484 |
p = q; |
|
485 |
q = s; |
|
486 |
} |
|
487 |
else if (p == null) { |
|
488 |
casStack(q, s); |
|
489 |
q = stack; |
|
490 |
} |
|
491 |
else { |
|
492 |
p.next = s; |
|
493 |
if (p.isLive()) |
|
494 |
q = s; |
|
495 |
else { |
|
496 |
p = null; // restart |
|
497 |
q = stack; |
|
498 |
} |
|
499 |
} |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
/* ------------- One-input Completions -------------- */ |
|
504 |
||
505 |
/** A Completion with a source, dependent, and executor. */ |
|
506 |
@SuppressWarnings("serial") |
|
507 |
abstract static class UniCompletion<T,V> extends Completion { |
|
508 |
Executor executor; // executor to use (null if none) |
|
509 |
CompletableFuture<V> dep; // the dependent to complete |
|
510 |
CompletableFuture<T> src; // source for action |
|
511 |
||
512 |
UniCompletion(Executor executor, CompletableFuture<V> dep, |
|
513 |
CompletableFuture<T> src) { |
|
514 |
this.executor = executor; this.dep = dep; this.src = src; |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* Returns true if action can be run. Call only when known to |
|
519 |
* be triggerable. Uses FJ tag bit to ensure that only one |
|
520 |
* thread claims ownership. If async, starts as task -- a |
|
521 |
* later call to tryFire will run action. |
|
522 |
*/ |
|
523 |
final boolean claim() { |
|
524 |
Executor e = executor; |
|
525 |
if (compareAndSetForkJoinTaskTag((short)0, (short)1)) { |
|
526 |
if (e == null) |
|
527 |
return true; |
|
528 |
executor = null; // disable |
|
529 |
e.execute(this); |
|
530 |
} |
|
531 |
return false; |
|
532 |
} |
|
533 |
||
534 |
final boolean isLive() { return dep != null; } |
|
535 |
} |
|
536 |
||
537 |
/** Pushes the given completion (if it exists) unless done. */ |
|
538 |
final void push(UniCompletion<?,?> c) { |
|
539 |
if (c != null) { |
|
540 |
while (result == null && !tryPushStack(c)) |
|
541 |
lazySetNext(c, null); // clear on failure |
|
16745 | 542 |
} |
543 |
} |
|
544 |
||
545 |
/** |
|
26336 | 546 |
* Post-processing by dependent after successful UniCompletion |
547 |
* tryFire. Tries to clean stack of source a, and then either runs |
|
548 |
* postComplete or returns this to caller, depending on mode. |
|
16745 | 549 |
*/ |
26336 | 550 |
final CompletableFuture<T> postFire(CompletableFuture<?> a, int mode) { |
551 |
if (a != null && a.stack != null) { |
|
552 |
if (mode < 0 || a.result == null) |
|
553 |
a.cleanStack(); |
|
554 |
else |
|
555 |
a.postComplete(); |
|
556 |
} |
|
557 |
if (result != null && stack != null) { |
|
558 |
if (mode < 0) |
|
559 |
return this; |
|
560 |
else |
|
561 |
postComplete(); |
|
562 |
} |
|
563 |
return null; |
|
564 |
} |
|
565 |
||
566 |
@SuppressWarnings("serial") |
|
567 |
static final class UniApply<T,V> extends UniCompletion<T,V> { |
|
568 |
Function<? super T,? extends V> fn; |
|
569 |
UniApply(Executor executor, CompletableFuture<V> dep, |
|
570 |
CompletableFuture<T> src, |
|
571 |
Function<? super T,? extends V> fn) { |
|
572 |
super(executor, dep, src); this.fn = fn; |
|
573 |
} |
|
574 |
final CompletableFuture<V> tryFire(int mode) { |
|
575 |
CompletableFuture<V> d; CompletableFuture<T> a; |
|
576 |
if ((d = dep) == null || |
|
577 |
!d.uniApply(a = src, fn, mode > 0 ? null : this)) |
|
578 |
return null; |
|
579 |
dep = null; src = null; fn = null; |
|
580 |
return d.postFire(a, mode); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
final <S> boolean uniApply(CompletableFuture<S> a, |
|
585 |
Function<? super S,? extends T> f, |
|
586 |
UniApply<S,T> c) { |
|
587 |
Object r; Throwable x; |
|
588 |
if (a == null || (r = a.result) == null || f == null) |
|
589 |
return false; |
|
590 |
tryComplete: if (result == null) { |
|
591 |
if (r instanceof AltResult) { |
|
592 |
if ((x = ((AltResult)r).ex) != null) { |
|
593 |
completeThrowable(x, r); |
|
594 |
break tryComplete; |
|
595 |
} |
|
596 |
r = null; |
|
597 |
} |
|
598 |
try { |
|
599 |
if (c != null && !c.claim()) |
|
600 |
return false; |
|
601 |
@SuppressWarnings("unchecked") S s = (S) r; |
|
602 |
completeValue(f.apply(s)); |
|
603 |
} catch (Throwable ex) { |
|
604 |
completeThrowable(ex); |
|
605 |
} |
|
606 |
} |
|
607 |
return true; |
|
608 |
} |
|
609 |
||
610 |
private <V> CompletableFuture<V> uniApplyStage( |
|
611 |
Executor e, Function<? super T,? extends V> f) { |
|
612 |
if (f == null) throw new NullPointerException(); |
|
613 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
|
614 |
if (e != null || !d.uniApply(this, f, null)) { |
|
615 |
UniApply<T,V> c = new UniApply<T,V>(e, d, this, f); |
|
616 |
push(c); |
|
617 |
c.tryFire(SYNC); |
|
618 |
} |
|
619 |
return d; |
|
620 |
} |
|
621 |
||
622 |
@SuppressWarnings("serial") |
|
623 |
static final class UniAccept<T> extends UniCompletion<T,Void> { |
|
624 |
Consumer<? super T> fn; |
|
625 |
UniAccept(Executor executor, CompletableFuture<Void> dep, |
|
626 |
CompletableFuture<T> src, Consumer<? super T> fn) { |
|
627 |
super(executor, dep, src); this.fn = fn; |
|
628 |
} |
|
629 |
final CompletableFuture<Void> tryFire(int mode) { |
|
630 |
CompletableFuture<Void> d; CompletableFuture<T> a; |
|
631 |
if ((d = dep) == null || |
|
632 |
!d.uniAccept(a = src, fn, mode > 0 ? null : this)) |
|
633 |
return null; |
|
634 |
dep = null; src = null; fn = null; |
|
635 |
return d.postFire(a, mode); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
final <S> boolean uniAccept(CompletableFuture<S> a, |
|
640 |
Consumer<? super S> f, UniAccept<S> c) { |
|
641 |
Object r; Throwable x; |
|
642 |
if (a == null || (r = a.result) == null || f == null) |
|
643 |
return false; |
|
644 |
tryComplete: if (result == null) { |
|
645 |
if (r instanceof AltResult) { |
|
646 |
if ((x = ((AltResult)r).ex) != null) { |
|
647 |
completeThrowable(x, r); |
|
648 |
break tryComplete; |
|
649 |
} |
|
650 |
r = null; |
|
651 |
} |
|
652 |
try { |
|
653 |
if (c != null && !c.claim()) |
|
654 |
return false; |
|
655 |
@SuppressWarnings("unchecked") S s = (S) r; |
|
656 |
f.accept(s); |
|
657 |
completeNull(); |
|
658 |
} catch (Throwable ex) { |
|
659 |
completeThrowable(ex); |
|
660 |
} |
|
661 |
} |
|
662 |
return true; |
|
663 |
} |
|
664 |
||
665 |
private CompletableFuture<Void> uniAcceptStage(Executor e, |
|
666 |
Consumer<? super T> f) { |
|
667 |
if (f == null) throw new NullPointerException(); |
|
668 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
669 |
if (e != null || !d.uniAccept(this, f, null)) { |
|
670 |
UniAccept<T> c = new UniAccept<T>(e, d, this, f); |
|
671 |
push(c); |
|
672 |
c.tryFire(SYNC); |
|
673 |
} |
|
674 |
return d; |
|
675 |
} |
|
676 |
||
677 |
@SuppressWarnings("serial") |
|
678 |
static final class UniRun<T> extends UniCompletion<T,Void> { |
|
679 |
Runnable fn; |
|
680 |
UniRun(Executor executor, CompletableFuture<Void> dep, |
|
681 |
CompletableFuture<T> src, Runnable fn) { |
|
682 |
super(executor, dep, src); this.fn = fn; |
|
683 |
} |
|
684 |
final CompletableFuture<Void> tryFire(int mode) { |
|
685 |
CompletableFuture<Void> d; CompletableFuture<T> a; |
|
686 |
if ((d = dep) == null || |
|
687 |
!d.uniRun(a = src, fn, mode > 0 ? null : this)) |
|
688 |
return null; |
|
689 |
dep = null; src = null; fn = null; |
|
690 |
return d.postFire(a, mode); |
|
691 |
} |
|
692 |
} |
|
693 |
||
694 |
final boolean uniRun(CompletableFuture<?> a, Runnable f, UniRun<?> c) { |
|
695 |
Object r; Throwable x; |
|
696 |
if (a == null || (r = a.result) == null || f == null) |
|
697 |
return false; |
|
698 |
if (result == null) { |
|
699 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
|
700 |
completeThrowable(x, r); |
|
701 |
else |
|
702 |
try { |
|
703 |
if (c != null && !c.claim()) |
|
704 |
return false; |
|
705 |
f.run(); |
|
706 |
completeNull(); |
|
707 |
} catch (Throwable ex) { |
|
708 |
completeThrowable(ex); |
|
709 |
} |
|
710 |
} |
|
711 |
return true; |
|
712 |
} |
|
713 |
||
714 |
private CompletableFuture<Void> uniRunStage(Executor e, Runnable f) { |
|
715 |
if (f == null) throw new NullPointerException(); |
|
716 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
717 |
if (e != null || !d.uniRun(this, f, null)) { |
|
718 |
UniRun<T> c = new UniRun<T>(e, d, this, f); |
|
719 |
push(c); |
|
720 |
c.tryFire(SYNC); |
|
721 |
} |
|
722 |
return d; |
|
723 |
} |
|
724 |
||
725 |
@SuppressWarnings("serial") |
|
726 |
static final class UniWhenComplete<T> extends UniCompletion<T,T> { |
|
727 |
BiConsumer<? super T, ? super Throwable> fn; |
|
728 |
UniWhenComplete(Executor executor, CompletableFuture<T> dep, |
|
729 |
CompletableFuture<T> src, |
|
730 |
BiConsumer<? super T, ? super Throwable> fn) { |
|
731 |
super(executor, dep, src); this.fn = fn; |
|
732 |
} |
|
733 |
final CompletableFuture<T> tryFire(int mode) { |
|
734 |
CompletableFuture<T> d; CompletableFuture<T> a; |
|
735 |
if ((d = dep) == null || |
|
736 |
!d.uniWhenComplete(a = src, fn, mode > 0 ? null : this)) |
|
737 |
return null; |
|
738 |
dep = null; src = null; fn = null; |
|
739 |
return d.postFire(a, mode); |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
final boolean uniWhenComplete(CompletableFuture<T> a, |
|
744 |
BiConsumer<? super T,? super Throwable> f, |
|
745 |
UniWhenComplete<T> c) { |
|
746 |
Object r; T t; Throwable x = null; |
|
747 |
if (a == null || (r = a.result) == null || f == null) |
|
748 |
return false; |
|
749 |
if (result == null) { |
|
750 |
try { |
|
751 |
if (c != null && !c.claim()) |
|
752 |
return false; |
|
753 |
if (r instanceof AltResult) { |
|
754 |
x = ((AltResult)r).ex; |
|
755 |
t = null; |
|
756 |
} else { |
|
757 |
@SuppressWarnings("unchecked") T tr = (T) r; |
|
758 |
t = tr; |
|
759 |
} |
|
760 |
f.accept(t, x); |
|
761 |
if (x == null) { |
|
762 |
internalComplete(r); |
|
763 |
return true; |
|
764 |
} |
|
765 |
} catch (Throwable ex) { |
|
766 |
if (x == null) |
|
767 |
x = ex; |
|
768 |
} |
|
769 |
completeThrowable(x, r); |
|
770 |
} |
|
771 |
return true; |
|
772 |
} |
|
773 |
||
774 |
private CompletableFuture<T> uniWhenCompleteStage( |
|
775 |
Executor e, BiConsumer<? super T, ? super Throwable> f) { |
|
776 |
if (f == null) throw new NullPointerException(); |
|
777 |
CompletableFuture<T> d = new CompletableFuture<T>(); |
|
778 |
if (e != null || !d.uniWhenComplete(this, f, null)) { |
|
779 |
UniWhenComplete<T> c = new UniWhenComplete<T>(e, d, this, f); |
|
780 |
push(c); |
|
781 |
c.tryFire(SYNC); |
|
782 |
} |
|
783 |
return d; |
|
784 |
} |
|
785 |
||
786 |
@SuppressWarnings("serial") |
|
787 |
static final class UniHandle<T,V> extends UniCompletion<T,V> { |
|
788 |
BiFunction<? super T, Throwable, ? extends V> fn; |
|
789 |
UniHandle(Executor executor, CompletableFuture<V> dep, |
|
790 |
CompletableFuture<T> src, |
|
791 |
BiFunction<? super T, Throwable, ? extends V> fn) { |
|
792 |
super(executor, dep, src); this.fn = fn; |
|
793 |
} |
|
794 |
final CompletableFuture<V> tryFire(int mode) { |
|
795 |
CompletableFuture<V> d; CompletableFuture<T> a; |
|
796 |
if ((d = dep) == null || |
|
797 |
!d.uniHandle(a = src, fn, mode > 0 ? null : this)) |
|
798 |
return null; |
|
799 |
dep = null; src = null; fn = null; |
|
800 |
return d.postFire(a, mode); |
|
801 |
} |
|
16745 | 802 |
} |
803 |
||
26336 | 804 |
final <S> boolean uniHandle(CompletableFuture<S> a, |
805 |
BiFunction<? super S, Throwable, ? extends T> f, |
|
806 |
UniHandle<S,T> c) { |
|
807 |
Object r; S s; Throwable x; |
|
808 |
if (a == null || (r = a.result) == null || f == null) |
|
809 |
return false; |
|
810 |
if (result == null) { |
|
811 |
try { |
|
812 |
if (c != null && !c.claim()) |
|
813 |
return false; |
|
814 |
if (r instanceof AltResult) { |
|
815 |
x = ((AltResult)r).ex; |
|
816 |
s = null; |
|
817 |
} else { |
|
818 |
x = null; |
|
819 |
@SuppressWarnings("unchecked") S ss = (S) r; |
|
820 |
s = ss; |
|
821 |
} |
|
822 |
completeValue(f.apply(s, x)); |
|
823 |
} catch (Throwable ex) { |
|
824 |
completeThrowable(ex); |
|
825 |
} |
|
826 |
} |
|
827 |
return true; |
|
828 |
} |
|
829 |
||
830 |
private <V> CompletableFuture<V> uniHandleStage( |
|
831 |
Executor e, BiFunction<? super T, Throwable, ? extends V> f) { |
|
832 |
if (f == null) throw new NullPointerException(); |
|
833 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
|
834 |
if (e != null || !d.uniHandle(this, f, null)) { |
|
835 |
UniHandle<T,V> c = new UniHandle<T,V>(e, d, this, f); |
|
836 |
push(c); |
|
837 |
c.tryFire(SYNC); |
|
838 |
} |
|
839 |
return d; |
|
840 |
} |
|
841 |
||
842 |
@SuppressWarnings("serial") |
|
843 |
static final class UniExceptionally<T> extends UniCompletion<T,T> { |
|
844 |
Function<? super Throwable, ? extends T> fn; |
|
845 |
UniExceptionally(CompletableFuture<T> dep, CompletableFuture<T> src, |
|
846 |
Function<? super Throwable, ? extends T> fn) { |
|
847 |
super(null, dep, src); this.fn = fn; |
|
848 |
} |
|
849 |
final CompletableFuture<T> tryFire(int mode) { // never ASYNC |
|
850 |
// assert mode != ASYNC; |
|
851 |
CompletableFuture<T> d; CompletableFuture<T> a; |
|
852 |
if ((d = dep) == null || !d.uniExceptionally(a = src, fn, this)) |
|
853 |
return null; |
|
854 |
dep = null; src = null; fn = null; |
|
855 |
return d.postFire(a, mode); |
|
856 |
} |
|
857 |
} |
|
858 |
||
859 |
final boolean uniExceptionally(CompletableFuture<T> a, |
|
860 |
Function<? super Throwable, ? extends T> f, |
|
861 |
UniExceptionally<T> c) { |
|
862 |
Object r; Throwable x; |
|
863 |
if (a == null || (r = a.result) == null || f == null) |
|
864 |
return false; |
|
865 |
if (result == null) { |
|
866 |
try { |
|
867 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) { |
|
868 |
if (c != null && !c.claim()) |
|
869 |
return false; |
|
870 |
completeValue(f.apply(x)); |
|
871 |
} else |
|
872 |
internalComplete(r); |
|
873 |
} catch (Throwable ex) { |
|
874 |
completeThrowable(ex); |
|
875 |
} |
|
876 |
} |
|
877 |
return true; |
|
878 |
} |
|
879 |
||
880 |
private CompletableFuture<T> uniExceptionallyStage( |
|
881 |
Function<Throwable, ? extends T> f) { |
|
882 |
if (f == null) throw new NullPointerException(); |
|
883 |
CompletableFuture<T> d = new CompletableFuture<T>(); |
|
884 |
if (!d.uniExceptionally(this, f, null)) { |
|
885 |
UniExceptionally<T> c = new UniExceptionally<T>(d, this, f); |
|
886 |
push(c); |
|
887 |
c.tryFire(SYNC); |
|
888 |
} |
|
889 |
return d; |
|
890 |
} |
|
891 |
||
892 |
@SuppressWarnings("serial") |
|
893 |
static final class UniRelay<T> extends UniCompletion<T,T> { // for Compose |
|
894 |
UniRelay(CompletableFuture<T> dep, CompletableFuture<T> src) { |
|
895 |
super(null, dep, src); |
|
896 |
} |
|
897 |
final CompletableFuture<T> tryFire(int mode) { |
|
898 |
CompletableFuture<T> d; CompletableFuture<T> a; |
|
899 |
if ((d = dep) == null || !d.uniRelay(a = src)) |
|
900 |
return null; |
|
901 |
src = null; dep = null; |
|
902 |
return d.postFire(a, mode); |
|
903 |
} |
|
904 |
} |
|
905 |
||
906 |
final boolean uniRelay(CompletableFuture<T> a) { |
|
907 |
Object r; |
|
908 |
if (a == null || (r = a.result) == null) |
|
909 |
return false; |
|
910 |
if (result == null) // no need to claim |
|
911 |
completeRelay(r); |
|
912 |
return true; |
|
913 |
} |
|
914 |
||
915 |
@SuppressWarnings("serial") |
|
916 |
static final class UniCompose<T,V> extends UniCompletion<T,V> { |
|
917 |
Function<? super T, ? extends CompletionStage<V>> fn; |
|
918 |
UniCompose(Executor executor, CompletableFuture<V> dep, |
|
919 |
CompletableFuture<T> src, |
|
920 |
Function<? super T, ? extends CompletionStage<V>> fn) { |
|
921 |
super(executor, dep, src); this.fn = fn; |
|
922 |
} |
|
923 |
final CompletableFuture<V> tryFire(int mode) { |
|
924 |
CompletableFuture<V> d; CompletableFuture<T> a; |
|
925 |
if ((d = dep) == null || |
|
926 |
!d.uniCompose(a = src, fn, mode > 0 ? null : this)) |
|
927 |
return null; |
|
928 |
dep = null; src = null; fn = null; |
|
929 |
return d.postFire(a, mode); |
|
930 |
} |
|
931 |
} |
|
932 |
||
933 |
final <S> boolean uniCompose( |
|
934 |
CompletableFuture<S> a, |
|
935 |
Function<? super S, ? extends CompletionStage<T>> f, |
|
936 |
UniCompose<S,T> c) { |
|
937 |
Object r; Throwable x; |
|
938 |
if (a == null || (r = a.result) == null || f == null) |
|
939 |
return false; |
|
940 |
tryComplete: if (result == null) { |
|
941 |
if (r instanceof AltResult) { |
|
942 |
if ((x = ((AltResult)r).ex) != null) { |
|
943 |
completeThrowable(x, r); |
|
944 |
break tryComplete; |
|
945 |
} |
|
946 |
r = null; |
|
947 |
} |
|
948 |
try { |
|
949 |
if (c != null && !c.claim()) |
|
950 |
return false; |
|
951 |
@SuppressWarnings("unchecked") S s = (S) r; |
|
952 |
CompletableFuture<T> g = f.apply(s).toCompletableFuture(); |
|
953 |
if (g.result == null || !uniRelay(g)) { |
|
954 |
UniRelay<T> copy = new UniRelay<T>(this, g); |
|
955 |
g.push(copy); |
|
956 |
copy.tryFire(SYNC); |
|
957 |
if (result == null) |
|
958 |
return false; |
|
959 |
} |
|
960 |
} catch (Throwable ex) { |
|
961 |
completeThrowable(ex); |
|
962 |
} |
|
963 |
} |
|
964 |
return true; |
|
965 |
} |
|
966 |
||
967 |
private <V> CompletableFuture<V> uniComposeStage( |
|
968 |
Executor e, Function<? super T, ? extends CompletionStage<V>> f) { |
|
969 |
if (f == null) throw new NullPointerException(); |
|
970 |
Object r; Throwable x; |
|
971 |
if (e == null && (r = result) != null) { |
|
972 |
// try to return function result directly |
|
973 |
if (r instanceof AltResult) { |
|
974 |
if ((x = ((AltResult)r).ex) != null) { |
|
975 |
return new CompletableFuture<V>(encodeThrowable(x, r)); |
|
976 |
} |
|
977 |
r = null; |
|
978 |
} |
|
979 |
try { |
|
980 |
@SuppressWarnings("unchecked") T t = (T) r; |
|
28540
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
981 |
CompletableFuture<V> g = f.apply(t).toCompletableFuture(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
982 |
Object s = g.result; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
983 |
if (s != null) |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
984 |
return new CompletableFuture<V>(encodeRelay(s)); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
985 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
986 |
UniRelay<V> copy = new UniRelay<V>(d, g); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
987 |
g.push(copy); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
988 |
copy.tryFire(SYNC); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
26336
diff
changeset
|
989 |
return d; |
26336 | 990 |
} catch (Throwable ex) { |
991 |
return new CompletableFuture<V>(encodeThrowable(ex)); |
|
992 |
} |
|
993 |
} |
|
994 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
|
995 |
UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f); |
|
996 |
push(c); |
|
997 |
c.tryFire(SYNC); |
|
998 |
return d; |
|
999 |
} |
|
1000 |
||
1001 |
/* ------------- Two-input Completions -------------- */ |
|
1002 |
||
1003 |
/** A Completion for an action with two sources */ |
|
1004 |
@SuppressWarnings("serial") |
|
1005 |
abstract static class BiCompletion<T,U,V> extends UniCompletion<T,V> { |
|
1006 |
CompletableFuture<U> snd; // second source for action |
|
1007 |
BiCompletion(Executor executor, CompletableFuture<V> dep, |
|
1008 |
CompletableFuture<T> src, CompletableFuture<U> snd) { |
|
1009 |
super(executor, dep, src); this.snd = snd; |
|
1010 |
} |
|
1011 |
} |
|
1012 |
||
1013 |
/** A Completion delegating to a BiCompletion */ |
|
1014 |
@SuppressWarnings("serial") |
|
1015 |
static final class CoCompletion extends Completion { |
|
1016 |
BiCompletion<?,?,?> base; |
|
1017 |
CoCompletion(BiCompletion<?,?,?> base) { this.base = base; } |
|
1018 |
final CompletableFuture<?> tryFire(int mode) { |
|
1019 |
BiCompletion<?,?,?> c; CompletableFuture<?> d; |
|
1020 |
if ((c = base) == null || (d = c.tryFire(mode)) == null) |
|
1021 |
return null; |
|
1022 |
base = null; // detach |
|
1023 |
return d; |
|
1024 |
} |
|
1025 |
final boolean isLive() { |
|
1026 |
BiCompletion<?,?,?> c; |
|
1027 |
return (c = base) != null && c.dep != null; |
|
1028 |
} |
|
1029 |
} |
|
1030 |
||
1031 |
/** Pushes completion to this and b unless both done. */ |
|
1032 |
final void bipush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
|
1033 |
if (c != null) { |
|
1034 |
Object r; |
|
1035 |
while ((r = result) == null && !tryPushStack(c)) |
|
1036 |
lazySetNext(c, null); // clear on failure |
|
1037 |
if (b != null && b != this && b.result == null) { |
|
1038 |
Completion q = (r != null) ? c : new CoCompletion(c); |
|
1039 |
while (b.result == null && !b.tryPushStack(q)) |
|
1040 |
lazySetNext(q, null); // clear on failure |
|
1041 |
} |
|
1042 |
} |
|
1043 |
} |
|
1044 |
||
1045 |
/** Post-processing after successful BiCompletion tryFire. */ |
|
1046 |
final CompletableFuture<T> postFire(CompletableFuture<?> a, |
|
1047 |
CompletableFuture<?> b, int mode) { |
|
1048 |
if (b != null && b.stack != null) { // clean second source |
|
1049 |
if (mode < 0 || b.result == null) |
|
1050 |
b.cleanStack(); |
|
1051 |
else |
|
1052 |
b.postComplete(); |
|
1053 |
} |
|
1054 |
return postFire(a, mode); |
|
1055 |
} |
|
1056 |
||
1057 |
@SuppressWarnings("serial") |
|
1058 |
static final class BiApply<T,U,V> extends BiCompletion<T,U,V> { |
|
1059 |
BiFunction<? super T,? super U,? extends V> fn; |
|
1060 |
BiApply(Executor executor, CompletableFuture<V> dep, |
|
1061 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
|
1062 |
BiFunction<? super T,? super U,? extends V> fn) { |
|
1063 |
super(executor, dep, src, snd); this.fn = fn; |
|
1064 |
} |
|
1065 |
final CompletableFuture<V> tryFire(int mode) { |
|
1066 |
CompletableFuture<V> d; |
|
1067 |
CompletableFuture<T> a; |
|
1068 |
CompletableFuture<U> b; |
|
1069 |
if ((d = dep) == null || |
|
1070 |
!d.biApply(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1071 |
return null; |
|
1072 |
dep = null; src = null; snd = null; fn = null; |
|
1073 |
return d.postFire(a, b, mode); |
|
1074 |
} |
|
16745 | 1075 |
} |
1076 |
||
26336 | 1077 |
final <R,S> boolean biApply(CompletableFuture<R> a, |
1078 |
CompletableFuture<S> b, |
|
1079 |
BiFunction<? super R,? super S,? extends T> f, |
|
1080 |
BiApply<R,S,T> c) { |
|
1081 |
Object r, s; Throwable x; |
|
1082 |
if (a == null || (r = a.result) == null || |
|
1083 |
b == null || (s = b.result) == null || f == null) |
|
1084 |
return false; |
|
1085 |
tryComplete: if (result == null) { |
|
1086 |
if (r instanceof AltResult) { |
|
1087 |
if ((x = ((AltResult)r).ex) != null) { |
|
1088 |
completeThrowable(x, r); |
|
1089 |
break tryComplete; |
|
1090 |
} |
|
1091 |
r = null; |
|
1092 |
} |
|
1093 |
if (s instanceof AltResult) { |
|
1094 |
if ((x = ((AltResult)s).ex) != null) { |
|
1095 |
completeThrowable(x, s); |
|
1096 |
break tryComplete; |
|
1097 |
} |
|
1098 |
s = null; |
|
1099 |
} |
|
1100 |
try { |
|
1101 |
if (c != null && !c.claim()) |
|
1102 |
return false; |
|
1103 |
@SuppressWarnings("unchecked") R rr = (R) r; |
|
1104 |
@SuppressWarnings("unchecked") S ss = (S) s; |
|
1105 |
completeValue(f.apply(rr, ss)); |
|
1106 |
} catch (Throwable ex) { |
|
1107 |
completeThrowable(ex); |
|
1108 |
} |
|
1109 |
} |
|
1110 |
return true; |
|
1111 |
} |
|
1112 |
||
1113 |
private <U,V> CompletableFuture<V> biApplyStage( |
|
1114 |
Executor e, CompletionStage<U> o, |
|
1115 |
BiFunction<? super T,? super U,? extends V> f) { |
|
1116 |
CompletableFuture<U> b; |
|
1117 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1118 |
throw new NullPointerException(); |
|
1119 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
|
1120 |
if (e != null || !d.biApply(this, b, f, null)) { |
|
1121 |
BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f); |
|
1122 |
bipush(b, c); |
|
1123 |
c.tryFire(SYNC); |
|
1124 |
} |
|
1125 |
return d; |
|
1126 |
} |
|
1127 |
||
1128 |
@SuppressWarnings("serial") |
|
1129 |
static final class BiAccept<T,U> extends BiCompletion<T,U,Void> { |
|
1130 |
BiConsumer<? super T,? super U> fn; |
|
1131 |
BiAccept(Executor executor, CompletableFuture<Void> dep, |
|
1132 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
|
1133 |
BiConsumer<? super T,? super U> fn) { |
|
1134 |
super(executor, dep, src, snd); this.fn = fn; |
|
1135 |
} |
|
1136 |
final CompletableFuture<Void> tryFire(int mode) { |
|
1137 |
CompletableFuture<Void> d; |
|
1138 |
CompletableFuture<T> a; |
|
1139 |
CompletableFuture<U> b; |
|
1140 |
if ((d = dep) == null || |
|
1141 |
!d.biAccept(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1142 |
return null; |
|
1143 |
dep = null; src = null; snd = null; fn = null; |
|
1144 |
return d.postFire(a, b, mode); |
|
1145 |
} |
|
1146 |
} |
|
1147 |
||
1148 |
final <R,S> boolean biAccept(CompletableFuture<R> a, |
|
1149 |
CompletableFuture<S> b, |
|
1150 |
BiConsumer<? super R,? super S> f, |
|
1151 |
BiAccept<R,S> c) { |
|
1152 |
Object r, s; Throwable x; |
|
1153 |
if (a == null || (r = a.result) == null || |
|
1154 |
b == null || (s = b.result) == null || f == null) |
|
1155 |
return false; |
|
1156 |
tryComplete: if (result == null) { |
|
1157 |
if (r instanceof AltResult) { |
|
1158 |
if ((x = ((AltResult)r).ex) != null) { |
|
1159 |
completeThrowable(x, r); |
|
1160 |
break tryComplete; |
|
1161 |
} |
|
1162 |
r = null; |
|
1163 |
} |
|
1164 |
if (s instanceof AltResult) { |
|
1165 |
if ((x = ((AltResult)s).ex) != null) { |
|
1166 |
completeThrowable(x, s); |
|
1167 |
break tryComplete; |
|
1168 |
} |
|
1169 |
s = null; |
|
1170 |
} |
|
1171 |
try { |
|
1172 |
if (c != null && !c.claim()) |
|
1173 |
return false; |
|
1174 |
@SuppressWarnings("unchecked") R rr = (R) r; |
|
1175 |
@SuppressWarnings("unchecked") S ss = (S) s; |
|
1176 |
f.accept(rr, ss); |
|
1177 |
completeNull(); |
|
1178 |
} catch (Throwable ex) { |
|
1179 |
completeThrowable(ex); |
|
1180 |
} |
|
1181 |
} |
|
1182 |
return true; |
|
1183 |
} |
|
1184 |
||
1185 |
private <U> CompletableFuture<Void> biAcceptStage( |
|
1186 |
Executor e, CompletionStage<U> o, |
|
1187 |
BiConsumer<? super T,? super U> f) { |
|
1188 |
CompletableFuture<U> b; |
|
1189 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1190 |
throw new NullPointerException(); |
|
1191 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1192 |
if (e != null || !d.biAccept(this, b, f, null)) { |
|
1193 |
BiAccept<T,U> c = new BiAccept<T,U>(e, d, this, b, f); |
|
1194 |
bipush(b, c); |
|
1195 |
c.tryFire(SYNC); |
|
1196 |
} |
|
1197 |
return d; |
|
1198 |
} |
|
16745 | 1199 |
|
26336 | 1200 |
@SuppressWarnings("serial") |
1201 |
static final class BiRun<T,U> extends BiCompletion<T,U,Void> { |
|
1202 |
Runnable fn; |
|
1203 |
BiRun(Executor executor, CompletableFuture<Void> dep, |
|
1204 |
CompletableFuture<T> src, |
|
1205 |
CompletableFuture<U> snd, |
|
1206 |
Runnable fn) { |
|
1207 |
super(executor, dep, src, snd); this.fn = fn; |
|
1208 |
} |
|
1209 |
final CompletableFuture<Void> tryFire(int mode) { |
|
1210 |
CompletableFuture<Void> d; |
|
1211 |
CompletableFuture<T> a; |
|
1212 |
CompletableFuture<U> b; |
|
1213 |
if ((d = dep) == null || |
|
1214 |
!d.biRun(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1215 |
return null; |
|
1216 |
dep = null; src = null; snd = null; fn = null; |
|
1217 |
return d.postFire(a, b, mode); |
|
1218 |
} |
|
1219 |
} |
|
1220 |
||
1221 |
final boolean biRun(CompletableFuture<?> a, CompletableFuture<?> b, |
|
1222 |
Runnable f, BiRun<?,?> c) { |
|
1223 |
Object r, s; Throwable x; |
|
1224 |
if (a == null || (r = a.result) == null || |
|
1225 |
b == null || (s = b.result) == null || f == null) |
|
1226 |
return false; |
|
1227 |
if (result == null) { |
|
1228 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
|
1229 |
completeThrowable(x, r); |
|
1230 |
else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null) |
|
1231 |
completeThrowable(x, s); |
|
1232 |
else |
|
1233 |
try { |
|
1234 |
if (c != null && !c.claim()) |
|
1235 |
return false; |
|
1236 |
f.run(); |
|
1237 |
completeNull(); |
|
1238 |
} catch (Throwable ex) { |
|
1239 |
completeThrowable(ex); |
|
1240 |
} |
|
1241 |
} |
|
1242 |
return true; |
|
1243 |
} |
|
1244 |
||
1245 |
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o, |
|
1246 |
Runnable f) { |
|
1247 |
CompletableFuture<?> b; |
|
1248 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1249 |
throw new NullPointerException(); |
|
1250 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1251 |
if (e != null || !d.biRun(this, b, f, null)) { |
|
1252 |
BiRun<T,?> c = new BiRun<>(e, d, this, b, f); |
|
1253 |
bipush(b, c); |
|
1254 |
c.tryFire(SYNC); |
|
1255 |
} |
|
1256 |
return d; |
|
1257 |
} |
|
1258 |
||
1259 |
@SuppressWarnings("serial") |
|
1260 |
static final class BiRelay<T,U> extends BiCompletion<T,U,Void> { // for And |
|
1261 |
BiRelay(CompletableFuture<Void> dep, |
|
1262 |
CompletableFuture<T> src, |
|
1263 |
CompletableFuture<U> snd) { |
|
1264 |
super(null, dep, src, snd); |
|
1265 |
} |
|
1266 |
final CompletableFuture<Void> tryFire(int mode) { |
|
1267 |
CompletableFuture<Void> d; |
|
1268 |
CompletableFuture<T> a; |
|
1269 |
CompletableFuture<U> b; |
|
1270 |
if ((d = dep) == null || !d.biRelay(a = src, b = snd)) |
|
1271 |
return null; |
|
1272 |
src = null; snd = null; dep = null; |
|
1273 |
return d.postFire(a, b, mode); |
|
1274 |
} |
|
1275 |
} |
|
1276 |
||
1277 |
boolean biRelay(CompletableFuture<?> a, CompletableFuture<?> b) { |
|
1278 |
Object r, s; Throwable x; |
|
1279 |
if (a == null || (r = a.result) == null || |
|
1280 |
b == null || (s = b.result) == null) |
|
1281 |
return false; |
|
1282 |
if (result == null) { |
|
1283 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
|
1284 |
completeThrowable(x, r); |
|
1285 |
else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null) |
|
1286 |
completeThrowable(x, s); |
|
1287 |
else |
|
1288 |
completeNull(); |
|
1289 |
} |
|
1290 |
return true; |
|
1291 |
} |
|
1292 |
||
1293 |
/** Recursively constructs a tree of completions. */ |
|
1294 |
static CompletableFuture<Void> andTree(CompletableFuture<?>[] cfs, |
|
1295 |
int lo, int hi) { |
|
1296 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1297 |
if (lo > hi) // empty |
|
1298 |
d.result = NIL; |
|
1299 |
else { |
|
1300 |
CompletableFuture<?> a, b; |
|
1301 |
int mid = (lo + hi) >>> 1; |
|
1302 |
if ((a = (lo == mid ? cfs[lo] : |
|
1303 |
andTree(cfs, lo, mid))) == null || |
|
1304 |
(b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] : |
|
1305 |
andTree(cfs, mid+1, hi))) == null) |
|
1306 |
throw new NullPointerException(); |
|
1307 |
if (!d.biRelay(a, b)) { |
|
1308 |
BiRelay<?,?> c = new BiRelay<>(d, a, b); |
|
1309 |
a.bipush(b, c); |
|
1310 |
c.tryFire(SYNC); |
|
1311 |
} |
|
1312 |
} |
|
1313 |
return d; |
|
1314 |
} |
|
1315 |
||
1316 |
/* ------------- Projected (Ored) BiCompletions -------------- */ |
|
1317 |
||
1318 |
/** Pushes completion to this and b unless either done. */ |
|
1319 |
final void orpush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
|
1320 |
if (c != null) { |
|
1321 |
while ((b == null || b.result == null) && result == null) { |
|
1322 |
if (tryPushStack(c)) { |
|
1323 |
if (b != null && b != this && b.result == null) { |
|
1324 |
Completion q = new CoCompletion(c); |
|
1325 |
while (result == null && b.result == null && |
|
1326 |
!b.tryPushStack(q)) |
|
1327 |
lazySetNext(q, null); // clear on failure |
|
1328 |
} |
|
1329 |
break; |
|
1330 |
} |
|
1331 |
lazySetNext(c, null); // clear on failure |
|
1332 |
} |
|
1333 |
} |
|
1334 |
} |
|
1335 |
||
1336 |
@SuppressWarnings("serial") |
|
1337 |
static final class OrApply<T,U extends T,V> extends BiCompletion<T,U,V> { |
|
1338 |
Function<? super T,? extends V> fn; |
|
1339 |
OrApply(Executor executor, CompletableFuture<V> dep, |
|
1340 |
CompletableFuture<T> src, |
|
1341 |
CompletableFuture<U> snd, |
|
1342 |
Function<? super T,? extends V> fn) { |
|
1343 |
super(executor, dep, src, snd); this.fn = fn; |
|
1344 |
} |
|
1345 |
final CompletableFuture<V> tryFire(int mode) { |
|
1346 |
CompletableFuture<V> d; |
|
1347 |
CompletableFuture<T> a; |
|
1348 |
CompletableFuture<U> b; |
|
1349 |
if ((d = dep) == null || |
|
1350 |
!d.orApply(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1351 |
return null; |
|
1352 |
dep = null; src = null; snd = null; fn = null; |
|
1353 |
return d.postFire(a, b, mode); |
|
1354 |
} |
|
1355 |
} |
|
16745 | 1356 |
|
26336 | 1357 |
final <R,S extends R> boolean orApply(CompletableFuture<R> a, |
1358 |
CompletableFuture<S> b, |
|
1359 |
Function<? super R, ? extends T> f, |
|
1360 |
OrApply<R,S,T> c) { |
|
1361 |
Object r; Throwable x; |
|
1362 |
if (a == null || b == null || |
|
1363 |
((r = a.result) == null && (r = b.result) == null) || f == null) |
|
1364 |
return false; |
|
1365 |
tryComplete: if (result == null) { |
|
1366 |
try { |
|
1367 |
if (c != null && !c.claim()) |
|
1368 |
return false; |
|
1369 |
if (r instanceof AltResult) { |
|
1370 |
if ((x = ((AltResult)r).ex) != null) { |
|
1371 |
completeThrowable(x, r); |
|
1372 |
break tryComplete; |
|
1373 |
} |
|
1374 |
r = null; |
|
1375 |
} |
|
1376 |
@SuppressWarnings("unchecked") R rr = (R) r; |
|
1377 |
completeValue(f.apply(rr)); |
|
1378 |
} catch (Throwable ex) { |
|
1379 |
completeThrowable(ex); |
|
1380 |
} |
|
1381 |
} |
|
1382 |
return true; |
|
1383 |
} |
|
1384 |
||
1385 |
private <U extends T,V> CompletableFuture<V> orApplyStage( |
|
1386 |
Executor e, CompletionStage<U> o, |
|
1387 |
Function<? super T, ? extends V> f) { |
|
1388 |
CompletableFuture<U> b; |
|
1389 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1390 |
throw new NullPointerException(); |
|
1391 |
CompletableFuture<V> d = new CompletableFuture<V>(); |
|
1392 |
if (e != null || !d.orApply(this, b, f, null)) { |
|
1393 |
OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f); |
|
1394 |
orpush(b, c); |
|
1395 |
c.tryFire(SYNC); |
|
1396 |
} |
|
1397 |
return d; |
|
1398 |
} |
|
1399 |
||
1400 |
@SuppressWarnings("serial") |
|
1401 |
static final class OrAccept<T,U extends T> extends BiCompletion<T,U,Void> { |
|
1402 |
Consumer<? super T> fn; |
|
1403 |
OrAccept(Executor executor, CompletableFuture<Void> dep, |
|
1404 |
CompletableFuture<T> src, |
|
1405 |
CompletableFuture<U> snd, |
|
1406 |
Consumer<? super T> fn) { |
|
1407 |
super(executor, dep, src, snd); this.fn = fn; |
|
1408 |
} |
|
1409 |
final CompletableFuture<Void> tryFire(int mode) { |
|
1410 |
CompletableFuture<Void> d; |
|
1411 |
CompletableFuture<T> a; |
|
1412 |
CompletableFuture<U> b; |
|
1413 |
if ((d = dep) == null || |
|
1414 |
!d.orAccept(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1415 |
return null; |
|
1416 |
dep = null; src = null; snd = null; fn = null; |
|
1417 |
return d.postFire(a, b, mode); |
|
1418 |
} |
|
1419 |
} |
|
1420 |
||
1421 |
final <R,S extends R> boolean orAccept(CompletableFuture<R> a, |
|
1422 |
CompletableFuture<S> b, |
|
1423 |
Consumer<? super R> f, |
|
1424 |
OrAccept<R,S> c) { |
|
1425 |
Object r; Throwable x; |
|
1426 |
if (a == null || b == null || |
|
1427 |
((r = a.result) == null && (r = b.result) == null) || f == null) |
|
1428 |
return false; |
|
1429 |
tryComplete: if (result == null) { |
|
1430 |
try { |
|
1431 |
if (c != null && !c.claim()) |
|
1432 |
return false; |
|
1433 |
if (r instanceof AltResult) { |
|
1434 |
if ((x = ((AltResult)r).ex) != null) { |
|
1435 |
completeThrowable(x, r); |
|
1436 |
break tryComplete; |
|
1437 |
} |
|
1438 |
r = null; |
|
1439 |
} |
|
1440 |
@SuppressWarnings("unchecked") R rr = (R) r; |
|
1441 |
f.accept(rr); |
|
1442 |
completeNull(); |
|
1443 |
} catch (Throwable ex) { |
|
1444 |
completeThrowable(ex); |
|
1445 |
} |
|
1446 |
} |
|
1447 |
return true; |
|
1448 |
} |
|
1449 |
||
1450 |
private <U extends T> CompletableFuture<Void> orAcceptStage( |
|
1451 |
Executor e, CompletionStage<U> o, Consumer<? super T> f) { |
|
1452 |
CompletableFuture<U> b; |
|
1453 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1454 |
throw new NullPointerException(); |
|
1455 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1456 |
if (e != null || !d.orAccept(this, b, f, null)) { |
|
1457 |
OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f); |
|
1458 |
orpush(b, c); |
|
1459 |
c.tryFire(SYNC); |
|
1460 |
} |
|
1461 |
return d; |
|
1462 |
} |
|
1463 |
||
1464 |
@SuppressWarnings("serial") |
|
1465 |
static final class OrRun<T,U> extends BiCompletion<T,U,Void> { |
|
1466 |
Runnable fn; |
|
1467 |
OrRun(Executor executor, CompletableFuture<Void> dep, |
|
1468 |
CompletableFuture<T> src, |
|
1469 |
CompletableFuture<U> snd, |
|
1470 |
Runnable fn) { |
|
1471 |
super(executor, dep, src, snd); this.fn = fn; |
|
1472 |
} |
|
1473 |
final CompletableFuture<Void> tryFire(int mode) { |
|
1474 |
CompletableFuture<Void> d; |
|
1475 |
CompletableFuture<T> a; |
|
1476 |
CompletableFuture<U> b; |
|
1477 |
if ((d = dep) == null || |
|
1478 |
!d.orRun(a = src, b = snd, fn, mode > 0 ? null : this)) |
|
1479 |
return null; |
|
1480 |
dep = null; src = null; snd = null; fn = null; |
|
1481 |
return d.postFire(a, b, mode); |
|
1482 |
} |
|
1483 |
} |
|
1484 |
||
1485 |
final boolean orRun(CompletableFuture<?> a, CompletableFuture<?> b, |
|
1486 |
Runnable f, OrRun<?,?> c) { |
|
1487 |
Object r; Throwable x; |
|
1488 |
if (a == null || b == null || |
|
1489 |
((r = a.result) == null && (r = b.result) == null) || f == null) |
|
1490 |
return false; |
|
1491 |
if (result == null) { |
|
1492 |
try { |
|
1493 |
if (c != null && !c.claim()) |
|
1494 |
return false; |
|
1495 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
|
1496 |
completeThrowable(x, r); |
|
1497 |
else { |
|
1498 |
f.run(); |
|
1499 |
completeNull(); |
|
1500 |
} |
|
1501 |
} catch (Throwable ex) { |
|
1502 |
completeThrowable(ex); |
|
1503 |
} |
|
1504 |
} |
|
1505 |
return true; |
|
1506 |
} |
|
1507 |
||
1508 |
private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o, |
|
1509 |
Runnable f) { |
|
1510 |
CompletableFuture<?> b; |
|
1511 |
if (f == null || (b = o.toCompletableFuture()) == null) |
|
1512 |
throw new NullPointerException(); |
|
1513 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1514 |
if (e != null || !d.orRun(this, b, f, null)) { |
|
1515 |
OrRun<T,?> c = new OrRun<>(e, d, this, b, f); |
|
1516 |
orpush(b, c); |
|
1517 |
c.tryFire(SYNC); |
|
1518 |
} |
|
1519 |
return d; |
|
1520 |
} |
|
1521 |
||
1522 |
@SuppressWarnings("serial") |
|
1523 |
static final class OrRelay<T,U> extends BiCompletion<T,U,Object> { // for Or |
|
1524 |
OrRelay(CompletableFuture<Object> dep, CompletableFuture<T> src, |
|
1525 |
CompletableFuture<U> snd) { |
|
1526 |
super(null, dep, src, snd); |
|
1527 |
} |
|
1528 |
final CompletableFuture<Object> tryFire(int mode) { |
|
1529 |
CompletableFuture<Object> d; |
|
1530 |
CompletableFuture<T> a; |
|
1531 |
CompletableFuture<U> b; |
|
1532 |
if ((d = dep) == null || !d.orRelay(a = src, b = snd)) |
|
1533 |
return null; |
|
1534 |
src = null; snd = null; dep = null; |
|
1535 |
return d.postFire(a, b, mode); |
|
1536 |
} |
|
1537 |
} |
|
1538 |
||
1539 |
final boolean orRelay(CompletableFuture<?> a, CompletableFuture<?> b) { |
|
1540 |
Object r; |
|
1541 |
if (a == null || b == null || |
|
1542 |
((r = a.result) == null && (r = b.result) == null)) |
|
1543 |
return false; |
|
1544 |
if (result == null) |
|
1545 |
completeRelay(r); |
|
1546 |
return true; |
|
1547 |
} |
|
1548 |
||
1549 |
/** Recursively constructs a tree of completions. */ |
|
1550 |
static CompletableFuture<Object> orTree(CompletableFuture<?>[] cfs, |
|
1551 |
int lo, int hi) { |
|
1552 |
CompletableFuture<Object> d = new CompletableFuture<Object>(); |
|
1553 |
if (lo <= hi) { |
|
1554 |
CompletableFuture<?> a, b; |
|
1555 |
int mid = (lo + hi) >>> 1; |
|
1556 |
if ((a = (lo == mid ? cfs[lo] : |
|
1557 |
orTree(cfs, lo, mid))) == null || |
|
1558 |
(b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] : |
|
1559 |
orTree(cfs, mid+1, hi))) == null) |
|
1560 |
throw new NullPointerException(); |
|
1561 |
if (!d.orRelay(a, b)) { |
|
1562 |
OrRelay<?,?> c = new OrRelay<>(d, a, b); |
|
1563 |
a.orpush(b, c); |
|
1564 |
c.tryFire(SYNC); |
|
1565 |
} |
|
1566 |
} |
|
1567 |
return d; |
|
1568 |
} |
|
1569 |
||
1570 |
/* ------------- Zero-input Async forms -------------- */ |
|
1571 |
||
1572 |
@SuppressWarnings("serial") |
|
1573 |
static final class AsyncSupply<T> extends ForkJoinTask<Void> |
|
1574 |
implements Runnable, AsynchronousCompletionTask { |
|
1575 |
CompletableFuture<T> dep; Supplier<T> fn; |
|
1576 |
AsyncSupply(CompletableFuture<T> dep, Supplier<T> fn) { |
|
1577 |
this.dep = dep; this.fn = fn; |
|
1578 |
} |
|
1579 |
||
1580 |
public final Void getRawResult() { return null; } |
|
1581 |
public final void setRawResult(Void v) {} |
|
1582 |
public final boolean exec() { run(); return true; } |
|
1583 |
||
1584 |
public void run() { |
|
1585 |
CompletableFuture<T> d; Supplier<T> f; |
|
1586 |
if ((d = dep) != null && (f = fn) != null) { |
|
1587 |
dep = null; fn = null; |
|
1588 |
if (d.result == null) { |
|
1589 |
try { |
|
1590 |
d.completeValue(f.get()); |
|
1591 |
} catch (Throwable ex) { |
|
1592 |
d.completeThrowable(ex); |
|
1593 |
} |
|
1594 |
} |
|
1595 |
d.postComplete(); |
|
1596 |
} |
|
1597 |
} |
|
1598 |
} |
|
1599 |
||
1600 |
static <U> CompletableFuture<U> asyncSupplyStage(Executor e, |
|
1601 |
Supplier<U> f) { |
|
1602 |
if (f == null) throw new NullPointerException(); |
|
1603 |
CompletableFuture<U> d = new CompletableFuture<U>(); |
|
1604 |
e.execute(new AsyncSupply<U>(d, f)); |
|
1605 |
return d; |
|
1606 |
} |
|
1607 |
||
1608 |
@SuppressWarnings("serial") |
|
1609 |
static final class AsyncRun extends ForkJoinTask<Void> |
|
1610 |
implements Runnable, AsynchronousCompletionTask { |
|
1611 |
CompletableFuture<Void> dep; Runnable fn; |
|
1612 |
AsyncRun(CompletableFuture<Void> dep, Runnable fn) { |
|
1613 |
this.dep = dep; this.fn = fn; |
|
1614 |
} |
|
1615 |
||
1616 |
public final Void getRawResult() { return null; } |
|
1617 |
public final void setRawResult(Void v) {} |
|
1618 |
public final boolean exec() { run(); return true; } |
|
1619 |
||
1620 |
public void run() { |
|
1621 |
CompletableFuture<Void> d; Runnable f; |
|
1622 |
if ((d = dep) != null && (f = fn) != null) { |
|
1623 |
dep = null; fn = null; |
|
1624 |
if (d.result == null) { |
|
1625 |
try { |
|
1626 |
f.run(); |
|
1627 |
d.completeNull(); |
|
1628 |
} catch (Throwable ex) { |
|
1629 |
d.completeThrowable(ex); |
|
1630 |
} |
|
1631 |
} |
|
1632 |
d.postComplete(); |
|
1633 |
} |
|
1634 |
} |
|
1635 |
} |
|
1636 |
||
1637 |
static CompletableFuture<Void> asyncRunStage(Executor e, Runnable f) { |
|
1638 |
if (f == null) throw new NullPointerException(); |
|
1639 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
|
1640 |
e.execute(new AsyncRun(d, f)); |
|
1641 |
return d; |
|
1642 |
} |
|
1643 |
||
1644 |
/* ------------- Signallers -------------- */ |
|
16745 | 1645 |
|
1646 |
/** |
|
26336 | 1647 |
* Completion for recording and releasing a waiting thread. This |
1648 |
* class implements ManagedBlocker to avoid starvation when |
|
1649 |
* blocking actions pile up in ForkJoinPools. |
|
16745 | 1650 |
*/ |
26336 | 1651 |
@SuppressWarnings("serial") |
1652 |
static final class Signaller extends Completion |
|
1653 |
implements ForkJoinPool.ManagedBlocker { |
|
1654 |
long nanos; // wait time if timed |
|
1655 |
final long deadline; // non-zero if timed |
|
16745 | 1656 |
volatile int interruptControl; // > 0: interruptible, < 0: interrupted |
1657 |
volatile Thread thread; |
|
26336 | 1658 |
|
1659 |
Signaller(boolean interruptible, long nanos, long deadline) { |
|
16745 | 1660 |
this.thread = Thread.currentThread(); |
1661 |
this.interruptControl = interruptible ? 1 : 0; |
|
1662 |
this.nanos = nanos; |
|
1663 |
this.deadline = deadline; |
|
1664 |
} |
|
26336 | 1665 |
final CompletableFuture<?> tryFire(int ignore) { |
1666 |
Thread w; // no need to atomically claim |
|
1667 |
if ((w = thread) != null) { |
|
1668 |
thread = null; |
|
1669 |
LockSupport.unpark(w); |
|
1670 |
} |
|
1671 |
return null; |
|
1672 |
} |
|
16745 | 1673 |
public boolean isReleasable() { |
1674 |
if (thread == null) |
|
1675 |
return true; |
|
1676 |
if (Thread.interrupted()) { |
|
1677 |
int i = interruptControl; |
|
1678 |
interruptControl = -1; |
|
1679 |
if (i > 0) |
|
1680 |
return true; |
|
1681 |
} |
|
1682 |
if (deadline != 0L && |
|
1683 |
(nanos <= 0L || (nanos = deadline - System.nanoTime()) <= 0L)) { |
|
1684 |
thread = null; |
|
1685 |
return true; |
|
1686 |
} |
|
1687 |
return false; |
|
1688 |
} |
|
1689 |
public boolean block() { |
|
1690 |
if (isReleasable()) |
|
1691 |
return true; |
|
1692 |
else if (deadline == 0L) |
|
1693 |
LockSupport.park(this); |
|
1694 |
else if (nanos > 0L) |
|
1695 |
LockSupport.parkNanos(this, nanos); |
|
1696 |
return isReleasable(); |
|
1697 |
} |
|
26336 | 1698 |
final boolean isLive() { return thread != null; } |
16745 | 1699 |
} |
1700 |
||
1701 |
/** |
|
1702 |
* Returns raw result after waiting, or null if interruptible and |
|
1703 |
* interrupted. |
|
1704 |
*/ |
|
1705 |
private Object waitingGet(boolean interruptible) { |
|
26336 | 1706 |
Signaller q = null; |
16745 | 1707 |
boolean queued = false; |
26336 | 1708 |
int spins = -1; |
1709 |
Object r; |
|
1710 |
while ((r = result) == null) { |
|
1711 |
if (spins < 0) |
|
1712 |
spins = (Runtime.getRuntime().availableProcessors() > 1) ? |
|
1713 |
1 << 8 : 0; // Use brief spin-wait on multiprocessors |
|
16745 | 1714 |
else if (spins > 0) { |
26336 | 1715 |
if (ThreadLocalRandom.nextSecondarySeed() >= 0) |
16745 | 1716 |
--spins; |
1717 |
} |
|
1718 |
else if (q == null) |
|
26336 | 1719 |
q = new Signaller(interruptible, 0L, 0L); |
16745 | 1720 |
else if (!queued) |
26336 | 1721 |
queued = tryPushStack(q); |
16745 | 1722 |
else if (interruptible && q.interruptControl < 0) { |
26336 | 1723 |
q.thread = null; |
1724 |
cleanStack(); |
|
16745 | 1725 |
return null; |
1726 |
} |
|
1727 |
else if (q.thread != null && result == null) { |
|
1728 |
try { |
|
1729 |
ForkJoinPool.managedBlock(q); |
|
26336 | 1730 |
} catch (InterruptedException ie) { |
16745 | 1731 |
q.interruptControl = -1; |
1732 |
} |
|
1733 |
} |
|
1734 |
} |
|
26336 | 1735 |
if (q != null) { |
1736 |
q.thread = null; |
|
1737 |
if (q.interruptControl < 0) { |
|
1738 |
if (interruptible) |
|
1739 |
r = null; // report interruption |
|
1740 |
else |
|
1741 |
Thread.currentThread().interrupt(); |
|
16745 | 1742 |
} |
1743 |
} |
|
26336 | 1744 |
postComplete(); |
1745 |
return r; |
|
16745 | 1746 |
} |
1747 |
||
19192 | 1748 |
/** |
26336 | 1749 |
* Returns raw result after waiting, or null if interrupted, or |
1750 |
* throws TimeoutException on timeout. |
|
16745 | 1751 |
*/ |
26336 | 1752 |
private Object timedGet(long nanos) throws TimeoutException { |
1753 |
if (Thread.interrupted()) |
|
1754 |
return null; |
|
1755 |
if (nanos <= 0L) |
|
1756 |
throw new TimeoutException(); |
|
1757 |
long d = System.nanoTime() + nanos; |
|
1758 |
Signaller q = new Signaller(true, nanos, d == 0L ? 1L : d); // avoid 0 |
|
1759 |
boolean queued = false; |
|
1760 |
Object r; |
|
1761 |
// We intentionally don't spin here (as waitingGet does) because |
|
1762 |
// the call to nanoTime() above acts much like a spin. |
|
1763 |
while ((r = result) == null) { |
|
1764 |
if (!queued) |
|
1765 |
queued = tryPushStack(q); |
|
1766 |
else if (q.interruptControl < 0 || q.nanos <= 0L) { |
|
1767 |
q.thread = null; |
|
1768 |
cleanStack(); |
|
1769 |
if (q.interruptControl < 0) |
|
1770 |
return null; |
|
1771 |
throw new TimeoutException(); |
|
16745 | 1772 |
} |
26336 | 1773 |
else if (q.thread != null && result == null) { |
19192 | 1774 |
try { |
26336 | 1775 |
ForkJoinPool.managedBlock(q); |
1776 |
} catch (InterruptedException ie) { |
|
1777 |
q.interruptControl = -1; |
|
19192 | 1778 |
} |
1779 |
} |
|
1780 |
} |
|
26336 | 1781 |
if (q.interruptControl < 0) |
1782 |
r = null; |
|
1783 |
q.thread = null; |
|
1784 |
postComplete(); |
|
1785 |
return r; |
|
19192 | 1786 |
} |
1787 |
||
26336 | 1788 |
/* ------------- public methods -------------- */ |
16745 | 1789 |
|
1790 |
/** |
|
1791 |
* Creates a new incomplete CompletableFuture. |
|
1792 |
*/ |
|
1793 |
public CompletableFuture() { |
|
1794 |
} |
|
1795 |
||
1796 |
/** |
|
26336 | 1797 |
* Creates a new complete CompletableFuture with given encoded result. |
1798 |
*/ |
|
1799 |
private CompletableFuture(Object r) { |
|
1800 |
this.result = r; |
|
1801 |
} |
|
1802 |
||
1803 |
/** |
|
16745 | 1804 |
* Returns a new CompletableFuture that is asynchronously completed |
1805 |
* by a task running in the {@link ForkJoinPool#commonPool()} with |
|
1806 |
* the value obtained by calling the given Supplier. |
|
1807 |
* |
|
1808 |
* @param supplier a function returning the value to be used |
|
1809 |
* to complete the returned CompletableFuture |
|
19192 | 1810 |
* @param <U> the function's return type |
16745 | 1811 |
* @return the new CompletableFuture |
1812 |
*/ |
|
1813 |
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { |
|
26336 | 1814 |
return asyncSupplyStage(asyncPool, supplier); |
16745 | 1815 |
} |
1816 |
||
1817 |
/** |
|
1818 |
* Returns a new CompletableFuture that is asynchronously completed |
|
1819 |
* by a task running in the given executor with the value obtained |
|
1820 |
* by calling the given Supplier. |
|
1821 |
* |
|
1822 |
* @param supplier a function returning the value to be used |
|
1823 |
* to complete the returned CompletableFuture |
|
1824 |
* @param executor the executor to use for asynchronous execution |
|
19192 | 1825 |
* @param <U> the function's return type |
16745 | 1826 |
* @return the new CompletableFuture |
1827 |
*/ |
|
1828 |
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, |
|
1829 |
Executor executor) { |
|
26336 | 1830 |
return asyncSupplyStage(screenExecutor(executor), supplier); |
16745 | 1831 |
} |
1832 |
||
1833 |
/** |
|
1834 |
* Returns a new CompletableFuture that is asynchronously completed |
|
1835 |
* by a task running in the {@link ForkJoinPool#commonPool()} after |
|
1836 |
* it runs the given action. |
|
1837 |
* |
|
1838 |
* @param runnable the action to run before completing the |
|
1839 |
* returned CompletableFuture |
|
1840 |
* @return the new CompletableFuture |
|
1841 |
*/ |
|
1842 |
public static CompletableFuture<Void> runAsync(Runnable runnable) { |
|
26336 | 1843 |
return asyncRunStage(asyncPool, runnable); |
16745 | 1844 |
} |
1845 |
||
1846 |
/** |
|
1847 |
* Returns a new CompletableFuture that is asynchronously completed |
|
1848 |
* by a task running in the given executor after it runs the given |
|
1849 |
* action. |
|
1850 |
* |
|
1851 |
* @param runnable the action to run before completing the |
|
1852 |
* returned CompletableFuture |
|
1853 |
* @param executor the executor to use for asynchronous execution |
|
1854 |
* @return the new CompletableFuture |
|
1855 |
*/ |
|
1856 |
public static CompletableFuture<Void> runAsync(Runnable runnable, |
|
1857 |
Executor executor) { |
|
26336 | 1858 |
return asyncRunStage(screenExecutor(executor), runnable); |
16745 | 1859 |
} |
1860 |
||
1861 |
/** |
|
1862 |
* Returns a new CompletableFuture that is already completed with |
|
1863 |
* the given value. |
|
1864 |
* |
|
1865 |
* @param value the value |
|
19192 | 1866 |
* @param <U> the type of the value |
16745 | 1867 |
* @return the completed CompletableFuture |
1868 |
*/ |
|
1869 |
public static <U> CompletableFuture<U> completedFuture(U value) { |
|
26336 | 1870 |
return new CompletableFuture<U>((value == null) ? NIL : value); |
16745 | 1871 |
} |
1872 |
||
1873 |
/** |
|
1874 |
* Returns {@code true} if completed in any fashion: normally, |
|
1875 |
* exceptionally, or via cancellation. |
|
1876 |
* |
|
1877 |
* @return {@code true} if completed |
|
1878 |
*/ |
|
1879 |
public boolean isDone() { |
|
1880 |
return result != null; |
|
1881 |
} |
|
1882 |
||
1883 |
/** |
|
1884 |
* Waits if necessary for this future to complete, and then |
|
1885 |
* returns its result. |
|
1886 |
* |
|
1887 |
* @return the result value |
|
1888 |
* @throws CancellationException if this future was cancelled |
|
1889 |
* @throws ExecutionException if this future completed exceptionally |
|
1890 |
* @throws InterruptedException if the current thread was interrupted |
|
1891 |
* while waiting |
|
1892 |
*/ |
|
1893 |
public T get() throws InterruptedException, ExecutionException { |
|
26336 | 1894 |
Object r; |
1895 |
return reportGet((r = result) == null ? waitingGet(true) : r); |
|
16745 | 1896 |
} |
1897 |
||
1898 |
/** |
|
1899 |
* Waits if necessary for at most the given time for this future |
|
1900 |
* to complete, and then returns its result, if available. |
|
1901 |
* |
|
1902 |
* @param timeout the maximum time to wait |
|
1903 |
* @param unit the time unit of the timeout argument |
|
1904 |
* @return the result value |
|
1905 |
* @throws CancellationException if this future was cancelled |
|
1906 |
* @throws ExecutionException if this future completed exceptionally |
|
1907 |
* @throws InterruptedException if the current thread was interrupted |
|
1908 |
* while waiting |
|
1909 |
* @throws TimeoutException if the wait timed out |
|
1910 |
*/ |
|
1911 |
public T get(long timeout, TimeUnit unit) |
|
1912 |
throws InterruptedException, ExecutionException, TimeoutException { |
|
26336 | 1913 |
Object r; |
16745 | 1914 |
long nanos = unit.toNanos(timeout); |
26336 | 1915 |
return reportGet((r = result) == null ? timedGet(nanos) : r); |
16745 | 1916 |
} |
1917 |
||
1918 |
/** |
|
1919 |
* Returns the result value when complete, or throws an |
|
1920 |
* (unchecked) exception if completed exceptionally. To better |
|
1921 |
* conform with the use of common functional forms, if a |
|
1922 |
* computation involved in the completion of this |
|
1923 |
* CompletableFuture threw an exception, this method throws an |
|
1924 |
* (unchecked) {@link CompletionException} with the underlying |
|
1925 |
* exception as its cause. |
|
1926 |
* |
|
1927 |
* @return the result value |
|
1928 |
* @throws CancellationException if the computation was cancelled |
|
1929 |
* @throws CompletionException if this future completed |
|
1930 |
* exceptionally or a completion computation threw an exception |
|
1931 |
*/ |
|
1932 |
public T join() { |
|
26336 | 1933 |
Object r; |
1934 |
return reportJoin((r = result) == null ? waitingGet(false) : r); |
|
16745 | 1935 |
} |
1936 |
||
1937 |
/** |
|
1938 |
* Returns the result value (or throws any encountered exception) |
|
1939 |
* if completed, else returns the given valueIfAbsent. |
|
1940 |
* |
|
1941 |
* @param valueIfAbsent the value to return if not completed |
|
1942 |
* @return the result value, if completed, else the given valueIfAbsent |
|
1943 |
* @throws CancellationException if the computation was cancelled |
|
1944 |
* @throws CompletionException if this future completed |
|
1945 |
* exceptionally or a completion computation threw an exception |
|
1946 |
*/ |
|
1947 |
public T getNow(T valueIfAbsent) { |
|
26336 | 1948 |
Object r; |
1949 |
return ((r = result) == null) ? valueIfAbsent : reportJoin(r); |
|
16745 | 1950 |
} |
1951 |
||
1952 |
/** |
|
1953 |
* If not already completed, sets the value returned by {@link |
|
1954 |
* #get()} and related methods to the given value. |
|
1955 |
* |
|
1956 |
* @param value the result value |
|
1957 |
* @return {@code true} if this invocation caused this CompletableFuture |
|
1958 |
* to transition to a completed state, else {@code false} |
|
1959 |
*/ |
|
1960 |
public boolean complete(T value) { |
|
26336 | 1961 |
boolean triggered = completeValue(value); |
16745 | 1962 |
postComplete(); |
1963 |
return triggered; |
|
1964 |
} |
|
1965 |
||
1966 |
/** |
|
1967 |
* If not already completed, causes invocations of {@link #get()} |
|
1968 |
* and related methods to throw the given exception. |
|
1969 |
* |
|
1970 |
* @param ex the exception |
|
1971 |
* @return {@code true} if this invocation caused this CompletableFuture |
|
1972 |
* to transition to a completed state, else {@code false} |
|
1973 |
*/ |
|
1974 |
public boolean completeExceptionally(Throwable ex) { |
|
1975 |
if (ex == null) throw new NullPointerException(); |
|
26336 | 1976 |
boolean triggered = internalComplete(new AltResult(ex)); |
16745 | 1977 |
postComplete(); |
1978 |
return triggered; |
|
1979 |
} |
|
1980 |
||
26336 | 1981 |
public <U> CompletableFuture<U> thenApply( |
1982 |
Function<? super T,? extends U> fn) { |
|
1983 |
return uniApplyStage(null, fn); |
|
16745 | 1984 |
} |
1985 |
||
26336 | 1986 |
public <U> CompletableFuture<U> thenApplyAsync( |
1987 |
Function<? super T,? extends U> fn) { |
|
1988 |
return uniApplyStage(asyncPool, fn); |
|
16745 | 1989 |
} |
1990 |
||
26336 | 1991 |
public <U> CompletableFuture<U> thenApplyAsync( |
1992 |
Function<? super T,? extends U> fn, Executor executor) { |
|
1993 |
return uniApplyStage(screenExecutor(executor), fn); |
|
16745 | 1994 |
} |
1995 |
||
26336 | 1996 |
public CompletableFuture<Void> thenAccept(Consumer<? super T> action) { |
1997 |
return uniAcceptStage(null, action); |
|
16745 | 1998 |
} |
1999 |
||
26336 | 2000 |
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) { |
2001 |
return uniAcceptStage(asyncPool, action); |
|
16745 | 2002 |
} |
2003 |
||
26336 | 2004 |
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, |
2005 |
Executor executor) { |
|
2006 |
return uniAcceptStage(screenExecutor(executor), action); |
|
16745 | 2007 |
} |
2008 |
||
26336 | 2009 |
public CompletableFuture<Void> thenRun(Runnable action) { |
2010 |
return uniRunStage(null, action); |
|
16745 | 2011 |
} |
2012 |
||
26336 | 2013 |
public CompletableFuture<Void> thenRunAsync(Runnable action) { |
2014 |
return uniRunStage(asyncPool, action); |
|
16745 | 2015 |
} |
2016 |
||
26336 | 2017 |
public CompletableFuture<Void> thenRunAsync(Runnable action, |
2018 |
Executor executor) { |
|
2019 |
return uniRunStage(screenExecutor(executor), action); |
|
16745 | 2020 |
} |
2021 |
||
26336 | 2022 |
public <U,V> CompletableFuture<V> thenCombine( |
2023 |
CompletionStage<? extends U> other, |
|
2024 |
BiFunction<? super T,? super U,? extends V> fn) { |
|
2025 |
return biApplyStage(null, other, fn); |
|
16745 | 2026 |
} |
2027 |
||
26336 | 2028 |
public <U,V> CompletableFuture<V> thenCombineAsync( |
2029 |
CompletionStage<? extends U> other, |
|
2030 |
BiFunction<? super T,? super U,? extends V> fn) { |
|
2031 |
return biApplyStage(asyncPool, other, fn); |
|
16745 | 2032 |
} |
2033 |
||
26336 | 2034 |
public <U,V> CompletableFuture<V> thenCombineAsync( |
2035 |
CompletionStage<? extends U> other, |
|
2036 |
BiFunction<? super T,? super U,? extends V> fn, Executor executor) { |
|
2037 |
return biApplyStage(screenExecutor(executor), other, fn); |
|
19192 | 2038 |
} |
2039 |
||
26336 | 2040 |
public <U> CompletableFuture<Void> thenAcceptBoth( |
2041 |
CompletionStage<? extends U> other, |
|
2042 |
BiConsumer<? super T, ? super U> action) { |
|
2043 |
return biAcceptStage(null, other, action); |
|
16745 | 2044 |
} |
2045 |
||
26336 | 2046 |
public <U> CompletableFuture<Void> thenAcceptBothAsync( |
2047 |
CompletionStage<? extends U> other, |
|
2048 |
BiConsumer<? super T, ? super U> action) { |
|
2049 |
return biAcceptStage(asyncPool, other, action); |
|
16745 | 2050 |
} |
2051 |
||
26336 | 2052 |
public <U> CompletableFuture<Void> thenAcceptBothAsync( |
2053 |
CompletionStage<? extends U> other, |
|
2054 |
BiConsumer<? super T, ? super U> action, Executor executor) { |
|
2055 |
return biAcceptStage(screenExecutor(executor), other, action); |
|
16745 | 2056 |
} |
2057 |
||
26336 | 2058 |
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, |
2059 |
Runnable action) { |
|
2060 |
return biRunStage(null, other, action); |
|
16745 | 2061 |
} |
2062 |
||
26336 | 2063 |
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, |
2064 |
Runnable action) { |
|
2065 |
return biRunStage(asyncPool, other, action); |
|
16745 | 2066 |
} |
2067 |
||
26336 | 2068 |
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, |
2069 |
Runnable action, |
|
2070 |
Executor executor) { |
|
2071 |
return biRunStage(screenExecutor(executor), other, action); |
|
16745 | 2072 |
} |
2073 |
||
26336 | 2074 |
public <U> CompletableFuture<U> applyToEither( |
2075 |
CompletionStage<? extends T> other, Function<? super T, U> fn) { |
|
2076 |
return orApplyStage(null, other, fn); |
|
2077 |
} |
|
2078 |
||
2079 |
public <U> CompletableFuture<U> applyToEitherAsync( |
|
2080 |
CompletionStage<? extends T> other, Function<? super T, U> fn) { |
|
2081 |
return orApplyStage(asyncPool, other, fn); |
|
16745 | 2082 |
} |
2083 |
||
26336 | 2084 |
public <U> CompletableFuture<U> applyToEitherAsync( |
2085 |
CompletionStage<? extends T> other, Function<? super T, U> fn, |
|
2086 |
Executor executor) { |
|
2087 |
return orApplyStage(screenExecutor(executor), other, fn); |
|
16745 | 2088 |
} |
2089 |
||
26336 | 2090 |
public CompletableFuture<Void> acceptEither( |
2091 |
CompletionStage<? extends T> other, Consumer<? super T> action) { |
|
2092 |
return orAcceptStage(null, other, action); |
|
16745 | 2093 |
} |
2094 |
||
26336 | 2095 |
public CompletableFuture<Void> acceptEitherAsync( |
2096 |
CompletionStage<? extends T> other, Consumer<? super T> action) { |
|
2097 |
return orAcceptStage(asyncPool, other, action); |
|
2098 |
} |
|
2099 |
||
2100 |
public CompletableFuture<Void> acceptEitherAsync( |
|
2101 |
CompletionStage<? extends T> other, Consumer<? super T> action, |
|
2102 |
Executor executor) { |
|
2103 |
return orAcceptStage(screenExecutor(executor), other, action); |
|
16745 | 2104 |
} |
2105 |
||
19192 | 2106 |
public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, |
2107 |
Runnable action) { |
|
26336 | 2108 |
return orRunStage(null, other, action); |
16745 | 2109 |
} |
2110 |
||
26336 | 2111 |
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, |
2112 |
Runnable action) { |
|
2113 |
return orRunStage(asyncPool, other, action); |
|
16745 | 2114 |
} |
2115 |
||
26336 | 2116 |
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, |
2117 |
Runnable action, |
|
2118 |
Executor executor) { |
|
2119 |
return orRunStage(screenExecutor(executor), other, action); |
|
16745 | 2120 |
} |
2121 |
||
26336 | 2122 |
public <U> CompletableFuture<U> thenCompose( |
2123 |
Function<? super T, ? extends CompletionStage<U>> fn) { |
|
2124 |
return uniComposeStage(null, fn); |
|
16745 | 2125 |
} |
2126 |
||
26336 | 2127 |
public <U> CompletableFuture<U> thenComposeAsync( |
2128 |
Function<? super T, ? extends CompletionStage<U>> fn) { |
|
2129 |
return uniComposeStage(asyncPool, fn); |
|
16745 | 2130 |
} |
2131 |
||
26336 | 2132 |
public <U> CompletableFuture<U> thenComposeAsync( |
2133 |
Function<? super T, ? extends CompletionStage<U>> fn, |
|
2134 |
Executor executor) { |
|
2135 |
return uniComposeStage(screenExecutor(executor), fn); |
|
16745 | 2136 |
} |
2137 |
||
26336 | 2138 |
public CompletableFuture<T> whenComplete( |
2139 |
BiConsumer<? super T, ? super Throwable> action) { |
|
2140 |
return uniWhenCompleteStage(null, action); |
|
19192 | 2141 |
} |
2142 |
||
26336 | 2143 |
public CompletableFuture<T> whenCompleteAsync( |
2144 |
BiConsumer<? super T, ? super Throwable> action) { |
|
2145 |
return uniWhenCompleteStage(asyncPool, action); |
|
19192 | 2146 |
} |
2147 |
||
26336 | 2148 |
public CompletableFuture<T> whenCompleteAsync( |
2149 |
BiConsumer<? super T, ? super Throwable> action, Executor executor) { |
|
2150 |
return uniWhenCompleteStage(screenExecutor(executor), action); |
|
19192 | 2151 |
} |
2152 |
||
26336 | 2153 |
public <U> CompletableFuture<U> handle( |
2154 |
BiFunction<? super T, Throwable, ? extends U> fn) { |
|
2155 |
return uniHandleStage(null, fn); |
|
16745 | 2156 |
} |
2157 |
||
26336 | 2158 |
public <U> CompletableFuture<U> handleAsync( |
2159 |
BiFunction<? super T, Throwable, ? extends U> fn) { |
|
2160 |
return uniHandleStage(asyncPool, fn); |
|
19192 | 2161 |
} |
2162 |
||
26336 | 2163 |
public <U> CompletableFuture<U> handleAsync( |
2164 |
BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) { |
|
2165 |
return uniHandleStage(screenExecutor(executor), fn); |
|
19192 | 2166 |
} |
2167 |
||
2168 |
/** |
|
26336 | 2169 |
* Returns this CompletableFuture. |
19192 | 2170 |
* |
2171 |
* @return this CompletableFuture |
|
2172 |
*/ |
|
2173 |
public CompletableFuture<T> toCompletableFuture() { |
|
2174 |
return this; |
|
2175 |
} |
|
2176 |
||
2177 |
// not in interface CompletionStage |
|
2178 |
||
16745 | 2179 |
/** |
2180 |
* Returns a new CompletableFuture that is completed when this |
|
2181 |
* CompletableFuture completes, with the result of the given |
|
2182 |
* function of the exception triggering this CompletableFuture's |
|
2183 |
* completion when it completes exceptionally; otherwise, if this |
|
2184 |
* CompletableFuture completes normally, then the returned |
|
2185 |
* CompletableFuture also completes normally with the same value. |
|
19192 | 2186 |
* Note: More flexible versions of this functionality are |
2187 |
* available using methods {@code whenComplete} and {@code handle}. |
|
16745 | 2188 |
* |
2189 |
* @param fn the function to use to compute the value of the |
|
2190 |
* returned CompletableFuture if this CompletableFuture completed |
|
2191 |
* exceptionally |
|
2192 |
* @return the new CompletableFuture |
|
2193 |
*/ |
|
26336 | 2194 |
public CompletableFuture<T> exceptionally( |
2195 |
Function<Throwable, ? extends T> fn) { |
|
2196 |
return uniExceptionallyStage(fn); |
|
16745 | 2197 |
} |
2198 |
||
2199 |
/* ------------- Arbitrary-arity constructions -------------- */ |
|
2200 |
||
2201 |
/** |
|
2202 |
* Returns a new CompletableFuture that is completed when all of |
|
2203 |
* the given CompletableFutures complete. If any of the given |
|
2204 |
* CompletableFutures complete exceptionally, then the returned |
|
2205 |
* CompletableFuture also does so, with a CompletionException |
|
2206 |
* holding this exception as its cause. Otherwise, the results, |
|
2207 |
* if any, of the given CompletableFutures are not reflected in |
|
2208 |
* the returned CompletableFuture, but may be obtained by |
|
2209 |
* inspecting them individually. If no CompletableFutures are |
|
2210 |
* provided, returns a CompletableFuture completed with the value |
|
2211 |
* {@code null}. |
|
2212 |
* |
|
2213 |
* <p>Among the applications of this method is to await completion |
|
2214 |
* of a set of independent CompletableFutures before continuing a |
|
2215 |
* program, as in: {@code CompletableFuture.allOf(c1, c2, |
|
2216 |
* c3).join();}. |
|
2217 |
* |
|
2218 |
* @param cfs the CompletableFutures |
|
2219 |
* @return a new CompletableFuture that is completed when all of the |
|
2220 |
* given CompletableFutures complete |
|
2221 |
* @throws NullPointerException if the array or any of its elements are |
|
2222 |
* {@code null} |
|
2223 |
*/ |
|
2224 |
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { |
|
26336 | 2225 |
return andTree(cfs, 0, cfs.length - 1); |
16745 | 2226 |
} |
2227 |
||
2228 |
/** |
|
2229 |
* Returns a new CompletableFuture that is completed when any of |
|
2230 |
* the given CompletableFutures complete, with the same result. |
|
2231 |
* Otherwise, if it completed exceptionally, the returned |
|
2232 |
* CompletableFuture also does so, with a CompletionException |
|
2233 |
* holding this exception as its cause. If no CompletableFutures |
|
2234 |
* are provided, returns an incomplete CompletableFuture. |
|
2235 |
* |
|
2236 |
* @param cfs the CompletableFutures |
|
2237 |
* @return a new CompletableFuture that is completed with the |
|
2238 |
* result or exception of any of the given CompletableFutures when |
|
2239 |
* one completes |
|
2240 |
* @throws NullPointerException if the array or any of its elements are |
|
2241 |
* {@code null} |
|
2242 |
*/ |
|
2243 |
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { |
|
26336 | 2244 |
return orTree(cfs, 0, cfs.length - 1); |
16745 | 2245 |
} |
2246 |
||
2247 |
/* ------------- Control and status methods -------------- */ |
|
2248 |
||
2249 |
/** |
|
2250 |
* If not already completed, completes this CompletableFuture with |
|
2251 |
* a {@link CancellationException}. Dependent CompletableFutures |
|
2252 |
* that have not already completed will also complete |
|
2253 |
* exceptionally, with a {@link CompletionException} caused by |
|
2254 |
* this {@code CancellationException}. |
|
2255 |
* |
|
2256 |
* @param mayInterruptIfRunning this value has no effect in this |
|
2257 |
* implementation because interrupts are not used to control |
|
2258 |
* processing. |
|
2259 |
* |
|
2260 |
* @return {@code true} if this task is now cancelled |
|
2261 |
*/ |
|
2262 |
public boolean cancel(boolean mayInterruptIfRunning) { |
|
2263 |
boolean cancelled = (result == null) && |
|
26336 | 2264 |
internalComplete(new AltResult(new CancellationException())); |
16745 | 2265 |
postComplete(); |
2266 |
return cancelled || isCancelled(); |
|
2267 |
} |
|
2268 |
||
2269 |
/** |
|
2270 |
* Returns {@code true} if this CompletableFuture was cancelled |
|
2271 |
* before it completed normally. |
|
2272 |
* |
|
2273 |
* @return {@code true} if this CompletableFuture was cancelled |
|
2274 |
* before it completed normally |
|
2275 |
*/ |
|
2276 |
public boolean isCancelled() { |
|
2277 |
Object r; |
|
2278 |
return ((r = result) instanceof AltResult) && |
|
2279 |
(((AltResult)r).ex instanceof CancellationException); |
|
2280 |
} |
|
2281 |
||
2282 |
/** |
|
19192 | 2283 |
* Returns {@code true} if this CompletableFuture completed |
2284 |
* exceptionally, in any way. Possible causes include |
|
2285 |
* cancellation, explicit invocation of {@code |
|
2286 |
* completeExceptionally}, and abrupt termination of a |
|
2287 |
* CompletionStage action. |
|
2288 |
* |
|
2289 |
* @return {@code true} if this CompletableFuture completed |
|
2290 |
* exceptionally |
|
2291 |
*/ |
|
2292 |
public boolean isCompletedExceptionally() { |
|
2293 |
Object r; |
|
2294 |
return ((r = result) instanceof AltResult) && r != NIL; |
|
2295 |
} |
|
2296 |
||
2297 |
/** |
|
16745 | 2298 |
* Forcibly sets or resets the value subsequently returned by |
2299 |
* method {@link #get()} and related methods, whether or not |
|
2300 |
* already completed. This method is designed for use only in |
|
2301 |
* error recovery actions, and even in such situations may result |
|
2302 |
* in ongoing dependent completions using established versus |
|
2303 |
* overwritten outcomes. |
|
2304 |
* |
|
2305 |
* @param value the completion value |
|
2306 |
*/ |
|
2307 |
public void obtrudeValue(T value) { |
|
2308 |
result = (value == null) ? NIL : value; |
|
2309 |
postComplete(); |
|
2310 |
} |
|
2311 |
||
2312 |
/** |
|
2313 |
* Forcibly causes subsequent invocations of method {@link #get()} |
|
2314 |
* and related methods to throw the given exception, whether or |
|
2315 |
* not already completed. This method is designed for use only in |
|
26336 | 2316 |
* error recovery actions, and even in such situations may result |
2317 |
* in ongoing dependent completions using established versus |
|
16745 | 2318 |
* overwritten outcomes. |
2319 |
* |
|
2320 |
* @param ex the exception |
|
26336 | 2321 |
* @throws NullPointerException if the exception is null |
16745 | 2322 |
*/ |
2323 |
public void obtrudeException(Throwable ex) { |
|
2324 |
if (ex == null) throw new NullPointerException(); |
|
2325 |
result = new AltResult(ex); |
|
2326 |
postComplete(); |
|
2327 |
} |
|
2328 |
||
2329 |
/** |
|
2330 |
* Returns the estimated number of CompletableFutures whose |
|
2331 |
* completions are awaiting completion of this CompletableFuture. |
|
2332 |
* This method is designed for use in monitoring system state, not |
|
2333 |
* for synchronization control. |
|
2334 |
* |
|
2335 |
* @return the number of dependent CompletableFutures |
|
2336 |
*/ |
|
2337 |
public int getNumberOfDependents() { |
|
2338 |
int count = 0; |
|
26336 | 2339 |
for (Completion p = stack; p != null; p = p.next) |
16745 | 2340 |
++count; |
2341 |
return count; |
|
2342 |
} |
|
2343 |
||
2344 |
/** |
|
2345 |
* Returns a string identifying this CompletableFuture, as well as |
|
2346 |
* its completion state. The state, in brackets, contains the |
|
2347 |
* String {@code "Completed Normally"} or the String {@code |
|
2348 |
* "Completed Exceptionally"}, or the String {@code "Not |
|
2349 |
* completed"} followed by the number of CompletableFutures |
|
2350 |
* dependent upon its completion, if any. |
|
2351 |
* |
|
2352 |
* @return a string identifying this CompletableFuture, as well as its state |
|
2353 |
*/ |
|
2354 |
public String toString() { |
|
2355 |
Object r = result; |
|
2356 |
int count; |
|
2357 |
return super.toString() + |
|
2358 |
((r == null) ? |
|
2359 |
(((count = getNumberOfDependents()) == 0) ? |
|
2360 |
"[Not completed]" : |
|
2361 |
"[Not completed, " + count + " dependents]") : |
|
2362 |
(((r instanceof AltResult) && ((AltResult)r).ex != null) ? |
|
2363 |
"[Completed exceptionally]" : |
|
2364 |
"[Completed normally]")); |
|
2365 |
} |
|
2366 |
||
2367 |
// Unsafe mechanics |
|
2368 |
private static final sun.misc.Unsafe UNSAFE; |
|
2369 |
private static final long RESULT; |
|
26336 | 2370 |
private static final long STACK; |
2371 |
private static final long NEXT; |
|
16745 | 2372 |
static { |
2373 |
try { |
|
26336 | 2374 |
final sun.misc.Unsafe u; |
2375 |
UNSAFE = u = sun.misc.Unsafe.getUnsafe(); |
|
16745 | 2376 |
Class<?> k = CompletableFuture.class; |
26336 | 2377 |
RESULT = u.objectFieldOffset(k.getDeclaredField("result")); |
2378 |
STACK = u.objectFieldOffset(k.getDeclaredField("stack")); |
|
2379 |
NEXT = u.objectFieldOffset |
|
2380 |
(Completion.class.getDeclaredField("next")); |
|
2381 |
} catch (Exception x) { |
|
2382 |
throw new Error(x); |
|
16745 | 2383 |
} |
2384 |
} |
|
2385 |
} |