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

Comparing jsr166/src/jsr166e/CompletableFuture.java (file contents):
Revision 1.1 by dl, Wed Jan 2 14:13:12 2013 UTC vs.
Revision 1.11 by jsr166, Wed Feb 6 07:51:57 2013 UTC

# Line 83 | Line 83 | public class CompletableFuture<T> implem
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)
# Line 287 | Line 287 | public class CompletableFuture<T> implem
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              }
# Line 320 | Line 320 | public class CompletableFuture<T> implem
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,
# Line 338 | Line 338 | public class CompletableFuture<T> implem
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              }
# Line 388 | Line 388 | public class CompletableFuture<T> implem
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) { }
# Line 403 | Line 403 | public class CompletableFuture<T> implem
403          }
404          public final boolean exec() {
405              CompletableFuture<Void> d; Throwable ex;
406 <            if ((d = this.dst) != null) {
406 >            if ((d = this.dst) != null && d.result == null) {
407                  try {
408                      fn.run();
409                      ex = null;
# Line 425 | Line 425 | public class CompletableFuture<T> implem
425          }
426          public final boolean exec() {
427              CompletableFuture<U> d; U u; Throwable ex;
428 <            if ((d = this.dst) != null) {
428 >            if ((d = this.dst) != null && d.result == null) {
429                  try {
430                      u = fn.get();
431                      ex = null;
# Line 450 | Line 450 | public class CompletableFuture<T> implem
450          }
451          public final boolean exec() {
452              CompletableFuture<U> d; U u; Throwable ex;
453 <            if ((d = this.dst) != null) {
453 >            if ((d = this.dst) != null && d.result == null) {
454                  try {
455                      u = fn.apply(arg);
456                      ex = null;
# Line 477 | Line 477 | public class CompletableFuture<T> implem
477          }
478          public final boolean exec() {
479              CompletableFuture<V> d; V v; Throwable ex;
480 <            if ((d = this.dst) != null) {
480 >            if ((d = this.dst) != null && d.result == null) {
481                  try {
482                      v = fn.apply(arg1, arg2);
483                      ex = null;
# Line 502 | Line 502 | public class CompletableFuture<T> implem
502          }
503          public final boolean exec() {
504              CompletableFuture<Void> d; Throwable ex;
505 <            if ((d = this.dst) != null) {
505 >            if ((d = this.dst) != null && d.result == null) {
506                  try {
507                      fn.accept(arg);
508                      ex = null;
# Line 528 | Line 528 | public class CompletableFuture<T> implem
528          }
529          public final boolean exec() {
530              CompletableFuture<Void> d; Throwable ex;
531 <            if ((d = this.dst) != null) {
531 >            if ((d = this.dst) != null && d.result == null) {
532                  try {
533                      fn.accept(arg1, arg2);
534                      ex = null;
# Line 557 | Line 557 | public class CompletableFuture<T> implem
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 {
# Line 2449 | Line 2449 | public class CompletableFuture<T> implem
2449       * then the returned CompletableFuture also does so, with a
2450       * CompletionException holding this exception as its cause.
2451       *
2452 <     * @param fn the function returning a new CompletableFuture.
2452 >     * @param fn the function returning a new CompletableFuture
2453       * @return the CompletableFuture, that {@code isDone()} upon
2454       * return if completed by the given function, or an exception
2455 <     * occurs.
2455 >     * occurs
2456       */
2457      public <U> CompletableFuture<U> thenCompose(Fun<? super T,
2458                                                  CompletableFuture<U>> fn) {
# Line 2633 | Line 2633 | public class CompletableFuture<T> implem
2633       */
2634      public boolean isCancelled() {
2635          Object r;
2636 <        return ((r = result) != null &&
2637 <                (r instanceof AltResult) &&
2638 <                (((AltResult)r).ex instanceof CancellationException));
2636 >        return ((r = result) instanceof AltResult) &&
2637 >            (((AltResult)r).ex instanceof CancellationException);
2638      }
2639  
2640      /**
2641       * Forcibly sets or resets the value subsequently returned by
2642 <     * method get() and related methods, whether or not already
2643 <     * completed. This method is designed for use only in error
2644 <     * recovery actions, and even in such situations may result in
2645 <     * ongoing dependent completions using established versus
2646 <     * overwritten values.
2642 >     * method {@link #get()} and related methods, whether or not
2643 >     * already completed. This method is designed for use only in
2644 >     * error recovery actions, and even in such situations may result
2645 >     * in ongoing dependent completions using established versus
2646 >     * overwritten outcomes.
2647       *
2648       * @param value the completion value
2649       */
# Line 2653 | Line 2652 | public class CompletableFuture<T> implem
2652          postComplete();
2653      }
2654  
2655 +    /**
2656 +     * Forcibly causes subsequent invocations of method {@link #get()}
2657 +     * and related methods to throw the given exception, whether or
2658 +     * not already completed. This method is designed for use only in
2659 +     * recovery actions, and even in such situations may result in
2660 +     * ongoing dependent completions using established versus
2661 +     * overwritten outcomes.
2662 +     *
2663 +     * @param ex the exception
2664 +     */
2665 +    public void obtrudeException(Throwable ex) {
2666 +        if (ex == null) throw new NullPointerException();
2667 +        result = new AltResult(ex);
2668 +        postComplete();
2669 +    }
2670 +
2671      // Unsafe mechanics
2672      private static final sun.misc.Unsafe UNSAFE;
2673      private static final long RESULT;
# Line 2684 | Line 2699 | public class CompletableFuture<T> implem
2699      private static sun.misc.Unsafe getUnsafe() {
2700          try {
2701              return sun.misc.Unsafe.getUnsafe();
2702 <        } catch (SecurityException se) {
2703 <            try {
2704 <                return java.security.AccessController.doPrivileged
2705 <                    (new java.security
2706 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
2707 <                        public sun.misc.Unsafe run() throws Exception {
2708 <                            java.lang.reflect.Field f = sun.misc
2709 <                                .Unsafe.class.getDeclaredField("theUnsafe");
2710 <                            f.setAccessible(true);
2711 <                            return (sun.misc.Unsafe) f.get(null);
2712 <                        }});
2713 <            } catch (java.security.PrivilegedActionException e) {
2714 <                throw new RuntimeException("Could not initialize intrinsics",
2715 <                                           e.getCause());
2716 <            }
2702 >        } catch (SecurityException tryReflectionInstead) {}
2703 >        try {
2704 >            return java.security.AccessController.doPrivileged
2705 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
2706 >                public sun.misc.Unsafe run() throws Exception {
2707 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
2708 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
2709 >                        f.setAccessible(true);
2710 >                        Object x = f.get(null);
2711 >                        if (k.isInstance(x))
2712 >                            return k.cast(x);
2713 >                    }
2714 >                    throw new NoSuchFieldError("the Unsafe");
2715 >                }});
2716 >        } catch (java.security.PrivilegedActionException e) {
2717 >            throw new RuntimeException("Could not initialize intrinsics",
2718 >                                       e.getCause());
2719          }
2720      }
2704
2721   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines