Rabbit-R1/android (non root)/java/sources/com/google/common/util/concurrent/ExecutionSequencer.java
2024-05-21 17:08:36 -04:00

198 lines
8.1 KiB
Java

package com.google.common.util.concurrent;
import com.google.common.base.Preconditions;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.dex */
public final class ExecutionSequencer {
private final AtomicReference<ListenableFuture<Void>> ref = new AtomicReference<>(Futures.immediateVoidFuture());
private ThreadConfinedTaskQueue latestTaskQueue = new ThreadConfinedTaskQueue();
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes3.dex */
public enum RunningState {
NOT_RUN,
CANCELLED,
STARTED
}
private ExecutionSequencer() {
}
public static ExecutionSequencer create() {
return new ExecutionSequencer();
}
/* loaded from: classes3.dex */
private static final class ThreadConfinedTaskQueue {
@CheckForNull
Executor nextExecutor;
@CheckForNull
Runnable nextTask;
@CheckForNull
Thread thread;
private ThreadConfinedTaskQueue() {
}
}
public <T> ListenableFuture<T> submit(final Callable<T> callable, Executor executor) {
Preconditions.checkNotNull(callable);
Preconditions.checkNotNull(executor);
return submitAsync(new AsyncCallable<T>(this) { // from class: com.google.common.util.concurrent.ExecutionSequencer.1
@Override // com.google.common.util.concurrent.AsyncCallable
public ListenableFuture<T> call() throws Exception {
return Futures.immediateFuture(callable.call());
}
public String toString() {
return callable.toString();
}
}, executor);
}
public <T> ListenableFuture<T> submitAsync(final AsyncCallable<T> asyncCallable, Executor executor) {
Preconditions.checkNotNull(asyncCallable);
Preconditions.checkNotNull(executor);
final TaskNonReentrantExecutor taskNonReentrantExecutor = new TaskNonReentrantExecutor(executor, this);
AsyncCallable<T> asyncCallable2 = new AsyncCallable<T>(this) { // from class: com.google.common.util.concurrent.ExecutionSequencer.2
@Override // com.google.common.util.concurrent.AsyncCallable
public ListenableFuture<T> call() throws Exception {
if (!taskNonReentrantExecutor.trySetStarted()) {
return Futures.immediateCancelledFuture();
}
return asyncCallable.call();
}
public String toString() {
return asyncCallable.toString();
}
};
final SettableFuture create = SettableFuture.create();
final ListenableFuture<Void> andSet = this.ref.getAndSet(create);
final TrustedListenableFutureTask create2 = TrustedListenableFutureTask.create(asyncCallable2);
andSet.addListener(create2, taskNonReentrantExecutor);
final ListenableFuture<T> nonCancellationPropagating = Futures.nonCancellationPropagating(create2);
Runnable runnable = new Runnable() { // from class: com.google.common.util.concurrent.ExecutionSequencer$$ExternalSyntheticLambda0
@Override // java.lang.Runnable
public final void run() {
ExecutionSequencer.lambda$submitAsync$0(TrustedListenableFutureTask.this, create, andSet, nonCancellationPropagating, taskNonReentrantExecutor);
}
};
nonCancellationPropagating.addListener(runnable, MoreExecutors.directExecutor());
create2.addListener(runnable, MoreExecutors.directExecutor());
return nonCancellationPropagating;
}
/* JADX INFO: Access modifiers changed from: package-private */
public static /* synthetic */ void lambda$submitAsync$0(TrustedListenableFutureTask trustedListenableFutureTask, SettableFuture settableFuture, ListenableFuture listenableFuture, ListenableFuture listenableFuture2, TaskNonReentrantExecutor taskNonReentrantExecutor) {
if (trustedListenableFutureTask.isDone()) {
settableFuture.setFuture(listenableFuture);
} else if (listenableFuture2.isCancelled() && taskNonReentrantExecutor.trySetCancelled()) {
trustedListenableFutureTask.cancel(false);
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes3.dex */
public static final class TaskNonReentrantExecutor extends AtomicReference<RunningState> implements Executor, Runnable {
@CheckForNull
Executor delegate;
@CheckForNull
ExecutionSequencer sequencer;
@CheckForNull
Thread submitting;
@CheckForNull
Runnable task;
private TaskNonReentrantExecutor(Executor executor, ExecutionSequencer executionSequencer) {
super(RunningState.NOT_RUN);
this.delegate = executor;
this.sequencer = executionSequencer;
}
@Override // java.util.concurrent.Executor
public void execute(Runnable runnable) {
if (get() == RunningState.CANCELLED) {
this.delegate = null;
this.sequencer = null;
return;
}
this.submitting = Thread.currentThread();
try {
ThreadConfinedTaskQueue threadConfinedTaskQueue = ((ExecutionSequencer) Objects.requireNonNull(this.sequencer)).latestTaskQueue;
if (threadConfinedTaskQueue.thread == this.submitting) {
this.sequencer = null;
Preconditions.checkState(threadConfinedTaskQueue.nextTask == null);
threadConfinedTaskQueue.nextTask = runnable;
threadConfinedTaskQueue.nextExecutor = (Executor) Objects.requireNonNull(this.delegate);
this.delegate = null;
} else {
Executor executor = (Executor) Objects.requireNonNull(this.delegate);
this.delegate = null;
this.task = runnable;
executor.execute(this);
}
} finally {
this.submitting = null;
}
}
/* JADX WARN: Multi-variable type inference failed */
@Override // java.lang.Runnable
public void run() {
Executor executor;
Thread currentThread = Thread.currentThread();
Thread thread = null;
Object[] objArr = 0;
if (currentThread != this.submitting) {
Runnable runnable = (Runnable) Objects.requireNonNull(this.task);
this.task = null;
runnable.run();
return;
}
ThreadConfinedTaskQueue threadConfinedTaskQueue = new ThreadConfinedTaskQueue();
threadConfinedTaskQueue.thread = currentThread;
((ExecutionSequencer) Objects.requireNonNull(this.sequencer)).latestTaskQueue = threadConfinedTaskQueue;
this.sequencer = null;
try {
Runnable runnable2 = (Runnable) Objects.requireNonNull(this.task);
this.task = null;
runnable2.run();
while (true) {
Runnable runnable3 = threadConfinedTaskQueue.nextTask;
if (runnable3 == null || (executor = threadConfinedTaskQueue.nextExecutor) == null) {
break;
}
threadConfinedTaskQueue.nextTask = null;
threadConfinedTaskQueue.nextExecutor = null;
executor.execute(runnable3);
}
} finally {
threadConfinedTaskQueue.thread = null;
}
}
/* JADX INFO: Access modifiers changed from: private */
public boolean trySetStarted() {
return compareAndSet(RunningState.NOT_RUN, RunningState.STARTED);
}
/* JADX INFO: Access modifiers changed from: private */
public boolean trySetCancelled() {
return compareAndSet(RunningState.NOT_RUN, RunningState.CANCELLED);
}
}
}