55 |
|
* Executor} or by default the {@link ForkJoinPool#commonPool()}, of a |
56 |
|
* function or action that will result in the completion of a new |
57 |
|
* CompletableFuture. To simplify monitoring, debugging, and tracking, |
58 |
< |
* all generated asynchronous tasks are instances of the tagging |
58 |
> |
* all generated asynchronous tasks are instances of the marker |
59 |
|
* interface {@link AsynchronousCompletionTask}. |
60 |
|
* |
61 |
|
* <p><em>jsr166e note: During transition, this class |
83 |
|
/* |
84 |
|
* Overview: |
85 |
|
* |
86 |
< |
* 1. Non-nullness of field result (set via CAS) indicates |
87 |
< |
* done. An AltResult is used to box null as a result, as well as |
88 |
< |
* to hold exceptions. Using a single field makes completion fast |
86 |
> |
* 1. Non-nullness of field result (set via CAS) indicates done. |
87 |
> |
* An AltResult is used to box null as a result, as well as to |
88 |
> |
* hold exceptions. Using a single field makes completion fast |
89 |
|
* and simple to detect and trigger, at the expense of a lot of |
90 |
|
* encoding and decoding that infiltrates many methods. One minor |
91 |
|
* simplification relies on the (static) NIL (to box null results) |
287 |
|
else if (q.thread != null && result == null) { |
288 |
|
try { |
289 |
|
ForkJoinPool.managedBlock(q); |
290 |
< |
} catch(InterruptedException ex){ |
290 |
> |
} catch (InterruptedException ex) { |
291 |
|
q.interruptControl = -1; |
292 |
|
} |
293 |
|
} |
320 |
|
if (nanos <= 0L) |
321 |
|
throw new TimeoutException(); |
322 |
|
long d = System.nanoTime() + nanos; |
323 |
< |
q = new WaitNode(true, nanos, d == 0L? 1L : d); // avoid 0 |
323 |
> |
q = new WaitNode(true, nanos, d == 0L ? 1L : d); // avoid 0 |
324 |
|
} |
325 |
|
else if (!queued) |
326 |
|
queued = UNSAFE.compareAndSwapObject(this, WAITERS, |
338 |
|
else if (q.thread != null && result == null) { |
339 |
|
try { |
340 |
|
ForkJoinPool.managedBlock(q); |
341 |
< |
} catch(InterruptedException ex){ |
341 |
> |
} catch (InterruptedException ex) { |
342 |
|
q.interruptControl = -1; |
343 |
|
} |
344 |
|
} |
380 |
|
/* ------------- Async tasks -------------- */ |
381 |
|
|
382 |
|
/** |
383 |
< |
* A tagging interface identifying asynchronous tasks produced by |
383 |
> |
* A marker interface identifying asynchronous tasks produced by |
384 |
|
* {@code async} methods. This may be useful for monitoring, |
385 |
|
* debugging, and tracking asynchronous activities. |
386 |
|
*/ |
388 |
|
} |
389 |
|
|
390 |
|
/** Base class can act as either FJ or plain Runnable */ |
391 |
< |
static abstract class Async extends ForkJoinTask<Void> |
391 |
> |
abstract static class Async extends ForkJoinTask<Void> |
392 |
|
implements Runnable, AsynchronousCompletionTask { |
393 |
|
public final Void getRawResult() { return null; } |
394 |
|
public final void setRawResult(Void v) { } |
557 |
|
} |
558 |
|
|
559 |
|
// Opportunistically subclass AtomicInteger to use compareAndSet to claim. |
560 |
< |
static abstract class Completion extends AtomicInteger implements Runnable { |
560 |
> |
abstract static class Completion extends AtomicInteger implements Runnable { |
561 |
|
} |
562 |
|
|
563 |
|
static final class ApplyCompletion<T,U> extends Completion { |
1053 |
|
(r = a.result) != null && |
1054 |
|
compareAndSet(0, 1)) { |
1055 |
|
if ((r instanceof AltResult) && |
1056 |
< |
(ex = ((AltResult)r).ex) != null) { |
1056 |
> |
(ex = ((AltResult)r).ex) != null) { |
1057 |
|
try { |
1058 |
|
t = fn.apply(ex); |
1059 |
|
} catch (Throwable rex) { |
1319 |
|
Object r; Throwable ex, cause; |
1320 |
|
if ((r = result) == null && (r = waitingGet(true)) == null) |
1321 |
|
throw new InterruptedException(); |
1322 |
< |
if (r instanceof AltResult) { |
1323 |
< |
if ((ex = ((AltResult)r).ex) != null) { |
1324 |
< |
if (ex instanceof CancellationException) |
1325 |
< |
throw (CancellationException)ex; |
1326 |
< |
if ((ex instanceof CompletionException) && |
1327 |
< |
(cause = ex.getCause()) != null) |
1328 |
< |
ex = cause; |
1329 |
< |
throw new ExecutionException(ex); |
1330 |
< |
} |
1331 |
< |
return null; |
1322 |
> |
if (!(r instanceof AltResult)) { |
1323 |
> |
@SuppressWarnings("unchecked") T tr = (T) r; |
1324 |
> |
return tr; |
1325 |
|
} |
1326 |
< |
@SuppressWarnings("unchecked") T tr = (T) r; |
1327 |
< |
return tr; |
1326 |
> |
if ((ex = ((AltResult)r).ex) == null) |
1327 |
> |
return null; |
1328 |
> |
if (ex instanceof CancellationException) |
1329 |
> |
throw (CancellationException)ex; |
1330 |
> |
if ((ex instanceof CompletionException) && |
1331 |
> |
(cause = ex.getCause()) != null) |
1332 |
> |
ex = cause; |
1333 |
> |
throw new ExecutionException(ex); |
1334 |
|
} |
1335 |
|
|
1336 |
|
/** |
1355 |
|
throw new InterruptedException(); |
1356 |
|
if ((r = result) == null) |
1357 |
|
r = timedAwaitDone(nanos); |
1358 |
< |
if (r instanceof AltResult) { |
1359 |
< |
if ((ex = ((AltResult)r).ex) != null) { |
1360 |
< |
if (ex instanceof CancellationException) |
1362 |
< |
throw (CancellationException)ex; |
1363 |
< |
if ((ex instanceof CompletionException) && |
1364 |
< |
(cause = ex.getCause()) != null) |
1365 |
< |
ex = cause; |
1366 |
< |
throw new ExecutionException(ex); |
1367 |
< |
} |
1368 |
< |
return null; |
1358 |
> |
if (!(r instanceof AltResult)) { |
1359 |
> |
@SuppressWarnings("unchecked") T tr = (T) r; |
1360 |
> |
return tr; |
1361 |
|
} |
1362 |
< |
@SuppressWarnings("unchecked") T tr = (T) r; |
1363 |
< |
return tr; |
1362 |
> |
if ((ex = ((AltResult)r).ex) == null) |
1363 |
> |
return null; |
1364 |
> |
if (ex instanceof CancellationException) |
1365 |
> |
throw (CancellationException)ex; |
1366 |
> |
if ((ex instanceof CompletionException) && |
1367 |
> |
(cause = ex.getCause()) != null) |
1368 |
> |
ex = cause; |
1369 |
> |
throw new ExecutionException(ex); |
1370 |
|
} |
1371 |
|
|
1372 |
|
/** |
1387 |
|
Object r; Throwable ex; |
1388 |
|
if ((r = result) == null) |
1389 |
|
r = waitingGet(false); |
1390 |
< |
if (r instanceof AltResult) { |
1391 |
< |
if ((ex = ((AltResult)r).ex) != null) { |
1392 |
< |
if (ex instanceof CancellationException) |
1395 |
< |
throw (CancellationException)ex; |
1396 |
< |
if (ex instanceof CompletionException) |
1397 |
< |
throw (CompletionException)ex; |
1398 |
< |
throw new CompletionException(ex); |
1399 |
< |
} |
1400 |
< |
return null; |
1390 |
> |
if (!(r instanceof AltResult)) { |
1391 |
> |
@SuppressWarnings("unchecked") T tr = (T) r; |
1392 |
> |
return tr; |
1393 |
|
} |
1394 |
< |
@SuppressWarnings("unchecked") T tr = (T) r; |
1395 |
< |
return tr; |
1394 |
> |
if ((ex = ((AltResult)r).ex) == null) |
1395 |
> |
return null; |
1396 |
> |
if (ex instanceof CancellationException) |
1397 |
> |
throw (CancellationException)ex; |
1398 |
> |
if (ex instanceof CompletionException) |
1399 |
> |
throw (CompletionException)ex; |
1400 |
> |
throw new CompletionException(ex); |
1401 |
|
} |
1402 |
|
|
1403 |
|
/** |
1414 |
|
Object r; Throwable ex; |
1415 |
|
if ((r = result) == null) |
1416 |
|
return valueIfAbsent; |
1417 |
< |
if (r instanceof AltResult) { |
1418 |
< |
if ((ex = ((AltResult)r).ex) != null) { |
1419 |
< |
if (ex instanceof CancellationException) |
1423 |
< |
throw (CancellationException)ex; |
1424 |
< |
if (ex instanceof CompletionException) |
1425 |
< |
throw (CompletionException)ex; |
1426 |
< |
throw new CompletionException(ex); |
1427 |
< |
} |
1428 |
< |
return null; |
1417 |
> |
if (!(r instanceof AltResult)) { |
1418 |
> |
@SuppressWarnings("unchecked") T tr = (T) r; |
1419 |
> |
return tr; |
1420 |
|
} |
1421 |
< |
@SuppressWarnings("unchecked") T tr = (T) r; |
1422 |
< |
return tr; |
1421 |
> |
if ((ex = ((AltResult)r).ex) == null) |
1422 |
> |
return null; |
1423 |
> |
if (ex instanceof CancellationException) |
1424 |
> |
throw (CancellationException)ex; |
1425 |
> |
if (ex instanceof CompletionException) |
1426 |
> |
throw (CompletionException)ex; |
1427 |
> |
throw new CompletionException(ex); |
1428 |
|
} |
1429 |
|
|
1430 |
|
/** |
1788 |
|
* @param executor the executor to use for asynchronous execution |
1789 |
|
* @return the new CompletableFuture |
1790 |
|
*/ |
1795 |
– |
|
1791 |
|
public <U,V> CompletableFuture<V> thenCombineAsync(CompletableFuture<? extends U> other, |
1792 |
|
BiFun<? super T,? super U,? extends V> fn, |
1793 |
|
Executor executor) { |
2444 |
|
* then the returned CompletableFuture also does so, with a |
2445 |
|
* CompletionException holding this exception as its cause. |
2446 |
|
* |
2447 |
< |
* @param fn the function returning a new CompletableFuture. |
2447 |
> |
* @param fn the function returning a new CompletableFuture |
2448 |
|
* @return the CompletableFuture, that {@code isDone()} upon |
2449 |
|
* return if completed by the given function, or an exception |
2450 |
< |
* occurs. |
2450 |
> |
* occurs |
2451 |
|
*/ |
2452 |
|
public <U> CompletableFuture<U> thenCompose(Fun<? super T, |
2453 |
|
CompletableFuture<U>> fn) { |
2526 |
|
if (r != null && (d == null || d.compareAndSet(0, 1))) { |
2527 |
|
T t = null; Throwable ex, dx = null; |
2528 |
|
if (r instanceof AltResult) { |
2529 |
< |
if ((ex = ((AltResult)r).ex) != null) { |
2529 |
> |
if ((ex = ((AltResult)r).ex) != null) { |
2530 |
|
try { |
2531 |
|
t = fn.apply(ex); |
2532 |
|
} catch (Throwable rex) { |
2554 |
|
* |
2555 |
|
* @param fn the function to use to compute the value of the |
2556 |
|
* returned CompletableFuture |
2562 |
– |
|
2557 |
|
* @return the new CompletableFuture |
2558 |
|
*/ |
2559 |
|
public <U> CompletableFuture<U> handle(BiFun<? super T, Throwable, ? extends U> fn) { |
2627 |
|
*/ |
2628 |
|
public boolean isCancelled() { |
2629 |
|
Object r; |
2630 |
< |
return ((r = result) != null && |
2631 |
< |
(r instanceof AltResult) && |
2638 |
< |
(((AltResult)r).ex instanceof CancellationException)); |
2630 |
> |
return ((r = result) instanceof AltResult) && |
2631 |
> |
(((AltResult)r).ex instanceof CancellationException); |
2632 |
|
} |
2633 |
|
|
2634 |
|
/** |
2635 |
|
* Forcibly sets or resets the value subsequently returned by |
2636 |
< |
* method get() and related methods, whether or not already |
2637 |
< |
* completed. This method is designed for use only in error |
2638 |
< |
* recovery actions, and even in such situations may result in |
2639 |
< |
* ongoing dependent completions using established versus |
2640 |
< |
* overwritten values. |
2636 |
> |
* method {@link #get()} and related methods, whether or not |
2637 |
> |
* already completed. This method is designed for use only in |
2638 |
> |
* error recovery actions, and even in such situations may result |
2639 |
> |
* in ongoing dependent completions using established versus |
2640 |
> |
* overwritten outcomes. |
2641 |
|
* |
2642 |
|
* @param value the completion value |
2643 |
|
*/ |
2646 |
|
postComplete(); |
2647 |
|
} |
2648 |
|
|
2649 |
+ |
/** |
2650 |
+ |
* Forcibly causes subsequent invocations of method {@link #get()} |
2651 |
+ |
* and related methods to throw the given exception, whether or |
2652 |
+ |
* not already completed. This method is designed for use only in |
2653 |
+ |
* recovery actions, and even in such situations may result in |
2654 |
+ |
* ongoing dependent completions using established versus |
2655 |
+ |
* overwritten outcomes. |
2656 |
+ |
* |
2657 |
+ |
* @param ex the exception |
2658 |
+ |
*/ |
2659 |
+ |
public void obtrudeException(Throwable ex) { |
2660 |
+ |
if (ex == null) throw new NullPointerException(); |
2661 |
+ |
result = new AltResult(ex); |
2662 |
+ |
postComplete(); |
2663 |
+ |
} |
2664 |
+ |
|
2665 |
|
// Unsafe mechanics |
2666 |
|
private static final sun.misc.Unsafe UNSAFE; |
2667 |
|
private static final long RESULT; |
2693 |
|
private static sun.misc.Unsafe getUnsafe() { |
2694 |
|
try { |
2695 |
|
return sun.misc.Unsafe.getUnsafe(); |
2696 |
< |
} catch (SecurityException se) { |
2697 |
< |
try { |
2698 |
< |
return java.security.AccessController.doPrivileged |
2699 |
< |
(new java.security |
2700 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
2701 |
< |
public sun.misc.Unsafe run() throws Exception { |
2702 |
< |
java.lang.reflect.Field f = sun.misc |
2703 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
2704 |
< |
f.setAccessible(true); |
2705 |
< |
return (sun.misc.Unsafe) f.get(null); |
2706 |
< |
}}); |
2707 |
< |
} catch (java.security.PrivilegedActionException e) { |
2708 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
2709 |
< |
e.getCause()); |
2710 |
< |
} |
2696 |
> |
} catch (SecurityException tryReflectionInstead) {} |
2697 |
> |
try { |
2698 |
> |
return java.security.AccessController.doPrivileged |
2699 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
2700 |
> |
public sun.misc.Unsafe run() throws Exception { |
2701 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
2702 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
2703 |
> |
f.setAccessible(true); |
2704 |
> |
Object x = f.get(null); |
2705 |
> |
if (k.isInstance(x)) |
2706 |
> |
return k.cast(x); |
2707 |
> |
} |
2708 |
> |
throw new NoSuchFieldError("the Unsafe"); |
2709 |
> |
}}); |
2710 |
> |
} catch (java.security.PrivilegedActionException e) { |
2711 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
2712 |
> |
e.getCause()); |
2713 |
|
} |
2714 |
|
} |
2704 |
– |
|
2715 |
|
} |