420 |
|
static final int ASYNC = 1; |
421 |
|
static final int NESTED = -1; |
422 |
|
|
423 |
– |
/** |
424 |
– |
* Spins before blocking in waitingGet |
425 |
– |
*/ |
426 |
– |
static final int SPINS = (Runtime.getRuntime().availableProcessors() > 1 ? |
427 |
– |
1 << 8 : 0); |
428 |
– |
|
423 |
|
/* ------------- Base Completion classes and operations -------------- */ |
424 |
|
|
425 |
|
@SuppressWarnings("serial") |
612 |
|
CompletableFuture<V> d = newIncompleteFuture(); |
613 |
|
if (e != null || !d.uniApply(this, f, null)) { |
614 |
|
UniApply<T,V> c = new UniApply<T,V>(e, d, this, f); |
615 |
< |
push(c); |
616 |
< |
c.tryFire(SYNC); |
615 |
> |
if (e != null && result != null) { |
616 |
> |
try { |
617 |
> |
e.execute(c); |
618 |
> |
} catch (Throwable ex) { |
619 |
> |
d.completeThrowable(ex); |
620 |
> |
} |
621 |
> |
} |
622 |
> |
else { |
623 |
> |
push(c); |
624 |
> |
c.tryFire(SYNC); |
625 |
> |
} |
626 |
|
} |
627 |
|
return d; |
628 |
|
} |
676 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
677 |
|
if (e != null || !d.uniAccept(this, f, null)) { |
678 |
|
UniAccept<T> c = new UniAccept<T>(e, d, this, f); |
679 |
< |
push(c); |
680 |
< |
c.tryFire(SYNC); |
679 |
> |
if (e != null && result != null) { |
680 |
> |
try { |
681 |
> |
e.execute(c); |
682 |
> |
} catch (Throwable ex) { |
683 |
> |
d.completeThrowable(ex); |
684 |
> |
} |
685 |
> |
} |
686 |
> |
else { |
687 |
> |
push(c); |
688 |
> |
c.tryFire(SYNC); |
689 |
> |
} |
690 |
|
} |
691 |
|
return d; |
692 |
|
} |
733 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
734 |
|
if (e != null || !d.uniRun(this, f, null)) { |
735 |
|
UniRun<T> c = new UniRun<T>(e, d, this, f); |
736 |
< |
push(c); |
737 |
< |
c.tryFire(SYNC); |
736 |
> |
if (e != null && result != null) { |
737 |
> |
try { |
738 |
> |
e.execute(c); |
739 |
> |
} catch (Throwable ex) { |
740 |
> |
d.completeThrowable(ex); |
741 |
> |
} |
742 |
> |
} |
743 |
> |
else { |
744 |
> |
push(c); |
745 |
> |
c.tryFire(SYNC); |
746 |
> |
} |
747 |
|
} |
748 |
|
return d; |
749 |
|
} |
805 |
|
CompletableFuture<T> d = newIncompleteFuture(); |
806 |
|
if (e != null || !d.uniWhenComplete(this, f, null)) { |
807 |
|
UniWhenComplete<T> c = new UniWhenComplete<T>(e, d, this, f); |
808 |
< |
push(c); |
809 |
< |
c.tryFire(SYNC); |
808 |
> |
if (e != null && result != null) { |
809 |
> |
try { |
810 |
> |
e.execute(c); |
811 |
> |
} catch (Throwable ex) { |
812 |
> |
d.completeThrowable(ex); |
813 |
> |
} |
814 |
> |
} |
815 |
> |
else { |
816 |
> |
push(c); |
817 |
> |
c.tryFire(SYNC); |
818 |
> |
} |
819 |
|
} |
820 |
|
return d; |
821 |
|
} |
870 |
|
CompletableFuture<V> d = newIncompleteFuture(); |
871 |
|
if (e != null || !d.uniHandle(this, f, null)) { |
872 |
|
UniHandle<T,V> c = new UniHandle<T,V>(e, d, this, f); |
873 |
< |
push(c); |
874 |
< |
c.tryFire(SYNC); |
873 |
> |
if (e != null && result != null) { |
874 |
> |
try { |
875 |
> |
e.execute(c); |
876 |
> |
} catch (Throwable ex) { |
877 |
> |
d.completeThrowable(ex); |
878 |
> |
} |
879 |
> |
} |
880 |
> |
else { |
881 |
> |
push(c); |
882 |
> |
c.tryFire(SYNC); |
883 |
> |
} |
884 |
|
} |
885 |
|
return d; |
886 |
|
} |
1039 |
|
if (f == null) throw new NullPointerException(); |
1040 |
|
Object r, s; Throwable x; |
1041 |
|
CompletableFuture<V> d = newIncompleteFuture(); |
1042 |
< |
if (e == null && (r = result) != null) { |
1042 |
> |
if ((r = result) != null && e == null) { |
1043 |
|
if (r instanceof AltResult) { |
1044 |
|
if ((x = ((AltResult)r).ex) != null) { |
1045 |
|
d.result = encodeThrowable(x, r); |
1064 |
|
} |
1065 |
|
} |
1066 |
|
UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f); |
1067 |
< |
push(c); |
1068 |
< |
c.tryFire(SYNC); |
1067 |
> |
if (r != null && e != null) { |
1068 |
> |
try { |
1069 |
> |
e.execute(new UniCompose<T,V>(null, d, this, f)); |
1070 |
> |
} catch (Throwable ex) { |
1071 |
> |
d.completeThrowable(ex); |
1072 |
> |
} |
1073 |
> |
} |
1074 |
> |
else { |
1075 |
> |
push(c); |
1076 |
> |
c.tryFire(SYNC); |
1077 |
> |
} |
1078 |
|
return d; |
1079 |
|
} |
1080 |
|
|
1199 |
|
CompletableFuture<V> d = newIncompleteFuture(); |
1200 |
|
if (e != null || !d.biApply(this, b, f, null)) { |
1201 |
|
BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f); |
1202 |
< |
bipush(b, c); |
1203 |
< |
c.tryFire(SYNC); |
1202 |
> |
if (e != null && result != null && b.result != null) { |
1203 |
> |
try { |
1204 |
> |
e.execute(c); |
1205 |
> |
} catch (Throwable ex) { |
1206 |
> |
d.completeThrowable(ex); |
1207 |
> |
} |
1208 |
> |
} |
1209 |
> |
else { |
1210 |
> |
bipush(b, c); |
1211 |
> |
c.tryFire(SYNC); |
1212 |
> |
} |
1213 |
|
} |
1214 |
|
return d; |
1215 |
|
} |
1280 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
1281 |
|
if (e != null || !d.biAccept(this, b, f, null)) { |
1282 |
|
BiAccept<T,U> c = new BiAccept<T,U>(e, d, this, b, f); |
1283 |
< |
bipush(b, c); |
1284 |
< |
c.tryFire(SYNC); |
1283 |
> |
if (e != null && result != null && b.result != null) { |
1284 |
> |
try { |
1285 |
> |
e.execute(c); |
1286 |
> |
} catch (Throwable ex) { |
1287 |
> |
d.completeThrowable(ex); |
1288 |
> |
} |
1289 |
> |
} |
1290 |
> |
else { |
1291 |
> |
bipush(b, c); |
1292 |
> |
c.tryFire(SYNC); |
1293 |
> |
} |
1294 |
|
} |
1295 |
|
return d; |
1296 |
|
} |
1348 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
1349 |
|
if (e != null || !d.biRun(this, b, f, null)) { |
1350 |
|
BiRun<T,?> c = new BiRun<>(e, d, this, b, f); |
1351 |
< |
bipush(b, c); |
1352 |
< |
c.tryFire(SYNC); |
1351 |
> |
if (e != null && result != null && b.result != null) { |
1352 |
> |
try { |
1353 |
> |
e.execute(c); |
1354 |
> |
} catch (Throwable ex) { |
1355 |
> |
d.completeThrowable(ex); |
1356 |
> |
} |
1357 |
> |
} |
1358 |
> |
else { |
1359 |
> |
bipush(b, c); |
1360 |
> |
c.tryFire(SYNC); |
1361 |
> |
} |
1362 |
|
} |
1363 |
|
return d; |
1364 |
|
} |
1498 |
|
CompletableFuture<V> d = newIncompleteFuture(); |
1499 |
|
if (e != null || !d.orApply(this, b, f, null)) { |
1500 |
|
OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f); |
1501 |
< |
orpush(b, c); |
1502 |
< |
c.tryFire(SYNC); |
1501 |
> |
if (e != null && (result != null || b.result != null)) { |
1502 |
> |
try { |
1503 |
> |
e.execute(c); |
1504 |
> |
} catch (Throwable ex) { |
1505 |
> |
d.completeThrowable(ex); |
1506 |
> |
} |
1507 |
> |
} |
1508 |
> |
else { |
1509 |
> |
orpush(b, c); |
1510 |
> |
c.tryFire(SYNC); |
1511 |
> |
} |
1512 |
|
} |
1513 |
|
return d; |
1514 |
|
} |
1571 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
1572 |
|
if (e != null || !d.orAccept(this, b, f, null)) { |
1573 |
|
OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f); |
1574 |
< |
orpush(b, c); |
1575 |
< |
c.tryFire(SYNC); |
1574 |
> |
if (e != null && (result != null || b.result != null)) { |
1575 |
> |
try { |
1576 |
> |
e.execute(c); |
1577 |
> |
} catch (Throwable ex) { |
1578 |
> |
d.completeThrowable(ex); |
1579 |
> |
} |
1580 |
> |
} |
1581 |
> |
else { |
1582 |
> |
orpush(b, c); |
1583 |
> |
c.tryFire(SYNC); |
1584 |
> |
} |
1585 |
|
} |
1586 |
|
return d; |
1587 |
|
} |
1638 |
|
CompletableFuture<Void> d = newIncompleteFuture(); |
1639 |
|
if (e != null || !d.orRun(this, b, f, null)) { |
1640 |
|
OrRun<T,?> c = new OrRun<>(e, d, this, b, f); |
1641 |
< |
orpush(b, c); |
1642 |
< |
c.tryFire(SYNC); |
1641 |
> |
if (e != null && (result != null || b.result != null)) { |
1642 |
> |
try { |
1643 |
> |
e.execute(c); |
1644 |
> |
} catch (Throwable ex) { |
1645 |
> |
d.completeThrowable(ex); |
1646 |
> |
} |
1647 |
> |
} |
1648 |
> |
else { |
1649 |
> |
orpush(b, c); |
1650 |
> |
c.tryFire(SYNC); |
1651 |
> |
} |
1652 |
|
} |
1653 |
|
return d; |
1654 |
|
} |
1833 |
|
private Object waitingGet(boolean interruptible) { |
1834 |
|
Signaller q = null; |
1835 |
|
boolean queued = false; |
1734 |
– |
int spins = SPINS; |
1836 |
|
Object r; |
1837 |
|
while ((r = result) == null) { |
1838 |
< |
if (spins > 0) { |
1738 |
< |
if (ThreadLocalRandom.nextSecondarySeed() >= 0) |
1739 |
< |
--spins; |
1740 |
< |
} |
1741 |
< |
else if (q == null) |
1838 |
> |
if (q == null) { |
1839 |
|
q = new Signaller(interruptible, 0L, 0L); |
1840 |
+ |
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q); |
1841 |
+ |
} |
1842 |
|
else if (!queued) |
1843 |
|
queued = tryPushStack(q); |
1844 |
|
else { |
1878 |
|
Signaller q = null; |
1879 |
|
boolean queued = false; |
1880 |
|
Object r; |
1881 |
< |
while ((r = result) == null) { // similar to untimed, without spins |
1882 |
< |
if (q == null) |
1881 |
> |
while ((r = result) == null) { // similar to untimed |
1882 |
> |
if (q == null) { |
1883 |
|
q = new Signaller(true, nanos, deadline); |
1884 |
+ |
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q); |
1885 |
+ |
} |
1886 |
|
else if (!queued) |
1887 |
|
queued = tryPushStack(q); |
1888 |
|
else if (q.nanos <= 0L) |