Skip to content

Commit

Permalink
Move tsan suppressions to annotations
Browse files Browse the repository at this point in the history
RELNOTES=Improved j2objc compatibility
PiperOrigin-RevId: 603470054
  • Loading branch information
java-team-github-bot authored and Google Java Core Libraries committed Feb 1, 2024
1 parent a9d243c commit d3232b7
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.util.concurrent.internal.InternalFutureFailureAccess;
import com.google.common.util.concurrent.internal.InternalFutures;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -62,9 +63,9 @@ abstract class AbstractCatchingFuture<
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull ListenableFuture<? extends V> inputFuture;
@CheckForNull Class<X> exceptionType;
@CheckForNull F fallback;
@CheckForNull @LazyInit ListenableFuture<? extends V> inputFuture;
@CheckForNull @LazyInit Class<X> exceptionType;
@CheckForNull @LazyInit F fallback;

AbstractCatchingFuture(
ListenableFuture<? extends V> inputFuture, Class<X> exceptionType, F fallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -57,8 +58,8 @@ abstract class AbstractTransformFuture<
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull ListenableFuture<? extends I> inputFuture;
@CheckForNull F function;
@CheckForNull @LazyInit ListenableFuture<? extends I> inputFuture;
@CheckForNull @LazyInit F function;

AbstractTransformFuture(ListenableFuture<? extends I> inputFuture, F function) {
this.inputFuture = checkNotNull(inputFuture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableCollection;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -55,7 +56,8 @@ abstract class AggregateFuture<InputT extends @Nullable Object, OutputT extends
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull private ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures;
@CheckForNull @LazyInit
private ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures;

private final boolean allMustSucceed;
private final boolean collectsValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
Expand All @@ -36,7 +37,7 @@ abstract class CollectionFuture<V extends @Nullable Object, C extends @Nullable
* there: cancel() never reads this field, only writes to it. That makes the race here completely
* harmless, rather than just 99.99% harmless.
*/
@CheckForNull private List<@Nullable Present<V>> values;
@CheckForNull @LazyInit private List<@Nullable Present<V>> values;

CollectionFuture(
ImmutableCollection<? extends ListenableFuture<? extends V>> futures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableCollection;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.WeakOuter;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
Expand All @@ -33,7 +34,7 @@
@ElementTypesAreNonnullByDefault
final class CombinedFuture<V extends @Nullable Object>
extends AggregateFuture<@Nullable Object, V> {
@CheckForNull private CombinedFutureInterruptibleTask<?> task;
@CheckForNull @LazyInit private CombinedFutureInterruptibleTask<?> task;

CombinedFuture(
ImmutableCollection<? extends ListenableFuture<?>> futures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -100,7 +100,7 @@ public static ExecutionSequencer create() {
private final AtomicReference<ListenableFuture<@Nullable Void>> ref =
new AtomicReference<>(immediateVoidFuture());

private ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();
private @LazyInit ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();

/**
* This object is unsafely published, but avoids problematic races by relying exclusively on the
Expand Down Expand Up @@ -131,9 +131,11 @@ private static final class ThreadConfinedTaskQueue {
* All the states where thread != currentThread are identical for our purposes, and so even
* though it's racy, we don't care which of those values we get, so no need to synchronize.
*/
@CheckForNull Thread thread;
@CheckForNull @LazyInit Thread thread;

/** Only used by the thread associated with this object */
@CheckForNull Runnable nextTask;

/** Only used by the thread associated with this object */
@CheckForNull Executor nextExecutor;
}
Expand Down Expand Up @@ -308,7 +310,7 @@ private static final class TaskNonReentrantExecutor extends AtomicReference<Runn
@CheckForNull Runnable task;

/** Thread that called execute(). Set in execute, cleared when delegate.execute() returns. */
@CheckForNull Thread submitting;
@CheckForNull @LazyInit Thread submitting;

private TaskNonReentrantExecutor(Executor delegate, ExecutionSequencer sequencer) {
super(NOT_RUN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.common.util.concurrent.internal.InternalFutureFailureAccess;
import com.google.common.util.concurrent.internal.InternalFutures;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -760,7 +761,7 @@ public Void call() throws Exception {
/** A wrapped future that does not propagate cancellation to its delegate. */
private static final class NonCancellationPropagatingFuture<V extends @Nullable Object>
extends AbstractFuture.TrustedFuture<V> implements Runnable {
@CheckForNull private ListenableFuture<V> delegate;
@CheckForNull @LazyInit private ListenableFuture<V> delegate;

NonCancellationPropagatingFuture(final ListenableFuture<V> delegate) {
this.delegate = delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -70,6 +71,7 @@ enum WorkerRunningState {
private final Deque<Runnable> queue = new ArrayDeque<>();

/** see {@link WorkerRunningState} */
@LazyInit
@GuardedBy("queue")
private WorkerRunningState workerRunningState = IDLE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -75,16 +76,16 @@ final class TimeoutFuture<V extends @Nullable Object> extends FluentFuture.Trust
* write-barriers).
*/

@CheckForNull private ListenableFuture<V> delegateRef;
@CheckForNull private ScheduledFuture<?> timer;
@CheckForNull @LazyInit private ListenableFuture<V> delegateRef;
@CheckForNull @LazyInit private ScheduledFuture<?> timer;

private TimeoutFuture(ListenableFuture<V> delegate) {
this.delegateRef = Preconditions.checkNotNull(delegate);
}

/** A runnable that is called when the delegate or the timer completes. */
private static final class Fire<V extends @Nullable Object> implements Runnable {
@CheckForNull TimeoutFuture<V> timeoutFutureRef;
@CheckForNull @LazyInit TimeoutFuture<V> timeoutFutureRef;

Fire(TimeoutFuture<V> timeoutFuture) {
this.timeoutFutureRef = timeoutFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.util.concurrent.internal.InternalFutureFailureAccess;
import com.google.common.util.concurrent.internal.InternalFutures;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -62,9 +63,9 @@ abstract class AbstractCatchingFuture<
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull ListenableFuture<? extends V> inputFuture;
@CheckForNull Class<X> exceptionType;
@CheckForNull F fallback;
@CheckForNull @LazyInit ListenableFuture<? extends V> inputFuture;
@CheckForNull @LazyInit Class<X> exceptionType;
@CheckForNull @LazyInit F fallback;

AbstractCatchingFuture(
ListenableFuture<? extends V> inputFuture, Class<X> exceptionType, F fallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -57,8 +58,8 @@ abstract class AbstractTransformFuture<
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull ListenableFuture<? extends I> inputFuture;
@CheckForNull F function;
@CheckForNull @LazyInit ListenableFuture<? extends I> inputFuture;
@CheckForNull @LazyInit F function;

AbstractTransformFuture(ListenableFuture<? extends I> inputFuture, F function) {
this.inputFuture = checkNotNull(inputFuture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableCollection;
import com.google.errorprone.annotations.ForOverride;
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -55,7 +56,8 @@ abstract class AggregateFuture<InputT extends @Nullable Object, OutputT extends
* In certain circumstances, this field might theoretically not be visible to an afterDone() call
* triggered by cancel(). For details, see the comments on the fields of TimeoutFuture.
*/
@CheckForNull private ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures;
@CheckForNull @LazyInit
private ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures;

private final boolean allMustSucceed;
private final boolean collectsValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
Expand All @@ -36,7 +37,7 @@ abstract class CollectionFuture<V extends @Nullable Object, C extends @Nullable
* there: cancel() never reads this field, only writes to it. That makes the race here completely
* harmless, rather than just 99.99% harmless.
*/
@CheckForNull private List<@Nullable Present<V>> values;
@CheckForNull @LazyInit private List<@Nullable Present<V>> values;

CollectionFuture(
ImmutableCollection<? extends ListenableFuture<? extends V>> futures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableCollection;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.WeakOuter;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
Expand All @@ -33,7 +34,7 @@
@ElementTypesAreNonnullByDefault
final class CombinedFuture<V extends @Nullable Object>
extends AggregateFuture<@Nullable Object, V> {
@CheckForNull private CombinedFutureInterruptibleTask<?> task;
@CheckForNull @LazyInit private CombinedFutureInterruptibleTask<?> task;

CombinedFuture(
ImmutableCollection<? extends ListenableFuture<?>> futures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -100,7 +100,7 @@ public static ExecutionSequencer create() {
private final AtomicReference<ListenableFuture<@Nullable Void>> ref =
new AtomicReference<>(immediateVoidFuture());

private ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();
private @LazyInit ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();

/**
* This object is unsafely published, but avoids problematic races by relying exclusively on the
Expand Down Expand Up @@ -131,9 +131,11 @@ private static final class ThreadConfinedTaskQueue {
* All the states where thread != currentThread are identical for our purposes, and so even
* though it's racy, we don't care which of those values we get, so no need to synchronize.
*/
@CheckForNull Thread thread;
@CheckForNull @LazyInit Thread thread;

/** Only used by the thread associated with this object */
@CheckForNull Runnable nextTask;

/** Only used by the thread associated with this object */
@CheckForNull Executor nextExecutor;
}
Expand Down Expand Up @@ -308,7 +310,7 @@ private static final class TaskNonReentrantExecutor extends AtomicReference<Runn
@CheckForNull Runnable task;

/** Thread that called execute(). Set in execute, cleared when delegate.execute() returns. */
@CheckForNull Thread submitting;
@CheckForNull @LazyInit Thread submitting;

private TaskNonReentrantExecutor(Executor delegate, ExecutionSequencer sequencer) {
super(NOT_RUN);
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/util/concurrent/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.common.util.concurrent.internal.InternalFutureFailureAccess;
import com.google.common.util.concurrent.internal.InternalFutures;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -795,7 +796,7 @@ public Void call() throws Exception {
/** A wrapped future that does not propagate cancellation to its delegate. */
private static final class NonCancellationPropagatingFuture<V extends @Nullable Object>
extends AbstractFuture.TrustedFuture<V> implements Runnable {
@CheckForNull private ListenableFuture<V> delegate;
@CheckForNull @LazyInit private ListenableFuture<V> delegate;

NonCancellationPropagatingFuture(final ListenableFuture<V> delegate) {
this.delegate = delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -70,6 +71,7 @@ enum WorkerRunningState {
private final Deque<Runnable> queue = new ArrayDeque<>();

/** see {@link WorkerRunningState} */
@LazyInit
@GuardedBy("queue")
private WorkerRunningState workerRunningState = IDLE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -75,16 +76,16 @@ final class TimeoutFuture<V extends @Nullable Object> extends FluentFuture.Trust
* write-barriers).
*/

@CheckForNull private ListenableFuture<V> delegateRef;
@CheckForNull private ScheduledFuture<?> timer;
@CheckForNull @LazyInit private ListenableFuture<V> delegateRef;
@CheckForNull @LazyInit private ScheduledFuture<?> timer;

private TimeoutFuture(ListenableFuture<V> delegate) {
this.delegateRef = Preconditions.checkNotNull(delegate);
}

/** A runnable that is called when the delegate or the timer completes. */
private static final class Fire<V extends @Nullable Object> implements Runnable {
@CheckForNull TimeoutFuture<V> timeoutFutureRef;
@CheckForNull @LazyInit TimeoutFuture<V> timeoutFutureRef;

Fire(TimeoutFuture<V> timeoutFuture) {
this.timeoutFutureRef = timeoutFuture;
Expand Down

0 comments on commit d3232b7

Please sign in to comment.