001/*
002 * Copyright (C) 2018 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static com.google.common.base.Preconditions.checkState;
019import static com.google.common.util.concurrent.ExecutionSequencer.RunningState.CANCELLED;
020import static com.google.common.util.concurrent.ExecutionSequencer.RunningState.NOT_RUN;
021import static com.google.common.util.concurrent.ExecutionSequencer.RunningState.STARTED;
022import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
023import static com.google.common.util.concurrent.Futures.immediateFuture;
024import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
025import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
026import static java.util.Objects.requireNonNull;
027
028import com.google.common.annotations.J2ktIncompatible;
029import java.util.concurrent.Callable;
030import java.util.concurrent.Executor;
031import java.util.concurrent.Future;
032import java.util.concurrent.atomic.AtomicReference;
033import javax.annotation.CheckForNull;
034import org.checkerframework.checker.nullness.qual.Nullable;
035
036/**
037 * Serializes execution of tasks, somewhat like an "asynchronous {@code synchronized} block." Each
038 * {@linkplain #submit enqueued} callable will not be submitted to its associated executor until the
039 * previous callable has returned -- and, if the previous callable was an {@link AsyncCallable}, not
040 * until the {@code Future} it returned is {@linkplain Future#isDone done} (successful, failed, or
041 * cancelled).
042 *
043 * <p>This class serializes execution of <i>submitted</i> tasks but not any <i>listeners</i> of
044 * those tasks.
045 *
046 * <p>Submitted tasks have a happens-before order as defined in the Java Language Specification.
047 * Tasks execute with the same happens-before order that the function calls to {@link #submit} and
048 * {@link #submitAsync} that submitted those tasks had.
049 *
050 * <p>This class has limited support for cancellation and other "early completions":
051 *
052 * <ul>
053 *   <li>While calls to {@code submit} and {@code submitAsync} return a {@code Future} that can be
054 *       cancelled, cancellation never propagates to a task that has started to run -- neither to
055 *       the callable itself nor to any {@code Future} returned by an {@code AsyncCallable}.
056 *       (However, cancellation can prevent an <i>unstarted</i> task from running.) Therefore, the
057 *       next task will wait for any running callable (or pending {@code Future} returned by an
058 *       {@code AsyncCallable}) to complete, without interrupting it (and without calling {@code
059 *       cancel} on the {@code Future}). So beware: <i>Even if you cancel every preceding {@code
060 *       Future} returned by this class, the next task may still have to wait.</i>.
061 *   <li>Once an {@code AsyncCallable} returns a {@code Future}, this class considers that task to
062 *       be "done" as soon as <i>that</i> {@code Future} completes in any way. Notably, a {@code
063 *       Future} is "completed" even if it is cancelled while its underlying work continues on a
064 *       thread, an RPC, etc. The {@code Future} is also "completed" if it fails "early" -- for
065 *       example, if the deadline expires on a {@code Future} returned from {@link
066 *       Futures#withTimeout} while the {@code Future} it wraps continues its underlying work. So
067 *       beware: <i>Your {@code AsyncCallable} should not complete its {@code Future} until it is
068 *       safe for the next task to start.</i>
069 * </ul>
070 *
071 * <p>This class is similar to {@link MoreExecutors#newSequentialExecutor}. This class is different
072 * in a few ways:
073 *
074 * <ul>
075 *   <li>Each task may be associated with a different executor.
076 *   <li>Tasks may be of type {@code AsyncCallable}.
077 *   <li>Running tasks <i>cannot</i> be interrupted. (Note that {@code newSequentialExecutor} does
078 *       not return {@code Future} objects, so it doesn't support interruption directly, either.
079 *       However, utilities that <i>use</i> that executor have the ability to interrupt tasks
080 *       running on it. This class, by contrast, does not expose an {@code Executor} API.)
081 * </ul>
082 *
083 * <p>If you don't need the features of this class, you may prefer {@code newSequentialExecutor} for
084 * its simplicity and ability to accommodate interruption.
085 *
086 * @since 26.0
087 */
088@ElementTypesAreNonnullByDefault
089@J2ktIncompatible
090public final class ExecutionSequencer {
091
092  private ExecutionSequencer() {}
093
094  /** Creates a new instance. */
095  public static ExecutionSequencer create() {
096    return new ExecutionSequencer();
097  }
098
099  /** This reference acts as a pointer tracking the head of a linked list of ListenableFutures. */
100  private final AtomicReference<ListenableFuture<@Nullable Void>> ref =
101      new AtomicReference<>(immediateVoidFuture());
102
103  private ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();
104
105  /**
106   * This object is unsafely published, but avoids problematic races by relying exclusively on the
107   * identity equality of its Thread field so that the task field is only accessed by a single
108   * thread.
109   */
110  private static final class ThreadConfinedTaskQueue {
111    /**
112     * This field is only used for identity comparisons with the current thread. Field assignments
113     * are atomic, but do not provide happens-before ordering; however:
114     *
115     * <ul>
116     *   <li>If this field's value == currentThread, we know that it's up to date, because write
117     *       operations in a thread always happen-before subsequent read operations in the same
118     *       thread
119     *   <li>If this field's value == null because of unsafe publication, we know that it isn't the
120     *       object associated with our thread, because if it was the publication wouldn't have been
121     *       unsafe and we'd have seen our thread as the value. This state is also why a new
122     *       ThreadConfinedTaskQueue object must be created for each inline execution, because
123     *       observing a null thread does not mean the object is safe to reuse.
124     *   <li>If this field's value is some other thread object, we know that it's not our thread.
125     *   <li>If this field's value == null because it originally belonged to another thread and that
126     *       thread cleared it, we still know that it's not associated with our thread
127     *   <li>If this field's value == null because it was associated with our thread and was
128     *       cleared, we know that we're not executing inline any more
129     * </ul>
130     *
131     * All the states where thread != currentThread are identical for our purposes, and so even
132     * though it's racy, we don't care which of those values we get, so no need to synchronize.
133     */
134    @CheckForNull Thread thread;
135    /** Only used by the thread associated with this object */
136    @CheckForNull Runnable nextTask;
137    /** Only used by the thread associated with this object */
138    @CheckForNull Executor nextExecutor;
139  }
140
141  /**
142   * Enqueues a task to run when the previous task (if any) completes.
143   *
144   * <p>Cancellation does not propagate from the output future to a callable that has begun to
145   * execute, but if the output future is cancelled before {@link Callable#call()} is invoked,
146   * {@link Callable#call()} will not be invoked.
147   */
148  public <T extends @Nullable Object> ListenableFuture<T> submit(
149      Callable<T> callable, Executor executor) {
150    checkNotNull(callable);
151    checkNotNull(executor);
152    return submitAsync(
153        new AsyncCallable<T>() {
154          @Override
155          public ListenableFuture<T> call() throws Exception {
156            return immediateFuture(callable.call());
157          }
158
159          @Override
160          public String toString() {
161            return callable.toString();
162          }
163        },
164        executor);
165  }
166
167  /**
168   * Enqueues a task to run when the previous task (if any) completes.
169   *
170   * <p>Cancellation does not propagate from the output future to the future returned from {@code
171   * callable} or a callable that has begun to execute, but if the output future is cancelled before
172   * {@link AsyncCallable#call()} is invoked, {@link AsyncCallable#call()} will not be invoked.
173   */
174  public <T extends @Nullable Object> ListenableFuture<T> submitAsync(
175      AsyncCallable<T> callable, Executor executor) {
176    checkNotNull(callable);
177    checkNotNull(executor);
178    TaskNonReentrantExecutor taskExecutor = new TaskNonReentrantExecutor(executor, this);
179    AsyncCallable<T> task =
180        new AsyncCallable<T>() {
181          @Override
182          public ListenableFuture<T> call() throws Exception {
183            if (!taskExecutor.trySetStarted()) {
184              return immediateCancelledFuture();
185            }
186            return callable.call();
187          }
188
189          @Override
190          public String toString() {
191            return callable.toString();
192          }
193        };
194    /*
195     * Four futures are at play here:
196     * taskFuture is the future tracking the result of the callable.
197     * newFuture is a future that completes after this and all prior tasks are done.
198     * oldFuture is the previous task's newFuture.
199     * outputFuture is the future we return to the caller, a nonCancellationPropagating taskFuture.
200     *
201     * newFuture is guaranteed to only complete once all tasks previously submitted to this instance
202     * have completed - namely after oldFuture is done, and taskFuture has either completed or been
203     * cancelled before the callable started execution.
204     */
205    SettableFuture<@Nullable Void> newFuture = SettableFuture.create();
206
207    ListenableFuture<@Nullable Void> oldFuture = ref.getAndSet(newFuture);
208
209    // Invoke our task once the previous future completes.
210    TrustedListenableFutureTask<T> taskFuture = TrustedListenableFutureTask.create(task);
211    oldFuture.addListener(taskFuture, taskExecutor);
212
213    ListenableFuture<T> outputFuture = Futures.nonCancellationPropagating(taskFuture);
214
215    // newFuture's lifetime is determined by taskFuture, which can't complete before oldFuture
216    // unless taskFuture is cancelled, in which case it falls back to oldFuture. This ensures that
217    // if the future we return is cancelled, we don't begin execution of the next task until after
218    // oldFuture completes.
219    Runnable listener =
220        () -> {
221          if (taskFuture.isDone()) {
222            // Since the value of oldFuture can only ever be immediateFuture(null) or setFuture of
223            // a future that eventually came from immediateFuture(null), this doesn't leak
224            // throwables or completion values.
225            newFuture.setFuture(oldFuture);
226          } else if (outputFuture.isCancelled() && taskExecutor.trySetCancelled()) {
227            // If this CAS succeeds, we know that the provided callable will never be invoked,
228            // so when oldFuture completes it is safe to allow the next submitted task to
229            // proceed. Doing this immediately here lets the next task run without waiting for
230            // the cancelled task's executor to run the noop AsyncCallable.
231            //
232            // ---
233            //
234            // If the CAS fails, the provided callable already started running (or it is about
235            // to). Our contract promises:
236            //
237            // 1. not to execute a new callable until the old one has returned
238            //
239            // If we were to cancel taskFuture, that would let the next task start while the old
240            // one is still running.
241            //
242            // Now, maybe we could tweak our implementation to not start the next task until the
243            // callable actually completes. (We could detect completion in our wrapper
244            // `AsyncCallable task`.) However, our contract also promises:
245            //
246            // 2. not to cancel any Future the user returned from an AsyncCallable
247            //
248            // We promise this because, once we cancel that Future, we would no longer be able to
249            // tell when any underlying work it is doing is done. Thus, we might start a new task
250            // while that underlying work is still running.
251            //
252            // So that is why we cancel only in the case of CAS success.
253            taskFuture.cancel(false);
254          }
255        };
256    // Adding the listener to both futures guarantees that newFuture will always be set. Adding to
257    // taskFuture guarantees completion if the callable is invoked, and adding to outputFuture
258    // propagates cancellation if the callable has not yet been invoked.
259    outputFuture.addListener(listener, directExecutor());
260    taskFuture.addListener(listener, directExecutor());
261
262    return outputFuture;
263  }
264
265  enum RunningState {
266    NOT_RUN,
267    CANCELLED,
268    STARTED,
269  }
270
271  /**
272   * This class helps avoid a StackOverflowError when large numbers of tasks are submitted with
273   * {@link MoreExecutors#directExecutor}. Normally, when the first future completes, all the other
274   * tasks would be called recursively. Here, we detect that the delegate executor is executing
275   * inline, and maintain a queue to dispatch tasks iteratively. There is one instance of this class
276   * per call to submit() or submitAsync(), and each instance supports only one call to execute().
277   *
278   * <p>This class would certainly be simpler and easier to reason about if it were built with
279   * ThreadLocal; however, ThreadLocal is not well optimized for the case where the ThreadLocal is
280   * non-static, and is initialized/removed frequently - this causes churn in the Thread specific
281   * hashmaps. Using a static ThreadLocal to avoid that overhead would mean that different
282   * ExecutionSequencer objects interfere with each other, which would be undesirable, in addition
283   * to increasing the memory footprint of every thread that interacted with it. In order to release
284   * entries in thread-specific maps when the ThreadLocal object itself is no longer referenced,
285   * ThreadLocal is usually implemented with a WeakReference, which can have negative performance
286   * properties; for example, calling WeakReference.get() on Android will block during an
287   * otherwise-concurrent GC cycle.
288   */
289  private static final class TaskNonReentrantExecutor extends AtomicReference<RunningState>
290      implements Executor, Runnable {
291
292    /**
293     * Used to update and read the latestTaskQueue field. Set to null once the runnable has been run
294     * or queued.
295     */
296    @CheckForNull ExecutionSequencer sequencer;
297
298    /**
299     * Executor the task was set to run on. Set to null when the task has been queued, run, or
300     * cancelled.
301     */
302    @CheckForNull Executor delegate;
303
304    /**
305     * Set before calling delegate.execute(); set to null once run, so that it can be GCed; this
306     * object may live on after, if submitAsync returns an incomplete future.
307     */
308    @CheckForNull Runnable task;
309
310    /** Thread that called execute(). Set in execute, cleared when delegate.execute() returns. */
311    @CheckForNull Thread submitting;
312
313    private TaskNonReentrantExecutor(Executor delegate, ExecutionSequencer sequencer) {
314      super(NOT_RUN);
315      this.delegate = delegate;
316      this.sequencer = sequencer;
317    }
318
319    @Override
320    public void execute(Runnable task) {
321      // If this operation was successfully cancelled already, calling the runnable will be a noop.
322      // This also avoids a race where if outputFuture is cancelled, it will call taskFuture.cancel,
323      // which will call newFuture.setFuture(oldFuture), to allow the next task in the queue to run
324      // without waiting for the user's executor to run our submitted Runnable. However, this can
325      // interact poorly with the reentrancy-avoiding behavior of this executor - when the operation
326      // before the cancelled future completes, it will synchronously complete both the newFuture
327      // from the cancelled operation and its own. This can cause one runnable to queue two tasks,
328      // breaking the invariant this method relies on to iteratively run the next task after the
329      // previous one completes.
330      if (get() == RunningState.CANCELLED) {
331        delegate = null;
332        sequencer = null;
333        return;
334      }
335      submitting = Thread.currentThread();
336
337      try {
338        /*
339         * requireNonNull is safe because we don't null out `sequencer` except:
340         *
341         * - above, where we return (in which case we never get here)
342         *
343         * - in `run`, which can't run until this Runnable is submitted to an executor, which
344         *   doesn't happen until below. (And this Executor -- yes, the object is both a Runnable
345         *   and an Executor -- is used for only a single `execute` call.)
346         */
347        ThreadConfinedTaskQueue submittingTaskQueue = requireNonNull(sequencer).latestTaskQueue;
348        if (submittingTaskQueue.thread == submitting) {
349          sequencer = null;
350          // Submit from inside a reentrant submit. We don't know if this one will be reentrant (and
351          // can't know without submitting something to the executor) so queue to run iteratively.
352          // Task must be null, since each execution on this executor can only produce one more
353          // execution.
354          checkState(submittingTaskQueue.nextTask == null);
355          submittingTaskQueue.nextTask = task;
356          // requireNonNull(delegate) is safe for reasons similar to requireNonNull(sequencer).
357          submittingTaskQueue.nextExecutor = requireNonNull(delegate);
358          delegate = null;
359        } else {
360          // requireNonNull(delegate) is safe for reasons similar to requireNonNull(sequencer).
361          Executor localDelegate = requireNonNull(delegate);
362          delegate = null;
363          this.task = task;
364          localDelegate.execute(this);
365        }
366      } finally {
367        // Important to null this out here - if we did *not* execute inline, we might still
368        // run() on the same thread that called execute() - such as in a thread pool, and think
369        // that it was happening inline. As a side benefit, avoids holding on to the Thread object
370        // longer than necessary.
371        submitting = null;
372      }
373    }
374
375    @SuppressWarnings("ShortCircuitBoolean")
376    @Override
377    public void run() {
378      Thread currentThread = Thread.currentThread();
379      if (currentThread != submitting) {
380        /*
381         * requireNonNull is safe because we set `task` before submitting this Runnable to an
382         * Executor, and we don't null it out until here.
383         */
384        Runnable localTask = requireNonNull(task);
385        task = null;
386        localTask.run();
387        return;
388      }
389      // Executor called reentrantly! Make sure that further calls don't overflow stack. Further
390      // reentrant calls will see that their current thread is the same as the one set in
391      // latestTaskQueue, and queue rather than calling execute() directly.
392      ThreadConfinedTaskQueue executingTaskQueue = new ThreadConfinedTaskQueue();
393      executingTaskQueue.thread = currentThread;
394      /*
395       * requireNonNull is safe because we don't null out `sequencer` except:
396       *
397       * - after the requireNonNull call below. (And this object has its Runnable.run override
398       *   called only once, just as it has its Executor.execute override called only once.)
399       *
400       * - if we return immediately from `execute` (in which case we never get here)
401       *
402       * - in the "reentrant submit" case of `execute` (in which case we must have started running a
403       *   user task -- which means that we already got past this code (or else we exited early
404       *   above))
405       */
406      // Unconditionally set; there is no risk of throwing away a queued task from another thread,
407      // because in order for the current task to run on this executor the previous task must have
408      // already started execution. Because each task on a TaskNonReentrantExecutor can only produce
409      // one execute() call to another instance from the same ExecutionSequencer, we know by
410      // induction that the task that launched this one must not have added any other runnables to
411      // that thread's queue, and thus we cannot be replacing a TaskAndThread object that would
412      // otherwise have another task queued on to it. Note the exception to this, cancellation, is
413      // specially handled in execute() - execute() calls triggered by cancellation are no-ops, and
414      // thus don't count.
415      requireNonNull(sequencer).latestTaskQueue = executingTaskQueue;
416      sequencer = null;
417      try {
418        // requireNonNull is safe, as discussed above.
419        Runnable localTask = requireNonNull(task);
420        task = null;
421        localTask.run();
422        // Now check if our task attempted to reentrantly execute the next task.
423        Runnable queuedTask;
424        Executor queuedExecutor;
425        // Intentionally using non-short-circuit operator
426        while ((queuedTask = executingTaskQueue.nextTask) != null
427            && (queuedExecutor = executingTaskQueue.nextExecutor) != null) {
428          executingTaskQueue.nextTask = null;
429          executingTaskQueue.nextExecutor = null;
430          queuedExecutor.execute(queuedTask);
431        }
432      } finally {
433        // Null out the thread field, so that we don't leak a reference to Thread, and so that
434        // future `thread == currentThread()` calls from this thread don't incorrectly queue instead
435        // of executing. Don't null out the latestTaskQueue field, because the work done here
436        // may have scheduled more operations on another thread, and if those operations then
437        // trigger reentrant calls that thread will have updated the latestTaskQueue field, and
438        // we'd be interfering with their operation.
439        executingTaskQueue.thread = null;
440      }
441    }
442
443    private boolean trySetStarted() {
444      return compareAndSet(NOT_RUN, STARTED);
445    }
446
447    private boolean trySetCancelled() {
448      return compareAndSet(NOT_RUN, CANCELLED);
449    }
450  }
451}