ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletableFuture.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/CompletableFuture.java (file contents):
Revision 1.127 by jsr166, Sat Jun 14 14:24:06 2014 UTC vs.
Revision 1.128 by jsr166, Mon Jun 16 20:38:11 2014 UTC

# Line 192 | Line 192 | public class CompletableFuture<T> implem
192          return UNSAFE.compareAndSwapObject(this, RESULT, null, r);
193      }
194  
195    final boolean completeValue(T t) {
196        return UNSAFE.compareAndSwapObject(this, RESULT, null,
197                                           (t == null) ? NIL : t);
198    }
199
200    final boolean completeNil() {
201        return UNSAFE.compareAndSwapObject(this, RESULT, null, NIL);
202    }
203
204    final boolean completeThrowable(Throwable x) {
205        return UNSAFE.compareAndSwapObject(this, RESULT, null,
206                                           encodeThrowable(x));
207    }
208
209    final boolean completeRelay(Object r) {
210        return UNSAFE.compareAndSwapObject(this, RESULT, null, encodeRelay(r));
211    }
212
195      final boolean casStack(Completion cmp, Completion val) {
196          return UNSAFE.compareAndSwapObject(this, STACK, cmp, val);
197      }
# Line 221 | Line 203 | public class CompletableFuture<T> implem
203          AltResult(Throwable x) { this.ex = x; }
204      }
205  
206 +    /** The encoding of the null value. */
207      static final AltResult NIL = new AltResult(null);
208  
209 +    /** Completes with the null value, unless already completed. */
210 +    final boolean completeNull() {
211 +        return UNSAFE.compareAndSwapObject(this, RESULT, null,
212 +                                           NIL);
213 +    }
214 +
215 +    /** Returns the encoding of the given non-exceptional value. */
216 +    final Object encodeValue(T t) {
217 +        return (t == null) ? NIL : t;
218 +    }
219 +
220 +    /** Completes with a non-exceptional result, unless already completed. */
221 +    final boolean completeValue(T t) {
222 +        return UNSAFE.compareAndSwapObject(this, RESULT, null,
223 +                                           (t == null) ? NIL : t);
224 +    }
225 +
226      /**
227       * Returns the encoding of the given (non-null) exception as a
228       * wrapped CompletionException unless it is one already.
# Line 232 | Line 232 | public class CompletableFuture<T> implem
232                               new CompletionException(x));
233      }
234  
235 +    /** Completes with an exceptional result, unless already completed. */
236 +    final boolean completeThrowable(Throwable x) {
237 +        return UNSAFE.compareAndSwapObject(this, RESULT, null,
238 +                                           encodeThrowable(x));
239 +    }
240 +
241 +    /**
242 +     * Returns the encoding of the given (non-null) exception as a
243 +     * wrapped CompletionException unless it is one already.  May
244 +     * return the given Object r (which must have been the result of a
245 +     * source future) if it is equivalent, i.e. if this is a simple
246 +     * relay of an existing CompletionException.
247 +     */
248 +    static Object encodeThrowable(Throwable x, Object r) {
249 +        if (!(x instanceof CompletionException))
250 +            x = new CompletionException(x);
251 +        else if (r instanceof AltResult && x == ((AltResult)r).ex)
252 +            return r;
253 +        return new AltResult(x);
254 +    }
255 +
256 +    /**
257 +     * Completes with the given (non-null) exceptional result as a
258 +     * wrapped CompletionException unless it is one already, unless
259 +     * already completed.  May complete with the given Object r
260 +     * (which must have been the result of a source future) if it is
261 +     * equivalent, i.e. if this is a simple propagation of an
262 +     * existing CompletionException.
263 +     */
264 +    final boolean completeThrowable(Throwable x, Object r) {
265 +        return UNSAFE.compareAndSwapObject(this, RESULT, null,
266 +                                           encodeThrowable(x, r));
267 +    }
268 +
269      /**
270       * Returns the encoding of the given arguments: if the exception
271       * is non-null, encodes as AltResult.  Otherwise uses the given
# Line 254 | Line 288 | public class CompletableFuture<T> implem
288      }
289  
290      /**
291 +     * Completes with r or a copy of r, unless already completed.
292 +     * If exceptional, r is first coerced to a CompletionException.
293 +     */
294 +    final boolean completeRelay(Object r) {
295 +        return UNSAFE.compareAndSwapObject(this, RESULT, null,
296 +                                           encodeRelay(r));
297 +    }
298 +
299 +    /**
300       * Reports result using Future.get conventions.
301       */
302      private static <T> T reportGet(Object r)
# Line 498 | Line 541 | public class CompletableFuture<T> implem
541          Object r; Throwable x;
542          if (a == null || (r = a.result) == null || f == null)
543              return false;
544 <        if (result == null) {
545 <            exceptionalCompletion: try {
546 <                if (r instanceof AltResult)
547 <                    if ((x = ((AltResult)r).ex) != null)
548 <                        break exceptionalCompletion;
549 <                    else
550 <                        r = null;
544 >        tryComplete: if (result == null) {
545 >            if (r instanceof AltResult) {
546 >                if ((x = ((AltResult)r).ex) != null) {
547 >                    completeThrowable(x, r);
548 >                    break tryComplete;
549 >                }
550 >                r = null;
551 >            }
552 >            try {
553                  if (c != null && !c.claim())
554                      return false;
555                  @SuppressWarnings("unchecked") S s = (S) r;
556                  completeValue(f.apply(s));
512                return true;
557              } catch (Throwable ex) {
558 <                x = ex;
558 >                completeThrowable(ex);
559              }
516            completeThrowable(x);
560          }
561          return true;
562      }
# Line 552 | Line 595 | public class CompletableFuture<T> implem
595          Object r; Throwable x;
596          if (a == null || (r = a.result) == null || f == null)
597              return false;
598 <        if (result == null) {
599 <            exceptionalCompletion: try {
600 <                if (r instanceof AltResult)
601 <                    if ((x = ((AltResult)r).ex) != null)
602 <                        break exceptionalCompletion;
603 <                    else
604 <                        r = null;
598 >        tryComplete: if (result == null) {
599 >            if (r instanceof AltResult) {
600 >                if ((x = ((AltResult)r).ex) != null) {
601 >                    completeThrowable(x, r);
602 >                    break tryComplete;
603 >                }
604 >                r = null;
605 >            }
606 >            try {
607                  if (c != null && !c.claim())
608                      return false;
609                  @SuppressWarnings("unchecked") S s = (S) r;
610                  f.accept(s);
611 <                completeNil();
567 <                return true;
611 >                completeNull();
612              } catch (Throwable ex) {
613 <                x = ex;
613 >                completeThrowable(ex);
614              }
571            completeThrowable(x);
615          }
616          return true;
617      }
# Line 607 | Line 650 | public class CompletableFuture<T> implem
650          if (a == null || (r = a.result) == null || f == null)
651              return false;
652          if (result == null) {
653 <            exceptionalCompletion: try {
654 <                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
655 <                    break exceptionalCompletion;
656 <                if (c != null && !c.claim())
657 <                    return false;
658 <                f.run();
659 <                completeNil();
660 <                return true;
661 <            } catch (Throwable ex) {
662 <                x = ex;
663 <            }
621 <            completeThrowable(x);
653 >            if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
654 >                completeThrowable(x, r);
655 >            else
656 >                try {
657 >                    if (c != null && !c.claim())
658 >                        return false;
659 >                    f.run();
660 >                    completeNull();
661 >                } catch (Throwable ex) {
662 >                    completeThrowable(ex);
663 >                }
664          }
665          return true;
666      }
# Line 678 | Line 720 | public class CompletableFuture<T> implem
720                  if (x == null)
721                      x = ex;
722              }
723 <            completeThrowable(x);
723 >            completeThrowable(x, r);
724          }
725          return true;
726      }
# Line 759 | Line 801 | public class CompletableFuture<T> implem
801              super(null, dep, src); this.fn = fn;
802          }
803          final CompletableFuture<T> tryFire(int mode) { // never ASYNC
804 +            // assert mode != ASYNC;
805              CompletableFuture<T> d; CompletableFuture<T> a;
806              if ((d = dep) == null || !d.uniExceptionally(a = src, fn, this))
807                  return null;
# Line 848 | Line 891 | public class CompletableFuture<T> implem
891          Object r; Throwable x;
892          if (a == null || (r = a.result) == null || f == null)
893              return false;
894 <        if (result == null) {
895 <            exceptionalCompletion: try {
896 <                if (r instanceof AltResult)
897 <                    if ((x = ((AltResult)r).ex) != null)
898 <                        break exceptionalCompletion;
899 <                    else
900 <                        r = null;
894 >        tryComplete: if (result == null) {
895 >            if (r instanceof AltResult) {
896 >                if ((x = ((AltResult)r).ex) != null) {
897 >                    completeThrowable(x, r);
898 >                    break tryComplete;
899 >                }
900 >                r = null;
901 >            }
902 >            try {
903                  if (c != null && !c.claim())
904                      return false;
905                  @SuppressWarnings("unchecked") S s = (S) r;
# Line 866 | Line 911 | public class CompletableFuture<T> implem
911                      if (result == null)
912                          return false;
913                  }
869                return true;
914              } catch (Throwable ex) {
915 <                x = ex;
915 >                completeThrowable(ex);
916              }
873            completeThrowable(x);
917          }
918          return true;
919      }
# Line 879 | Line 922 | public class CompletableFuture<T> implem
922          Executor e, Function<? super T, ? extends CompletionStage<V>> f) {
923          if (f == null) throw new NullPointerException();
924          Object r; Throwable x;
925 <        if (e != null || (r = result) == null)
926 <            x = null;
927 <        else {                  // try to return function result directly
928 <            exceptionalCompletion: try {
929 <                if (r instanceof AltResult)
930 <                    if ((x = ((AltResult)r).ex) != null)
931 <                        break exceptionalCompletion;
932 <                    else
933 <                        r = null;
925 >        if (e == null && (r = result) != null) {
926 >            // try to return function result directly
927 >            if (r instanceof AltResult) {
928 >                if ((x = ((AltResult)r).ex) != null) {
929 >                    return new CompletableFuture<V>(encodeThrowable(x, r));
930 >                }
931 >                r = null;
932 >            }
933 >            try {
934                  @SuppressWarnings("unchecked") T t = (T) r;
935                  return f.apply(t).toCompletableFuture();
936              } catch (Throwable ex) {
937 <                x = ex;
937 >                return new CompletableFuture<V>(encodeThrowable(ex));
938              }
939          }
940          CompletableFuture<V> d = new CompletableFuture<V>();
941 <        if (x != null) {
942 <            d.result = encodeThrowable(x);
943 <        } else {
901 <            UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f);
902 <            push(c);
903 <            c.tryFire(SYNC);
904 <        }
941 >        UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f);
942 >        push(c);
943 >        c.tryFire(SYNC);
944          return d;
945      }
946  
# Line 989 | Line 1028 | public class CompletableFuture<T> implem
1028          if (a == null || (r = a.result) == null ||
1029              b == null || (s = b.result) == null || f == null)
1030              return false;
1031 <        if (result == null) {
1032 <            exceptionalCompletion: try {
1033 <                if (r instanceof AltResult)
1034 <                    if ((x = ((AltResult)r).ex) != null)
1035 <                        break exceptionalCompletion;
1036 <                    else
1037 <                        r = null;
1038 <                if (s instanceof AltResult)
1039 <                    if ((x = ((AltResult)s).ex) != null)
1040 <                        break exceptionalCompletion;
1041 <                    else
1042 <                        s = null;
1031 >        tryComplete: if (result == null) {
1032 >            if (r instanceof AltResult) {
1033 >                if ((x = ((AltResult)r).ex) != null) {
1034 >                    completeThrowable(x, r);
1035 >                    break tryComplete;
1036 >                }
1037 >                r = null;
1038 >            }
1039 >            if (s instanceof AltResult) {
1040 >                if ((x = ((AltResult)s).ex) != null) {
1041 >                    completeThrowable(x, s);
1042 >                    break tryComplete;
1043 >                }
1044 >                s = null;
1045 >            }
1046 >            try {
1047                  if (c != null && !c.claim())
1048                      return false;
1049                  @SuppressWarnings("unchecked") R rr = (R) r;
1050                  @SuppressWarnings("unchecked") S ss = (S) s;
1051                  completeValue(f.apply(rr, ss));
1009                return true;
1052              } catch (Throwable ex) {
1053 <                x = ex;
1053 >                completeThrowable(ex);
1054              }
1013            completeThrowable(x);
1055          }
1056          return true;
1057      }
# Line 1058 | Line 1099 | public class CompletableFuture<T> implem
1099          if (a == null || (r = a.result) == null ||
1100              b == null || (s = b.result) == null || f == null)
1101              return false;
1102 <        if (result == null) {
1103 <            exceptionalCompletion: try {
1104 <                if (r instanceof AltResult)
1105 <                    if ((x = ((AltResult)r).ex) != null)
1106 <                        break exceptionalCompletion;
1107 <                    else
1108 <                        r = null;
1109 <                if (s instanceof AltResult)
1110 <                    if ((x = ((AltResult)s).ex) != null)
1111 <                        break exceptionalCompletion;
1112 <                    else
1113 <                        s = null;
1102 >        tryComplete: if (result == null) {
1103 >            if (r instanceof AltResult) {
1104 >                if ((x = ((AltResult)r).ex) != null) {
1105 >                    completeThrowable(x, r);
1106 >                    break tryComplete;
1107 >                }
1108 >                r = null;
1109 >            }
1110 >            if (s instanceof AltResult) {
1111 >                if ((x = ((AltResult)s).ex) != null) {
1112 >                    completeThrowable(x, s);
1113 >                    break tryComplete;
1114 >                }
1115 >                s = null;
1116 >            }
1117 >            try {
1118                  if (c != null && !c.claim())
1119                      return false;
1120                  @SuppressWarnings("unchecked") R rr = (R) r;
1121                  @SuppressWarnings("unchecked") S ss = (S) s;
1122                  f.accept(rr, ss);
1123 <                completeNil();
1079 <                return true;
1123 >                completeNull();
1124              } catch (Throwable ex) {
1125 <                x = ex;
1125 >                completeThrowable(ex);
1126              }
1083            completeThrowable(x);
1127          }
1128          return true;
1129      }
# Line 1128 | Line 1171 | public class CompletableFuture<T> implem
1171              b == null || (s = b.result) == null || f == null)
1172              return false;
1173          if (result == null) {
1174 <            exceptionalCompletion: try {
1175 <                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null ||
1176 <                    s instanceof AltResult && (x = ((AltResult)s).ex) != null)
1177 <                    break exceptionalCompletion;
1178 <                if (c != null && !c.claim())
1179 <                    return false;
1180 <                f.run();
1181 <                completeNil();
1182 <                return true;
1183 <            } catch (Throwable ex) {
1184 <                x = ex;
1185 <            }
1186 <            completeThrowable(x);
1174 >            if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
1175 >                completeThrowable(x, r);
1176 >            else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null)
1177 >                completeThrowable(x, s);
1178 >            else
1179 >                try {
1180 >                    if (c != null && !c.claim())
1181 >                        return false;
1182 >                    f.run();
1183 >                    completeNull();
1184 >                } catch (Throwable ex) {
1185 >                    completeThrowable(ex);
1186 >                }
1187          }
1188          return true;
1189      }
# Line 1183 | Line 1226 | public class CompletableFuture<T> implem
1226              b == null || (s = b.result) == null)
1227              return false;
1228          if (result == null) {
1229 <            if (r instanceof AltResult && (x = ((AltResult)r).ex) != null ||
1230 <                s instanceof AltResult && (x = ((AltResult)s).ex) != null)
1231 <                completeThrowable(x);
1229 >            if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
1230 >                completeThrowable(x, r);
1231 >            else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null)
1232 >                completeThrowable(x, s);
1233              else
1234 <                completeNil();
1234 >                completeNull();
1235          }
1236          return true;
1237      }
# Line 1264 | Line 1308 | public class CompletableFuture<T> implem
1308          if (a == null || b == null ||
1309              ((r = a.result) == null && (r = b.result) == null) || f == null)
1310              return false;
1311 <        if (result == null) {
1312 <            exceptionalCompletion: try {
1311 >        tryComplete: if (result == null) {
1312 >            try {
1313                  if (c != null && !c.claim())
1314                      return false;
1315 <                if (r instanceof AltResult)
1316 <                    if ((x = ((AltResult)r).ex) != null)
1317 <                        break exceptionalCompletion;
1318 <                    else
1319 <                        r = null;
1315 >                if (r instanceof AltResult) {
1316 >                    if ((x = ((AltResult)r).ex) != null) {
1317 >                        completeThrowable(x, r);
1318 >                        break tryComplete;
1319 >                    }
1320 >                    r = null;
1321 >                }
1322                  @SuppressWarnings("unchecked") R rr = (R) r;
1323                  completeValue(f.apply(rr));
1278                return true;
1324              } catch (Throwable ex) {
1325 <                x = ex;
1325 >                completeThrowable(ex);
1326              }
1282            completeThrowable(x);
1327          }
1328          return true;
1329      }
# Line 1328 | Line 1372 | public class CompletableFuture<T> implem
1372          if (a == null || b == null ||
1373              ((r = a.result) == null && (r = b.result) == null) || f == null)
1374              return false;
1375 <        if (result == null) {
1376 <            exceptionalCompletion: try {
1375 >        tryComplete: if (result == null) {
1376 >            try {
1377                  if (c != null && !c.claim())
1378                      return false;
1379 <                if (r instanceof AltResult)
1380 <                    if ((x = ((AltResult)r).ex) != null)
1381 <                        break exceptionalCompletion;
1382 <                    else
1383 <                        r = null;
1379 >                if (r instanceof AltResult) {
1380 >                    if ((x = ((AltResult)r).ex) != null) {
1381 >                        completeThrowable(x, r);
1382 >                        break tryComplete;
1383 >                    }
1384 >                    r = null;
1385 >                }
1386                  @SuppressWarnings("unchecked") R rr = (R) r;
1387                  f.accept(rr);
1388 <                completeNil();
1343 <                return true;
1388 >                completeNull();
1389              } catch (Throwable ex) {
1390 <                x = ex;
1390 >                completeThrowable(ex);
1391              }
1347            completeThrowable(x);
1392          }
1393          return true;
1394      }
# Line 1391 | Line 1435 | public class CompletableFuture<T> implem
1435              ((r = a.result) == null && (r = b.result) == null) || f == null)
1436              return false;
1437          if (result == null) {
1438 <            exceptionalCompletion: try {
1438 >            try {
1439                  if (c != null && !c.claim())
1440                      return false;
1441                  if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
1442 <                    break exceptionalCompletion;
1443 <                f.run();
1444 <                completeNil();
1445 <                return true;
1442 >                    completeThrowable(x, r);
1443 >                else {
1444 >                    f.run();
1445 >                    completeNull();
1446 >                }
1447              } catch (Throwable ex) {
1448 <                x = ex;
1448 >                completeThrowable(ex);
1449              }
1405            completeThrowable(x);
1450          }
1451          return true;
1452      }
# Line 1479 | Line 1523 | public class CompletableFuture<T> implem
1523          }
1524  
1525          final CompletableFuture<T> tryFire(int alwaysAsync) {
1526 +            // assert alwaysAsync == ASYNC;
1527              CompletableFuture<T> d; Supplier<T> f;
1528              if ((d = dep) != null && (f = fn) != null) {
1529                  dep = null; fn = null;
# Line 1512 | Line 1557 | public class CompletableFuture<T> implem
1557          }
1558  
1559          final CompletableFuture<Void> tryFire(int alwaysAsync) {
1560 +            // assert alwaysAsync == ASYNC;
1561              CompletableFuture<Void> d; Runnable f;
1562              if ((d = dep) != null && (f = fn) != null) {
1563                  dep = null; fn = null;
1564                  if (d.result == null) {
1565                      try {
1566                          f.run();
1567 <                        d.completeNil();
1567 >                        d.completeNull();
1568                      } catch (Throwable ex) {
1569                          d.completeThrowable(ex);
1570                      }
# Line 1688 | Line 1734 | public class CompletableFuture<T> implem
1734      }
1735  
1736      /**
1737 +     * Creates a new complete CompletableFuture with given encoded result.
1738 +     */
1739 +    private CompletableFuture(Object r) {
1740 +        this.result = r;
1741 +    }
1742 +
1743 +    /**
1744       * Returns a new CompletableFuture that is asynchronously completed
1745       * by a task running in the {@link ForkJoinPool#commonPool()} with
1746       * the value obtained by calling the given Supplier.
# Line 1754 | Line 1807 | public class CompletableFuture<T> implem
1807       * @return the completed CompletableFuture
1808       */
1809      public static <U> CompletableFuture<U> completedFuture(U value) {
1810 <        CompletableFuture<U> d = new CompletableFuture<U>();
1758 <        d.result = (value == null) ? NIL : value;
1759 <        return d;
1810 >        return new CompletableFuture<U>((value == null) ? NIL : value);
1811      }
1812  
1813      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines