Rabbit-R1/android (non root)/java/sources/androidx/concurrent/futures/AbstractResolvableFuture.java
2024-05-21 17:08:36 -04:00

726 lines
28 KiB
Java

package androidx.concurrent.futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
/* loaded from: classes.dex */
public abstract class AbstractResolvableFuture<V> implements ListenableFuture<V> {
static final AtomicHelper ATOMIC_HELPER;
private static final Object NULL;
private static final long SPIN_THRESHOLD_NANOS = 1000;
volatile Listener listeners;
volatile Object value;
volatile Waiter waiters;
static final boolean GENERATE_CANCELLATION_CAUSES = Boolean.parseBoolean(System.getProperty("guava.concurrent.generate_cancellation_cause", "false"));
private static final Logger log = Logger.getLogger(AbstractResolvableFuture.class.getName());
protected void afterDone() {
}
protected void interruptTask() {
}
static {
AtomicHelper synchronizedHelper;
try {
synchronizedHelper = new SafeAtomicHelper(AtomicReferenceFieldUpdater.newUpdater(Waiter.class, Thread.class, "thread"), AtomicReferenceFieldUpdater.newUpdater(Waiter.class, Waiter.class, "next"), AtomicReferenceFieldUpdater.newUpdater(AbstractResolvableFuture.class, Waiter.class, "waiters"), AtomicReferenceFieldUpdater.newUpdater(AbstractResolvableFuture.class, Listener.class, "listeners"), AtomicReferenceFieldUpdater.newUpdater(AbstractResolvableFuture.class, Object.class, "value"));
th = null;
} catch (Throwable th) {
th = th;
synchronizedHelper = new SynchronizedHelper();
}
ATOMIC_HELPER = synchronizedHelper;
if (th != null) {
log.log(Level.SEVERE, "SafeAtomicHelper is broken!", th);
}
NULL = new Object();
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Waiter {
static final Waiter TOMBSTONE = new Waiter(false);
volatile Waiter next;
volatile Thread thread;
Waiter(boolean z) {
}
Waiter() {
AbstractResolvableFuture.ATOMIC_HELPER.putThread(this, Thread.currentThread());
}
void setNext(Waiter waiter) {
AbstractResolvableFuture.ATOMIC_HELPER.putNext(this, waiter);
}
void unpark() {
Thread thread = this.thread;
if (thread != null) {
this.thread = null;
LockSupport.unpark(thread);
}
}
}
private void removeWaiter(Waiter waiter) {
waiter.thread = null;
while (true) {
Waiter waiter2 = this.waiters;
if (waiter2 == Waiter.TOMBSTONE) {
return;
}
Waiter waiter3 = null;
while (waiter2 != null) {
Waiter waiter4 = waiter2.next;
if (waiter2.thread != null) {
waiter3 = waiter2;
} else if (waiter3 != null) {
waiter3.next = waiter4;
if (waiter3.thread == null) {
break;
}
} else if (!ATOMIC_HELPER.casWaiters(this, waiter2, waiter4)) {
break;
}
waiter2 = waiter4;
}
return;
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Listener {
static final Listener TOMBSTONE = new Listener(null, null);
final Executor executor;
Listener next;
final Runnable task;
Listener(Runnable runnable, Executor executor) {
this.task = runnable;
this.executor = executor;
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Failure {
static final Failure FALLBACK_INSTANCE = new Failure(new Throwable("Failure occurred while trying to finish a future.") { // from class: androidx.concurrent.futures.AbstractResolvableFuture.Failure.1
@Override // java.lang.Throwable
public synchronized Throwable fillInStackTrace() {
return this;
}
});
final Throwable exception;
Failure(Throwable th) {
this.exception = (Throwable) AbstractResolvableFuture.checkNotNull(th);
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class Cancellation {
static final Cancellation CAUSELESS_CANCELLED;
static final Cancellation CAUSELESS_INTERRUPTED;
final Throwable cause;
final boolean wasInterrupted;
static {
if (AbstractResolvableFuture.GENERATE_CANCELLATION_CAUSES) {
CAUSELESS_CANCELLED = null;
CAUSELESS_INTERRUPTED = null;
} else {
CAUSELESS_CANCELLED = new Cancellation(false, null);
CAUSELESS_INTERRUPTED = new Cancellation(true, null);
}
}
Cancellation(boolean z, Throwable th) {
this.wasInterrupted = z;
this.cause = th;
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static final class SetFuture<V> implements Runnable {
final ListenableFuture<? extends V> future;
final AbstractResolvableFuture<V> owner;
SetFuture(AbstractResolvableFuture<V> abstractResolvableFuture, ListenableFuture<? extends V> listenableFuture) {
this.owner = abstractResolvableFuture;
this.future = listenableFuture;
}
@Override // java.lang.Runnable
public void run() {
if (this.owner.value != this) {
return;
}
if (AbstractResolvableFuture.ATOMIC_HELPER.casValue(this.owner, this, AbstractResolvableFuture.getFutureValue(this.future))) {
AbstractResolvableFuture.complete(this.owner);
}
}
}
@Override // java.util.concurrent.Future
public final V get(long j, TimeUnit timeUnit) throws InterruptedException, TimeoutException, ExecutionException {
long nanos = timeUnit.toNanos(j);
if (Thread.interrupted()) {
throw new InterruptedException();
}
Object obj = this.value;
if ((obj != null) & (!(obj instanceof SetFuture))) {
return getDoneValue(obj);
}
long nanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
if (nanos >= 1000) {
Waiter waiter = this.waiters;
if (waiter != Waiter.TOMBSTONE) {
Waiter waiter2 = new Waiter();
do {
waiter2.setNext(waiter);
if (ATOMIC_HELPER.casWaiters(this, waiter, waiter2)) {
do {
LockSupport.parkNanos(this, nanos);
if (Thread.interrupted()) {
removeWaiter(waiter2);
throw new InterruptedException();
}
Object obj2 = this.value;
if ((obj2 != null) & (!(obj2 instanceof SetFuture))) {
return getDoneValue(obj2);
}
nanos = nanoTime - System.nanoTime();
} while (nanos >= 1000);
removeWaiter(waiter2);
} else {
waiter = this.waiters;
}
} while (waiter != Waiter.TOMBSTONE);
}
return getDoneValue(this.value);
}
while (nanos > 0) {
Object obj3 = this.value;
if ((obj3 != null) & (!(obj3 instanceof SetFuture))) {
return getDoneValue(obj3);
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
nanos = nanoTime - System.nanoTime();
}
String abstractResolvableFuture = toString();
String lowerCase = timeUnit.toString().toLowerCase(Locale.ROOT);
String str = "Waited " + j + " " + timeUnit.toString().toLowerCase(Locale.ROOT);
if (nanos + 1000 < 0) {
String str2 = str + " (plus ";
long j2 = -nanos;
long convert = timeUnit.convert(j2, TimeUnit.NANOSECONDS);
long nanos2 = j2 - timeUnit.toNanos(convert);
boolean z = convert == 0 || nanos2 > 1000;
if (convert > 0) {
String str3 = str2 + convert + " " + lowerCase;
if (z) {
str3 = str3 + ",";
}
str2 = str3 + " ";
}
if (z) {
str2 = str2 + nanos2 + " nanoseconds ";
}
str = str2 + "delay)";
}
if (isDone()) {
throw new TimeoutException(str + " but future completed as timeout expired");
}
throw new TimeoutException(str + " for " + abstractResolvableFuture);
}
@Override // java.util.concurrent.Future
public final V get() throws InterruptedException, ExecutionException {
Object obj;
if (Thread.interrupted()) {
throw new InterruptedException();
}
Object obj2 = this.value;
if ((obj2 != null) & (!(obj2 instanceof SetFuture))) {
return getDoneValue(obj2);
}
Waiter waiter = this.waiters;
if (waiter != Waiter.TOMBSTONE) {
Waiter waiter2 = new Waiter();
do {
waiter2.setNext(waiter);
if (ATOMIC_HELPER.casWaiters(this, waiter, waiter2)) {
do {
LockSupport.park(this);
if (Thread.interrupted()) {
removeWaiter(waiter2);
throw new InterruptedException();
}
obj = this.value;
} while (!((obj != null) & (!(obj instanceof SetFuture))));
return getDoneValue(obj);
}
waiter = this.waiters;
} while (waiter != Waiter.TOMBSTONE);
}
return getDoneValue(this.value);
}
/* JADX WARN: Multi-variable type inference failed */
private V getDoneValue(Object obj) throws ExecutionException {
if (obj instanceof Cancellation) {
throw cancellationExceptionWithCause("Task was cancelled.", ((Cancellation) obj).cause);
}
if (obj instanceof Failure) {
throw new ExecutionException(((Failure) obj).exception);
}
if (obj == NULL) {
return null;
}
return obj;
}
@Override // java.util.concurrent.Future
public final boolean isDone() {
return (!(r2 instanceof SetFuture)) & (this.value != null);
}
@Override // java.util.concurrent.Future
public final boolean isCancelled() {
return this.value instanceof Cancellation;
}
@Override // java.util.concurrent.Future
public final boolean cancel(boolean z) {
Cancellation cancellation;
Object obj = this.value;
if (!(obj == null) && !(obj instanceof SetFuture)) {
return false;
}
if (GENERATE_CANCELLATION_CAUSES) {
cancellation = new Cancellation(z, new CancellationException("Future.cancel() was called."));
} else if (z) {
cancellation = Cancellation.CAUSELESS_INTERRUPTED;
} else {
cancellation = Cancellation.CAUSELESS_CANCELLED;
}
boolean z2 = false;
while (true) {
if (ATOMIC_HELPER.casValue(this, obj, cancellation)) {
if (z) {
this.interruptTask();
}
complete(this);
if (!(obj instanceof SetFuture)) {
return true;
}
ListenableFuture<? extends V> listenableFuture = ((SetFuture) obj).future;
if (listenableFuture instanceof AbstractResolvableFuture) {
this = (AbstractResolvableFuture) listenableFuture;
obj = this.value;
if (!(obj == null) && !(obj instanceof SetFuture)) {
return true;
}
z2 = true;
} else {
listenableFuture.cancel(z);
return true;
}
} else {
obj = this.value;
if (!(obj instanceof SetFuture)) {
return z2;
}
}
}
}
protected final boolean wasInterrupted() {
Object obj = this.value;
return (obj instanceof Cancellation) && ((Cancellation) obj).wasInterrupted;
}
@Override // com.google.common.util.concurrent.ListenableFuture
public final void addListener(Runnable runnable, Executor executor) {
checkNotNull(runnable);
checkNotNull(executor);
Listener listener = this.listeners;
if (listener != Listener.TOMBSTONE) {
Listener listener2 = new Listener(runnable, executor);
do {
listener2.next = listener;
if (ATOMIC_HELPER.casListeners(this, listener, listener2)) {
return;
} else {
listener = this.listeners;
}
} while (listener != Listener.TOMBSTONE);
}
executeListener(runnable, executor);
}
/* JADX INFO: Access modifiers changed from: protected */
public boolean set(V v) {
if (v == null) {
v = (V) NULL;
}
if (!ATOMIC_HELPER.casValue(this, null, v)) {
return false;
}
complete(this);
return true;
}
/* JADX INFO: Access modifiers changed from: protected */
public boolean setException(Throwable th) {
if (!ATOMIC_HELPER.casValue(this, null, new Failure((Throwable) checkNotNull(th)))) {
return false;
}
complete(this);
return true;
}
/* JADX INFO: Access modifiers changed from: protected */
public boolean setFuture(ListenableFuture<? extends V> listenableFuture) {
Failure failure;
checkNotNull(listenableFuture);
Object obj = this.value;
if (obj == null) {
if (listenableFuture.isDone()) {
if (!ATOMIC_HELPER.casValue(this, null, getFutureValue(listenableFuture))) {
return false;
}
complete(this);
return true;
}
SetFuture setFuture = new SetFuture(this, listenableFuture);
if (ATOMIC_HELPER.casValue(this, null, setFuture)) {
try {
listenableFuture.addListener(setFuture, DirectExecutor.INSTANCE);
} catch (Throwable th) {
try {
failure = new Failure(th);
} catch (Throwable unused) {
failure = Failure.FALLBACK_INSTANCE;
}
ATOMIC_HELPER.casValue(this, setFuture, failure);
}
return true;
}
obj = this.value;
}
if (obj instanceof Cancellation) {
listenableFuture.cancel(((Cancellation) obj).wasInterrupted);
}
return false;
}
static Object getFutureValue(ListenableFuture<?> listenableFuture) {
if (listenableFuture instanceof AbstractResolvableFuture) {
Object obj = ((AbstractResolvableFuture) listenableFuture).value;
if (!(obj instanceof Cancellation)) {
return obj;
}
Cancellation cancellation = (Cancellation) obj;
return cancellation.wasInterrupted ? cancellation.cause != null ? new Cancellation(false, cancellation.cause) : Cancellation.CAUSELESS_CANCELLED : obj;
}
boolean isCancelled = listenableFuture.isCancelled();
if ((!GENERATE_CANCELLATION_CAUSES) & isCancelled) {
return Cancellation.CAUSELESS_CANCELLED;
}
try {
Object uninterruptibly = getUninterruptibly(listenableFuture);
return uninterruptibly == null ? NULL : uninterruptibly;
} catch (CancellationException e) {
if (!isCancelled) {
return new Failure(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: " + listenableFuture, e));
}
return new Cancellation(false, e);
} catch (ExecutionException e2) {
return new Failure(e2.getCause());
} catch (Throwable th) {
return new Failure(th);
}
}
static <V> V getUninterruptibly(Future<V> future) throws ExecutionException {
V v;
boolean z = false;
while (true) {
try {
v = future.get();
break;
} catch (InterruptedException unused) {
z = true;
} catch (Throwable th) {
if (z) {
Thread.currentThread().interrupt();
}
throw th;
}
}
if (z) {
Thread.currentThread().interrupt();
}
return v;
}
static void complete(AbstractResolvableFuture<?> abstractResolvableFuture) {
Listener listener = null;
while (true) {
abstractResolvableFuture.releaseWaiters();
abstractResolvableFuture.afterDone();
Listener clearListeners = abstractResolvableFuture.clearListeners(listener);
while (clearListeners != null) {
listener = clearListeners.next;
Runnable runnable = clearListeners.task;
if (runnable instanceof SetFuture) {
SetFuture setFuture = (SetFuture) runnable;
abstractResolvableFuture = setFuture.owner;
if (abstractResolvableFuture.value == setFuture) {
if (ATOMIC_HELPER.casValue(abstractResolvableFuture, setFuture, getFutureValue(setFuture.future))) {
break;
}
} else {
continue;
}
} else {
executeListener(runnable, clearListeners.executor);
}
clearListeners = listener;
}
return;
}
}
final void maybePropagateCancellationTo(Future<?> future) {
if ((future != null) && isCancelled()) {
future.cancel(wasInterrupted());
}
}
private void releaseWaiters() {
Waiter waiter;
do {
waiter = this.waiters;
} while (!ATOMIC_HELPER.casWaiters(this, waiter, Waiter.TOMBSTONE));
while (waiter != null) {
waiter.unpark();
waiter = waiter.next;
}
}
private Listener clearListeners(Listener listener) {
Listener listener2;
do {
listener2 = this.listeners;
} while (!ATOMIC_HELPER.casListeners(this, listener2, Listener.TOMBSTONE));
while (true) {
Listener listener3 = listener;
listener = listener2;
if (listener == null) {
return listener3;
}
listener2 = listener.next;
listener.next = listener3;
}
}
public String toString() {
String str;
StringBuilder append = new StringBuilder().append(super.toString()).append("[status=");
if (isCancelled()) {
append.append("CANCELLED");
} else if (isDone()) {
addDoneString(append);
} else {
try {
str = pendingToString();
} catch (RuntimeException e) {
str = "Exception thrown from implementation: " + e.getClass();
}
if (str != null && !str.isEmpty()) {
append.append("PENDING, info=[").append(str).append("]");
} else if (isDone()) {
addDoneString(append);
} else {
append.append("PENDING");
}
}
return append.append("]").toString();
}
/* JADX WARN: Multi-variable type inference failed */
protected String pendingToString() {
Object obj = this.value;
if (obj instanceof SetFuture) {
return "setFuture=[" + userObjectToString(((SetFuture) obj).future) + "]";
}
if (this instanceof ScheduledFuture) {
return "remaining delay=[" + ((ScheduledFuture) this).getDelay(TimeUnit.MILLISECONDS) + " ms]";
}
return null;
}
private void addDoneString(StringBuilder sb) {
try {
sb.append("SUCCESS, result=[").append(userObjectToString(getUninterruptibly(this))).append("]");
} catch (CancellationException unused) {
sb.append("CANCELLED");
} catch (RuntimeException e) {
sb.append("UNKNOWN, cause=[").append(e.getClass()).append(" thrown from get()]");
} catch (ExecutionException e2) {
sb.append("FAILURE, cause=[").append(e2.getCause()).append("]");
}
}
private String userObjectToString(Object obj) {
return obj == this ? "this future" : String.valueOf(obj);
}
private static void executeListener(Runnable runnable, Executor executor) {
try {
executor.execute(runnable);
} catch (RuntimeException e) {
log.log(Level.SEVERE, "RuntimeException while executing runnable " + runnable + " with executor " + executor, (Throwable) e);
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes.dex */
public static abstract class AtomicHelper {
abstract boolean casListeners(AbstractResolvableFuture<?> abstractResolvableFuture, Listener listener, Listener listener2);
abstract boolean casValue(AbstractResolvableFuture<?> abstractResolvableFuture, Object obj, Object obj2);
abstract boolean casWaiters(AbstractResolvableFuture<?> abstractResolvableFuture, Waiter waiter, Waiter waiter2);
abstract void putNext(Waiter waiter, Waiter waiter2);
abstract void putThread(Waiter waiter, Thread thread);
private AtomicHelper() {
}
}
/* loaded from: classes.dex */
private static final class SafeAtomicHelper extends AtomicHelper {
final AtomicReferenceFieldUpdater<AbstractResolvableFuture, Listener> listenersUpdater;
final AtomicReferenceFieldUpdater<AbstractResolvableFuture, Object> valueUpdater;
final AtomicReferenceFieldUpdater<Waiter, Waiter> waiterNextUpdater;
final AtomicReferenceFieldUpdater<Waiter, Thread> waiterThreadUpdater;
final AtomicReferenceFieldUpdater<AbstractResolvableFuture, Waiter> waitersUpdater;
SafeAtomicHelper(AtomicReferenceFieldUpdater<Waiter, Thread> atomicReferenceFieldUpdater, AtomicReferenceFieldUpdater<Waiter, Waiter> atomicReferenceFieldUpdater2, AtomicReferenceFieldUpdater<AbstractResolvableFuture, Waiter> atomicReferenceFieldUpdater3, AtomicReferenceFieldUpdater<AbstractResolvableFuture, Listener> atomicReferenceFieldUpdater4, AtomicReferenceFieldUpdater<AbstractResolvableFuture, Object> atomicReferenceFieldUpdater5) {
super();
this.waiterThreadUpdater = atomicReferenceFieldUpdater;
this.waiterNextUpdater = atomicReferenceFieldUpdater2;
this.waitersUpdater = atomicReferenceFieldUpdater3;
this.listenersUpdater = atomicReferenceFieldUpdater4;
this.valueUpdater = atomicReferenceFieldUpdater5;
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
void putThread(Waiter waiter, Thread thread) {
this.waiterThreadUpdater.lazySet(waiter, thread);
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
void putNext(Waiter waiter, Waiter waiter2) {
this.waiterNextUpdater.lazySet(waiter, waiter2);
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casWaiters(AbstractResolvableFuture<?> abstractResolvableFuture, Waiter waiter, Waiter waiter2) {
return this.waitersUpdater.compareAndSet(abstractResolvableFuture, waiter, waiter2);
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casListeners(AbstractResolvableFuture<?> abstractResolvableFuture, Listener listener, Listener listener2) {
return this.listenersUpdater.compareAndSet(abstractResolvableFuture, listener, listener2);
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casValue(AbstractResolvableFuture<?> abstractResolvableFuture, Object obj, Object obj2) {
return this.valueUpdater.compareAndSet(abstractResolvableFuture, obj, obj2);
}
}
/* loaded from: classes.dex */
private static final class SynchronizedHelper extends AtomicHelper {
SynchronizedHelper() {
super();
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
void putThread(Waiter waiter, Thread thread) {
waiter.thread = thread;
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
void putNext(Waiter waiter, Waiter waiter2) {
waiter.next = waiter2;
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casWaiters(AbstractResolvableFuture<?> abstractResolvableFuture, Waiter waiter, Waiter waiter2) {
synchronized (abstractResolvableFuture) {
if (abstractResolvableFuture.waiters != waiter) {
return false;
}
abstractResolvableFuture.waiters = waiter2;
return true;
}
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casListeners(AbstractResolvableFuture<?> abstractResolvableFuture, Listener listener, Listener listener2) {
synchronized (abstractResolvableFuture) {
if (abstractResolvableFuture.listeners != listener) {
return false;
}
abstractResolvableFuture.listeners = listener2;
return true;
}
}
@Override // androidx.concurrent.futures.AbstractResolvableFuture.AtomicHelper
boolean casValue(AbstractResolvableFuture<?> abstractResolvableFuture, Object obj, Object obj2) {
synchronized (abstractResolvableFuture) {
if (abstractResolvableFuture.value != obj) {
return false;
}
abstractResolvableFuture.value = obj2;
return true;
}
}
}
private static CancellationException cancellationExceptionWithCause(String str, Throwable th) {
CancellationException cancellationException = new CancellationException(str);
cancellationException.initCause(th);
return cancellationException;
}
static <T> T checkNotNull(T t) {
t.getClass();
return t;
}
}