Skip to content

Commit

Permalink
Add @GoogleInternal propagateCancellation to Futures.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=169256636
  • Loading branch information
herbyderby authored and cpovirk committed Sep 19, 2017
1 parent bf61419 commit 52047f9
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected String pendingToString() {

@Override
protected final void afterDone() {
maybePropagateCancellation(inputFuture);
maybePropagateCancellationTo(inputFuture);
this.inputFuture = null;
this.exceptionType = null;
this.fallback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,8 @@ final Throwable trustedGetException() {
/**
* If this future has been cancelled (and possibly interrupted), cancels (and possibly interrupts)
* the given future (if available).
*
* <p>This method should be used only when this future is completed. It is designed to be called
* from {@code done}.
*/
final void maybePropagateCancellation(@Nullable Future<?> related) {
final void maybePropagateCancellationTo(@Nullable Future<?> related) {
if (related != null & isCancelled()) {
related.cancel(wasInterrupted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public final void run() {

@Override
protected final void afterDone() {
maybePropagateCancellation(inputFuture);
maybePropagateCancellationTo(inputFuture);
this.inputFuture = null;
this.function = null;
}
Expand Down
27 changes: 27 additions & 0 deletions android/guava/src/com/google/common/util/concurrent/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,33 @@ private static void wrapAndThrowUnchecked(Throwable cause) {
* Fork-Join framework: http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
*/

private static void maybePropagateCancellation(
@Nullable ListenableFuture<?> from, @Nullable Future<?> to) {
// AbstractFuture can propagate the mayInterruptIfRunning bit.
if (from instanceof AbstractFuture) {
((AbstractFuture<?>) from).maybePropagateCancellationTo(to);
} else if (from != null && from.isCancelled() && to != null) {
to.cancel(false);
}
}

private static final class CancellationPropagater implements Runnable {
ListenableFuture<?> from;
Future<?> to;

CancellationPropagater(ListenableFuture<?> from, Future<?> to) {
this.from = from;
this.to = to;
}

@Override
public void run() {
maybePropagateCancellation(from, to);
from = null;
to = null;
}
}

/**
* A checked future that uses a function to map from exceptions to the appropriate checked type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected String pendingToString() {

@Override
protected void afterDone() {
maybePropagateCancellation(delegateRef);
maybePropagateCancellationTo(delegateRef);

Future<?> localTimer = timer;
// Try to cancel the timer as an optimization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected boolean setFuture(ListenableFuture<? extends V> future) {

// If this future is already cancelled, cancel the delegate.
// TODO(cpovirk): Should we do this at the end of the method, as in the server version?
// TODO(cpovirk): Use maybePropagateCancellation?
// TODO(cpovirk): Use maybePropagateCancellationTo?
if (isCancelled()) {
future.cancel(mayInterruptIfRunning);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ final Throwable trustedGetException() {
return throwable;
}

final void maybePropagateCancellation(@Nullable Future<?> related) {
final void maybePropagateCancellationTo(@Nullable Future<?> related) {
if (related != null & isCancelled()) {
related.cancel(wasInterrupted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected String pendingToString() {

@Override
protected final void afterDone() {
maybePropagateCancellation(inputFuture);
maybePropagateCancellationTo(inputFuture);
this.inputFuture = null;
this.exceptionType = null;
this.fallback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,8 @@ final Throwable trustedGetException() {
/**
* If this future has been cancelled (and possibly interrupted), cancels (and possibly interrupts)
* the given future (if available).
*
* <p>This method should be used only when this future is completed. It is designed to be called
* from {@code done}.
*/
final void maybePropagateCancellation(@Nullable Future<?> related) {
final void maybePropagateCancellationTo(@Nullable Future<?> related) {
if (related != null & isCancelled()) {
related.cancel(wasInterrupted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public final void run() {

@Override
protected final void afterDone() {
maybePropagateCancellation(inputFuture);
maybePropagateCancellationTo(inputFuture);
this.inputFuture = null;
this.function = null;
}
Expand Down
27 changes: 27 additions & 0 deletions guava/src/com/google/common/util/concurrent/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,33 @@ private static void wrapAndThrowUnchecked(Throwable cause) {
* Fork-Join framework: http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
*/

private static void maybePropagateCancellation(
@Nullable ListenableFuture<?> from, @Nullable Future<?> to) {
// AbstractFuture can propagate the mayInterruptIfRunning bit.
if (from instanceof AbstractFuture) {
((AbstractFuture<?>) from).maybePropagateCancellationTo(to);
} else if (from != null && from.isCancelled() && to != null) {
to.cancel(false);
}
}

private static final class CancellationPropagater implements Runnable {
ListenableFuture<?> from;
Future<?> to;

CancellationPropagater(ListenableFuture<?> from, Future<?> to) {
this.from = from;
this.to = to;
}

@Override
public void run() {
maybePropagateCancellation(from, to);
from = null;
to = null;
}
}

/**
* A checked future that uses a function to map from exceptions to the appropriate checked type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected String pendingToString() {

@Override
protected void afterDone() {
maybePropagateCancellation(delegateRef);
maybePropagateCancellationTo(delegateRef);

Future<?> localTimer = timer;
// Try to cancel the timer as an optimization.
Expand Down

0 comments on commit 52047f9

Please sign in to comment.