alexandroid/decompiled/jadx/sources/com/google/common/util/concurrent/InterruptibleTask.java
KaKi87 a47fad5135 📦 Add decompiled Alexa 2.2.730155.0 (jadx, apktool, Hermes strings)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 00:04:16 +01:00

173 lines
5.8 KiB
Java

package com.google.common.util.concurrent;
import com.amazon.alexa.ra.core.ui.markdown.parser.formatter.MarkdownConstants;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.j2objc.annotations.ReflectionSupport;
import defpackage.a$$ExternalSyntheticOutline0;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.AbstractOwnableSynchronizer;
import java.util.concurrent.locks.LockSupport;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
@ReflectionSupport(ReflectionSupport.Level.FULL)
@GwtCompatible(emulated = true)
/* loaded from: classes8.dex */
abstract class InterruptibleTask<T> extends AtomicReference<Runnable> implements Runnable {
private static final Runnable DONE;
private static final int MAX_BUSY_WAIT_SPINS = 1000;
private static final Runnable PARKED;
static {
DONE = new DoNothingRunnable();
PARKED = new DoNothingRunnable();
}
private void waitForInterrupt(Thread thread) {
Runnable runnable = get();
Blocker blocker = null;
boolean z = false;
int i = 0;
while (true) {
boolean z2 = runnable instanceof Blocker;
if (!z2 && runnable != PARKED) {
break;
}
if (z2) {
blocker = (Blocker) runnable;
}
i++;
if (i > 1000) {
Runnable runnable2 = PARKED;
if (runnable == runnable2 || compareAndSet(runnable, runnable2)) {
z = Thread.interrupted() || z;
LockSupport.park(blocker);
}
} else {
Thread.yield();
}
runnable = get();
}
if (z) {
thread.interrupt();
}
}
public abstract void afterRanInterruptiblyFailure(Throwable th);
public abstract void afterRanInterruptiblySuccess(@ParametricNullness T t);
public final void interruptTask() {
Runnable runnable = get();
if (runnable instanceof Thread) {
Blocker blocker = new Blocker();
blocker.setOwner(Thread.currentThread());
if (compareAndSet(runnable, blocker)) {
try {
((Thread) runnable).interrupt();
} finally {
if (getAndSet(DONE) == PARKED) {
LockSupport.unpark((Thread) runnable);
}
}
}
}
}
public abstract boolean isDone();
/* JADX WARN: Multi-variable type inference failed */
@Override // java.lang.Runnable
public final void run() {
Thread currentThread = Thread.currentThread();
Object obj = null;
if (compareAndSet(null, currentThread)) {
boolean isDone = isDone();
if (!isDone) {
try {
obj = runInterruptibly();
} catch (Throwable th) {
try {
Platform.restoreInterruptIfIsInterruptedException(th);
if (!compareAndSet(currentThread, DONE)) {
waitForInterrupt(currentThread);
}
if (isDone) {
return;
}
afterRanInterruptiblyFailure(th);
return;
} finally {
if (!compareAndSet(currentThread, DONE)) {
waitForInterrupt(currentThread);
}
if (!isDone) {
afterRanInterruptiblySuccess(NullnessCasts.uncheckedCastNullableTToT(null));
}
}
}
}
}
}
@ParametricNullness
public abstract T runInterruptibly() throws Exception;
public abstract String toPendingString();
@Override // java.util.concurrent.atomic.AtomicReference
public final String toString() {
String str;
Runnable runnable = get();
if (runnable == DONE) {
str = "running=[DONE]";
} else if (runnable instanceof Blocker) {
str = "running=[INTERRUPTED]";
} else if (runnable instanceof Thread) {
str = "running=[RUNNING ON " + ((Thread) runnable).getName() + MarkdownConstants.RIGHT_BRACKET;
} else {
str = "running=[NOT STARTED YET]";
}
StringBuilder m1m = a$$ExternalSyntheticOutline0.m1m(str, ", ");
m1m.append(toPendingString());
return m1m.toString();
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
@VisibleForTesting
public static final class Blocker extends AbstractOwnableSynchronizer implements Runnable {
private final InterruptibleTask<?> task;
private Blocker(InterruptibleTask<?> interruptibleTask) {
this.task = interruptibleTask;
}
/* JADX INFO: Access modifiers changed from: private */
public void setOwner(Thread thread) {
setExclusiveOwnerThread(thread);
}
@VisibleForTesting
public Thread getOwner() {
return getExclusiveOwnerThread();
}
public String toString() {
return this.task.toString();
}
@Override // java.lang.Runnable
public void run() {
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static final class DoNothingRunnable implements Runnable {
private DoNothingRunnable() {
}
@Override // java.lang.Runnable
public void run() {
}
}
}